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