1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * SMB2 version specific operations
4 *
5 * Copyright (c) 2012, Jeff Layton <jlayton@redhat.com>
6 */
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>
13 #include <linux/sort.h>
14 #include <crypto/aead.h>
15 #include <linux/fiemap.h>
16 #include <linux/folio_queue.h>
17 #include <uapi/linux/magic.h>
18 #include "cifsfs.h"
19 #include "cifsglob.h"
20 #include "cifsproto.h"
21 #include "smb2proto.h"
22 #include "smb2pdu.h"
23 #include "cifs_debug.h"
24 #include "cifs_unicode.h"
25 #include "../common/smb2status.h"
26 #include "smb2glob.h"
27 #include "cifs_ioctl.h"
28 #include "smbdirect.h"
29 #include "fscache.h"
30 #include "fs_context.h"
31 #include "cached_dir.h"
32 #include "reparse.h"
33
34 /* Change credits for different ops and return the total number of credits */
35 static int
change_conf(struct TCP_Server_Info * server)36 change_conf(struct TCP_Server_Info *server)
37 {
38 server->credits += server->echo_credits + server->oplock_credits;
39 if (server->credits > server->max_credits)
40 server->credits = server->max_credits;
41 server->oplock_credits = server->echo_credits = 0;
42 switch (server->credits) {
43 case 0:
44 return 0;
45 case 1:
46 server->echoes = false;
47 server->oplocks = false;
48 break;
49 case 2:
50 server->echoes = true;
51 server->oplocks = false;
52 server->echo_credits = 1;
53 break;
54 default:
55 server->echoes = true;
56 if (enable_oplocks) {
57 server->oplocks = true;
58 server->oplock_credits = 1;
59 } else
60 server->oplocks = false;
61
62 server->echo_credits = 1;
63 }
64 server->credits -= server->echo_credits + server->oplock_credits;
65 return server->credits + server->echo_credits + server->oplock_credits;
66 }
67
68 static void
smb2_add_credits(struct TCP_Server_Info * server,struct cifs_credits * credits,const int optype)69 smb2_add_credits(struct TCP_Server_Info *server,
70 struct cifs_credits *credits, const int optype)
71 {
72 int *val, rc = -1;
73 int scredits, in_flight;
74 unsigned int add = credits->value;
75 unsigned int instance = credits->instance;
76 bool reconnect_detected = false;
77 bool reconnect_with_invalid_credits = false;
78
79 spin_lock(&server->req_lock);
80 val = server->ops->get_credits_field(server, optype);
81
82 /* eg found case where write overlapping reconnect messed up credits */
83 if (((optype & CIFS_OP_MASK) == CIFS_NEG_OP) && (*val != 0))
84 reconnect_with_invalid_credits = true;
85
86 if ((instance == 0) || (instance == server->reconnect_instance))
87 *val += add;
88 else
89 reconnect_detected = true;
90
91 if (*val > 65000) {
92 *val = 65000; /* Don't get near 64K credits, avoid srv bugs */
93 pr_warn_once("server overflowed SMB3 credits\n");
94 trace_smb3_overflow_credits(server->current_mid,
95 server->conn_id, server->hostname, *val,
96 add, server->in_flight);
97 }
98 if (credits->in_flight_check > 1) {
99 pr_warn_once("rreq R=%08x[%x] Credits not in flight\n",
100 credits->rreq_debug_id, credits->rreq_debug_index);
101 } else {
102 credits->in_flight_check = 2;
103 }
104 if (WARN_ON_ONCE(server->in_flight == 0)) {
105 pr_warn_once("rreq R=%08x[%x] Zero in_flight\n",
106 credits->rreq_debug_id, credits->rreq_debug_index);
107 trace_smb3_rw_credits(credits->rreq_debug_id,
108 credits->rreq_debug_index,
109 credits->value,
110 server->credits, server->in_flight, 0,
111 cifs_trace_rw_credits_zero_in_flight);
112 }
113 server->in_flight--;
114 if (server->in_flight == 0 &&
115 ((optype & CIFS_OP_MASK) != CIFS_NEG_OP) &&
116 ((optype & CIFS_OP_MASK) != CIFS_SESS_OP))
117 rc = change_conf(server);
118 /*
119 * Sometimes server returns 0 credits on oplock break ack - we need to
120 * rebalance credits in this case.
121 */
122 else if (server->in_flight > 0 && server->oplock_credits == 0 &&
123 server->oplocks) {
124 if (server->credits > 1) {
125 server->credits--;
126 server->oplock_credits++;
127 }
128 } else if ((server->in_flight > 0) && (server->oplock_credits > 3) &&
129 ((optype & CIFS_OP_MASK) == CIFS_OBREAK_OP))
130 /* if now have too many oplock credits, rebalance so don't starve normal ops */
131 change_conf(server);
132
133 scredits = *val;
134 in_flight = server->in_flight;
135 spin_unlock(&server->req_lock);
136 wake_up(&server->request_q);
137
138 if (reconnect_detected) {
139 trace_smb3_reconnect_detected(server->current_mid,
140 server->conn_id, server->hostname, scredits, add, in_flight);
141
142 cifs_dbg(FYI, "trying to put %d credits from the old server instance %d\n",
143 add, instance);
144 }
145
146 if (reconnect_with_invalid_credits) {
147 trace_smb3_reconnect_with_invalid_credits(server->current_mid,
148 server->conn_id, server->hostname, scredits, add, in_flight);
149 cifs_dbg(FYI, "Negotiate operation when server credits is non-zero. Optype: %d, server credits: %d, credits added: %d\n",
150 optype, scredits, add);
151 }
152
153 spin_lock(&server->srv_lock);
154 if (server->tcpStatus == CifsNeedReconnect
155 || server->tcpStatus == CifsExiting) {
156 spin_unlock(&server->srv_lock);
157 return;
158 }
159 spin_unlock(&server->srv_lock);
160
161 switch (rc) {
162 case -1:
163 /* change_conf hasn't been executed */
164 break;
165 case 0:
166 cifs_server_dbg(VFS, "Possible client or server bug - zero credits\n");
167 break;
168 case 1:
169 cifs_server_dbg(VFS, "disabling echoes and oplocks\n");
170 break;
171 case 2:
172 cifs_dbg(FYI, "disabling oplocks\n");
173 break;
174 default:
175 /* change_conf rebalanced credits for different types */
176 break;
177 }
178
179 trace_smb3_add_credits(server->current_mid,
180 server->conn_id, server->hostname, scredits, add, in_flight);
181 cifs_dbg(FYI, "%s: added %u credits total=%d\n", __func__, add, scredits);
182 }
183
184 static void
smb2_set_credits(struct TCP_Server_Info * server,const int val)185 smb2_set_credits(struct TCP_Server_Info *server, const int val)
186 {
187 int scredits, in_flight;
188
189 spin_lock(&server->req_lock);
190 server->credits = val;
191 if (val == 1) {
192 server->reconnect_instance++;
193 /*
194 * ChannelSequence updated for all channels in primary channel so that consistent
195 * across SMB3 requests sent on any channel. See MS-SMB2 3.2.4.1 and 3.2.7.1
196 */
197 if (SERVER_IS_CHAN(server))
198 server->primary_server->channel_sequence_num++;
199 else
200 server->channel_sequence_num++;
201 }
202 scredits = server->credits;
203 in_flight = server->in_flight;
204 spin_unlock(&server->req_lock);
205
206 trace_smb3_set_credits(server->current_mid,
207 server->conn_id, server->hostname, scredits, val, in_flight);
208 cifs_dbg(FYI, "%s: set %u credits\n", __func__, val);
209
210 /* don't log while holding the lock */
211 if (val == 1)
212 cifs_dbg(FYI, "set credits to 1 due to smb2 reconnect\n");
213 }
214
215 static int *
smb2_get_credits_field(struct TCP_Server_Info * server,const int optype)216 smb2_get_credits_field(struct TCP_Server_Info *server, const int optype)
217 {
218 switch (optype) {
219 case CIFS_ECHO_OP:
220 return &server->echo_credits;
221 case CIFS_OBREAK_OP:
222 return &server->oplock_credits;
223 default:
224 return &server->credits;
225 }
226 }
227
228 static unsigned int
smb2_get_credits(struct mid_q_entry * mid)229 smb2_get_credits(struct mid_q_entry *mid)
230 {
231 return mid->credits_received;
232 }
233
234 static int
smb2_wait_mtu_credits(struct TCP_Server_Info * server,size_t size,size_t * num,struct cifs_credits * credits)235 smb2_wait_mtu_credits(struct TCP_Server_Info *server, size_t size,
236 size_t *num, struct cifs_credits *credits)
237 {
238 int rc = 0;
239 unsigned int scredits, in_flight;
240
241 spin_lock(&server->req_lock);
242 while (1) {
243 spin_unlock(&server->req_lock);
244
245 spin_lock(&server->srv_lock);
246 if (server->tcpStatus == CifsExiting) {
247 spin_unlock(&server->srv_lock);
248 return -ENOENT;
249 }
250 spin_unlock(&server->srv_lock);
251
252 spin_lock(&server->req_lock);
253 if (server->credits <= 0) {
254 spin_unlock(&server->req_lock);
255 cifs_num_waiters_inc(server);
256 rc = wait_event_killable(server->request_q,
257 has_credits(server, &server->credits, 1));
258 cifs_num_waiters_dec(server);
259 if (rc)
260 return rc;
261 spin_lock(&server->req_lock);
262 } else {
263 scredits = server->credits;
264 /* can deadlock with reopen */
265 if (scredits <= 8) {
266 *num = SMB2_MAX_BUFFER_SIZE;
267 credits->value = 0;
268 credits->instance = 0;
269 break;
270 }
271
272 /* leave some credits for reopen and other ops */
273 scredits -= 8;
274 *num = min_t(unsigned int, size,
275 scredits * SMB2_MAX_BUFFER_SIZE);
276
277 credits->value =
278 DIV_ROUND_UP(*num, SMB2_MAX_BUFFER_SIZE);
279 credits->instance = server->reconnect_instance;
280 server->credits -= credits->value;
281 server->in_flight++;
282 if (server->in_flight > server->max_in_flight)
283 server->max_in_flight = server->in_flight;
284 break;
285 }
286 }
287 scredits = server->credits;
288 in_flight = server->in_flight;
289 spin_unlock(&server->req_lock);
290
291 trace_smb3_wait_credits(server->current_mid,
292 server->conn_id, server->hostname, scredits, -(credits->value), in_flight);
293 cifs_dbg(FYI, "%s: removed %u credits total=%d\n",
294 __func__, credits->value, scredits);
295
296 return rc;
297 }
298
299 static int
smb2_adjust_credits(struct TCP_Server_Info * server,struct cifs_io_subrequest * subreq,unsigned int trace)300 smb2_adjust_credits(struct TCP_Server_Info *server,
301 struct cifs_io_subrequest *subreq,
302 unsigned int /*enum smb3_rw_credits_trace*/ trace)
303 {
304 struct cifs_credits *credits = &subreq->credits;
305 int new_val = DIV_ROUND_UP(subreq->subreq.len - subreq->subreq.transferred,
306 SMB2_MAX_BUFFER_SIZE);
307 int scredits, in_flight;
308
309 if (!credits->value || credits->value == new_val)
310 return 0;
311
312 if (credits->value < new_val) {
313 trace_smb3_rw_credits(subreq->rreq->debug_id,
314 subreq->subreq.debug_index,
315 credits->value,
316 server->credits, server->in_flight,
317 new_val - credits->value,
318 cifs_trace_rw_credits_no_adjust_up);
319 trace_smb3_too_many_credits(server->current_mid,
320 server->conn_id, server->hostname, 0, credits->value - new_val, 0);
321 cifs_server_dbg(VFS, "R=%x[%x] request has less credits (%d) than required (%d)",
322 subreq->rreq->debug_id, subreq->subreq.debug_index,
323 credits->value, new_val);
324
325 return -EOPNOTSUPP;
326 }
327
328 spin_lock(&server->req_lock);
329
330 if (server->reconnect_instance != credits->instance) {
331 scredits = server->credits;
332 in_flight = server->in_flight;
333 spin_unlock(&server->req_lock);
334
335 trace_smb3_rw_credits(subreq->rreq->debug_id,
336 subreq->subreq.debug_index,
337 credits->value,
338 server->credits, server->in_flight,
339 new_val - credits->value,
340 cifs_trace_rw_credits_old_session);
341 trace_smb3_reconnect_detected(server->current_mid,
342 server->conn_id, server->hostname, scredits,
343 credits->value - new_val, in_flight);
344 cifs_server_dbg(VFS, "R=%x[%x] trying to return %d credits to old session\n",
345 subreq->rreq->debug_id, subreq->subreq.debug_index,
346 credits->value - new_val);
347 return -EAGAIN;
348 }
349
350 trace_smb3_rw_credits(subreq->rreq->debug_id,
351 subreq->subreq.debug_index,
352 credits->value,
353 server->credits, server->in_flight,
354 new_val - credits->value, trace);
355 server->credits += credits->value - new_val;
356 scredits = server->credits;
357 in_flight = server->in_flight;
358 spin_unlock(&server->req_lock);
359 wake_up(&server->request_q);
360
361 trace_smb3_adj_credits(server->current_mid,
362 server->conn_id, server->hostname, scredits,
363 credits->value - new_val, in_flight);
364 cifs_dbg(FYI, "%s: adjust added %u credits total=%d\n",
365 __func__, credits->value - new_val, scredits);
366
367 credits->value = new_val;
368
369 return 0;
370 }
371
372 static __u64
smb2_get_next_mid(struct TCP_Server_Info * server)373 smb2_get_next_mid(struct TCP_Server_Info *server)
374 {
375 __u64 mid;
376 /* for SMB2 we need the current value */
377 spin_lock(&server->mid_counter_lock);
378 mid = server->current_mid++;
379 spin_unlock(&server->mid_counter_lock);
380 return mid;
381 }
382
383 static void
smb2_revert_current_mid(struct TCP_Server_Info * server,const unsigned int val)384 smb2_revert_current_mid(struct TCP_Server_Info *server, const unsigned int val)
385 {
386 spin_lock(&server->mid_counter_lock);
387 if (server->current_mid >= val)
388 server->current_mid -= val;
389 spin_unlock(&server->mid_counter_lock);
390 }
391
392 static struct mid_q_entry *
__smb2_find_mid(struct TCP_Server_Info * server,char * buf,bool dequeue)393 __smb2_find_mid(struct TCP_Server_Info *server, char *buf, bool dequeue)
394 {
395 struct mid_q_entry *mid;
396 struct smb2_hdr *shdr = (struct smb2_hdr *)buf;
397 __u64 wire_mid = le64_to_cpu(shdr->MessageId);
398
399 if (shdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM) {
400 cifs_server_dbg(VFS, "Encrypted frame parsing not supported yet\n");
401 return NULL;
402 }
403
404 spin_lock(&server->mid_queue_lock);
405 list_for_each_entry(mid, &server->pending_mid_q, qhead) {
406 if ((mid->mid == wire_mid) &&
407 (mid->mid_state == MID_REQUEST_SUBMITTED) &&
408 (mid->command == shdr->Command)) {
409 smb_get_mid(mid);
410 if (dequeue) {
411 list_del_init(&mid->qhead);
412 mid->deleted_from_q = true;
413 }
414 spin_unlock(&server->mid_queue_lock);
415 return mid;
416 }
417 }
418 spin_unlock(&server->mid_queue_lock);
419 return NULL;
420 }
421
422 static struct mid_q_entry *
smb2_find_mid(struct TCP_Server_Info * server,char * buf)423 smb2_find_mid(struct TCP_Server_Info *server, char *buf)
424 {
425 return __smb2_find_mid(server, buf, false);
426 }
427
428 static struct mid_q_entry *
smb2_find_dequeue_mid(struct TCP_Server_Info * server,char * buf)429 smb2_find_dequeue_mid(struct TCP_Server_Info *server, char *buf)
430 {
431 return __smb2_find_mid(server, buf, true);
432 }
433
434 static void
smb2_dump_detail(void * buf,size_t buf_len,struct TCP_Server_Info * server)435 smb2_dump_detail(void *buf, size_t buf_len, struct TCP_Server_Info *server)
436 {
437 #ifdef CONFIG_CIFS_DEBUG2
438 struct smb2_hdr *shdr = (struct smb2_hdr *)buf;
439
440 cifs_server_dbg(VFS, "Cmd: %d Err: 0x%x Flags: 0x%x Mid: %llu Pid: %d\n",
441 shdr->Command, shdr->Status, shdr->Flags, shdr->MessageId,
442 shdr->Id.SyncId.ProcessId);
443 if (!server->ops->check_message(buf, buf_len, server->total_read, server)) {
444 cifs_server_dbg(VFS, "smb buf %p len %u\n", buf,
445 server->ops->calc_smb_size(buf));
446 }
447 #endif
448 }
449
450 static bool
smb2_need_neg(struct TCP_Server_Info * server)451 smb2_need_neg(struct TCP_Server_Info *server)
452 {
453 return server->max_read == 0;
454 }
455
456 static int
smb2_negotiate(const unsigned int xid,struct cifs_ses * ses,struct TCP_Server_Info * server)457 smb2_negotiate(const unsigned int xid,
458 struct cifs_ses *ses,
459 struct TCP_Server_Info *server)
460 {
461 int rc;
462
463 spin_lock(&server->mid_counter_lock);
464 server->current_mid = 0;
465 spin_unlock(&server->mid_counter_lock);
466 rc = SMB2_negotiate(xid, ses, server);
467 return rc;
468 }
469
470 static inline unsigned int
prevent_zero_iosize(unsigned int size,const char * type)471 prevent_zero_iosize(unsigned int size, const char *type)
472 {
473 if (size == 0) {
474 cifs_dbg(VFS, "SMB: Zero %ssize calculated, using minimum value %u\n",
475 type, CIFS_MIN_DEFAULT_IOSIZE);
476 return CIFS_MIN_DEFAULT_IOSIZE;
477 }
478 return size;
479 }
480
481 static unsigned int
smb2_negotiate_wsize(struct cifs_tcon * tcon,struct smb3_fs_context * ctx)482 smb2_negotiate_wsize(struct cifs_tcon *tcon, struct smb3_fs_context *ctx)
483 {
484 struct TCP_Server_Info *server = tcon->ses->server;
485 unsigned int wsize;
486
487 /* start with specified wsize, or default */
488 wsize = ctx->got_wsize ? ctx->vol_wsize : CIFS_DEFAULT_IOSIZE;
489 wsize = min_t(unsigned int, wsize, server->max_write);
490 if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
491 wsize = min_t(unsigned int, wsize, SMB2_MAX_BUFFER_SIZE);
492
493 return prevent_zero_iosize(wsize, "w");
494 }
495
496 static unsigned int
smb3_negotiate_wsize(struct cifs_tcon * tcon,struct smb3_fs_context * ctx)497 smb3_negotiate_wsize(struct cifs_tcon *tcon, struct smb3_fs_context *ctx)
498 {
499 struct TCP_Server_Info *server = tcon->ses->server;
500 unsigned int wsize;
501
502 /* start with specified wsize, or default */
503 wsize = ctx->got_wsize ? ctx->vol_wsize : SMB3_DEFAULT_IOSIZE;
504 wsize = min_t(unsigned int, wsize, server->max_write);
505 #ifdef CONFIG_CIFS_SMB_DIRECT
506 if (server->rdma) {
507 const struct smbdirect_socket_parameters *sp =
508 smbd_get_parameters(server->smbd_conn);
509
510 if (server->sign)
511 /*
512 * Account for SMB2 data transfer packet header and
513 * possible encryption header
514 */
515 wsize = min_t(unsigned int,
516 wsize,
517 sp->max_fragmented_send_size -
518 SMB2_READWRITE_PDU_HEADER_SIZE -
519 sizeof(struct smb2_transform_hdr));
520 else
521 wsize = min_t(unsigned int,
522 wsize, sp->max_read_write_size);
523 }
524 #endif
525 if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
526 wsize = min_t(unsigned int, wsize, SMB2_MAX_BUFFER_SIZE);
527
528 return prevent_zero_iosize(wsize, "w");
529 }
530
531 static unsigned int
smb2_negotiate_rsize(struct cifs_tcon * tcon,struct smb3_fs_context * ctx)532 smb2_negotiate_rsize(struct cifs_tcon *tcon, struct smb3_fs_context *ctx)
533 {
534 struct TCP_Server_Info *server = tcon->ses->server;
535 unsigned int rsize;
536
537 /* start with specified rsize, or default */
538 rsize = ctx->got_rsize ? ctx->vol_rsize : CIFS_DEFAULT_IOSIZE;
539 rsize = min_t(unsigned int, rsize, server->max_read);
540
541 if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
542 rsize = min_t(unsigned int, rsize, SMB2_MAX_BUFFER_SIZE);
543
544 return prevent_zero_iosize(rsize, "r");
545 }
546
547 static unsigned int
smb3_negotiate_rsize(struct cifs_tcon * tcon,struct smb3_fs_context * ctx)548 smb3_negotiate_rsize(struct cifs_tcon *tcon, struct smb3_fs_context *ctx)
549 {
550 struct TCP_Server_Info *server = tcon->ses->server;
551 unsigned int rsize;
552
553 /* start with specified rsize, or default */
554 rsize = ctx->got_rsize ? ctx->vol_rsize : SMB3_DEFAULT_IOSIZE;
555 rsize = min_t(unsigned int, rsize, server->max_read);
556 #ifdef CONFIG_CIFS_SMB_DIRECT
557 if (server->rdma) {
558 const struct smbdirect_socket_parameters *sp =
559 smbd_get_parameters(server->smbd_conn);
560
561 if (server->sign)
562 /*
563 * Account for SMB2 data transfer packet header and
564 * possible encryption header
565 */
566 rsize = min_t(unsigned int,
567 rsize,
568 sp->max_fragmented_recv_size -
569 SMB2_READWRITE_PDU_HEADER_SIZE -
570 sizeof(struct smb2_transform_hdr));
571 else
572 rsize = min_t(unsigned int,
573 rsize, sp->max_read_write_size);
574 }
575 #endif
576
577 if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
578 rsize = min_t(unsigned int, rsize, SMB2_MAX_BUFFER_SIZE);
579
580 return prevent_zero_iosize(rsize, "r");
581 }
582
583 /*
584 * compare two interfaces a and b
585 * return 0 if everything matches.
586 * return 1 if a is rdma capable, or rss capable, or has higher link speed
587 * return -1 otherwise.
588 */
589 static int
iface_cmp(struct cifs_server_iface * a,struct cifs_server_iface * b)590 iface_cmp(struct cifs_server_iface *a, struct cifs_server_iface *b)
591 {
592 int cmp_ret = 0;
593
594 WARN_ON(!a || !b);
595 if (a->rdma_capable == b->rdma_capable) {
596 if (a->rss_capable == b->rss_capable) {
597 if (a->speed == b->speed) {
598 cmp_ret = cifs_ipaddr_cmp((struct sockaddr *) &a->sockaddr,
599 (struct sockaddr *) &b->sockaddr);
600 if (!cmp_ret)
601 return 0;
602 else if (cmp_ret > 0)
603 return 1;
604 else
605 return -1;
606 } else if (a->speed > b->speed)
607 return 1;
608 else
609 return -1;
610 } else if (a->rss_capable > b->rss_capable)
611 return 1;
612 else
613 return -1;
614 } else if (a->rdma_capable > b->rdma_capable)
615 return 1;
616 else
617 return -1;
618 }
619
620 static int
parse_server_interfaces(struct network_interface_info_ioctl_rsp * buf,size_t buf_len,struct cifs_ses * ses,bool in_mount)621 parse_server_interfaces(struct network_interface_info_ioctl_rsp *buf,
622 size_t buf_len, struct cifs_ses *ses, bool in_mount)
623 {
624 struct network_interface_info_ioctl_rsp *p;
625 struct sockaddr_in *addr4;
626 struct sockaddr_in6 *addr6;
627 struct smb_sockaddr_in *p4;
628 struct smb_sockaddr_in6 *p6;
629 struct cifs_server_iface *info = NULL, *iface = NULL, *niface = NULL;
630 struct cifs_server_iface tmp_iface;
631 ssize_t bytes_left;
632 size_t next = 0;
633 int nb_iface = 0;
634 int rc = 0, ret = 0;
635
636 bytes_left = buf_len;
637 p = buf;
638
639 spin_lock(&ses->iface_lock);
640 /* do not query too frequently, this time with lock held */
641 if (ses->iface_last_update &&
642 time_before(jiffies, ses->iface_last_update +
643 (SMB_INTERFACE_POLL_INTERVAL * HZ))) {
644 spin_unlock(&ses->iface_lock);
645 return 0;
646 }
647
648 /*
649 * Go through iface_list and mark them as inactive
650 */
651 list_for_each_entry_safe(iface, niface, &ses->iface_list,
652 iface_head)
653 iface->is_active = 0;
654
655 spin_unlock(&ses->iface_lock);
656
657 /*
658 * Samba server e.g. can return an empty interface list in some cases,
659 * which would only be a problem if we were requesting multichannel
660 */
661 if (bytes_left == 0) {
662 /* avoid spamming logs every 10 minutes, so log only in mount */
663 if ((ses->chan_max > 1) && in_mount)
664 cifs_dbg(VFS,
665 "multichannel not available\n"
666 "Empty network interface list returned by server %s\n",
667 ses->server->hostname);
668 rc = -EOPNOTSUPP;
669 ses->iface_last_update = jiffies;
670 goto out;
671 }
672
673 while (bytes_left >= (ssize_t)sizeof(*p)) {
674 memset(&tmp_iface, 0, sizeof(tmp_iface));
675 /* default to 1Gbps when link speed is unset */
676 tmp_iface.speed = le64_to_cpu(p->LinkSpeed) ?: 1000000000;
677 tmp_iface.rdma_capable = le32_to_cpu(p->Capability & RDMA_CAPABLE) ? 1 : 0;
678 tmp_iface.rss_capable = le32_to_cpu(p->Capability & RSS_CAPABLE) ? 1 : 0;
679
680 switch (p->Family) {
681 /*
682 * The kernel and wire socket structures have the same
683 * layout and use network byte order but make the
684 * conversion explicit in case either one changes.
685 */
686 case INTERNETWORK:
687 addr4 = (struct sockaddr_in *)&tmp_iface.sockaddr;
688 p4 = (struct smb_sockaddr_in *)p->Buffer;
689 addr4->sin_family = AF_INET;
690 memcpy(&addr4->sin_addr, &p4->IPv4Address, 4);
691
692 /* [MS-SMB2] 2.2.32.5.1.1 Clients MUST ignore these */
693 addr4->sin_port = cpu_to_be16(CIFS_PORT);
694
695 cifs_dbg(FYI, "%s: ipv4 %pI4\n", __func__,
696 &addr4->sin_addr);
697 break;
698 case INTERNETWORKV6:
699 addr6 = (struct sockaddr_in6 *)&tmp_iface.sockaddr;
700 p6 = (struct smb_sockaddr_in6 *)p->Buffer;
701 addr6->sin6_family = AF_INET6;
702 memcpy(&addr6->sin6_addr, &p6->IPv6Address, 16);
703
704 /* [MS-SMB2] 2.2.32.5.1.2 Clients MUST ignore these */
705 addr6->sin6_flowinfo = 0;
706 addr6->sin6_scope_id = 0;
707 addr6->sin6_port = cpu_to_be16(CIFS_PORT);
708
709 cifs_dbg(FYI, "%s: ipv6 %pI6\n", __func__,
710 &addr6->sin6_addr);
711 break;
712 default:
713 cifs_dbg(VFS,
714 "%s: skipping unsupported socket family\n",
715 __func__);
716 goto next_iface;
717 }
718
719 /*
720 * The iface_list is assumed to be sorted by speed.
721 * Check if the new interface exists in that list.
722 * NEVER change iface. it could be in use.
723 * Add a new one instead
724 */
725 spin_lock(&ses->iface_lock);
726 list_for_each_entry_safe(iface, niface, &ses->iface_list,
727 iface_head) {
728 ret = iface_cmp(iface, &tmp_iface);
729 if (!ret) {
730 iface->is_active = 1;
731 spin_unlock(&ses->iface_lock);
732 goto next_iface;
733 } else if (ret < 0) {
734 /* all remaining ifaces are slower */
735 kref_get(&iface->refcount);
736 break;
737 }
738 }
739 spin_unlock(&ses->iface_lock);
740
741 /* no match. insert the entry in the list */
742 info = kmalloc(sizeof(struct cifs_server_iface),
743 GFP_KERNEL);
744 if (!info) {
745 rc = -ENOMEM;
746 goto out;
747 }
748 memcpy(info, &tmp_iface, sizeof(tmp_iface));
749
750 /* add this new entry to the list */
751 kref_init(&info->refcount);
752 info->is_active = 1;
753
754 cifs_dbg(FYI, "%s: adding iface %zu\n", __func__, ses->iface_count);
755 cifs_dbg(FYI, "%s: speed %zu bps\n", __func__, info->speed);
756 cifs_dbg(FYI, "%s: capabilities 0x%08x\n", __func__,
757 le32_to_cpu(p->Capability));
758
759 spin_lock(&ses->iface_lock);
760 if (!list_entry_is_head(iface, &ses->iface_list, iface_head)) {
761 list_add_tail(&info->iface_head, &iface->iface_head);
762 kref_put(&iface->refcount, release_iface);
763 } else
764 list_add_tail(&info->iface_head, &ses->iface_list);
765
766 ses->iface_count++;
767 spin_unlock(&ses->iface_lock);
768 next_iface:
769 nb_iface++;
770 next = le32_to_cpu(p->Next);
771 if (!next) {
772 bytes_left -= sizeof(*p);
773 break;
774 }
775 /* Validate that Next doesn't point beyond the buffer */
776 if (next > bytes_left) {
777 cifs_dbg(VFS, "%s: invalid Next pointer %zu > %zd\n",
778 __func__, next, bytes_left);
779 rc = -EINVAL;
780 goto out;
781 }
782 p = (struct network_interface_info_ioctl_rsp *)((u8 *)p+next);
783 bytes_left -= next;
784 }
785
786 if (!nb_iface) {
787 cifs_dbg(VFS, "%s: malformed interface info\n", __func__);
788 rc = -EINVAL;
789 goto out;
790 }
791
792 /* Azure rounds the buffer size up 8, to a 16 byte boundary */
793 if ((bytes_left > 8) ||
794 (bytes_left >= offsetof(struct network_interface_info_ioctl_rsp, Next)
795 + sizeof(p->Next) && p->Next))
796 cifs_dbg(VFS, "%s: incomplete interface info\n", __func__);
797
798 ses->iface_last_update = jiffies;
799
800 out:
801 /*
802 * Go through the list again and put the inactive entries
803 */
804 spin_lock(&ses->iface_lock);
805 list_for_each_entry_safe(iface, niface, &ses->iface_list,
806 iface_head) {
807 if (!iface->is_active) {
808 list_del(&iface->iface_head);
809 kref_put(&iface->refcount, release_iface);
810 ses->iface_count--;
811 }
812 }
813 spin_unlock(&ses->iface_lock);
814
815 return rc;
816 }
817
818 int
SMB3_request_interfaces(const unsigned int xid,struct cifs_tcon * tcon,bool in_mount)819 SMB3_request_interfaces(const unsigned int xid, struct cifs_tcon *tcon, bool in_mount)
820 {
821 int rc;
822 unsigned int ret_data_len = 0;
823 struct network_interface_info_ioctl_rsp *out_buf = NULL;
824 struct cifs_ses *ses = tcon->ses;
825 struct TCP_Server_Info *pserver;
826
827 /* do not query too frequently */
828 if (ses->iface_last_update &&
829 time_before(jiffies, ses->iface_last_update +
830 (SMB_INTERFACE_POLL_INTERVAL * HZ)))
831 return 0;
832
833 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
834 FSCTL_QUERY_NETWORK_INTERFACE_INFO,
835 NULL /* no data input */, 0 /* no data input */,
836 CIFSMaxBufSize, (char **)&out_buf, &ret_data_len);
837 if (rc == -EOPNOTSUPP) {
838 cifs_dbg(FYI,
839 "server does not support query network interfaces\n");
840 ret_data_len = 0;
841 } else if (rc != 0) {
842 cifs_tcon_dbg(VFS, "error %d on ioctl to get interface list\n", rc);
843 goto out;
844 }
845
846 rc = parse_server_interfaces(out_buf, ret_data_len, ses, in_mount);
847 if (rc)
848 goto out;
849
850 /* check if iface is still active */
851 spin_lock(&ses->chan_lock);
852 pserver = ses->chans[0].server;
853 if (pserver && !cifs_chan_is_iface_active(ses, pserver)) {
854 spin_unlock(&ses->chan_lock);
855 cifs_chan_update_iface(ses, pserver);
856 spin_lock(&ses->chan_lock);
857 }
858 spin_unlock(&ses->chan_lock);
859
860 out:
861 kfree(out_buf);
862 return rc;
863 }
864
865 static void
smb3_qfs_tcon(const unsigned int xid,struct cifs_tcon * tcon,struct cifs_sb_info * cifs_sb)866 smb3_qfs_tcon(const unsigned int xid, struct cifs_tcon *tcon,
867 struct cifs_sb_info *cifs_sb)
868 {
869 int rc;
870 __le16 srch_path = 0; /* Null - open root of share */
871 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
872 struct cifs_open_parms oparms;
873 struct cifs_fid fid;
874 struct cached_fid *cfid = NULL;
875
876 oparms = (struct cifs_open_parms) {
877 .tcon = tcon,
878 .path = "",
879 .desired_access = FILE_READ_ATTRIBUTES,
880 .disposition = FILE_OPEN,
881 .create_options = cifs_create_options(cifs_sb, 0),
882 .fid = &fid,
883 };
884
885 rc = open_cached_dir(xid, tcon, "", cifs_sb, false, &cfid);
886 if (rc == 0)
887 memcpy(&fid, &cfid->fid, sizeof(struct cifs_fid));
888 else
889 rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL,
890 NULL, NULL);
891 if (rc)
892 return;
893
894 SMB3_request_interfaces(xid, tcon, true /* called during mount */);
895
896 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
897 FS_ATTRIBUTE_INFORMATION);
898 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
899 FS_DEVICE_INFORMATION);
900 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
901 FS_VOLUME_INFORMATION);
902 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
903 FS_SECTOR_SIZE_INFORMATION); /* SMB3 specific */
904 if (cfid == NULL)
905 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
906 else
907 close_cached_dir(cfid);
908 }
909
910 static void
smb2_qfs_tcon(const unsigned int xid,struct cifs_tcon * tcon,struct cifs_sb_info * cifs_sb)911 smb2_qfs_tcon(const unsigned int xid, struct cifs_tcon *tcon,
912 struct cifs_sb_info *cifs_sb)
913 {
914 int rc;
915 __le16 srch_path = 0; /* Null - open root of share */
916 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
917 struct cifs_open_parms oparms;
918 struct cifs_fid fid;
919
920 oparms = (struct cifs_open_parms) {
921 .tcon = tcon,
922 .path = "",
923 .desired_access = FILE_READ_ATTRIBUTES,
924 .disposition = FILE_OPEN,
925 .create_options = cifs_create_options(cifs_sb, 0),
926 .fid = &fid,
927 };
928
929 rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL,
930 NULL, NULL);
931 if (rc)
932 return;
933
934 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
935 FS_ATTRIBUTE_INFORMATION);
936 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
937 FS_DEVICE_INFORMATION);
938 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
939 }
940
941 static int
smb2_is_path_accessible(const unsigned int xid,struct cifs_tcon * tcon,struct cifs_sb_info * cifs_sb,const char * full_path)942 smb2_is_path_accessible(const unsigned int xid, struct cifs_tcon *tcon,
943 struct cifs_sb_info *cifs_sb, const char *full_path)
944 {
945 __le16 *utf16_path;
946 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
947 int err_buftype = CIFS_NO_BUFFER;
948 struct cifs_open_parms oparms;
949 struct kvec err_iov = {};
950 struct cifs_fid fid;
951 struct cached_fid *cfid;
952 bool islink;
953 int rc, rc2;
954
955 rc = open_cached_dir(xid, tcon, full_path, cifs_sb, true, &cfid);
956 if (!rc) {
957 close_cached_dir(cfid);
958 return 0;
959 }
960
961 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
962 if (!utf16_path)
963 return -ENOMEM;
964
965 oparms = (struct cifs_open_parms) {
966 .tcon = tcon,
967 .path = full_path,
968 .desired_access = FILE_READ_ATTRIBUTES,
969 .disposition = FILE_OPEN,
970 .create_options = cifs_create_options(cifs_sb, 0),
971 .fid = &fid,
972 };
973
974 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL,
975 &err_iov, &err_buftype);
976 if (rc) {
977 struct smb2_hdr *hdr = err_iov.iov_base;
978
979 if (unlikely(!hdr || err_buftype == CIFS_NO_BUFFER))
980 goto out;
981
982 if (rc != -EREMOTE && hdr->Status == STATUS_OBJECT_NAME_INVALID) {
983 rc2 = cifs_inval_name_dfs_link_error(xid, tcon, cifs_sb,
984 full_path, &islink);
985 if (rc2) {
986 rc = rc2;
987 goto out;
988 }
989 if (islink)
990 rc = -EREMOTE;
991 }
992 if (rc == -EREMOTE && IS_ENABLED(CONFIG_CIFS_DFS_UPCALL) &&
993 (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_DFS))
994 rc = -EOPNOTSUPP;
995 goto out;
996 }
997
998 rc = SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
999
1000 out:
1001 free_rsp_buf(err_buftype, err_iov.iov_base);
1002 kfree(utf16_path);
1003 return rc;
1004 }
1005
smb2_get_srv_inum(const unsigned int xid,struct cifs_tcon * tcon,struct cifs_sb_info * cifs_sb,const char * full_path,u64 * uniqueid,struct cifs_open_info_data * data)1006 static int smb2_get_srv_inum(const unsigned int xid, struct cifs_tcon *tcon,
1007 struct cifs_sb_info *cifs_sb, const char *full_path,
1008 u64 *uniqueid, struct cifs_open_info_data *data)
1009 {
1010 *uniqueid = le64_to_cpu(data->fi.IndexNumber);
1011 return 0;
1012 }
1013
smb2_query_file_info(const unsigned int xid,struct cifs_tcon * tcon,struct cifsFileInfo * cfile,struct cifs_open_info_data * data)1014 static int smb2_query_file_info(const unsigned int xid, struct cifs_tcon *tcon,
1015 struct cifsFileInfo *cfile, struct cifs_open_info_data *data)
1016 {
1017 struct cifs_fid *fid = &cfile->fid;
1018
1019 if (cfile->symlink_target) {
1020 data->symlink_target = kstrdup(cfile->symlink_target, GFP_KERNEL);
1021 if (!data->symlink_target)
1022 return -ENOMEM;
1023 }
1024 data->contains_posix_file_info = false;
1025 return SMB2_query_info(xid, tcon, fid->persistent_fid, fid->volatile_fid, &data->fi);
1026 }
1027
1028 #ifdef CONFIG_CIFS_XATTR
1029 static ssize_t
move_smb2_ea_to_cifs(char * dst,size_t dst_size,struct smb2_file_full_ea_info * src,size_t src_size,const unsigned char * ea_name)1030 move_smb2_ea_to_cifs(char *dst, size_t dst_size,
1031 struct smb2_file_full_ea_info *src, size_t src_size,
1032 const unsigned char *ea_name)
1033 {
1034 int rc = 0;
1035 unsigned int ea_name_len = ea_name ? strlen(ea_name) : 0;
1036 char *name, *value;
1037 size_t buf_size = dst_size;
1038 size_t name_len, value_len, user_name_len;
1039
1040 while (src_size > 0) {
1041 name_len = (size_t)src->ea_name_length;
1042 value_len = (size_t)le16_to_cpu(src->ea_value_length);
1043
1044 if (name_len == 0)
1045 break;
1046
1047 if (src_size < 8 + name_len + 1 + value_len) {
1048 cifs_dbg(FYI, "EA entry goes beyond length of list\n");
1049 rc = smb_EIO2(smb_eio_trace_ea_overrun,
1050 src_size, 8 + name_len + 1 + value_len);
1051 goto out;
1052 }
1053
1054 name = &src->ea_data[0];
1055 value = &src->ea_data[src->ea_name_length + 1];
1056
1057 if (ea_name) {
1058 if (ea_name_len == name_len &&
1059 memcmp(ea_name, name, name_len) == 0) {
1060 rc = value_len;
1061 if (dst_size == 0)
1062 goto out;
1063 if (dst_size < value_len) {
1064 rc = -ERANGE;
1065 goto out;
1066 }
1067 memcpy(dst, value, value_len);
1068 goto out;
1069 }
1070 } else {
1071 /* 'user.' plus a terminating null */
1072 user_name_len = 5 + 1 + name_len;
1073
1074 if (buf_size == 0) {
1075 /* skip copy - calc size only */
1076 rc += user_name_len;
1077 } else if (dst_size >= user_name_len) {
1078 dst_size -= user_name_len;
1079 memcpy(dst, "user.", 5);
1080 dst += 5;
1081 memcpy(dst, src->ea_data, name_len);
1082 dst += name_len;
1083 *dst = 0;
1084 ++dst;
1085 rc += user_name_len;
1086 } else {
1087 /* stop before overrun buffer */
1088 rc = -ERANGE;
1089 break;
1090 }
1091 }
1092
1093 if (!src->next_entry_offset)
1094 break;
1095
1096 if (src_size < le32_to_cpu(src->next_entry_offset)) {
1097 /* stop before overrun buffer */
1098 rc = -ERANGE;
1099 break;
1100 }
1101 src_size -= le32_to_cpu(src->next_entry_offset);
1102 src = (void *)((char *)src +
1103 le32_to_cpu(src->next_entry_offset));
1104 }
1105
1106 /* didn't find the named attribute */
1107 if (ea_name)
1108 rc = -ENODATA;
1109
1110 out:
1111 return (ssize_t)rc;
1112 }
1113
1114 static ssize_t
smb2_query_eas(const unsigned int xid,struct cifs_tcon * tcon,const unsigned char * path,const unsigned char * ea_name,char * ea_data,size_t buf_size,struct cifs_sb_info * cifs_sb)1115 smb2_query_eas(const unsigned int xid, struct cifs_tcon *tcon,
1116 const unsigned char *path, const unsigned char *ea_name,
1117 char *ea_data, size_t buf_size,
1118 struct cifs_sb_info *cifs_sb)
1119 {
1120 int rc;
1121 struct kvec rsp_iov = {NULL, 0};
1122 int buftype = CIFS_NO_BUFFER;
1123 struct smb2_query_info_rsp *rsp;
1124 struct smb2_file_full_ea_info *info = NULL;
1125
1126 rc = smb2_query_info_compound(xid, tcon, path,
1127 FILE_READ_EA,
1128 FILE_FULL_EA_INFORMATION,
1129 SMB2_O_INFO_FILE,
1130 CIFSMaxBufSize -
1131 MAX_SMB2_CREATE_RESPONSE_SIZE -
1132 MAX_SMB2_CLOSE_RESPONSE_SIZE,
1133 &rsp_iov, &buftype, cifs_sb);
1134 if (rc) {
1135 /*
1136 * If ea_name is NULL (listxattr) and there are no EAs,
1137 * return 0 as it's not an error. Otherwise, the specified
1138 * ea_name was not found.
1139 */
1140 if (!ea_name && rc == -ENODATA)
1141 rc = 0;
1142 goto qeas_exit;
1143 }
1144
1145 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
1146 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
1147 le32_to_cpu(rsp->OutputBufferLength),
1148 &rsp_iov,
1149 sizeof(struct smb2_file_full_ea_info));
1150 if (rc)
1151 goto qeas_exit;
1152
1153 info = (struct smb2_file_full_ea_info *)(
1154 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
1155 rc = move_smb2_ea_to_cifs(ea_data, buf_size, info,
1156 le32_to_cpu(rsp->OutputBufferLength), ea_name);
1157
1158 qeas_exit:
1159 free_rsp_buf(buftype, rsp_iov.iov_base);
1160 return rc;
1161 }
1162
1163 static int
smb2_set_ea(const unsigned int xid,struct cifs_tcon * tcon,const char * path,const char * ea_name,const void * ea_value,const __u16 ea_value_len,const struct nls_table * nls_codepage,struct cifs_sb_info * cifs_sb)1164 smb2_set_ea(const unsigned int xid, struct cifs_tcon *tcon,
1165 const char *path, const char *ea_name, const void *ea_value,
1166 const __u16 ea_value_len, const struct nls_table *nls_codepage,
1167 struct cifs_sb_info *cifs_sb)
1168 {
1169 struct smb2_compound_vars *vars;
1170 struct cifs_ses *ses = tcon->ses;
1171 struct TCP_Server_Info *server;
1172 struct smb_rqst *rqst;
1173 struct kvec *rsp_iov;
1174 __le16 *utf16_path = NULL;
1175 int ea_name_len = strlen(ea_name);
1176 int flags = CIFS_CP_CREATE_CLOSE_OP;
1177 int len;
1178 int resp_buftype[3];
1179 struct cifs_open_parms oparms;
1180 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
1181 struct cifs_fid fid;
1182 unsigned int size[1];
1183 void *data[1];
1184 struct smb2_file_full_ea_info *ea;
1185 struct smb2_query_info_rsp *rsp;
1186 int rc, used_len = 0;
1187 int retries = 0, cur_sleep = 1;
1188
1189 replay_again:
1190 /* reinitialize for possible replay */
1191 flags = CIFS_CP_CREATE_CLOSE_OP;
1192 oplock = SMB2_OPLOCK_LEVEL_NONE;
1193 server = cifs_pick_channel(ses);
1194
1195 if (smb3_encryption_required(tcon))
1196 flags |= CIFS_TRANSFORM_REQ;
1197
1198 if (ea_name_len > 255)
1199 return -EINVAL;
1200
1201 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
1202 if (!utf16_path)
1203 return -ENOMEM;
1204
1205 ea = NULL;
1206 resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
1207 vars = kzalloc(sizeof(*vars), GFP_KERNEL);
1208 if (!vars) {
1209 rc = -ENOMEM;
1210 goto out_free_path;
1211 }
1212 rqst = vars->rqst;
1213 rsp_iov = vars->rsp_iov;
1214
1215 if (ses->server->ops->query_all_EAs) {
1216 if (!ea_value) {
1217 rc = ses->server->ops->query_all_EAs(xid, tcon, path,
1218 ea_name, NULL, 0,
1219 cifs_sb);
1220 if (rc == -ENODATA)
1221 goto sea_exit;
1222 } else {
1223 /* If we are adding a attribute we should first check
1224 * if there will be enough space available to store
1225 * the new EA. If not we should not add it since we
1226 * would not be able to even read the EAs back.
1227 */
1228 rc = smb2_query_info_compound(xid, tcon, path,
1229 FILE_READ_EA,
1230 FILE_FULL_EA_INFORMATION,
1231 SMB2_O_INFO_FILE,
1232 CIFSMaxBufSize -
1233 MAX_SMB2_CREATE_RESPONSE_SIZE -
1234 MAX_SMB2_CLOSE_RESPONSE_SIZE,
1235 &rsp_iov[1], &resp_buftype[1], cifs_sb);
1236 if (rc == 0) {
1237 rsp = (struct smb2_query_info_rsp *)rsp_iov[1].iov_base;
1238 used_len = le32_to_cpu(rsp->OutputBufferLength);
1239 }
1240 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
1241 resp_buftype[1] = CIFS_NO_BUFFER;
1242 memset(&rsp_iov[1], 0, sizeof(rsp_iov[1]));
1243 rc = 0;
1244
1245 /* Use a fudge factor of 256 bytes in case we collide
1246 * with a different set_EAs command.
1247 */
1248 if (CIFSMaxBufSize - MAX_SMB2_CREATE_RESPONSE_SIZE -
1249 MAX_SMB2_CLOSE_RESPONSE_SIZE - 256 <
1250 used_len + ea_name_len + ea_value_len + 1) {
1251 rc = -ENOSPC;
1252 goto sea_exit;
1253 }
1254 }
1255 }
1256
1257 /* Open */
1258 rqst[0].rq_iov = vars->open_iov;
1259 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
1260
1261 oparms = (struct cifs_open_parms) {
1262 .tcon = tcon,
1263 .path = path,
1264 .desired_access = FILE_WRITE_EA,
1265 .disposition = FILE_OPEN,
1266 .create_options = cifs_create_options(cifs_sb, 0),
1267 .fid = &fid,
1268 .replay = !!(retries),
1269 };
1270
1271 rc = SMB2_open_init(tcon, server,
1272 &rqst[0], &oplock, &oparms, utf16_path);
1273 if (rc)
1274 goto sea_exit;
1275 smb2_set_next_command(tcon, &rqst[0]);
1276
1277
1278 /* Set Info */
1279 rqst[1].rq_iov = vars->si_iov;
1280 rqst[1].rq_nvec = 1;
1281
1282 len = sizeof(*ea) + ea_name_len + ea_value_len + 1;
1283 ea = kzalloc(len, GFP_KERNEL);
1284 if (ea == NULL) {
1285 rc = -ENOMEM;
1286 goto sea_exit;
1287 }
1288
1289 ea->ea_name_length = ea_name_len;
1290 ea->ea_value_length = cpu_to_le16(ea_value_len);
1291 memcpy(ea->ea_data, ea_name, ea_name_len + 1);
1292 memcpy(ea->ea_data + ea_name_len + 1, ea_value, ea_value_len);
1293
1294 size[0] = len;
1295 data[0] = ea;
1296
1297 rc = SMB2_set_info_init(tcon, server,
1298 &rqst[1], COMPOUND_FID,
1299 COMPOUND_FID, current->tgid,
1300 FILE_FULL_EA_INFORMATION,
1301 SMB2_O_INFO_FILE, 0, data, size);
1302 if (rc)
1303 goto sea_exit;
1304 smb2_set_next_command(tcon, &rqst[1]);
1305 smb2_set_related(&rqst[1]);
1306
1307 /* Close */
1308 rqst[2].rq_iov = &vars->close_iov;
1309 rqst[2].rq_nvec = 1;
1310 rc = SMB2_close_init(tcon, server,
1311 &rqst[2], COMPOUND_FID, COMPOUND_FID, false);
1312 if (rc)
1313 goto sea_exit;
1314 smb2_set_related(&rqst[2]);
1315
1316 if (retries) {
1317 smb2_set_replay(server, &rqst[0]);
1318 smb2_set_replay(server, &rqst[1]);
1319 smb2_set_replay(server, &rqst[2]);
1320 }
1321
1322 rc = compound_send_recv(xid, ses, server,
1323 flags, 3, rqst,
1324 resp_buftype, rsp_iov);
1325 /* no need to bump num_remote_opens because handle immediately closed */
1326
1327 sea_exit:
1328 kfree(ea);
1329 SMB2_open_free(&rqst[0]);
1330 SMB2_set_info_free(&rqst[1]);
1331 SMB2_close_free(&rqst[2]);
1332 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
1333 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
1334 free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
1335 kfree(vars);
1336 out_free_path:
1337 kfree(utf16_path);
1338
1339 if (is_replayable_error(rc) &&
1340 smb2_should_replay(tcon, &retries, &cur_sleep))
1341 goto replay_again;
1342
1343 return rc;
1344 }
1345 #endif
1346
1347 static bool
smb2_can_echo(struct TCP_Server_Info * server)1348 smb2_can_echo(struct TCP_Server_Info *server)
1349 {
1350 return server->echoes;
1351 }
1352
1353 static void
smb2_clear_stats(struct cifs_tcon * tcon)1354 smb2_clear_stats(struct cifs_tcon *tcon)
1355 {
1356 int i;
1357
1358 for (i = 0; i < NUMBER_OF_SMB2_COMMANDS; i++) {
1359 atomic_set(&tcon->stats.smb2_stats.smb2_com_sent[i], 0);
1360 atomic_set(&tcon->stats.smb2_stats.smb2_com_failed[i], 0);
1361 }
1362 }
1363
1364 static void
smb2_dump_share_caps(struct seq_file * m,struct cifs_tcon * tcon)1365 smb2_dump_share_caps(struct seq_file *m, struct cifs_tcon *tcon)
1366 {
1367 seq_puts(m, "\n\tShare Capabilities:");
1368 if (tcon->capabilities & SMB2_SHARE_CAP_DFS)
1369 seq_puts(m, " DFS,");
1370 if (tcon->capabilities & SMB2_SHARE_CAP_CONTINUOUS_AVAILABILITY)
1371 seq_puts(m, " CONTINUOUS AVAILABILITY,");
1372 if (tcon->capabilities & SMB2_SHARE_CAP_SCALEOUT)
1373 seq_puts(m, " SCALEOUT,");
1374 if (tcon->capabilities & SMB2_SHARE_CAP_CLUSTER)
1375 seq_puts(m, " CLUSTER,");
1376 if (tcon->capabilities & SMB2_SHARE_CAP_ASYMMETRIC)
1377 seq_puts(m, " ASYMMETRIC,");
1378 if (tcon->capabilities == 0)
1379 seq_puts(m, " None");
1380 if (tcon->ss_flags & SSINFO_FLAGS_ALIGNED_DEVICE)
1381 seq_puts(m, " Aligned,");
1382 if (tcon->ss_flags & SSINFO_FLAGS_PARTITION_ALIGNED_ON_DEVICE)
1383 seq_puts(m, " Partition Aligned,");
1384 if (tcon->ss_flags & SSINFO_FLAGS_NO_SEEK_PENALTY)
1385 seq_puts(m, " SSD,");
1386 if (tcon->ss_flags & SSINFO_FLAGS_TRIM_ENABLED)
1387 seq_puts(m, " TRIM-support,");
1388
1389 seq_printf(m, "\tShare Flags: 0x%x", tcon->share_flags);
1390 seq_printf(m, "\n\ttid: 0x%x", tcon->tid);
1391 if (tcon->perf_sector_size)
1392 seq_printf(m, "\tOptimal sector size: 0x%x",
1393 tcon->perf_sector_size);
1394 seq_printf(m, "\tMaximal Access: 0x%x", tcon->maximal_access);
1395 }
1396
1397 static void
smb2_print_stats(struct seq_file * m,struct cifs_tcon * tcon)1398 smb2_print_stats(struct seq_file *m, struct cifs_tcon *tcon)
1399 {
1400 atomic_t *sent = tcon->stats.smb2_stats.smb2_com_sent;
1401 atomic_t *failed = tcon->stats.smb2_stats.smb2_com_failed;
1402
1403 /*
1404 * Can't display SMB2_NEGOTIATE, SESSION_SETUP, LOGOFF, CANCEL and ECHO
1405 * totals (requests sent) since those SMBs are per-session not per tcon
1406 */
1407 seq_printf(m, "\nBytes read: %llu Bytes written: %llu",
1408 (long long)(tcon->bytes_read),
1409 (long long)(tcon->bytes_written));
1410 seq_printf(m, "\nOpen files: %d total (local), %d open on server",
1411 atomic_read(&tcon->num_local_opens),
1412 atomic_read(&tcon->num_remote_opens));
1413 seq_printf(m, "\nTreeConnects: %d total %d failed",
1414 atomic_read(&sent[SMB2_TREE_CONNECT_HE]),
1415 atomic_read(&failed[SMB2_TREE_CONNECT_HE]));
1416 seq_printf(m, "\nTreeDisconnects: %d total %d failed",
1417 atomic_read(&sent[SMB2_TREE_DISCONNECT_HE]),
1418 atomic_read(&failed[SMB2_TREE_DISCONNECT_HE]));
1419 seq_printf(m, "\nCreates: %d total %d failed",
1420 atomic_read(&sent[SMB2_CREATE_HE]),
1421 atomic_read(&failed[SMB2_CREATE_HE]));
1422 seq_printf(m, "\nCloses: %d total %d failed",
1423 atomic_read(&sent[SMB2_CLOSE_HE]),
1424 atomic_read(&failed[SMB2_CLOSE_HE]));
1425 seq_printf(m, "\nFlushes: %d total %d failed",
1426 atomic_read(&sent[SMB2_FLUSH_HE]),
1427 atomic_read(&failed[SMB2_FLUSH_HE]));
1428 seq_printf(m, "\nReads: %d total %d failed",
1429 atomic_read(&sent[SMB2_READ_HE]),
1430 atomic_read(&failed[SMB2_READ_HE]));
1431 seq_printf(m, "\nWrites: %d total %d failed",
1432 atomic_read(&sent[SMB2_WRITE_HE]),
1433 atomic_read(&failed[SMB2_WRITE_HE]));
1434 seq_printf(m, "\nLocks: %d total %d failed",
1435 atomic_read(&sent[SMB2_LOCK_HE]),
1436 atomic_read(&failed[SMB2_LOCK_HE]));
1437 seq_printf(m, "\nIOCTLs: %d total %d failed",
1438 atomic_read(&sent[SMB2_IOCTL_HE]),
1439 atomic_read(&failed[SMB2_IOCTL_HE]));
1440 seq_printf(m, "\nQueryDirectories: %d total %d failed",
1441 atomic_read(&sent[SMB2_QUERY_DIRECTORY_HE]),
1442 atomic_read(&failed[SMB2_QUERY_DIRECTORY_HE]));
1443 seq_printf(m, "\nChangeNotifies: %d total %d failed",
1444 atomic_read(&sent[SMB2_CHANGE_NOTIFY_HE]),
1445 atomic_read(&failed[SMB2_CHANGE_NOTIFY_HE]));
1446 seq_printf(m, "\nQueryInfos: %d total %d failed",
1447 atomic_read(&sent[SMB2_QUERY_INFO_HE]),
1448 atomic_read(&failed[SMB2_QUERY_INFO_HE]));
1449 seq_printf(m, "\nSetInfos: %d total %d failed",
1450 atomic_read(&sent[SMB2_SET_INFO_HE]),
1451 atomic_read(&failed[SMB2_SET_INFO_HE]));
1452 seq_printf(m, "\nOplockBreaks: %d sent %d failed",
1453 atomic_read(&sent[SMB2_OPLOCK_BREAK_HE]),
1454 atomic_read(&failed[SMB2_OPLOCK_BREAK_HE]));
1455 }
1456
1457 static void
smb2_set_fid(struct cifsFileInfo * cfile,struct cifs_fid * fid,__u32 oplock)1458 smb2_set_fid(struct cifsFileInfo *cfile, struct cifs_fid *fid, __u32 oplock)
1459 {
1460 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
1461 struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server;
1462
1463 cfile->fid.persistent_fid = fid->persistent_fid;
1464 cfile->fid.volatile_fid = fid->volatile_fid;
1465 cfile->fid.access = fid->access;
1466 #ifdef CONFIG_CIFS_DEBUG2
1467 cfile->fid.mid = fid->mid;
1468 #endif /* CIFS_DEBUG2 */
1469 server->ops->set_oplock_level(cinode, oplock, fid->epoch,
1470 &fid->purge_cache);
1471 cinode->can_cache_brlcks = CIFS_CACHE_WRITE(cinode);
1472 memcpy(cfile->fid.create_guid, fid->create_guid, 16);
1473 }
1474
1475 static int
smb2_close_file(const unsigned int xid,struct cifs_tcon * tcon,struct cifs_fid * fid)1476 smb2_close_file(const unsigned int xid, struct cifs_tcon *tcon,
1477 struct cifs_fid *fid)
1478 {
1479 return SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
1480 }
1481
1482 static int
smb2_close_getattr(const unsigned int xid,struct cifs_tcon * tcon,struct cifsFileInfo * cfile)1483 smb2_close_getattr(const unsigned int xid, struct cifs_tcon *tcon,
1484 struct cifsFileInfo *cfile)
1485 {
1486 struct smb2_file_network_open_info file_inf;
1487 struct inode *inode;
1488 int rc;
1489
1490 rc = __SMB2_close(xid, tcon, cfile->fid.persistent_fid,
1491 cfile->fid.volatile_fid, &file_inf);
1492 if (rc)
1493 return rc;
1494
1495 inode = d_inode(cfile->dentry);
1496
1497 spin_lock(&inode->i_lock);
1498 CIFS_I(inode)->time = jiffies;
1499
1500 /* Creation time should not need to be updated on close */
1501 if (file_inf.LastWriteTime)
1502 inode_set_mtime_to_ts(inode,
1503 cifs_NTtimeToUnix(file_inf.LastWriteTime));
1504 if (file_inf.ChangeTime)
1505 inode_set_ctime_to_ts(inode,
1506 cifs_NTtimeToUnix(file_inf.ChangeTime));
1507 if (file_inf.LastAccessTime)
1508 inode_set_atime_to_ts(inode,
1509 cifs_NTtimeToUnix(file_inf.LastAccessTime));
1510
1511 /*
1512 * i_blocks is not related to (i_size / i_blksize),
1513 * but instead 512 byte (2**9) size is required for
1514 * calculating num blocks.
1515 */
1516 if (le64_to_cpu(file_inf.AllocationSize) > 4096)
1517 inode->i_blocks =
1518 (512 - 1 + le64_to_cpu(file_inf.AllocationSize)) >> 9;
1519
1520 /* End of file and Attributes should not have to be updated on close */
1521 spin_unlock(&inode->i_lock);
1522 return rc;
1523 }
1524
1525 static int
SMB2_request_res_key(const unsigned int xid,struct cifs_tcon * tcon,u64 persistent_fid,u64 volatile_fid,struct copychunk_ioctl_req * pcchunk)1526 SMB2_request_res_key(const unsigned int xid, struct cifs_tcon *tcon,
1527 u64 persistent_fid, u64 volatile_fid,
1528 struct copychunk_ioctl_req *pcchunk)
1529 {
1530 int rc;
1531 unsigned int ret_data_len;
1532 struct resume_key_ioctl_rsp *res_key;
1533
1534 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
1535 FSCTL_SRV_REQUEST_RESUME_KEY, NULL, 0 /* no input */,
1536 CIFSMaxBufSize, (char **)&res_key, &ret_data_len);
1537
1538 if (rc == -EOPNOTSUPP) {
1539 pr_warn_once("Server share %s does not support copy range\n", tcon->tree_name);
1540 goto req_res_key_exit;
1541 } else if (rc) {
1542 cifs_tcon_dbg(VFS, "refcpy ioctl error %d getting resume key\n", rc);
1543 goto req_res_key_exit;
1544 }
1545 if (ret_data_len < sizeof(struct resume_key_ioctl_rsp)) {
1546 cifs_tcon_dbg(VFS, "Invalid refcopy resume key length\n");
1547 rc = -EINVAL;
1548 goto req_res_key_exit;
1549 }
1550 memcpy(pcchunk->SourceKey, res_key->ResumeKey, COPY_CHUNK_RES_KEY_SIZE);
1551
1552 req_res_key_exit:
1553 kfree(res_key);
1554 return rc;
1555 }
1556
1557 static int
smb2_ioctl_query_info(const unsigned int xid,struct cifs_tcon * tcon,struct cifs_sb_info * cifs_sb,__le16 * path,int is_dir,unsigned long p)1558 smb2_ioctl_query_info(const unsigned int xid,
1559 struct cifs_tcon *tcon,
1560 struct cifs_sb_info *cifs_sb,
1561 __le16 *path, int is_dir,
1562 unsigned long p)
1563 {
1564 struct smb2_compound_vars *vars;
1565 struct smb_rqst *rqst;
1566 struct kvec *rsp_iov;
1567 struct cifs_ses *ses = tcon->ses;
1568 struct TCP_Server_Info *server;
1569 char __user *arg = (char __user *)p;
1570 struct smb_query_info qi;
1571 struct smb_query_info __user *pqi;
1572 int rc = 0;
1573 int flags = CIFS_CP_CREATE_CLOSE_OP;
1574 struct smb2_query_info_rsp *qi_rsp = NULL;
1575 struct smb2_ioctl_rsp *io_rsp = NULL;
1576 void *buffer = NULL;
1577 int resp_buftype[3];
1578 struct cifs_open_parms oparms;
1579 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
1580 struct cifs_fid fid;
1581 unsigned int size[2];
1582 void *data[2];
1583 int create_options = is_dir ? CREATE_NOT_FILE : CREATE_NOT_DIR;
1584 void (*free_req1_func)(struct smb_rqst *r);
1585 int retries = 0, cur_sleep = 1;
1586
1587 replay_again:
1588 /* reinitialize for possible replay */
1589 flags = CIFS_CP_CREATE_CLOSE_OP;
1590 oplock = SMB2_OPLOCK_LEVEL_NONE;
1591 server = cifs_pick_channel(ses);
1592
1593 vars = kzalloc(sizeof(*vars), GFP_ATOMIC);
1594 if (vars == NULL)
1595 return -ENOMEM;
1596 rqst = &vars->rqst[0];
1597 rsp_iov = &vars->rsp_iov[0];
1598
1599 resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
1600
1601 if (copy_from_user(&qi, arg, sizeof(struct smb_query_info))) {
1602 rc = -EFAULT;
1603 goto free_vars;
1604 }
1605 if (qi.output_buffer_length > 1024) {
1606 rc = -EINVAL;
1607 goto free_vars;
1608 }
1609
1610 if (!ses || !server) {
1611 rc = smb_EIO(smb_eio_trace_null_pointers);
1612 goto free_vars;
1613 }
1614
1615 if (smb3_encryption_required(tcon))
1616 flags |= CIFS_TRANSFORM_REQ;
1617
1618 if (qi.output_buffer_length) {
1619 buffer = memdup_user(arg + sizeof(struct smb_query_info), qi.output_buffer_length);
1620 if (IS_ERR(buffer)) {
1621 rc = PTR_ERR(buffer);
1622 goto free_vars;
1623 }
1624 }
1625
1626 /* Open */
1627 rqst[0].rq_iov = &vars->open_iov[0];
1628 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
1629
1630 oparms = (struct cifs_open_parms) {
1631 .tcon = tcon,
1632 .disposition = FILE_OPEN,
1633 .create_options = cifs_create_options(cifs_sb, create_options),
1634 .fid = &fid,
1635 .replay = !!(retries),
1636 };
1637
1638 if (qi.flags & PASSTHRU_FSCTL) {
1639 switch (qi.info_type & FSCTL_DEVICE_ACCESS_MASK) {
1640 case FSCTL_DEVICE_ACCESS_FILE_READ_WRITE_ACCESS:
1641 oparms.desired_access = FILE_READ_DATA | FILE_WRITE_DATA | FILE_READ_ATTRIBUTES | SYNCHRONIZE;
1642 break;
1643 case FSCTL_DEVICE_ACCESS_FILE_ANY_ACCESS:
1644 oparms.desired_access = GENERIC_ALL;
1645 break;
1646 case FSCTL_DEVICE_ACCESS_FILE_READ_ACCESS:
1647 oparms.desired_access = GENERIC_READ;
1648 break;
1649 case FSCTL_DEVICE_ACCESS_FILE_WRITE_ACCESS:
1650 oparms.desired_access = GENERIC_WRITE;
1651 break;
1652 }
1653 } else if (qi.flags & PASSTHRU_SET_INFO) {
1654 oparms.desired_access = GENERIC_WRITE;
1655 } else {
1656 oparms.desired_access = FILE_READ_ATTRIBUTES | READ_CONTROL;
1657 }
1658
1659 rc = SMB2_open_init(tcon, server,
1660 &rqst[0], &oplock, &oparms, path);
1661 if (rc)
1662 goto free_output_buffer;
1663 smb2_set_next_command(tcon, &rqst[0]);
1664
1665 /* Query */
1666 if (qi.flags & PASSTHRU_FSCTL) {
1667 /* Can eventually relax perm check since server enforces too */
1668 if (!capable(CAP_SYS_ADMIN)) {
1669 rc = -EPERM;
1670 goto free_open_req;
1671 }
1672 rqst[1].rq_iov = &vars->io_iov[0];
1673 rqst[1].rq_nvec = SMB2_IOCTL_IOV_SIZE;
1674
1675 rc = SMB2_ioctl_init(tcon, server, &rqst[1], COMPOUND_FID, COMPOUND_FID,
1676 qi.info_type, buffer, qi.output_buffer_length,
1677 CIFSMaxBufSize - MAX_SMB2_CREATE_RESPONSE_SIZE -
1678 MAX_SMB2_CLOSE_RESPONSE_SIZE);
1679 free_req1_func = SMB2_ioctl_free;
1680 } else if (qi.flags == PASSTHRU_SET_INFO) {
1681 /* Can eventually relax perm check since server enforces too */
1682 if (!capable(CAP_SYS_ADMIN)) {
1683 rc = -EPERM;
1684 goto free_open_req;
1685 }
1686 if (qi.output_buffer_length < 8) {
1687 rc = -EINVAL;
1688 goto free_open_req;
1689 }
1690 rqst[1].rq_iov = vars->si_iov;
1691 rqst[1].rq_nvec = 1;
1692
1693 /* MS-FSCC 2.4.13 FileEndOfFileInformation */
1694 size[0] = 8;
1695 data[0] = buffer;
1696
1697 rc = SMB2_set_info_init(tcon, server, &rqst[1], COMPOUND_FID, COMPOUND_FID,
1698 current->tgid, FILE_END_OF_FILE_INFORMATION,
1699 SMB2_O_INFO_FILE, 0, data, size);
1700 free_req1_func = SMB2_set_info_free;
1701 } else if (qi.flags == PASSTHRU_QUERY_INFO) {
1702 rqst[1].rq_iov = &vars->qi_iov;
1703 rqst[1].rq_nvec = 1;
1704
1705 rc = SMB2_query_info_init(tcon, server,
1706 &rqst[1], COMPOUND_FID,
1707 COMPOUND_FID, qi.file_info_class,
1708 qi.info_type, qi.additional_information,
1709 qi.input_buffer_length,
1710 qi.output_buffer_length, buffer);
1711 free_req1_func = SMB2_query_info_free;
1712 } else { /* unknown flags */
1713 cifs_tcon_dbg(VFS, "Invalid passthru query flags: 0x%x\n",
1714 qi.flags);
1715 rc = -EINVAL;
1716 }
1717
1718 if (rc)
1719 goto free_open_req;
1720 smb2_set_next_command(tcon, &rqst[1]);
1721 smb2_set_related(&rqst[1]);
1722
1723 /* Close */
1724 rqst[2].rq_iov = &vars->close_iov;
1725 rqst[2].rq_nvec = 1;
1726
1727 rc = SMB2_close_init(tcon, server,
1728 &rqst[2], COMPOUND_FID, COMPOUND_FID, false);
1729 if (rc)
1730 goto free_req_1;
1731 smb2_set_related(&rqst[2]);
1732
1733 if (retries) {
1734 smb2_set_replay(server, &rqst[0]);
1735 smb2_set_replay(server, &rqst[1]);
1736 smb2_set_replay(server, &rqst[2]);
1737 }
1738
1739 rc = compound_send_recv(xid, ses, server,
1740 flags, 3, rqst,
1741 resp_buftype, rsp_iov);
1742 if (rc)
1743 goto out;
1744
1745 /* No need to bump num_remote_opens since handle immediately closed */
1746 if (qi.flags & PASSTHRU_FSCTL) {
1747 pqi = (struct smb_query_info __user *)arg;
1748 io_rsp = (struct smb2_ioctl_rsp *)rsp_iov[1].iov_base;
1749 if (le32_to_cpu(io_rsp->OutputCount) < qi.input_buffer_length)
1750 qi.input_buffer_length = le32_to_cpu(io_rsp->OutputCount);
1751 if (qi.input_buffer_length > 0 &&
1752 le32_to_cpu(io_rsp->OutputOffset) + qi.input_buffer_length
1753 > rsp_iov[1].iov_len) {
1754 rc = -EFAULT;
1755 goto out;
1756 }
1757
1758 if (copy_to_user(&pqi->input_buffer_length,
1759 &qi.input_buffer_length,
1760 sizeof(qi.input_buffer_length))) {
1761 rc = -EFAULT;
1762 goto out;
1763 }
1764
1765 if (copy_to_user((void __user *)pqi + sizeof(struct smb_query_info),
1766 (const void *)io_rsp + le32_to_cpu(io_rsp->OutputOffset),
1767 qi.input_buffer_length))
1768 rc = -EFAULT;
1769 } else {
1770 pqi = (struct smb_query_info __user *)arg;
1771 qi_rsp = (struct smb2_query_info_rsp *)rsp_iov[1].iov_base;
1772 if (le32_to_cpu(qi_rsp->OutputBufferLength) < qi.input_buffer_length)
1773 qi.input_buffer_length = le32_to_cpu(qi_rsp->OutputBufferLength);
1774 if (copy_to_user(&pqi->input_buffer_length,
1775 &qi.input_buffer_length,
1776 sizeof(qi.input_buffer_length))) {
1777 rc = -EFAULT;
1778 goto out;
1779 }
1780
1781 if (copy_to_user(pqi + 1, qi_rsp->Buffer,
1782 qi.input_buffer_length))
1783 rc = -EFAULT;
1784 }
1785
1786 out:
1787 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
1788 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
1789 free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
1790 SMB2_close_free(&rqst[2]);
1791 free_req_1:
1792 free_req1_func(&rqst[1]);
1793 free_open_req:
1794 SMB2_open_free(&rqst[0]);
1795 free_output_buffer:
1796 kfree(buffer);
1797 free_vars:
1798 kfree(vars);
1799
1800 if (is_replayable_error(rc) &&
1801 smb2_should_replay(tcon, &retries, &cur_sleep))
1802 goto replay_again;
1803
1804 return rc;
1805 }
1806
1807 /**
1808 * calc_chunk_count - calculates the number chunks to be filled in the Chunks[]
1809 * array of struct copychunk_ioctl
1810 *
1811 * @tcon: destination file tcon
1812 * @bytes_left: how many bytes are left to copy
1813 *
1814 * Return: maximum number of chunks with which Chunks[] can be filled.
1815 */
1816 static inline u32
calc_chunk_count(struct cifs_tcon * tcon,u64 bytes_left)1817 calc_chunk_count(struct cifs_tcon *tcon, u64 bytes_left)
1818 {
1819 u32 max_chunks = READ_ONCE(tcon->max_chunks);
1820 u32 max_bytes_copy = READ_ONCE(tcon->max_bytes_copy);
1821 u32 max_bytes_chunk = READ_ONCE(tcon->max_bytes_chunk);
1822 u64 need;
1823 u32 allowed;
1824
1825 if (!max_bytes_chunk || !max_bytes_copy || !max_chunks)
1826 return 0;
1827
1828 /* chunks needed for the remaining bytes */
1829 need = DIV_ROUND_UP_ULL(bytes_left, max_bytes_chunk);
1830 /* chunks allowed per cc request */
1831 allowed = DIV_ROUND_UP(max_bytes_copy, max_bytes_chunk);
1832
1833 return (u32)umin(need, umin(max_chunks, allowed));
1834 }
1835
1836 /**
1837 * smb2_copychunk_range - server-side copy of data range
1838 *
1839 * @xid: transaction id
1840 * @src_file: source file
1841 * @dst_file: destination file
1842 * @src_off: source file byte offset
1843 * @len: number of bytes to copy
1844 * @dst_off: destination file byte offset
1845 *
1846 * Obtains a resume key for @src_file and issues FSCTL_SRV_COPYCHUNK_WRITE
1847 * IOCTLs, splitting the request into chunks limited by tcon->max_*.
1848 *
1849 * Return: @len on success; negative errno on failure.
1850 */
1851 static ssize_t
smb2_copychunk_range(const unsigned int xid,struct cifsFileInfo * src_file,struct cifsFileInfo * dst_file,u64 src_off,u64 len,u64 dst_off)1852 smb2_copychunk_range(const unsigned int xid,
1853 struct cifsFileInfo *src_file,
1854 struct cifsFileInfo *dst_file,
1855 u64 src_off,
1856 u64 len,
1857 u64 dst_off)
1858 {
1859 int rc = 0;
1860 unsigned int ret_data_len = 0;
1861 struct copychunk_ioctl_req *cc_req = NULL;
1862 struct copychunk_ioctl_rsp *cc_rsp = NULL;
1863 struct cifs_tcon *tcon;
1864 struct srv_copychunk *chunk;
1865 u32 chunks, chunk_count, chunk_bytes;
1866 u32 copy_bytes, copy_bytes_left;
1867 u32 chunks_written, bytes_written;
1868 u64 total_bytes_left = len;
1869 u64 src_off_prev, dst_off_prev;
1870 u32 retries = 0;
1871
1872 tcon = tlink_tcon(dst_file->tlink);
1873
1874 trace_smb3_copychunk_enter(xid, src_file->fid.volatile_fid,
1875 dst_file->fid.volatile_fid, tcon->tid,
1876 tcon->ses->Suid, src_off, dst_off, len);
1877
1878 retry:
1879 chunk_count = calc_chunk_count(tcon, total_bytes_left);
1880 if (!chunk_count) {
1881 rc = -EOPNOTSUPP;
1882 goto out;
1883 }
1884
1885 cc_req = kzalloc(struct_size(cc_req, Chunks, chunk_count), GFP_KERNEL);
1886 if (!cc_req) {
1887 rc = -ENOMEM;
1888 goto out;
1889 }
1890
1891 /* Request a key from the server to identify the source of the copy */
1892 rc = SMB2_request_res_key(xid,
1893 tlink_tcon(src_file->tlink),
1894 src_file->fid.persistent_fid,
1895 src_file->fid.volatile_fid,
1896 cc_req);
1897
1898 /* Note: request_res_key sets res_key null only if rc != 0 */
1899 if (rc)
1900 goto out;
1901
1902 while (total_bytes_left > 0) {
1903
1904 /* Store previous offsets to allow rewind */
1905 src_off_prev = src_off;
1906 dst_off_prev = dst_off;
1907
1908 /*
1909 * __counted_by_le(ChunkCount): set to allocated chunks before
1910 * populating Chunks[]
1911 */
1912 cc_req->ChunkCount = cpu_to_le32(chunk_count);
1913
1914 chunks = 0;
1915 copy_bytes = 0;
1916 copy_bytes_left = umin(total_bytes_left, tcon->max_bytes_copy);
1917 while (copy_bytes_left > 0 && chunks < chunk_count) {
1918 chunk = &cc_req->Chunks[chunks++];
1919
1920 chunk->SourceOffset = cpu_to_le64(src_off);
1921 chunk->TargetOffset = cpu_to_le64(dst_off);
1922
1923 chunk_bytes = umin(copy_bytes_left, tcon->max_bytes_chunk);
1924
1925 chunk->Length = cpu_to_le32(chunk_bytes);
1926 /* Buffer is zeroed, no need to set chunk->Reserved = 0 */
1927
1928 src_off += chunk_bytes;
1929 dst_off += chunk_bytes;
1930
1931 copy_bytes_left -= chunk_bytes;
1932 copy_bytes += chunk_bytes;
1933 }
1934
1935 cc_req->ChunkCount = cpu_to_le32(chunks);
1936 /* Buffer is zeroed, no need to set cc_req->Reserved = 0 */
1937
1938 /* Request server copy to target from src identified by key */
1939 kfree(cc_rsp);
1940 cc_rsp = NULL;
1941 rc = SMB2_ioctl(xid, tcon, dst_file->fid.persistent_fid,
1942 dst_file->fid.volatile_fid, FSCTL_SRV_COPYCHUNK_WRITE,
1943 (char *)cc_req, struct_size(cc_req, Chunks, chunks),
1944 CIFSMaxBufSize, (char **)&cc_rsp, &ret_data_len);
1945
1946 if (rc && rc != -EINVAL)
1947 goto out;
1948
1949 if (unlikely(ret_data_len != sizeof(*cc_rsp))) {
1950 cifs_tcon_dbg(VFS, "Copychunk invalid response: size %u/%zu\n",
1951 ret_data_len, sizeof(*cc_rsp));
1952 rc = smb_EIO1(smb_eio_trace_copychunk_inv_rsp, ret_data_len);
1953 goto out;
1954 }
1955
1956 bytes_written = le32_to_cpu(cc_rsp->TotalBytesWritten);
1957 chunks_written = le32_to_cpu(cc_rsp->ChunksWritten);
1958 chunk_bytes = le32_to_cpu(cc_rsp->ChunkBytesWritten);
1959
1960 if (rc == 0) {
1961 /* Check if server claimed to write more than we asked */
1962 if (unlikely(!bytes_written || bytes_written > copy_bytes)) {
1963 cifs_tcon_dbg(VFS, "Copychunk invalid response: bytes written %u/%u\n",
1964 bytes_written, copy_bytes);
1965 rc = smb_EIO2(smb_eio_trace_copychunk_overcopy_b,
1966 bytes_written, copy_bytes);
1967 goto out;
1968 }
1969 if (unlikely(!chunks_written || chunks_written > chunks)) {
1970 cifs_tcon_dbg(VFS, "Copychunk invalid response: chunks written %u/%u\n",
1971 chunks_written, chunks);
1972 rc = smb_EIO2(smb_eio_trace_copychunk_overcopy_c,
1973 chunks_written, chunks);
1974 goto out;
1975 }
1976
1977 /* Partial write: rewind */
1978 if (bytes_written < copy_bytes) {
1979 u32 delta = copy_bytes - bytes_written;
1980
1981 src_off -= delta;
1982 dst_off -= delta;
1983 }
1984
1985 total_bytes_left -= bytes_written;
1986 continue;
1987 }
1988
1989 /*
1990 * Check if server is not asking us to reduce size.
1991 *
1992 * Note: As per MS-SMB2 2.2.32.1, the values returned
1993 * in cc_rsp are not strictly lower than what existed
1994 * before.
1995 */
1996 if (bytes_written < tcon->max_bytes_copy) {
1997 cifs_tcon_dbg(FYI, "Copychunk MaxBytesCopy updated: %u -> %u\n",
1998 tcon->max_bytes_copy, bytes_written);
1999 tcon->max_bytes_copy = bytes_written;
2000 }
2001
2002 if (chunks_written < tcon->max_chunks) {
2003 cifs_tcon_dbg(FYI, "Copychunk MaxChunks updated: %u -> %u\n",
2004 tcon->max_chunks, chunks_written);
2005 tcon->max_chunks = chunks_written;
2006 }
2007
2008 if (chunk_bytes < tcon->max_bytes_chunk) {
2009 cifs_tcon_dbg(FYI, "Copychunk MaxBytesChunk updated: %u -> %u\n",
2010 tcon->max_bytes_chunk, chunk_bytes);
2011 tcon->max_bytes_chunk = chunk_bytes;
2012 }
2013
2014 /* reset to last offsets */
2015 if (retries++ < 2) {
2016 src_off = src_off_prev;
2017 dst_off = dst_off_prev;
2018 kfree(cc_req);
2019 cc_req = NULL;
2020 goto retry;
2021 }
2022
2023 break;
2024 }
2025
2026 out:
2027 kfree(cc_req);
2028 kfree(cc_rsp);
2029 if (rc) {
2030 trace_smb3_copychunk_err(xid, src_file->fid.volatile_fid,
2031 dst_file->fid.volatile_fid, tcon->tid,
2032 tcon->ses->Suid, src_off, dst_off, len, rc);
2033 return rc;
2034 } else {
2035 trace_smb3_copychunk_done(xid, src_file->fid.volatile_fid,
2036 dst_file->fid.volatile_fid, tcon->tid,
2037 tcon->ses->Suid, src_off, dst_off, len);
2038 return len;
2039 }
2040 }
2041
2042 static int
smb2_flush_file(const unsigned int xid,struct cifs_tcon * tcon,struct cifs_fid * fid)2043 smb2_flush_file(const unsigned int xid, struct cifs_tcon *tcon,
2044 struct cifs_fid *fid)
2045 {
2046 return SMB2_flush(xid, tcon, fid->persistent_fid, fid->volatile_fid);
2047 }
2048
2049 static unsigned int
smb2_read_data_offset(char * buf)2050 smb2_read_data_offset(char *buf)
2051 {
2052 struct smb2_read_rsp *rsp = (struct smb2_read_rsp *)buf;
2053
2054 return rsp->DataOffset;
2055 }
2056
2057 static unsigned int
smb2_read_data_length(char * buf,bool in_remaining)2058 smb2_read_data_length(char *buf, bool in_remaining)
2059 {
2060 struct smb2_read_rsp *rsp = (struct smb2_read_rsp *)buf;
2061
2062 if (in_remaining)
2063 return le32_to_cpu(rsp->DataRemaining);
2064
2065 return le32_to_cpu(rsp->DataLength);
2066 }
2067
2068
2069 static int
smb2_sync_read(const unsigned int xid,struct cifs_fid * pfid,struct cifs_io_parms * parms,unsigned int * bytes_read,char ** buf,int * buf_type)2070 smb2_sync_read(const unsigned int xid, struct cifs_fid *pfid,
2071 struct cifs_io_parms *parms, unsigned int *bytes_read,
2072 char **buf, int *buf_type)
2073 {
2074 parms->persistent_fid = pfid->persistent_fid;
2075 parms->volatile_fid = pfid->volatile_fid;
2076 return SMB2_read(xid, parms, bytes_read, buf, buf_type);
2077 }
2078
2079 static int
smb2_sync_write(const unsigned int xid,struct cifs_fid * pfid,struct cifs_io_parms * parms,unsigned int * written,struct kvec * iov,unsigned long nr_segs)2080 smb2_sync_write(const unsigned int xid, struct cifs_fid *pfid,
2081 struct cifs_io_parms *parms, unsigned int *written,
2082 struct kvec *iov, unsigned long nr_segs)
2083 {
2084
2085 parms->persistent_fid = pfid->persistent_fid;
2086 parms->volatile_fid = pfid->volatile_fid;
2087 return SMB2_write(xid, parms, written, iov, nr_segs);
2088 }
2089
2090 /* Set or clear the SPARSE_FILE attribute based on value passed in setsparse */
smb2_set_sparse(const unsigned int xid,struct cifs_tcon * tcon,struct cifsFileInfo * cfile,struct inode * inode,__u8 setsparse)2091 static bool smb2_set_sparse(const unsigned int xid, struct cifs_tcon *tcon,
2092 struct cifsFileInfo *cfile, struct inode *inode, __u8 setsparse)
2093 {
2094 struct cifsInodeInfo *cifsi;
2095 int rc;
2096
2097 cifsi = CIFS_I(inode);
2098
2099 /* if file already sparse don't bother setting sparse again */
2100 if ((cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) && setsparse)
2101 return true; /* already sparse */
2102
2103 if (!(cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) && !setsparse)
2104 return true; /* already not sparse */
2105
2106 /*
2107 * Can't check for sparse support on share the usual way via the
2108 * FS attribute info (FILE_SUPPORTS_SPARSE_FILES) on the share
2109 * since Samba server doesn't set the flag on the share, yet
2110 * supports the set sparse FSCTL and returns sparse correctly
2111 * in the file attributes. If we fail setting sparse though we
2112 * mark that server does not support sparse files for this share
2113 * to avoid repeatedly sending the unsupported fsctl to server
2114 * if the file is repeatedly extended.
2115 */
2116 if (tcon->broken_sparse_sup)
2117 return false;
2118
2119 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
2120 cfile->fid.volatile_fid, FSCTL_SET_SPARSE,
2121 &setsparse, 1, CIFSMaxBufSize, NULL, NULL);
2122 if (rc) {
2123 tcon->broken_sparse_sup = true;
2124 cifs_dbg(FYI, "set sparse rc = %d\n", rc);
2125 return false;
2126 }
2127
2128 if (setsparse)
2129 cifsi->cifsAttrs |= FILE_ATTRIBUTE_SPARSE_FILE;
2130 else
2131 cifsi->cifsAttrs &= (~FILE_ATTRIBUTE_SPARSE_FILE);
2132
2133 return true;
2134 }
2135
2136 static int
smb2_set_file_size(const unsigned int xid,struct cifs_tcon * tcon,struct cifsFileInfo * cfile,__u64 size,bool set_alloc)2137 smb2_set_file_size(const unsigned int xid, struct cifs_tcon *tcon,
2138 struct cifsFileInfo *cfile, __u64 size, bool set_alloc)
2139 {
2140 struct inode *inode;
2141
2142 /*
2143 * If extending file more than one page make sparse. Many Linux fs
2144 * make files sparse by default when extending via ftruncate
2145 */
2146 inode = d_inode(cfile->dentry);
2147
2148 if (!set_alloc && (size > inode->i_size + 8192)) {
2149 __u8 set_sparse = 1;
2150
2151 /* whether set sparse succeeds or not, extend the file */
2152 smb2_set_sparse(xid, tcon, cfile, inode, set_sparse);
2153 }
2154
2155 return SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
2156 cfile->fid.volatile_fid, cfile->pid, size);
2157 }
2158
2159 static int
smb2_duplicate_extents(const unsigned int xid,struct cifsFileInfo * srcfile,struct cifsFileInfo * trgtfile,u64 src_off,u64 len,u64 dest_off)2160 smb2_duplicate_extents(const unsigned int xid,
2161 struct cifsFileInfo *srcfile,
2162 struct cifsFileInfo *trgtfile, u64 src_off,
2163 u64 len, u64 dest_off)
2164 {
2165 int rc;
2166 unsigned int ret_data_len;
2167 struct inode *inode;
2168 struct duplicate_extents_to_file dup_ext_buf;
2169 struct cifs_tcon *tcon = tlink_tcon(trgtfile->tlink);
2170
2171 /* server fileays advertise duplicate extent support with this flag */
2172 if ((le32_to_cpu(tcon->fsAttrInfo.Attributes) &
2173 FILE_SUPPORTS_BLOCK_REFCOUNTING) == 0)
2174 return -EOPNOTSUPP;
2175
2176 dup_ext_buf.VolatileFileHandle = srcfile->fid.volatile_fid;
2177 dup_ext_buf.PersistentFileHandle = srcfile->fid.persistent_fid;
2178 dup_ext_buf.SourceFileOffset = cpu_to_le64(src_off);
2179 dup_ext_buf.TargetFileOffset = cpu_to_le64(dest_off);
2180 dup_ext_buf.ByteCount = cpu_to_le64(len);
2181 cifs_dbg(FYI, "Duplicate extents: src off %lld dst off %lld len %lld\n",
2182 src_off, dest_off, len);
2183 trace_smb3_clone_enter(xid, srcfile->fid.volatile_fid,
2184 trgtfile->fid.volatile_fid, tcon->tid,
2185 tcon->ses->Suid, src_off, dest_off, len);
2186 inode = d_inode(trgtfile->dentry);
2187 if (inode->i_size < dest_off + len) {
2188 rc = smb2_set_file_size(xid, tcon, trgtfile, dest_off + len, false);
2189 if (rc)
2190 goto duplicate_extents_out;
2191
2192 /*
2193 * Although also could set plausible allocation size (i_blocks)
2194 * here in addition to setting the file size, in reflink
2195 * it is likely that the target file is sparse. Its allocation
2196 * size will be queried on next revalidate, but it is important
2197 * to make sure that file's cached size is updated immediately
2198 */
2199 netfs_resize_file(netfs_inode(inode), dest_off + len, true);
2200 cifs_setsize(inode, dest_off + len);
2201 }
2202 rc = SMB2_ioctl(xid, tcon, trgtfile->fid.persistent_fid,
2203 trgtfile->fid.volatile_fid,
2204 FSCTL_DUPLICATE_EXTENTS_TO_FILE,
2205 (char *)&dup_ext_buf,
2206 sizeof(struct duplicate_extents_to_file),
2207 CIFSMaxBufSize, NULL,
2208 &ret_data_len);
2209
2210 if (ret_data_len > 0)
2211 cifs_dbg(FYI, "Non-zero response length in duplicate extents\n");
2212
2213 duplicate_extents_out:
2214 if (rc)
2215 trace_smb3_clone_err(xid, srcfile->fid.volatile_fid,
2216 trgtfile->fid.volatile_fid,
2217 tcon->tid, tcon->ses->Suid, src_off,
2218 dest_off, len, rc);
2219 else
2220 trace_smb3_clone_done(xid, srcfile->fid.volatile_fid,
2221 trgtfile->fid.volatile_fid, tcon->tid,
2222 tcon->ses->Suid, src_off, dest_off, len);
2223 return rc;
2224 }
2225
2226 static int
smb2_set_compression(const unsigned int xid,struct cifs_tcon * tcon,struct cifsFileInfo * cfile)2227 smb2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
2228 struct cifsFileInfo *cfile)
2229 {
2230 return SMB2_set_compression(xid, tcon, cfile->fid.persistent_fid,
2231 cfile->fid.volatile_fid);
2232 }
2233
2234 static int
smb3_set_integrity(const unsigned int xid,struct cifs_tcon * tcon,struct cifsFileInfo * cfile)2235 smb3_set_integrity(const unsigned int xid, struct cifs_tcon *tcon,
2236 struct cifsFileInfo *cfile)
2237 {
2238 struct fsctl_set_integrity_information_req integr_info;
2239 unsigned int ret_data_len;
2240
2241 integr_info.ChecksumAlgorithm = cpu_to_le16(CHECKSUM_TYPE_UNCHANGED);
2242 integr_info.Flags = 0;
2243 integr_info.Reserved = 0;
2244
2245 return SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
2246 cfile->fid.volatile_fid,
2247 FSCTL_SET_INTEGRITY_INFORMATION,
2248 (char *)&integr_info,
2249 sizeof(struct fsctl_set_integrity_information_req),
2250 CIFSMaxBufSize, NULL,
2251 &ret_data_len);
2252
2253 }
2254
2255 /* GMT Token is @GMT-YYYY.MM.DD-HH.MM.SS Unicode which is 48 bytes + null */
2256 #define GMT_TOKEN_SIZE 50
2257
2258 #define MIN_SNAPSHOT_ARRAY_SIZE 16 /* See MS-SMB2 section 3.3.5.15.1 */
2259
2260 /*
2261 * Input buffer contains (empty) struct smb_snapshot array with size filled in
2262 * For output see struct SRV_SNAPSHOT_ARRAY in MS-SMB2 section 2.2.32.2
2263 */
2264 static int
smb3_enum_snapshots(const unsigned int xid,struct cifs_tcon * tcon,struct cifsFileInfo * cfile,void __user * ioc_buf)2265 smb3_enum_snapshots(const unsigned int xid, struct cifs_tcon *tcon,
2266 struct cifsFileInfo *cfile, void __user *ioc_buf)
2267 {
2268 char *retbuf = NULL;
2269 unsigned int ret_data_len = 0;
2270 int rc;
2271 u32 max_response_size;
2272 struct smb_snapshot_array snapshot_in;
2273
2274 /*
2275 * On the first query to enumerate the list of snapshots available
2276 * for this volume the buffer begins with 0 (number of snapshots
2277 * which can be returned is zero since at that point we do not know
2278 * how big the buffer needs to be). On the second query,
2279 * it (ret_data_len) is set to number of snapshots so we can
2280 * know to set the maximum response size larger (see below).
2281 */
2282 if (get_user(ret_data_len, (unsigned int __user *)ioc_buf))
2283 return -EFAULT;
2284
2285 /*
2286 * Note that for snapshot queries that servers like Azure expect that
2287 * the first query be minimal size (and just used to get the number/size
2288 * of previous versions) so response size must be specified as EXACTLY
2289 * sizeof(struct snapshot_array) which is 16 when rounded up to multiple
2290 * of eight bytes.
2291 */
2292 if (ret_data_len == 0)
2293 max_response_size = MIN_SNAPSHOT_ARRAY_SIZE;
2294 else
2295 max_response_size = CIFSMaxBufSize;
2296
2297 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
2298 cfile->fid.volatile_fid,
2299 FSCTL_SRV_ENUMERATE_SNAPSHOTS,
2300 NULL, 0 /* no input data */, max_response_size,
2301 (char **)&retbuf,
2302 &ret_data_len);
2303 cifs_dbg(FYI, "enum snapshots ioctl returned %d and ret buflen is %d\n",
2304 rc, ret_data_len);
2305 if (rc)
2306 return rc;
2307
2308 if (ret_data_len && (ioc_buf != NULL) && (retbuf != NULL)) {
2309 /* Fixup buffer */
2310 if (copy_from_user(&snapshot_in, ioc_buf,
2311 sizeof(struct smb_snapshot_array))) {
2312 rc = -EFAULT;
2313 kfree(retbuf);
2314 return rc;
2315 }
2316
2317 /*
2318 * Check for min size, ie not large enough to fit even one GMT
2319 * token (snapshot). On the first ioctl some users may pass in
2320 * smaller size (or zero) to simply get the size of the array
2321 * so the user space caller can allocate sufficient memory
2322 * and retry the ioctl again with larger array size sufficient
2323 * to hold all of the snapshot GMT tokens on the second try.
2324 */
2325 if (snapshot_in.snapshot_array_size < GMT_TOKEN_SIZE)
2326 ret_data_len = sizeof(struct smb_snapshot_array);
2327
2328 /*
2329 * We return struct SRV_SNAPSHOT_ARRAY, followed by
2330 * the snapshot array (of 50 byte GMT tokens) each
2331 * representing an available previous version of the data
2332 */
2333 if (ret_data_len > (snapshot_in.snapshot_array_size +
2334 sizeof(struct smb_snapshot_array)))
2335 ret_data_len = snapshot_in.snapshot_array_size +
2336 sizeof(struct smb_snapshot_array);
2337
2338 if (copy_to_user(ioc_buf, retbuf, ret_data_len))
2339 rc = -EFAULT;
2340 }
2341
2342 kfree(retbuf);
2343 return rc;
2344 }
2345
2346
2347
2348 static int
smb3_notify(const unsigned int xid,struct file * pfile,void __user * ioc_buf,bool return_changes)2349 smb3_notify(const unsigned int xid, struct file *pfile,
2350 void __user *ioc_buf, bool return_changes)
2351 {
2352 struct smb3_notify_info notify;
2353 struct smb3_notify_info __user *pnotify_buf;
2354 struct dentry *dentry = pfile->f_path.dentry;
2355 struct inode *inode = file_inode(pfile);
2356 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
2357 struct cifs_open_parms oparms;
2358 struct cifs_fid fid;
2359 struct cifs_tcon *tcon;
2360 const unsigned char *path;
2361 char *returned_ioctl_info = NULL;
2362 void *page = alloc_dentry_path();
2363 __le16 *utf16_path = NULL;
2364 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2365 int rc = 0;
2366 __u32 ret_len = 0;
2367
2368 path = build_path_from_dentry(dentry, page);
2369 if (IS_ERR(path)) {
2370 rc = PTR_ERR(path);
2371 goto notify_exit;
2372 }
2373
2374 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
2375 if (utf16_path == NULL) {
2376 rc = -ENOMEM;
2377 goto notify_exit;
2378 }
2379
2380 if (return_changes) {
2381 if (copy_from_user(¬ify, ioc_buf, sizeof(struct smb3_notify_info))) {
2382 rc = -EFAULT;
2383 goto notify_exit;
2384 }
2385 } else {
2386 if (copy_from_user(¬ify, ioc_buf, sizeof(struct smb3_notify))) {
2387 rc = -EFAULT;
2388 goto notify_exit;
2389 }
2390 notify.data_len = 0;
2391 }
2392
2393 tcon = cifs_sb_master_tcon(cifs_sb);
2394 oparms = (struct cifs_open_parms) {
2395 .tcon = tcon,
2396 .path = path,
2397 .desired_access = FILE_READ_ATTRIBUTES | FILE_READ_DATA,
2398 .disposition = FILE_OPEN,
2399 .create_options = cifs_create_options(cifs_sb, 0),
2400 .fid = &fid,
2401 };
2402
2403 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, NULL,
2404 NULL);
2405 if (rc)
2406 goto notify_exit;
2407
2408 rc = SMB2_change_notify(xid, tcon, fid.persistent_fid, fid.volatile_fid,
2409 notify.watch_tree, notify.completion_filter,
2410 notify.data_len, &returned_ioctl_info, &ret_len);
2411
2412 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
2413
2414 cifs_dbg(FYI, "change notify for path %s rc %d\n", path, rc);
2415 if (return_changes && (ret_len > 0) && (notify.data_len > 0)) {
2416 if (ret_len > notify.data_len)
2417 ret_len = notify.data_len;
2418 pnotify_buf = (struct smb3_notify_info __user *)ioc_buf;
2419 if (copy_to_user(pnotify_buf->notify_data, returned_ioctl_info, ret_len))
2420 rc = -EFAULT;
2421 else if (copy_to_user(&pnotify_buf->data_len, &ret_len, sizeof(ret_len)))
2422 rc = -EFAULT;
2423 }
2424 kfree(returned_ioctl_info);
2425 notify_exit:
2426 free_dentry_path(page);
2427 kfree(utf16_path);
2428 return rc;
2429 }
2430
2431 static int
smb2_query_dir_first(const unsigned int xid,struct cifs_tcon * tcon,const char * path,struct cifs_sb_info * cifs_sb,struct cifs_fid * fid,__u16 search_flags,struct cifs_search_info * srch_inf)2432 smb2_query_dir_first(const unsigned int xid, struct cifs_tcon *tcon,
2433 const char *path, struct cifs_sb_info *cifs_sb,
2434 struct cifs_fid *fid, __u16 search_flags,
2435 struct cifs_search_info *srch_inf)
2436 {
2437 __le16 *utf16_path;
2438 struct smb_rqst rqst[2];
2439 struct kvec rsp_iov[2];
2440 int resp_buftype[2];
2441 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
2442 struct kvec qd_iov[SMB2_QUERY_DIRECTORY_IOV_SIZE];
2443 int rc, flags = 0;
2444 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2445 struct cifs_open_parms oparms;
2446 struct smb2_query_directory_rsp *qd_rsp = NULL;
2447 struct smb2_create_rsp *op_rsp = NULL;
2448 struct TCP_Server_Info *server;
2449 int retries = 0, cur_sleep = 1;
2450
2451 replay_again:
2452 /* reinitialize for possible replay */
2453 flags = 0;
2454 oplock = SMB2_OPLOCK_LEVEL_NONE;
2455 server = cifs_pick_channel(tcon->ses);
2456
2457 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
2458 if (!utf16_path)
2459 return -ENOMEM;
2460
2461 if (smb3_encryption_required(tcon))
2462 flags |= CIFS_TRANSFORM_REQ;
2463
2464 memset(rqst, 0, sizeof(rqst));
2465 resp_buftype[0] = resp_buftype[1] = CIFS_NO_BUFFER;
2466 memset(rsp_iov, 0, sizeof(rsp_iov));
2467
2468 /* Open */
2469 memset(&open_iov, 0, sizeof(open_iov));
2470 rqst[0].rq_iov = open_iov;
2471 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
2472
2473 oparms = (struct cifs_open_parms) {
2474 .tcon = tcon,
2475 .path = path,
2476 .desired_access = FILE_READ_ATTRIBUTES | FILE_READ_DATA,
2477 .disposition = FILE_OPEN,
2478 .create_options = cifs_create_options(cifs_sb, 0),
2479 .fid = fid,
2480 .replay = !!(retries),
2481 };
2482
2483 rc = SMB2_open_init(tcon, server,
2484 &rqst[0], &oplock, &oparms, utf16_path);
2485 if (rc)
2486 goto qdf_free;
2487 smb2_set_next_command(tcon, &rqst[0]);
2488
2489 /* Query directory */
2490 srch_inf->entries_in_buffer = 0;
2491 srch_inf->index_of_last_entry = 2;
2492
2493 memset(&qd_iov, 0, sizeof(qd_iov));
2494 rqst[1].rq_iov = qd_iov;
2495 rqst[1].rq_nvec = SMB2_QUERY_DIRECTORY_IOV_SIZE;
2496
2497 rc = SMB2_query_directory_init(xid, tcon, server,
2498 &rqst[1],
2499 COMPOUND_FID, COMPOUND_FID,
2500 0, srch_inf->info_level);
2501 if (rc)
2502 goto qdf_free;
2503
2504 smb2_set_related(&rqst[1]);
2505
2506 if (retries) {
2507 smb2_set_replay(server, &rqst[0]);
2508 smb2_set_replay(server, &rqst[1]);
2509 }
2510
2511 rc = compound_send_recv(xid, tcon->ses, server,
2512 flags, 2, rqst,
2513 resp_buftype, rsp_iov);
2514
2515 /* If the open failed there is nothing to do */
2516 op_rsp = (struct smb2_create_rsp *)rsp_iov[0].iov_base;
2517 if (op_rsp == NULL || op_rsp->hdr.Status != STATUS_SUCCESS) {
2518 cifs_dbg(FYI, "query_dir_first: open failed rc=%d\n", rc);
2519 goto qdf_free;
2520 }
2521 fid->persistent_fid = op_rsp->PersistentFileId;
2522 fid->volatile_fid = op_rsp->VolatileFileId;
2523
2524 /* Anything else than ENODATA means a genuine error */
2525 if (rc && rc != -ENODATA) {
2526 SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
2527 cifs_dbg(FYI, "query_dir_first: query directory failed rc=%d\n", rc);
2528 trace_smb3_query_dir_err(xid, fid->persistent_fid,
2529 tcon->tid, tcon->ses->Suid, 0, 0, rc);
2530 goto qdf_free;
2531 }
2532
2533 atomic_inc(&tcon->num_remote_opens);
2534
2535 qd_rsp = (struct smb2_query_directory_rsp *)rsp_iov[1].iov_base;
2536 if (qd_rsp->hdr.Status == STATUS_NO_MORE_FILES) {
2537 trace_smb3_query_dir_done(xid, fid->persistent_fid,
2538 tcon->tid, tcon->ses->Suid, 0, 0);
2539 srch_inf->endOfSearch = true;
2540 rc = 0;
2541 goto qdf_free;
2542 }
2543
2544 rc = smb2_parse_query_directory(tcon, &rsp_iov[1], resp_buftype[1],
2545 srch_inf);
2546 if (rc) {
2547 trace_smb3_query_dir_err(xid, fid->persistent_fid, tcon->tid,
2548 tcon->ses->Suid, 0, 0, rc);
2549 goto qdf_free;
2550 }
2551 resp_buftype[1] = CIFS_NO_BUFFER;
2552
2553 trace_smb3_query_dir_done(xid, fid->persistent_fid, tcon->tid,
2554 tcon->ses->Suid, 0, srch_inf->entries_in_buffer);
2555
2556 qdf_free:
2557 kfree(utf16_path);
2558 SMB2_open_free(&rqst[0]);
2559 SMB2_query_directory_free(&rqst[1]);
2560 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
2561 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
2562
2563 if (is_replayable_error(rc) &&
2564 smb2_should_replay(tcon, &retries, &cur_sleep))
2565 goto replay_again;
2566
2567 return rc;
2568 }
2569
2570 static int
smb2_query_dir_next(const unsigned int xid,struct cifs_tcon * tcon,struct cifs_fid * fid,__u16 search_flags,struct cifs_search_info * srch_inf)2571 smb2_query_dir_next(const unsigned int xid, struct cifs_tcon *tcon,
2572 struct cifs_fid *fid, __u16 search_flags,
2573 struct cifs_search_info *srch_inf)
2574 {
2575 return SMB2_query_directory(xid, tcon, fid->persistent_fid,
2576 fid->volatile_fid, 0, srch_inf);
2577 }
2578
2579 static int
smb2_close_dir(const unsigned int xid,struct cifs_tcon * tcon,struct cifs_fid * fid)2580 smb2_close_dir(const unsigned int xid, struct cifs_tcon *tcon,
2581 struct cifs_fid *fid)
2582 {
2583 return SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
2584 }
2585
2586 /*
2587 * If we negotiate SMB2 protocol and get STATUS_PENDING - update
2588 * the number of credits and return true. Otherwise - return false.
2589 */
2590 static bool
smb2_is_status_pending(char * buf,struct TCP_Server_Info * server)2591 smb2_is_status_pending(char *buf, struct TCP_Server_Info *server)
2592 {
2593 struct smb2_hdr *shdr = (struct smb2_hdr *)buf;
2594 int scredits, in_flight;
2595
2596 if (shdr->Status != STATUS_PENDING)
2597 return false;
2598
2599 if (shdr->CreditRequest) {
2600 spin_lock(&server->req_lock);
2601 server->credits += le16_to_cpu(shdr->CreditRequest);
2602 scredits = server->credits;
2603 in_flight = server->in_flight;
2604 spin_unlock(&server->req_lock);
2605 wake_up(&server->request_q);
2606
2607 trace_smb3_pend_credits(server->current_mid,
2608 server->conn_id, server->hostname, scredits,
2609 le16_to_cpu(shdr->CreditRequest), in_flight);
2610 cifs_dbg(FYI, "%s: status pending add %u credits total=%d\n",
2611 __func__, le16_to_cpu(shdr->CreditRequest), scredits);
2612 }
2613
2614 return true;
2615 }
2616
2617 static bool
smb2_is_session_expired(char * buf)2618 smb2_is_session_expired(char *buf)
2619 {
2620 struct smb2_hdr *shdr = (struct smb2_hdr *)buf;
2621
2622 if (shdr->Status != STATUS_NETWORK_SESSION_EXPIRED &&
2623 shdr->Status != STATUS_USER_SESSION_DELETED)
2624 return false;
2625
2626 trace_smb3_ses_expired(le32_to_cpu(shdr->Id.SyncId.TreeId),
2627 le64_to_cpu(shdr->SessionId),
2628 le16_to_cpu(shdr->Command),
2629 le64_to_cpu(shdr->MessageId));
2630 cifs_dbg(FYI, "Session expired or deleted\n");
2631
2632 return true;
2633 }
2634
2635 static bool
smb2_is_status_io_timeout(char * buf)2636 smb2_is_status_io_timeout(char *buf)
2637 {
2638 struct smb2_hdr *shdr = (struct smb2_hdr *)buf;
2639
2640 if (shdr->Status == STATUS_IO_TIMEOUT)
2641 return true;
2642 else
2643 return false;
2644 }
2645
2646 static bool
smb2_is_network_name_deleted(char * buf,struct TCP_Server_Info * server)2647 smb2_is_network_name_deleted(char *buf, struct TCP_Server_Info *server)
2648 {
2649 struct smb2_hdr *shdr = (struct smb2_hdr *)buf;
2650 struct TCP_Server_Info *pserver;
2651 struct cifs_ses *ses;
2652 struct cifs_tcon *tcon;
2653
2654 if (shdr->Status != STATUS_NETWORK_NAME_DELETED)
2655 return false;
2656
2657 /* If server is a channel, select the primary channel */
2658 pserver = SERVER_IS_CHAN(server) ? server->primary_server : server;
2659
2660 spin_lock(&cifs_tcp_ses_lock);
2661 list_for_each_entry(ses, &pserver->smb_ses_list, smb_ses_list) {
2662 if (cifs_ses_exiting(ses))
2663 continue;
2664 list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
2665 if (tcon->tid == le32_to_cpu(shdr->Id.SyncId.TreeId)) {
2666 spin_lock(&tcon->tc_lock);
2667 tcon->need_reconnect = true;
2668 spin_unlock(&tcon->tc_lock);
2669 spin_unlock(&cifs_tcp_ses_lock);
2670 pr_warn_once("Server share %s deleted.\n",
2671 tcon->tree_name);
2672 return true;
2673 }
2674 }
2675 }
2676 spin_unlock(&cifs_tcp_ses_lock);
2677
2678 return false;
2679 }
2680
2681 static int
smb2_oplock_response(struct cifs_tcon * tcon,__u64 persistent_fid,__u64 volatile_fid,__u16 net_fid,struct cifsInodeInfo * cinode)2682 smb2_oplock_response(struct cifs_tcon *tcon, __u64 persistent_fid,
2683 __u64 volatile_fid, __u16 net_fid, struct cifsInodeInfo *cinode)
2684 {
2685 if (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_LEASING)
2686 return SMB2_lease_break(0, tcon, cinode->lease_key,
2687 smb2_get_lease_state(cinode));
2688
2689 return SMB2_oplock_break(0, tcon, persistent_fid, volatile_fid,
2690 CIFS_CACHE_READ(cinode) ? 1 : 0);
2691 }
2692
2693 void
smb2_set_replay(struct TCP_Server_Info * server,struct smb_rqst * rqst)2694 smb2_set_replay(struct TCP_Server_Info *server, struct smb_rqst *rqst)
2695 {
2696 struct smb2_hdr *shdr;
2697
2698 if (server->dialect < SMB30_PROT_ID)
2699 return;
2700
2701 shdr = (struct smb2_hdr *)(rqst->rq_iov[0].iov_base);
2702 if (shdr == NULL) {
2703 cifs_dbg(FYI, "shdr NULL in smb2_set_related\n");
2704 return;
2705 }
2706 shdr->Flags |= SMB2_FLAGS_REPLAY_OPERATION;
2707 }
2708
2709 void
smb2_set_related(struct smb_rqst * rqst)2710 smb2_set_related(struct smb_rqst *rqst)
2711 {
2712 struct smb2_hdr *shdr;
2713
2714 shdr = (struct smb2_hdr *)(rqst->rq_iov[0].iov_base);
2715 if (shdr == NULL) {
2716 cifs_dbg(FYI, "shdr NULL in smb2_set_related\n");
2717 return;
2718 }
2719 shdr->Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
2720 }
2721
2722 char smb2_padding[7] = {0, 0, 0, 0, 0, 0, 0};
2723
2724 void
smb2_set_next_command(struct cifs_tcon * tcon,struct smb_rqst * rqst)2725 smb2_set_next_command(struct cifs_tcon *tcon, struct smb_rqst *rqst)
2726 {
2727 struct smb2_hdr *shdr;
2728 struct cifs_ses *ses = tcon->ses;
2729 struct TCP_Server_Info *server = ses->server;
2730 unsigned long len = smb_rqst_len(server, rqst);
2731 int num_padding;
2732
2733 shdr = (struct smb2_hdr *)(rqst->rq_iov[0].iov_base);
2734 if (shdr == NULL) {
2735 cifs_dbg(FYI, "shdr NULL in smb2_set_next_command\n");
2736 return;
2737 }
2738
2739 /* SMB headers in a compound are 8 byte aligned. */
2740 if (IS_ALIGNED(len, 8))
2741 goto out;
2742
2743 num_padding = 8 - (len & 7);
2744 if (smb3_encryption_required(tcon)) {
2745 int i;
2746
2747 /*
2748 * Flatten request into a single buffer with required padding as
2749 * the encryption layer can't handle the padding iovs.
2750 */
2751 for (i = 1; i < rqst->rq_nvec; i++) {
2752 memcpy(rqst->rq_iov[0].iov_base +
2753 rqst->rq_iov[0].iov_len,
2754 rqst->rq_iov[i].iov_base,
2755 rqst->rq_iov[i].iov_len);
2756 rqst->rq_iov[0].iov_len += rqst->rq_iov[i].iov_len;
2757 }
2758 memset(rqst->rq_iov[0].iov_base + rqst->rq_iov[0].iov_len,
2759 0, num_padding);
2760 rqst->rq_iov[0].iov_len += num_padding;
2761 rqst->rq_nvec = 1;
2762 } else {
2763 rqst->rq_iov[rqst->rq_nvec].iov_base = smb2_padding;
2764 rqst->rq_iov[rqst->rq_nvec].iov_len = num_padding;
2765 rqst->rq_nvec++;
2766 }
2767 len += num_padding;
2768 out:
2769 shdr->NextCommand = cpu_to_le32(len);
2770 }
2771
2772 /*
2773 * helper function for exponential backoff and check if replayable
2774 */
smb2_should_replay(struct cifs_tcon * tcon,int * pretries,int * pcur_sleep)2775 bool smb2_should_replay(struct cifs_tcon *tcon,
2776 int *pretries,
2777 int *pcur_sleep)
2778 {
2779 if (!pretries || !pcur_sleep)
2780 return false;
2781
2782 if (tcon->retry || (*pretries)++ < tcon->ses->server->retrans) {
2783 msleep(*pcur_sleep);
2784 (*pcur_sleep) = ((*pcur_sleep) << 1);
2785 if ((*pcur_sleep) > CIFS_MAX_SLEEP)
2786 (*pcur_sleep) = CIFS_MAX_SLEEP;
2787 return true;
2788 }
2789
2790 return false;
2791 }
2792
2793 /*
2794 * Passes the query info response back to the caller on success.
2795 * Caller need to free this with free_rsp_buf().
2796 */
2797 int
smb2_query_info_compound(const unsigned int xid,struct cifs_tcon * tcon,const char * path,u32 desired_access,u32 class,u32 type,u32 output_len,struct kvec * rsp,int * buftype,struct cifs_sb_info * cifs_sb)2798 smb2_query_info_compound(const unsigned int xid, struct cifs_tcon *tcon,
2799 const char *path, u32 desired_access,
2800 u32 class, u32 type, u32 output_len,
2801 struct kvec *rsp, int *buftype,
2802 struct cifs_sb_info *cifs_sb)
2803 {
2804 struct smb2_compound_vars *vars;
2805 struct cifs_ses *ses = tcon->ses;
2806 struct TCP_Server_Info *server;
2807 int flags = CIFS_CP_CREATE_CLOSE_OP;
2808 struct smb_rqst *rqst;
2809 int resp_buftype[3];
2810 struct kvec *rsp_iov;
2811 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2812 struct cifs_open_parms oparms;
2813 struct cifs_fid fid;
2814 int rc;
2815 __le16 *utf16_path;
2816 struct cached_fid *cfid;
2817 int retries = 0, cur_sleep = 1;
2818
2819 replay_again:
2820 /* reinitialize for possible replay */
2821 cfid = NULL;
2822 flags = CIFS_CP_CREATE_CLOSE_OP;
2823 oplock = SMB2_OPLOCK_LEVEL_NONE;
2824 server = cifs_pick_channel(ses);
2825
2826 if (!path)
2827 path = "";
2828 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
2829 if (!utf16_path)
2830 return -ENOMEM;
2831
2832 if (smb3_encryption_required(tcon))
2833 flags |= CIFS_TRANSFORM_REQ;
2834
2835 resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
2836 vars = kzalloc(sizeof(*vars), GFP_KERNEL);
2837 if (!vars) {
2838 rc = -ENOMEM;
2839 goto out_free_path;
2840 }
2841 rqst = vars->rqst;
2842 rsp_iov = vars->rsp_iov;
2843
2844 /*
2845 * We can only call this for things we know are directories.
2846 */
2847 if (!strcmp(path, ""))
2848 open_cached_dir(xid, tcon, path, cifs_sb, false,
2849 &cfid); /* cfid null if open dir failed */
2850
2851 rqst[0].rq_iov = vars->open_iov;
2852 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
2853
2854 oparms = (struct cifs_open_parms) {
2855 .tcon = tcon,
2856 .path = path,
2857 .desired_access = desired_access,
2858 .disposition = FILE_OPEN,
2859 .create_options = cifs_create_options(cifs_sb, 0),
2860 .fid = &fid,
2861 .replay = !!(retries),
2862 };
2863
2864 rc = SMB2_open_init(tcon, server,
2865 &rqst[0], &oplock, &oparms, utf16_path);
2866 if (rc)
2867 goto qic_exit;
2868 smb2_set_next_command(tcon, &rqst[0]);
2869
2870 rqst[1].rq_iov = &vars->qi_iov;
2871 rqst[1].rq_nvec = 1;
2872
2873 if (cfid) {
2874 rc = SMB2_query_info_init(tcon, server,
2875 &rqst[1],
2876 cfid->fid.persistent_fid,
2877 cfid->fid.volatile_fid,
2878 class, type, 0,
2879 output_len, 0,
2880 NULL);
2881 } else {
2882 rc = SMB2_query_info_init(tcon, server,
2883 &rqst[1],
2884 COMPOUND_FID,
2885 COMPOUND_FID,
2886 class, type, 0,
2887 output_len, 0,
2888 NULL);
2889 }
2890 if (rc)
2891 goto qic_exit;
2892 if (!cfid) {
2893 smb2_set_next_command(tcon, &rqst[1]);
2894 smb2_set_related(&rqst[1]);
2895 }
2896
2897 rqst[2].rq_iov = &vars->close_iov;
2898 rqst[2].rq_nvec = 1;
2899
2900 rc = SMB2_close_init(tcon, server,
2901 &rqst[2], COMPOUND_FID, COMPOUND_FID, false);
2902 if (rc)
2903 goto qic_exit;
2904 smb2_set_related(&rqst[2]);
2905
2906 if (retries) {
2907 if (!cfid) {
2908 smb2_set_replay(server, &rqst[0]);
2909 smb2_set_replay(server, &rqst[2]);
2910 }
2911 smb2_set_replay(server, &rqst[1]);
2912 }
2913
2914 if (cfid) {
2915 rc = compound_send_recv(xid, ses, server,
2916 flags, 1, &rqst[1],
2917 &resp_buftype[1], &rsp_iov[1]);
2918 } else {
2919 rc = compound_send_recv(xid, ses, server,
2920 flags, 3, rqst,
2921 resp_buftype, rsp_iov);
2922 }
2923 if (rc) {
2924 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
2925 if (rc == -EREMCHG) {
2926 tcon->need_reconnect = true;
2927 pr_warn_once("server share %s deleted\n",
2928 tcon->tree_name);
2929 }
2930 goto qic_exit;
2931 }
2932 *rsp = rsp_iov[1];
2933 *buftype = resp_buftype[1];
2934
2935 qic_exit:
2936 SMB2_open_free(&rqst[0]);
2937 SMB2_query_info_free(&rqst[1]);
2938 SMB2_close_free(&rqst[2]);
2939 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
2940 free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
2941 if (cfid)
2942 close_cached_dir(cfid);
2943 kfree(vars);
2944 out_free_path:
2945 kfree(utf16_path);
2946
2947 if (is_replayable_error(rc) &&
2948 smb2_should_replay(tcon, &retries, &cur_sleep))
2949 goto replay_again;
2950
2951 return rc;
2952 }
2953
2954 static int
smb2_queryfs(const unsigned int xid,struct cifs_tcon * tcon,const char * path,struct cifs_sb_info * cifs_sb,struct kstatfs * buf)2955 smb2_queryfs(const unsigned int xid, struct cifs_tcon *tcon,
2956 const char *path, struct cifs_sb_info *cifs_sb, struct kstatfs *buf)
2957 {
2958 struct smb2_query_info_rsp *rsp;
2959 struct smb2_fs_full_size_info *info = NULL;
2960 struct kvec rsp_iov = {NULL, 0};
2961 int buftype = CIFS_NO_BUFFER;
2962 int rc;
2963
2964
2965 rc = smb2_query_info_compound(xid, tcon, path,
2966 FILE_READ_ATTRIBUTES,
2967 FS_FULL_SIZE_INFORMATION,
2968 SMB2_O_INFO_FILESYSTEM,
2969 sizeof(struct smb2_fs_full_size_info),
2970 &rsp_iov, &buftype, cifs_sb);
2971 if (rc)
2972 goto qfs_exit;
2973
2974 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
2975 buf->f_type = SMB2_SUPER_MAGIC;
2976 info = (struct smb2_fs_full_size_info *)(
2977 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
2978 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
2979 le32_to_cpu(rsp->OutputBufferLength),
2980 &rsp_iov,
2981 sizeof(struct smb2_fs_full_size_info));
2982 if (!rc)
2983 smb2_copy_fs_info_to_kstatfs(info, buf);
2984
2985 qfs_exit:
2986 trace_smb3_qfs_done(xid, tcon->tid, tcon->ses->Suid, tcon->tree_name, rc);
2987 free_rsp_buf(buftype, rsp_iov.iov_base);
2988 return rc;
2989 }
2990
2991 static int
smb311_queryfs(const unsigned int xid,struct cifs_tcon * tcon,const char * path,struct cifs_sb_info * cifs_sb,struct kstatfs * buf)2992 smb311_queryfs(const unsigned int xid, struct cifs_tcon *tcon,
2993 const char *path, struct cifs_sb_info *cifs_sb, struct kstatfs *buf)
2994 {
2995 int rc;
2996 __le16 *utf16_path = NULL;
2997 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2998 struct cifs_open_parms oparms;
2999 struct cifs_fid fid;
3000
3001 if (!tcon->posix_extensions)
3002 return smb2_queryfs(xid, tcon, path, cifs_sb, buf);
3003
3004 oparms = (struct cifs_open_parms) {
3005 .tcon = tcon,
3006 .path = path,
3007 .desired_access = FILE_READ_ATTRIBUTES,
3008 .disposition = FILE_OPEN,
3009 .create_options = cifs_create_options(cifs_sb, 0),
3010 .fid = &fid,
3011 };
3012
3013 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
3014 if (utf16_path == NULL)
3015 return -ENOMEM;
3016
3017 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL,
3018 NULL, NULL);
3019 kfree(utf16_path);
3020 if (rc)
3021 return rc;
3022
3023 rc = SMB311_posix_qfs_info(xid, tcon, fid.persistent_fid,
3024 fid.volatile_fid, buf);
3025 buf->f_type = SMB2_SUPER_MAGIC;
3026 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
3027 return rc;
3028 }
3029
3030 static bool
smb2_compare_fids(struct cifsFileInfo * ob1,struct cifsFileInfo * ob2)3031 smb2_compare_fids(struct cifsFileInfo *ob1, struct cifsFileInfo *ob2)
3032 {
3033 return ob1->fid.persistent_fid == ob2->fid.persistent_fid &&
3034 ob1->fid.volatile_fid == ob2->fid.volatile_fid;
3035 }
3036
3037 static int
smb2_mand_lock(const unsigned int xid,struct cifsFileInfo * cfile,__u64 offset,__u64 length,__u32 type,int lock,int unlock,bool wait)3038 smb2_mand_lock(const unsigned int xid, struct cifsFileInfo *cfile, __u64 offset,
3039 __u64 length, __u32 type, int lock, int unlock, bool wait)
3040 {
3041 if (unlock && !lock)
3042 type = SMB2_LOCKFLAG_UNLOCK;
3043 return SMB2_lock(xid, tlink_tcon(cfile->tlink),
3044 cfile->fid.persistent_fid, cfile->fid.volatile_fid,
3045 current->tgid, length, offset, type, wait);
3046 }
3047
3048 static void
smb2_get_lease_key(struct inode * inode,struct cifs_fid * fid)3049 smb2_get_lease_key(struct inode *inode, struct cifs_fid *fid)
3050 {
3051 memcpy(fid->lease_key, CIFS_I(inode)->lease_key, SMB2_LEASE_KEY_SIZE);
3052 }
3053
3054 static void
smb2_set_lease_key(struct inode * inode,struct cifs_fid * fid)3055 smb2_set_lease_key(struct inode *inode, struct cifs_fid *fid)
3056 {
3057 memcpy(CIFS_I(inode)->lease_key, fid->lease_key, SMB2_LEASE_KEY_SIZE);
3058 }
3059
3060 static void
smb2_new_lease_key(struct cifs_fid * fid)3061 smb2_new_lease_key(struct cifs_fid *fid)
3062 {
3063 generate_random_uuid(fid->lease_key);
3064 }
3065
3066 static int
smb2_get_dfs_refer(const unsigned int xid,struct cifs_ses * ses,const char * search_name,struct dfs_info3_param ** target_nodes,unsigned int * num_of_nodes,const struct nls_table * nls_codepage,int remap)3067 smb2_get_dfs_refer(const unsigned int xid, struct cifs_ses *ses,
3068 const char *search_name,
3069 struct dfs_info3_param **target_nodes,
3070 unsigned int *num_of_nodes,
3071 const struct nls_table *nls_codepage, int remap)
3072 {
3073 int rc;
3074 __le16 *utf16_path = NULL;
3075 int utf16_path_len = 0;
3076 struct cifs_tcon *tcon;
3077 struct fsctl_get_dfs_referral_req *dfs_req = NULL;
3078 struct get_dfs_referral_rsp *dfs_rsp = NULL;
3079 u32 dfs_req_size = 0, dfs_rsp_size = 0;
3080 int retry_once = 0;
3081
3082 cifs_dbg(FYI, "%s: path: %s\n", __func__, search_name);
3083
3084 /*
3085 * Try to use the IPC tcon, otherwise just use any
3086 */
3087 tcon = ses->tcon_ipc;
3088 if (tcon == NULL) {
3089 spin_lock(&cifs_tcp_ses_lock);
3090 tcon = list_first_entry_or_null(&ses->tcon_list,
3091 struct cifs_tcon,
3092 tcon_list);
3093 if (tcon) {
3094 tcon->tc_count++;
3095 trace_smb3_tcon_ref(tcon->debug_id, tcon->tc_count,
3096 netfs_trace_tcon_ref_get_dfs_refer);
3097 }
3098 spin_unlock(&cifs_tcp_ses_lock);
3099 }
3100
3101 if (tcon == NULL) {
3102 cifs_dbg(VFS, "session %p has no tcon available for a dfs referral request\n",
3103 ses);
3104 rc = -ENOTCONN;
3105 goto out;
3106 }
3107
3108 utf16_path = cifs_strndup_to_utf16(search_name, PATH_MAX,
3109 &utf16_path_len,
3110 nls_codepage, remap);
3111 if (!utf16_path) {
3112 rc = -ENOMEM;
3113 goto out;
3114 }
3115
3116 dfs_req_size = sizeof(*dfs_req) + utf16_path_len;
3117 dfs_req = kzalloc(dfs_req_size, GFP_KERNEL);
3118 if (!dfs_req) {
3119 rc = -ENOMEM;
3120 goto out;
3121 }
3122
3123 /* Highest DFS referral version understood */
3124 dfs_req->MaxReferralLevel = DFS_VERSION;
3125
3126 /* Path to resolve in an UTF-16 null-terminated string */
3127 memcpy(dfs_req->RequestFileName, utf16_path, utf16_path_len);
3128
3129 for (;;) {
3130 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
3131 FSCTL_DFS_GET_REFERRALS,
3132 (char *)dfs_req, dfs_req_size, CIFSMaxBufSize,
3133 (char **)&dfs_rsp, &dfs_rsp_size);
3134 if (fatal_signal_pending(current)) {
3135 rc = -EINTR;
3136 break;
3137 }
3138 if (!is_retryable_error(rc) || retry_once++)
3139 break;
3140 usleep_range(512, 2048);
3141 }
3142
3143 if (!rc && !dfs_rsp)
3144 rc = smb_EIO(smb_eio_trace_dfsref_no_rsp);
3145 if (rc) {
3146 if (!is_retryable_error(rc) && rc != -ENOENT && rc != -EOPNOTSUPP)
3147 cifs_tcon_dbg(FYI, "%s: ioctl error: rc=%d\n", __func__, rc);
3148 goto out;
3149 }
3150
3151 rc = parse_dfs_referrals(dfs_rsp, dfs_rsp_size,
3152 num_of_nodes, target_nodes,
3153 nls_codepage, remap, search_name,
3154 true /* is_unicode */);
3155 if (rc && rc != -ENOENT) {
3156 cifs_tcon_dbg(VFS, "%s: failed to parse DFS referral %s: %d\n",
3157 __func__, search_name, rc);
3158 }
3159
3160 out:
3161 if (tcon && !tcon->ipc) {
3162 /* ipc tcons are not refcounted */
3163 spin_lock(&cifs_tcp_ses_lock);
3164 tcon->tc_count--;
3165 trace_smb3_tcon_ref(tcon->debug_id, tcon->tc_count,
3166 netfs_trace_tcon_ref_dec_dfs_refer);
3167 /* tc_count can never go negative */
3168 WARN_ON(tcon->tc_count < 0);
3169 spin_unlock(&cifs_tcp_ses_lock);
3170 }
3171 kfree(utf16_path);
3172 kfree(dfs_req);
3173 kfree(dfs_rsp);
3174 return rc;
3175 }
3176
3177 static struct smb_ntsd *
get_smb2_acl_by_fid(struct cifs_sb_info * cifs_sb,const struct cifs_fid * cifsfid,u32 * pacllen,u32 info)3178 get_smb2_acl_by_fid(struct cifs_sb_info *cifs_sb,
3179 const struct cifs_fid *cifsfid, u32 *pacllen, u32 info)
3180 {
3181 struct smb_ntsd *pntsd = NULL;
3182 unsigned int xid;
3183 int rc = -EOPNOTSUPP;
3184 struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
3185
3186 if (IS_ERR(tlink))
3187 return ERR_CAST(tlink);
3188
3189 xid = get_xid();
3190 cifs_dbg(FYI, "trying to get acl\n");
3191
3192 rc = SMB2_query_acl(xid, tlink_tcon(tlink), cifsfid->persistent_fid,
3193 cifsfid->volatile_fid, (void **)&pntsd, pacllen,
3194 info);
3195 free_xid(xid);
3196
3197 cifs_put_tlink(tlink);
3198
3199 cifs_dbg(FYI, "%s: rc = %d ACL len %d\n", __func__, rc, *pacllen);
3200 if (rc)
3201 return ERR_PTR(rc);
3202 return pntsd;
3203
3204 }
3205
3206 static struct smb_ntsd *
get_smb2_acl_by_path(struct cifs_sb_info * cifs_sb,const char * path,u32 * pacllen,u32 info)3207 get_smb2_acl_by_path(struct cifs_sb_info *cifs_sb,
3208 const char *path, u32 *pacllen, u32 info)
3209 {
3210 struct smb_ntsd *pntsd = NULL;
3211 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
3212 unsigned int xid;
3213 int rc;
3214 struct cifs_tcon *tcon;
3215 struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
3216 struct cifs_fid fid;
3217 struct cifs_open_parms oparms;
3218 __le16 *utf16_path;
3219
3220 cifs_dbg(FYI, "get smb3 acl for path %s\n", path);
3221 if (IS_ERR(tlink))
3222 return ERR_CAST(tlink);
3223
3224 tcon = tlink_tcon(tlink);
3225 xid = get_xid();
3226
3227 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
3228 if (!utf16_path) {
3229 rc = -ENOMEM;
3230 goto put_tlink;
3231 }
3232
3233 oparms = (struct cifs_open_parms) {
3234 .tcon = tcon,
3235 .path = path,
3236 .desired_access = READ_CONTROL,
3237 .disposition = FILE_OPEN,
3238 /*
3239 * When querying an ACL, even if the file is a symlink
3240 * we want to open the source not the target, and so
3241 * the protocol requires that the client specify this
3242 * flag when opening a reparse point
3243 */
3244 .create_options = cifs_create_options(cifs_sb, 0) |
3245 OPEN_REPARSE_POINT,
3246 .fid = &fid,
3247 };
3248
3249 if (info & SACL_SECINFO)
3250 oparms.desired_access |= SYSTEM_SECURITY;
3251
3252 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, NULL,
3253 NULL);
3254 kfree(utf16_path);
3255 if (!rc) {
3256 rc = SMB2_query_acl(xid, tlink_tcon(tlink), fid.persistent_fid,
3257 fid.volatile_fid, (void **)&pntsd, pacllen,
3258 info);
3259 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
3260 }
3261
3262 put_tlink:
3263 cifs_put_tlink(tlink);
3264 free_xid(xid);
3265
3266 cifs_dbg(FYI, "%s: rc = %d ACL len %d\n", __func__, rc, *pacllen);
3267 if (rc)
3268 return ERR_PTR(rc);
3269 return pntsd;
3270 }
3271
3272 static int
set_smb2_acl(struct smb_ntsd * pnntsd,__u32 acllen,struct inode * inode,const char * path,int aclflag)3273 set_smb2_acl(struct smb_ntsd *pnntsd, __u32 acllen,
3274 struct inode *inode, const char *path, int aclflag)
3275 {
3276 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
3277 unsigned int xid;
3278 int rc, access_flags = 0;
3279 struct cifs_tcon *tcon;
3280 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
3281 struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
3282 struct cifs_fid fid;
3283 struct cifs_open_parms oparms;
3284 __le16 *utf16_path;
3285
3286 cifs_dbg(FYI, "set smb3 acl for path %s\n", path);
3287 if (IS_ERR(tlink))
3288 return PTR_ERR(tlink);
3289
3290 tcon = tlink_tcon(tlink);
3291 xid = get_xid();
3292
3293 if (aclflag & CIFS_ACL_OWNER || aclflag & CIFS_ACL_GROUP)
3294 access_flags |= WRITE_OWNER;
3295 if (aclflag & CIFS_ACL_SACL)
3296 access_flags |= SYSTEM_SECURITY;
3297 if (aclflag & CIFS_ACL_DACL)
3298 access_flags |= WRITE_DAC;
3299
3300 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
3301 if (!utf16_path) {
3302 rc = -ENOMEM;
3303 goto put_tlink;
3304 }
3305
3306 oparms = (struct cifs_open_parms) {
3307 .tcon = tcon,
3308 .desired_access = access_flags,
3309 .create_options = cifs_create_options(cifs_sb, 0),
3310 .disposition = FILE_OPEN,
3311 .path = path,
3312 .fid = &fid,
3313 };
3314
3315 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL,
3316 NULL, NULL);
3317 kfree(utf16_path);
3318 if (!rc) {
3319 rc = SMB2_set_acl(xid, tlink_tcon(tlink), fid.persistent_fid,
3320 fid.volatile_fid, pnntsd, acllen, aclflag);
3321 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
3322 }
3323
3324 put_tlink:
3325 cifs_put_tlink(tlink);
3326 free_xid(xid);
3327 return rc;
3328 }
3329
3330 /* Retrieve an ACL from the server */
3331 static struct smb_ntsd *
get_smb2_acl(struct cifs_sb_info * cifs_sb,struct inode * inode,const char * path,u32 * pacllen,u32 info)3332 get_smb2_acl(struct cifs_sb_info *cifs_sb,
3333 struct inode *inode, const char *path,
3334 u32 *pacllen, u32 info)
3335 {
3336 struct smb_ntsd *pntsd = NULL;
3337 struct cifsFileInfo *open_file = NULL;
3338
3339 if (inode && !(info & SACL_SECINFO))
3340 open_file = find_readable_file(CIFS_I(inode), true);
3341 if (!open_file || (info & SACL_SECINFO))
3342 return get_smb2_acl_by_path(cifs_sb, path, pacllen, info);
3343
3344 pntsd = get_smb2_acl_by_fid(cifs_sb, &open_file->fid, pacllen, info);
3345 cifsFileInfo_put(open_file);
3346 return pntsd;
3347 }
3348
smb3_zero_data(struct file * file,struct cifs_tcon * tcon,loff_t offset,loff_t len,unsigned int xid)3349 static long smb3_zero_data(struct file *file, struct cifs_tcon *tcon,
3350 loff_t offset, loff_t len, unsigned int xid)
3351 {
3352 struct cifsFileInfo *cfile = file->private_data;
3353 struct file_zero_data_information fsctl_buf;
3354
3355 cifs_dbg(FYI, "Offset %lld len %lld\n", offset, len);
3356
3357 fsctl_buf.FileOffset = cpu_to_le64(offset);
3358 fsctl_buf.BeyondFinalZero = cpu_to_le64(offset + len);
3359
3360 return SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
3361 cfile->fid.volatile_fid, FSCTL_SET_ZERO_DATA,
3362 (char *)&fsctl_buf,
3363 sizeof(struct file_zero_data_information),
3364 0, NULL, NULL);
3365 }
3366
smb3_zero_range(struct file * file,struct cifs_tcon * tcon,unsigned long long offset,unsigned long long len,bool keep_size)3367 static long smb3_zero_range(struct file *file, struct cifs_tcon *tcon,
3368 unsigned long long offset, unsigned long long len,
3369 bool keep_size)
3370 {
3371 struct cifs_ses *ses = tcon->ses;
3372 struct inode *inode = file_inode(file);
3373 struct cifsInodeInfo *cifsi = CIFS_I(inode);
3374 struct cifsFileInfo *cfile = file->private_data;
3375 struct netfs_inode *ictx = netfs_inode(inode);
3376 unsigned long long i_size, new_size, remote_size;
3377 long rc;
3378 unsigned int xid;
3379
3380 xid = get_xid();
3381
3382 trace_smb3_zero_enter(xid, cfile->fid.persistent_fid, tcon->tid,
3383 ses->Suid, offset, len);
3384
3385 filemap_invalidate_lock(inode->i_mapping);
3386
3387 i_size = i_size_read(inode);
3388 remote_size = ictx->remote_i_size;
3389 if (offset + len >= remote_size && offset < i_size) {
3390 unsigned long long top = umin(offset + len, i_size);
3391
3392 rc = filemap_write_and_wait_range(inode->i_mapping, offset, top - 1);
3393 if (rc < 0)
3394 goto zero_range_exit;
3395 }
3396
3397 /*
3398 * We zero the range through ioctl, so we need remove the page caches
3399 * first, otherwise the data may be inconsistent with the server.
3400 */
3401 truncate_pagecache_range(inode, offset, offset + len - 1);
3402 netfs_wait_for_outstanding_io(inode);
3403
3404 /* if file not oplocked can't be sure whether asking to extend size */
3405 rc = -EOPNOTSUPP;
3406 if (keep_size == false && !CIFS_CACHE_READ(cifsi))
3407 goto zero_range_exit;
3408
3409 rc = smb3_zero_data(file, tcon, offset, len, xid);
3410 if (rc < 0)
3411 goto zero_range_exit;
3412
3413 /*
3414 * do we also need to change the size of the file?
3415 */
3416 new_size = offset + len;
3417 if (keep_size == false && (unsigned long long)i_size_read(inode) < new_size) {
3418 rc = SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
3419 cfile->fid.volatile_fid, cfile->pid, new_size);
3420 if (rc >= 0) {
3421 truncate_setsize(inode, new_size);
3422 netfs_resize_file(&cifsi->netfs, new_size, true);
3423 if (offset < cifsi->netfs.zero_point)
3424 cifsi->netfs.zero_point = offset;
3425 fscache_resize_cookie(cifs_inode_cookie(inode), new_size);
3426 }
3427 }
3428
3429 zero_range_exit:
3430 filemap_invalidate_unlock(inode->i_mapping);
3431 free_xid(xid);
3432 if (rc)
3433 trace_smb3_zero_err(xid, cfile->fid.persistent_fid, tcon->tid,
3434 ses->Suid, offset, len, rc);
3435 else
3436 trace_smb3_zero_done(xid, cfile->fid.persistent_fid, tcon->tid,
3437 ses->Suid, offset, len);
3438 return rc;
3439 }
3440
smb3_punch_hole(struct file * file,struct cifs_tcon * tcon,loff_t offset,loff_t len)3441 static long smb3_punch_hole(struct file *file, struct cifs_tcon *tcon,
3442 loff_t offset, loff_t len)
3443 {
3444 struct inode *inode = file_inode(file);
3445 struct cifsFileInfo *cfile = file->private_data;
3446 struct file_zero_data_information fsctl_buf;
3447 unsigned long long end = offset + len, i_size, remote_i_size;
3448 long rc;
3449 unsigned int xid;
3450 __u8 set_sparse = 1;
3451
3452 xid = get_xid();
3453
3454 /* Need to make file sparse, if not already, before freeing range. */
3455 /* Consider adding equivalent for compressed since it could also work */
3456 if (!smb2_set_sparse(xid, tcon, cfile, inode, set_sparse)) {
3457 rc = -EOPNOTSUPP;
3458 goto out;
3459 }
3460
3461 filemap_invalidate_lock(inode->i_mapping);
3462 /*
3463 * We implement the punch hole through ioctl, so we need remove the page
3464 * caches first, otherwise the data may be inconsistent with the server.
3465 */
3466 truncate_pagecache_range(inode, offset, offset + len - 1);
3467 netfs_wait_for_outstanding_io(inode);
3468
3469 cifs_dbg(FYI, "Offset %lld len %lld\n", offset, len);
3470
3471 fsctl_buf.FileOffset = cpu_to_le64(offset);
3472 fsctl_buf.BeyondFinalZero = cpu_to_le64(offset + len);
3473
3474 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
3475 cfile->fid.volatile_fid, FSCTL_SET_ZERO_DATA,
3476 (char *)&fsctl_buf,
3477 sizeof(struct file_zero_data_information),
3478 CIFSMaxBufSize, NULL, NULL);
3479
3480 if (rc)
3481 goto unlock;
3482
3483 /* If there's dirty data in the buffer that would extend the EOF if it
3484 * were written, then we need to move the EOF marker over to the lower
3485 * of the high end of the hole and the proposed EOF. The problem is
3486 * that we locally hole-punch the tail of the dirty data, the proposed
3487 * EOF update will end up in the wrong place.
3488 */
3489 i_size = i_size_read(inode);
3490 remote_i_size = netfs_inode(inode)->remote_i_size;
3491 if (end > remote_i_size && i_size > remote_i_size) {
3492 unsigned long long extend_to = umin(end, i_size);
3493 rc = SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
3494 cfile->fid.volatile_fid, cfile->pid, extend_to);
3495 if (rc >= 0)
3496 netfs_inode(inode)->remote_i_size = extend_to;
3497 }
3498
3499 unlock:
3500 filemap_invalidate_unlock(inode->i_mapping);
3501 out:
3502 free_xid(xid);
3503 return rc;
3504 }
3505
smb3_simple_fallocate_write_range(unsigned int xid,struct cifs_tcon * tcon,struct cifsFileInfo * cfile,loff_t off,loff_t len,char * buf)3506 static int smb3_simple_fallocate_write_range(unsigned int xid,
3507 struct cifs_tcon *tcon,
3508 struct cifsFileInfo *cfile,
3509 loff_t off, loff_t len,
3510 char *buf)
3511 {
3512 struct cifs_io_parms io_parms = {0};
3513 int nbytes;
3514 int rc = 0;
3515 struct kvec iov[2];
3516
3517 io_parms.netfid = cfile->fid.netfid;
3518 io_parms.pid = current->tgid;
3519 io_parms.tcon = tcon;
3520 io_parms.persistent_fid = cfile->fid.persistent_fid;
3521 io_parms.volatile_fid = cfile->fid.volatile_fid;
3522
3523 while (len) {
3524 io_parms.offset = off;
3525 io_parms.length = len;
3526 if (io_parms.length > SMB2_MAX_BUFFER_SIZE)
3527 io_parms.length = SMB2_MAX_BUFFER_SIZE;
3528 /* iov[0] is reserved for smb header */
3529 iov[1].iov_base = buf;
3530 iov[1].iov_len = io_parms.length;
3531 rc = SMB2_write(xid, &io_parms, &nbytes, iov, 1);
3532 if (rc)
3533 break;
3534 if (nbytes > len)
3535 return -EINVAL;
3536 buf += nbytes;
3537 off += nbytes;
3538 len -= nbytes;
3539 }
3540 return rc;
3541 }
3542
smb3_simple_fallocate_range(unsigned int xid,struct cifs_tcon * tcon,struct cifsFileInfo * cfile,loff_t off,loff_t len)3543 static int smb3_simple_fallocate_range(unsigned int xid,
3544 struct cifs_tcon *tcon,
3545 struct cifsFileInfo *cfile,
3546 loff_t off, loff_t len)
3547 {
3548 struct file_allocated_range_buffer in_data, *out_data = NULL, *tmp_data;
3549 u32 out_data_len;
3550 char *buf = NULL;
3551 loff_t l;
3552 int rc;
3553
3554 in_data.file_offset = cpu_to_le64(off);
3555 in_data.length = cpu_to_le64(len);
3556 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
3557 cfile->fid.volatile_fid,
3558 FSCTL_QUERY_ALLOCATED_RANGES,
3559 (char *)&in_data, sizeof(in_data),
3560 1024 * sizeof(struct file_allocated_range_buffer),
3561 (char **)&out_data, &out_data_len);
3562 if (rc)
3563 goto out;
3564
3565 buf = kzalloc(1024 * 1024, GFP_KERNEL);
3566 if (buf == NULL) {
3567 rc = -ENOMEM;
3568 goto out;
3569 }
3570
3571 tmp_data = out_data;
3572 while (len) {
3573 /*
3574 * The rest of the region is unmapped so write it all.
3575 */
3576 if (out_data_len == 0) {
3577 rc = smb3_simple_fallocate_write_range(xid, tcon,
3578 cfile, off, len, buf);
3579 goto out;
3580 }
3581
3582 if (out_data_len < sizeof(struct file_allocated_range_buffer)) {
3583 rc = -EINVAL;
3584 goto out;
3585 }
3586
3587 if (off < le64_to_cpu(tmp_data->file_offset)) {
3588 /*
3589 * We are at a hole. Write until the end of the region
3590 * or until the next allocated data,
3591 * whichever comes next.
3592 */
3593 l = le64_to_cpu(tmp_data->file_offset) - off;
3594 if (len < l)
3595 l = len;
3596 rc = smb3_simple_fallocate_write_range(xid, tcon,
3597 cfile, off, l, buf);
3598 if (rc)
3599 goto out;
3600 off = off + l;
3601 len = len - l;
3602 if (len == 0)
3603 goto out;
3604 }
3605 /*
3606 * We are at a section of allocated data, just skip forward
3607 * until the end of the data or the end of the region
3608 * we are supposed to fallocate, whichever comes first.
3609 */
3610 l = le64_to_cpu(tmp_data->length);
3611 if (len < l)
3612 l = len;
3613 off += l;
3614 len -= l;
3615
3616 tmp_data = &tmp_data[1];
3617 out_data_len -= sizeof(struct file_allocated_range_buffer);
3618 }
3619
3620 out:
3621 kfree(out_data);
3622 kfree(buf);
3623 return rc;
3624 }
3625
3626
smb3_simple_falloc(struct file * file,struct cifs_tcon * tcon,loff_t off,loff_t len,bool keep_size)3627 static long smb3_simple_falloc(struct file *file, struct cifs_tcon *tcon,
3628 loff_t off, loff_t len, bool keep_size)
3629 {
3630 struct inode *inode;
3631 struct cifsInodeInfo *cifsi;
3632 struct cifsFileInfo *cfile = file->private_data;
3633 long rc = -EOPNOTSUPP;
3634 unsigned int xid;
3635 loff_t new_eof;
3636
3637 xid = get_xid();
3638
3639 inode = d_inode(cfile->dentry);
3640 cifsi = CIFS_I(inode);
3641
3642 trace_smb3_falloc_enter(xid, cfile->fid.persistent_fid, tcon->tid,
3643 tcon->ses->Suid, off, len);
3644 /* if file not oplocked can't be sure whether asking to extend size */
3645 if (!CIFS_CACHE_READ(cifsi))
3646 if (keep_size == false) {
3647 trace_smb3_falloc_err(xid, cfile->fid.persistent_fid,
3648 tcon->tid, tcon->ses->Suid, off, len, rc);
3649 free_xid(xid);
3650 return rc;
3651 }
3652
3653 /*
3654 * Extending the file
3655 */
3656 if ((keep_size == false) && i_size_read(inode) < off + len) {
3657 rc = inode_newsize_ok(inode, off + len);
3658 if (rc)
3659 goto out;
3660
3661 if (cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE)
3662 smb2_set_sparse(xid, tcon, cfile, inode, false);
3663
3664 new_eof = off + len;
3665 rc = SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
3666 cfile->fid.volatile_fid, cfile->pid, new_eof);
3667 if (rc == 0) {
3668 netfs_resize_file(&cifsi->netfs, new_eof, true);
3669 cifs_setsize(inode, new_eof);
3670 }
3671 goto out;
3672 }
3673
3674 /*
3675 * Files are non-sparse by default so falloc may be a no-op
3676 * Must check if file sparse. If not sparse, and since we are not
3677 * extending then no need to do anything since file already allocated
3678 */
3679 if ((cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) == 0) {
3680 rc = 0;
3681 goto out;
3682 }
3683
3684 if (keep_size == true) {
3685 /*
3686 * We can not preallocate pages beyond the end of the file
3687 * in SMB2
3688 */
3689 if (off >= i_size_read(inode)) {
3690 rc = 0;
3691 goto out;
3692 }
3693 /*
3694 * For fallocates that are partially beyond the end of file,
3695 * clamp len so we only fallocate up to the end of file.
3696 */
3697 if (off + len > i_size_read(inode)) {
3698 len = i_size_read(inode) - off;
3699 }
3700 }
3701
3702 if ((keep_size == true) || (i_size_read(inode) >= off + len)) {
3703 /*
3704 * At this point, we are trying to fallocate an internal
3705 * regions of a sparse file. Since smb2 does not have a
3706 * fallocate command we have two options on how to emulate this.
3707 * We can either turn the entire file to become non-sparse
3708 * which we only do if the fallocate is for virtually
3709 * the whole file, or we can overwrite the region with zeroes
3710 * using SMB2_write, which could be prohibitevly expensive
3711 * if len is large.
3712 */
3713 /*
3714 * We are only trying to fallocate a small region so
3715 * just write it with zero.
3716 */
3717 if (len <= 1024 * 1024) {
3718 rc = smb3_simple_fallocate_range(xid, tcon, cfile,
3719 off, len);
3720 goto out;
3721 }
3722
3723 /*
3724 * Check if falloc starts within first few pages of file
3725 * and ends within a few pages of the end of file to
3726 * ensure that most of file is being forced to be
3727 * fallocated now. If so then setting whole file sparse
3728 * ie potentially making a few extra pages at the beginning
3729 * or end of the file non-sparse via set_sparse is harmless.
3730 */
3731 if ((off > 8192) || (off + len + 8192 < i_size_read(inode))) {
3732 rc = -EOPNOTSUPP;
3733 goto out;
3734 }
3735 }
3736
3737 smb2_set_sparse(xid, tcon, cfile, inode, false);
3738 rc = 0;
3739
3740 out:
3741 if (rc)
3742 trace_smb3_falloc_err(xid, cfile->fid.persistent_fid, tcon->tid,
3743 tcon->ses->Suid, off, len, rc);
3744 else
3745 trace_smb3_falloc_done(xid, cfile->fid.persistent_fid, tcon->tid,
3746 tcon->ses->Suid, off, len);
3747
3748 free_xid(xid);
3749 return rc;
3750 }
3751
smb3_collapse_range(struct file * file,struct cifs_tcon * tcon,loff_t off,loff_t len)3752 static long smb3_collapse_range(struct file *file, struct cifs_tcon *tcon,
3753 loff_t off, loff_t len)
3754 {
3755 int rc;
3756 unsigned int xid;
3757 struct inode *inode = file_inode(file);
3758 struct cifsInodeInfo *cifsi = CIFS_I(inode);
3759 struct cifsFileInfo *cfile = file->private_data;
3760 struct netfs_inode *ictx = &cifsi->netfs;
3761 loff_t old_eof, new_eof;
3762
3763 xid = get_xid();
3764
3765 old_eof = i_size_read(inode);
3766 if ((off >= old_eof) ||
3767 off + len >= old_eof) {
3768 rc = -EINVAL;
3769 goto out;
3770 }
3771
3772 filemap_invalidate_lock(inode->i_mapping);
3773 rc = filemap_write_and_wait_range(inode->i_mapping, off, old_eof - 1);
3774 if (rc < 0)
3775 goto out_2;
3776
3777 truncate_pagecache_range(inode, off, old_eof);
3778 ictx->zero_point = old_eof;
3779 netfs_wait_for_outstanding_io(inode);
3780
3781 rc = smb2_copychunk_range(xid, cfile, cfile, off + len,
3782 old_eof - off - len, off);
3783 if (rc < 0)
3784 goto out_2;
3785
3786 new_eof = old_eof - len;
3787 rc = SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
3788 cfile->fid.volatile_fid, cfile->pid, new_eof);
3789 if (rc < 0)
3790 goto out_2;
3791
3792 rc = 0;
3793
3794 truncate_setsize(inode, new_eof);
3795 netfs_resize_file(&cifsi->netfs, new_eof, true);
3796 ictx->zero_point = new_eof;
3797 fscache_resize_cookie(cifs_inode_cookie(inode), new_eof);
3798 out_2:
3799 filemap_invalidate_unlock(inode->i_mapping);
3800 out:
3801 free_xid(xid);
3802 return rc;
3803 }
3804
smb3_insert_range(struct file * file,struct cifs_tcon * tcon,loff_t off,loff_t len)3805 static long smb3_insert_range(struct file *file, struct cifs_tcon *tcon,
3806 loff_t off, loff_t len)
3807 {
3808 int rc;
3809 unsigned int xid;
3810 struct cifsFileInfo *cfile = file->private_data;
3811 struct inode *inode = file_inode(file);
3812 struct cifsInodeInfo *cifsi = CIFS_I(inode);
3813 __u64 count, old_eof, new_eof;
3814
3815 xid = get_xid();
3816
3817 old_eof = i_size_read(inode);
3818 if (off >= old_eof) {
3819 rc = -EINVAL;
3820 goto out;
3821 }
3822
3823 count = old_eof - off;
3824 new_eof = old_eof + len;
3825
3826 filemap_invalidate_lock(inode->i_mapping);
3827 rc = filemap_write_and_wait_range(inode->i_mapping, off, new_eof - 1);
3828 if (rc < 0)
3829 goto out_2;
3830 truncate_pagecache_range(inode, off, old_eof);
3831 netfs_wait_for_outstanding_io(inode);
3832
3833 rc = SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
3834 cfile->fid.volatile_fid, cfile->pid, new_eof);
3835 if (rc < 0)
3836 goto out_2;
3837
3838 truncate_setsize(inode, new_eof);
3839 netfs_resize_file(&cifsi->netfs, i_size_read(inode), true);
3840 fscache_resize_cookie(cifs_inode_cookie(inode), i_size_read(inode));
3841
3842 rc = smb2_copychunk_range(xid, cfile, cfile, off, count, off + len);
3843 if (rc < 0)
3844 goto out_2;
3845 cifsi->netfs.zero_point = new_eof;
3846
3847 rc = smb3_zero_data(file, tcon, off, len, xid);
3848 if (rc < 0)
3849 goto out_2;
3850
3851 rc = 0;
3852 out_2:
3853 filemap_invalidate_unlock(inode->i_mapping);
3854 out:
3855 free_xid(xid);
3856 return rc;
3857 }
3858
smb3_llseek(struct file * file,struct cifs_tcon * tcon,loff_t offset,int whence)3859 static loff_t smb3_llseek(struct file *file, struct cifs_tcon *tcon, loff_t offset, int whence)
3860 {
3861 struct cifsFileInfo *wrcfile, *cfile = file->private_data;
3862 struct cifsInodeInfo *cifsi;
3863 struct inode *inode;
3864 int rc = 0;
3865 struct file_allocated_range_buffer in_data, *out_data = NULL;
3866 u32 out_data_len;
3867 unsigned int xid;
3868
3869 if (whence != SEEK_HOLE && whence != SEEK_DATA)
3870 return generic_file_llseek(file, offset, whence);
3871
3872 inode = d_inode(cfile->dentry);
3873 cifsi = CIFS_I(inode);
3874
3875 if (offset < 0 || offset >= i_size_read(inode))
3876 return -ENXIO;
3877
3878 xid = get_xid();
3879 /*
3880 * We need to be sure that all dirty pages are written as they
3881 * might fill holes on the server.
3882 * Note that we also MUST flush any written pages since at least
3883 * some servers (Windows2016) will not reflect recent writes in
3884 * QUERY_ALLOCATED_RANGES until SMB2_flush is called.
3885 */
3886 wrcfile = find_writable_file(cifsi, FIND_WR_ANY);
3887 if (wrcfile) {
3888 filemap_write_and_wait(inode->i_mapping);
3889 smb2_flush_file(xid, tcon, &wrcfile->fid);
3890 cifsFileInfo_put(wrcfile);
3891 }
3892
3893 if (!(cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE)) {
3894 if (whence == SEEK_HOLE)
3895 offset = i_size_read(inode);
3896 goto lseek_exit;
3897 }
3898
3899 in_data.file_offset = cpu_to_le64(offset);
3900 in_data.length = cpu_to_le64(i_size_read(inode));
3901
3902 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
3903 cfile->fid.volatile_fid,
3904 FSCTL_QUERY_ALLOCATED_RANGES,
3905 (char *)&in_data, sizeof(in_data),
3906 sizeof(struct file_allocated_range_buffer),
3907 (char **)&out_data, &out_data_len);
3908 if (rc == -E2BIG)
3909 rc = 0;
3910 if (rc)
3911 goto lseek_exit;
3912
3913 if (whence == SEEK_HOLE && out_data_len == 0)
3914 goto lseek_exit;
3915
3916 if (whence == SEEK_DATA && out_data_len == 0) {
3917 rc = -ENXIO;
3918 goto lseek_exit;
3919 }
3920
3921 if (out_data_len < sizeof(struct file_allocated_range_buffer)) {
3922 rc = -EINVAL;
3923 goto lseek_exit;
3924 }
3925 if (whence == SEEK_DATA) {
3926 offset = le64_to_cpu(out_data->file_offset);
3927 goto lseek_exit;
3928 }
3929 if (offset < le64_to_cpu(out_data->file_offset))
3930 goto lseek_exit;
3931
3932 offset = le64_to_cpu(out_data->file_offset) + le64_to_cpu(out_data->length);
3933
3934 lseek_exit:
3935 free_xid(xid);
3936 kfree(out_data);
3937 if (!rc)
3938 return vfs_setpos(file, offset, inode->i_sb->s_maxbytes);
3939 else
3940 return rc;
3941 }
3942
smb3_fiemap(struct cifs_tcon * tcon,struct cifsFileInfo * cfile,struct fiemap_extent_info * fei,u64 start,u64 len)3943 static int smb3_fiemap(struct cifs_tcon *tcon,
3944 struct cifsFileInfo *cfile,
3945 struct fiemap_extent_info *fei, u64 start, u64 len)
3946 {
3947 unsigned int xid;
3948 struct file_allocated_range_buffer in_data, *out_data;
3949 u32 out_data_len;
3950 int i, num, rc, flags, last_blob;
3951 u64 next;
3952
3953 rc = fiemap_prep(d_inode(cfile->dentry), fei, start, &len, 0);
3954 if (rc)
3955 return rc;
3956
3957 xid = get_xid();
3958 again:
3959 in_data.file_offset = cpu_to_le64(start);
3960 in_data.length = cpu_to_le64(len);
3961
3962 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
3963 cfile->fid.volatile_fid,
3964 FSCTL_QUERY_ALLOCATED_RANGES,
3965 (char *)&in_data, sizeof(in_data),
3966 1024 * sizeof(struct file_allocated_range_buffer),
3967 (char **)&out_data, &out_data_len);
3968 if (rc == -E2BIG) {
3969 last_blob = 0;
3970 rc = 0;
3971 } else
3972 last_blob = 1;
3973 if (rc)
3974 goto out;
3975
3976 if (out_data_len && out_data_len < sizeof(struct file_allocated_range_buffer)) {
3977 rc = -EINVAL;
3978 goto out;
3979 }
3980 if (out_data_len % sizeof(struct file_allocated_range_buffer)) {
3981 rc = -EINVAL;
3982 goto out;
3983 }
3984
3985 num = out_data_len / sizeof(struct file_allocated_range_buffer);
3986 for (i = 0; i < num; i++) {
3987 flags = 0;
3988 if (i == num - 1 && last_blob)
3989 flags |= FIEMAP_EXTENT_LAST;
3990
3991 rc = fiemap_fill_next_extent(fei,
3992 le64_to_cpu(out_data[i].file_offset),
3993 le64_to_cpu(out_data[i].file_offset),
3994 le64_to_cpu(out_data[i].length),
3995 flags);
3996 if (rc < 0)
3997 goto out;
3998 if (rc == 1) {
3999 rc = 0;
4000 goto out;
4001 }
4002 }
4003
4004 if (!last_blob) {
4005 next = le64_to_cpu(out_data[num - 1].file_offset) +
4006 le64_to_cpu(out_data[num - 1].length);
4007 len = len - (next - start);
4008 start = next;
4009 goto again;
4010 }
4011
4012 out:
4013 free_xid(xid);
4014 kfree(out_data);
4015 return rc;
4016 }
4017
smb3_fallocate(struct file * file,struct cifs_tcon * tcon,int mode,loff_t off,loff_t len)4018 static long smb3_fallocate(struct file *file, struct cifs_tcon *tcon, int mode,
4019 loff_t off, loff_t len)
4020 {
4021 /* KEEP_SIZE already checked for by do_fallocate */
4022 if (mode & FALLOC_FL_PUNCH_HOLE)
4023 return smb3_punch_hole(file, tcon, off, len);
4024 else if (mode & FALLOC_FL_ZERO_RANGE) {
4025 if (mode & FALLOC_FL_KEEP_SIZE)
4026 return smb3_zero_range(file, tcon, off, len, true);
4027 return smb3_zero_range(file, tcon, off, len, false);
4028 } else if (mode == FALLOC_FL_KEEP_SIZE)
4029 return smb3_simple_falloc(file, tcon, off, len, true);
4030 else if (mode == FALLOC_FL_COLLAPSE_RANGE)
4031 return smb3_collapse_range(file, tcon, off, len);
4032 else if (mode == FALLOC_FL_INSERT_RANGE)
4033 return smb3_insert_range(file, tcon, off, len);
4034 else if (mode == 0)
4035 return smb3_simple_falloc(file, tcon, off, len, false);
4036
4037 return -EOPNOTSUPP;
4038 }
4039
4040 static void
smb2_downgrade_oplock(struct TCP_Server_Info * server,struct cifsInodeInfo * cinode,__u32 oplock,__u16 epoch,bool * purge_cache)4041 smb2_downgrade_oplock(struct TCP_Server_Info *server,
4042 struct cifsInodeInfo *cinode, __u32 oplock,
4043 __u16 epoch, bool *purge_cache)
4044 {
4045 server->ops->set_oplock_level(cinode, oplock, 0, NULL);
4046 }
4047
4048 static void
4049 smb21_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
4050 __u16 epoch, bool *purge_cache);
4051
4052 static void
smb3_downgrade_oplock(struct TCP_Server_Info * server,struct cifsInodeInfo * cinode,__u32 oplock,__u16 epoch,bool * purge_cache)4053 smb3_downgrade_oplock(struct TCP_Server_Info *server,
4054 struct cifsInodeInfo *cinode, __u32 oplock,
4055 __u16 epoch, bool *purge_cache)
4056 {
4057 unsigned int old_state = cinode->oplock;
4058 __u16 old_epoch = cinode->epoch;
4059 unsigned int new_state;
4060
4061 if (epoch > old_epoch) {
4062 smb21_set_oplock_level(cinode, oplock, 0, NULL);
4063 cinode->epoch = epoch;
4064 }
4065
4066 new_state = cinode->oplock;
4067 *purge_cache = false;
4068
4069 if ((old_state & CIFS_CACHE_READ_FLG) != 0 &&
4070 (new_state & CIFS_CACHE_READ_FLG) == 0)
4071 *purge_cache = true;
4072 else if (old_state == new_state && (epoch - old_epoch > 1))
4073 *purge_cache = true;
4074 }
4075
4076 static void
smb2_set_oplock_level(struct cifsInodeInfo * cinode,__u32 oplock,__u16 epoch,bool * purge_cache)4077 smb2_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
4078 __u16 epoch, bool *purge_cache)
4079 {
4080 oplock &= 0xFF;
4081 cinode->lease_granted = false;
4082 if (oplock == SMB2_OPLOCK_LEVEL_NOCHANGE)
4083 return;
4084 if (oplock == SMB2_OPLOCK_LEVEL_BATCH) {
4085 cinode->oplock = CIFS_CACHE_RHW_FLG;
4086 cifs_dbg(FYI, "Batch Oplock granted on inode %p\n",
4087 &cinode->netfs.inode);
4088 } else if (oplock == SMB2_OPLOCK_LEVEL_EXCLUSIVE) {
4089 cinode->oplock = CIFS_CACHE_RW_FLG;
4090 cifs_dbg(FYI, "Exclusive Oplock granted on inode %p\n",
4091 &cinode->netfs.inode);
4092 } else if (oplock == SMB2_OPLOCK_LEVEL_II) {
4093 cinode->oplock = CIFS_CACHE_READ_FLG;
4094 cifs_dbg(FYI, "Level II Oplock granted on inode %p\n",
4095 &cinode->netfs.inode);
4096 } else
4097 cinode->oplock = 0;
4098 }
4099
4100 static void
smb21_set_oplock_level(struct cifsInodeInfo * cinode,__u32 oplock,__u16 epoch,bool * purge_cache)4101 smb21_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
4102 __u16 epoch, bool *purge_cache)
4103 {
4104 char message[5] = {0};
4105 unsigned int new_oplock = 0;
4106
4107 oplock &= 0xFF;
4108 cinode->lease_granted = true;
4109 if (oplock == SMB2_OPLOCK_LEVEL_NOCHANGE)
4110 return;
4111
4112 /* Check if the server granted an oplock rather than a lease */
4113 if (oplock & SMB2_OPLOCK_LEVEL_EXCLUSIVE)
4114 return smb2_set_oplock_level(cinode, oplock, epoch,
4115 purge_cache);
4116
4117 if (oplock & SMB2_LEASE_READ_CACHING_HE) {
4118 new_oplock |= CIFS_CACHE_READ_FLG;
4119 strcat(message, "R");
4120 }
4121 if (oplock & SMB2_LEASE_HANDLE_CACHING_HE) {
4122 new_oplock |= CIFS_CACHE_HANDLE_FLG;
4123 strcat(message, "H");
4124 }
4125 if (oplock & SMB2_LEASE_WRITE_CACHING_HE) {
4126 new_oplock |= CIFS_CACHE_WRITE_FLG;
4127 strcat(message, "W");
4128 }
4129 if (!new_oplock)
4130 strscpy(message, "None");
4131
4132 cinode->oplock = new_oplock;
4133 cifs_dbg(FYI, "%s Lease granted on inode %p\n", message,
4134 &cinode->netfs.inode);
4135 }
4136
4137 static void
smb3_set_oplock_level(struct cifsInodeInfo * cinode,__u32 oplock,__u16 epoch,bool * purge_cache)4138 smb3_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
4139 __u16 epoch, bool *purge_cache)
4140 {
4141 unsigned int old_oplock = cinode->oplock;
4142
4143 smb21_set_oplock_level(cinode, oplock, epoch, purge_cache);
4144
4145 if (purge_cache) {
4146 *purge_cache = false;
4147 if (old_oplock == CIFS_CACHE_READ_FLG) {
4148 if (cinode->oplock == CIFS_CACHE_READ_FLG &&
4149 (epoch - cinode->epoch > 0))
4150 *purge_cache = true;
4151 else if (cinode->oplock == CIFS_CACHE_RH_FLG &&
4152 (epoch - cinode->epoch > 1))
4153 *purge_cache = true;
4154 else if (cinode->oplock == CIFS_CACHE_RHW_FLG &&
4155 (epoch - cinode->epoch > 1))
4156 *purge_cache = true;
4157 else if (cinode->oplock == 0 &&
4158 (epoch - cinode->epoch > 0))
4159 *purge_cache = true;
4160 } else if (old_oplock == CIFS_CACHE_RH_FLG) {
4161 if (cinode->oplock == CIFS_CACHE_RH_FLG &&
4162 (epoch - cinode->epoch > 0))
4163 *purge_cache = true;
4164 else if (cinode->oplock == CIFS_CACHE_RHW_FLG &&
4165 (epoch - cinode->epoch > 1))
4166 *purge_cache = true;
4167 }
4168 cinode->epoch = epoch;
4169 }
4170 }
4171
4172 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
4173 static bool
smb2_is_read_op(__u32 oplock)4174 smb2_is_read_op(__u32 oplock)
4175 {
4176 return oplock == SMB2_OPLOCK_LEVEL_II;
4177 }
4178 #endif /* CIFS_ALLOW_INSECURE_LEGACY */
4179
4180 static bool
smb21_is_read_op(__u32 oplock)4181 smb21_is_read_op(__u32 oplock)
4182 {
4183 return (oplock & SMB2_LEASE_READ_CACHING_HE) &&
4184 !(oplock & SMB2_LEASE_WRITE_CACHING_HE);
4185 }
4186
4187 static __le32
map_oplock_to_lease(u8 oplock)4188 map_oplock_to_lease(u8 oplock)
4189 {
4190 if (oplock == SMB2_OPLOCK_LEVEL_EXCLUSIVE)
4191 return SMB2_LEASE_WRITE_CACHING_LE | SMB2_LEASE_READ_CACHING_LE;
4192 else if (oplock == SMB2_OPLOCK_LEVEL_II)
4193 return SMB2_LEASE_READ_CACHING_LE | SMB2_LEASE_HANDLE_CACHING_LE;
4194 else if (oplock == SMB2_OPLOCK_LEVEL_BATCH)
4195 return SMB2_LEASE_HANDLE_CACHING_LE | SMB2_LEASE_READ_CACHING_LE |
4196 SMB2_LEASE_WRITE_CACHING_LE;
4197 return 0;
4198 }
4199
4200 static char *
smb2_create_lease_buf(u8 * lease_key,u8 oplock,u8 * parent_lease_key,__le32 flags)4201 smb2_create_lease_buf(u8 *lease_key, u8 oplock, u8 *parent_lease_key, __le32 flags)
4202 {
4203 struct create_lease *buf;
4204
4205 buf = kzalloc(sizeof(struct create_lease), GFP_KERNEL);
4206 if (!buf)
4207 return NULL;
4208
4209 memcpy(&buf->lcontext.LeaseKey, lease_key, SMB2_LEASE_KEY_SIZE);
4210 buf->lcontext.LeaseState = map_oplock_to_lease(oplock);
4211
4212 buf->ccontext.DataOffset = cpu_to_le16(offsetof
4213 (struct create_lease, lcontext));
4214 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context));
4215 buf->ccontext.NameOffset = cpu_to_le16(offsetof
4216 (struct create_lease, Name));
4217 buf->ccontext.NameLength = cpu_to_le16(4);
4218 /* SMB2_CREATE_REQUEST_LEASE is "RqLs" */
4219 buf->Name[0] = 'R';
4220 buf->Name[1] = 'q';
4221 buf->Name[2] = 'L';
4222 buf->Name[3] = 's';
4223 return (char *)buf;
4224 }
4225
4226 static char *
smb3_create_lease_buf(u8 * lease_key,u8 oplock,u8 * parent_lease_key,__le32 flags)4227 smb3_create_lease_buf(u8 *lease_key, u8 oplock, u8 *parent_lease_key, __le32 flags)
4228 {
4229 struct create_lease_v2 *buf;
4230
4231 buf = kzalloc(sizeof(struct create_lease_v2), GFP_KERNEL);
4232 if (!buf)
4233 return NULL;
4234
4235 memcpy(&buf->lcontext.LeaseKey, lease_key, SMB2_LEASE_KEY_SIZE);
4236 buf->lcontext.LeaseState = map_oplock_to_lease(oplock);
4237 buf->lcontext.LeaseFlags = flags;
4238 if (flags & SMB2_LEASE_FLAG_PARENT_LEASE_KEY_SET_LE)
4239 memcpy(&buf->lcontext.ParentLeaseKey, parent_lease_key, SMB2_LEASE_KEY_SIZE);
4240
4241 buf->ccontext.DataOffset = cpu_to_le16(offsetof
4242 (struct create_lease_v2, lcontext));
4243 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context_v2));
4244 buf->ccontext.NameOffset = cpu_to_le16(offsetof
4245 (struct create_lease_v2, Name));
4246 buf->ccontext.NameLength = cpu_to_le16(4);
4247 /* SMB2_CREATE_REQUEST_LEASE is "RqLs" */
4248 buf->Name[0] = 'R';
4249 buf->Name[1] = 'q';
4250 buf->Name[2] = 'L';
4251 buf->Name[3] = 's';
4252 return (char *)buf;
4253 }
4254
4255 static __u8
smb2_parse_lease_buf(void * buf,__u16 * epoch,char * lease_key)4256 smb2_parse_lease_buf(void *buf, __u16 *epoch, char *lease_key)
4257 {
4258 struct create_lease *lc = (struct create_lease *)buf;
4259
4260 *epoch = 0; /* not used */
4261 if (lc->lcontext.LeaseFlags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS_LE)
4262 return SMB2_OPLOCK_LEVEL_NOCHANGE;
4263 return le32_to_cpu(lc->lcontext.LeaseState);
4264 }
4265
4266 static __u8
smb3_parse_lease_buf(void * buf,__u16 * epoch,char * lease_key)4267 smb3_parse_lease_buf(void *buf, __u16 *epoch, char *lease_key)
4268 {
4269 struct create_lease_v2 *lc = (struct create_lease_v2 *)buf;
4270
4271 *epoch = le16_to_cpu(lc->lcontext.Epoch);
4272 if (lc->lcontext.LeaseFlags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS_LE)
4273 return SMB2_OPLOCK_LEVEL_NOCHANGE;
4274 if (lease_key)
4275 memcpy(lease_key, &lc->lcontext.LeaseKey, SMB2_LEASE_KEY_SIZE);
4276 return le32_to_cpu(lc->lcontext.LeaseState);
4277 }
4278
4279 static unsigned int
smb2_wp_retry_size(struct inode * inode)4280 smb2_wp_retry_size(struct inode *inode)
4281 {
4282 return min_t(unsigned int, CIFS_SB(inode->i_sb)->ctx->wsize,
4283 SMB2_MAX_BUFFER_SIZE);
4284 }
4285
4286 static bool
smb2_dir_needs_close(struct cifsFileInfo * cfile)4287 smb2_dir_needs_close(struct cifsFileInfo *cfile)
4288 {
4289 return !cfile->invalidHandle;
4290 }
4291
4292 static void
fill_transform_hdr(struct smb2_transform_hdr * tr_hdr,unsigned int orig_len,struct smb_rqst * old_rq,__le16 cipher_type)4293 fill_transform_hdr(struct smb2_transform_hdr *tr_hdr, unsigned int orig_len,
4294 struct smb_rqst *old_rq, __le16 cipher_type)
4295 {
4296 struct smb2_hdr *shdr =
4297 (struct smb2_hdr *)old_rq->rq_iov[0].iov_base;
4298
4299 memset(tr_hdr, 0, sizeof(struct smb2_transform_hdr));
4300 tr_hdr->ProtocolId = SMB2_TRANSFORM_PROTO_NUM;
4301 tr_hdr->OriginalMessageSize = cpu_to_le32(orig_len);
4302 tr_hdr->Flags = cpu_to_le16(0x01);
4303 if ((cipher_type == SMB2_ENCRYPTION_AES128_GCM) ||
4304 (cipher_type == SMB2_ENCRYPTION_AES256_GCM))
4305 get_random_bytes(&tr_hdr->Nonce, SMB3_AES_GCM_NONCE);
4306 else
4307 get_random_bytes(&tr_hdr->Nonce, SMB3_AES_CCM_NONCE);
4308 memcpy(&tr_hdr->SessionId, &shdr->SessionId, 8);
4309 }
4310
smb2_aead_req_alloc(struct crypto_aead * tfm,const struct smb_rqst * rqst,int num_rqst,const u8 * sig,u8 ** iv,struct aead_request ** req,struct sg_table * sgt,unsigned int * num_sgs)4311 static void *smb2_aead_req_alloc(struct crypto_aead *tfm, const struct smb_rqst *rqst,
4312 int num_rqst, const u8 *sig, u8 **iv,
4313 struct aead_request **req, struct sg_table *sgt,
4314 unsigned int *num_sgs)
4315 {
4316 unsigned int req_size = sizeof(**req) + crypto_aead_reqsize(tfm);
4317 unsigned int iv_size = crypto_aead_ivsize(tfm);
4318 unsigned int len;
4319 u8 *p;
4320
4321 *num_sgs = cifs_get_num_sgs(rqst, num_rqst, sig);
4322 if (IS_ERR_VALUE((long)(int)*num_sgs))
4323 return ERR_PTR(*num_sgs);
4324
4325 len = iv_size;
4326 len += crypto_aead_alignmask(tfm) & ~(crypto_tfm_ctx_alignment() - 1);
4327 len = ALIGN(len, crypto_tfm_ctx_alignment());
4328 len += req_size;
4329 len = ALIGN(len, __alignof__(struct scatterlist));
4330 len += array_size(*num_sgs, sizeof(struct scatterlist));
4331
4332 p = kzalloc(len, GFP_NOFS);
4333 if (!p)
4334 return ERR_PTR(-ENOMEM);
4335
4336 *iv = (u8 *)PTR_ALIGN(p, crypto_aead_alignmask(tfm) + 1);
4337 *req = (struct aead_request *)PTR_ALIGN(*iv + iv_size,
4338 crypto_tfm_ctx_alignment());
4339 sgt->sgl = (struct scatterlist *)PTR_ALIGN((u8 *)*req + req_size,
4340 __alignof__(struct scatterlist));
4341 return p;
4342 }
4343
smb2_get_aead_req(struct crypto_aead * tfm,struct smb_rqst * rqst,int num_rqst,const u8 * sig,u8 ** iv,struct aead_request ** req,struct scatterlist ** sgl)4344 static void *smb2_get_aead_req(struct crypto_aead *tfm, struct smb_rqst *rqst,
4345 int num_rqst, const u8 *sig, u8 **iv,
4346 struct aead_request **req, struct scatterlist **sgl)
4347 {
4348 struct sg_table sgtable = {};
4349 unsigned int skip, num_sgs, i, j;
4350 ssize_t rc;
4351 void *p;
4352
4353 p = smb2_aead_req_alloc(tfm, rqst, num_rqst, sig, iv, req, &sgtable, &num_sgs);
4354 if (IS_ERR(p))
4355 return ERR_CAST(p);
4356
4357 sg_init_marker(sgtable.sgl, num_sgs);
4358
4359 /*
4360 * The first rqst has a transform header where the
4361 * first 20 bytes are not part of the encrypted blob.
4362 */
4363 skip = 20;
4364
4365 for (i = 0; i < num_rqst; i++) {
4366 struct iov_iter *iter = &rqst[i].rq_iter;
4367 size_t count = iov_iter_count(iter);
4368
4369 for (j = 0; j < rqst[i].rq_nvec; j++) {
4370 cifs_sg_set_buf(&sgtable,
4371 rqst[i].rq_iov[j].iov_base + skip,
4372 rqst[i].rq_iov[j].iov_len - skip);
4373
4374 /* See the above comment on the 'skip' assignment */
4375 skip = 0;
4376 }
4377 sgtable.orig_nents = sgtable.nents;
4378
4379 rc = extract_iter_to_sg(iter, count, &sgtable,
4380 num_sgs - sgtable.nents, 0);
4381 iov_iter_revert(iter, rc);
4382 sgtable.orig_nents = sgtable.nents;
4383 }
4384
4385 cifs_sg_set_buf(&sgtable, sig, SMB2_SIGNATURE_SIZE);
4386 sg_mark_end(&sgtable.sgl[sgtable.nents - 1]);
4387 *sgl = sgtable.sgl;
4388 return p;
4389 }
4390
4391 static int
smb2_get_enc_key(struct TCP_Server_Info * server,__u64 ses_id,int enc,u8 * key)4392 smb2_get_enc_key(struct TCP_Server_Info *server, __u64 ses_id, int enc, u8 *key)
4393 {
4394 struct TCP_Server_Info *pserver;
4395 struct cifs_ses *ses;
4396 u8 *ses_enc_key;
4397
4398 /* If server is a channel, select the primary channel */
4399 pserver = SERVER_IS_CHAN(server) ? server->primary_server : server;
4400
4401 spin_lock(&cifs_tcp_ses_lock);
4402 list_for_each_entry(ses, &pserver->smb_ses_list, smb_ses_list) {
4403 if (ses->Suid == ses_id) {
4404 spin_lock(&ses->ses_lock);
4405 ses_enc_key = enc ? ses->smb3encryptionkey :
4406 ses->smb3decryptionkey;
4407 memcpy(key, ses_enc_key, SMB3_ENC_DEC_KEY_SIZE);
4408 spin_unlock(&ses->ses_lock);
4409 spin_unlock(&cifs_tcp_ses_lock);
4410 return 0;
4411 }
4412 }
4413 spin_unlock(&cifs_tcp_ses_lock);
4414
4415 trace_smb3_ses_not_found(ses_id);
4416
4417 return -EAGAIN;
4418 }
4419 /*
4420 * Encrypt or decrypt @rqst message. @rqst[0] has the following format:
4421 * iov[0] - transform header (associate data),
4422 * iov[1-N] - SMB2 header and pages - data to encrypt.
4423 * On success return encrypted data in iov[1-N] and pages, leave iov[0]
4424 * untouched.
4425 */
4426 static int
crypt_message(struct TCP_Server_Info * server,int num_rqst,struct smb_rqst * rqst,int enc,struct crypto_aead * tfm)4427 crypt_message(struct TCP_Server_Info *server, int num_rqst,
4428 struct smb_rqst *rqst, int enc, struct crypto_aead *tfm)
4429 {
4430 struct smb2_transform_hdr *tr_hdr =
4431 (struct smb2_transform_hdr *)rqst[0].rq_iov[0].iov_base;
4432 unsigned int assoc_data_len = sizeof(struct smb2_transform_hdr) - 20;
4433 int rc = 0;
4434 struct scatterlist *sg;
4435 u8 sign[SMB2_SIGNATURE_SIZE] = {};
4436 u8 key[SMB3_ENC_DEC_KEY_SIZE];
4437 struct aead_request *req;
4438 u8 *iv;
4439 DECLARE_CRYPTO_WAIT(wait);
4440 unsigned int crypt_len = le32_to_cpu(tr_hdr->OriginalMessageSize);
4441 void *creq;
4442
4443 rc = smb2_get_enc_key(server, le64_to_cpu(tr_hdr->SessionId), enc, key);
4444 if (rc) {
4445 cifs_server_dbg(FYI, "%s: Could not get %scryption key. sid: 0x%llx\n", __func__,
4446 enc ? "en" : "de", le64_to_cpu(tr_hdr->SessionId));
4447 return rc;
4448 }
4449
4450 if ((server->cipher_type == SMB2_ENCRYPTION_AES256_CCM) ||
4451 (server->cipher_type == SMB2_ENCRYPTION_AES256_GCM))
4452 rc = crypto_aead_setkey(tfm, key, SMB3_GCM256_CRYPTKEY_SIZE);
4453 else
4454 rc = crypto_aead_setkey(tfm, key, SMB3_GCM128_CRYPTKEY_SIZE);
4455
4456 if (rc) {
4457 cifs_server_dbg(VFS, "%s: Failed to set aead key %d\n", __func__, rc);
4458 return rc;
4459 }
4460
4461 rc = crypto_aead_setauthsize(tfm, SMB2_SIGNATURE_SIZE);
4462 if (rc) {
4463 cifs_server_dbg(VFS, "%s: Failed to set authsize %d\n", __func__, rc);
4464 return rc;
4465 }
4466
4467 creq = smb2_get_aead_req(tfm, rqst, num_rqst, sign, &iv, &req, &sg);
4468 if (IS_ERR(creq))
4469 return PTR_ERR(creq);
4470
4471 if (!enc) {
4472 memcpy(sign, &tr_hdr->Signature, SMB2_SIGNATURE_SIZE);
4473 crypt_len += SMB2_SIGNATURE_SIZE;
4474 }
4475
4476 if ((server->cipher_type == SMB2_ENCRYPTION_AES128_GCM) ||
4477 (server->cipher_type == SMB2_ENCRYPTION_AES256_GCM))
4478 memcpy(iv, (char *)tr_hdr->Nonce, SMB3_AES_GCM_NONCE);
4479 else {
4480 iv[0] = 3;
4481 memcpy(iv + 1, (char *)tr_hdr->Nonce, SMB3_AES_CCM_NONCE);
4482 }
4483
4484 aead_request_set_tfm(req, tfm);
4485 aead_request_set_crypt(req, sg, sg, crypt_len, iv);
4486 aead_request_set_ad(req, assoc_data_len);
4487
4488 aead_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
4489 crypto_req_done, &wait);
4490
4491 rc = crypto_wait_req(enc ? crypto_aead_encrypt(req)
4492 : crypto_aead_decrypt(req), &wait);
4493
4494 if (!rc && enc)
4495 memcpy(&tr_hdr->Signature, sign, SMB2_SIGNATURE_SIZE);
4496
4497 kfree_sensitive(creq);
4498 return rc;
4499 }
4500
4501 /*
4502 * Copy data from an iterator to the folios in a folio queue buffer.
4503 */
cifs_copy_iter_to_folioq(struct iov_iter * iter,size_t size,struct folio_queue * buffer)4504 static bool cifs_copy_iter_to_folioq(struct iov_iter *iter, size_t size,
4505 struct folio_queue *buffer)
4506 {
4507 for (; buffer; buffer = buffer->next) {
4508 for (int s = 0; s < folioq_count(buffer); s++) {
4509 struct folio *folio = folioq_folio(buffer, s);
4510 size_t part = folioq_folio_size(buffer, s);
4511
4512 part = umin(part, size);
4513
4514 if (copy_folio_from_iter(folio, 0, part, iter) != part)
4515 return false;
4516 size -= part;
4517 }
4518 }
4519 return true;
4520 }
4521
4522 void
smb3_free_compound_rqst(int num_rqst,struct smb_rqst * rqst)4523 smb3_free_compound_rqst(int num_rqst, struct smb_rqst *rqst)
4524 {
4525 for (int i = 0; i < num_rqst; i++)
4526 netfs_free_folioq_buffer(rqst[i].rq_buffer);
4527 }
4528
4529 /*
4530 * This function will initialize new_rq and encrypt the content.
4531 * The first entry, new_rq[0], only contains a single iov which contains
4532 * a smb2_transform_hdr and is pre-allocated by the caller.
4533 * This function then populates new_rq[1+] with the content from olq_rq[0+].
4534 *
4535 * The end result is an array of smb_rqst structures where the first structure
4536 * only contains a single iov for the transform header which we then can pass
4537 * to crypt_message().
4538 *
4539 * new_rq[0].rq_iov[0] : smb2_transform_hdr pre-allocated by the caller
4540 * new_rq[1+].rq_iov[*] == old_rq[0+].rq_iov[*] : SMB2/3 requests
4541 */
4542 static int
smb3_init_transform_rq(struct TCP_Server_Info * server,int num_rqst,struct smb_rqst * new_rq,struct smb_rqst * old_rq)4543 smb3_init_transform_rq(struct TCP_Server_Info *server, int num_rqst,
4544 struct smb_rqst *new_rq, struct smb_rqst *old_rq)
4545 {
4546 struct smb2_transform_hdr *tr_hdr = new_rq[0].rq_iov[0].iov_base;
4547 unsigned int orig_len = 0;
4548 int rc = -ENOMEM;
4549
4550 for (int i = 1; i < num_rqst; i++) {
4551 struct smb_rqst *old = &old_rq[i - 1];
4552 struct smb_rqst *new = &new_rq[i];
4553 struct folio_queue *buffer = NULL;
4554 size_t size = iov_iter_count(&old->rq_iter);
4555
4556 orig_len += smb_rqst_len(server, old);
4557 new->rq_iov = old->rq_iov;
4558 new->rq_nvec = old->rq_nvec;
4559
4560 if (size > 0) {
4561 size_t cur_size = 0;
4562 rc = netfs_alloc_folioq_buffer(NULL, &buffer, &cur_size,
4563 size, GFP_NOFS);
4564 if (rc < 0)
4565 goto err_free;
4566
4567 new->rq_buffer = buffer;
4568 iov_iter_folio_queue(&new->rq_iter, ITER_SOURCE,
4569 buffer, 0, 0, size);
4570
4571 if (!cifs_copy_iter_to_folioq(&old->rq_iter, size, buffer)) {
4572 rc = smb_EIO1(smb_eio_trace_tx_copy_iter_to_buf, size);
4573 goto err_free;
4574 }
4575 }
4576 }
4577
4578 /* fill the 1st iov with a transform header */
4579 fill_transform_hdr(tr_hdr, orig_len, old_rq, server->cipher_type);
4580
4581 rc = crypt_message(server, num_rqst, new_rq, 1, server->secmech.enc);
4582 cifs_dbg(FYI, "Encrypt message returned %d\n", rc);
4583 if (rc)
4584 goto err_free;
4585
4586 return rc;
4587
4588 err_free:
4589 smb3_free_compound_rqst(num_rqst - 1, &new_rq[1]);
4590 return rc;
4591 }
4592
4593 static int
smb3_is_transform_hdr(void * buf)4594 smb3_is_transform_hdr(void *buf)
4595 {
4596 struct smb2_transform_hdr *trhdr = buf;
4597
4598 return trhdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM;
4599 }
4600
4601 static int
decrypt_raw_data(struct TCP_Server_Info * server,char * buf,unsigned int buf_data_size,struct iov_iter * iter,bool is_offloaded)4602 decrypt_raw_data(struct TCP_Server_Info *server, char *buf,
4603 unsigned int buf_data_size, struct iov_iter *iter,
4604 bool is_offloaded)
4605 {
4606 struct crypto_aead *tfm;
4607 struct smb_rqst rqst = {NULL};
4608 struct kvec iov[2];
4609 size_t iter_size = 0;
4610 int rc;
4611
4612 iov[0].iov_base = buf;
4613 iov[0].iov_len = sizeof(struct smb2_transform_hdr);
4614 iov[1].iov_base = buf + sizeof(struct smb2_transform_hdr);
4615 iov[1].iov_len = buf_data_size;
4616
4617 rqst.rq_iov = iov;
4618 rqst.rq_nvec = 2;
4619 if (iter) {
4620 rqst.rq_iter = *iter;
4621 iter_size = iov_iter_count(iter);
4622 }
4623
4624 if (is_offloaded) {
4625 if ((server->cipher_type == SMB2_ENCRYPTION_AES128_GCM) ||
4626 (server->cipher_type == SMB2_ENCRYPTION_AES256_GCM))
4627 tfm = crypto_alloc_aead("gcm(aes)", 0, 0);
4628 else
4629 tfm = crypto_alloc_aead("ccm(aes)", 0, 0);
4630 if (IS_ERR(tfm)) {
4631 rc = PTR_ERR(tfm);
4632 cifs_server_dbg(VFS, "%s: Failed alloc decrypt TFM, rc=%d\n", __func__, rc);
4633
4634 return rc;
4635 }
4636 } else {
4637 rc = smb3_crypto_aead_allocate(server);
4638 if (unlikely(rc))
4639 return rc;
4640 tfm = server->secmech.dec;
4641 }
4642
4643 rc = crypt_message(server, 1, &rqst, 0, tfm);
4644 cifs_dbg(FYI, "Decrypt message returned %d\n", rc);
4645
4646 if (is_offloaded)
4647 crypto_free_aead(tfm);
4648
4649 if (rc)
4650 return rc;
4651
4652 memmove(buf, iov[1].iov_base, buf_data_size);
4653
4654 if (!is_offloaded)
4655 server->total_read = buf_data_size + iter_size;
4656
4657 return rc;
4658 }
4659
4660 static int
cifs_copy_folioq_to_iter(struct folio_queue * folioq,size_t data_size,size_t skip,struct iov_iter * iter)4661 cifs_copy_folioq_to_iter(struct folio_queue *folioq, size_t data_size,
4662 size_t skip, struct iov_iter *iter)
4663 {
4664 for (; folioq; folioq = folioq->next) {
4665 for (int s = 0; s < folioq_count(folioq); s++) {
4666 struct folio *folio = folioq_folio(folioq, s);
4667 size_t fsize = folio_size(folio);
4668 size_t n, len = umin(fsize - skip, data_size);
4669
4670 n = copy_folio_to_iter(folio, skip, len, iter);
4671 if (n != len) {
4672 cifs_dbg(VFS, "%s: something went wrong\n", __func__);
4673 return smb_EIO2(smb_eio_trace_rx_copy_to_iter,
4674 n, len);
4675 }
4676 data_size -= n;
4677 skip = 0;
4678 }
4679 }
4680
4681 return 0;
4682 }
4683
4684 static int
handle_read_data(struct TCP_Server_Info * server,struct mid_q_entry * mid,char * buf,unsigned int buf_len,struct folio_queue * buffer,unsigned int buffer_len,bool is_offloaded)4685 handle_read_data(struct TCP_Server_Info *server, struct mid_q_entry *mid,
4686 char *buf, unsigned int buf_len, struct folio_queue *buffer,
4687 unsigned int buffer_len, bool is_offloaded)
4688 {
4689 unsigned int data_offset;
4690 unsigned int data_len;
4691 unsigned int cur_off;
4692 unsigned int cur_page_idx;
4693 unsigned int pad_len;
4694 struct cifs_io_subrequest *rdata = mid->callback_data;
4695 struct smb2_hdr *shdr = (struct smb2_hdr *)buf;
4696 size_t copied;
4697 bool use_rdma_mr = false;
4698
4699 if (shdr->Command != SMB2_READ) {
4700 cifs_server_dbg(VFS, "only big read responses are supported\n");
4701 return -EOPNOTSUPP;
4702 }
4703
4704 if (server->ops->is_session_expired &&
4705 server->ops->is_session_expired(buf)) {
4706 if (!is_offloaded)
4707 cifs_reconnect(server, true);
4708 return -1;
4709 }
4710
4711 if (server->ops->is_status_pending &&
4712 server->ops->is_status_pending(buf, server))
4713 return -1;
4714
4715 /* set up first two iov to get credits */
4716 rdata->iov[0].iov_base = buf;
4717 rdata->iov[0].iov_len = 0;
4718 rdata->iov[1].iov_base = buf;
4719 rdata->iov[1].iov_len =
4720 min_t(unsigned int, buf_len, server->vals->read_rsp_size);
4721 cifs_dbg(FYI, "0: iov_base=%p iov_len=%zu\n",
4722 rdata->iov[0].iov_base, rdata->iov[0].iov_len);
4723 cifs_dbg(FYI, "1: iov_base=%p iov_len=%zu\n",
4724 rdata->iov[1].iov_base, rdata->iov[1].iov_len);
4725
4726 rdata->result = server->ops->map_error(buf, true);
4727 if (rdata->result != 0) {
4728 cifs_dbg(FYI, "%s: server returned error %d\n",
4729 __func__, rdata->result);
4730 /* normal error on read response */
4731 if (is_offloaded)
4732 mid->mid_state = MID_RESPONSE_RECEIVED;
4733 else
4734 dequeue_mid(server, mid, false);
4735 return 0;
4736 }
4737
4738 data_offset = server->ops->read_data_offset(buf);
4739 #ifdef CONFIG_CIFS_SMB_DIRECT
4740 use_rdma_mr = rdata->mr;
4741 #endif
4742 data_len = server->ops->read_data_length(buf, use_rdma_mr);
4743
4744 if (data_offset < server->vals->read_rsp_size) {
4745 /*
4746 * win2k8 sometimes sends an offset of 0 when the read
4747 * is beyond the EOF. Treat it as if the data starts just after
4748 * the header.
4749 */
4750 cifs_dbg(FYI, "%s: data offset (%u) inside read response header\n",
4751 __func__, data_offset);
4752 data_offset = server->vals->read_rsp_size;
4753 } else if (data_offset > MAX_CIFS_SMALL_BUFFER_SIZE) {
4754 /* data_offset is beyond the end of smallbuf */
4755 cifs_dbg(FYI, "%s: data offset (%u) beyond end of smallbuf\n",
4756 __func__, data_offset);
4757 rdata->result = smb_EIO1(smb_eio_trace_rx_overlong, data_offset);
4758 if (is_offloaded)
4759 mid->mid_state = MID_RESPONSE_MALFORMED;
4760 else
4761 dequeue_mid(server, mid, rdata->result);
4762 return 0;
4763 }
4764
4765 pad_len = data_offset - server->vals->read_rsp_size;
4766
4767 if (buf_len <= data_offset) {
4768 /* read response payload is in pages */
4769 cur_page_idx = pad_len / PAGE_SIZE;
4770 cur_off = pad_len % PAGE_SIZE;
4771
4772 if (cur_page_idx != 0) {
4773 /* data offset is beyond the 1st page of response */
4774 cifs_dbg(FYI, "%s: data offset (%u) beyond 1st page of response\n",
4775 __func__, data_offset);
4776 rdata->result = smb_EIO1(smb_eio_trace_rx_overpage, data_offset);
4777 if (is_offloaded)
4778 mid->mid_state = MID_RESPONSE_MALFORMED;
4779 else
4780 dequeue_mid(server, mid, rdata->result);
4781 return 0;
4782 }
4783
4784 if (data_len > buffer_len - pad_len) {
4785 /* data_len is corrupt -- discard frame */
4786 rdata->result = smb_EIO1(smb_eio_trace_rx_bad_datalen, data_len);
4787 if (is_offloaded)
4788 mid->mid_state = MID_RESPONSE_MALFORMED;
4789 else
4790 dequeue_mid(server, mid, rdata->result);
4791 return 0;
4792 }
4793
4794 /* Copy the data to the output I/O iterator. */
4795 rdata->result = cifs_copy_folioq_to_iter(buffer, buffer_len,
4796 cur_off, &rdata->subreq.io_iter);
4797 if (rdata->result != 0) {
4798 if (is_offloaded)
4799 mid->mid_state = MID_RESPONSE_MALFORMED;
4800 else
4801 dequeue_mid(server, mid, rdata->result);
4802 return 0;
4803 }
4804 rdata->got_bytes = buffer_len;
4805
4806 } else if (buf_len >= data_offset + data_len) {
4807 /* read response payload is in buf */
4808 WARN_ONCE(buffer, "read data can be either in buf or in buffer");
4809 copied = copy_to_iter(buf + data_offset, data_len, &rdata->subreq.io_iter);
4810 if (copied == 0)
4811 return smb_EIO2(smb_eio_trace_rx_copy_to_iter, copied, data_len);
4812 rdata->got_bytes = copied;
4813 } else {
4814 /* read response payload cannot be in both buf and pages */
4815 WARN_ONCE(1, "buf can not contain only a part of read data");
4816 rdata->result = smb_EIO(smb_eio_trace_rx_both_buf);
4817 if (is_offloaded)
4818 mid->mid_state = MID_RESPONSE_MALFORMED;
4819 else
4820 dequeue_mid(server, mid, rdata->result);
4821 return 0;
4822 }
4823
4824 if (is_offloaded)
4825 mid->mid_state = MID_RESPONSE_RECEIVED;
4826 else
4827 dequeue_mid(server, mid, false);
4828 return 0;
4829 }
4830
4831 struct smb2_decrypt_work {
4832 struct work_struct decrypt;
4833 struct TCP_Server_Info *server;
4834 struct folio_queue *buffer;
4835 char *buf;
4836 unsigned int len;
4837 };
4838
4839
smb2_decrypt_offload(struct work_struct * work)4840 static void smb2_decrypt_offload(struct work_struct *work)
4841 {
4842 struct smb2_decrypt_work *dw = container_of(work,
4843 struct smb2_decrypt_work, decrypt);
4844 int rc;
4845 struct mid_q_entry *mid;
4846 struct iov_iter iter;
4847
4848 iov_iter_folio_queue(&iter, ITER_DEST, dw->buffer, 0, 0, dw->len);
4849 rc = decrypt_raw_data(dw->server, dw->buf, dw->server->vals->read_rsp_size,
4850 &iter, true);
4851 if (rc) {
4852 cifs_dbg(VFS, "error decrypting rc=%d\n", rc);
4853 goto free_pages;
4854 }
4855
4856 dw->server->lstrp = jiffies;
4857 mid = smb2_find_dequeue_mid(dw->server, dw->buf);
4858 if (mid == NULL)
4859 cifs_dbg(FYI, "mid not found\n");
4860 else {
4861 mid->decrypted = true;
4862 rc = handle_read_data(dw->server, mid, dw->buf,
4863 dw->server->vals->read_rsp_size,
4864 dw->buffer, dw->len,
4865 true);
4866 if (rc >= 0) {
4867 #ifdef CONFIG_CIFS_STATS2
4868 mid->when_received = jiffies;
4869 #endif
4870 if (dw->server->ops->is_network_name_deleted)
4871 dw->server->ops->is_network_name_deleted(dw->buf,
4872 dw->server);
4873
4874 mid_execute_callback(dw->server, mid);
4875 } else {
4876 spin_lock(&dw->server->srv_lock);
4877 if (dw->server->tcpStatus == CifsNeedReconnect) {
4878 spin_lock(&dw->server->mid_queue_lock);
4879 mid->mid_state = MID_RETRY_NEEDED;
4880 spin_unlock(&dw->server->mid_queue_lock);
4881 spin_unlock(&dw->server->srv_lock);
4882 mid_execute_callback(dw->server, mid);
4883 } else {
4884 spin_lock(&dw->server->mid_queue_lock);
4885 mid->mid_state = MID_REQUEST_SUBMITTED;
4886 mid->deleted_from_q = false;
4887 list_add_tail(&mid->qhead,
4888 &dw->server->pending_mid_q);
4889 spin_unlock(&dw->server->mid_queue_lock);
4890 spin_unlock(&dw->server->srv_lock);
4891 }
4892 }
4893 release_mid(dw->server, mid);
4894 }
4895
4896 free_pages:
4897 netfs_free_folioq_buffer(dw->buffer);
4898 cifs_small_buf_release(dw->buf);
4899 kfree(dw);
4900 }
4901
4902
4903 static int
receive_encrypted_read(struct TCP_Server_Info * server,struct mid_q_entry ** mid,int * num_mids)4904 receive_encrypted_read(struct TCP_Server_Info *server, struct mid_q_entry **mid,
4905 int *num_mids)
4906 {
4907 char *buf = server->smallbuf;
4908 struct smb2_transform_hdr *tr_hdr = (struct smb2_transform_hdr *)buf;
4909 struct iov_iter iter;
4910 unsigned int len;
4911 unsigned int buflen = server->pdu_size;
4912 int rc;
4913 struct smb2_decrypt_work *dw;
4914
4915 dw = kzalloc(sizeof(struct smb2_decrypt_work), GFP_KERNEL);
4916 if (!dw)
4917 return -ENOMEM;
4918 INIT_WORK(&dw->decrypt, smb2_decrypt_offload);
4919 dw->server = server;
4920
4921 *num_mids = 1;
4922 len = min_t(unsigned int, buflen, server->vals->read_rsp_size +
4923 sizeof(struct smb2_transform_hdr)) - HEADER_SIZE(server) + 1;
4924
4925 rc = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1, len);
4926 if (rc < 0)
4927 goto free_dw;
4928 server->total_read += rc;
4929
4930 len = le32_to_cpu(tr_hdr->OriginalMessageSize) -
4931 server->vals->read_rsp_size;
4932 dw->len = len;
4933 len = round_up(dw->len, PAGE_SIZE);
4934
4935 size_t cur_size = 0;
4936 rc = netfs_alloc_folioq_buffer(NULL, &dw->buffer, &cur_size, len, GFP_NOFS);
4937 if (rc < 0)
4938 goto discard_data;
4939
4940 iov_iter_folio_queue(&iter, ITER_DEST, dw->buffer, 0, 0, len);
4941
4942 /* Read the data into the buffer and clear excess bufferage. */
4943 rc = cifs_read_iter_from_socket(server, &iter, dw->len);
4944 if (rc < 0)
4945 goto discard_data;
4946
4947 server->total_read += rc;
4948 if (rc < len) {
4949 struct iov_iter tmp = iter;
4950
4951 iov_iter_advance(&tmp, rc);
4952 iov_iter_zero(len - rc, &tmp);
4953 }
4954 iov_iter_truncate(&iter, dw->len);
4955
4956 rc = cifs_discard_remaining_data(server);
4957 if (rc)
4958 goto free_pages;
4959
4960 /*
4961 * For large reads, offload to different thread for better performance,
4962 * use more cores decrypting which can be expensive
4963 */
4964
4965 if ((server->min_offload) && (server->in_flight > 1) &&
4966 (server->pdu_size >= server->min_offload)) {
4967 dw->buf = server->smallbuf;
4968 server->smallbuf = (char *)cifs_small_buf_get();
4969
4970 queue_work(decrypt_wq, &dw->decrypt);
4971 *num_mids = 0; /* worker thread takes care of finding mid */
4972 return -1;
4973 }
4974
4975 rc = decrypt_raw_data(server, buf, server->vals->read_rsp_size,
4976 &iter, false);
4977 if (rc)
4978 goto free_pages;
4979
4980 *mid = smb2_find_mid(server, buf);
4981 if (*mid == NULL) {
4982 cifs_dbg(FYI, "mid not found\n");
4983 } else {
4984 cifs_dbg(FYI, "mid found\n");
4985 (*mid)->decrypted = true;
4986 rc = handle_read_data(server, *mid, buf,
4987 server->vals->read_rsp_size,
4988 dw->buffer, dw->len, false);
4989 if (rc >= 0) {
4990 if (server->ops->is_network_name_deleted) {
4991 server->ops->is_network_name_deleted(buf,
4992 server);
4993 }
4994 }
4995 }
4996
4997 free_pages:
4998 netfs_free_folioq_buffer(dw->buffer);
4999 free_dw:
5000 kfree(dw);
5001 return rc;
5002 discard_data:
5003 cifs_discard_remaining_data(server);
5004 goto free_pages;
5005 }
5006
5007 static int
receive_encrypted_standard(struct TCP_Server_Info * server,struct mid_q_entry ** mids,char ** bufs,int * num_mids)5008 receive_encrypted_standard(struct TCP_Server_Info *server,
5009 struct mid_q_entry **mids, char **bufs,
5010 int *num_mids)
5011 {
5012 int ret, length;
5013 char *buf = server->smallbuf;
5014 struct smb2_hdr *shdr;
5015 unsigned int pdu_length = server->pdu_size;
5016 unsigned int buf_size;
5017 unsigned int next_cmd;
5018 struct mid_q_entry *mid_entry;
5019 int next_is_large;
5020 char *next_buffer = NULL;
5021
5022 *num_mids = 0;
5023
5024 /* switch to large buffer if too big for a small one */
5025 if (pdu_length > MAX_CIFS_SMALL_BUFFER_SIZE) {
5026 server->large_buf = true;
5027 memcpy(server->bigbuf, buf, server->total_read);
5028 buf = server->bigbuf;
5029 }
5030
5031 /* now read the rest */
5032 length = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1,
5033 pdu_length - HEADER_SIZE(server) + 1);
5034 if (length < 0)
5035 return length;
5036 server->total_read += length;
5037
5038 buf_size = pdu_length - sizeof(struct smb2_transform_hdr);
5039 length = decrypt_raw_data(server, buf, buf_size, NULL, false);
5040 if (length)
5041 return length;
5042
5043 next_is_large = server->large_buf;
5044 one_more:
5045 shdr = (struct smb2_hdr *)buf;
5046 next_cmd = le32_to_cpu(shdr->NextCommand);
5047 if (next_cmd) {
5048 if (WARN_ON_ONCE(next_cmd > pdu_length))
5049 return -1;
5050 if (next_is_large)
5051 next_buffer = (char *)cifs_buf_get();
5052 else
5053 next_buffer = (char *)cifs_small_buf_get();
5054 if (!next_buffer) {
5055 cifs_server_dbg(VFS, "No memory for (large) SMB response\n");
5056 return -1;
5057 }
5058 memcpy(next_buffer, buf + next_cmd, pdu_length - next_cmd);
5059 }
5060
5061 mid_entry = smb2_find_mid(server, buf);
5062 if (mid_entry == NULL)
5063 cifs_dbg(FYI, "mid not found\n");
5064 else {
5065 cifs_dbg(FYI, "mid found\n");
5066 mid_entry->decrypted = true;
5067 mid_entry->resp_buf_size = server->pdu_size;
5068 }
5069
5070 if (*num_mids >= MAX_COMPOUND) {
5071 cifs_server_dbg(VFS, "too many PDUs in compound\n");
5072 return -1;
5073 }
5074 bufs[*num_mids] = buf;
5075 mids[(*num_mids)++] = mid_entry;
5076
5077 if (mid_entry && mid_entry->handle)
5078 ret = mid_entry->handle(server, mid_entry);
5079 else
5080 ret = cifs_handle_standard(server, mid_entry);
5081
5082 if (ret == 0 && next_cmd) {
5083 pdu_length -= next_cmd;
5084 server->large_buf = next_is_large;
5085 if (next_is_large)
5086 server->bigbuf = buf = next_buffer;
5087 else
5088 server->smallbuf = buf = next_buffer;
5089 goto one_more;
5090 } else if (ret != 0) {
5091 /*
5092 * ret != 0 here means that we didn't get to handle_mid() thus
5093 * server->smallbuf and server->bigbuf are still valid. We need
5094 * to free next_buffer because it is not going to be used
5095 * anywhere.
5096 */
5097 if (next_is_large)
5098 free_rsp_buf(CIFS_LARGE_BUFFER, next_buffer);
5099 else
5100 free_rsp_buf(CIFS_SMALL_BUFFER, next_buffer);
5101 }
5102
5103 return ret;
5104 }
5105
5106 static int
smb3_receive_transform(struct TCP_Server_Info * server,struct mid_q_entry ** mids,char ** bufs,int * num_mids)5107 smb3_receive_transform(struct TCP_Server_Info *server,
5108 struct mid_q_entry **mids, char **bufs, int *num_mids)
5109 {
5110 char *buf = server->smallbuf;
5111 unsigned int pdu_length = server->pdu_size;
5112 struct smb2_transform_hdr *tr_hdr = (struct smb2_transform_hdr *)buf;
5113 unsigned int orig_len = le32_to_cpu(tr_hdr->OriginalMessageSize);
5114
5115 if (pdu_length < sizeof(struct smb2_transform_hdr) +
5116 sizeof(struct smb2_hdr)) {
5117 cifs_server_dbg(VFS, "Transform message is too small (%u)\n",
5118 pdu_length);
5119 cifs_reconnect(server, true);
5120 return -ECONNABORTED;
5121 }
5122
5123 if (pdu_length < orig_len + sizeof(struct smb2_transform_hdr)) {
5124 cifs_server_dbg(VFS, "Transform message is broken\n");
5125 cifs_reconnect(server, true);
5126 return -ECONNABORTED;
5127 }
5128
5129 /* TODO: add support for compounds containing READ. */
5130 if (pdu_length > CIFSMaxBufSize + MAX_HEADER_SIZE(server)) {
5131 return receive_encrypted_read(server, &mids[0], num_mids);
5132 }
5133
5134 return receive_encrypted_standard(server, mids, bufs, num_mids);
5135 }
5136
5137 int
smb3_handle_read_data(struct TCP_Server_Info * server,struct mid_q_entry * mid)5138 smb3_handle_read_data(struct TCP_Server_Info *server, struct mid_q_entry *mid)
5139 {
5140 char *buf = server->large_buf ? server->bigbuf : server->smallbuf;
5141
5142 return handle_read_data(server, mid, buf, server->pdu_size,
5143 NULL, 0, false);
5144 }
5145
smb2_next_header(struct TCP_Server_Info * server,char * buf,unsigned int * noff)5146 static int smb2_next_header(struct TCP_Server_Info *server, char *buf,
5147 unsigned int *noff)
5148 {
5149 struct smb2_hdr *hdr = (struct smb2_hdr *)buf;
5150 struct smb2_transform_hdr *t_hdr = (struct smb2_transform_hdr *)buf;
5151
5152 if (hdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM) {
5153 *noff = le32_to_cpu(t_hdr->OriginalMessageSize);
5154 if (unlikely(check_add_overflow(*noff, sizeof(*t_hdr), noff)))
5155 return -EINVAL;
5156 } else {
5157 *noff = le32_to_cpu(hdr->NextCommand);
5158 }
5159 if (unlikely(*noff && *noff < MID_HEADER_SIZE(server)))
5160 return -EINVAL;
5161 return 0;
5162 }
5163
__cifs_sfu_make_node(unsigned int xid,struct inode * inode,struct dentry * dentry,struct cifs_tcon * tcon,const char * full_path,umode_t mode,dev_t dev,const char * symname)5164 int __cifs_sfu_make_node(unsigned int xid, struct inode *inode,
5165 struct dentry *dentry, struct cifs_tcon *tcon,
5166 const char *full_path, umode_t mode, dev_t dev,
5167 const char *symname)
5168 {
5169 struct TCP_Server_Info *server = tcon->ses->server;
5170 struct cifs_open_parms oparms;
5171 struct cifs_open_info_data idata;
5172 struct cifs_io_parms io_parms = {};
5173 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
5174 struct cifs_fid fid;
5175 unsigned int bytes_written;
5176 u8 type[8];
5177 int type_len = 0;
5178 struct {
5179 __le64 major;
5180 __le64 minor;
5181 } __packed pdev = {};
5182 __le16 *symname_utf16 = NULL;
5183 u8 *data = NULL;
5184 int data_len = 0;
5185 struct kvec iov[3];
5186 __u32 oplock = server->oplocks ? REQ_OPLOCK : 0;
5187 int rc;
5188
5189 switch (mode & S_IFMT) {
5190 case S_IFCHR:
5191 type_len = 8;
5192 memcpy(type, "IntxCHR\0", type_len);
5193 pdev.major = cpu_to_le64(MAJOR(dev));
5194 pdev.minor = cpu_to_le64(MINOR(dev));
5195 data = (u8 *)&pdev;
5196 data_len = sizeof(pdev);
5197 break;
5198 case S_IFBLK:
5199 type_len = 8;
5200 memcpy(type, "IntxBLK\0", type_len);
5201 pdev.major = cpu_to_le64(MAJOR(dev));
5202 pdev.minor = cpu_to_le64(MINOR(dev));
5203 data = (u8 *)&pdev;
5204 data_len = sizeof(pdev);
5205 break;
5206 case S_IFLNK:
5207 type_len = 8;
5208 memcpy(type, "IntxLNK\1", type_len);
5209 symname_utf16 = cifs_strndup_to_utf16(symname, strlen(symname),
5210 &data_len, cifs_sb->local_nls,
5211 NO_MAP_UNI_RSVD);
5212 if (!symname_utf16) {
5213 rc = -ENOMEM;
5214 goto out;
5215 }
5216 data_len -= 2; /* symlink is without trailing wide-nul */
5217 data = (u8 *)symname_utf16;
5218 break;
5219 case S_IFSOCK:
5220 type_len = 8;
5221 strscpy(type, "LnxSOCK");
5222 data = (u8 *)&pdev;
5223 data_len = sizeof(pdev);
5224 break;
5225 case S_IFIFO:
5226 type_len = 8;
5227 strscpy(type, "LnxFIFO");
5228 data = (u8 *)&pdev;
5229 data_len = sizeof(pdev);
5230 break;
5231 default:
5232 rc = -EPERM;
5233 goto out;
5234 }
5235
5236 oparms = CIFS_OPARMS(cifs_sb, tcon, full_path, GENERIC_WRITE,
5237 FILE_CREATE, CREATE_NOT_DIR |
5238 CREATE_OPTION_SPECIAL, ACL_NO_MODE);
5239 oparms.fid = &fid;
5240 idata.contains_posix_file_info = false;
5241 rc = server->ops->open(xid, &oparms, &oplock, &idata);
5242 if (rc)
5243 goto out;
5244
5245 /*
5246 * Check if the server honored ATTR_SYSTEM flag by CREATE_OPTION_SPECIAL
5247 * option. If not then server does not support ATTR_SYSTEM and newly
5248 * created file is not SFU compatible, which means that the call failed.
5249 */
5250 if (!(le32_to_cpu(idata.fi.Attributes) & ATTR_SYSTEM)) {
5251 rc = -EOPNOTSUPP;
5252 goto out_close;
5253 }
5254
5255 if (type_len + data_len > 0) {
5256 io_parms.pid = current->tgid;
5257 io_parms.tcon = tcon;
5258 io_parms.length = type_len + data_len;
5259 iov[1].iov_base = type;
5260 iov[1].iov_len = type_len;
5261 iov[2].iov_base = data;
5262 iov[2].iov_len = data_len;
5263
5264 rc = server->ops->sync_write(xid, &fid, &io_parms,
5265 &bytes_written,
5266 iov, ARRAY_SIZE(iov)-1);
5267 }
5268
5269 out_close:
5270 server->ops->close(xid, tcon, &fid);
5271
5272 /*
5273 * If CREATE was successful but either setting ATTR_SYSTEM failed or
5274 * writing type/data information failed then remove the intermediate
5275 * object created by CREATE. Otherwise intermediate empty object stay
5276 * on the server.
5277 */
5278 if (rc)
5279 server->ops->unlink(xid, tcon, full_path, cifs_sb, NULL);
5280
5281 out:
5282 kfree(symname_utf16);
5283 return rc;
5284 }
5285
cifs_sfu_make_node(unsigned int xid,struct inode * inode,struct dentry * dentry,struct cifs_tcon * tcon,const char * full_path,umode_t mode,dev_t dev)5286 int cifs_sfu_make_node(unsigned int xid, struct inode *inode,
5287 struct dentry *dentry, struct cifs_tcon *tcon,
5288 const char *full_path, umode_t mode, dev_t dev)
5289 {
5290 struct inode *new = NULL;
5291 int rc;
5292
5293 rc = __cifs_sfu_make_node(xid, inode, dentry, tcon,
5294 full_path, mode, dev, NULL);
5295 if (rc)
5296 return rc;
5297
5298 if (tcon->posix_extensions) {
5299 rc = smb311_posix_get_inode_info(&new, full_path, NULL,
5300 inode->i_sb, xid);
5301 } else if (tcon->unix_ext) {
5302 rc = cifs_get_inode_info_unix(&new, full_path,
5303 inode->i_sb, xid);
5304 } else {
5305 rc = cifs_get_inode_info(&new, full_path, NULL,
5306 inode->i_sb, xid, NULL);
5307 }
5308 if (!rc)
5309 d_instantiate(dentry, new);
5310 return rc;
5311 }
5312
smb2_make_node(unsigned int xid,struct inode * inode,struct dentry * dentry,struct cifs_tcon * tcon,const char * full_path,umode_t mode,dev_t dev)5313 static int smb2_make_node(unsigned int xid, struct inode *inode,
5314 struct dentry *dentry, struct cifs_tcon *tcon,
5315 const char *full_path, umode_t mode, dev_t dev)
5316 {
5317 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
5318 int rc = -EOPNOTSUPP;
5319
5320 /*
5321 * Check if mounted with mount parm 'sfu' mount parm.
5322 * SFU emulation should work with all servers, but only
5323 * supports block and char device, socket & fifo,
5324 * and was used by default in earlier versions of Windows
5325 */
5326 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL) {
5327 rc = cifs_sfu_make_node(xid, inode, dentry, tcon,
5328 full_path, mode, dev);
5329 } else if (CIFS_REPARSE_SUPPORT(tcon)) {
5330 rc = mknod_reparse(xid, inode, dentry, tcon,
5331 full_path, mode, dev);
5332 }
5333 return rc;
5334 }
5335
5336 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
5337 struct smb_version_operations smb20_operations = {
5338 .compare_fids = smb2_compare_fids,
5339 .setup_request = smb2_setup_request,
5340 .setup_async_request = smb2_setup_async_request,
5341 .check_receive = smb2_check_receive,
5342 .add_credits = smb2_add_credits,
5343 .set_credits = smb2_set_credits,
5344 .get_credits_field = smb2_get_credits_field,
5345 .get_credits = smb2_get_credits,
5346 .wait_mtu_credits = cifs_wait_mtu_credits,
5347 .get_next_mid = smb2_get_next_mid,
5348 .revert_current_mid = smb2_revert_current_mid,
5349 .read_data_offset = smb2_read_data_offset,
5350 .read_data_length = smb2_read_data_length,
5351 .map_error = map_smb2_to_linux_error,
5352 .find_mid = smb2_find_mid,
5353 .check_message = smb2_check_message,
5354 .dump_detail = smb2_dump_detail,
5355 .clear_stats = smb2_clear_stats,
5356 .print_stats = smb2_print_stats,
5357 .is_oplock_break = smb2_is_valid_oplock_break,
5358 .handle_cancelled_mid = smb2_handle_cancelled_mid,
5359 .downgrade_oplock = smb2_downgrade_oplock,
5360 .need_neg = smb2_need_neg,
5361 .negotiate = smb2_negotiate,
5362 .negotiate_wsize = smb2_negotiate_wsize,
5363 .negotiate_rsize = smb2_negotiate_rsize,
5364 .sess_setup = SMB2_sess_setup,
5365 .logoff = SMB2_logoff,
5366 .tree_connect = SMB2_tcon,
5367 .tree_disconnect = SMB2_tdis,
5368 .qfs_tcon = smb2_qfs_tcon,
5369 .is_path_accessible = smb2_is_path_accessible,
5370 .can_echo = smb2_can_echo,
5371 .echo = SMB2_echo,
5372 .query_path_info = smb2_query_path_info,
5373 .query_reparse_point = smb2_query_reparse_point,
5374 .get_srv_inum = smb2_get_srv_inum,
5375 .query_file_info = smb2_query_file_info,
5376 .set_path_size = smb2_set_path_size,
5377 .set_file_size = smb2_set_file_size,
5378 .set_file_info = smb2_set_file_info,
5379 .set_compression = smb2_set_compression,
5380 .mkdir = smb2_mkdir,
5381 .mkdir_setinfo = smb2_mkdir_setinfo,
5382 .rmdir = smb2_rmdir,
5383 .unlink = smb2_unlink,
5384 .rename = smb2_rename_path,
5385 .create_hardlink = smb2_create_hardlink,
5386 .get_reparse_point_buffer = smb2_get_reparse_point_buffer,
5387 .query_mf_symlink = smb3_query_mf_symlink,
5388 .create_mf_symlink = smb3_create_mf_symlink,
5389 .create_reparse_inode = smb2_create_reparse_inode,
5390 .open = smb2_open_file,
5391 .set_fid = smb2_set_fid,
5392 .close = smb2_close_file,
5393 .flush = smb2_flush_file,
5394 .async_readv = smb2_async_readv,
5395 .async_writev = smb2_async_writev,
5396 .sync_read = smb2_sync_read,
5397 .sync_write = smb2_sync_write,
5398 .query_dir_first = smb2_query_dir_first,
5399 .query_dir_next = smb2_query_dir_next,
5400 .close_dir = smb2_close_dir,
5401 .calc_smb_size = smb2_calc_size,
5402 .is_status_pending = smb2_is_status_pending,
5403 .is_session_expired = smb2_is_session_expired,
5404 .oplock_response = smb2_oplock_response,
5405 .queryfs = smb2_queryfs,
5406 .mand_lock = smb2_mand_lock,
5407 .mand_unlock_range = smb2_unlock_range,
5408 .push_mand_locks = smb2_push_mandatory_locks,
5409 .get_lease_key = smb2_get_lease_key,
5410 .set_lease_key = smb2_set_lease_key,
5411 .new_lease_key = smb2_new_lease_key,
5412 .is_read_op = smb2_is_read_op,
5413 .set_oplock_level = smb2_set_oplock_level,
5414 .create_lease_buf = smb2_create_lease_buf,
5415 .parse_lease_buf = smb2_parse_lease_buf,
5416 .copychunk_range = smb2_copychunk_range,
5417 .wp_retry_size = smb2_wp_retry_size,
5418 .dir_needs_close = smb2_dir_needs_close,
5419 .get_dfs_refer = smb2_get_dfs_refer,
5420 .select_sectype = smb2_select_sectype,
5421 #ifdef CONFIG_CIFS_XATTR
5422 .query_all_EAs = smb2_query_eas,
5423 .set_EA = smb2_set_ea,
5424 #endif /* CIFS_XATTR */
5425 .get_acl = get_smb2_acl,
5426 .get_acl_by_fid = get_smb2_acl_by_fid,
5427 .set_acl = set_smb2_acl,
5428 .next_header = smb2_next_header,
5429 .ioctl_query_info = smb2_ioctl_query_info,
5430 .make_node = smb2_make_node,
5431 .fiemap = smb3_fiemap,
5432 .llseek = smb3_llseek,
5433 .is_status_io_timeout = smb2_is_status_io_timeout,
5434 .is_network_name_deleted = smb2_is_network_name_deleted,
5435 .rename_pending_delete = smb2_rename_pending_delete,
5436 };
5437 #endif /* CIFS_ALLOW_INSECURE_LEGACY */
5438
5439 struct smb_version_operations smb21_operations = {
5440 .compare_fids = smb2_compare_fids,
5441 .setup_request = smb2_setup_request,
5442 .setup_async_request = smb2_setup_async_request,
5443 .check_receive = smb2_check_receive,
5444 .add_credits = smb2_add_credits,
5445 .set_credits = smb2_set_credits,
5446 .get_credits_field = smb2_get_credits_field,
5447 .get_credits = smb2_get_credits,
5448 .wait_mtu_credits = smb2_wait_mtu_credits,
5449 .adjust_credits = smb2_adjust_credits,
5450 .get_next_mid = smb2_get_next_mid,
5451 .revert_current_mid = smb2_revert_current_mid,
5452 .read_data_offset = smb2_read_data_offset,
5453 .read_data_length = smb2_read_data_length,
5454 .map_error = map_smb2_to_linux_error,
5455 .find_mid = smb2_find_mid,
5456 .check_message = smb2_check_message,
5457 .dump_detail = smb2_dump_detail,
5458 .clear_stats = smb2_clear_stats,
5459 .print_stats = smb2_print_stats,
5460 .is_oplock_break = smb2_is_valid_oplock_break,
5461 .handle_cancelled_mid = smb2_handle_cancelled_mid,
5462 .downgrade_oplock = smb2_downgrade_oplock,
5463 .need_neg = smb2_need_neg,
5464 .negotiate = smb2_negotiate,
5465 .negotiate_wsize = smb2_negotiate_wsize,
5466 .negotiate_rsize = smb2_negotiate_rsize,
5467 .sess_setup = SMB2_sess_setup,
5468 .logoff = SMB2_logoff,
5469 .tree_connect = SMB2_tcon,
5470 .tree_disconnect = SMB2_tdis,
5471 .qfs_tcon = smb2_qfs_tcon,
5472 .is_path_accessible = smb2_is_path_accessible,
5473 .can_echo = smb2_can_echo,
5474 .echo = SMB2_echo,
5475 .query_path_info = smb2_query_path_info,
5476 .query_reparse_point = smb2_query_reparse_point,
5477 .get_srv_inum = smb2_get_srv_inum,
5478 .query_file_info = smb2_query_file_info,
5479 .set_path_size = smb2_set_path_size,
5480 .set_file_size = smb2_set_file_size,
5481 .set_file_info = smb2_set_file_info,
5482 .set_compression = smb2_set_compression,
5483 .mkdir = smb2_mkdir,
5484 .mkdir_setinfo = smb2_mkdir_setinfo,
5485 .rmdir = smb2_rmdir,
5486 .unlink = smb2_unlink,
5487 .rename = smb2_rename_path,
5488 .create_hardlink = smb2_create_hardlink,
5489 .get_reparse_point_buffer = smb2_get_reparse_point_buffer,
5490 .query_mf_symlink = smb3_query_mf_symlink,
5491 .create_mf_symlink = smb3_create_mf_symlink,
5492 .create_reparse_inode = smb2_create_reparse_inode,
5493 .open = smb2_open_file,
5494 .set_fid = smb2_set_fid,
5495 .close = smb2_close_file,
5496 .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 = smb2_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 .is_read_op = smb21_is_read_op,
5516 .set_oplock_level = smb21_set_oplock_level,
5517 .create_lease_buf = smb2_create_lease_buf,
5518 .parse_lease_buf = smb2_parse_lease_buf,
5519 .copychunk_range = smb2_copychunk_range,
5520 .wp_retry_size = smb2_wp_retry_size,
5521 .dir_needs_close = smb2_dir_needs_close,
5522 .enum_snapshots = smb3_enum_snapshots,
5523 .notify = smb3_notify,
5524 .get_dfs_refer = smb2_get_dfs_refer,
5525 .select_sectype = smb2_select_sectype,
5526 #ifdef CONFIG_CIFS_XATTR
5527 .query_all_EAs = smb2_query_eas,
5528 .set_EA = smb2_set_ea,
5529 #endif /* CIFS_XATTR */
5530 .get_acl = get_smb2_acl,
5531 .get_acl_by_fid = get_smb2_acl_by_fid,
5532 .set_acl = set_smb2_acl,
5533 .next_header = smb2_next_header,
5534 .ioctl_query_info = smb2_ioctl_query_info,
5535 .make_node = smb2_make_node,
5536 .fiemap = smb3_fiemap,
5537 .llseek = smb3_llseek,
5538 .is_status_io_timeout = smb2_is_status_io_timeout,
5539 .is_network_name_deleted = smb2_is_network_name_deleted,
5540 .rename_pending_delete = smb2_rename_pending_delete,
5541 };
5542
5543 struct smb_version_operations smb30_operations = {
5544 .compare_fids = smb2_compare_fids,
5545 .setup_request = smb2_setup_request,
5546 .setup_async_request = smb2_setup_async_request,
5547 .check_receive = smb2_check_receive,
5548 .add_credits = smb2_add_credits,
5549 .set_credits = smb2_set_credits,
5550 .get_credits_field = smb2_get_credits_field,
5551 .get_credits = smb2_get_credits,
5552 .wait_mtu_credits = smb2_wait_mtu_credits,
5553 .adjust_credits = smb2_adjust_credits,
5554 .get_next_mid = smb2_get_next_mid,
5555 .revert_current_mid = smb2_revert_current_mid,
5556 .read_data_offset = smb2_read_data_offset,
5557 .read_data_length = smb2_read_data_length,
5558 .map_error = map_smb2_to_linux_error,
5559 .find_mid = smb2_find_mid,
5560 .check_message = smb2_check_message,
5561 .dump_detail = smb2_dump_detail,
5562 .clear_stats = smb2_clear_stats,
5563 .print_stats = smb2_print_stats,
5564 .dump_share_caps = smb2_dump_share_caps,
5565 .is_oplock_break = smb2_is_valid_oplock_break,
5566 .handle_cancelled_mid = smb2_handle_cancelled_mid,
5567 .downgrade_oplock = smb3_downgrade_oplock,
5568 .need_neg = smb2_need_neg,
5569 .negotiate = smb2_negotiate,
5570 .negotiate_wsize = smb3_negotiate_wsize,
5571 .negotiate_rsize = smb3_negotiate_rsize,
5572 .sess_setup = SMB2_sess_setup,
5573 .logoff = SMB2_logoff,
5574 .tree_connect = SMB2_tcon,
5575 .tree_disconnect = SMB2_tdis,
5576 .qfs_tcon = smb3_qfs_tcon,
5577 .query_server_interfaces = SMB3_request_interfaces,
5578 .is_path_accessible = smb2_is_path_accessible,
5579 .can_echo = smb2_can_echo,
5580 .echo = SMB2_echo,
5581 .query_path_info = smb2_query_path_info,
5582 /* WSL tags introduced long after smb2.1, enable for SMB3, 3.11 only */
5583 .query_reparse_point = smb2_query_reparse_point,
5584 .get_srv_inum = smb2_get_srv_inum,
5585 .query_file_info = smb2_query_file_info,
5586 .set_path_size = smb2_set_path_size,
5587 .set_file_size = smb2_set_file_size,
5588 .set_file_info = smb2_set_file_info,
5589 .set_compression = smb2_set_compression,
5590 .mkdir = smb2_mkdir,
5591 .mkdir_setinfo = smb2_mkdir_setinfo,
5592 .rmdir = smb2_rmdir,
5593 .unlink = smb2_unlink,
5594 .rename = smb2_rename_path,
5595 .create_hardlink = smb2_create_hardlink,
5596 .get_reparse_point_buffer = smb2_get_reparse_point_buffer,
5597 .query_mf_symlink = smb3_query_mf_symlink,
5598 .create_mf_symlink = smb3_create_mf_symlink,
5599 .create_reparse_inode = smb2_create_reparse_inode,
5600 .open = smb2_open_file,
5601 .set_fid = smb2_set_fid,
5602 .close = smb2_close_file,
5603 .close_getattr = smb2_close_getattr,
5604 .flush = smb2_flush_file,
5605 .async_readv = smb2_async_readv,
5606 .async_writev = smb2_async_writev,
5607 .sync_read = smb2_sync_read,
5608 .sync_write = smb2_sync_write,
5609 .query_dir_first = smb2_query_dir_first,
5610 .query_dir_next = smb2_query_dir_next,
5611 .close_dir = smb2_close_dir,
5612 .calc_smb_size = smb2_calc_size,
5613 .is_status_pending = smb2_is_status_pending,
5614 .is_session_expired = smb2_is_session_expired,
5615 .oplock_response = smb2_oplock_response,
5616 .queryfs = smb2_queryfs,
5617 .mand_lock = smb2_mand_lock,
5618 .mand_unlock_range = smb2_unlock_range,
5619 .push_mand_locks = smb2_push_mandatory_locks,
5620 .get_lease_key = smb2_get_lease_key,
5621 .set_lease_key = smb2_set_lease_key,
5622 .new_lease_key = smb2_new_lease_key,
5623 .generate_signingkey = generate_smb30signingkey,
5624 .set_integrity = smb3_set_integrity,
5625 .is_read_op = smb21_is_read_op,
5626 .set_oplock_level = smb3_set_oplock_level,
5627 .create_lease_buf = smb3_create_lease_buf,
5628 .parse_lease_buf = smb3_parse_lease_buf,
5629 .copychunk_range = smb2_copychunk_range,
5630 .duplicate_extents = smb2_duplicate_extents,
5631 .validate_negotiate = smb3_validate_negotiate,
5632 .wp_retry_size = smb2_wp_retry_size,
5633 .dir_needs_close = smb2_dir_needs_close,
5634 .fallocate = smb3_fallocate,
5635 .enum_snapshots = smb3_enum_snapshots,
5636 .notify = smb3_notify,
5637 .init_transform_rq = smb3_init_transform_rq,
5638 .is_transform_hdr = smb3_is_transform_hdr,
5639 .receive_transform = smb3_receive_transform,
5640 .get_dfs_refer = smb2_get_dfs_refer,
5641 .select_sectype = smb2_select_sectype,
5642 #ifdef CONFIG_CIFS_XATTR
5643 .query_all_EAs = smb2_query_eas,
5644 .set_EA = smb2_set_ea,
5645 #endif /* CIFS_XATTR */
5646 .get_acl = get_smb2_acl,
5647 .get_acl_by_fid = get_smb2_acl_by_fid,
5648 .set_acl = set_smb2_acl,
5649 .next_header = smb2_next_header,
5650 .ioctl_query_info = smb2_ioctl_query_info,
5651 .make_node = smb2_make_node,
5652 .fiemap = smb3_fiemap,
5653 .llseek = smb3_llseek,
5654 .is_status_io_timeout = smb2_is_status_io_timeout,
5655 .is_network_name_deleted = smb2_is_network_name_deleted,
5656 .rename_pending_delete = smb2_rename_pending_delete,
5657 };
5658
5659 struct smb_version_operations smb311_operations = {
5660 .compare_fids = smb2_compare_fids,
5661 .setup_request = smb2_setup_request,
5662 .setup_async_request = smb2_setup_async_request,
5663 .check_receive = smb2_check_receive,
5664 .add_credits = smb2_add_credits,
5665 .set_credits = smb2_set_credits,
5666 .get_credits_field = smb2_get_credits_field,
5667 .get_credits = smb2_get_credits,
5668 .wait_mtu_credits = smb2_wait_mtu_credits,
5669 .adjust_credits = smb2_adjust_credits,
5670 .get_next_mid = smb2_get_next_mid,
5671 .revert_current_mid = smb2_revert_current_mid,
5672 .read_data_offset = smb2_read_data_offset,
5673 .read_data_length = smb2_read_data_length,
5674 .map_error = map_smb2_to_linux_error,
5675 .find_mid = smb2_find_mid,
5676 .check_message = smb2_check_message,
5677 .dump_detail = smb2_dump_detail,
5678 .clear_stats = smb2_clear_stats,
5679 .print_stats = smb2_print_stats,
5680 .dump_share_caps = smb2_dump_share_caps,
5681 .is_oplock_break = smb2_is_valid_oplock_break,
5682 .handle_cancelled_mid = smb2_handle_cancelled_mid,
5683 .downgrade_oplock = smb3_downgrade_oplock,
5684 .need_neg = smb2_need_neg,
5685 .negotiate = smb2_negotiate,
5686 .negotiate_wsize = smb3_negotiate_wsize,
5687 .negotiate_rsize = smb3_negotiate_rsize,
5688 .sess_setup = SMB2_sess_setup,
5689 .logoff = SMB2_logoff,
5690 .tree_connect = SMB2_tcon,
5691 .tree_disconnect = SMB2_tdis,
5692 .qfs_tcon = smb3_qfs_tcon,
5693 .query_server_interfaces = SMB3_request_interfaces,
5694 .is_path_accessible = smb2_is_path_accessible,
5695 .can_echo = smb2_can_echo,
5696 .echo = SMB2_echo,
5697 .query_path_info = smb2_query_path_info,
5698 .query_reparse_point = smb2_query_reparse_point,
5699 .get_srv_inum = smb2_get_srv_inum,
5700 .query_file_info = smb2_query_file_info,
5701 .set_path_size = smb2_set_path_size,
5702 .set_file_size = smb2_set_file_size,
5703 .set_file_info = smb2_set_file_info,
5704 .set_compression = smb2_set_compression,
5705 .mkdir = smb2_mkdir,
5706 .mkdir_setinfo = smb2_mkdir_setinfo,
5707 .posix_mkdir = smb311_posix_mkdir,
5708 .rmdir = smb2_rmdir,
5709 .unlink = smb2_unlink,
5710 .rename = smb2_rename_path,
5711 .create_hardlink = smb2_create_hardlink,
5712 .get_reparse_point_buffer = smb2_get_reparse_point_buffer,
5713 .query_mf_symlink = smb3_query_mf_symlink,
5714 .create_mf_symlink = smb3_create_mf_symlink,
5715 .create_reparse_inode = smb2_create_reparse_inode,
5716 .open = smb2_open_file,
5717 .set_fid = smb2_set_fid,
5718 .close = smb2_close_file,
5719 .close_getattr = smb2_close_getattr,
5720 .flush = smb2_flush_file,
5721 .async_readv = smb2_async_readv,
5722 .async_writev = smb2_async_writev,
5723 .sync_read = smb2_sync_read,
5724 .sync_write = smb2_sync_write,
5725 .query_dir_first = smb2_query_dir_first,
5726 .query_dir_next = smb2_query_dir_next,
5727 .close_dir = smb2_close_dir,
5728 .calc_smb_size = smb2_calc_size,
5729 .is_status_pending = smb2_is_status_pending,
5730 .is_session_expired = smb2_is_session_expired,
5731 .oplock_response = smb2_oplock_response,
5732 .queryfs = smb311_queryfs,
5733 .mand_lock = smb2_mand_lock,
5734 .mand_unlock_range = smb2_unlock_range,
5735 .push_mand_locks = smb2_push_mandatory_locks,
5736 .get_lease_key = smb2_get_lease_key,
5737 .set_lease_key = smb2_set_lease_key,
5738 .new_lease_key = smb2_new_lease_key,
5739 .generate_signingkey = generate_smb311signingkey,
5740 .set_integrity = smb3_set_integrity,
5741 .is_read_op = smb21_is_read_op,
5742 .set_oplock_level = smb3_set_oplock_level,
5743 .create_lease_buf = smb3_create_lease_buf,
5744 .parse_lease_buf = smb3_parse_lease_buf,
5745 .copychunk_range = smb2_copychunk_range,
5746 .duplicate_extents = smb2_duplicate_extents,
5747 /* .validate_negotiate = smb3_validate_negotiate, */ /* not used in 3.11 */
5748 .wp_retry_size = smb2_wp_retry_size,
5749 .dir_needs_close = smb2_dir_needs_close,
5750 .fallocate = smb3_fallocate,
5751 .enum_snapshots = smb3_enum_snapshots,
5752 .notify = smb3_notify,
5753 .init_transform_rq = smb3_init_transform_rq,
5754 .is_transform_hdr = smb3_is_transform_hdr,
5755 .receive_transform = smb3_receive_transform,
5756 .get_dfs_refer = smb2_get_dfs_refer,
5757 .select_sectype = smb2_select_sectype,
5758 #ifdef CONFIG_CIFS_XATTR
5759 .query_all_EAs = smb2_query_eas,
5760 .set_EA = smb2_set_ea,
5761 #endif /* CIFS_XATTR */
5762 .get_acl = get_smb2_acl,
5763 .get_acl_by_fid = get_smb2_acl_by_fid,
5764 .set_acl = set_smb2_acl,
5765 .next_header = smb2_next_header,
5766 .ioctl_query_info = smb2_ioctl_query_info,
5767 .make_node = smb2_make_node,
5768 .fiemap = smb3_fiemap,
5769 .llseek = smb3_llseek,
5770 .is_status_io_timeout = smb2_is_status_io_timeout,
5771 .is_network_name_deleted = smb2_is_network_name_deleted,
5772 .rename_pending_delete = smb2_rename_pending_delete,
5773 };
5774
5775 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
5776 struct smb_version_values smb20_values = {
5777 .version_string = SMB20_VERSION_STRING,
5778 .protocol_id = SMB20_PROT_ID,
5779 .req_capabilities = 0, /* MBZ */
5780 .large_lock_type = 0,
5781 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE,
5782 .shared_lock_type = SMB2_LOCKFLAG_SHARED,
5783 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
5784 .header_size = sizeof(struct smb2_hdr),
5785 .max_header_size = MAX_SMB2_HDR_SIZE,
5786 .read_rsp_size = sizeof(struct smb2_read_rsp),
5787 .lock_cmd = SMB2_LOCK,
5788 .cap_unix = 0,
5789 .cap_nt_find = SMB2_NT_FIND,
5790 .cap_large_files = SMB2_LARGE_FILES,
5791 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5792 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5793 .create_lease_size = sizeof(struct create_lease),
5794 };
5795 #endif /* ALLOW_INSECURE_LEGACY */
5796
5797 struct smb_version_values smb21_values = {
5798 .version_string = SMB21_VERSION_STRING,
5799 .protocol_id = SMB21_PROT_ID,
5800 .req_capabilities = 0, /* MBZ on negotiate req until SMB3 dialect */
5801 .large_lock_type = 0,
5802 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE,
5803 .shared_lock_type = SMB2_LOCKFLAG_SHARED,
5804 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
5805 .header_size = sizeof(struct smb2_hdr),
5806 .max_header_size = MAX_SMB2_HDR_SIZE,
5807 .read_rsp_size = sizeof(struct smb2_read_rsp),
5808 .lock_cmd = SMB2_LOCK,
5809 .cap_unix = 0,
5810 .cap_nt_find = SMB2_NT_FIND,
5811 .cap_large_files = SMB2_LARGE_FILES,
5812 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5813 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5814 .create_lease_size = sizeof(struct create_lease),
5815 };
5816
5817 struct smb_version_values smb3any_values = {
5818 .version_string = SMB3ANY_VERSION_STRING,
5819 .protocol_id = SMB302_PROT_ID, /* doesn't matter, send protocol array */
5820 .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,
5821 .large_lock_type = 0,
5822 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE,
5823 .shared_lock_type = SMB2_LOCKFLAG_SHARED,
5824 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
5825 .header_size = sizeof(struct smb2_hdr),
5826 .max_header_size = MAX_SMB2_HDR_SIZE,
5827 .read_rsp_size = sizeof(struct smb2_read_rsp),
5828 .lock_cmd = SMB2_LOCK,
5829 .cap_unix = 0,
5830 .cap_nt_find = SMB2_NT_FIND,
5831 .cap_large_files = SMB2_LARGE_FILES,
5832 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5833 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5834 .create_lease_size = sizeof(struct create_lease_v2),
5835 };
5836
5837 struct smb_version_values smbdefault_values = {
5838 .version_string = SMBDEFAULT_VERSION_STRING,
5839 .protocol_id = SMB302_PROT_ID, /* doesn't matter, send protocol array */
5840 .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,
5841 .large_lock_type = 0,
5842 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE,
5843 .shared_lock_type = SMB2_LOCKFLAG_SHARED,
5844 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
5845 .header_size = sizeof(struct smb2_hdr),
5846 .max_header_size = MAX_SMB2_HDR_SIZE,
5847 .read_rsp_size = sizeof(struct smb2_read_rsp),
5848 .lock_cmd = SMB2_LOCK,
5849 .cap_unix = 0,
5850 .cap_nt_find = SMB2_NT_FIND,
5851 .cap_large_files = SMB2_LARGE_FILES,
5852 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5853 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5854 .create_lease_size = sizeof(struct create_lease_v2),
5855 };
5856
5857 struct smb_version_values smb30_values = {
5858 .version_string = SMB30_VERSION_STRING,
5859 .protocol_id = SMB30_PROT_ID,
5860 .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,
5861 .large_lock_type = 0,
5862 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE,
5863 .shared_lock_type = SMB2_LOCKFLAG_SHARED,
5864 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
5865 .header_size = sizeof(struct smb2_hdr),
5866 .max_header_size = MAX_SMB2_HDR_SIZE,
5867 .read_rsp_size = sizeof(struct smb2_read_rsp),
5868 .lock_cmd = SMB2_LOCK,
5869 .cap_unix = 0,
5870 .cap_nt_find = SMB2_NT_FIND,
5871 .cap_large_files = SMB2_LARGE_FILES,
5872 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5873 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5874 .create_lease_size = sizeof(struct create_lease_v2),
5875 };
5876
5877 struct smb_version_values smb302_values = {
5878 .version_string = SMB302_VERSION_STRING,
5879 .protocol_id = SMB302_PROT_ID,
5880 .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,
5881 .large_lock_type = 0,
5882 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE,
5883 .shared_lock_type = SMB2_LOCKFLAG_SHARED,
5884 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
5885 .header_size = sizeof(struct smb2_hdr),
5886 .max_header_size = MAX_SMB2_HDR_SIZE,
5887 .read_rsp_size = sizeof(struct smb2_read_rsp),
5888 .lock_cmd = SMB2_LOCK,
5889 .cap_unix = 0,
5890 .cap_nt_find = SMB2_NT_FIND,
5891 .cap_large_files = SMB2_LARGE_FILES,
5892 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5893 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5894 .create_lease_size = sizeof(struct create_lease_v2),
5895 };
5896
5897 struct smb_version_values smb311_values = {
5898 .version_string = SMB311_VERSION_STRING,
5899 .protocol_id = SMB311_PROT_ID,
5900 .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,
5901 .large_lock_type = 0,
5902 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE,
5903 .shared_lock_type = SMB2_LOCKFLAG_SHARED,
5904 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
5905 .header_size = sizeof(struct smb2_hdr),
5906 .max_header_size = MAX_SMB2_HDR_SIZE,
5907 .read_rsp_size = sizeof(struct smb2_read_rsp),
5908 .lock_cmd = SMB2_LOCK,
5909 .cap_unix = 0,
5910 .cap_nt_find = SMB2_NT_FIND,
5911 .cap_large_files = SMB2_LARGE_FILES,
5912 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5913 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5914 .create_lease_size = sizeof(struct create_lease_v2),
5915 };
5916