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