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