blob: 9fd56b0acd7e2fb73e92017ba41ea6a992c50ee9 [file] [log] [blame]
Christoph Probsta205d502019-05-08 21:36:25 +02001// SPDX-License-Identifier: GPL-2.0
Steve French1080ef72011-02-24 18:07:19 +00002/*
3 * SMB2 version specific operations
4 *
5 * Copyright (c) 2012, Jeff Layton <jlayton@redhat.com>
Steve French1080ef72011-02-24 18:07:19 +00006 */
7
Pavel Shilovsky3a3bab52012-09-18 16:20:28 -07008#include <linux/pagemap.h>
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07009#include <linux/vfs.h>
Steve Frenchf29ebb42014-07-19 21:44:58 -050010#include <linux/falloc.h>
Pavel Shilovsky026e93d2016-11-03 16:47:37 -070011#include <linux/scatterlist.h>
Tobias Regnery4fa8e502017-03-30 12:34:14 +020012#include <linux/uuid.h>
Pavel Shilovsky026e93d2016-11-03 16:47:37 -070013#include <crypto/aead.h>
Steve French1080ef72011-02-24 18:07:19 +000014#include "cifsglob.h"
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +040015#include "smb2pdu.h"
16#include "smb2proto.h"
Pavel Shilovsky28ea5292012-05-23 16:18:00 +040017#include "cifsproto.h"
18#include "cifs_debug.h"
Pavel Shilovskyb42bf882013-08-14 19:25:21 +040019#include "cifs_unicode.h"
Pavel Shilovsky2e44b282012-09-18 16:20:33 -070020#include "smb2status.h"
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -070021#include "smb2glob.h"
Steve French834170c2016-09-30 21:14:26 -050022#include "cifs_ioctl.h"
Long Li09902f82017-11-22 17:38:39 -070023#include "smbdirect.h"
Pavel Shilovsky28ea5292012-05-23 16:18:00 +040024
Pavel Shilovskyef68e832019-01-18 17:25:36 -080025/* Change credits for different ops and return the total number of credits */
Pavel Shilovsky28ea5292012-05-23 16:18:00 +040026static int
27change_conf(struct TCP_Server_Info *server)
28{
29 server->credits += server->echo_credits + server->oplock_credits;
30 server->oplock_credits = server->echo_credits = 0;
31 switch (server->credits) {
32 case 0:
Pavel Shilovskyef68e832019-01-18 17:25:36 -080033 return 0;
Pavel Shilovsky28ea5292012-05-23 16:18:00 +040034 case 1:
35 server->echoes = false;
36 server->oplocks = false;
Pavel Shilovsky28ea5292012-05-23 16:18:00 +040037 break;
38 case 2:
39 server->echoes = true;
40 server->oplocks = false;
41 server->echo_credits = 1;
Pavel Shilovsky28ea5292012-05-23 16:18:00 +040042 break;
43 default:
44 server->echoes = true;
Steve Frenche0ddde92015-09-22 09:29:38 -050045 if (enable_oplocks) {
46 server->oplocks = true;
47 server->oplock_credits = 1;
48 } else
49 server->oplocks = false;
50
Pavel Shilovsky28ea5292012-05-23 16:18:00 +040051 server->echo_credits = 1;
Pavel Shilovsky28ea5292012-05-23 16:18:00 +040052 }
53 server->credits -= server->echo_credits + server->oplock_credits;
Pavel Shilovskyef68e832019-01-18 17:25:36 -080054 return server->credits + server->echo_credits + server->oplock_credits;
Pavel Shilovsky28ea5292012-05-23 16:18:00 +040055}
56
57static void
Pavel Shilovsky335b7b62019-01-16 11:12:41 -080058smb2_add_credits(struct TCP_Server_Info *server,
59 const struct cifs_credits *credits, const int optype)
Pavel Shilovsky28ea5292012-05-23 16:18:00 +040060{
Pavel Shilovskyef68e832019-01-18 17:25:36 -080061 int *val, rc = -1;
Pavel Shilovsky335b7b62019-01-16 11:12:41 -080062 unsigned int add = credits->value;
63 unsigned int instance = credits->instance;
64 bool reconnect_detected = false;
Pavel Shilovskyef68e832019-01-18 17:25:36 -080065
Pavel Shilovsky28ea5292012-05-23 16:18:00 +040066 spin_lock(&server->req_lock);
67 val = server->ops->get_credits_field(server, optype);
Steve Frenchb340a4d2018-09-01 01:10:17 -050068
69 /* eg found case where write overlapping reconnect messed up credits */
70 if (((optype & CIFS_OP_MASK) == CIFS_NEG_OP) && (*val != 0))
71 trace_smb3_reconnect_with_invalid_credits(server->CurrentMid,
72 server->hostname, *val);
Pavel Shilovsky335b7b62019-01-16 11:12:41 -080073 if ((instance == 0) || (instance == server->reconnect_instance))
74 *val += add;
75 else
76 reconnect_detected = true;
Steve Frenchb340a4d2018-09-01 01:10:17 -050077
Steve French141891f2016-09-23 00:44:16 -050078 if (*val > 65000) {
79 *val = 65000; /* Don't get near 64K credits, avoid srv bugs */
80 printk_once(KERN_WARNING "server overflowed SMB3 credits\n");
81 }
Pavel Shilovsky28ea5292012-05-23 16:18:00 +040082 server->in_flight--;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +040083 if (server->in_flight == 0 && (optype & CIFS_OP_MASK) != CIFS_NEG_OP)
Pavel Shilovsky28ea5292012-05-23 16:18:00 +040084 rc = change_conf(server);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -070085 /*
86 * Sometimes server returns 0 credits on oplock break ack - we need to
87 * rebalance credits in this case.
88 */
89 else if (server->in_flight > 0 && server->oplock_credits == 0 &&
90 server->oplocks) {
91 if (server->credits > 1) {
92 server->credits--;
93 server->oplock_credits++;
94 }
95 }
Pavel Shilovsky28ea5292012-05-23 16:18:00 +040096 spin_unlock(&server->req_lock);
97 wake_up(&server->request_q);
Pavel Shilovskyef68e832019-01-18 17:25:36 -080098
Pavel Shilovsky335b7b62019-01-16 11:12:41 -080099 if (reconnect_detected)
100 cifs_dbg(FYI, "trying to put %d credits from the old server instance %d\n",
101 add, instance);
102
Pavel Shilovsky82e04572019-01-25 10:56:41 -0800103 if (server->tcpStatus == CifsNeedReconnect
104 || server->tcpStatus == CifsExiting)
Pavel Shilovskyef68e832019-01-18 17:25:36 -0800105 return;
106
107 switch (rc) {
108 case -1:
109 /* change_conf hasn't been executed */
110 break;
111 case 0:
112 cifs_dbg(VFS, "Possible client or server bug - zero credits\n");
113 break;
114 case 1:
115 cifs_dbg(VFS, "disabling echoes and oplocks\n");
116 break;
117 case 2:
118 cifs_dbg(FYI, "disabling oplocks\n");
119 break;
120 default:
121 cifs_dbg(FYI, "add %u credits total=%d\n", add, rc);
122 }
Pavel Shilovsky28ea5292012-05-23 16:18:00 +0400123}
124
125static void
126smb2_set_credits(struct TCP_Server_Info *server, const int val)
127{
128 spin_lock(&server->req_lock);
129 server->credits = val;
Steve French9e1a37d2018-09-19 02:38:17 -0500130 if (val == 1)
131 server->reconnect_instance++;
Pavel Shilovsky28ea5292012-05-23 16:18:00 +0400132 spin_unlock(&server->req_lock);
Steve French6e4d3bb2018-09-22 11:25:04 -0500133 /* don't log while holding the lock */
134 if (val == 1)
135 cifs_dbg(FYI, "set credits to 1 due to smb2 reconnect\n");
Pavel Shilovsky28ea5292012-05-23 16:18:00 +0400136}
137
138static int *
139smb2_get_credits_field(struct TCP_Server_Info *server, const int optype)
140{
141 switch (optype) {
142 case CIFS_ECHO_OP:
143 return &server->echo_credits;
144 case CIFS_OBREAK_OP:
145 return &server->oplock_credits;
146 default:
147 return &server->credits;
148 }
149}
150
151static unsigned int
152smb2_get_credits(struct mid_q_entry *mid)
153{
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +1000154 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)mid->resp_buf;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700155
Pavel Shilovsky3d3003f2019-01-22 16:50:21 -0800156 if (mid->mid_state == MID_RESPONSE_RECEIVED
157 || mid->mid_state == MID_RESPONSE_MALFORMED)
158 return le16_to_cpu(shdr->CreditRequest);
159
160 return 0;
Pavel Shilovsky28ea5292012-05-23 16:18:00 +0400161}
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400162
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +0400163static int
164smb2_wait_mtu_credits(struct TCP_Server_Info *server, unsigned int size,
Pavel Shilovsky335b7b62019-01-16 11:12:41 -0800165 unsigned int *num, struct cifs_credits *credits)
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +0400166{
167 int rc = 0;
168 unsigned int scredits;
169
170 spin_lock(&server->req_lock);
171 while (1) {
172 if (server->credits <= 0) {
173 spin_unlock(&server->req_lock);
174 cifs_num_waiters_inc(server);
175 rc = wait_event_killable(server->request_q,
Ronnie Sahlbergb227d212019-03-08 12:58:20 +1000176 has_credits(server, &server->credits, 1));
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +0400177 cifs_num_waiters_dec(server);
178 if (rc)
179 return rc;
180 spin_lock(&server->req_lock);
181 } else {
182 if (server->tcpStatus == CifsExiting) {
183 spin_unlock(&server->req_lock);
184 return -ENOENT;
185 }
186
187 scredits = server->credits;
188 /* can deadlock with reopen */
Pavel Shilovskyacc58d02019-01-17 08:21:24 -0800189 if (scredits <= 8) {
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +0400190 *num = SMB2_MAX_BUFFER_SIZE;
Pavel Shilovsky335b7b62019-01-16 11:12:41 -0800191 credits->value = 0;
192 credits->instance = 0;
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +0400193 break;
194 }
195
Pavel Shilovskyacc58d02019-01-17 08:21:24 -0800196 /* leave some credits for reopen and other ops */
197 scredits -= 8;
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +0400198 *num = min_t(unsigned int, size,
199 scredits * SMB2_MAX_BUFFER_SIZE);
200
Pavel Shilovsky335b7b62019-01-16 11:12:41 -0800201 credits->value =
202 DIV_ROUND_UP(*num, SMB2_MAX_BUFFER_SIZE);
203 credits->instance = server->reconnect_instance;
204 server->credits -= credits->value;
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +0400205 server->in_flight++;
206 break;
207 }
208 }
209 spin_unlock(&server->req_lock);
210 return rc;
211}
212
Pavel Shilovsky9a1c67e2019-01-23 18:15:52 -0800213static int
214smb2_adjust_credits(struct TCP_Server_Info *server,
215 struct cifs_credits *credits,
216 const unsigned int payload_size)
217{
218 int new_val = DIV_ROUND_UP(payload_size, SMB2_MAX_BUFFER_SIZE);
219
220 if (!credits->value || credits->value == new_val)
221 return 0;
222
223 if (credits->value < new_val) {
224 WARN_ONCE(1, "request has less credits (%d) than required (%d)",
225 credits->value, new_val);
226 return -ENOTSUPP;
227 }
228
229 spin_lock(&server->req_lock);
230
231 if (server->reconnect_instance != credits->instance) {
232 spin_unlock(&server->req_lock);
233 cifs_dbg(VFS, "trying to return %d credits to old session\n",
234 credits->value - new_val);
235 return -EAGAIN;
236 }
237
238 server->credits += credits->value - new_val;
239 spin_unlock(&server->req_lock);
240 wake_up(&server->request_q);
241 credits->value = new_val;
242 return 0;
243}
244
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400245static __u64
246smb2_get_next_mid(struct TCP_Server_Info *server)
247{
248 __u64 mid;
249 /* for SMB2 we need the current value */
250 spin_lock(&GlobalMid_Lock);
251 mid = server->CurrentMid++;
252 spin_unlock(&GlobalMid_Lock);
253 return mid;
254}
Steve French1080ef72011-02-24 18:07:19 +0000255
Pavel Shilovskyc781af72019-03-04 14:02:50 -0800256static void
257smb2_revert_current_mid(struct TCP_Server_Info *server, const unsigned int val)
258{
259 spin_lock(&GlobalMid_Lock);
260 if (server->CurrentMid >= val)
261 server->CurrentMid -= val;
262 spin_unlock(&GlobalMid_Lock);
263}
264
Pavel Shilovsky093b2bd2011-06-08 15:51:07 +0400265static struct mid_q_entry *
266smb2_find_mid(struct TCP_Server_Info *server, char *buf)
267{
268 struct mid_q_entry *mid;
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +1000269 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700270 __u64 wire_mid = le64_to_cpu(shdr->MessageId);
Pavel Shilovsky093b2bd2011-06-08 15:51:07 +0400271
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700272 if (shdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM) {
Christoph Probsta205d502019-05-08 21:36:25 +0200273 cifs_dbg(VFS, "Encrypted frame parsing not supported yet\n");
Steve French373512e2015-12-18 13:05:30 -0600274 return NULL;
275 }
276
Pavel Shilovsky093b2bd2011-06-08 15:51:07 +0400277 spin_lock(&GlobalMid_Lock);
278 list_for_each_entry(mid, &server->pending_mid_q, qhead) {
Sachin Prabhu9235d092014-12-09 17:37:00 +0000279 if ((mid->mid == wire_mid) &&
Pavel Shilovsky093b2bd2011-06-08 15:51:07 +0400280 (mid->mid_state == MID_REQUEST_SUBMITTED) &&
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700281 (mid->command == shdr->Command)) {
Lars Persson696e4202018-06-25 14:05:25 +0200282 kref_get(&mid->refcount);
Pavel Shilovsky093b2bd2011-06-08 15:51:07 +0400283 spin_unlock(&GlobalMid_Lock);
284 return mid;
285 }
286 }
287 spin_unlock(&GlobalMid_Lock);
288 return NULL;
289}
290
291static void
Ronnie Sahlberg14547f72018-04-22 14:45:53 -0600292smb2_dump_detail(void *buf, struct TCP_Server_Info *server)
Pavel Shilovsky093b2bd2011-06-08 15:51:07 +0400293{
294#ifdef CONFIG_CIFS_DEBUG2
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +1000295 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
Pavel Shilovsky093b2bd2011-06-08 15:51:07 +0400296
Joe Perchesf96637b2013-05-04 22:12:25 -0500297 cifs_dbg(VFS, "Cmd: %d Err: 0x%x Flags: 0x%x Mid: %llu Pid: %d\n",
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700298 shdr->Command, shdr->Status, shdr->Flags, shdr->MessageId,
299 shdr->ProcessId);
Ronnie Sahlberg14547f72018-04-22 14:45:53 -0600300 cifs_dbg(VFS, "smb buf %p len %u\n", buf,
Steve French71992e622018-05-06 15:58:51 -0500301 server->ops->calc_smb_size(buf, server));
Pavel Shilovsky093b2bd2011-06-08 15:51:07 +0400302#endif
303}
304
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400305static bool
306smb2_need_neg(struct TCP_Server_Info *server)
307{
308 return server->max_read == 0;
309}
310
311static int
312smb2_negotiate(const unsigned int xid, struct cifs_ses *ses)
313{
314 int rc;
Christoph Probsta205d502019-05-08 21:36:25 +0200315
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400316 ses->server->CurrentMid = 0;
317 rc = SMB2_negotiate(xid, ses);
318 /* BB we probably don't need to retry with modern servers */
319 if (rc == -EAGAIN)
320 rc = -EHOSTDOWN;
321 return rc;
322}
323
Pavel Shilovsky3a3bab52012-09-18 16:20:28 -0700324static unsigned int
325smb2_negotiate_wsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
326{
327 struct TCP_Server_Info *server = tcon->ses->server;
328 unsigned int wsize;
329
330 /* start with specified wsize, or default */
331 wsize = volume_info->wsize ? volume_info->wsize : CIFS_DEFAULT_IOSIZE;
332 wsize = min_t(unsigned int, wsize, server->max_write);
Long Li09902f82017-11-22 17:38:39 -0700333#ifdef CONFIG_CIFS_SMB_DIRECT
Long Libb4c0412018-04-17 12:17:08 -0700334 if (server->rdma) {
335 if (server->sign)
336 wsize = min_t(unsigned int,
337 wsize, server->smbd_conn->max_fragmented_send_size);
338 else
339 wsize = min_t(unsigned int,
Long Li09902f82017-11-22 17:38:39 -0700340 wsize, server->smbd_conn->max_readwrite_size);
Long Libb4c0412018-04-17 12:17:08 -0700341 }
Long Li09902f82017-11-22 17:38:39 -0700342#endif
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +0400343 if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
344 wsize = min_t(unsigned int, wsize, SMB2_MAX_BUFFER_SIZE);
Pavel Shilovsky3a3bab52012-09-18 16:20:28 -0700345
Pavel Shilovsky3a3bab52012-09-18 16:20:28 -0700346 return wsize;
347}
348
349static unsigned int
Steve French3d621232018-09-25 15:33:47 -0500350smb3_negotiate_wsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
351{
352 struct TCP_Server_Info *server = tcon->ses->server;
353 unsigned int wsize;
354
355 /* start with specified wsize, or default */
356 wsize = volume_info->wsize ? volume_info->wsize : SMB3_DEFAULT_IOSIZE;
357 wsize = min_t(unsigned int, wsize, server->max_write);
358#ifdef CONFIG_CIFS_SMB_DIRECT
359 if (server->rdma) {
360 if (server->sign)
361 wsize = min_t(unsigned int,
362 wsize, server->smbd_conn->max_fragmented_send_size);
363 else
364 wsize = min_t(unsigned int,
365 wsize, server->smbd_conn->max_readwrite_size);
366 }
367#endif
368 if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
369 wsize = min_t(unsigned int, wsize, SMB2_MAX_BUFFER_SIZE);
370
371 return wsize;
372}
373
374static unsigned int
Pavel Shilovsky3a3bab52012-09-18 16:20:28 -0700375smb2_negotiate_rsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
376{
377 struct TCP_Server_Info *server = tcon->ses->server;
378 unsigned int rsize;
379
380 /* start with specified rsize, or default */
381 rsize = volume_info->rsize ? volume_info->rsize : CIFS_DEFAULT_IOSIZE;
382 rsize = min_t(unsigned int, rsize, server->max_read);
Long Li09902f82017-11-22 17:38:39 -0700383#ifdef CONFIG_CIFS_SMB_DIRECT
Long Libb4c0412018-04-17 12:17:08 -0700384 if (server->rdma) {
385 if (server->sign)
386 rsize = min_t(unsigned int,
387 rsize, server->smbd_conn->max_fragmented_recv_size);
388 else
389 rsize = min_t(unsigned int,
Long Li09902f82017-11-22 17:38:39 -0700390 rsize, server->smbd_conn->max_readwrite_size);
Long Libb4c0412018-04-17 12:17:08 -0700391 }
Long Li09902f82017-11-22 17:38:39 -0700392#endif
Pavel Shilovskybed9da02014-06-25 11:28:57 +0400393
394 if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
395 rsize = min_t(unsigned int, rsize, SMB2_MAX_BUFFER_SIZE);
Pavel Shilovsky3a3bab52012-09-18 16:20:28 -0700396
Pavel Shilovsky3a3bab52012-09-18 16:20:28 -0700397 return rsize;
398}
399
Steve French3d621232018-09-25 15:33:47 -0500400static unsigned int
401smb3_negotiate_rsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
402{
403 struct TCP_Server_Info *server = tcon->ses->server;
404 unsigned int rsize;
405
406 /* start with specified rsize, or default */
407 rsize = volume_info->rsize ? volume_info->rsize : SMB3_DEFAULT_IOSIZE;
408 rsize = min_t(unsigned int, rsize, server->max_read);
409#ifdef CONFIG_CIFS_SMB_DIRECT
410 if (server->rdma) {
411 if (server->sign)
412 rsize = min_t(unsigned int,
413 rsize, server->smbd_conn->max_fragmented_recv_size);
414 else
415 rsize = min_t(unsigned int,
416 rsize, server->smbd_conn->max_readwrite_size);
417 }
418#endif
419
420 if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
421 rsize = min_t(unsigned int, rsize, SMB2_MAX_BUFFER_SIZE);
422
423 return rsize;
424}
Aurelien Aptelfe856be2018-06-14 17:04:51 +0200425
426static int
427parse_server_interfaces(struct network_interface_info_ioctl_rsp *buf,
428 size_t buf_len,
429 struct cifs_server_iface **iface_list,
430 size_t *iface_count)
431{
432 struct network_interface_info_ioctl_rsp *p;
433 struct sockaddr_in *addr4;
434 struct sockaddr_in6 *addr6;
435 struct iface_info_ipv4 *p4;
436 struct iface_info_ipv6 *p6;
437 struct cifs_server_iface *info;
438 ssize_t bytes_left;
439 size_t next = 0;
440 int nb_iface = 0;
441 int rc = 0;
442
443 *iface_list = NULL;
444 *iface_count = 0;
445
446 /*
447 * Fist pass: count and sanity check
448 */
449
450 bytes_left = buf_len;
451 p = buf;
452 while (bytes_left >= sizeof(*p)) {
453 nb_iface++;
454 next = le32_to_cpu(p->Next);
455 if (!next) {
456 bytes_left -= sizeof(*p);
457 break;
458 }
459 p = (struct network_interface_info_ioctl_rsp *)((u8 *)p+next);
460 bytes_left -= next;
461 }
462
463 if (!nb_iface) {
464 cifs_dbg(VFS, "%s: malformed interface info\n", __func__);
465 rc = -EINVAL;
466 goto out;
467 }
468
469 if (bytes_left || p->Next)
470 cifs_dbg(VFS, "%s: incomplete interface info\n", __func__);
471
472
473 /*
474 * Second pass: extract info to internal structure
475 */
476
477 *iface_list = kcalloc(nb_iface, sizeof(**iface_list), GFP_KERNEL);
478 if (!*iface_list) {
479 rc = -ENOMEM;
480 goto out;
481 }
482
483 info = *iface_list;
484 bytes_left = buf_len;
485 p = buf;
486 while (bytes_left >= sizeof(*p)) {
487 info->speed = le64_to_cpu(p->LinkSpeed);
488 info->rdma_capable = le32_to_cpu(p->Capability & RDMA_CAPABLE);
489 info->rss_capable = le32_to_cpu(p->Capability & RSS_CAPABLE);
490
491 cifs_dbg(FYI, "%s: adding iface %zu\n", __func__, *iface_count);
492 cifs_dbg(FYI, "%s: speed %zu bps\n", __func__, info->speed);
493 cifs_dbg(FYI, "%s: capabilities 0x%08x\n", __func__,
494 le32_to_cpu(p->Capability));
495
496 switch (p->Family) {
497 /*
498 * The kernel and wire socket structures have the same
499 * layout and use network byte order but make the
500 * conversion explicit in case either one changes.
501 */
502 case INTERNETWORK:
503 addr4 = (struct sockaddr_in *)&info->sockaddr;
504 p4 = (struct iface_info_ipv4 *)p->Buffer;
505 addr4->sin_family = AF_INET;
506 memcpy(&addr4->sin_addr, &p4->IPv4Address, 4);
507
508 /* [MS-SMB2] 2.2.32.5.1.1 Clients MUST ignore these */
509 addr4->sin_port = cpu_to_be16(CIFS_PORT);
510
511 cifs_dbg(FYI, "%s: ipv4 %pI4\n", __func__,
512 &addr4->sin_addr);
513 break;
514 case INTERNETWORKV6:
515 addr6 = (struct sockaddr_in6 *)&info->sockaddr;
516 p6 = (struct iface_info_ipv6 *)p->Buffer;
517 addr6->sin6_family = AF_INET6;
518 memcpy(&addr6->sin6_addr, &p6->IPv6Address, 16);
519
520 /* [MS-SMB2] 2.2.32.5.1.2 Clients MUST ignore these */
521 addr6->sin6_flowinfo = 0;
522 addr6->sin6_scope_id = 0;
523 addr6->sin6_port = cpu_to_be16(CIFS_PORT);
524
525 cifs_dbg(FYI, "%s: ipv6 %pI6\n", __func__,
526 &addr6->sin6_addr);
527 break;
528 default:
529 cifs_dbg(VFS,
530 "%s: skipping unsupported socket family\n",
531 __func__);
532 goto next_iface;
533 }
534
535 (*iface_count)++;
536 info++;
537next_iface:
538 next = le32_to_cpu(p->Next);
539 if (!next)
540 break;
541 p = (struct network_interface_info_ioctl_rsp *)((u8 *)p+next);
542 bytes_left -= next;
543 }
544
545 if (!*iface_count) {
546 rc = -EINVAL;
547 goto out;
548 }
549
550out:
551 if (rc) {
552 kfree(*iface_list);
553 *iface_count = 0;
554 *iface_list = NULL;
555 }
556 return rc;
557}
558
559
Steve Frenchc481e9f2013-10-14 01:21:53 -0500560static int
561SMB3_request_interfaces(const unsigned int xid, struct cifs_tcon *tcon)
562{
563 int rc;
564 unsigned int ret_data_len = 0;
Aurelien Aptelfe856be2018-06-14 17:04:51 +0200565 struct network_interface_info_ioctl_rsp *out_buf = NULL;
566 struct cifs_server_iface *iface_list;
567 size_t iface_count;
568 struct cifs_ses *ses = tcon->ses;
Steve Frenchc481e9f2013-10-14 01:21:53 -0500569
570 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
571 FSCTL_QUERY_NETWORK_INTERFACE_INFO, true /* is_fsctl */,
572 NULL /* no data input */, 0 /* no data input */,
Steve French153322f2019-03-28 22:32:49 -0500573 CIFSMaxBufSize, (char **)&out_buf, &ret_data_len);
Steve Frenchc3ed4402018-06-28 22:53:39 -0500574 if (rc == -EOPNOTSUPP) {
575 cifs_dbg(FYI,
576 "server does not support query network interfaces\n");
577 goto out;
578 } else if (rc != 0) {
Steve French9ffc5412014-10-16 15:13:14 -0500579 cifs_dbg(VFS, "error %d on ioctl to get interface list\n", rc);
Aurelien Aptelfe856be2018-06-14 17:04:51 +0200580 goto out;
Steve French9ffc5412014-10-16 15:13:14 -0500581 }
Aurelien Aptelfe856be2018-06-14 17:04:51 +0200582
583 rc = parse_server_interfaces(out_buf, ret_data_len,
584 &iface_list, &iface_count);
585 if (rc)
586 goto out;
587
588 spin_lock(&ses->iface_lock);
589 kfree(ses->iface_list);
590 ses->iface_list = iface_list;
591 ses->iface_count = iface_count;
592 ses->iface_last_update = jiffies;
593 spin_unlock(&ses->iface_lock);
594
595out:
Steve French24df1482016-09-29 04:20:23 -0500596 kfree(out_buf);
Steve Frenchc481e9f2013-10-14 01:21:53 -0500597 return rc;
598}
Steve Frenchc481e9f2013-10-14 01:21:53 -0500599
Ronnie Sahlberg9da6ec72018-07-31 08:48:22 +1000600static void
601smb2_close_cached_fid(struct kref *ref)
Ronnie Sahlberga93864d2018-06-14 06:48:35 +1000602{
Ronnie Sahlberg9da6ec72018-07-31 08:48:22 +1000603 struct cached_fid *cfid = container_of(ref, struct cached_fid,
604 refcount);
605
Ronnie Sahlberga93864d2018-06-14 06:48:35 +1000606 if (cfid->is_valid) {
607 cifs_dbg(FYI, "clear cached root file handle\n");
608 SMB2_close(0, cfid->tcon, cfid->fid->persistent_fid,
609 cfid->fid->volatile_fid);
610 cfid->is_valid = false;
Ronnie Sahlbergb0f6df72019-03-12 13:58:31 +1000611 cfid->file_all_info_is_valid = false;
Ronnie Sahlberga93864d2018-06-14 06:48:35 +1000612 }
Ronnie Sahlberg9da6ec72018-07-31 08:48:22 +1000613}
614
615void close_shroot(struct cached_fid *cfid)
616{
617 mutex_lock(&cfid->fid_mutex);
618 kref_put(&cfid->refcount, smb2_close_cached_fid);
Ronnie Sahlberga93864d2018-06-14 06:48:35 +1000619 mutex_unlock(&cfid->fid_mutex);
620}
621
Ronnie Sahlberg9da6ec72018-07-31 08:48:22 +1000622void
623smb2_cached_lease_break(struct work_struct *work)
624{
625 struct cached_fid *cfid = container_of(work,
626 struct cached_fid, lease_break);
627
628 close_shroot(cfid);
629}
630
Steve French3d4ef9a2018-04-25 22:19:09 -0500631/*
632 * Open the directory at the root of a share
633 */
634int open_shroot(unsigned int xid, struct cifs_tcon *tcon, struct cifs_fid *pfid)
635{
Ronnie Sahlbergb0f6df72019-03-12 13:58:31 +1000636 struct cifs_ses *ses = tcon->ses;
637 struct TCP_Server_Info *server = ses->server;
638 struct cifs_open_parms oparms;
639 struct smb2_create_rsp *o_rsp = NULL;
640 struct smb2_query_info_rsp *qi_rsp = NULL;
641 int resp_buftype[2];
642 struct smb_rqst rqst[2];
643 struct kvec rsp_iov[2];
644 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
645 struct kvec qi_iov[1];
646 int rc, flags = 0;
647 __le16 utf16_path = 0; /* Null - since an open of top of share */
Ronnie Sahlberga93864d2018-06-14 06:48:35 +1000648 u8 oplock = SMB2_OPLOCK_LEVEL_II;
Steve French3d4ef9a2018-04-25 22:19:09 -0500649
Ronnie Sahlberga93864d2018-06-14 06:48:35 +1000650 mutex_lock(&tcon->crfid.fid_mutex);
651 if (tcon->crfid.is_valid) {
Steve French3d4ef9a2018-04-25 22:19:09 -0500652 cifs_dbg(FYI, "found a cached root file handle\n");
Ronnie Sahlberga93864d2018-06-14 06:48:35 +1000653 memcpy(pfid, tcon->crfid.fid, sizeof(struct cifs_fid));
Ronnie Sahlberg9da6ec72018-07-31 08:48:22 +1000654 kref_get(&tcon->crfid.refcount);
Ronnie Sahlberga93864d2018-06-14 06:48:35 +1000655 mutex_unlock(&tcon->crfid.fid_mutex);
Steve French3d4ef9a2018-04-25 22:19:09 -0500656 return 0;
657 }
658
Ronnie Sahlbergb0f6df72019-03-12 13:58:31 +1000659 if (smb3_encryption_required(tcon))
660 flags |= CIFS_TRANSFORM_REQ;
Steve French3d4ef9a2018-04-25 22:19:09 -0500661
Ronnie Sahlbergb0f6df72019-03-12 13:58:31 +1000662 memset(rqst, 0, sizeof(rqst));
663 resp_buftype[0] = resp_buftype[1] = CIFS_NO_BUFFER;
664 memset(rsp_iov, 0, sizeof(rsp_iov));
665
666 /* Open */
667 memset(&open_iov, 0, sizeof(open_iov));
668 rqst[0].rq_iov = open_iov;
669 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
670
671 oparms.tcon = tcon;
672 oparms.create_options = 0;
673 oparms.desired_access = FILE_READ_ATTRIBUTES;
674 oparms.disposition = FILE_OPEN;
675 oparms.fid = pfid;
676 oparms.reconnect = false;
677
678 rc = SMB2_open_init(tcon, &rqst[0], &oplock, &oparms, &utf16_path);
679 if (rc)
680 goto oshr_exit;
681 smb2_set_next_command(tcon, &rqst[0]);
682
683 memset(&qi_iov, 0, sizeof(qi_iov));
684 rqst[1].rq_iov = qi_iov;
685 rqst[1].rq_nvec = 1;
686
687 rc = SMB2_query_info_init(tcon, &rqst[1], COMPOUND_FID,
688 COMPOUND_FID, FILE_ALL_INFORMATION,
689 SMB2_O_INFO_FILE, 0,
690 sizeof(struct smb2_file_all_info) +
691 PATH_MAX * 2, 0, NULL);
692 if (rc)
693 goto oshr_exit;
694
695 smb2_set_related(&rqst[1]);
696
697 rc = compound_send_recv(xid, ses, flags, 2, rqst,
698 resp_buftype, rsp_iov);
699 if (rc)
700 goto oshr_exit;
701
702 o_rsp = (struct smb2_create_rsp *)rsp_iov[0].iov_base;
703 oparms.fid->persistent_fid = o_rsp->PersistentFileId;
704 oparms.fid->volatile_fid = o_rsp->VolatileFileId;
705#ifdef CONFIG_CIFS_DEBUG2
706 oparms.fid->mid = le64_to_cpu(o_rsp->sync_hdr.MessageId);
707#endif /* CIFS_DEBUG2 */
708
Ronnie Sahlbergb0f6df72019-03-12 13:58:31 +1000709 memcpy(tcon->crfid.fid, pfid, sizeof(struct cifs_fid));
710 tcon->crfid.tcon = tcon;
711 tcon->crfid.is_valid = true;
712 kref_init(&tcon->crfid.refcount);
Ronnie Sahlbergb0f6df72019-03-12 13:58:31 +1000713
Ronnie Sahlberg2f94a3122019-03-28 11:20:02 +1000714 if (o_rsp->OplockLevel == SMB2_OPLOCK_LEVEL_LEASE) {
715 kref_get(&tcon->crfid.refcount);
716 oplock = smb2_parse_lease_state(server, o_rsp,
717 &oparms.fid->epoch,
718 oparms.fid->lease_key);
719 } else
720 goto oshr_exit;
Ronnie Sahlbergb0f6df72019-03-12 13:58:31 +1000721
722 qi_rsp = (struct smb2_query_info_rsp *)rsp_iov[1].iov_base;
723 if (le32_to_cpu(qi_rsp->OutputBufferLength) < sizeof(struct smb2_file_all_info))
724 goto oshr_exit;
Ronnie Sahlberg4811e302019-04-01 09:53:44 +1000725 if (!smb2_validate_and_copy_iov(
Ronnie Sahlbergb0f6df72019-03-12 13:58:31 +1000726 le16_to_cpu(qi_rsp->OutputBufferOffset),
727 sizeof(struct smb2_file_all_info),
728 &rsp_iov[1], sizeof(struct smb2_file_all_info),
Ronnie Sahlberg4811e302019-04-01 09:53:44 +1000729 (char *)&tcon->crfid.file_all_info))
730 tcon->crfid.file_all_info_is_valid = 1;
Ronnie Sahlbergb0f6df72019-03-12 13:58:31 +1000731
732 oshr_exit:
Ronnie Sahlberga93864d2018-06-14 06:48:35 +1000733 mutex_unlock(&tcon->crfid.fid_mutex);
Ronnie Sahlbergb0f6df72019-03-12 13:58:31 +1000734 SMB2_open_free(&rqst[0]);
735 SMB2_query_info_free(&rqst[1]);
736 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
737 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
Steve French3d4ef9a2018-04-25 22:19:09 -0500738 return rc;
739}
740
Steve French34f62642013-10-09 02:07:00 -0500741static void
Steven Frenchaf6a12e2013-10-09 20:55:53 -0500742smb3_qfs_tcon(const unsigned int xid, struct cifs_tcon *tcon)
743{
744 int rc;
745 __le16 srch_path = 0; /* Null - open root of share */
746 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
747 struct cifs_open_parms oparms;
748 struct cifs_fid fid;
Steve French3d4ef9a2018-04-25 22:19:09 -0500749 bool no_cached_open = tcon->nohandlecache;
Steven Frenchaf6a12e2013-10-09 20:55:53 -0500750
751 oparms.tcon = tcon;
752 oparms.desired_access = FILE_READ_ATTRIBUTES;
753 oparms.disposition = FILE_OPEN;
754 oparms.create_options = 0;
755 oparms.fid = &fid;
756 oparms.reconnect = false;
757
Steve French3d4ef9a2018-04-25 22:19:09 -0500758 if (no_cached_open)
Ronnie Sahlberg9d874c32018-06-08 13:21:18 +1000759 rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL,
760 NULL);
Steve French3d4ef9a2018-04-25 22:19:09 -0500761 else
762 rc = open_shroot(xid, tcon, &fid);
763
Steven Frenchaf6a12e2013-10-09 20:55:53 -0500764 if (rc)
765 return;
766
Steve Frenchc481e9f2013-10-14 01:21:53 -0500767 SMB3_request_interfaces(xid, tcon);
Steve Frenchc481e9f2013-10-14 01:21:53 -0500768
Steven Frenchaf6a12e2013-10-09 20:55:53 -0500769 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
770 FS_ATTRIBUTE_INFORMATION);
771 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
772 FS_DEVICE_INFORMATION);
773 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
Steve French21ba3842018-06-24 23:18:52 -0500774 FS_VOLUME_INFORMATION);
775 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
Steven Frenchaf6a12e2013-10-09 20:55:53 -0500776 FS_SECTOR_SIZE_INFORMATION); /* SMB3 specific */
Steve French3d4ef9a2018-04-25 22:19:09 -0500777 if (no_cached_open)
778 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
Ronnie Sahlberg9da6ec72018-07-31 08:48:22 +1000779 else
780 close_shroot(&tcon->crfid);
Steven Frenchaf6a12e2013-10-09 20:55:53 -0500781}
782
783static void
Steve French34f62642013-10-09 02:07:00 -0500784smb2_qfs_tcon(const unsigned int xid, struct cifs_tcon *tcon)
785{
786 int rc;
787 __le16 srch_path = 0; /* Null - open root of share */
788 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
789 struct cifs_open_parms oparms;
790 struct cifs_fid fid;
791
792 oparms.tcon = tcon;
793 oparms.desired_access = FILE_READ_ATTRIBUTES;
794 oparms.disposition = FILE_OPEN;
795 oparms.create_options = 0;
796 oparms.fid = &fid;
797 oparms.reconnect = false;
798
Ronnie Sahlberg9d874c32018-06-08 13:21:18 +1000799 rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL, NULL);
Steve French34f62642013-10-09 02:07:00 -0500800 if (rc)
801 return;
802
Steven French21671142013-10-09 13:36:35 -0500803 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
804 FS_ATTRIBUTE_INFORMATION);
805 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
806 FS_DEVICE_INFORMATION);
Steve French34f62642013-10-09 02:07:00 -0500807 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
Steve French34f62642013-10-09 02:07:00 -0500808}
809
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +0400810static int
811smb2_is_path_accessible(const unsigned int xid, struct cifs_tcon *tcon,
812 struct cifs_sb_info *cifs_sb, const char *full_path)
813{
814 int rc;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +0400815 __le16 *utf16_path;
Pavel Shilovsky2e44b282012-09-18 16:20:33 -0700816 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
Pavel Shilovsky064f6042013-07-09 18:20:30 +0400817 struct cifs_open_parms oparms;
818 struct cifs_fid fid;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +0400819
Ronnie Sahlberga93864d2018-06-14 06:48:35 +1000820 if ((*full_path == 0) && tcon->crfid.is_valid)
Steve French3d4ef9a2018-04-25 22:19:09 -0500821 return 0;
822
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +0400823 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
824 if (!utf16_path)
825 return -ENOMEM;
826
Pavel Shilovsky064f6042013-07-09 18:20:30 +0400827 oparms.tcon = tcon;
828 oparms.desired_access = FILE_READ_ATTRIBUTES;
829 oparms.disposition = FILE_OPEN;
Steve French5e196972018-08-27 17:04:13 -0500830 if (backup_cred(cifs_sb))
831 oparms.create_options = CREATE_OPEN_BACKUP_INTENT;
832 else
833 oparms.create_options = 0;
Pavel Shilovsky064f6042013-07-09 18:20:30 +0400834 oparms.fid = &fid;
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +0400835 oparms.reconnect = false;
Pavel Shilovsky064f6042013-07-09 18:20:30 +0400836
Ronnie Sahlberg9d874c32018-06-08 13:21:18 +1000837 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, NULL);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +0400838 if (rc) {
839 kfree(utf16_path);
840 return rc;
841 }
842
Pavel Shilovsky064f6042013-07-09 18:20:30 +0400843 rc = SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +0400844 kfree(utf16_path);
845 return rc;
846}
847
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +0400848static int
849smb2_get_srv_inum(const unsigned int xid, struct cifs_tcon *tcon,
850 struct cifs_sb_info *cifs_sb, const char *full_path,
851 u64 *uniqueid, FILE_ALL_INFO *data)
852{
853 *uniqueid = le64_to_cpu(data->IndexNumber);
854 return 0;
855}
856
Pavel Shilovskyb7546bc2012-09-18 16:20:27 -0700857static int
858smb2_query_file_info(const unsigned int xid, struct cifs_tcon *tcon,
859 struct cifs_fid *fid, FILE_ALL_INFO *data)
860{
861 int rc;
862 struct smb2_file_all_info *smb2_data;
863
Pavel Shilovsky1bbe4992014-08-22 13:32:11 +0400864 smb2_data = kzalloc(sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
Pavel Shilovskyb7546bc2012-09-18 16:20:27 -0700865 GFP_KERNEL);
866 if (smb2_data == NULL)
867 return -ENOMEM;
868
869 rc = SMB2_query_info(xid, tcon, fid->persistent_fid, fid->volatile_fid,
870 smb2_data);
871 if (!rc)
872 move_smb2_info_to_cifs(data, smb2_data);
873 kfree(smb2_data);
874 return rc;
875}
876
Arnd Bergmann1368f152017-09-05 11:24:15 +0200877#ifdef CONFIG_CIFS_XATTR
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +1000878static ssize_t
879move_smb2_ea_to_cifs(char *dst, size_t dst_size,
880 struct smb2_file_full_ea_info *src, size_t src_size,
881 const unsigned char *ea_name)
882{
883 int rc = 0;
884 unsigned int ea_name_len = ea_name ? strlen(ea_name) : 0;
885 char *name, *value;
Ronnie Sahlberg0c5d6cb2018-10-25 15:43:36 +1000886 size_t buf_size = dst_size;
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +1000887 size_t name_len, value_len, user_name_len;
888
889 while (src_size > 0) {
890 name = &src->ea_data[0];
891 name_len = (size_t)src->ea_name_length;
892 value = &src->ea_data[src->ea_name_length + 1];
893 value_len = (size_t)le16_to_cpu(src->ea_value_length);
894
Christoph Probsta205d502019-05-08 21:36:25 +0200895 if (name_len == 0)
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +1000896 break;
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +1000897
898 if (src_size < 8 + name_len + 1 + value_len) {
899 cifs_dbg(FYI, "EA entry goes beyond length of list\n");
900 rc = -EIO;
901 goto out;
902 }
903
904 if (ea_name) {
905 if (ea_name_len == name_len &&
906 memcmp(ea_name, name, name_len) == 0) {
907 rc = value_len;
908 if (dst_size == 0)
909 goto out;
910 if (dst_size < value_len) {
911 rc = -ERANGE;
912 goto out;
913 }
914 memcpy(dst, value, value_len);
915 goto out;
916 }
917 } else {
918 /* 'user.' plus a terminating null */
919 user_name_len = 5 + 1 + name_len;
920
Ronnie Sahlberg0c5d6cb2018-10-25 15:43:36 +1000921 if (buf_size == 0) {
922 /* skip copy - calc size only */
923 rc += user_name_len;
924 } else if (dst_size >= user_name_len) {
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +1000925 dst_size -= user_name_len;
926 memcpy(dst, "user.", 5);
927 dst += 5;
928 memcpy(dst, src->ea_data, name_len);
929 dst += name_len;
930 *dst = 0;
931 ++dst;
Ronnie Sahlberg0c5d6cb2018-10-25 15:43:36 +1000932 rc += user_name_len;
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +1000933 } else {
934 /* stop before overrun buffer */
935 rc = -ERANGE;
936 break;
937 }
938 }
939
940 if (!src->next_entry_offset)
941 break;
942
943 if (src_size < le32_to_cpu(src->next_entry_offset)) {
944 /* stop before overrun buffer */
945 rc = -ERANGE;
946 break;
947 }
948 src_size -= le32_to_cpu(src->next_entry_offset);
949 src = (void *)((char *)src +
950 le32_to_cpu(src->next_entry_offset));
951 }
952
953 /* didn't find the named attribute */
954 if (ea_name)
955 rc = -ENODATA;
956
957out:
958 return (ssize_t)rc;
959}
960
961static ssize_t
962smb2_query_eas(const unsigned int xid, struct cifs_tcon *tcon,
963 const unsigned char *path, const unsigned char *ea_name,
964 char *ea_data, size_t buf_size,
965 struct cifs_sb_info *cifs_sb)
966{
967 int rc;
968 __le16 *utf16_path;
Ronnie Sahlbergf9793b62018-11-27 09:52:04 +1000969 struct kvec rsp_iov = {NULL, 0};
970 int buftype = CIFS_NO_BUFFER;
971 struct smb2_query_info_rsp *rsp;
972 struct smb2_file_full_ea_info *info = NULL;
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +1000973
974 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
975 if (!utf16_path)
976 return -ENOMEM;
977
Ronnie Sahlbergf9793b62018-11-27 09:52:04 +1000978 rc = smb2_query_info_compound(xid, tcon, utf16_path,
979 FILE_READ_EA,
980 FILE_FULL_EA_INFORMATION,
981 SMB2_O_INFO_FILE,
Ronnie Sahlbergc4627e62019-01-29 12:46:17 +1000982 CIFSMaxBufSize -
983 MAX_SMB2_CREATE_RESPONSE_SIZE -
984 MAX_SMB2_CLOSE_RESPONSE_SIZE,
Ronnie Sahlbergf9793b62018-11-27 09:52:04 +1000985 &rsp_iov, &buftype, cifs_sb);
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +1000986 if (rc) {
Ronnie Sahlbergf9793b62018-11-27 09:52:04 +1000987 /*
988 * If ea_name is NULL (listxattr) and there are no EAs,
989 * return 0 as it's not an error. Otherwise, the specified
990 * ea_name was not found.
991 */
992 if (!ea_name && rc == -ENODATA)
993 rc = 0;
994 goto qeas_exit;
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +1000995 }
996
Ronnie Sahlbergf9793b62018-11-27 09:52:04 +1000997 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
998 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
999 le32_to_cpu(rsp->OutputBufferLength),
1000 &rsp_iov,
1001 sizeof(struct smb2_file_full_ea_info));
1002 if (rc)
1003 goto qeas_exit;
Ronnie Sahlberg7cb3def2017-09-28 09:39:58 +10001004
Ronnie Sahlbergf9793b62018-11-27 09:52:04 +10001005 info = (struct smb2_file_full_ea_info *)(
1006 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
1007 rc = move_smb2_ea_to_cifs(ea_data, buf_size, info,
1008 le32_to_cpu(rsp->OutputBufferLength), ea_name);
Ronnie Sahlberg7cb3def2017-09-28 09:39:58 +10001009
Ronnie Sahlbergf9793b62018-11-27 09:52:04 +10001010 qeas_exit:
1011 kfree(utf16_path);
1012 free_rsp_buf(buftype, rsp_iov.iov_base);
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10001013 return rc;
1014}
1015
Ronnie Sahlberg55175542017-08-24 11:24:56 +10001016
1017static int
1018smb2_set_ea(const unsigned int xid, struct cifs_tcon *tcon,
1019 const char *path, const char *ea_name, const void *ea_value,
1020 const __u16 ea_value_len, const struct nls_table *nls_codepage,
1021 struct cifs_sb_info *cifs_sb)
1022{
Ronnie Sahlberg0967e542018-11-06 22:52:43 +10001023 struct cifs_ses *ses = tcon->ses;
Ronnie Sahlberg0967e542018-11-06 22:52:43 +10001024 __le16 *utf16_path = NULL;
Ronnie Sahlberg55175542017-08-24 11:24:56 +10001025 int ea_name_len = strlen(ea_name);
Ronnie Sahlberg0967e542018-11-06 22:52:43 +10001026 int flags = 0;
Ronnie Sahlberg55175542017-08-24 11:24:56 +10001027 int len;
Ronnie Sahlberg0967e542018-11-06 22:52:43 +10001028 struct smb_rqst rqst[3];
1029 int resp_buftype[3];
1030 struct kvec rsp_iov[3];
1031 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
1032 struct cifs_open_parms oparms;
1033 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
1034 struct cifs_fid fid;
1035 struct kvec si_iov[SMB2_SET_INFO_IOV_SIZE];
1036 unsigned int size[1];
1037 void *data[1];
1038 struct smb2_file_full_ea_info *ea = NULL;
1039 struct kvec close_iov[1];
1040 int rc;
1041
1042 if (smb3_encryption_required(tcon))
1043 flags |= CIFS_TRANSFORM_REQ;
Ronnie Sahlberg55175542017-08-24 11:24:56 +10001044
1045 if (ea_name_len > 255)
1046 return -EINVAL;
1047
1048 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
1049 if (!utf16_path)
1050 return -ENOMEM;
1051
Ronnie Sahlberg0967e542018-11-06 22:52:43 +10001052 memset(rqst, 0, sizeof(rqst));
1053 resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
1054 memset(rsp_iov, 0, sizeof(rsp_iov));
1055
Ronnie Sahlberg21094642019-02-07 15:48:44 +10001056 if (ses->server->ops->query_all_EAs) {
1057 if (!ea_value) {
1058 rc = ses->server->ops->query_all_EAs(xid, tcon, path,
1059 ea_name, NULL, 0,
1060 cifs_sb);
1061 if (rc == -ENODATA)
1062 goto sea_exit;
1063 }
1064 }
1065
Ronnie Sahlberg0967e542018-11-06 22:52:43 +10001066 /* Open */
1067 memset(&open_iov, 0, sizeof(open_iov));
1068 rqst[0].rq_iov = open_iov;
1069 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
1070
1071 memset(&oparms, 0, sizeof(oparms));
Ronnie Sahlberg55175542017-08-24 11:24:56 +10001072 oparms.tcon = tcon;
1073 oparms.desired_access = FILE_WRITE_EA;
1074 oparms.disposition = FILE_OPEN;
Steve French5e196972018-08-27 17:04:13 -05001075 if (backup_cred(cifs_sb))
1076 oparms.create_options = CREATE_OPEN_BACKUP_INTENT;
1077 else
1078 oparms.create_options = 0;
Ronnie Sahlberg55175542017-08-24 11:24:56 +10001079 oparms.fid = &fid;
1080 oparms.reconnect = false;
1081
Ronnie Sahlberg0967e542018-11-06 22:52:43 +10001082 rc = SMB2_open_init(tcon, &rqst[0], &oplock, &oparms, utf16_path);
1083 if (rc)
1084 goto sea_exit;
Ronnie Sahlberge77fe732018-12-31 13:43:40 +10001085 smb2_set_next_command(tcon, &rqst[0]);
Ronnie Sahlberg0967e542018-11-06 22:52:43 +10001086
1087
1088 /* Set Info */
1089 memset(&si_iov, 0, sizeof(si_iov));
1090 rqst[1].rq_iov = si_iov;
1091 rqst[1].rq_nvec = 1;
Ronnie Sahlberg55175542017-08-24 11:24:56 +10001092
1093 len = sizeof(ea) + ea_name_len + ea_value_len + 1;
1094 ea = kzalloc(len, GFP_KERNEL);
1095 if (ea == NULL) {
Ronnie Sahlberg0967e542018-11-06 22:52:43 +10001096 rc = -ENOMEM;
1097 goto sea_exit;
Ronnie Sahlberg55175542017-08-24 11:24:56 +10001098 }
1099
1100 ea->ea_name_length = ea_name_len;
1101 ea->ea_value_length = cpu_to_le16(ea_value_len);
1102 memcpy(ea->ea_data, ea_name, ea_name_len + 1);
1103 memcpy(ea->ea_data + ea_name_len + 1, ea_value, ea_value_len);
1104
Ronnie Sahlberg0967e542018-11-06 22:52:43 +10001105 size[0] = len;
1106 data[0] = ea;
1107
1108 rc = SMB2_set_info_init(tcon, &rqst[1], COMPOUND_FID,
1109 COMPOUND_FID, current->tgid,
1110 FILE_FULL_EA_INFORMATION,
1111 SMB2_O_INFO_FILE, 0, data, size);
Ronnie Sahlberge77fe732018-12-31 13:43:40 +10001112 smb2_set_next_command(tcon, &rqst[1]);
Ronnie Sahlberg0967e542018-11-06 22:52:43 +10001113 smb2_set_related(&rqst[1]);
1114
1115
1116 /* Close */
1117 memset(&close_iov, 0, sizeof(close_iov));
1118 rqst[2].rq_iov = close_iov;
1119 rqst[2].rq_nvec = 1;
1120 rc = SMB2_close_init(tcon, &rqst[2], COMPOUND_FID, COMPOUND_FID);
1121 smb2_set_related(&rqst[2]);
1122
1123 rc = compound_send_recv(xid, ses, flags, 3, rqst,
1124 resp_buftype, rsp_iov);
1125
1126 sea_exit:
Paulo Alcantara6aa0c112018-07-04 14:16:16 -03001127 kfree(ea);
Ronnie Sahlberg0967e542018-11-06 22:52:43 +10001128 kfree(utf16_path);
1129 SMB2_open_free(&rqst[0]);
1130 SMB2_set_info_free(&rqst[1]);
1131 SMB2_close_free(&rqst[2]);
1132 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
1133 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
1134 free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
Ronnie Sahlberg55175542017-08-24 11:24:56 +10001135 return rc;
1136}
Arnd Bergmann1368f152017-09-05 11:24:15 +02001137#endif
Ronnie Sahlberg55175542017-08-24 11:24:56 +10001138
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04001139static bool
1140smb2_can_echo(struct TCP_Server_Info *server)
1141{
1142 return server->echoes;
1143}
1144
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001145static void
1146smb2_clear_stats(struct cifs_tcon *tcon)
1147{
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001148 int i;
Christoph Probsta205d502019-05-08 21:36:25 +02001149
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001150 for (i = 0; i < NUMBER_OF_SMB2_COMMANDS; i++) {
1151 atomic_set(&tcon->stats.smb2_stats.smb2_com_sent[i], 0);
1152 atomic_set(&tcon->stats.smb2_stats.smb2_com_failed[i], 0);
1153 }
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001154}
1155
1156static void
Steve French769ee6a2013-06-19 14:15:30 -05001157smb2_dump_share_caps(struct seq_file *m, struct cifs_tcon *tcon)
1158{
1159 seq_puts(m, "\n\tShare Capabilities:");
1160 if (tcon->capabilities & SMB2_SHARE_CAP_DFS)
1161 seq_puts(m, " DFS,");
1162 if (tcon->capabilities & SMB2_SHARE_CAP_CONTINUOUS_AVAILABILITY)
1163 seq_puts(m, " CONTINUOUS AVAILABILITY,");
1164 if (tcon->capabilities & SMB2_SHARE_CAP_SCALEOUT)
1165 seq_puts(m, " SCALEOUT,");
1166 if (tcon->capabilities & SMB2_SHARE_CAP_CLUSTER)
1167 seq_puts(m, " CLUSTER,");
1168 if (tcon->capabilities & SMB2_SHARE_CAP_ASYMMETRIC)
1169 seq_puts(m, " ASYMMETRIC,");
1170 if (tcon->capabilities == 0)
1171 seq_puts(m, " None");
Steven Frenchaf6a12e2013-10-09 20:55:53 -05001172 if (tcon->ss_flags & SSINFO_FLAGS_ALIGNED_DEVICE)
1173 seq_puts(m, " Aligned,");
1174 if (tcon->ss_flags & SSINFO_FLAGS_PARTITION_ALIGNED_ON_DEVICE)
1175 seq_puts(m, " Partition Aligned,");
1176 if (tcon->ss_flags & SSINFO_FLAGS_NO_SEEK_PENALTY)
1177 seq_puts(m, " SSD,");
1178 if (tcon->ss_flags & SSINFO_FLAGS_TRIM_ENABLED)
1179 seq_puts(m, " TRIM-support,");
1180
Steve French769ee6a2013-06-19 14:15:30 -05001181 seq_printf(m, "\tShare Flags: 0x%x", tcon->share_flags);
Steve Frenche0386e42018-05-20 01:27:03 -05001182 seq_printf(m, "\n\ttid: 0x%x", tcon->tid);
Steven Frenchaf6a12e2013-10-09 20:55:53 -05001183 if (tcon->perf_sector_size)
1184 seq_printf(m, "\tOptimal sector size: 0x%x",
1185 tcon->perf_sector_size);
Steve Frenche0386e42018-05-20 01:27:03 -05001186 seq_printf(m, "\tMaximal Access: 0x%x", tcon->maximal_access);
Steve French769ee6a2013-06-19 14:15:30 -05001187}
1188
1189static void
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001190smb2_print_stats(struct seq_file *m, struct cifs_tcon *tcon)
1191{
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001192 atomic_t *sent = tcon->stats.smb2_stats.smb2_com_sent;
1193 atomic_t *failed = tcon->stats.smb2_stats.smb2_com_failed;
Steve French1995d282018-07-27 15:14:04 -05001194
1195 /*
1196 * Can't display SMB2_NEGOTIATE, SESSION_SETUP, LOGOFF, CANCEL and ECHO
1197 * totals (requests sent) since those SMBs are per-session not per tcon
1198 */
Steve French52ce1ac2018-07-31 01:46:47 -05001199 seq_printf(m, "\nBytes read: %llu Bytes written: %llu",
1200 (long long)(tcon->bytes_read),
1201 (long long)(tcon->bytes_written));
Steve Frenchfae80442018-10-19 17:14:32 -05001202 seq_printf(m, "\nOpen files: %d total (local), %d open on server",
1203 atomic_read(&tcon->num_local_opens),
1204 atomic_read(&tcon->num_remote_opens));
Steve French1995d282018-07-27 15:14:04 -05001205 seq_printf(m, "\nTreeConnects: %d total %d failed",
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001206 atomic_read(&sent[SMB2_TREE_CONNECT_HE]),
1207 atomic_read(&failed[SMB2_TREE_CONNECT_HE]));
Steve French1995d282018-07-27 15:14:04 -05001208 seq_printf(m, "\nTreeDisconnects: %d total %d failed",
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001209 atomic_read(&sent[SMB2_TREE_DISCONNECT_HE]),
1210 atomic_read(&failed[SMB2_TREE_DISCONNECT_HE]));
Steve French1995d282018-07-27 15:14:04 -05001211 seq_printf(m, "\nCreates: %d total %d failed",
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001212 atomic_read(&sent[SMB2_CREATE_HE]),
1213 atomic_read(&failed[SMB2_CREATE_HE]));
Steve French1995d282018-07-27 15:14:04 -05001214 seq_printf(m, "\nCloses: %d total %d failed",
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001215 atomic_read(&sent[SMB2_CLOSE_HE]),
1216 atomic_read(&failed[SMB2_CLOSE_HE]));
Steve French1995d282018-07-27 15:14:04 -05001217 seq_printf(m, "\nFlushes: %d total %d failed",
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001218 atomic_read(&sent[SMB2_FLUSH_HE]),
1219 atomic_read(&failed[SMB2_FLUSH_HE]));
Steve French1995d282018-07-27 15:14:04 -05001220 seq_printf(m, "\nReads: %d total %d failed",
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001221 atomic_read(&sent[SMB2_READ_HE]),
1222 atomic_read(&failed[SMB2_READ_HE]));
Steve French1995d282018-07-27 15:14:04 -05001223 seq_printf(m, "\nWrites: %d total %d failed",
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001224 atomic_read(&sent[SMB2_WRITE_HE]),
1225 atomic_read(&failed[SMB2_WRITE_HE]));
Steve French1995d282018-07-27 15:14:04 -05001226 seq_printf(m, "\nLocks: %d total %d failed",
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001227 atomic_read(&sent[SMB2_LOCK_HE]),
1228 atomic_read(&failed[SMB2_LOCK_HE]));
Steve French1995d282018-07-27 15:14:04 -05001229 seq_printf(m, "\nIOCTLs: %d total %d failed",
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001230 atomic_read(&sent[SMB2_IOCTL_HE]),
1231 atomic_read(&failed[SMB2_IOCTL_HE]));
Steve French1995d282018-07-27 15:14:04 -05001232 seq_printf(m, "\nQueryDirectories: %d total %d failed",
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001233 atomic_read(&sent[SMB2_QUERY_DIRECTORY_HE]),
1234 atomic_read(&failed[SMB2_QUERY_DIRECTORY_HE]));
Steve French1995d282018-07-27 15:14:04 -05001235 seq_printf(m, "\nChangeNotifies: %d total %d failed",
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001236 atomic_read(&sent[SMB2_CHANGE_NOTIFY_HE]),
1237 atomic_read(&failed[SMB2_CHANGE_NOTIFY_HE]));
Steve French1995d282018-07-27 15:14:04 -05001238 seq_printf(m, "\nQueryInfos: %d total %d failed",
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001239 atomic_read(&sent[SMB2_QUERY_INFO_HE]),
1240 atomic_read(&failed[SMB2_QUERY_INFO_HE]));
Steve French1995d282018-07-27 15:14:04 -05001241 seq_printf(m, "\nSetInfos: %d total %d failed",
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001242 atomic_read(&sent[SMB2_SET_INFO_HE]),
1243 atomic_read(&failed[SMB2_SET_INFO_HE]));
1244 seq_printf(m, "\nOplockBreaks: %d sent %d failed",
1245 atomic_read(&sent[SMB2_OPLOCK_BREAK_HE]),
1246 atomic_read(&failed[SMB2_OPLOCK_BREAK_HE]));
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001247}
1248
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001249static void
1250smb2_set_fid(struct cifsFileInfo *cfile, struct cifs_fid *fid, __u32 oplock)
1251{
David Howells2b0143b2015-03-17 22:25:59 +00001252 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04001253 struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server;
1254
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001255 cfile->fid.persistent_fid = fid->persistent_fid;
1256 cfile->fid.volatile_fid = fid->volatile_fid;
Steve Frenchdfe33f92018-10-30 19:50:31 -05001257#ifdef CONFIG_CIFS_DEBUG2
1258 cfile->fid.mid = fid->mid;
1259#endif /* CIFS_DEBUG2 */
Pavel Shilovsky42873b02013-09-05 21:30:16 +04001260 server->ops->set_oplock_level(cinode, oplock, fid->epoch,
1261 &fid->purge_cache);
Pavel Shilovsky18cceb62013-09-05 13:01:06 +04001262 cinode->can_cache_brlcks = CIFS_CACHE_WRITE(cinode);
Aurelien Aptel94f87372016-09-22 07:38:50 +02001263 memcpy(cfile->fid.create_guid, fid->create_guid, 16);
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001264}
1265
Pavel Shilovsky760ad0c2012-09-25 11:00:07 +04001266static void
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001267smb2_close_file(const unsigned int xid, struct cifs_tcon *tcon,
1268 struct cifs_fid *fid)
1269{
Pavel Shilovsky760ad0c2012-09-25 11:00:07 +04001270 SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001271}
1272
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07001273static int
Steve French41c13582013-11-14 00:05:36 -06001274SMB2_request_res_key(const unsigned int xid, struct cifs_tcon *tcon,
1275 u64 persistent_fid, u64 volatile_fid,
1276 struct copychunk_ioctl *pcchunk)
1277{
1278 int rc;
1279 unsigned int ret_data_len;
1280 struct resume_key_req *res_key;
1281
1282 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
1283 FSCTL_SRV_REQUEST_RESUME_KEY, true /* is_fsctl */,
Steve French153322f2019-03-28 22:32:49 -05001284 NULL, 0 /* no input */, CIFSMaxBufSize,
Steve French41c13582013-11-14 00:05:36 -06001285 (char **)&res_key, &ret_data_len);
1286
1287 if (rc) {
1288 cifs_dbg(VFS, "refcpy ioctl error %d getting resume key\n", rc);
1289 goto req_res_key_exit;
1290 }
1291 if (ret_data_len < sizeof(struct resume_key_req)) {
1292 cifs_dbg(VFS, "Invalid refcopy resume key length\n");
1293 rc = -EINVAL;
1294 goto req_res_key_exit;
1295 }
1296 memcpy(pcchunk->SourceKey, res_key->ResumeKey, COPY_CHUNK_RES_KEY_SIZE);
1297
1298req_res_key_exit:
1299 kfree(res_key);
1300 return rc;
1301}
1302
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05001303static int
1304smb2_ioctl_query_info(const unsigned int xid,
Ronnie Sahlberg8d8b26e2018-10-17 05:47:58 +10001305 struct cifs_tcon *tcon,
1306 __le16 *path, int is_dir,
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05001307 unsigned long p)
1308{
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05001309 struct cifs_ses *ses = tcon->ses;
1310 char __user *arg = (char __user *)p;
1311 struct smb_query_info qi;
1312 struct smb_query_info __user *pqi;
1313 int rc = 0;
1314 int flags = 0;
Ronnie Sahlbergf5778c32019-03-15 09:07:22 +10001315 struct smb2_query_info_rsp *qi_rsp = NULL;
1316 struct smb2_ioctl_rsp *io_rsp = NULL;
Ronnie Sahlberg8d8b26e2018-10-17 05:47:58 +10001317 void *buffer = NULL;
1318 struct smb_rqst rqst[3];
1319 int resp_buftype[3];
1320 struct kvec rsp_iov[3];
1321 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
1322 struct cifs_open_parms oparms;
1323 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
1324 struct cifs_fid fid;
1325 struct kvec qi_iov[1];
Ronnie Sahlbergf5778c32019-03-15 09:07:22 +10001326 struct kvec io_iov[SMB2_IOCTL_IOV_SIZE];
Ronnie Sahlberg8d8b26e2018-10-17 05:47:58 +10001327 struct kvec close_iov[1];
1328
1329 memset(rqst, 0, sizeof(rqst));
1330 resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
1331 memset(rsp_iov, 0, sizeof(rsp_iov));
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05001332
1333 if (copy_from_user(&qi, arg, sizeof(struct smb_query_info)))
1334 return -EFAULT;
1335
1336 if (qi.output_buffer_length > 1024)
1337 return -EINVAL;
1338
1339 if (!ses || !(ses->server))
1340 return -EIO;
1341
1342 if (smb3_encryption_required(tcon))
1343 flags |= CIFS_TRANSFORM_REQ;
1344
1345 buffer = kmalloc(qi.output_buffer_length, GFP_KERNEL);
1346 if (buffer == NULL)
1347 return -ENOMEM;
1348
1349 if (copy_from_user(buffer, arg + sizeof(struct smb_query_info),
1350 qi.output_buffer_length)) {
Ronnie Sahlberg8d8b26e2018-10-17 05:47:58 +10001351 rc = -EFAULT;
1352 goto iqinf_exit;
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05001353 }
1354
Ronnie Sahlberg8d8b26e2018-10-17 05:47:58 +10001355 /* Open */
1356 memset(&open_iov, 0, sizeof(open_iov));
1357 rqst[0].rq_iov = open_iov;
1358 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05001359
Ronnie Sahlberg8d8b26e2018-10-17 05:47:58 +10001360 memset(&oparms, 0, sizeof(oparms));
1361 oparms.tcon = tcon;
1362 oparms.desired_access = FILE_READ_ATTRIBUTES | READ_CONTROL;
1363 oparms.disposition = FILE_OPEN;
1364 if (is_dir)
1365 oparms.create_options = CREATE_NOT_FILE;
1366 else
1367 oparms.create_options = CREATE_NOT_DIR;
1368 oparms.fid = &fid;
1369 oparms.reconnect = false;
1370
Ronnie Sahlbergefac7792019-04-11 12:20:17 +10001371 /*
1372 * FSCTL codes encode the special access they need in the fsctl code.
1373 */
1374 if (qi.flags & PASSTHRU_FSCTL) {
1375 switch (qi.info_type & FSCTL_DEVICE_ACCESS_MASK) {
1376 case FSCTL_DEVICE_ACCESS_FILE_READ_WRITE_ACCESS:
1377 oparms.desired_access = FILE_READ_DATA | FILE_WRITE_DATA | FILE_READ_ATTRIBUTES | SYNCHRONIZE;
Steve French46e66612019-04-11 13:53:17 -05001378 break;
1379 case FSCTL_DEVICE_ACCESS_FILE_ANY_ACCESS:
1380 oparms.desired_access = GENERIC_ALL;
1381 break;
1382 case FSCTL_DEVICE_ACCESS_FILE_READ_ACCESS:
1383 oparms.desired_access = GENERIC_READ;
1384 break;
1385 case FSCTL_DEVICE_ACCESS_FILE_WRITE_ACCESS:
1386 oparms.desired_access = GENERIC_WRITE;
Ronnie Sahlbergefac7792019-04-11 12:20:17 +10001387 break;
1388 }
1389 }
1390
Ronnie Sahlberg8d8b26e2018-10-17 05:47:58 +10001391 rc = SMB2_open_init(tcon, &rqst[0], &oplock, &oparms, path);
1392 if (rc)
1393 goto iqinf_exit;
Ronnie Sahlberge77fe732018-12-31 13:43:40 +10001394 smb2_set_next_command(tcon, &rqst[0]);
Ronnie Sahlberg8d8b26e2018-10-17 05:47:58 +10001395
1396 /* Query */
Steve French31ba4332019-03-13 02:40:07 -05001397 if (qi.flags & PASSTHRU_FSCTL) {
1398 /* Can eventually relax perm check since server enforces too */
1399 if (!capable(CAP_SYS_ADMIN))
1400 rc = -EPERM;
Ronnie Sahlbergf5778c32019-03-15 09:07:22 +10001401 else {
1402 memset(&io_iov, 0, sizeof(io_iov));
1403 rqst[1].rq_iov = io_iov;
1404 rqst[1].rq_nvec = SMB2_IOCTL_IOV_SIZE;
1405
1406 rc = SMB2_ioctl_init(tcon, &rqst[1],
1407 COMPOUND_FID, COMPOUND_FID,
Ronnie Sahlbergefac7792019-04-11 12:20:17 +10001408 qi.info_type, true, buffer,
1409 qi.output_buffer_length,
1410 CIFSMaxBufSize);
Ronnie Sahlbergf5778c32019-03-15 09:07:22 +10001411 }
Steve French31ba4332019-03-13 02:40:07 -05001412 } else if (qi.flags == PASSTHRU_QUERY_INFO) {
1413 memset(&qi_iov, 0, sizeof(qi_iov));
1414 rqst[1].rq_iov = qi_iov;
1415 rqst[1].rq_nvec = 1;
Ronnie Sahlberg8d8b26e2018-10-17 05:47:58 +10001416
Steve French31ba4332019-03-13 02:40:07 -05001417 rc = SMB2_query_info_init(tcon, &rqst[1], COMPOUND_FID,
1418 COMPOUND_FID, qi.file_info_class,
1419 qi.info_type, qi.additional_information,
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05001420 qi.input_buffer_length,
1421 qi.output_buffer_length, buffer);
Steve French31ba4332019-03-13 02:40:07 -05001422 } else { /* unknown flags */
1423 cifs_dbg(VFS, "invalid passthru query flags: 0x%x\n", qi.flags);
1424 rc = -EINVAL;
1425 }
1426
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05001427 if (rc)
1428 goto iqinf_exit;
Ronnie Sahlberge77fe732018-12-31 13:43:40 +10001429 smb2_set_next_command(tcon, &rqst[1]);
Ronnie Sahlberg8d8b26e2018-10-17 05:47:58 +10001430 smb2_set_related(&rqst[1]);
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05001431
Ronnie Sahlberg8d8b26e2018-10-17 05:47:58 +10001432 /* Close */
1433 memset(&close_iov, 0, sizeof(close_iov));
1434 rqst[2].rq_iov = close_iov;
1435 rqst[2].rq_nvec = 1;
1436
1437 rc = SMB2_close_init(tcon, &rqst[2], COMPOUND_FID, COMPOUND_FID);
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05001438 if (rc)
1439 goto iqinf_exit;
Ronnie Sahlberg8d8b26e2018-10-17 05:47:58 +10001440 smb2_set_related(&rqst[2]);
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05001441
Ronnie Sahlberg8d8b26e2018-10-17 05:47:58 +10001442 rc = compound_send_recv(xid, ses, flags, 3, rqst,
1443 resp_buftype, rsp_iov);
1444 if (rc)
1445 goto iqinf_exit;
Ronnie Sahlbergf5778c32019-03-15 09:07:22 +10001446 if (qi.flags & PASSTHRU_FSCTL) {
1447 pqi = (struct smb_query_info __user *)arg;
1448 io_rsp = (struct smb2_ioctl_rsp *)rsp_iov[1].iov_base;
1449 if (le32_to_cpu(io_rsp->OutputCount) < qi.input_buffer_length)
1450 qi.input_buffer_length = le32_to_cpu(io_rsp->OutputCount);
Ronnie Sahlberg5242fcb2019-04-15 12:13:52 +10001451 if (qi.input_buffer_length > 0 &&
1452 le32_to_cpu(io_rsp->OutputOffset) + qi.input_buffer_length > rsp_iov[1].iov_len) {
1453 rc = -EFAULT;
1454 goto iqinf_exit;
1455 }
Ronnie Sahlbergf5778c32019-03-15 09:07:22 +10001456 if (copy_to_user(&pqi->input_buffer_length, &qi.input_buffer_length,
1457 sizeof(qi.input_buffer_length))) {
1458 rc = -EFAULT;
1459 goto iqinf_exit;
1460 }
Ronnie Sahlberg5242fcb2019-04-15 12:13:52 +10001461 if (copy_to_user((void __user *)pqi + sizeof(struct smb_query_info),
1462 (const void *)io_rsp + le32_to_cpu(io_rsp->OutputOffset),
1463 qi.input_buffer_length)) {
Ronnie Sahlbergf5778c32019-03-15 09:07:22 +10001464 rc = -EFAULT;
1465 goto iqinf_exit;
1466 }
1467 } else {
1468 pqi = (struct smb_query_info __user *)arg;
1469 qi_rsp = (struct smb2_query_info_rsp *)rsp_iov[1].iov_base;
1470 if (le32_to_cpu(qi_rsp->OutputBufferLength) < qi.input_buffer_length)
1471 qi.input_buffer_length = le32_to_cpu(qi_rsp->OutputBufferLength);
1472 if (copy_to_user(&pqi->input_buffer_length, &qi.input_buffer_length,
1473 sizeof(qi.input_buffer_length))) {
1474 rc = -EFAULT;
1475 goto iqinf_exit;
1476 }
1477 if (copy_to_user(pqi + 1, qi_rsp->Buffer, qi.input_buffer_length)) {
1478 rc = -EFAULT;
1479 goto iqinf_exit;
1480 }
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05001481 }
1482
1483 iqinf_exit:
Ronnie Sahlberg8d8b26e2018-10-17 05:47:58 +10001484 kfree(buffer);
1485 SMB2_open_free(&rqst[0]);
Ronnie Sahlbergf5778c32019-03-15 09:07:22 +10001486 if (qi.flags & PASSTHRU_FSCTL)
1487 SMB2_ioctl_free(&rqst[1]);
1488 else
1489 SMB2_query_info_free(&rqst[1]);
1490
Ronnie Sahlberg8d8b26e2018-10-17 05:47:58 +10001491 SMB2_close_free(&rqst[2]);
1492 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
1493 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
1494 free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05001495 return rc;
1496}
1497
Sachin Prabhu620d8742017-02-10 16:03:51 +05301498static ssize_t
Sachin Prabhu312bbc52017-04-04 02:12:04 -05001499smb2_copychunk_range(const unsigned int xid,
Steve French41c13582013-11-14 00:05:36 -06001500 struct cifsFileInfo *srcfile,
1501 struct cifsFileInfo *trgtfile, u64 src_off,
1502 u64 len, u64 dest_off)
1503{
1504 int rc;
1505 unsigned int ret_data_len;
1506 struct copychunk_ioctl *pcchunk;
Steve French9bf0c9c2013-11-16 18:05:28 -06001507 struct copychunk_ioctl_rsp *retbuf = NULL;
1508 struct cifs_tcon *tcon;
1509 int chunks_copied = 0;
1510 bool chunk_sizes_updated = false;
Sachin Prabhu620d8742017-02-10 16:03:51 +05301511 ssize_t bytes_written, total_bytes_written = 0;
Steve French41c13582013-11-14 00:05:36 -06001512
1513 pcchunk = kmalloc(sizeof(struct copychunk_ioctl), GFP_KERNEL);
1514
1515 if (pcchunk == NULL)
1516 return -ENOMEM;
1517
Christoph Probsta205d502019-05-08 21:36:25 +02001518 cifs_dbg(FYI, "%s: about to call request res key\n", __func__);
Steve French41c13582013-11-14 00:05:36 -06001519 /* Request a key from the server to identify the source of the copy */
1520 rc = SMB2_request_res_key(xid, tlink_tcon(srcfile->tlink),
1521 srcfile->fid.persistent_fid,
1522 srcfile->fid.volatile_fid, pcchunk);
1523
1524 /* Note: request_res_key sets res_key null only if rc !=0 */
1525 if (rc)
Steve French9bf0c9c2013-11-16 18:05:28 -06001526 goto cchunk_out;
Steve French41c13582013-11-14 00:05:36 -06001527
1528 /* For now array only one chunk long, will make more flexible later */
Fabian Frederickbc09d142014-12-10 15:41:15 -08001529 pcchunk->ChunkCount = cpu_to_le32(1);
Steve French41c13582013-11-14 00:05:36 -06001530 pcchunk->Reserved = 0;
Steve French41c13582013-11-14 00:05:36 -06001531 pcchunk->Reserved2 = 0;
1532
Steve French9bf0c9c2013-11-16 18:05:28 -06001533 tcon = tlink_tcon(trgtfile->tlink);
1534
1535 while (len > 0) {
1536 pcchunk->SourceOffset = cpu_to_le64(src_off);
1537 pcchunk->TargetOffset = cpu_to_le64(dest_off);
1538 pcchunk->Length =
1539 cpu_to_le32(min_t(u32, len, tcon->max_bytes_chunk));
1540
1541 /* Request server copy to target from src identified by key */
1542 rc = SMB2_ioctl(xid, tcon, trgtfile->fid.persistent_fid,
Steve French41c13582013-11-14 00:05:36 -06001543 trgtfile->fid.volatile_fid, FSCTL_SRV_COPYCHUNK_WRITE,
Aurelien Aptel63a83b82018-01-24 13:46:11 +01001544 true /* is_fsctl */, (char *)pcchunk,
Steve French153322f2019-03-28 22:32:49 -05001545 sizeof(struct copychunk_ioctl), CIFSMaxBufSize,
1546 (char **)&retbuf, &ret_data_len);
Steve French9bf0c9c2013-11-16 18:05:28 -06001547 if (rc == 0) {
1548 if (ret_data_len !=
1549 sizeof(struct copychunk_ioctl_rsp)) {
1550 cifs_dbg(VFS, "invalid cchunk response size\n");
1551 rc = -EIO;
1552 goto cchunk_out;
1553 }
1554 if (retbuf->TotalBytesWritten == 0) {
1555 cifs_dbg(FYI, "no bytes copied\n");
1556 rc = -EIO;
1557 goto cchunk_out;
1558 }
1559 /*
1560 * Check if server claimed to write more than we asked
1561 */
1562 if (le32_to_cpu(retbuf->TotalBytesWritten) >
1563 le32_to_cpu(pcchunk->Length)) {
1564 cifs_dbg(VFS, "invalid copy chunk response\n");
1565 rc = -EIO;
1566 goto cchunk_out;
1567 }
1568 if (le32_to_cpu(retbuf->ChunksWritten) != 1) {
1569 cifs_dbg(VFS, "invalid num chunks written\n");
1570 rc = -EIO;
1571 goto cchunk_out;
1572 }
1573 chunks_copied++;
Steve French41c13582013-11-14 00:05:36 -06001574
Sachin Prabhu620d8742017-02-10 16:03:51 +05301575 bytes_written = le32_to_cpu(retbuf->TotalBytesWritten);
1576 src_off += bytes_written;
1577 dest_off += bytes_written;
1578 len -= bytes_written;
1579 total_bytes_written += bytes_written;
Steve French41c13582013-11-14 00:05:36 -06001580
Sachin Prabhu620d8742017-02-10 16:03:51 +05301581 cifs_dbg(FYI, "Chunks %d PartialChunk %d Total %zu\n",
Steve French9bf0c9c2013-11-16 18:05:28 -06001582 le32_to_cpu(retbuf->ChunksWritten),
1583 le32_to_cpu(retbuf->ChunkBytesWritten),
Sachin Prabhu620d8742017-02-10 16:03:51 +05301584 bytes_written);
Steve French9bf0c9c2013-11-16 18:05:28 -06001585 } else if (rc == -EINVAL) {
1586 if (ret_data_len != sizeof(struct copychunk_ioctl_rsp))
1587 goto cchunk_out;
Steve French41c13582013-11-14 00:05:36 -06001588
Steve French9bf0c9c2013-11-16 18:05:28 -06001589 cifs_dbg(FYI, "MaxChunks %d BytesChunk %d MaxCopy %d\n",
1590 le32_to_cpu(retbuf->ChunksWritten),
1591 le32_to_cpu(retbuf->ChunkBytesWritten),
1592 le32_to_cpu(retbuf->TotalBytesWritten));
1593
1594 /*
1595 * Check if this is the first request using these sizes,
1596 * (ie check if copy succeed once with original sizes
1597 * and check if the server gave us different sizes after
1598 * we already updated max sizes on previous request).
1599 * if not then why is the server returning an error now
1600 */
1601 if ((chunks_copied != 0) || chunk_sizes_updated)
1602 goto cchunk_out;
1603
1604 /* Check that server is not asking us to grow size */
1605 if (le32_to_cpu(retbuf->ChunkBytesWritten) <
1606 tcon->max_bytes_chunk)
1607 tcon->max_bytes_chunk =
1608 le32_to_cpu(retbuf->ChunkBytesWritten);
1609 else
1610 goto cchunk_out; /* server gave us bogus size */
1611
1612 /* No need to change MaxChunks since already set to 1 */
1613 chunk_sizes_updated = true;
Sachin Prabhu2477bc52015-02-04 13:10:26 +00001614 } else
1615 goto cchunk_out;
Steve French9bf0c9c2013-11-16 18:05:28 -06001616 }
1617
1618cchunk_out:
Steve French41c13582013-11-14 00:05:36 -06001619 kfree(pcchunk);
Steve French24df1482016-09-29 04:20:23 -05001620 kfree(retbuf);
Sachin Prabhu620d8742017-02-10 16:03:51 +05301621 if (rc)
1622 return rc;
1623 else
1624 return total_bytes_written;
Steve French41c13582013-11-14 00:05:36 -06001625}
1626
1627static int
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07001628smb2_flush_file(const unsigned int xid, struct cifs_tcon *tcon,
1629 struct cifs_fid *fid)
1630{
1631 return SMB2_flush(xid, tcon, fid->persistent_fid, fid->volatile_fid);
1632}
1633
Pavel Shilovsky09a47072012-09-18 16:20:29 -07001634static unsigned int
1635smb2_read_data_offset(char *buf)
1636{
1637 struct smb2_read_rsp *rsp = (struct smb2_read_rsp *)buf;
Christoph Probsta205d502019-05-08 21:36:25 +02001638
Pavel Shilovsky09a47072012-09-18 16:20:29 -07001639 return rsp->DataOffset;
1640}
1641
1642static unsigned int
Long Li74dcf412017-11-22 17:38:46 -07001643smb2_read_data_length(char *buf, bool in_remaining)
Pavel Shilovsky09a47072012-09-18 16:20:29 -07001644{
1645 struct smb2_read_rsp *rsp = (struct smb2_read_rsp *)buf;
Long Li74dcf412017-11-22 17:38:46 -07001646
1647 if (in_remaining)
1648 return le32_to_cpu(rsp->DataRemaining);
1649
Pavel Shilovsky09a47072012-09-18 16:20:29 -07001650 return le32_to_cpu(rsp->DataLength);
1651}
1652
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07001653
1654static int
Steve Frenchdb8b6312014-09-22 05:13:55 -05001655smb2_sync_read(const unsigned int xid, struct cifs_fid *pfid,
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07001656 struct cifs_io_parms *parms, unsigned int *bytes_read,
1657 char **buf, int *buf_type)
1658{
Steve Frenchdb8b6312014-09-22 05:13:55 -05001659 parms->persistent_fid = pfid->persistent_fid;
1660 parms->volatile_fid = pfid->volatile_fid;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07001661 return SMB2_read(xid, parms, bytes_read, buf, buf_type);
1662}
1663
Pavel Shilovsky009d3442012-09-18 16:20:30 -07001664static int
Steve Frenchdb8b6312014-09-22 05:13:55 -05001665smb2_sync_write(const unsigned int xid, struct cifs_fid *pfid,
Pavel Shilovsky009d3442012-09-18 16:20:30 -07001666 struct cifs_io_parms *parms, unsigned int *written,
1667 struct kvec *iov, unsigned long nr_segs)
1668{
1669
Steve Frenchdb8b6312014-09-22 05:13:55 -05001670 parms->persistent_fid = pfid->persistent_fid;
1671 parms->volatile_fid = pfid->volatile_fid;
Pavel Shilovsky009d3442012-09-18 16:20:30 -07001672 return SMB2_write(xid, parms, written, iov, nr_segs);
1673}
1674
Steve Frenchd43cc792014-08-13 17:16:29 -05001675/* Set or clear the SPARSE_FILE attribute based on value passed in setsparse */
1676static bool smb2_set_sparse(const unsigned int xid, struct cifs_tcon *tcon,
1677 struct cifsFileInfo *cfile, struct inode *inode, __u8 setsparse)
1678{
1679 struct cifsInodeInfo *cifsi;
1680 int rc;
1681
1682 cifsi = CIFS_I(inode);
1683
1684 /* if file already sparse don't bother setting sparse again */
1685 if ((cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) && setsparse)
1686 return true; /* already sparse */
1687
1688 if (!(cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) && !setsparse)
1689 return true; /* already not sparse */
1690
1691 /*
1692 * Can't check for sparse support on share the usual way via the
1693 * FS attribute info (FILE_SUPPORTS_SPARSE_FILES) on the share
1694 * since Samba server doesn't set the flag on the share, yet
1695 * supports the set sparse FSCTL and returns sparse correctly
1696 * in the file attributes. If we fail setting sparse though we
1697 * mark that server does not support sparse files for this share
1698 * to avoid repeatedly sending the unsupported fsctl to server
1699 * if the file is repeatedly extended.
1700 */
1701 if (tcon->broken_sparse_sup)
1702 return false;
1703
1704 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
1705 cfile->fid.volatile_fid, FSCTL_SET_SPARSE,
Aurelien Aptel63a83b82018-01-24 13:46:11 +01001706 true /* is_fctl */,
Steve French153322f2019-03-28 22:32:49 -05001707 &setsparse, 1, CIFSMaxBufSize, NULL, NULL);
Steve Frenchd43cc792014-08-13 17:16:29 -05001708 if (rc) {
1709 tcon->broken_sparse_sup = true;
1710 cifs_dbg(FYI, "set sparse rc = %d\n", rc);
1711 return false;
1712 }
1713
1714 if (setsparse)
1715 cifsi->cifsAttrs |= FILE_ATTRIBUTE_SPARSE_FILE;
1716 else
1717 cifsi->cifsAttrs &= (~FILE_ATTRIBUTE_SPARSE_FILE);
1718
1719 return true;
1720}
1721
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07001722static int
1723smb2_set_file_size(const unsigned int xid, struct cifs_tcon *tcon,
1724 struct cifsFileInfo *cfile, __u64 size, bool set_alloc)
1725{
1726 __le64 eof = cpu_to_le64(size);
Steve French3d1a3742014-08-11 21:05:25 -05001727 struct inode *inode;
1728
1729 /*
1730 * If extending file more than one page make sparse. Many Linux fs
1731 * make files sparse by default when extending via ftruncate
1732 */
David Howells2b0143b2015-03-17 22:25:59 +00001733 inode = d_inode(cfile->dentry);
Steve French3d1a3742014-08-11 21:05:25 -05001734
1735 if (!set_alloc && (size > inode->i_size + 8192)) {
Steve French3d1a3742014-08-11 21:05:25 -05001736 __u8 set_sparse = 1;
Steve French3d1a3742014-08-11 21:05:25 -05001737
Steve Frenchd43cc792014-08-13 17:16:29 -05001738 /* whether set sparse succeeds or not, extend the file */
1739 smb2_set_sparse(xid, tcon, cfile, inode, set_sparse);
Steve French3d1a3742014-08-11 21:05:25 -05001740 }
1741
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07001742 return SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
Ronnie Sahlberg3764cbd2018-09-03 13:33:47 +10001743 cfile->fid.volatile_fid, cfile->pid, &eof);
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07001744}
1745
Steve French02b16662015-06-27 21:18:36 -07001746static int
1747smb2_duplicate_extents(const unsigned int xid,
1748 struct cifsFileInfo *srcfile,
1749 struct cifsFileInfo *trgtfile, u64 src_off,
1750 u64 len, u64 dest_off)
1751{
1752 int rc;
1753 unsigned int ret_data_len;
Steve French02b16662015-06-27 21:18:36 -07001754 struct duplicate_extents_to_file dup_ext_buf;
1755 struct cifs_tcon *tcon = tlink_tcon(trgtfile->tlink);
1756
1757 /* server fileays advertise duplicate extent support with this flag */
1758 if ((le32_to_cpu(tcon->fsAttrInfo.Attributes) &
1759 FILE_SUPPORTS_BLOCK_REFCOUNTING) == 0)
1760 return -EOPNOTSUPP;
1761
1762 dup_ext_buf.VolatileFileHandle = srcfile->fid.volatile_fid;
1763 dup_ext_buf.PersistentFileHandle = srcfile->fid.persistent_fid;
1764 dup_ext_buf.SourceFileOffset = cpu_to_le64(src_off);
1765 dup_ext_buf.TargetFileOffset = cpu_to_le64(dest_off);
1766 dup_ext_buf.ByteCount = cpu_to_le64(len);
Christoph Probsta205d502019-05-08 21:36:25 +02001767 cifs_dbg(FYI, "Duplicate extents: src off %lld dst off %lld len %lld\n",
Steve French02b16662015-06-27 21:18:36 -07001768 src_off, dest_off, len);
1769
1770 rc = smb2_set_file_size(xid, tcon, trgtfile, dest_off + len, false);
1771 if (rc)
1772 goto duplicate_extents_out;
1773
1774 rc = SMB2_ioctl(xid, tcon, trgtfile->fid.persistent_fid,
1775 trgtfile->fid.volatile_fid,
1776 FSCTL_DUPLICATE_EXTENTS_TO_FILE,
Aurelien Aptel63a83b82018-01-24 13:46:11 +01001777 true /* is_fsctl */,
Aurelien Aptel51146622017-02-28 15:08:41 +01001778 (char *)&dup_ext_buf,
Steve French02b16662015-06-27 21:18:36 -07001779 sizeof(struct duplicate_extents_to_file),
Steve French153322f2019-03-28 22:32:49 -05001780 CIFSMaxBufSize, NULL,
Steve French02b16662015-06-27 21:18:36 -07001781 &ret_data_len);
1782
1783 if (ret_data_len > 0)
Christoph Probsta205d502019-05-08 21:36:25 +02001784 cifs_dbg(FYI, "Non-zero response length in duplicate extents\n");
Steve French02b16662015-06-27 21:18:36 -07001785
1786duplicate_extents_out:
1787 return rc;
1788}
Steve French02b16662015-06-27 21:18:36 -07001789
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07001790static int
Steve French64a5cfa2013-10-14 15:31:32 -05001791smb2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
1792 struct cifsFileInfo *cfile)
1793{
1794 return SMB2_set_compression(xid, tcon, cfile->fid.persistent_fid,
1795 cfile->fid.volatile_fid);
1796}
1797
1798static int
Steve Frenchb3152e22015-06-24 03:17:02 -05001799smb3_set_integrity(const unsigned int xid, struct cifs_tcon *tcon,
1800 struct cifsFileInfo *cfile)
1801{
1802 struct fsctl_set_integrity_information_req integr_info;
Steve Frenchb3152e22015-06-24 03:17:02 -05001803 unsigned int ret_data_len;
1804
1805 integr_info.ChecksumAlgorithm = cpu_to_le16(CHECKSUM_TYPE_UNCHANGED);
1806 integr_info.Flags = 0;
1807 integr_info.Reserved = 0;
1808
1809 return SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
1810 cfile->fid.volatile_fid,
1811 FSCTL_SET_INTEGRITY_INFORMATION,
Aurelien Aptel63a83b82018-01-24 13:46:11 +01001812 true /* is_fsctl */,
Aurelien Aptel51146622017-02-28 15:08:41 +01001813 (char *)&integr_info,
Steve Frenchb3152e22015-06-24 03:17:02 -05001814 sizeof(struct fsctl_set_integrity_information_req),
Steve French153322f2019-03-28 22:32:49 -05001815 CIFSMaxBufSize, NULL,
Steve Frenchb3152e22015-06-24 03:17:02 -05001816 &ret_data_len);
1817
1818}
1819
Steve Frenche02789a2018-08-09 14:33:12 -05001820/* GMT Token is @GMT-YYYY.MM.DD-HH.MM.SS Unicode which is 48 bytes + null */
1821#define GMT_TOKEN_SIZE 50
1822
Steve French153322f2019-03-28 22:32:49 -05001823#define MIN_SNAPSHOT_ARRAY_SIZE 16 /* See MS-SMB2 section 3.3.5.15.1 */
1824
Steve Frenche02789a2018-08-09 14:33:12 -05001825/*
1826 * Input buffer contains (empty) struct smb_snapshot array with size filled in
1827 * For output see struct SRV_SNAPSHOT_ARRAY in MS-SMB2 section 2.2.32.2
1828 */
Steve Frenchb3152e22015-06-24 03:17:02 -05001829static int
Steve French834170c2016-09-30 21:14:26 -05001830smb3_enum_snapshots(const unsigned int xid, struct cifs_tcon *tcon,
1831 struct cifsFileInfo *cfile, void __user *ioc_buf)
1832{
1833 char *retbuf = NULL;
1834 unsigned int ret_data_len = 0;
1835 int rc;
Steve French153322f2019-03-28 22:32:49 -05001836 u32 max_response_size;
Steve French834170c2016-09-30 21:14:26 -05001837 struct smb_snapshot_array snapshot_in;
1838
Steve French973189a2019-04-04 00:41:04 -05001839 /*
1840 * On the first query to enumerate the list of snapshots available
1841 * for this volume the buffer begins with 0 (number of snapshots
1842 * which can be returned is zero since at that point we do not know
1843 * how big the buffer needs to be). On the second query,
1844 * it (ret_data_len) is set to number of snapshots so we can
1845 * know to set the maximum response size larger (see below).
1846 */
Steve French153322f2019-03-28 22:32:49 -05001847 if (get_user(ret_data_len, (unsigned int __user *)ioc_buf))
1848 return -EFAULT;
1849
1850 /*
1851 * Note that for snapshot queries that servers like Azure expect that
1852 * the first query be minimal size (and just used to get the number/size
1853 * of previous versions) so response size must be specified as EXACTLY
1854 * sizeof(struct snapshot_array) which is 16 when rounded up to multiple
1855 * of eight bytes.
1856 */
1857 if (ret_data_len == 0)
1858 max_response_size = MIN_SNAPSHOT_ARRAY_SIZE;
1859 else
1860 max_response_size = CIFSMaxBufSize;
1861
Steve French834170c2016-09-30 21:14:26 -05001862 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
1863 cfile->fid.volatile_fid,
1864 FSCTL_SRV_ENUMERATE_SNAPSHOTS,
Aurelien Aptel63a83b82018-01-24 13:46:11 +01001865 true /* is_fsctl */,
Steve French153322f2019-03-28 22:32:49 -05001866 NULL, 0 /* no input data */, max_response_size,
Steve French834170c2016-09-30 21:14:26 -05001867 (char **)&retbuf,
1868 &ret_data_len);
1869 cifs_dbg(FYI, "enum snaphots ioctl returned %d and ret buflen is %d\n",
1870 rc, ret_data_len);
1871 if (rc)
1872 return rc;
1873
1874 if (ret_data_len && (ioc_buf != NULL) && (retbuf != NULL)) {
1875 /* Fixup buffer */
1876 if (copy_from_user(&snapshot_in, ioc_buf,
1877 sizeof(struct smb_snapshot_array))) {
1878 rc = -EFAULT;
1879 kfree(retbuf);
1880 return rc;
1881 }
Steve French834170c2016-09-30 21:14:26 -05001882
Steve Frenche02789a2018-08-09 14:33:12 -05001883 /*
1884 * Check for min size, ie not large enough to fit even one GMT
1885 * token (snapshot). On the first ioctl some users may pass in
1886 * smaller size (or zero) to simply get the size of the array
1887 * so the user space caller can allocate sufficient memory
1888 * and retry the ioctl again with larger array size sufficient
1889 * to hold all of the snapshot GMT tokens on the second try.
1890 */
1891 if (snapshot_in.snapshot_array_size < GMT_TOKEN_SIZE)
1892 ret_data_len = sizeof(struct smb_snapshot_array);
1893
1894 /*
1895 * We return struct SRV_SNAPSHOT_ARRAY, followed by
1896 * the snapshot array (of 50 byte GMT tokens) each
1897 * representing an available previous version of the data
1898 */
1899 if (ret_data_len > (snapshot_in.snapshot_array_size +
1900 sizeof(struct smb_snapshot_array)))
1901 ret_data_len = snapshot_in.snapshot_array_size +
1902 sizeof(struct smb_snapshot_array);
Steve French834170c2016-09-30 21:14:26 -05001903
1904 if (copy_to_user(ioc_buf, retbuf, ret_data_len))
1905 rc = -EFAULT;
1906 }
1907
1908 kfree(retbuf);
1909 return rc;
1910}
1911
1912static int
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07001913smb2_query_dir_first(const unsigned int xid, struct cifs_tcon *tcon,
1914 const char *path, struct cifs_sb_info *cifs_sb,
1915 struct cifs_fid *fid, __u16 search_flags,
1916 struct cifs_search_info *srch_inf)
1917{
1918 __le16 *utf16_path;
1919 int rc;
Pavel Shilovsky2e44b282012-09-18 16:20:33 -07001920 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001921 struct cifs_open_parms oparms;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07001922
1923 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
1924 if (!utf16_path)
1925 return -ENOMEM;
1926
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001927 oparms.tcon = tcon;
1928 oparms.desired_access = FILE_READ_ATTRIBUTES | FILE_READ_DATA;
1929 oparms.disposition = FILE_OPEN;
Steve French5e196972018-08-27 17:04:13 -05001930 if (backup_cred(cifs_sb))
1931 oparms.create_options = CREATE_OPEN_BACKUP_INTENT;
1932 else
1933 oparms.create_options = 0;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001934 oparms.fid = fid;
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001935 oparms.reconnect = false;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001936
Ronnie Sahlberg9d874c32018-06-08 13:21:18 +10001937 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, NULL);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07001938 kfree(utf16_path);
1939 if (rc) {
Pavel Shilovskydcd878382017-06-06 16:58:58 -07001940 cifs_dbg(FYI, "open dir failed rc=%d\n", rc);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07001941 return rc;
1942 }
1943
1944 srch_inf->entries_in_buffer = 0;
Aurelien Aptel05957512018-05-17 16:35:07 +02001945 srch_inf->index_of_last_entry = 2;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07001946
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001947 rc = SMB2_query_directory(xid, tcon, fid->persistent_fid,
1948 fid->volatile_fid, 0, srch_inf);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07001949 if (rc) {
Pavel Shilovskydcd878382017-06-06 16:58:58 -07001950 cifs_dbg(FYI, "query directory failed rc=%d\n", rc);
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001951 SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07001952 }
1953 return rc;
1954}
1955
1956static int
1957smb2_query_dir_next(const unsigned int xid, struct cifs_tcon *tcon,
1958 struct cifs_fid *fid, __u16 search_flags,
1959 struct cifs_search_info *srch_inf)
1960{
1961 return SMB2_query_directory(xid, tcon, fid->persistent_fid,
1962 fid->volatile_fid, 0, srch_inf);
1963}
1964
1965static int
1966smb2_close_dir(const unsigned int xid, struct cifs_tcon *tcon,
1967 struct cifs_fid *fid)
1968{
1969 return SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
1970}
1971
Pavel Shilovsky2e44b282012-09-18 16:20:33 -07001972/*
Christoph Probsta205d502019-05-08 21:36:25 +02001973 * If we negotiate SMB2 protocol and get STATUS_PENDING - update
1974 * the number of credits and return true. Otherwise - return false.
1975 */
Pavel Shilovsky2e44b282012-09-18 16:20:33 -07001976static bool
Pavel Shilovsky66265f12019-01-23 17:11:16 -08001977smb2_is_status_pending(char *buf, struct TCP_Server_Info *server)
Pavel Shilovsky2e44b282012-09-18 16:20:33 -07001978{
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10001979 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
Pavel Shilovsky2e44b282012-09-18 16:20:33 -07001980
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001981 if (shdr->Status != STATUS_PENDING)
Pavel Shilovsky2e44b282012-09-18 16:20:33 -07001982 return false;
1983
Pavel Shilovsky66265f12019-01-23 17:11:16 -08001984 if (shdr->CreditRequest) {
Pavel Shilovsky2e44b282012-09-18 16:20:33 -07001985 spin_lock(&server->req_lock);
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001986 server->credits += le16_to_cpu(shdr->CreditRequest);
Pavel Shilovsky2e44b282012-09-18 16:20:33 -07001987 spin_unlock(&server->req_lock);
1988 wake_up(&server->request_q);
1989 }
1990
1991 return true;
1992}
1993
Pavel Shilovsky511c54a2017-07-08 14:32:00 -07001994static bool
1995smb2_is_session_expired(char *buf)
1996{
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10001997 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
Pavel Shilovsky511c54a2017-07-08 14:32:00 -07001998
Mark Symsd81243c2018-05-24 09:47:31 +01001999 if (shdr->Status != STATUS_NETWORK_SESSION_EXPIRED &&
2000 shdr->Status != STATUS_USER_SESSION_DELETED)
Pavel Shilovsky511c54a2017-07-08 14:32:00 -07002001 return false;
2002
Steve Frenche68a9322018-07-30 14:23:58 -05002003 trace_smb3_ses_expired(shdr->TreeId, shdr->SessionId,
2004 le16_to_cpu(shdr->Command),
2005 le64_to_cpu(shdr->MessageId));
Mark Symsd81243c2018-05-24 09:47:31 +01002006 cifs_dbg(FYI, "Session expired or deleted\n");
Steve Frenche68a9322018-07-30 14:23:58 -05002007
Pavel Shilovsky511c54a2017-07-08 14:32:00 -07002008 return true;
2009}
2010
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07002011static int
2012smb2_oplock_response(struct cifs_tcon *tcon, struct cifs_fid *fid,
2013 struct cifsInodeInfo *cinode)
2014{
Pavel Shilovsky0822f512012-09-19 06:22:45 -07002015 if (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_LEASING)
2016 return SMB2_lease_break(0, tcon, cinode->lease_key,
2017 smb2_get_lease_state(cinode));
2018
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07002019 return SMB2_oplock_break(0, tcon, fid->persistent_fid,
2020 fid->volatile_fid,
Pavel Shilovsky18cceb62013-09-05 13:01:06 +04002021 CIFS_CACHE_READ(cinode) ? 1 : 0);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07002022}
2023
Ronnie Sahlbergc5a5f382018-09-03 13:33:41 +10002024void
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10002025smb2_set_related(struct smb_rqst *rqst)
2026{
2027 struct smb2_sync_hdr *shdr;
2028
2029 shdr = (struct smb2_sync_hdr *)(rqst->rq_iov[0].iov_base);
2030 shdr->Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
2031}
2032
2033char smb2_padding[7] = {0, 0, 0, 0, 0, 0, 0};
2034
Ronnie Sahlbergc5a5f382018-09-03 13:33:41 +10002035void
Ronnie Sahlberge77fe732018-12-31 13:43:40 +10002036smb2_set_next_command(struct cifs_tcon *tcon, struct smb_rqst *rqst)
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10002037{
2038 struct smb2_sync_hdr *shdr;
Ronnie Sahlberge77fe732018-12-31 13:43:40 +10002039 struct cifs_ses *ses = tcon->ses;
2040 struct TCP_Server_Info *server = ses->server;
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10002041 unsigned long len = smb_rqst_len(server, rqst);
Ronnie Sahlberge77fe732018-12-31 13:43:40 +10002042 int i, num_padding;
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10002043
2044 /* SMB headers in a compound are 8 byte aligned. */
Ronnie Sahlberge77fe732018-12-31 13:43:40 +10002045
2046 /* No padding needed */
2047 if (!(len & 7))
2048 goto finished;
2049
2050 num_padding = 8 - (len & 7);
2051 if (!smb3_encryption_required(tcon)) {
2052 /*
2053 * If we do not have encryption then we can just add an extra
2054 * iov for the padding.
2055 */
2056 rqst->rq_iov[rqst->rq_nvec].iov_base = smb2_padding;
2057 rqst->rq_iov[rqst->rq_nvec].iov_len = num_padding;
2058 rqst->rq_nvec++;
2059 len += num_padding;
2060 } else {
2061 /*
2062 * We can not add a small padding iov for the encryption case
2063 * because the encryption framework can not handle the padding
2064 * iovs.
2065 * We have to flatten this into a single buffer and add
2066 * the padding to it.
2067 */
2068 for (i = 1; i < rqst->rq_nvec; i++) {
2069 memcpy(rqst->rq_iov[0].iov_base +
2070 rqst->rq_iov[0].iov_len,
2071 rqst->rq_iov[i].iov_base,
2072 rqst->rq_iov[i].iov_len);
2073 rqst->rq_iov[0].iov_len += rqst->rq_iov[i].iov_len;
Ronnie Sahlberg271b9c02018-12-18 17:49:05 -06002074 }
Ronnie Sahlberge77fe732018-12-31 13:43:40 +10002075 memset(rqst->rq_iov[0].iov_base + rqst->rq_iov[0].iov_len,
2076 0, num_padding);
2077 rqst->rq_iov[0].iov_len += num_padding;
2078 len += num_padding;
2079 rqst->rq_nvec = 1;
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10002080 }
2081
Ronnie Sahlberge77fe732018-12-31 13:43:40 +10002082 finished:
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10002083 shdr = (struct smb2_sync_hdr *)(rqst->rq_iov[0].iov_base);
2084 shdr->NextCommand = cpu_to_le32(len);
2085}
2086
Ronnie Sahlberg07d3b2e2018-12-20 22:03:04 -06002087/*
2088 * Passes the query info response back to the caller on success.
2089 * Caller need to free this with free_rsp_buf().
2090 */
Ronnie Sahlbergf9793b62018-11-27 09:52:04 +10002091int
Ronnie Sahlberg07d3b2e2018-12-20 22:03:04 -06002092smb2_query_info_compound(const unsigned int xid, struct cifs_tcon *tcon,
2093 __le16 *utf16_path, u32 desired_access,
2094 u32 class, u32 type, u32 output_len,
Ronnie Sahlbergf9793b62018-11-27 09:52:04 +10002095 struct kvec *rsp, int *buftype,
2096 struct cifs_sb_info *cifs_sb)
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07002097{
Ronnie Sahlberg07d3b2e2018-12-20 22:03:04 -06002098 struct cifs_ses *ses = tcon->ses;
Ronnie Sahlberg07d3b2e2018-12-20 22:03:04 -06002099 int flags = 0;
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10002100 struct smb_rqst rqst[3];
2101 int resp_buftype[3];
2102 struct kvec rsp_iov[3];
Ronnie Sahlberg4d8dfaf2018-08-21 11:49:21 +10002103 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10002104 struct kvec qi_iov[1];
2105 struct kvec close_iov[1];
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07002106 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04002107 struct cifs_open_parms oparms;
2108 struct cifs_fid fid;
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10002109 int rc;
2110
2111 if (smb3_encryption_required(tcon))
2112 flags |= CIFS_TRANSFORM_REQ;
2113
2114 memset(rqst, 0, sizeof(rqst));
Ronnie Sahlbergc5a5f382018-09-03 13:33:41 +10002115 resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10002116 memset(rsp_iov, 0, sizeof(rsp_iov));
2117
2118 memset(&open_iov, 0, sizeof(open_iov));
2119 rqst[0].rq_iov = open_iov;
Ronnie Sahlberg4d8dfaf2018-08-21 11:49:21 +10002120 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07002121
Pavel Shilovsky064f6042013-07-09 18:20:30 +04002122 oparms.tcon = tcon;
Ronnie Sahlberg07d3b2e2018-12-20 22:03:04 -06002123 oparms.desired_access = desired_access;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04002124 oparms.disposition = FILE_OPEN;
Ronnie Sahlbergf9793b62018-11-27 09:52:04 +10002125 if (cifs_sb && backup_cred(cifs_sb))
2126 oparms.create_options = CREATE_OPEN_BACKUP_INTENT;
2127 else
2128 oparms.create_options = 0;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04002129 oparms.fid = &fid;
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04002130 oparms.reconnect = false;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04002131
Ronnie Sahlberg07d3b2e2018-12-20 22:03:04 -06002132 rc = SMB2_open_init(tcon, &rqst[0], &oplock, &oparms, utf16_path);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07002133 if (rc)
Ronnie Sahlberg07d3b2e2018-12-20 22:03:04 -06002134 goto qic_exit;
Ronnie Sahlberge77fe732018-12-31 13:43:40 +10002135 smb2_set_next_command(tcon, &rqst[0]);
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10002136
2137 memset(&qi_iov, 0, sizeof(qi_iov));
2138 rqst[1].rq_iov = qi_iov;
2139 rqst[1].rq_nvec = 1;
2140
2141 rc = SMB2_query_info_init(tcon, &rqst[1], COMPOUND_FID, COMPOUND_FID,
Ronnie Sahlberg07d3b2e2018-12-20 22:03:04 -06002142 class, type, 0,
2143 output_len, 0,
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05002144 NULL);
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10002145 if (rc)
Ronnie Sahlberg07d3b2e2018-12-20 22:03:04 -06002146 goto qic_exit;
Ronnie Sahlberge77fe732018-12-31 13:43:40 +10002147 smb2_set_next_command(tcon, &rqst[1]);
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10002148 smb2_set_related(&rqst[1]);
2149
2150 memset(&close_iov, 0, sizeof(close_iov));
2151 rqst[2].rq_iov = close_iov;
2152 rqst[2].rq_nvec = 1;
2153
2154 rc = SMB2_close_init(tcon, &rqst[2], COMPOUND_FID, COMPOUND_FID);
2155 if (rc)
Ronnie Sahlberg07d3b2e2018-12-20 22:03:04 -06002156 goto qic_exit;
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10002157 smb2_set_related(&rqst[2]);
2158
2159 rc = compound_send_recv(xid, ses, flags, 3, rqst,
2160 resp_buftype, rsp_iov);
Ronnie Sahlbergf9793b62018-11-27 09:52:04 +10002161 if (rc) {
2162 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
Ronnie Sahlberg07d3b2e2018-12-20 22:03:04 -06002163 goto qic_exit;
Ronnie Sahlbergf9793b62018-11-27 09:52:04 +10002164 }
Ronnie Sahlberg07d3b2e2018-12-20 22:03:04 -06002165 *rsp = rsp_iov[1];
2166 *buftype = resp_buftype[1];
2167
2168 qic_exit:
2169 SMB2_open_free(&rqst[0]);
2170 SMB2_query_info_free(&rqst[1]);
2171 SMB2_close_free(&rqst[2]);
2172 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
2173 free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
2174 return rc;
2175}
2176
2177static int
2178smb2_queryfs(const unsigned int xid, struct cifs_tcon *tcon,
2179 struct kstatfs *buf)
2180{
2181 struct smb2_query_info_rsp *rsp;
2182 struct smb2_fs_full_size_info *info = NULL;
2183 __le16 utf16_path = 0; /* Null - open root of share */
2184 struct kvec rsp_iov = {NULL, 0};
2185 int buftype = CIFS_NO_BUFFER;
2186 int rc;
2187
2188
2189 rc = smb2_query_info_compound(xid, tcon, &utf16_path,
2190 FILE_READ_ATTRIBUTES,
2191 FS_FULL_SIZE_INFORMATION,
2192 SMB2_O_INFO_FILESYSTEM,
2193 sizeof(struct smb2_fs_full_size_info),
Ronnie Sahlbergf9793b62018-11-27 09:52:04 +10002194 &rsp_iov, &buftype, NULL);
Ronnie Sahlberg07d3b2e2018-12-20 22:03:04 -06002195 if (rc)
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10002196 goto qfs_exit;
2197
Ronnie Sahlberg07d3b2e2018-12-20 22:03:04 -06002198 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07002199 buf->f_type = SMB2_MAGIC_NUMBER;
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10002200 info = (struct smb2_fs_full_size_info *)(
2201 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
2202 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
2203 le32_to_cpu(rsp->OutputBufferLength),
Ronnie Sahlberg07d3b2e2018-12-20 22:03:04 -06002204 &rsp_iov,
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10002205 sizeof(struct smb2_fs_full_size_info));
2206 if (!rc)
2207 smb2_copy_fs_info_to_kstatfs(info, buf);
2208
2209qfs_exit:
Ronnie Sahlberg07d3b2e2018-12-20 22:03:04 -06002210 free_rsp_buf(buftype, rsp_iov.iov_base);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07002211 return rc;
2212}
2213
Steve French2d304212018-06-24 23:28:12 -05002214static int
2215smb311_queryfs(const unsigned int xid, struct cifs_tcon *tcon,
2216 struct kstatfs *buf)
2217{
2218 int rc;
2219 __le16 srch_path = 0; /* Null - open root of share */
2220 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2221 struct cifs_open_parms oparms;
2222 struct cifs_fid fid;
2223
2224 if (!tcon->posix_extensions)
2225 return smb2_queryfs(xid, tcon, buf);
2226
2227 oparms.tcon = tcon;
2228 oparms.desired_access = FILE_READ_ATTRIBUTES;
2229 oparms.disposition = FILE_OPEN;
2230 oparms.create_options = 0;
2231 oparms.fid = &fid;
2232 oparms.reconnect = false;
2233
2234 rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL, NULL);
2235 if (rc)
2236 return rc;
2237
2238 rc = SMB311_posix_qfs_info(xid, tcon, fid.persistent_fid,
2239 fid.volatile_fid, buf);
2240 buf->f_type = SMB2_MAGIC_NUMBER;
2241 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
2242 return rc;
2243}
Steve French2d304212018-06-24 23:28:12 -05002244
Pavel Shilovsky027e8ee2012-09-19 06:22:43 -07002245static bool
2246smb2_compare_fids(struct cifsFileInfo *ob1, struct cifsFileInfo *ob2)
2247{
2248 return ob1->fid.persistent_fid == ob2->fid.persistent_fid &&
2249 ob1->fid.volatile_fid == ob2->fid.volatile_fid;
2250}
2251
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07002252static int
2253smb2_mand_lock(const unsigned int xid, struct cifsFileInfo *cfile, __u64 offset,
2254 __u64 length, __u32 type, int lock, int unlock, bool wait)
2255{
2256 if (unlock && !lock)
2257 type = SMB2_LOCKFLAG_UNLOCK;
2258 return SMB2_lock(xid, tlink_tcon(cfile->tlink),
2259 cfile->fid.persistent_fid, cfile->fid.volatile_fid,
2260 current->tgid, length, offset, type, wait);
2261}
2262
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07002263static void
2264smb2_get_lease_key(struct inode *inode, struct cifs_fid *fid)
2265{
2266 memcpy(fid->lease_key, CIFS_I(inode)->lease_key, SMB2_LEASE_KEY_SIZE);
2267}
2268
2269static void
2270smb2_set_lease_key(struct inode *inode, struct cifs_fid *fid)
2271{
2272 memcpy(CIFS_I(inode)->lease_key, fid->lease_key, SMB2_LEASE_KEY_SIZE);
2273}
2274
2275static void
2276smb2_new_lease_key(struct cifs_fid *fid)
2277{
Steve Frenchfa70b872016-09-22 00:39:34 -05002278 generate_random_uuid(fid->lease_key);
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07002279}
2280
Aurelien Aptel9d496402017-02-13 16:16:49 +01002281static int
2282smb2_get_dfs_refer(const unsigned int xid, struct cifs_ses *ses,
2283 const char *search_name,
2284 struct dfs_info3_param **target_nodes,
2285 unsigned int *num_of_nodes,
2286 const struct nls_table *nls_codepage, int remap)
2287{
2288 int rc;
2289 __le16 *utf16_path = NULL;
2290 int utf16_path_len = 0;
2291 struct cifs_tcon *tcon;
2292 struct fsctl_get_dfs_referral_req *dfs_req = NULL;
2293 struct get_dfs_referral_rsp *dfs_rsp = NULL;
2294 u32 dfs_req_size = 0, dfs_rsp_size = 0;
2295
Christoph Probsta205d502019-05-08 21:36:25 +02002296 cifs_dbg(FYI, "%s: path: %s\n", __func__, search_name);
Aurelien Aptel9d496402017-02-13 16:16:49 +01002297
2298 /*
Aurelien Aptel63a83b82018-01-24 13:46:11 +01002299 * Try to use the IPC tcon, otherwise just use any
Aurelien Aptel9d496402017-02-13 16:16:49 +01002300 */
Aurelien Aptel63a83b82018-01-24 13:46:11 +01002301 tcon = ses->tcon_ipc;
2302 if (tcon == NULL) {
2303 spin_lock(&cifs_tcp_ses_lock);
2304 tcon = list_first_entry_or_null(&ses->tcon_list,
2305 struct cifs_tcon,
2306 tcon_list);
2307 if (tcon)
2308 tcon->tc_count++;
2309 spin_unlock(&cifs_tcp_ses_lock);
2310 }
Aurelien Aptel9d496402017-02-13 16:16:49 +01002311
Aurelien Aptel63a83b82018-01-24 13:46:11 +01002312 if (tcon == NULL) {
Aurelien Aptel9d496402017-02-13 16:16:49 +01002313 cifs_dbg(VFS, "session %p has no tcon available for a dfs referral request\n",
2314 ses);
2315 rc = -ENOTCONN;
2316 goto out;
2317 }
2318
2319 utf16_path = cifs_strndup_to_utf16(search_name, PATH_MAX,
2320 &utf16_path_len,
2321 nls_codepage, remap);
2322 if (!utf16_path) {
2323 rc = -ENOMEM;
2324 goto out;
2325 }
2326
2327 dfs_req_size = sizeof(*dfs_req) + utf16_path_len;
2328 dfs_req = kzalloc(dfs_req_size, GFP_KERNEL);
2329 if (!dfs_req) {
2330 rc = -ENOMEM;
2331 goto out;
2332 }
2333
2334 /* Highest DFS referral version understood */
2335 dfs_req->MaxReferralLevel = DFS_VERSION;
2336
2337 /* Path to resolve in an UTF-16 null-terminated string */
2338 memcpy(dfs_req->RequestFileName, utf16_path, utf16_path_len);
2339
2340 do {
Aurelien Aptel9d496402017-02-13 16:16:49 +01002341 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
2342 FSCTL_DFS_GET_REFERRALS,
Aurelien Aptel63a83b82018-01-24 13:46:11 +01002343 true /* is_fsctl */,
Steve French153322f2019-03-28 22:32:49 -05002344 (char *)dfs_req, dfs_req_size, CIFSMaxBufSize,
Aurelien Aptel9d496402017-02-13 16:16:49 +01002345 (char **)&dfs_rsp, &dfs_rsp_size);
Aurelien Aptel9d496402017-02-13 16:16:49 +01002346 } while (rc == -EAGAIN);
2347
2348 if (rc) {
Steve French2564f2f2018-03-21 23:16:36 -05002349 if ((rc != -ENOENT) && (rc != -EOPNOTSUPP))
Christoph Probsta205d502019-05-08 21:36:25 +02002350 cifs_dbg(VFS, "ioctl error in %s rc=%d\n", __func__, rc);
Aurelien Aptel9d496402017-02-13 16:16:49 +01002351 goto out;
2352 }
2353
2354 rc = parse_dfs_referrals(dfs_rsp, dfs_rsp_size,
2355 num_of_nodes, target_nodes,
2356 nls_codepage, remap, search_name,
2357 true /* is_unicode */);
2358 if (rc) {
Christoph Probsta205d502019-05-08 21:36:25 +02002359 cifs_dbg(VFS, "parse error in %s rc=%d\n", __func__, rc);
Aurelien Aptel9d496402017-02-13 16:16:49 +01002360 goto out;
2361 }
2362
2363 out:
Aurelien Aptel63a83b82018-01-24 13:46:11 +01002364 if (tcon && !tcon->ipc) {
2365 /* ipc tcons are not refcounted */
Aurelien Aptel9d496402017-02-13 16:16:49 +01002366 spin_lock(&cifs_tcp_ses_lock);
2367 tcon->tc_count--;
2368 spin_unlock(&cifs_tcp_ses_lock);
2369 }
2370 kfree(utf16_path);
2371 kfree(dfs_req);
2372 kfree(dfs_rsp);
2373 return rc;
2374}
Ronnie Sahlberg5de254d2019-06-27 14:57:02 +10002375
2376static int
2377parse_reparse_symlink(struct reparse_symlink_data_buffer *symlink_buf,
2378 u32 plen, char **target_path,
2379 struct cifs_sb_info *cifs_sb)
2380{
2381 unsigned int sub_len;
2382 unsigned int sub_offset;
2383
2384 /* We only handle Symbolic Link : MS-FSCC 2.1.2.4 */
2385 if (le32_to_cpu(symlink_buf->ReparseTag) != IO_REPARSE_TAG_SYMLINK) {
2386 cifs_dbg(VFS, "srv returned invalid symlink buffer\n");
2387 return -EIO;
2388 }
2389
2390 sub_offset = le16_to_cpu(symlink_buf->SubstituteNameOffset);
2391 sub_len = le16_to_cpu(symlink_buf->SubstituteNameLength);
2392 if (sub_offset + 20 > plen ||
2393 sub_offset + sub_len + 20 > plen) {
2394 cifs_dbg(VFS, "srv returned malformed symlink buffer\n");
2395 return -EIO;
2396 }
2397
2398 *target_path = cifs_strndup_from_utf16(
2399 symlink_buf->PathBuffer + sub_offset,
2400 sub_len, true, cifs_sb->local_nls);
2401 if (!(*target_path))
2402 return -ENOMEM;
2403
2404 convert_delimiter(*target_path, '/');
2405 cifs_dbg(FYI, "%s: target path: %s\n", __func__, *target_path);
2406
2407 return 0;
2408}
2409
Pavel Shilovsky78932422016-07-24 10:37:38 +03002410#define SMB2_SYMLINK_STRUCT_SIZE \
2411 (sizeof(struct smb2_err_rsp) - 1 + sizeof(struct smb2_symlink_err_rsp))
2412
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04002413static int
2414smb2_query_symlink(const unsigned int xid, struct cifs_tcon *tcon,
Ronnie Sahlbergebaf5462019-04-10 08:44:46 +10002415 struct cifs_sb_info *cifs_sb, const char *full_path,
2416 char **target_path, bool is_reparse_point)
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04002417{
2418 int rc;
Ronnie Sahlbergebaf5462019-04-10 08:44:46 +10002419 __le16 *utf16_path = NULL;
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04002420 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2421 struct cifs_open_parms oparms;
2422 struct cifs_fid fid;
Ronnie Sahlberg91cb74f2018-04-13 09:03:19 +10002423 struct kvec err_iov = {NULL, 0};
Ronnie Sahlberg9d874c32018-06-08 13:21:18 +10002424 struct smb2_err_rsp *err_buf = NULL;
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04002425 struct smb2_symlink_err_rsp *symlink;
Pavel Shilovsky78932422016-07-24 10:37:38 +03002426 unsigned int sub_len;
2427 unsigned int sub_offset;
2428 unsigned int print_len;
2429 unsigned int print_offset;
Ronnie Sahlbergebaf5462019-04-10 08:44:46 +10002430 int flags = 0;
2431 struct smb_rqst rqst[3];
2432 int resp_buftype[3];
2433 struct kvec rsp_iov[3];
2434 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
2435 struct kvec io_iov[SMB2_IOCTL_IOV_SIZE];
2436 struct kvec close_iov[1];
2437 struct smb2_create_rsp *create_rsp;
2438 struct smb2_ioctl_rsp *ioctl_rsp;
Ronnie Sahlberg5de254d2019-06-27 14:57:02 +10002439 struct reparse_data_buffer *reparse_buf;
Ronnie Sahlbergebaf5462019-04-10 08:44:46 +10002440 u32 plen;
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04002441
2442 cifs_dbg(FYI, "%s: path: %s\n", __func__, full_path);
2443
Ronnie Sahlberg5de254d2019-06-27 14:57:02 +10002444 *target_path = NULL;
2445
Ronnie Sahlbergebaf5462019-04-10 08:44:46 +10002446 if (smb3_encryption_required(tcon))
2447 flags |= CIFS_TRANSFORM_REQ;
2448
2449 memset(rqst, 0, sizeof(rqst));
2450 resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
2451 memset(rsp_iov, 0, sizeof(rsp_iov));
2452
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04002453 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
2454 if (!utf16_path)
2455 return -ENOMEM;
2456
Ronnie Sahlbergebaf5462019-04-10 08:44:46 +10002457 /* Open */
2458 memset(&open_iov, 0, sizeof(open_iov));
2459 rqst[0].rq_iov = open_iov;
2460 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
2461
2462 memset(&oparms, 0, sizeof(oparms));
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04002463 oparms.tcon = tcon;
2464 oparms.desired_access = FILE_READ_ATTRIBUTES;
2465 oparms.disposition = FILE_OPEN;
Ronnie Sahlbergebaf5462019-04-10 08:44:46 +10002466
Steve French5e196972018-08-27 17:04:13 -05002467 if (backup_cred(cifs_sb))
2468 oparms.create_options = CREATE_OPEN_BACKUP_INTENT;
2469 else
2470 oparms.create_options = 0;
Ronnie Sahlbergebaf5462019-04-10 08:44:46 +10002471 if (is_reparse_point)
2472 oparms.create_options = OPEN_REPARSE_POINT;
2473
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04002474 oparms.fid = &fid;
2475 oparms.reconnect = false;
2476
Ronnie Sahlbergebaf5462019-04-10 08:44:46 +10002477 rc = SMB2_open_init(tcon, &rqst[0], &oplock, &oparms, utf16_path);
2478 if (rc)
2479 goto querty_exit;
2480 smb2_set_next_command(tcon, &rqst[0]);
2481
2482
2483 /* IOCTL */
2484 memset(&io_iov, 0, sizeof(io_iov));
2485 rqst[1].rq_iov = io_iov;
2486 rqst[1].rq_nvec = SMB2_IOCTL_IOV_SIZE;
2487
2488 rc = SMB2_ioctl_init(tcon, &rqst[1], fid.persistent_fid,
2489 fid.volatile_fid, FSCTL_GET_REPARSE_POINT,
2490 true /* is_fctl */, NULL, 0, CIFSMaxBufSize);
2491 if (rc)
2492 goto querty_exit;
2493
2494 smb2_set_next_command(tcon, &rqst[1]);
2495 smb2_set_related(&rqst[1]);
2496
2497
2498 /* Close */
2499 memset(&close_iov, 0, sizeof(close_iov));
2500 rqst[2].rq_iov = close_iov;
2501 rqst[2].rq_nvec = 1;
2502
2503 rc = SMB2_close_init(tcon, &rqst[2], COMPOUND_FID, COMPOUND_FID);
2504 if (rc)
2505 goto querty_exit;
2506
2507 smb2_set_related(&rqst[2]);
2508
2509 rc = compound_send_recv(xid, tcon->ses, flags, 3, rqst,
2510 resp_buftype, rsp_iov);
2511
2512 create_rsp = rsp_iov[0].iov_base;
2513 if (create_rsp && create_rsp->sync_hdr.Status)
2514 err_iov = rsp_iov[0];
2515 ioctl_rsp = rsp_iov[1].iov_base;
2516
2517 /*
2518 * Open was successful and we got an ioctl response.
2519 */
2520 if ((rc == 0) && (is_reparse_point)) {
2521 /* See MS-FSCC 2.3.23 */
2522
Ronnie Sahlberg5de254d2019-06-27 14:57:02 +10002523 reparse_buf = (struct reparse_data_buffer *)
2524 ((char *)ioctl_rsp +
2525 le32_to_cpu(ioctl_rsp->OutputOffset));
Ronnie Sahlbergebaf5462019-04-10 08:44:46 +10002526 plen = le32_to_cpu(ioctl_rsp->OutputCount);
2527
2528 if (plen + le32_to_cpu(ioctl_rsp->OutputOffset) >
2529 rsp_iov[1].iov_len) {
Ronnie Sahlberg5de254d2019-06-27 14:57:02 +10002530 cifs_dbg(VFS, "srv returned invalid ioctl len: %d\n",
2531 plen);
Ronnie Sahlbergebaf5462019-04-10 08:44:46 +10002532 rc = -EIO;
2533 goto querty_exit;
2534 }
2535
Ronnie Sahlberg5de254d2019-06-27 14:57:02 +10002536 if (plen < 8) {
2537 cifs_dbg(VFS, "reparse buffer is too small. Must be "
2538 "at least 8 bytes but was %d\n", plen);
2539 rc = -EIO;
2540 goto querty_exit;
2541 }
2542
2543 if (plen < le16_to_cpu(reparse_buf->ReparseDataLength) + 8) {
2544 cifs_dbg(VFS, "srv returned invalid reparse buf "
2545 "length: %d\n", plen);
2546 rc = -EIO;
2547 goto querty_exit;
2548 }
2549
2550 rc = parse_reparse_symlink(
2551 (struct reparse_symlink_data_buffer *)reparse_buf,
2552 plen, target_path, cifs_sb);
Ronnie Sahlbergebaf5462019-04-10 08:44:46 +10002553 goto querty_exit;
2554 }
2555
Gustavo A. R. Silva0d568cd2018-04-13 10:13:29 -05002556 if (!rc || !err_iov.iov_base) {
Ronnie Sahlberg9d874c32018-06-08 13:21:18 +10002557 rc = -ENOENT;
Ronnie Sahlbergebaf5462019-04-10 08:44:46 +10002558 goto querty_exit;
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04002559 }
Pavel Shilovsky78932422016-07-24 10:37:38 +03002560
Ronnie Sahlberg91cb74f2018-04-13 09:03:19 +10002561 err_buf = err_iov.iov_base;
Pavel Shilovsky78932422016-07-24 10:37:38 +03002562 if (le32_to_cpu(err_buf->ByteCount) < sizeof(struct smb2_symlink_err_rsp) ||
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10002563 err_iov.iov_len < SMB2_SYMLINK_STRUCT_SIZE) {
Ronnie Sahlberg9d874c32018-06-08 13:21:18 +10002564 rc = -ENOENT;
2565 goto querty_exit;
Pavel Shilovsky78932422016-07-24 10:37:38 +03002566 }
2567
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04002568 /* open must fail on symlink - reset rc */
2569 rc = 0;
2570 symlink = (struct smb2_symlink_err_rsp *)err_buf->ErrorData;
2571 sub_len = le16_to_cpu(symlink->SubstituteNameLength);
2572 sub_offset = le16_to_cpu(symlink->SubstituteNameOffset);
Pavel Shilovsky78932422016-07-24 10:37:38 +03002573 print_len = le16_to_cpu(symlink->PrintNameLength);
2574 print_offset = le16_to_cpu(symlink->PrintNameOffset);
2575
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10002576 if (err_iov.iov_len < SMB2_SYMLINK_STRUCT_SIZE + sub_offset + sub_len) {
Ronnie Sahlberg9d874c32018-06-08 13:21:18 +10002577 rc = -ENOENT;
2578 goto querty_exit;
Pavel Shilovsky78932422016-07-24 10:37:38 +03002579 }
2580
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10002581 if (err_iov.iov_len <
2582 SMB2_SYMLINK_STRUCT_SIZE + print_offset + print_len) {
Ronnie Sahlberg9d874c32018-06-08 13:21:18 +10002583 rc = -ENOENT;
2584 goto querty_exit;
Pavel Shilovsky78932422016-07-24 10:37:38 +03002585 }
2586
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04002587 *target_path = cifs_strndup_from_utf16(
2588 (char *)symlink->PathBuffer + sub_offset,
2589 sub_len, true, cifs_sb->local_nls);
2590 if (!(*target_path)) {
Ronnie Sahlberg9d874c32018-06-08 13:21:18 +10002591 rc = -ENOMEM;
2592 goto querty_exit;
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04002593 }
2594 convert_delimiter(*target_path, '/');
2595 cifs_dbg(FYI, "%s: target path: %s\n", __func__, *target_path);
Ronnie Sahlberg9d874c32018-06-08 13:21:18 +10002596
2597 querty_exit:
Ronnie Sahlbergebaf5462019-04-10 08:44:46 +10002598 cifs_dbg(FYI, "query symlink rc %d\n", rc);
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04002599 kfree(utf16_path);
Ronnie Sahlbergebaf5462019-04-10 08:44:46 +10002600 SMB2_open_free(&rqst[0]);
2601 SMB2_ioctl_free(&rqst[1]);
2602 SMB2_close_free(&rqst[2]);
2603 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
2604 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
2605 free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04002606 return rc;
2607}
2608
Arnd Bergmann84908422017-06-27 17:06:13 +02002609#ifdef CONFIG_CIFS_ACL
Shirish Pargaonkar2f1afe22017-06-22 22:52:05 -05002610static struct cifs_ntsd *
2611get_smb2_acl_by_fid(struct cifs_sb_info *cifs_sb,
2612 const struct cifs_fid *cifsfid, u32 *pacllen)
2613{
2614 struct cifs_ntsd *pntsd = NULL;
2615 unsigned int xid;
2616 int rc = -EOPNOTSUPP;
2617 struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
2618
2619 if (IS_ERR(tlink))
2620 return ERR_CAST(tlink);
2621
2622 xid = get_xid();
2623 cifs_dbg(FYI, "trying to get acl\n");
2624
2625 rc = SMB2_query_acl(xid, tlink_tcon(tlink), cifsfid->persistent_fid,
2626 cifsfid->volatile_fid, (void **)&pntsd, pacllen);
2627 free_xid(xid);
2628
2629 cifs_put_tlink(tlink);
2630
2631 cifs_dbg(FYI, "%s: rc = %d ACL len %d\n", __func__, rc, *pacllen);
2632 if (rc)
2633 return ERR_PTR(rc);
2634 return pntsd;
2635
2636}
2637
2638static struct cifs_ntsd *
2639get_smb2_acl_by_path(struct cifs_sb_info *cifs_sb,
2640 const char *path, u32 *pacllen)
2641{
2642 struct cifs_ntsd *pntsd = NULL;
2643 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2644 unsigned int xid;
2645 int rc;
2646 struct cifs_tcon *tcon;
2647 struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
2648 struct cifs_fid fid;
2649 struct cifs_open_parms oparms;
2650 __le16 *utf16_path;
2651
2652 cifs_dbg(FYI, "get smb3 acl for path %s\n", path);
2653 if (IS_ERR(tlink))
2654 return ERR_CAST(tlink);
2655
2656 tcon = tlink_tcon(tlink);
2657 xid = get_xid();
2658
2659 if (backup_cred(cifs_sb))
Colin Ian King709340a2017-07-05 13:47:34 +01002660 oparms.create_options = CREATE_OPEN_BACKUP_INTENT;
Shirish Pargaonkar2f1afe22017-06-22 22:52:05 -05002661 else
2662 oparms.create_options = 0;
2663
2664 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
Steve Frenchcfe89092018-05-19 02:04:55 -05002665 if (!utf16_path) {
2666 rc = -ENOMEM;
2667 free_xid(xid);
2668 return ERR_PTR(rc);
2669 }
Shirish Pargaonkar2f1afe22017-06-22 22:52:05 -05002670
2671 oparms.tcon = tcon;
2672 oparms.desired_access = READ_CONTROL;
2673 oparms.disposition = FILE_OPEN;
2674 oparms.fid = &fid;
2675 oparms.reconnect = false;
2676
Ronnie Sahlberg9d874c32018-06-08 13:21:18 +10002677 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, NULL);
Shirish Pargaonkar2f1afe22017-06-22 22:52:05 -05002678 kfree(utf16_path);
2679 if (!rc) {
2680 rc = SMB2_query_acl(xid, tlink_tcon(tlink), fid.persistent_fid,
2681 fid.volatile_fid, (void **)&pntsd, pacllen);
2682 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
2683 }
2684
2685 cifs_put_tlink(tlink);
2686 free_xid(xid);
2687
2688 cifs_dbg(FYI, "%s: rc = %d ACL len %d\n", __func__, rc, *pacllen);
2689 if (rc)
2690 return ERR_PTR(rc);
2691 return pntsd;
2692}
2693
Shirish Pargaonkar366ed842017-06-28 22:37:32 -05002694#ifdef CONFIG_CIFS_ACL
2695static int
2696set_smb2_acl(struct cifs_ntsd *pnntsd, __u32 acllen,
2697 struct inode *inode, const char *path, int aclflag)
2698{
2699 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2700 unsigned int xid;
2701 int rc, access_flags = 0;
2702 struct cifs_tcon *tcon;
2703 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
2704 struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
2705 struct cifs_fid fid;
2706 struct cifs_open_parms oparms;
2707 __le16 *utf16_path;
2708
2709 cifs_dbg(FYI, "set smb3 acl for path %s\n", path);
2710 if (IS_ERR(tlink))
2711 return PTR_ERR(tlink);
2712
2713 tcon = tlink_tcon(tlink);
2714 xid = get_xid();
2715
2716 if (backup_cred(cifs_sb))
2717 oparms.create_options = CREATE_OPEN_BACKUP_INTENT;
2718 else
2719 oparms.create_options = 0;
2720
2721 if (aclflag == CIFS_ACL_OWNER || aclflag == CIFS_ACL_GROUP)
2722 access_flags = WRITE_OWNER;
2723 else
2724 access_flags = WRITE_DAC;
2725
2726 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
Steve Frenchcfe89092018-05-19 02:04:55 -05002727 if (!utf16_path) {
2728 rc = -ENOMEM;
2729 free_xid(xid);
2730 return rc;
2731 }
Shirish Pargaonkar366ed842017-06-28 22:37:32 -05002732
2733 oparms.tcon = tcon;
2734 oparms.desired_access = access_flags;
2735 oparms.disposition = FILE_OPEN;
2736 oparms.path = path;
2737 oparms.fid = &fid;
2738 oparms.reconnect = false;
2739
Ronnie Sahlberg9d874c32018-06-08 13:21:18 +10002740 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, NULL);
Shirish Pargaonkar366ed842017-06-28 22:37:32 -05002741 kfree(utf16_path);
2742 if (!rc) {
2743 rc = SMB2_set_acl(xid, tlink_tcon(tlink), fid.persistent_fid,
2744 fid.volatile_fid, pnntsd, acllen, aclflag);
2745 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
2746 }
2747
2748 cifs_put_tlink(tlink);
2749 free_xid(xid);
2750 return rc;
2751}
2752#endif /* CIFS_ACL */
2753
Shirish Pargaonkar2f1afe22017-06-22 22:52:05 -05002754/* Retrieve an ACL from the server */
2755static struct cifs_ntsd *
2756get_smb2_acl(struct cifs_sb_info *cifs_sb,
2757 struct inode *inode, const char *path,
2758 u32 *pacllen)
2759{
2760 struct cifs_ntsd *pntsd = NULL;
2761 struct cifsFileInfo *open_file = NULL;
2762
2763 if (inode)
2764 open_file = find_readable_file(CIFS_I(inode), true);
2765 if (!open_file)
2766 return get_smb2_acl_by_path(cifs_sb, path, pacllen);
2767
2768 pntsd = get_smb2_acl_by_fid(cifs_sb, &open_file->fid, pacllen);
2769 cifsFileInfo_put(open_file);
2770 return pntsd;
2771}
Arnd Bergmann84908422017-06-27 17:06:13 +02002772#endif
Shirish Pargaonkar2f1afe22017-06-22 22:52:05 -05002773
Steve French30175622014-08-17 18:16:40 -05002774static long smb3_zero_range(struct file *file, struct cifs_tcon *tcon,
2775 loff_t offset, loff_t len, bool keep_size)
2776{
Ronnie Sahlberg72c419d2019-03-13 14:37:49 +10002777 struct cifs_ses *ses = tcon->ses;
Steve French30175622014-08-17 18:16:40 -05002778 struct inode *inode;
2779 struct cifsInodeInfo *cifsi;
2780 struct cifsFileInfo *cfile = file->private_data;
2781 struct file_zero_data_information fsctl_buf;
2782 long rc;
2783 unsigned int xid;
Ronnie Sahlberg72c419d2019-03-13 14:37:49 +10002784 __le64 eof;
Steve French30175622014-08-17 18:16:40 -05002785
2786 xid = get_xid();
2787
David Howells2b0143b2015-03-17 22:25:59 +00002788 inode = d_inode(cfile->dentry);
Steve French30175622014-08-17 18:16:40 -05002789 cifsi = CIFS_I(inode);
2790
Christoph Probsta205d502019-05-08 21:36:25 +02002791 trace_smb3_zero_enter(xid, cfile->fid.persistent_fid, tcon->tid,
Steve French779ede02019-03-13 01:41:49 -05002792 ses->Suid, offset, len);
2793
2794
Steve French30175622014-08-17 18:16:40 -05002795 /* if file not oplocked can't be sure whether asking to extend size */
2796 if (!CIFS_CACHE_READ(cifsi))
Steve Frenchcfe89092018-05-19 02:04:55 -05002797 if (keep_size == false) {
2798 rc = -EOPNOTSUPP;
Steve French779ede02019-03-13 01:41:49 -05002799 trace_smb3_zero_err(xid, cfile->fid.persistent_fid,
2800 tcon->tid, ses->Suid, offset, len, rc);
Steve Frenchcfe89092018-05-19 02:04:55 -05002801 free_xid(xid);
2802 return rc;
2803 }
Steve French30175622014-08-17 18:16:40 -05002804
Steve Frenchd1c35af2019-05-09 00:09:37 -05002805 cifs_dbg(FYI, "Offset %lld len %lld\n", offset, len);
Steve French30175622014-08-17 18:16:40 -05002806
2807 fsctl_buf.FileOffset = cpu_to_le64(offset);
2808 fsctl_buf.BeyondFinalZero = cpu_to_le64(offset + len);
2809
Ronnie Sahlbergc4250142019-05-02 15:52:57 +10002810 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
2811 cfile->fid.volatile_fid, FSCTL_SET_ZERO_DATA, true,
2812 (char *)&fsctl_buf,
2813 sizeof(struct file_zero_data_information),
2814 0, NULL, NULL);
Ronnie Sahlberg72c419d2019-03-13 14:37:49 +10002815 if (rc)
2816 goto zero_range_exit;
2817
2818 /*
2819 * do we also need to change the size of the file?
2820 */
2821 if (keep_size == false && i_size_read(inode) < offset + len) {
Ronnie Sahlberg72c419d2019-03-13 14:37:49 +10002822 eof = cpu_to_le64(offset + len);
Ronnie Sahlbergc4250142019-05-02 15:52:57 +10002823 rc = SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
2824 cfile->fid.volatile_fid, cfile->pid, &eof);
Ronnie Sahlberg72c419d2019-03-13 14:37:49 +10002825 }
2826
Ronnie Sahlberg72c419d2019-03-13 14:37:49 +10002827 zero_range_exit:
Steve French30175622014-08-17 18:16:40 -05002828 free_xid(xid);
Steve French779ede02019-03-13 01:41:49 -05002829 if (rc)
2830 trace_smb3_zero_err(xid, cfile->fid.persistent_fid, tcon->tid,
2831 ses->Suid, offset, len, rc);
2832 else
2833 trace_smb3_zero_done(xid, cfile->fid.persistent_fid, tcon->tid,
2834 ses->Suid, offset, len);
Steve French30175622014-08-17 18:16:40 -05002835 return rc;
2836}
2837
Steve French31742c52014-08-17 08:38:47 -05002838static long smb3_punch_hole(struct file *file, struct cifs_tcon *tcon,
2839 loff_t offset, loff_t len)
2840{
2841 struct inode *inode;
2842 struct cifsInodeInfo *cifsi;
2843 struct cifsFileInfo *cfile = file->private_data;
2844 struct file_zero_data_information fsctl_buf;
2845 long rc;
2846 unsigned int xid;
2847 __u8 set_sparse = 1;
2848
2849 xid = get_xid();
2850
David Howells2b0143b2015-03-17 22:25:59 +00002851 inode = d_inode(cfile->dentry);
Steve French31742c52014-08-17 08:38:47 -05002852 cifsi = CIFS_I(inode);
2853
2854 /* Need to make file sparse, if not already, before freeing range. */
2855 /* Consider adding equivalent for compressed since it could also work */
Steve Frenchcfe89092018-05-19 02:04:55 -05002856 if (!smb2_set_sparse(xid, tcon, cfile, inode, set_sparse)) {
2857 rc = -EOPNOTSUPP;
2858 free_xid(xid);
2859 return rc;
2860 }
Steve French31742c52014-08-17 08:38:47 -05002861
Christoph Probsta205d502019-05-08 21:36:25 +02002862 cifs_dbg(FYI, "Offset %lld len %lld\n", offset, len);
Steve French31742c52014-08-17 08:38:47 -05002863
2864 fsctl_buf.FileOffset = cpu_to_le64(offset);
2865 fsctl_buf.BeyondFinalZero = cpu_to_le64(offset + len);
2866
2867 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
2868 cfile->fid.volatile_fid, FSCTL_SET_ZERO_DATA,
Aurelien Aptel63a83b82018-01-24 13:46:11 +01002869 true /* is_fctl */, (char *)&fsctl_buf,
Steve French153322f2019-03-28 22:32:49 -05002870 sizeof(struct file_zero_data_information),
2871 CIFSMaxBufSize, NULL, NULL);
Steve French31742c52014-08-17 08:38:47 -05002872 free_xid(xid);
2873 return rc;
2874}
2875
Steve French9ccf3212014-10-18 17:01:15 -05002876static long smb3_simple_falloc(struct file *file, struct cifs_tcon *tcon,
2877 loff_t off, loff_t len, bool keep_size)
2878{
2879 struct inode *inode;
2880 struct cifsInodeInfo *cifsi;
2881 struct cifsFileInfo *cfile = file->private_data;
2882 long rc = -EOPNOTSUPP;
2883 unsigned int xid;
Ronnie Sahlbergf1699472019-03-15 00:08:48 +10002884 __le64 eof;
Steve French9ccf3212014-10-18 17:01:15 -05002885
2886 xid = get_xid();
2887
David Howells2b0143b2015-03-17 22:25:59 +00002888 inode = d_inode(cfile->dentry);
Steve French9ccf3212014-10-18 17:01:15 -05002889 cifsi = CIFS_I(inode);
2890
Steve French779ede02019-03-13 01:41:49 -05002891 trace_smb3_falloc_enter(xid, cfile->fid.persistent_fid, tcon->tid,
2892 tcon->ses->Suid, off, len);
Steve French9ccf3212014-10-18 17:01:15 -05002893 /* if file not oplocked can't be sure whether asking to extend size */
2894 if (!CIFS_CACHE_READ(cifsi))
Steve Frenchcfe89092018-05-19 02:04:55 -05002895 if (keep_size == false) {
Steve French779ede02019-03-13 01:41:49 -05002896 trace_smb3_falloc_err(xid, cfile->fid.persistent_fid,
2897 tcon->tid, tcon->ses->Suid, off, len, rc);
Steve Frenchcfe89092018-05-19 02:04:55 -05002898 free_xid(xid);
2899 return rc;
2900 }
Steve French9ccf3212014-10-18 17:01:15 -05002901
2902 /*
2903 * Files are non-sparse by default so falloc may be a no-op
2904 * Must check if file sparse. If not sparse, and not extending
2905 * then no need to do anything since file already allocated
2906 */
2907 if ((cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) == 0) {
2908 if (keep_size == true)
Steve Frenchcfe89092018-05-19 02:04:55 -05002909 rc = 0;
Steve French9ccf3212014-10-18 17:01:15 -05002910 /* check if extending file */
2911 else if (i_size_read(inode) >= off + len)
2912 /* not extending file and already not sparse */
Steve Frenchcfe89092018-05-19 02:04:55 -05002913 rc = 0;
Steve French9ccf3212014-10-18 17:01:15 -05002914 /* BB: in future add else clause to extend file */
2915 else
Steve Frenchcfe89092018-05-19 02:04:55 -05002916 rc = -EOPNOTSUPP;
Steve French779ede02019-03-13 01:41:49 -05002917 if (rc)
2918 trace_smb3_falloc_err(xid, cfile->fid.persistent_fid,
2919 tcon->tid, tcon->ses->Suid, off, len, rc);
2920 else
2921 trace_smb3_falloc_done(xid, cfile->fid.persistent_fid,
2922 tcon->tid, tcon->ses->Suid, off, len);
Steve Frenchcfe89092018-05-19 02:04:55 -05002923 free_xid(xid);
2924 return rc;
Steve French9ccf3212014-10-18 17:01:15 -05002925 }
2926
2927 if ((keep_size == true) || (i_size_read(inode) >= off + len)) {
2928 /*
2929 * Check if falloc starts within first few pages of file
2930 * and ends within a few pages of the end of file to
2931 * ensure that most of file is being forced to be
2932 * fallocated now. If so then setting whole file sparse
2933 * ie potentially making a few extra pages at the beginning
2934 * or end of the file non-sparse via set_sparse is harmless.
2935 */
Steve Frenchcfe89092018-05-19 02:04:55 -05002936 if ((off > 8192) || (off + len + 8192 < i_size_read(inode))) {
2937 rc = -EOPNOTSUPP;
Steve French779ede02019-03-13 01:41:49 -05002938 trace_smb3_falloc_err(xid, cfile->fid.persistent_fid,
2939 tcon->tid, tcon->ses->Suid, off, len, rc);
Steve Frenchcfe89092018-05-19 02:04:55 -05002940 free_xid(xid);
2941 return rc;
2942 }
Steve French9ccf3212014-10-18 17:01:15 -05002943
Ronnie Sahlbergf1699472019-03-15 00:08:48 +10002944 smb2_set_sparse(xid, tcon, cfile, inode, false);
2945 rc = 0;
2946 } else {
2947 smb2_set_sparse(xid, tcon, cfile, inode, false);
2948 rc = 0;
2949 if (i_size_read(inode) < off + len) {
2950 eof = cpu_to_le64(off + len);
2951 rc = SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
2952 cfile->fid.volatile_fid, cfile->pid,
2953 &eof);
2954 }
Steve French9ccf3212014-10-18 17:01:15 -05002955 }
Steve French9ccf3212014-10-18 17:01:15 -05002956
Steve French779ede02019-03-13 01:41:49 -05002957 if (rc)
2958 trace_smb3_falloc_err(xid, cfile->fid.persistent_fid, tcon->tid,
2959 tcon->ses->Suid, off, len, rc);
2960 else
2961 trace_smb3_falloc_done(xid, cfile->fid.persistent_fid, tcon->tid,
2962 tcon->ses->Suid, off, len);
Steve French9ccf3212014-10-18 17:01:15 -05002963
2964 free_xid(xid);
2965 return rc;
2966}
2967
Ronnie Sahlbergdece44e2019-05-15 07:17:02 +10002968static loff_t smb3_llseek(struct file *file, struct cifs_tcon *tcon, loff_t offset, int whence)
2969{
2970 struct cifsFileInfo *wrcfile, *cfile = file->private_data;
2971 struct cifsInodeInfo *cifsi;
2972 struct inode *inode;
2973 int rc = 0;
2974 struct file_allocated_range_buffer in_data, *out_data = NULL;
2975 u32 out_data_len;
2976 unsigned int xid;
2977
2978 if (whence != SEEK_HOLE && whence != SEEK_DATA)
2979 return generic_file_llseek(file, offset, whence);
2980
2981 inode = d_inode(cfile->dentry);
2982 cifsi = CIFS_I(inode);
2983
2984 if (offset < 0 || offset >= i_size_read(inode))
2985 return -ENXIO;
2986
2987 xid = get_xid();
2988 /*
2989 * We need to be sure that all dirty pages are written as they
2990 * might fill holes on the server.
2991 * Note that we also MUST flush any written pages since at least
2992 * some servers (Windows2016) will not reflect recent writes in
2993 * QUERY_ALLOCATED_RANGES until SMB2_flush is called.
2994 */
2995 wrcfile = find_writable_file(cifsi, false);
2996 if (wrcfile) {
2997 filemap_write_and_wait(inode->i_mapping);
2998 smb2_flush_file(xid, tcon, &wrcfile->fid);
2999 cifsFileInfo_put(wrcfile);
3000 }
3001
3002 if (!(cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE)) {
3003 if (whence == SEEK_HOLE)
3004 offset = i_size_read(inode);
3005 goto lseek_exit;
3006 }
3007
3008 in_data.file_offset = cpu_to_le64(offset);
3009 in_data.length = cpu_to_le64(i_size_read(inode));
3010
3011 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
3012 cfile->fid.volatile_fid,
3013 FSCTL_QUERY_ALLOCATED_RANGES, true,
3014 (char *)&in_data, sizeof(in_data),
3015 sizeof(struct file_allocated_range_buffer),
3016 (char **)&out_data, &out_data_len);
3017 if (rc == -E2BIG)
3018 rc = 0;
3019 if (rc)
3020 goto lseek_exit;
3021
3022 if (whence == SEEK_HOLE && out_data_len == 0)
3023 goto lseek_exit;
3024
3025 if (whence == SEEK_DATA && out_data_len == 0) {
3026 rc = -ENXIO;
3027 goto lseek_exit;
3028 }
3029
3030 if (out_data_len < sizeof(struct file_allocated_range_buffer)) {
3031 rc = -EINVAL;
3032 goto lseek_exit;
3033 }
3034 if (whence == SEEK_DATA) {
3035 offset = le64_to_cpu(out_data->file_offset);
3036 goto lseek_exit;
3037 }
3038 if (offset < le64_to_cpu(out_data->file_offset))
3039 goto lseek_exit;
3040
3041 offset = le64_to_cpu(out_data->file_offset) + le64_to_cpu(out_data->length);
3042
3043 lseek_exit:
3044 free_xid(xid);
3045 kfree(out_data);
3046 if (!rc)
3047 return vfs_setpos(file, offset, inode->i_sb->s_maxbytes);
3048 else
3049 return rc;
3050}
3051
Ronnie Sahlberg2f3ebab2019-04-25 16:45:29 +10003052static int smb3_fiemap(struct cifs_tcon *tcon,
3053 struct cifsFileInfo *cfile,
3054 struct fiemap_extent_info *fei, u64 start, u64 len)
3055{
3056 unsigned int xid;
3057 struct file_allocated_range_buffer in_data, *out_data;
3058 u32 out_data_len;
3059 int i, num, rc, flags, last_blob;
3060 u64 next;
3061
3062 if (fiemap_check_flags(fei, FIEMAP_FLAG_SYNC))
3063 return -EBADR;
3064
3065 xid = get_xid();
3066 again:
3067 in_data.file_offset = cpu_to_le64(start);
3068 in_data.length = cpu_to_le64(len);
3069
3070 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
3071 cfile->fid.volatile_fid,
3072 FSCTL_QUERY_ALLOCATED_RANGES, true,
3073 (char *)&in_data, sizeof(in_data),
3074 1024 * sizeof(struct file_allocated_range_buffer),
3075 (char **)&out_data, &out_data_len);
3076 if (rc == -E2BIG) {
3077 last_blob = 0;
3078 rc = 0;
3079 } else
3080 last_blob = 1;
3081 if (rc)
3082 goto out;
3083
3084 if (out_data_len < sizeof(struct file_allocated_range_buffer)) {
3085 rc = -EINVAL;
3086 goto out;
3087 }
3088 if (out_data_len % sizeof(struct file_allocated_range_buffer)) {
3089 rc = -EINVAL;
3090 goto out;
3091 }
3092
3093 num = out_data_len / sizeof(struct file_allocated_range_buffer);
3094 for (i = 0; i < num; i++) {
3095 flags = 0;
3096 if (i == num - 1 && last_blob)
3097 flags |= FIEMAP_EXTENT_LAST;
3098
3099 rc = fiemap_fill_next_extent(fei,
3100 le64_to_cpu(out_data[i].file_offset),
3101 le64_to_cpu(out_data[i].file_offset),
3102 le64_to_cpu(out_data[i].length),
3103 flags);
3104 if (rc < 0)
3105 goto out;
3106 if (rc == 1) {
3107 rc = 0;
3108 goto out;
3109 }
3110 }
3111
3112 if (!last_blob) {
3113 next = le64_to_cpu(out_data[num - 1].file_offset) +
3114 le64_to_cpu(out_data[num - 1].length);
3115 len = len - (next - start);
3116 start = next;
3117 goto again;
3118 }
3119
3120 out:
3121 free_xid(xid);
3122 kfree(out_data);
3123 return rc;
3124}
Steve French9ccf3212014-10-18 17:01:15 -05003125
Steve French31742c52014-08-17 08:38:47 -05003126static long smb3_fallocate(struct file *file, struct cifs_tcon *tcon, int mode,
3127 loff_t off, loff_t len)
3128{
3129 /* KEEP_SIZE already checked for by do_fallocate */
3130 if (mode & FALLOC_FL_PUNCH_HOLE)
3131 return smb3_punch_hole(file, tcon, off, len);
Steve French30175622014-08-17 18:16:40 -05003132 else if (mode & FALLOC_FL_ZERO_RANGE) {
3133 if (mode & FALLOC_FL_KEEP_SIZE)
3134 return smb3_zero_range(file, tcon, off, len, true);
3135 return smb3_zero_range(file, tcon, off, len, false);
Steve French9ccf3212014-10-18 17:01:15 -05003136 } else if (mode == FALLOC_FL_KEEP_SIZE)
3137 return smb3_simple_falloc(file, tcon, off, len, true);
3138 else if (mode == 0)
3139 return smb3_simple_falloc(file, tcon, off, len, false);
Steve French31742c52014-08-17 08:38:47 -05003140
3141 return -EOPNOTSUPP;
3142}
3143
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04003144static void
Sachin Prabhuc11f1df2014-03-11 16:11:47 +00003145smb2_downgrade_oplock(struct TCP_Server_Info *server,
3146 struct cifsInodeInfo *cinode, bool set_level2)
3147{
3148 if (set_level2)
3149 server->ops->set_oplock_level(cinode, SMB2_OPLOCK_LEVEL_II,
3150 0, NULL);
3151 else
3152 server->ops->set_oplock_level(cinode, 0, 0, NULL);
3153}
3154
3155static void
Pavel Shilovsky7b9b9ed2019-02-13 15:43:08 -08003156smb21_downgrade_oplock(struct TCP_Server_Info *server,
3157 struct cifsInodeInfo *cinode, bool set_level2)
3158{
3159 server->ops->set_oplock_level(cinode,
3160 set_level2 ? SMB2_LEASE_READ_CACHING_HE :
3161 0, 0, NULL);
3162}
3163
3164static void
Pavel Shilovsky42873b02013-09-05 21:30:16 +04003165smb2_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
3166 unsigned int epoch, bool *purge_cache)
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04003167{
3168 oplock &= 0xFF;
3169 if (oplock == SMB2_OPLOCK_LEVEL_NOCHANGE)
3170 return;
3171 if (oplock == SMB2_OPLOCK_LEVEL_BATCH) {
Pavel Shilovsky42873b02013-09-05 21:30:16 +04003172 cinode->oplock = CIFS_CACHE_RHW_FLG;
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04003173 cifs_dbg(FYI, "Batch Oplock granted on inode %p\n",
3174 &cinode->vfs_inode);
3175 } else if (oplock == SMB2_OPLOCK_LEVEL_EXCLUSIVE) {
Pavel Shilovsky42873b02013-09-05 21:30:16 +04003176 cinode->oplock = CIFS_CACHE_RW_FLG;
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04003177 cifs_dbg(FYI, "Exclusive Oplock granted on inode %p\n",
3178 &cinode->vfs_inode);
3179 } else if (oplock == SMB2_OPLOCK_LEVEL_II) {
3180 cinode->oplock = CIFS_CACHE_READ_FLG;
3181 cifs_dbg(FYI, "Level II Oplock granted on inode %p\n",
3182 &cinode->vfs_inode);
3183 } else
3184 cinode->oplock = 0;
3185}
3186
3187static void
Pavel Shilovsky42873b02013-09-05 21:30:16 +04003188smb21_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
3189 unsigned int epoch, bool *purge_cache)
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04003190{
3191 char message[5] = {0};
Christoph Probst6a54b2e2019-05-07 17:16:40 +02003192 unsigned int new_oplock = 0;
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04003193
3194 oplock &= 0xFF;
3195 if (oplock == SMB2_OPLOCK_LEVEL_NOCHANGE)
3196 return;
3197
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04003198 if (oplock & SMB2_LEASE_READ_CACHING_HE) {
Christoph Probst6a54b2e2019-05-07 17:16:40 +02003199 new_oplock |= CIFS_CACHE_READ_FLG;
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04003200 strcat(message, "R");
3201 }
3202 if (oplock & SMB2_LEASE_HANDLE_CACHING_HE) {
Christoph Probst6a54b2e2019-05-07 17:16:40 +02003203 new_oplock |= CIFS_CACHE_HANDLE_FLG;
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04003204 strcat(message, "H");
3205 }
3206 if (oplock & SMB2_LEASE_WRITE_CACHING_HE) {
Christoph Probst6a54b2e2019-05-07 17:16:40 +02003207 new_oplock |= CIFS_CACHE_WRITE_FLG;
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04003208 strcat(message, "W");
3209 }
Christoph Probst6a54b2e2019-05-07 17:16:40 +02003210 if (!new_oplock)
3211 strncpy(message, "None", sizeof(message));
3212
3213 cinode->oplock = new_oplock;
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04003214 cifs_dbg(FYI, "%s Lease granted on inode %p\n", message,
3215 &cinode->vfs_inode);
3216}
3217
Pavel Shilovsky42873b02013-09-05 21:30:16 +04003218static void
3219smb3_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
3220 unsigned int epoch, bool *purge_cache)
3221{
3222 unsigned int old_oplock = cinode->oplock;
3223
3224 smb21_set_oplock_level(cinode, oplock, epoch, purge_cache);
3225
3226 if (purge_cache) {
3227 *purge_cache = false;
3228 if (old_oplock == CIFS_CACHE_READ_FLG) {
3229 if (cinode->oplock == CIFS_CACHE_READ_FLG &&
3230 (epoch - cinode->epoch > 0))
3231 *purge_cache = true;
3232 else if (cinode->oplock == CIFS_CACHE_RH_FLG &&
3233 (epoch - cinode->epoch > 1))
3234 *purge_cache = true;
3235 else if (cinode->oplock == CIFS_CACHE_RHW_FLG &&
3236 (epoch - cinode->epoch > 1))
3237 *purge_cache = true;
3238 else if (cinode->oplock == 0 &&
3239 (epoch - cinode->epoch > 0))
3240 *purge_cache = true;
3241 } else if (old_oplock == CIFS_CACHE_RH_FLG) {
3242 if (cinode->oplock == CIFS_CACHE_RH_FLG &&
3243 (epoch - cinode->epoch > 0))
3244 *purge_cache = true;
3245 else if (cinode->oplock == CIFS_CACHE_RHW_FLG &&
3246 (epoch - cinode->epoch > 1))
3247 *purge_cache = true;
3248 }
3249 cinode->epoch = epoch;
3250 }
3251}
3252
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04003253static bool
3254smb2_is_read_op(__u32 oplock)
3255{
3256 return oplock == SMB2_OPLOCK_LEVEL_II;
3257}
3258
3259static bool
3260smb21_is_read_op(__u32 oplock)
3261{
3262 return (oplock & SMB2_LEASE_READ_CACHING_HE) &&
3263 !(oplock & SMB2_LEASE_WRITE_CACHING_HE);
3264}
3265
Pavel Shilovskyf0473902013-09-04 13:44:05 +04003266static __le32
3267map_oplock_to_lease(u8 oplock)
3268{
3269 if (oplock == SMB2_OPLOCK_LEVEL_EXCLUSIVE)
3270 return SMB2_LEASE_WRITE_CACHING | SMB2_LEASE_READ_CACHING;
3271 else if (oplock == SMB2_OPLOCK_LEVEL_II)
3272 return SMB2_LEASE_READ_CACHING;
3273 else if (oplock == SMB2_OPLOCK_LEVEL_BATCH)
3274 return SMB2_LEASE_HANDLE_CACHING | SMB2_LEASE_READ_CACHING |
3275 SMB2_LEASE_WRITE_CACHING;
3276 return 0;
3277}
3278
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04003279static char *
3280smb2_create_lease_buf(u8 *lease_key, u8 oplock)
3281{
3282 struct create_lease *buf;
3283
3284 buf = kzalloc(sizeof(struct create_lease), GFP_KERNEL);
3285 if (!buf)
3286 return NULL;
3287
Stefano Brivio729c0c92018-07-05 15:10:02 +02003288 memcpy(&buf->lcontext.LeaseKey, lease_key, SMB2_LEASE_KEY_SIZE);
Pavel Shilovskyf0473902013-09-04 13:44:05 +04003289 buf->lcontext.LeaseState = map_oplock_to_lease(oplock);
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04003290
3291 buf->ccontext.DataOffset = cpu_to_le16(offsetof
3292 (struct create_lease, lcontext));
3293 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context));
3294 buf->ccontext.NameOffset = cpu_to_le16(offsetof
3295 (struct create_lease, Name));
3296 buf->ccontext.NameLength = cpu_to_le16(4);
Steve French12197a72014-05-14 05:29:40 -07003297 /* SMB2_CREATE_REQUEST_LEASE is "RqLs" */
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04003298 buf->Name[0] = 'R';
3299 buf->Name[1] = 'q';
3300 buf->Name[2] = 'L';
3301 buf->Name[3] = 's';
3302 return (char *)buf;
3303}
3304
Pavel Shilovskyf0473902013-09-04 13:44:05 +04003305static char *
3306smb3_create_lease_buf(u8 *lease_key, u8 oplock)
3307{
3308 struct create_lease_v2 *buf;
3309
3310 buf = kzalloc(sizeof(struct create_lease_v2), GFP_KERNEL);
3311 if (!buf)
3312 return NULL;
3313
Stefano Brivio729c0c92018-07-05 15:10:02 +02003314 memcpy(&buf->lcontext.LeaseKey, lease_key, SMB2_LEASE_KEY_SIZE);
Pavel Shilovskyf0473902013-09-04 13:44:05 +04003315 buf->lcontext.LeaseState = map_oplock_to_lease(oplock);
3316
3317 buf->ccontext.DataOffset = cpu_to_le16(offsetof
3318 (struct create_lease_v2, lcontext));
3319 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context_v2));
3320 buf->ccontext.NameOffset = cpu_to_le16(offsetof
3321 (struct create_lease_v2, Name));
3322 buf->ccontext.NameLength = cpu_to_le16(4);
Steve French12197a72014-05-14 05:29:40 -07003323 /* SMB2_CREATE_REQUEST_LEASE is "RqLs" */
Pavel Shilovskyf0473902013-09-04 13:44:05 +04003324 buf->Name[0] = 'R';
3325 buf->Name[1] = 'q';
3326 buf->Name[2] = 'L';
3327 buf->Name[3] = 's';
3328 return (char *)buf;
3329}
3330
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04003331static __u8
Ronnie Sahlberg96164ab2018-04-26 08:10:18 -06003332smb2_parse_lease_buf(void *buf, unsigned int *epoch, char *lease_key)
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04003333{
3334 struct create_lease *lc = (struct create_lease *)buf;
3335
Pavel Shilovsky42873b02013-09-05 21:30:16 +04003336 *epoch = 0; /* not used */
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04003337 if (lc->lcontext.LeaseFlags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS)
3338 return SMB2_OPLOCK_LEVEL_NOCHANGE;
3339 return le32_to_cpu(lc->lcontext.LeaseState);
3340}
3341
Pavel Shilovskyf0473902013-09-04 13:44:05 +04003342static __u8
Ronnie Sahlberg96164ab2018-04-26 08:10:18 -06003343smb3_parse_lease_buf(void *buf, unsigned int *epoch, char *lease_key)
Pavel Shilovskyf0473902013-09-04 13:44:05 +04003344{
3345 struct create_lease_v2 *lc = (struct create_lease_v2 *)buf;
3346
Pavel Shilovsky42873b02013-09-05 21:30:16 +04003347 *epoch = le16_to_cpu(lc->lcontext.Epoch);
Pavel Shilovskyf0473902013-09-04 13:44:05 +04003348 if (lc->lcontext.LeaseFlags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS)
3349 return SMB2_OPLOCK_LEVEL_NOCHANGE;
Ronnie Sahlberg96164ab2018-04-26 08:10:18 -06003350 if (lease_key)
Stefano Brivio729c0c92018-07-05 15:10:02 +02003351 memcpy(lease_key, &lc->lcontext.LeaseKey, SMB2_LEASE_KEY_SIZE);
Pavel Shilovskyf0473902013-09-04 13:44:05 +04003352 return le32_to_cpu(lc->lcontext.LeaseState);
3353}
3354
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04003355static unsigned int
3356smb2_wp_retry_size(struct inode *inode)
3357{
3358 return min_t(unsigned int, CIFS_SB(inode->i_sb)->wsize,
3359 SMB2_MAX_BUFFER_SIZE);
3360}
3361
Pavel Shilovsky52755802014-08-18 20:49:57 +04003362static bool
3363smb2_dir_needs_close(struct cifsFileInfo *cfile)
3364{
3365 return !cfile->invalidHandle;
3366}
3367
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07003368static void
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10003369fill_transform_hdr(struct smb2_transform_hdr *tr_hdr, unsigned int orig_len,
3370 struct smb_rqst *old_rq)
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07003371{
3372 struct smb2_sync_hdr *shdr =
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10003373 (struct smb2_sync_hdr *)old_rq->rq_iov[0].iov_base;
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07003374
3375 memset(tr_hdr, 0, sizeof(struct smb2_transform_hdr));
3376 tr_hdr->ProtocolId = SMB2_TRANSFORM_PROTO_NUM;
3377 tr_hdr->OriginalMessageSize = cpu_to_le32(orig_len);
3378 tr_hdr->Flags = cpu_to_le16(0x01);
3379 get_random_bytes(&tr_hdr->Nonce, SMB3_AES128CMM_NONCE);
3380 memcpy(&tr_hdr->SessionId, &shdr->SessionId, 8);
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07003381}
3382
Ronnie Sahlberg262916b2018-02-20 12:45:21 +11003383/* We can not use the normal sg_set_buf() as we will sometimes pass a
3384 * stack object as buf.
3385 */
3386static inline void smb2_sg_set_buf(struct scatterlist *sg, const void *buf,
3387 unsigned int buflen)
3388{
3389 sg_set_page(sg, virt_to_page(buf), buflen, offset_in_page(buf));
3390}
3391
Ronnie Sahlbergb2c96de2018-08-01 09:26:11 +10003392/* Assumes the first rqst has a transform header as the first iov.
3393 * I.e.
3394 * rqst[0].rq_iov[0] is transform header
3395 * rqst[0].rq_iov[1+] data to be encrypted/decrypted
3396 * rqst[1+].rq_iov[0+] data to be encrypted/decrypted
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10003397 */
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07003398static struct scatterlist *
Ronnie Sahlbergb2c96de2018-08-01 09:26:11 +10003399init_sg(int num_rqst, struct smb_rqst *rqst, u8 *sign)
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07003400{
Ronnie Sahlbergb2c96de2018-08-01 09:26:11 +10003401 unsigned int sg_len;
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07003402 struct scatterlist *sg;
3403 unsigned int i;
3404 unsigned int j;
Ronnie Sahlbergb2c96de2018-08-01 09:26:11 +10003405 unsigned int idx = 0;
3406 int skip;
3407
3408 sg_len = 1;
3409 for (i = 0; i < num_rqst; i++)
3410 sg_len += rqst[i].rq_nvec + rqst[i].rq_npages;
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07003411
3412 sg = kmalloc_array(sg_len, sizeof(struct scatterlist), GFP_KERNEL);
3413 if (!sg)
3414 return NULL;
3415
3416 sg_init_table(sg, sg_len);
Ronnie Sahlbergb2c96de2018-08-01 09:26:11 +10003417 for (i = 0; i < num_rqst; i++) {
3418 for (j = 0; j < rqst[i].rq_nvec; j++) {
3419 /*
3420 * The first rqst has a transform header where the
3421 * first 20 bytes are not part of the encrypted blob
3422 */
3423 skip = (i == 0) && (j == 0) ? 20 : 0;
3424 smb2_sg_set_buf(&sg[idx++],
3425 rqst[i].rq_iov[j].iov_base + skip,
3426 rqst[i].rq_iov[j].iov_len - skip);
Ronnie Sahlberge77fe732018-12-31 13:43:40 +10003427 }
Steve Frenchd5f07fb2018-06-05 17:46:24 -05003428
Ronnie Sahlbergb2c96de2018-08-01 09:26:11 +10003429 for (j = 0; j < rqst[i].rq_npages; j++) {
3430 unsigned int len, offset;
3431
3432 rqst_page_get_length(&rqst[i], j, &len, &offset);
3433 sg_set_page(&sg[idx++], rqst[i].rq_pages[j], len, offset);
3434 }
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07003435 }
Ronnie Sahlbergb2c96de2018-08-01 09:26:11 +10003436 smb2_sg_set_buf(&sg[idx], sign, SMB2_SIGNATURE_SIZE);
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07003437 return sg;
3438}
3439
Pavel Shilovsky61cfac6f2017-02-28 16:05:19 -08003440static int
3441smb2_get_enc_key(struct TCP_Server_Info *server, __u64 ses_id, int enc, u8 *key)
3442{
3443 struct cifs_ses *ses;
3444 u8 *ses_enc_key;
3445
3446 spin_lock(&cifs_tcp_ses_lock);
3447 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
3448 if (ses->Suid != ses_id)
3449 continue;
3450 ses_enc_key = enc ? ses->smb3encryptionkey :
3451 ses->smb3decryptionkey;
3452 memcpy(key, ses_enc_key, SMB3_SIGN_KEY_SIZE);
3453 spin_unlock(&cifs_tcp_ses_lock);
3454 return 0;
3455 }
3456 spin_unlock(&cifs_tcp_ses_lock);
3457
3458 return 1;
3459}
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07003460/*
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10003461 * Encrypt or decrypt @rqst message. @rqst[0] has the following format:
3462 * iov[0] - transform header (associate data),
3463 * iov[1-N] - SMB2 header and pages - data to encrypt.
3464 * On success return encrypted data in iov[1-N] and pages, leave iov[0]
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07003465 * untouched.
3466 */
3467static int
Ronnie Sahlbergb2c96de2018-08-01 09:26:11 +10003468crypt_message(struct TCP_Server_Info *server, int num_rqst,
3469 struct smb_rqst *rqst, int enc)
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07003470{
3471 struct smb2_transform_hdr *tr_hdr =
Ronnie Sahlbergb2c96de2018-08-01 09:26:11 +10003472 (struct smb2_transform_hdr *)rqst[0].rq_iov[0].iov_base;
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10003473 unsigned int assoc_data_len = sizeof(struct smb2_transform_hdr) - 20;
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07003474 int rc = 0;
3475 struct scatterlist *sg;
3476 u8 sign[SMB2_SIGNATURE_SIZE] = {};
Pavel Shilovsky61cfac6f2017-02-28 16:05:19 -08003477 u8 key[SMB3_SIGN_KEY_SIZE];
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07003478 struct aead_request *req;
3479 char *iv;
3480 unsigned int iv_len;
Gilad Ben-Yossefa5186b82017-10-18 08:00:46 +01003481 DECLARE_CRYPTO_WAIT(wait);
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07003482 struct crypto_aead *tfm;
3483 unsigned int crypt_len = le32_to_cpu(tr_hdr->OriginalMessageSize);
3484
Pavel Shilovsky61cfac6f2017-02-28 16:05:19 -08003485 rc = smb2_get_enc_key(server, tr_hdr->SessionId, enc, key);
3486 if (rc) {
3487 cifs_dbg(VFS, "%s: Could not get %scryption key\n", __func__,
3488 enc ? "en" : "de");
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07003489 return 0;
3490 }
3491
3492 rc = smb3_crypto_aead_allocate(server);
3493 if (rc) {
3494 cifs_dbg(VFS, "%s: crypto alloc failed\n", __func__);
3495 return rc;
3496 }
3497
3498 tfm = enc ? server->secmech.ccmaesencrypt :
3499 server->secmech.ccmaesdecrypt;
Pavel Shilovsky61cfac6f2017-02-28 16:05:19 -08003500 rc = crypto_aead_setkey(tfm, key, SMB3_SIGN_KEY_SIZE);
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07003501 if (rc) {
3502 cifs_dbg(VFS, "%s: Failed to set aead key %d\n", __func__, rc);
3503 return rc;
3504 }
3505
3506 rc = crypto_aead_setauthsize(tfm, SMB2_SIGNATURE_SIZE);
3507 if (rc) {
3508 cifs_dbg(VFS, "%s: Failed to set authsize %d\n", __func__, rc);
3509 return rc;
3510 }
3511
3512 req = aead_request_alloc(tfm, GFP_KERNEL);
3513 if (!req) {
Christoph Probsta205d502019-05-08 21:36:25 +02003514 cifs_dbg(VFS, "%s: Failed to alloc aead request\n", __func__);
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07003515 return -ENOMEM;
3516 }
3517
3518 if (!enc) {
3519 memcpy(sign, &tr_hdr->Signature, SMB2_SIGNATURE_SIZE);
3520 crypt_len += SMB2_SIGNATURE_SIZE;
3521 }
3522
Ronnie Sahlbergb2c96de2018-08-01 09:26:11 +10003523 sg = init_sg(num_rqst, rqst, sign);
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07003524 if (!sg) {
Christoph Probsta205d502019-05-08 21:36:25 +02003525 cifs_dbg(VFS, "%s: Failed to init sg\n", __func__);
Christophe Jaillet517a6e42017-06-11 09:12:47 +02003526 rc = -ENOMEM;
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07003527 goto free_req;
3528 }
3529
3530 iv_len = crypto_aead_ivsize(tfm);
3531 iv = kzalloc(iv_len, GFP_KERNEL);
3532 if (!iv) {
Christoph Probsta205d502019-05-08 21:36:25 +02003533 cifs_dbg(VFS, "%s: Failed to alloc iv\n", __func__);
Christophe Jaillet517a6e42017-06-11 09:12:47 +02003534 rc = -ENOMEM;
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07003535 goto free_sg;
3536 }
3537 iv[0] = 3;
3538 memcpy(iv + 1, (char *)tr_hdr->Nonce, SMB3_AES128CMM_NONCE);
3539
3540 aead_request_set_crypt(req, sg, sg, crypt_len, iv);
3541 aead_request_set_ad(req, assoc_data_len);
3542
3543 aead_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
Gilad Ben-Yossefa5186b82017-10-18 08:00:46 +01003544 crypto_req_done, &wait);
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07003545
Gilad Ben-Yossefa5186b82017-10-18 08:00:46 +01003546 rc = crypto_wait_req(enc ? crypto_aead_encrypt(req)
3547 : crypto_aead_decrypt(req), &wait);
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07003548
3549 if (!rc && enc)
3550 memcpy(&tr_hdr->Signature, sign, SMB2_SIGNATURE_SIZE);
3551
3552 kfree(iv);
3553free_sg:
3554 kfree(sg);
3555free_req:
3556 kfree(req);
3557 return rc;
3558}
3559
Ronnie Sahlbergb2c96de2018-08-01 09:26:11 +10003560void
3561smb3_free_compound_rqst(int num_rqst, struct smb_rqst *rqst)
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07003562{
Ronnie Sahlbergb2c96de2018-08-01 09:26:11 +10003563 int i, j;
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07003564
Ronnie Sahlbergb2c96de2018-08-01 09:26:11 +10003565 for (i = 0; i < num_rqst; i++) {
3566 if (rqst[i].rq_pages) {
3567 for (j = rqst[i].rq_npages - 1; j >= 0; j--)
3568 put_page(rqst[i].rq_pages[j]);
3569 kfree(rqst[i].rq_pages);
3570 }
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07003571 }
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07003572}
3573
Ronnie Sahlbergb2c96de2018-08-01 09:26:11 +10003574/*
3575 * This function will initialize new_rq and encrypt the content.
3576 * The first entry, new_rq[0], only contains a single iov which contains
3577 * a smb2_transform_hdr and is pre-allocated by the caller.
3578 * This function then populates new_rq[1+] with the content from olq_rq[0+].
3579 *
3580 * The end result is an array of smb_rqst structures where the first structure
3581 * only contains a single iov for the transform header which we then can pass
3582 * to crypt_message().
3583 *
3584 * new_rq[0].rq_iov[0] : smb2_transform_hdr pre-allocated by the caller
3585 * new_rq[1+].rq_iov[*] == old_rq[0+].rq_iov[*] : SMB2/3 requests
3586 */
3587static int
3588smb3_init_transform_rq(struct TCP_Server_Info *server, int num_rqst,
3589 struct smb_rqst *new_rq, struct smb_rqst *old_rq)
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07003590{
Ronnie Sahlbergb2c96de2018-08-01 09:26:11 +10003591 struct page **pages;
3592 struct smb2_transform_hdr *tr_hdr = new_rq[0].rq_iov[0].iov_base;
3593 unsigned int npages;
3594 unsigned int orig_len = 0;
3595 int i, j;
3596 int rc = -ENOMEM;
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07003597
Ronnie Sahlbergb2c96de2018-08-01 09:26:11 +10003598 for (i = 1; i < num_rqst; i++) {
3599 npages = old_rq[i - 1].rq_npages;
3600 pages = kmalloc_array(npages, sizeof(struct page *),
3601 GFP_KERNEL);
3602 if (!pages)
3603 goto err_free;
3604
3605 new_rq[i].rq_pages = pages;
3606 new_rq[i].rq_npages = npages;
3607 new_rq[i].rq_offset = old_rq[i - 1].rq_offset;
3608 new_rq[i].rq_pagesz = old_rq[i - 1].rq_pagesz;
3609 new_rq[i].rq_tailsz = old_rq[i - 1].rq_tailsz;
3610 new_rq[i].rq_iov = old_rq[i - 1].rq_iov;
3611 new_rq[i].rq_nvec = old_rq[i - 1].rq_nvec;
3612
3613 orig_len += smb_rqst_len(server, &old_rq[i - 1]);
3614
3615 for (j = 0; j < npages; j++) {
3616 pages[j] = alloc_page(GFP_KERNEL|__GFP_HIGHMEM);
3617 if (!pages[j])
3618 goto err_free;
3619 }
3620
3621 /* copy pages form the old */
3622 for (j = 0; j < npages; j++) {
3623 char *dst, *src;
3624 unsigned int offset, len;
3625
3626 rqst_page_get_length(&new_rq[i], j, &len, &offset);
3627
3628 dst = (char *) kmap(new_rq[i].rq_pages[j]) + offset;
3629 src = (char *) kmap(old_rq[i - 1].rq_pages[j]) + offset;
3630
3631 memcpy(dst, src, len);
3632 kunmap(new_rq[i].rq_pages[j]);
3633 kunmap(old_rq[i - 1].rq_pages[j]);
3634 }
3635 }
3636
3637 /* fill the 1st iov with a transform header */
3638 fill_transform_hdr(tr_hdr, orig_len, old_rq);
3639
3640 rc = crypt_message(server, num_rqst, new_rq, 1);
Christoph Probsta205d502019-05-08 21:36:25 +02003641 cifs_dbg(FYI, "Encrypt message returned %d\n", rc);
Ronnie Sahlbergb2c96de2018-08-01 09:26:11 +10003642 if (rc)
3643 goto err_free;
3644
3645 return rc;
3646
3647err_free:
3648 smb3_free_compound_rqst(num_rqst - 1, &new_rq[1]);
3649 return rc;
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07003650}
3651
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08003652static int
3653smb3_is_transform_hdr(void *buf)
3654{
3655 struct smb2_transform_hdr *trhdr = buf;
3656
3657 return trhdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM;
3658}
3659
3660static int
3661decrypt_raw_data(struct TCP_Server_Info *server, char *buf,
3662 unsigned int buf_data_size, struct page **pages,
3663 unsigned int npages, unsigned int page_data_size)
3664{
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10003665 struct kvec iov[2];
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08003666 struct smb_rqst rqst = {NULL};
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08003667 int rc;
3668
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10003669 iov[0].iov_base = buf;
3670 iov[0].iov_len = sizeof(struct smb2_transform_hdr);
3671 iov[1].iov_base = buf + sizeof(struct smb2_transform_hdr);
3672 iov[1].iov_len = buf_data_size;
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08003673
3674 rqst.rq_iov = iov;
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10003675 rqst.rq_nvec = 2;
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08003676 rqst.rq_pages = pages;
3677 rqst.rq_npages = npages;
3678 rqst.rq_pagesz = PAGE_SIZE;
3679 rqst.rq_tailsz = (page_data_size % PAGE_SIZE) ? : PAGE_SIZE;
3680
Ronnie Sahlbergb2c96de2018-08-01 09:26:11 +10003681 rc = crypt_message(server, 1, &rqst, 0);
Christoph Probsta205d502019-05-08 21:36:25 +02003682 cifs_dbg(FYI, "Decrypt message returned %d\n", rc);
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08003683
3684 if (rc)
3685 return rc;
3686
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10003687 memmove(buf, iov[1].iov_base, buf_data_size);
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10003688
3689 server->total_read = buf_data_size + page_data_size;
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08003690
3691 return rc;
3692}
3693
3694static int
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08003695read_data_into_pages(struct TCP_Server_Info *server, struct page **pages,
3696 unsigned int npages, unsigned int len)
3697{
3698 int i;
3699 int length;
3700
3701 for (i = 0; i < npages; i++) {
3702 struct page *page = pages[i];
3703 size_t n;
3704
3705 n = len;
3706 if (len >= PAGE_SIZE) {
3707 /* enough data to fill the page */
3708 n = PAGE_SIZE;
3709 len -= n;
3710 } else {
3711 zero_user(page, len, PAGE_SIZE - len);
3712 len = 0;
3713 }
Long Li1dbe3462018-05-30 12:47:55 -07003714 length = cifs_read_page_from_socket(server, page, 0, n);
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08003715 if (length < 0)
3716 return length;
3717 server->total_read += length;
3718 }
3719
3720 return 0;
3721}
3722
3723static int
3724init_read_bvec(struct page **pages, unsigned int npages, unsigned int data_size,
3725 unsigned int cur_off, struct bio_vec **page_vec)
3726{
3727 struct bio_vec *bvec;
3728 int i;
3729
3730 bvec = kcalloc(npages, sizeof(struct bio_vec), GFP_KERNEL);
3731 if (!bvec)
3732 return -ENOMEM;
3733
3734 for (i = 0; i < npages; i++) {
3735 bvec[i].bv_page = pages[i];
3736 bvec[i].bv_offset = (i == 0) ? cur_off : 0;
3737 bvec[i].bv_len = min_t(unsigned int, PAGE_SIZE, data_size);
3738 data_size -= bvec[i].bv_len;
3739 }
3740
3741 if (data_size != 0) {
3742 cifs_dbg(VFS, "%s: something went wrong\n", __func__);
3743 kfree(bvec);
3744 return -EIO;
3745 }
3746
3747 *page_vec = bvec;
3748 return 0;
3749}
3750
3751static int
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08003752handle_read_data(struct TCP_Server_Info *server, struct mid_q_entry *mid,
3753 char *buf, unsigned int buf_len, struct page **pages,
3754 unsigned int npages, unsigned int page_data_size)
3755{
3756 unsigned int data_offset;
3757 unsigned int data_len;
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08003758 unsigned int cur_off;
3759 unsigned int cur_page_idx;
3760 unsigned int pad_len;
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08003761 struct cifs_readdata *rdata = mid->callback_data;
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10003762 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08003763 struct bio_vec *bvec = NULL;
3764 struct iov_iter iter;
3765 struct kvec iov;
3766 int length;
Long Li74dcf412017-11-22 17:38:46 -07003767 bool use_rdma_mr = false;
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08003768
3769 if (shdr->Command != SMB2_READ) {
3770 cifs_dbg(VFS, "only big read responses are supported\n");
3771 return -ENOTSUPP;
3772 }
3773
Pavel Shilovsky511c54a2017-07-08 14:32:00 -07003774 if (server->ops->is_session_expired &&
3775 server->ops->is_session_expired(buf)) {
3776 cifs_reconnect(server);
3777 wake_up(&server->response_q);
3778 return -1;
3779 }
3780
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08003781 if (server->ops->is_status_pending &&
Pavel Shilovsky66265f12019-01-23 17:11:16 -08003782 server->ops->is_status_pending(buf, server))
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08003783 return -1;
3784
Pavel Shilovskyec678ea2019-01-18 15:38:11 -08003785 /* set up first two iov to get credits */
3786 rdata->iov[0].iov_base = buf;
Pavel Shilovskybb1bccb2019-01-17 16:18:38 -08003787 rdata->iov[0].iov_len = 0;
3788 rdata->iov[1].iov_base = buf;
Pavel Shilovskyec678ea2019-01-18 15:38:11 -08003789 rdata->iov[1].iov_len =
Pavel Shilovskybb1bccb2019-01-17 16:18:38 -08003790 min_t(unsigned int, buf_len, server->vals->read_rsp_size);
Pavel Shilovskyec678ea2019-01-18 15:38:11 -08003791 cifs_dbg(FYI, "0: iov_base=%p iov_len=%zu\n",
3792 rdata->iov[0].iov_base, rdata->iov[0].iov_len);
3793 cifs_dbg(FYI, "1: iov_base=%p iov_len=%zu\n",
3794 rdata->iov[1].iov_base, rdata->iov[1].iov_len);
3795
3796 rdata->result = server->ops->map_error(buf, true);
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08003797 if (rdata->result != 0) {
3798 cifs_dbg(FYI, "%s: server returned error %d\n",
3799 __func__, rdata->result);
Pavel Shilovskyec678ea2019-01-18 15:38:11 -08003800 /* normal error on read response */
3801 dequeue_mid(mid, false);
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08003802 return 0;
3803 }
3804
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10003805 data_offset = server->ops->read_data_offset(buf);
Long Li74dcf412017-11-22 17:38:46 -07003806#ifdef CONFIG_CIFS_SMB_DIRECT
3807 use_rdma_mr = rdata->mr;
3808#endif
3809 data_len = server->ops->read_data_length(buf, use_rdma_mr);
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08003810
3811 if (data_offset < server->vals->read_rsp_size) {
3812 /*
3813 * win2k8 sometimes sends an offset of 0 when the read
3814 * is beyond the EOF. Treat it as if the data starts just after
3815 * the header.
3816 */
3817 cifs_dbg(FYI, "%s: data offset (%u) inside read response header\n",
3818 __func__, data_offset);
3819 data_offset = server->vals->read_rsp_size;
3820 } else if (data_offset > MAX_CIFS_SMALL_BUFFER_SIZE) {
3821 /* data_offset is beyond the end of smallbuf */
3822 cifs_dbg(FYI, "%s: data offset (%u) beyond end of smallbuf\n",
3823 __func__, data_offset);
3824 rdata->result = -EIO;
3825 dequeue_mid(mid, rdata->result);
3826 return 0;
3827 }
3828
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08003829 pad_len = data_offset - server->vals->read_rsp_size;
3830
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08003831 if (buf_len <= data_offset) {
3832 /* read response payload is in pages */
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08003833 cur_page_idx = pad_len / PAGE_SIZE;
3834 cur_off = pad_len % PAGE_SIZE;
3835
3836 if (cur_page_idx != 0) {
3837 /* data offset is beyond the 1st page of response */
3838 cifs_dbg(FYI, "%s: data offset (%u) beyond 1st page of response\n",
3839 __func__, data_offset);
3840 rdata->result = -EIO;
3841 dequeue_mid(mid, rdata->result);
3842 return 0;
3843 }
3844
3845 if (data_len > page_data_size - pad_len) {
3846 /* data_len is corrupt -- discard frame */
3847 rdata->result = -EIO;
3848 dequeue_mid(mid, rdata->result);
3849 return 0;
3850 }
3851
3852 rdata->result = init_read_bvec(pages, npages, page_data_size,
3853 cur_off, &bvec);
3854 if (rdata->result != 0) {
3855 dequeue_mid(mid, rdata->result);
3856 return 0;
3857 }
3858
David Howellsaa563d72018-10-20 00:57:56 +01003859 iov_iter_bvec(&iter, WRITE, bvec, npages, data_len);
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08003860 } else if (buf_len >= data_offset + data_len) {
3861 /* read response payload is in buf */
3862 WARN_ONCE(npages > 0, "read data can be either in buf or in pages");
3863 iov.iov_base = buf + data_offset;
3864 iov.iov_len = data_len;
David Howellsaa563d72018-10-20 00:57:56 +01003865 iov_iter_kvec(&iter, WRITE, &iov, 1, data_len);
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08003866 } else {
3867 /* read response payload cannot be in both buf and pages */
3868 WARN_ONCE(1, "buf can not contain only a part of read data");
3869 rdata->result = -EIO;
3870 dequeue_mid(mid, rdata->result);
3871 return 0;
3872 }
3873
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08003874 length = rdata->copy_into_pages(server, rdata, &iter);
3875
3876 kfree(bvec);
3877
3878 if (length < 0)
3879 return length;
3880
3881 dequeue_mid(mid, false);
3882 return length;
3883}
3884
3885static int
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08003886receive_encrypted_read(struct TCP_Server_Info *server, struct mid_q_entry **mid)
3887{
3888 char *buf = server->smallbuf;
3889 struct smb2_transform_hdr *tr_hdr = (struct smb2_transform_hdr *)buf;
3890 unsigned int npages;
3891 struct page **pages;
3892 unsigned int len;
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10003893 unsigned int buflen = server->pdu_size;
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08003894 int rc;
3895 int i = 0;
3896
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10003897 len = min_t(unsigned int, buflen, server->vals->read_rsp_size +
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08003898 sizeof(struct smb2_transform_hdr)) - HEADER_SIZE(server) + 1;
3899
3900 rc = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1, len);
3901 if (rc < 0)
3902 return rc;
3903 server->total_read += rc;
3904
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10003905 len = le32_to_cpu(tr_hdr->OriginalMessageSize) -
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +11003906 server->vals->read_rsp_size;
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08003907 npages = DIV_ROUND_UP(len, PAGE_SIZE);
3908
3909 pages = kmalloc_array(npages, sizeof(struct page *), GFP_KERNEL);
3910 if (!pages) {
3911 rc = -ENOMEM;
3912 goto discard_data;
3913 }
3914
3915 for (; i < npages; i++) {
3916 pages[i] = alloc_page(GFP_KERNEL|__GFP_HIGHMEM);
3917 if (!pages[i]) {
3918 rc = -ENOMEM;
3919 goto discard_data;
3920 }
3921 }
3922
3923 /* read read data into pages */
3924 rc = read_data_into_pages(server, pages, npages, len);
3925 if (rc)
3926 goto free_pages;
3927
Pavel Shilovsky350be252017-04-10 10:31:33 -07003928 rc = cifs_discard_remaining_data(server);
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08003929 if (rc)
3930 goto free_pages;
3931
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10003932 rc = decrypt_raw_data(server, buf, server->vals->read_rsp_size,
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08003933 pages, npages, len);
3934 if (rc)
3935 goto free_pages;
3936
3937 *mid = smb2_find_mid(server, buf);
3938 if (*mid == NULL)
3939 cifs_dbg(FYI, "mid not found\n");
3940 else {
3941 cifs_dbg(FYI, "mid found\n");
3942 (*mid)->decrypted = true;
3943 rc = handle_read_data(server, *mid, buf,
3944 server->vals->read_rsp_size,
3945 pages, npages, len);
3946 }
3947
3948free_pages:
3949 for (i = i - 1; i >= 0; i--)
3950 put_page(pages[i]);
3951 kfree(pages);
3952 return rc;
3953discard_data:
Pavel Shilovsky350be252017-04-10 10:31:33 -07003954 cifs_discard_remaining_data(server);
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08003955 goto free_pages;
3956}
3957
3958static int
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08003959receive_encrypted_standard(struct TCP_Server_Info *server,
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10003960 struct mid_q_entry **mids, char **bufs,
3961 int *num_mids)
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08003962{
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10003963 int ret, length;
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08003964 char *buf = server->smallbuf;
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10003965 char *tmpbuf;
3966 struct smb2_sync_hdr *shdr;
Ronnie Sahlberg2e964672018-04-09 18:06:26 +10003967 unsigned int pdu_length = server->pdu_size;
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08003968 unsigned int buf_size;
3969 struct mid_q_entry *mid_entry;
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10003970 int next_is_large;
3971 char *next_buffer = NULL;
3972
3973 *num_mids = 0;
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08003974
3975 /* switch to large buffer if too big for a small one */
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10003976 if (pdu_length > MAX_CIFS_SMALL_BUFFER_SIZE) {
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08003977 server->large_buf = true;
3978 memcpy(server->bigbuf, buf, server->total_read);
3979 buf = server->bigbuf;
3980 }
3981
3982 /* now read the rest */
3983 length = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1,
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10003984 pdu_length - HEADER_SIZE(server) + 1);
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08003985 if (length < 0)
3986 return length;
3987 server->total_read += length;
3988
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10003989 buf_size = pdu_length - sizeof(struct smb2_transform_hdr);
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08003990 length = decrypt_raw_data(server, buf, buf_size, NULL, 0, 0);
3991 if (length)
3992 return length;
3993
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10003994 next_is_large = server->large_buf;
3995 one_more:
3996 shdr = (struct smb2_sync_hdr *)buf;
3997 if (shdr->NextCommand) {
3998 if (next_is_large) {
3999 tmpbuf = server->bigbuf;
4000 next_buffer = (char *)cifs_buf_get();
4001 } else {
4002 tmpbuf = server->smallbuf;
4003 next_buffer = (char *)cifs_small_buf_get();
4004 }
4005 memcpy(next_buffer,
4006 tmpbuf + le32_to_cpu(shdr->NextCommand),
4007 pdu_length - le32_to_cpu(shdr->NextCommand));
4008 }
4009
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004010 mid_entry = smb2_find_mid(server, buf);
4011 if (mid_entry == NULL)
4012 cifs_dbg(FYI, "mid not found\n");
4013 else {
4014 cifs_dbg(FYI, "mid found\n");
4015 mid_entry->decrypted = true;
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10004016 mid_entry->resp_buf_size = server->pdu_size;
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004017 }
4018
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10004019 if (*num_mids >= MAX_COMPOUND) {
4020 cifs_dbg(VFS, "too many PDUs in compound\n");
4021 return -1;
4022 }
4023 bufs[*num_mids] = buf;
4024 mids[(*num_mids)++] = mid_entry;
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004025
4026 if (mid_entry && mid_entry->handle)
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10004027 ret = mid_entry->handle(server, mid_entry);
4028 else
4029 ret = cifs_handle_standard(server, mid_entry);
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004030
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10004031 if (ret == 0 && shdr->NextCommand) {
4032 pdu_length -= le32_to_cpu(shdr->NextCommand);
4033 server->large_buf = next_is_large;
4034 if (next_is_large)
4035 server->bigbuf = next_buffer;
4036 else
4037 server->smallbuf = next_buffer;
4038
4039 buf += le32_to_cpu(shdr->NextCommand);
4040 goto one_more;
4041 }
4042
4043 return ret;
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004044}
4045
4046static int
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10004047smb3_receive_transform(struct TCP_Server_Info *server,
4048 struct mid_q_entry **mids, char **bufs, int *num_mids)
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004049{
4050 char *buf = server->smallbuf;
Ronnie Sahlberg2e964672018-04-09 18:06:26 +10004051 unsigned int pdu_length = server->pdu_size;
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004052 struct smb2_transform_hdr *tr_hdr = (struct smb2_transform_hdr *)buf;
4053 unsigned int orig_len = le32_to_cpu(tr_hdr->OriginalMessageSize);
4054
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10004055 if (pdu_length < sizeof(struct smb2_transform_hdr) +
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004056 sizeof(struct smb2_sync_hdr)) {
4057 cifs_dbg(VFS, "Transform message is too small (%u)\n",
4058 pdu_length);
4059 cifs_reconnect(server);
4060 wake_up(&server->response_q);
4061 return -ECONNABORTED;
4062 }
4063
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10004064 if (pdu_length < orig_len + sizeof(struct smb2_transform_hdr)) {
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004065 cifs_dbg(VFS, "Transform message is broken\n");
4066 cifs_reconnect(server);
4067 wake_up(&server->response_q);
4068 return -ECONNABORTED;
4069 }
4070
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10004071 /* TODO: add support for compounds containing READ. */
Paul Aurich6d2f84e2018-12-31 14:13:34 -08004072 if (pdu_length > CIFSMaxBufSize + MAX_HEADER_SIZE(server)) {
4073 *num_mids = 1;
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10004074 return receive_encrypted_read(server, &mids[0]);
Paul Aurich6d2f84e2018-12-31 14:13:34 -08004075 }
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004076
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10004077 return receive_encrypted_standard(server, mids, bufs, num_mids);
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004078}
4079
4080int
4081smb3_handle_read_data(struct TCP_Server_Info *server, struct mid_q_entry *mid)
4082{
4083 char *buf = server->large_buf ? server->bigbuf : server->smallbuf;
4084
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10004085 return handle_read_data(server, mid, buf, server->pdu_size,
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004086 NULL, 0, 0);
4087}
4088
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +10004089static int
4090smb2_next_header(char *buf)
4091{
4092 struct smb2_sync_hdr *hdr = (struct smb2_sync_hdr *)buf;
4093 struct smb2_transform_hdr *t_hdr = (struct smb2_transform_hdr *)buf;
4094
4095 if (hdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM)
4096 return sizeof(struct smb2_transform_hdr) +
4097 le32_to_cpu(t_hdr->OriginalMessageSize);
4098
4099 return le32_to_cpu(hdr->NextCommand);
4100}
4101
Aurelien Aptelc847dcc2019-03-14 00:29:17 -05004102static int
4103smb2_make_node(unsigned int xid, struct inode *inode,
4104 struct dentry *dentry, struct cifs_tcon *tcon,
4105 char *full_path, umode_t mode, dev_t dev)
4106{
4107 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
4108 int rc = -EPERM;
4109 int create_options = CREATE_NOT_DIR | CREATE_OPTION_SPECIAL;
4110 FILE_ALL_INFO *buf = NULL;
4111 struct cifs_io_parms io_parms;
4112 __u32 oplock = 0;
4113 struct cifs_fid fid;
4114 struct cifs_open_parms oparms;
4115 unsigned int bytes_written;
4116 struct win_dev *pdev;
4117 struct kvec iov[2];
4118
4119 /*
4120 * Check if mounted with mount parm 'sfu' mount parm.
4121 * SFU emulation should work with all servers, but only
4122 * supports block and char device (no socket & fifo),
4123 * and was used by default in earlier versions of Windows
4124 */
4125 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL))
4126 goto out;
4127
4128 /*
4129 * TODO: Add ability to create instead via reparse point. Windows (e.g.
4130 * their current NFS server) uses this approach to expose special files
4131 * over SMB2/SMB3 and Samba will do this with SMB3.1.1 POSIX Extensions
4132 */
4133
4134 if (!S_ISCHR(mode) && !S_ISBLK(mode))
4135 goto out;
4136
4137 cifs_dbg(FYI, "sfu compat create special file\n");
4138
4139 buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL);
4140 if (buf == NULL) {
4141 rc = -ENOMEM;
4142 goto out;
4143 }
4144
4145 if (backup_cred(cifs_sb))
4146 create_options |= CREATE_OPEN_BACKUP_INTENT;
4147
4148 oparms.tcon = tcon;
4149 oparms.cifs_sb = cifs_sb;
4150 oparms.desired_access = GENERIC_WRITE;
4151 oparms.create_options = create_options;
4152 oparms.disposition = FILE_CREATE;
4153 oparms.path = full_path;
4154 oparms.fid = &fid;
4155 oparms.reconnect = false;
4156
4157 if (tcon->ses->server->oplocks)
4158 oplock = REQ_OPLOCK;
4159 else
4160 oplock = 0;
4161 rc = tcon->ses->server->ops->open(xid, &oparms, &oplock, buf);
4162 if (rc)
4163 goto out;
4164
4165 /*
4166 * BB Do not bother to decode buf since no local inode yet to put
4167 * timestamps in, but we can reuse it safely.
4168 */
4169
4170 pdev = (struct win_dev *)buf;
4171 io_parms.pid = current->tgid;
4172 io_parms.tcon = tcon;
4173 io_parms.offset = 0;
4174 io_parms.length = sizeof(struct win_dev);
4175 iov[1].iov_base = buf;
4176 iov[1].iov_len = sizeof(struct win_dev);
4177 if (S_ISCHR(mode)) {
4178 memcpy(pdev->type, "IntxCHR", 8);
4179 pdev->major = cpu_to_le64(MAJOR(dev));
4180 pdev->minor = cpu_to_le64(MINOR(dev));
4181 rc = tcon->ses->server->ops->sync_write(xid, &fid, &io_parms,
4182 &bytes_written, iov, 1);
4183 } else if (S_ISBLK(mode)) {
4184 memcpy(pdev->type, "IntxBLK", 8);
4185 pdev->major = cpu_to_le64(MAJOR(dev));
4186 pdev->minor = cpu_to_le64(MINOR(dev));
4187 rc = tcon->ses->server->ops->sync_write(xid, &fid, &io_parms,
4188 &bytes_written, iov, 1);
4189 }
4190 tcon->ses->server->ops->close(xid, tcon, &fid);
4191 d_drop(dentry);
4192
4193 /* FIXME: add code here to set EAs */
4194out:
4195 kfree(buf);
4196 return rc;
4197}
4198
4199
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04004200struct smb_version_operations smb20_operations = {
4201 .compare_fids = smb2_compare_fids,
4202 .setup_request = smb2_setup_request,
4203 .setup_async_request = smb2_setup_async_request,
4204 .check_receive = smb2_check_receive,
4205 .add_credits = smb2_add_credits,
4206 .set_credits = smb2_set_credits,
4207 .get_credits_field = smb2_get_credits_field,
4208 .get_credits = smb2_get_credits,
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04004209 .wait_mtu_credits = cifs_wait_mtu_credits,
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04004210 .get_next_mid = smb2_get_next_mid,
Pavel Shilovskyc781af72019-03-04 14:02:50 -08004211 .revert_current_mid = smb2_revert_current_mid,
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04004212 .read_data_offset = smb2_read_data_offset,
4213 .read_data_length = smb2_read_data_length,
4214 .map_error = map_smb2_to_linux_error,
4215 .find_mid = smb2_find_mid,
4216 .check_message = smb2_check_message,
4217 .dump_detail = smb2_dump_detail,
4218 .clear_stats = smb2_clear_stats,
4219 .print_stats = smb2_print_stats,
4220 .is_oplock_break = smb2_is_valid_oplock_break,
Sachin Prabhu38bd4902017-03-03 15:41:38 -08004221 .handle_cancelled_mid = smb2_handle_cancelled_mid,
Sachin Prabhuc11f1df2014-03-11 16:11:47 +00004222 .downgrade_oplock = smb2_downgrade_oplock,
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04004223 .need_neg = smb2_need_neg,
4224 .negotiate = smb2_negotiate,
4225 .negotiate_wsize = smb2_negotiate_wsize,
4226 .negotiate_rsize = smb2_negotiate_rsize,
4227 .sess_setup = SMB2_sess_setup,
4228 .logoff = SMB2_logoff,
4229 .tree_connect = SMB2_tcon,
4230 .tree_disconnect = SMB2_tdis,
Steve French34f62642013-10-09 02:07:00 -05004231 .qfs_tcon = smb2_qfs_tcon,
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04004232 .is_path_accessible = smb2_is_path_accessible,
4233 .can_echo = smb2_can_echo,
4234 .echo = SMB2_echo,
4235 .query_path_info = smb2_query_path_info,
4236 .get_srv_inum = smb2_get_srv_inum,
4237 .query_file_info = smb2_query_file_info,
4238 .set_path_size = smb2_set_path_size,
4239 .set_file_size = smb2_set_file_size,
4240 .set_file_info = smb2_set_file_info,
Steve French64a5cfa2013-10-14 15:31:32 -05004241 .set_compression = smb2_set_compression,
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04004242 .mkdir = smb2_mkdir,
4243 .mkdir_setinfo = smb2_mkdir_setinfo,
4244 .rmdir = smb2_rmdir,
4245 .unlink = smb2_unlink,
4246 .rename = smb2_rename_path,
4247 .create_hardlink = smb2_create_hardlink,
4248 .query_symlink = smb2_query_symlink,
Sachin Prabhu5b23c972016-07-11 16:53:20 +01004249 .query_mf_symlink = smb3_query_mf_symlink,
4250 .create_mf_symlink = smb3_create_mf_symlink,
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04004251 .open = smb2_open_file,
4252 .set_fid = smb2_set_fid,
4253 .close = smb2_close_file,
4254 .flush = smb2_flush_file,
4255 .async_readv = smb2_async_readv,
4256 .async_writev = smb2_async_writev,
4257 .sync_read = smb2_sync_read,
4258 .sync_write = smb2_sync_write,
4259 .query_dir_first = smb2_query_dir_first,
4260 .query_dir_next = smb2_query_dir_next,
4261 .close_dir = smb2_close_dir,
4262 .calc_smb_size = smb2_calc_size,
4263 .is_status_pending = smb2_is_status_pending,
Pavel Shilovsky511c54a2017-07-08 14:32:00 -07004264 .is_session_expired = smb2_is_session_expired,
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04004265 .oplock_response = smb2_oplock_response,
4266 .queryfs = smb2_queryfs,
4267 .mand_lock = smb2_mand_lock,
4268 .mand_unlock_range = smb2_unlock_range,
4269 .push_mand_locks = smb2_push_mandatory_locks,
4270 .get_lease_key = smb2_get_lease_key,
4271 .set_lease_key = smb2_set_lease_key,
4272 .new_lease_key = smb2_new_lease_key,
4273 .calc_signature = smb2_calc_signature,
4274 .is_read_op = smb2_is_read_op,
4275 .set_oplock_level = smb2_set_oplock_level,
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04004276 .create_lease_buf = smb2_create_lease_buf,
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04004277 .parse_lease_buf = smb2_parse_lease_buf,
Sachin Prabhu312bbc52017-04-04 02:12:04 -05004278 .copychunk_range = smb2_copychunk_range,
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04004279 .wp_retry_size = smb2_wp_retry_size,
Pavel Shilovsky52755802014-08-18 20:49:57 +04004280 .dir_needs_close = smb2_dir_needs_close,
Aurelien Aptel9d496402017-02-13 16:16:49 +01004281 .get_dfs_refer = smb2_get_dfs_refer,
Sachin Prabhuef65aae2017-01-18 15:35:57 +05304282 .select_sectype = smb2_select_sectype,
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10004283#ifdef CONFIG_CIFS_XATTR
4284 .query_all_EAs = smb2_query_eas,
Ronnie Sahlberg55175542017-08-24 11:24:56 +10004285 .set_EA = smb2_set_ea,
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10004286#endif /* CIFS_XATTR */
Shirish Pargaonkar2f1afe22017-06-22 22:52:05 -05004287#ifdef CONFIG_CIFS_ACL
4288 .get_acl = get_smb2_acl,
4289 .get_acl_by_fid = get_smb2_acl_by_fid,
Shirish Pargaonkar366ed842017-06-28 22:37:32 -05004290 .set_acl = set_smb2_acl,
Shirish Pargaonkar2f1afe22017-06-22 22:52:05 -05004291#endif /* CIFS_ACL */
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +10004292 .next_header = smb2_next_header,
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05004293 .ioctl_query_info = smb2_ioctl_query_info,
Aurelien Aptelc847dcc2019-03-14 00:29:17 -05004294 .make_node = smb2_make_node,
Ronnie Sahlberg2f3ebab2019-04-25 16:45:29 +10004295 .fiemap = smb3_fiemap,
Ronnie Sahlbergdece44e2019-05-15 07:17:02 +10004296 .llseek = smb3_llseek,
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04004297};
4298
Steve French1080ef72011-02-24 18:07:19 +00004299struct smb_version_operations smb21_operations = {
Pavel Shilovsky027e8ee2012-09-19 06:22:43 -07004300 .compare_fids = smb2_compare_fids,
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +04004301 .setup_request = smb2_setup_request,
Pavel Shilovskyc95b8ee2012-07-11 14:45:28 +04004302 .setup_async_request = smb2_setup_async_request,
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +04004303 .check_receive = smb2_check_receive,
Pavel Shilovsky28ea5292012-05-23 16:18:00 +04004304 .add_credits = smb2_add_credits,
4305 .set_credits = smb2_set_credits,
4306 .get_credits_field = smb2_get_credits_field,
4307 .get_credits = smb2_get_credits,
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04004308 .wait_mtu_credits = smb2_wait_mtu_credits,
Pavel Shilovsky9a1c67e2019-01-23 18:15:52 -08004309 .adjust_credits = smb2_adjust_credits,
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +04004310 .get_next_mid = smb2_get_next_mid,
Pavel Shilovskyc781af72019-03-04 14:02:50 -08004311 .revert_current_mid = smb2_revert_current_mid,
Pavel Shilovsky09a47072012-09-18 16:20:29 -07004312 .read_data_offset = smb2_read_data_offset,
4313 .read_data_length = smb2_read_data_length,
4314 .map_error = map_smb2_to_linux_error,
Pavel Shilovsky093b2bd2011-06-08 15:51:07 +04004315 .find_mid = smb2_find_mid,
4316 .check_message = smb2_check_message,
4317 .dump_detail = smb2_dump_detail,
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04004318 .clear_stats = smb2_clear_stats,
4319 .print_stats = smb2_print_stats,
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07004320 .is_oplock_break = smb2_is_valid_oplock_break,
Sachin Prabhu38bd4902017-03-03 15:41:38 -08004321 .handle_cancelled_mid = smb2_handle_cancelled_mid,
Pavel Shilovsky7b9b9ed2019-02-13 15:43:08 -08004322 .downgrade_oplock = smb21_downgrade_oplock,
Pavel Shilovskyec2e4522011-12-27 16:12:43 +04004323 .need_neg = smb2_need_neg,
4324 .negotiate = smb2_negotiate,
Pavel Shilovsky3a3bab52012-09-18 16:20:28 -07004325 .negotiate_wsize = smb2_negotiate_wsize,
4326 .negotiate_rsize = smb2_negotiate_rsize,
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04004327 .sess_setup = SMB2_sess_setup,
4328 .logoff = SMB2_logoff,
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04004329 .tree_connect = SMB2_tcon,
4330 .tree_disconnect = SMB2_tdis,
Steve French34f62642013-10-09 02:07:00 -05004331 .qfs_tcon = smb2_qfs_tcon,
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04004332 .is_path_accessible = smb2_is_path_accessible,
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04004333 .can_echo = smb2_can_echo,
4334 .echo = SMB2_echo,
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04004335 .query_path_info = smb2_query_path_info,
4336 .get_srv_inum = smb2_get_srv_inum,
Pavel Shilovskyb7546bc2012-09-18 16:20:27 -07004337 .query_file_info = smb2_query_file_info,
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07004338 .set_path_size = smb2_set_path_size,
4339 .set_file_size = smb2_set_file_size,
Pavel Shilovsky1feeaac2012-09-18 16:20:32 -07004340 .set_file_info = smb2_set_file_info,
Steve French64a5cfa2013-10-14 15:31:32 -05004341 .set_compression = smb2_set_compression,
Pavel Shilovskya0e73182011-07-19 12:56:37 +04004342 .mkdir = smb2_mkdir,
4343 .mkdir_setinfo = smb2_mkdir_setinfo,
Pavel Shilovsky1a500f02012-07-10 16:14:38 +04004344 .rmdir = smb2_rmdir,
Pavel Shilovskycbe6f432012-09-18 16:20:25 -07004345 .unlink = smb2_unlink,
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004346 .rename = smb2_rename_path,
Pavel Shilovsky568798c2012-09-18 16:20:31 -07004347 .create_hardlink = smb2_create_hardlink,
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04004348 .query_symlink = smb2_query_symlink,
Steve Frenchc22870e2014-09-16 07:18:19 -05004349 .query_mf_symlink = smb3_query_mf_symlink,
Steve French5ab97572014-09-15 04:49:28 -05004350 .create_mf_symlink = smb3_create_mf_symlink,
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07004351 .open = smb2_open_file,
4352 .set_fid = smb2_set_fid,
4353 .close = smb2_close_file,
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07004354 .flush = smb2_flush_file,
Pavel Shilovsky09a47072012-09-18 16:20:29 -07004355 .async_readv = smb2_async_readv,
Pavel Shilovsky33319142012-09-18 16:20:29 -07004356 .async_writev = smb2_async_writev,
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07004357 .sync_read = smb2_sync_read,
Pavel Shilovsky009d3442012-09-18 16:20:30 -07004358 .sync_write = smb2_sync_write,
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004359 .query_dir_first = smb2_query_dir_first,
4360 .query_dir_next = smb2_query_dir_next,
4361 .close_dir = smb2_close_dir,
4362 .calc_smb_size = smb2_calc_size,
Pavel Shilovsky2e44b282012-09-18 16:20:33 -07004363 .is_status_pending = smb2_is_status_pending,
Pavel Shilovsky511c54a2017-07-08 14:32:00 -07004364 .is_session_expired = smb2_is_session_expired,
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07004365 .oplock_response = smb2_oplock_response,
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004366 .queryfs = smb2_queryfs,
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07004367 .mand_lock = smb2_mand_lock,
4368 .mand_unlock_range = smb2_unlock_range,
Pavel Shilovskyb1407992012-09-19 06:22:44 -07004369 .push_mand_locks = smb2_push_mandatory_locks,
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07004370 .get_lease_key = smb2_get_lease_key,
4371 .set_lease_key = smb2_set_lease_key,
4372 .new_lease_key = smb2_new_lease_key,
Steve French38107d42012-12-08 22:08:06 -06004373 .calc_signature = smb2_calc_signature,
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04004374 .is_read_op = smb21_is_read_op,
4375 .set_oplock_level = smb21_set_oplock_level,
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04004376 .create_lease_buf = smb2_create_lease_buf,
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04004377 .parse_lease_buf = smb2_parse_lease_buf,
Sachin Prabhu312bbc52017-04-04 02:12:04 -05004378 .copychunk_range = smb2_copychunk_range,
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04004379 .wp_retry_size = smb2_wp_retry_size,
Pavel Shilovsky52755802014-08-18 20:49:57 +04004380 .dir_needs_close = smb2_dir_needs_close,
Steve French834170c2016-09-30 21:14:26 -05004381 .enum_snapshots = smb3_enum_snapshots,
Aurelien Aptel9d496402017-02-13 16:16:49 +01004382 .get_dfs_refer = smb2_get_dfs_refer,
Sachin Prabhuef65aae2017-01-18 15:35:57 +05304383 .select_sectype = smb2_select_sectype,
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10004384#ifdef CONFIG_CIFS_XATTR
4385 .query_all_EAs = smb2_query_eas,
Ronnie Sahlberg55175542017-08-24 11:24:56 +10004386 .set_EA = smb2_set_ea,
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10004387#endif /* CIFS_XATTR */
Shirish Pargaonkar2f1afe22017-06-22 22:52:05 -05004388#ifdef CONFIG_CIFS_ACL
4389 .get_acl = get_smb2_acl,
4390 .get_acl_by_fid = get_smb2_acl_by_fid,
Shirish Pargaonkar366ed842017-06-28 22:37:32 -05004391 .set_acl = set_smb2_acl,
Shirish Pargaonkar2f1afe22017-06-22 22:52:05 -05004392#endif /* CIFS_ACL */
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +10004393 .next_header = smb2_next_header,
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05004394 .ioctl_query_info = smb2_ioctl_query_info,
Aurelien Aptelc847dcc2019-03-14 00:29:17 -05004395 .make_node = smb2_make_node,
Ronnie Sahlberg2f3ebab2019-04-25 16:45:29 +10004396 .fiemap = smb3_fiemap,
Ronnie Sahlbergdece44e2019-05-15 07:17:02 +10004397 .llseek = smb3_llseek,
Steve French38107d42012-12-08 22:08:06 -06004398};
4399
Steve French38107d42012-12-08 22:08:06 -06004400struct smb_version_operations smb30_operations = {
4401 .compare_fids = smb2_compare_fids,
4402 .setup_request = smb2_setup_request,
4403 .setup_async_request = smb2_setup_async_request,
4404 .check_receive = smb2_check_receive,
4405 .add_credits = smb2_add_credits,
4406 .set_credits = smb2_set_credits,
4407 .get_credits_field = smb2_get_credits_field,
4408 .get_credits = smb2_get_credits,
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04004409 .wait_mtu_credits = smb2_wait_mtu_credits,
Pavel Shilovsky9a1c67e2019-01-23 18:15:52 -08004410 .adjust_credits = smb2_adjust_credits,
Steve French38107d42012-12-08 22:08:06 -06004411 .get_next_mid = smb2_get_next_mid,
Pavel Shilovskyc781af72019-03-04 14:02:50 -08004412 .revert_current_mid = smb2_revert_current_mid,
Steve French38107d42012-12-08 22:08:06 -06004413 .read_data_offset = smb2_read_data_offset,
4414 .read_data_length = smb2_read_data_length,
4415 .map_error = map_smb2_to_linux_error,
4416 .find_mid = smb2_find_mid,
4417 .check_message = smb2_check_message,
4418 .dump_detail = smb2_dump_detail,
4419 .clear_stats = smb2_clear_stats,
4420 .print_stats = smb2_print_stats,
Steve French769ee6a2013-06-19 14:15:30 -05004421 .dump_share_caps = smb2_dump_share_caps,
Steve French38107d42012-12-08 22:08:06 -06004422 .is_oplock_break = smb2_is_valid_oplock_break,
Sachin Prabhu38bd4902017-03-03 15:41:38 -08004423 .handle_cancelled_mid = smb2_handle_cancelled_mid,
Pavel Shilovsky7b9b9ed2019-02-13 15:43:08 -08004424 .downgrade_oplock = smb21_downgrade_oplock,
Steve French38107d42012-12-08 22:08:06 -06004425 .need_neg = smb2_need_neg,
4426 .negotiate = smb2_negotiate,
Steve French3d621232018-09-25 15:33:47 -05004427 .negotiate_wsize = smb3_negotiate_wsize,
4428 .negotiate_rsize = smb3_negotiate_rsize,
Steve French38107d42012-12-08 22:08:06 -06004429 .sess_setup = SMB2_sess_setup,
4430 .logoff = SMB2_logoff,
4431 .tree_connect = SMB2_tcon,
4432 .tree_disconnect = SMB2_tdis,
Steven Frenchaf6a12e2013-10-09 20:55:53 -05004433 .qfs_tcon = smb3_qfs_tcon,
Steve French38107d42012-12-08 22:08:06 -06004434 .is_path_accessible = smb2_is_path_accessible,
4435 .can_echo = smb2_can_echo,
4436 .echo = SMB2_echo,
4437 .query_path_info = smb2_query_path_info,
4438 .get_srv_inum = smb2_get_srv_inum,
4439 .query_file_info = smb2_query_file_info,
4440 .set_path_size = smb2_set_path_size,
4441 .set_file_size = smb2_set_file_size,
4442 .set_file_info = smb2_set_file_info,
Steve French64a5cfa2013-10-14 15:31:32 -05004443 .set_compression = smb2_set_compression,
Steve French38107d42012-12-08 22:08:06 -06004444 .mkdir = smb2_mkdir,
4445 .mkdir_setinfo = smb2_mkdir_setinfo,
4446 .rmdir = smb2_rmdir,
4447 .unlink = smb2_unlink,
4448 .rename = smb2_rename_path,
4449 .create_hardlink = smb2_create_hardlink,
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04004450 .query_symlink = smb2_query_symlink,
Steve Frenchc22870e2014-09-16 07:18:19 -05004451 .query_mf_symlink = smb3_query_mf_symlink,
Steve French5ab97572014-09-15 04:49:28 -05004452 .create_mf_symlink = smb3_create_mf_symlink,
Steve French38107d42012-12-08 22:08:06 -06004453 .open = smb2_open_file,
4454 .set_fid = smb2_set_fid,
4455 .close = smb2_close_file,
4456 .flush = smb2_flush_file,
4457 .async_readv = smb2_async_readv,
4458 .async_writev = smb2_async_writev,
4459 .sync_read = smb2_sync_read,
4460 .sync_write = smb2_sync_write,
4461 .query_dir_first = smb2_query_dir_first,
4462 .query_dir_next = smb2_query_dir_next,
4463 .close_dir = smb2_close_dir,
4464 .calc_smb_size = smb2_calc_size,
4465 .is_status_pending = smb2_is_status_pending,
Pavel Shilovsky511c54a2017-07-08 14:32:00 -07004466 .is_session_expired = smb2_is_session_expired,
Steve French38107d42012-12-08 22:08:06 -06004467 .oplock_response = smb2_oplock_response,
4468 .queryfs = smb2_queryfs,
4469 .mand_lock = smb2_mand_lock,
4470 .mand_unlock_range = smb2_unlock_range,
4471 .push_mand_locks = smb2_push_mandatory_locks,
4472 .get_lease_key = smb2_get_lease_key,
4473 .set_lease_key = smb2_set_lease_key,
4474 .new_lease_key = smb2_new_lease_key,
Steve French373512e2015-12-18 13:05:30 -06004475 .generate_signingkey = generate_smb30signingkey,
Steve French38107d42012-12-08 22:08:06 -06004476 .calc_signature = smb3_calc_signature,
Steve Frenchb3152e22015-06-24 03:17:02 -05004477 .set_integrity = smb3_set_integrity,
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04004478 .is_read_op = smb21_is_read_op,
Pavel Shilovsky42873b02013-09-05 21:30:16 +04004479 .set_oplock_level = smb3_set_oplock_level,
Pavel Shilovskyf0473902013-09-04 13:44:05 +04004480 .create_lease_buf = smb3_create_lease_buf,
4481 .parse_lease_buf = smb3_parse_lease_buf,
Sachin Prabhu312bbc52017-04-04 02:12:04 -05004482 .copychunk_range = smb2_copychunk_range,
Steve Frenchca9e7a12015-10-01 21:40:10 -05004483 .duplicate_extents = smb2_duplicate_extents,
Steve Frenchff1c0382013-11-19 23:44:46 -06004484 .validate_negotiate = smb3_validate_negotiate,
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04004485 .wp_retry_size = smb2_wp_retry_size,
Pavel Shilovsky52755802014-08-18 20:49:57 +04004486 .dir_needs_close = smb2_dir_needs_close,
Steve French31742c52014-08-17 08:38:47 -05004487 .fallocate = smb3_fallocate,
Steve French834170c2016-09-30 21:14:26 -05004488 .enum_snapshots = smb3_enum_snapshots,
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07004489 .init_transform_rq = smb3_init_transform_rq,
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004490 .is_transform_hdr = smb3_is_transform_hdr,
4491 .receive_transform = smb3_receive_transform,
Aurelien Aptel9d496402017-02-13 16:16:49 +01004492 .get_dfs_refer = smb2_get_dfs_refer,
Sachin Prabhuef65aae2017-01-18 15:35:57 +05304493 .select_sectype = smb2_select_sectype,
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10004494#ifdef CONFIG_CIFS_XATTR
4495 .query_all_EAs = smb2_query_eas,
Ronnie Sahlberg55175542017-08-24 11:24:56 +10004496 .set_EA = smb2_set_ea,
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10004497#endif /* CIFS_XATTR */
Shirish Pargaonkar2f1afe22017-06-22 22:52:05 -05004498#ifdef CONFIG_CIFS_ACL
4499 .get_acl = get_smb2_acl,
4500 .get_acl_by_fid = get_smb2_acl_by_fid,
Shirish Pargaonkar366ed842017-06-28 22:37:32 -05004501 .set_acl = set_smb2_acl,
Shirish Pargaonkar2f1afe22017-06-22 22:52:05 -05004502#endif /* CIFS_ACL */
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +10004503 .next_header = smb2_next_header,
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05004504 .ioctl_query_info = smb2_ioctl_query_info,
Aurelien Aptelc847dcc2019-03-14 00:29:17 -05004505 .make_node = smb2_make_node,
Ronnie Sahlberg2f3ebab2019-04-25 16:45:29 +10004506 .fiemap = smb3_fiemap,
Ronnie Sahlbergdece44e2019-05-15 07:17:02 +10004507 .llseek = smb3_llseek,
Steve French1080ef72011-02-24 18:07:19 +00004508};
4509
Steve Frenchaab18932015-06-23 23:37:11 -05004510struct smb_version_operations smb311_operations = {
4511 .compare_fids = smb2_compare_fids,
4512 .setup_request = smb2_setup_request,
4513 .setup_async_request = smb2_setup_async_request,
4514 .check_receive = smb2_check_receive,
4515 .add_credits = smb2_add_credits,
4516 .set_credits = smb2_set_credits,
4517 .get_credits_field = smb2_get_credits_field,
4518 .get_credits = smb2_get_credits,
4519 .wait_mtu_credits = smb2_wait_mtu_credits,
Pavel Shilovsky9a1c67e2019-01-23 18:15:52 -08004520 .adjust_credits = smb2_adjust_credits,
Steve Frenchaab18932015-06-23 23:37:11 -05004521 .get_next_mid = smb2_get_next_mid,
Pavel Shilovskyc781af72019-03-04 14:02:50 -08004522 .revert_current_mid = smb2_revert_current_mid,
Steve Frenchaab18932015-06-23 23:37:11 -05004523 .read_data_offset = smb2_read_data_offset,
4524 .read_data_length = smb2_read_data_length,
4525 .map_error = map_smb2_to_linux_error,
4526 .find_mid = smb2_find_mid,
4527 .check_message = smb2_check_message,
4528 .dump_detail = smb2_dump_detail,
4529 .clear_stats = smb2_clear_stats,
4530 .print_stats = smb2_print_stats,
4531 .dump_share_caps = smb2_dump_share_caps,
4532 .is_oplock_break = smb2_is_valid_oplock_break,
Sachin Prabhu38bd4902017-03-03 15:41:38 -08004533 .handle_cancelled_mid = smb2_handle_cancelled_mid,
Pavel Shilovsky7b9b9ed2019-02-13 15:43:08 -08004534 .downgrade_oplock = smb21_downgrade_oplock,
Steve Frenchaab18932015-06-23 23:37:11 -05004535 .need_neg = smb2_need_neg,
4536 .negotiate = smb2_negotiate,
Steve French3d621232018-09-25 15:33:47 -05004537 .negotiate_wsize = smb3_negotiate_wsize,
4538 .negotiate_rsize = smb3_negotiate_rsize,
Steve Frenchaab18932015-06-23 23:37:11 -05004539 .sess_setup = SMB2_sess_setup,
4540 .logoff = SMB2_logoff,
4541 .tree_connect = SMB2_tcon,
4542 .tree_disconnect = SMB2_tdis,
4543 .qfs_tcon = smb3_qfs_tcon,
4544 .is_path_accessible = smb2_is_path_accessible,
4545 .can_echo = smb2_can_echo,
4546 .echo = SMB2_echo,
4547 .query_path_info = smb2_query_path_info,
4548 .get_srv_inum = smb2_get_srv_inum,
4549 .query_file_info = smb2_query_file_info,
4550 .set_path_size = smb2_set_path_size,
4551 .set_file_size = smb2_set_file_size,
4552 .set_file_info = smb2_set_file_info,
4553 .set_compression = smb2_set_compression,
4554 .mkdir = smb2_mkdir,
4555 .mkdir_setinfo = smb2_mkdir_setinfo,
Steve Frenchbea851b2018-06-14 21:56:32 -05004556 .posix_mkdir = smb311_posix_mkdir,
Steve Frenchaab18932015-06-23 23:37:11 -05004557 .rmdir = smb2_rmdir,
4558 .unlink = smb2_unlink,
4559 .rename = smb2_rename_path,
4560 .create_hardlink = smb2_create_hardlink,
4561 .query_symlink = smb2_query_symlink,
4562 .query_mf_symlink = smb3_query_mf_symlink,
4563 .create_mf_symlink = smb3_create_mf_symlink,
4564 .open = smb2_open_file,
4565 .set_fid = smb2_set_fid,
4566 .close = smb2_close_file,
4567 .flush = smb2_flush_file,
4568 .async_readv = smb2_async_readv,
4569 .async_writev = smb2_async_writev,
4570 .sync_read = smb2_sync_read,
4571 .sync_write = smb2_sync_write,
4572 .query_dir_first = smb2_query_dir_first,
4573 .query_dir_next = smb2_query_dir_next,
4574 .close_dir = smb2_close_dir,
4575 .calc_smb_size = smb2_calc_size,
4576 .is_status_pending = smb2_is_status_pending,
Pavel Shilovsky511c54a2017-07-08 14:32:00 -07004577 .is_session_expired = smb2_is_session_expired,
Steve Frenchaab18932015-06-23 23:37:11 -05004578 .oplock_response = smb2_oplock_response,
Steve French2d304212018-06-24 23:28:12 -05004579 .queryfs = smb311_queryfs,
Steve Frenchaab18932015-06-23 23:37:11 -05004580 .mand_lock = smb2_mand_lock,
4581 .mand_unlock_range = smb2_unlock_range,
4582 .push_mand_locks = smb2_push_mandatory_locks,
4583 .get_lease_key = smb2_get_lease_key,
4584 .set_lease_key = smb2_set_lease_key,
4585 .new_lease_key = smb2_new_lease_key,
Steve French373512e2015-12-18 13:05:30 -06004586 .generate_signingkey = generate_smb311signingkey,
Steve Frenchaab18932015-06-23 23:37:11 -05004587 .calc_signature = smb3_calc_signature,
Steve Frenchb3152e22015-06-24 03:17:02 -05004588 .set_integrity = smb3_set_integrity,
Steve Frenchaab18932015-06-23 23:37:11 -05004589 .is_read_op = smb21_is_read_op,
4590 .set_oplock_level = smb3_set_oplock_level,
4591 .create_lease_buf = smb3_create_lease_buf,
4592 .parse_lease_buf = smb3_parse_lease_buf,
Sachin Prabhu312bbc52017-04-04 02:12:04 -05004593 .copychunk_range = smb2_copychunk_range,
Steve French02b16662015-06-27 21:18:36 -07004594 .duplicate_extents = smb2_duplicate_extents,
Steve Frenchaab18932015-06-23 23:37:11 -05004595/* .validate_negotiate = smb3_validate_negotiate, */ /* not used in 3.11 */
4596 .wp_retry_size = smb2_wp_retry_size,
4597 .dir_needs_close = smb2_dir_needs_close,
4598 .fallocate = smb3_fallocate,
Steve French834170c2016-09-30 21:14:26 -05004599 .enum_snapshots = smb3_enum_snapshots,
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07004600 .init_transform_rq = smb3_init_transform_rq,
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004601 .is_transform_hdr = smb3_is_transform_hdr,
4602 .receive_transform = smb3_receive_transform,
Aurelien Aptel9d496402017-02-13 16:16:49 +01004603 .get_dfs_refer = smb2_get_dfs_refer,
Sachin Prabhuef65aae2017-01-18 15:35:57 +05304604 .select_sectype = smb2_select_sectype,
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10004605#ifdef CONFIG_CIFS_XATTR
4606 .query_all_EAs = smb2_query_eas,
Ronnie Sahlberg55175542017-08-24 11:24:56 +10004607 .set_EA = smb2_set_ea,
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10004608#endif /* CIFS_XATTR */
Ronnie Sahlbergc1777df2018-08-10 11:03:55 +10004609#ifdef CONFIG_CIFS_ACL
4610 .get_acl = get_smb2_acl,
4611 .get_acl_by_fid = get_smb2_acl_by_fid,
4612 .set_acl = set_smb2_acl,
4613#endif /* CIFS_ACL */
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +10004614 .next_header = smb2_next_header,
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05004615 .ioctl_query_info = smb2_ioctl_query_info,
Aurelien Aptelc847dcc2019-03-14 00:29:17 -05004616 .make_node = smb2_make_node,
Ronnie Sahlberg2f3ebab2019-04-25 16:45:29 +10004617 .fiemap = smb3_fiemap,
Ronnie Sahlbergdece44e2019-05-15 07:17:02 +10004618 .llseek = smb3_llseek,
Steve Frenchaab18932015-06-23 23:37:11 -05004619};
Steve Frenchaab18932015-06-23 23:37:11 -05004620
Steve Frenchdd446b12012-11-28 23:21:06 -06004621struct smb_version_values smb20_values = {
4622 .version_string = SMB20_VERSION_STRING,
4623 .protocol_id = SMB20_PROT_ID,
4624 .req_capabilities = 0, /* MBZ */
4625 .large_lock_type = 0,
4626 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
4627 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
4628 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10004629 .header_size = sizeof(struct smb2_sync_hdr),
4630 .header_preamble_size = 0,
Steve Frenchdd446b12012-11-28 23:21:06 -06004631 .max_header_size = MAX_SMB2_HDR_SIZE,
4632 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
4633 .lock_cmd = SMB2_LOCK,
4634 .cap_unix = 0,
4635 .cap_nt_find = SMB2_NT_FIND,
4636 .cap_large_files = SMB2_LARGE_FILES,
Jeff Layton502858822013-06-27 12:45:00 -04004637 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
4638 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04004639 .create_lease_size = sizeof(struct create_lease),
Steve Frenchdd446b12012-11-28 23:21:06 -06004640};
4641
Steve French1080ef72011-02-24 18:07:19 +00004642struct smb_version_values smb21_values = {
4643 .version_string = SMB21_VERSION_STRING,
Steve Frenche4aa25e2012-10-01 12:26:22 -05004644 .protocol_id = SMB21_PROT_ID,
4645 .req_capabilities = 0, /* MBZ on negotiate req until SMB3 dialect */
4646 .large_lock_type = 0,
4647 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
4648 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
4649 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10004650 .header_size = sizeof(struct smb2_sync_hdr),
4651 .header_preamble_size = 0,
Steve Frenche4aa25e2012-10-01 12:26:22 -05004652 .max_header_size = MAX_SMB2_HDR_SIZE,
4653 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
4654 .lock_cmd = SMB2_LOCK,
4655 .cap_unix = 0,
4656 .cap_nt_find = SMB2_NT_FIND,
4657 .cap_large_files = SMB2_LARGE_FILES,
Jeff Layton502858822013-06-27 12:45:00 -04004658 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
4659 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04004660 .create_lease_size = sizeof(struct create_lease),
Steve Frenche4aa25e2012-10-01 12:26:22 -05004661};
4662
Steve French9764c022017-09-17 10:41:35 -05004663struct smb_version_values smb3any_values = {
4664 .version_string = SMB3ANY_VERSION_STRING,
4665 .protocol_id = SMB302_PROT_ID, /* doesn't matter, send protocol array */
Steve Frenchf8015682018-08-31 15:12:10 -05004666 .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,
Steve French9764c022017-09-17 10:41:35 -05004667 .large_lock_type = 0,
4668 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
4669 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
4670 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10004671 .header_size = sizeof(struct smb2_sync_hdr),
4672 .header_preamble_size = 0,
Steve French9764c022017-09-17 10:41:35 -05004673 .max_header_size = MAX_SMB2_HDR_SIZE,
4674 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
4675 .lock_cmd = SMB2_LOCK,
4676 .cap_unix = 0,
4677 .cap_nt_find = SMB2_NT_FIND,
4678 .cap_large_files = SMB2_LARGE_FILES,
4679 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
4680 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
4681 .create_lease_size = sizeof(struct create_lease_v2),
4682};
4683
4684struct smb_version_values smbdefault_values = {
4685 .version_string = SMBDEFAULT_VERSION_STRING,
4686 .protocol_id = SMB302_PROT_ID, /* doesn't matter, send protocol array */
Steve Frenchf8015682018-08-31 15:12:10 -05004687 .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,
Steve French9764c022017-09-17 10:41:35 -05004688 .large_lock_type = 0,
4689 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
4690 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
4691 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10004692 .header_size = sizeof(struct smb2_sync_hdr),
4693 .header_preamble_size = 0,
Steve French9764c022017-09-17 10:41:35 -05004694 .max_header_size = MAX_SMB2_HDR_SIZE,
4695 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
4696 .lock_cmd = SMB2_LOCK,
4697 .cap_unix = 0,
4698 .cap_nt_find = SMB2_NT_FIND,
4699 .cap_large_files = SMB2_LARGE_FILES,
4700 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
4701 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
4702 .create_lease_size = sizeof(struct create_lease_v2),
4703};
4704
Steve Frenche4aa25e2012-10-01 12:26:22 -05004705struct smb_version_values smb30_values = {
4706 .version_string = SMB30_VERSION_STRING,
4707 .protocol_id = SMB30_PROT_ID,
Steve Frenchf8015682018-08-31 15:12:10 -05004708 .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,
Pavel Shilovsky027e8ee2012-09-19 06:22:43 -07004709 .large_lock_type = 0,
4710 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
4711 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
4712 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10004713 .header_size = sizeof(struct smb2_sync_hdr),
4714 .header_preamble_size = 0,
Pavel Shilovsky093b2bd2011-06-08 15:51:07 +04004715 .max_header_size = MAX_SMB2_HDR_SIZE,
Pavel Shilovsky09a47072012-09-18 16:20:29 -07004716 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +04004717 .lock_cmd = SMB2_LOCK,
Pavel Shilovsky29e20f92012-07-13 13:58:14 +04004718 .cap_unix = 0,
4719 .cap_nt_find = SMB2_NT_FIND,
4720 .cap_large_files = SMB2_LARGE_FILES,
Jeff Layton502858822013-06-27 12:45:00 -04004721 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
4722 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
Pavel Shilovskyf0473902013-09-04 13:44:05 +04004723 .create_lease_size = sizeof(struct create_lease_v2),
Steve French1080ef72011-02-24 18:07:19 +00004724};
Steve French20b6d8b2013-06-12 22:48:41 -05004725
4726struct smb_version_values smb302_values = {
4727 .version_string = SMB302_VERSION_STRING,
4728 .protocol_id = SMB302_PROT_ID,
Steve Frenchf8015682018-08-31 15:12:10 -05004729 .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,
Steve French20b6d8b2013-06-12 22:48:41 -05004730 .large_lock_type = 0,
4731 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
4732 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
4733 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10004734 .header_size = sizeof(struct smb2_sync_hdr),
4735 .header_preamble_size = 0,
Steve French20b6d8b2013-06-12 22:48:41 -05004736 .max_header_size = MAX_SMB2_HDR_SIZE,
4737 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
4738 .lock_cmd = SMB2_LOCK,
4739 .cap_unix = 0,
4740 .cap_nt_find = SMB2_NT_FIND,
4741 .cap_large_files = SMB2_LARGE_FILES,
Jeff Layton502858822013-06-27 12:45:00 -04004742 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
4743 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
Pavel Shilovskyf0473902013-09-04 13:44:05 +04004744 .create_lease_size = sizeof(struct create_lease_v2),
Steve French20b6d8b2013-06-12 22:48:41 -05004745};
Steve French5f7fbf72014-12-17 22:52:58 -06004746
Steve French5f7fbf72014-12-17 22:52:58 -06004747struct smb_version_values smb311_values = {
4748 .version_string = SMB311_VERSION_STRING,
4749 .protocol_id = SMB311_PROT_ID,
Steve Frenchf8015682018-08-31 15:12:10 -05004750 .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,
Steve French5f7fbf72014-12-17 22:52:58 -06004751 .large_lock_type = 0,
4752 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
4753 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
4754 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10004755 .header_size = sizeof(struct smb2_sync_hdr),
4756 .header_preamble_size = 0,
Steve French5f7fbf72014-12-17 22:52:58 -06004757 .max_header_size = MAX_SMB2_HDR_SIZE,
4758 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
4759 .lock_cmd = SMB2_LOCK,
4760 .cap_unix = 0,
4761 .cap_nt_find = SMB2_NT_FIND,
4762 .cap_large_files = SMB2_LARGE_FILES,
4763 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
4764 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
4765 .create_lease_size = sizeof(struct create_lease_v2),
4766};