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