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