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