blob: 6787fce26f2080d1a8284b3a3bd0a00c20bfa0fa [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>
Aurelien Aptel35adffe2019-09-20 06:29:39 +020013#include <linux/sort.h>
Pavel Shilovsky026e93d2016-11-03 16:47:37 -070014#include <crypto/aead.h>
Ronnie Sahlberg8bd0d702020-01-17 11:45:02 +100015#include "cifsfs.h"
Steve French1080ef72011-02-24 18:07:19 +000016#include "cifsglob.h"
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +040017#include "smb2pdu.h"
18#include "smb2proto.h"
Pavel Shilovsky28ea5292012-05-23 16:18:00 +040019#include "cifsproto.h"
20#include "cifs_debug.h"
Pavel Shilovskyb42bf882013-08-14 19:25:21 +040021#include "cifs_unicode.h"
Pavel Shilovsky2e44b282012-09-18 16:20:33 -070022#include "smb2status.h"
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -070023#include "smb2glob.h"
Steve French834170c2016-09-30 21:14:26 -050024#include "cifs_ioctl.h"
Long Li09902f82017-11-22 17:38:39 -070025#include "smbdirect.h"
Pavel Shilovsky28ea5292012-05-23 16:18:00 +040026
Pavel Shilovskyef68e832019-01-18 17:25:36 -080027/* Change credits for different ops and return the total number of credits */
Pavel Shilovsky28ea5292012-05-23 16:18:00 +040028static int
29change_conf(struct TCP_Server_Info *server)
30{
31 server->credits += server->echo_credits + server->oplock_credits;
32 server->oplock_credits = server->echo_credits = 0;
33 switch (server->credits) {
34 case 0:
Pavel Shilovskyef68e832019-01-18 17:25:36 -080035 return 0;
Pavel Shilovsky28ea5292012-05-23 16:18:00 +040036 case 1:
37 server->echoes = false;
38 server->oplocks = false;
Pavel Shilovsky28ea5292012-05-23 16:18:00 +040039 break;
40 case 2:
41 server->echoes = true;
42 server->oplocks = false;
43 server->echo_credits = 1;
Pavel Shilovsky28ea5292012-05-23 16:18:00 +040044 break;
45 default:
46 server->echoes = true;
Steve Frenche0ddde92015-09-22 09:29:38 -050047 if (enable_oplocks) {
48 server->oplocks = true;
49 server->oplock_credits = 1;
50 } else
51 server->oplocks = false;
52
Pavel Shilovsky28ea5292012-05-23 16:18:00 +040053 server->echo_credits = 1;
Pavel Shilovsky28ea5292012-05-23 16:18:00 +040054 }
55 server->credits -= server->echo_credits + server->oplock_credits;
Pavel Shilovskyef68e832019-01-18 17:25:36 -080056 return server->credits + server->echo_credits + server->oplock_credits;
Pavel Shilovsky28ea5292012-05-23 16:18:00 +040057}
58
59static void
Pavel Shilovsky335b7b62019-01-16 11:12:41 -080060smb2_add_credits(struct TCP_Server_Info *server,
61 const struct cifs_credits *credits, const int optype)
Pavel Shilovsky28ea5292012-05-23 16:18:00 +040062{
Pavel Shilovskyef68e832019-01-18 17:25:36 -080063 int *val, rc = -1;
Pavel Shilovsky335b7b62019-01-16 11:12:41 -080064 unsigned int add = credits->value;
65 unsigned int instance = credits->instance;
66 bool reconnect_detected = false;
Pavel Shilovskyef68e832019-01-18 17:25:36 -080067
Pavel Shilovsky28ea5292012-05-23 16:18:00 +040068 spin_lock(&server->req_lock);
69 val = server->ops->get_credits_field(server, optype);
Steve Frenchb340a4d2018-09-01 01:10:17 -050070
71 /* eg found case where write overlapping reconnect messed up credits */
72 if (((optype & CIFS_OP_MASK) == CIFS_NEG_OP) && (*val != 0))
73 trace_smb3_reconnect_with_invalid_credits(server->CurrentMid,
74 server->hostname, *val);
Pavel Shilovsky335b7b62019-01-16 11:12:41 -080075 if ((instance == 0) || (instance == server->reconnect_instance))
76 *val += add;
77 else
78 reconnect_detected = true;
Steve Frenchb340a4d2018-09-01 01:10:17 -050079
Steve French141891f2016-09-23 00:44:16 -050080 if (*val > 65000) {
81 *val = 65000; /* Don't get near 64K credits, avoid srv bugs */
82 printk_once(KERN_WARNING "server overflowed SMB3 credits\n");
83 }
Pavel Shilovsky28ea5292012-05-23 16:18:00 +040084 server->in_flight--;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +040085 if (server->in_flight == 0 && (optype & CIFS_OP_MASK) != CIFS_NEG_OP)
Pavel Shilovsky28ea5292012-05-23 16:18:00 +040086 rc = change_conf(server);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -070087 /*
88 * Sometimes server returns 0 credits on oplock break ack - we need to
89 * rebalance credits in this case.
90 */
91 else if (server->in_flight > 0 && server->oplock_credits == 0 &&
92 server->oplocks) {
93 if (server->credits > 1) {
94 server->credits--;
95 server->oplock_credits++;
96 }
97 }
Pavel Shilovsky28ea5292012-05-23 16:18:00 +040098 spin_unlock(&server->req_lock);
99 wake_up(&server->request_q);
Pavel Shilovskyef68e832019-01-18 17:25:36 -0800100
Pavel Shilovsky335b7b62019-01-16 11:12:41 -0800101 if (reconnect_detected)
102 cifs_dbg(FYI, "trying to put %d credits from the old server instance %d\n",
103 add, instance);
104
Pavel Shilovsky82e04572019-01-25 10:56:41 -0800105 if (server->tcpStatus == CifsNeedReconnect
106 || server->tcpStatus == CifsExiting)
Pavel Shilovskyef68e832019-01-18 17:25:36 -0800107 return;
108
109 switch (rc) {
110 case -1:
111 /* change_conf hasn't been executed */
112 break;
113 case 0:
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +1000114 cifs_server_dbg(VFS, "Possible client or server bug - zero credits\n");
Pavel Shilovskyef68e832019-01-18 17:25:36 -0800115 break;
116 case 1:
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +1000117 cifs_server_dbg(VFS, "disabling echoes and oplocks\n");
Pavel Shilovskyef68e832019-01-18 17:25:36 -0800118 break;
119 case 2:
120 cifs_dbg(FYI, "disabling oplocks\n");
121 break;
122 default:
123 cifs_dbg(FYI, "add %u credits total=%d\n", add, rc);
124 }
Pavel Shilovsky28ea5292012-05-23 16:18:00 +0400125}
126
127static void
128smb2_set_credits(struct TCP_Server_Info *server, const int val)
129{
130 spin_lock(&server->req_lock);
131 server->credits = val;
Steve French9e1a37d2018-09-19 02:38:17 -0500132 if (val == 1)
133 server->reconnect_instance++;
Pavel Shilovsky28ea5292012-05-23 16:18:00 +0400134 spin_unlock(&server->req_lock);
Steve French6e4d3bb2018-09-22 11:25:04 -0500135 /* don't log while holding the lock */
136 if (val == 1)
137 cifs_dbg(FYI, "set credits to 1 due to smb2 reconnect\n");
Pavel Shilovsky28ea5292012-05-23 16:18:00 +0400138}
139
140static int *
141smb2_get_credits_field(struct TCP_Server_Info *server, const int optype)
142{
143 switch (optype) {
144 case CIFS_ECHO_OP:
145 return &server->echo_credits;
146 case CIFS_OBREAK_OP:
147 return &server->oplock_credits;
148 default:
149 return &server->credits;
150 }
151}
152
153static unsigned int
154smb2_get_credits(struct mid_q_entry *mid)
155{
Pavel Shilovsky86a79642019-11-21 11:35:13 -0800156 return mid->credits_received;
Pavel Shilovsky28ea5292012-05-23 16:18:00 +0400157}
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400158
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +0400159static int
160smb2_wait_mtu_credits(struct TCP_Server_Info *server, unsigned int size,
Pavel Shilovsky335b7b62019-01-16 11:12:41 -0800161 unsigned int *num, struct cifs_credits *credits)
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +0400162{
163 int rc = 0;
164 unsigned int scredits;
165
166 spin_lock(&server->req_lock);
167 while (1) {
168 if (server->credits <= 0) {
169 spin_unlock(&server->req_lock);
170 cifs_num_waiters_inc(server);
171 rc = wait_event_killable(server->request_q,
Ronnie Sahlbergb227d212019-03-08 12:58:20 +1000172 has_credits(server, &server->credits, 1));
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +0400173 cifs_num_waiters_dec(server);
174 if (rc)
175 return rc;
176 spin_lock(&server->req_lock);
177 } else {
178 if (server->tcpStatus == CifsExiting) {
179 spin_unlock(&server->req_lock);
180 return -ENOENT;
181 }
182
183 scredits = server->credits;
184 /* can deadlock with reopen */
Pavel Shilovskyacc58d02019-01-17 08:21:24 -0800185 if (scredits <= 8) {
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +0400186 *num = SMB2_MAX_BUFFER_SIZE;
Pavel Shilovsky335b7b62019-01-16 11:12:41 -0800187 credits->value = 0;
188 credits->instance = 0;
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +0400189 break;
190 }
191
Pavel Shilovskyacc58d02019-01-17 08:21:24 -0800192 /* leave some credits for reopen and other ops */
193 scredits -= 8;
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +0400194 *num = min_t(unsigned int, size,
195 scredits * SMB2_MAX_BUFFER_SIZE);
196
Pavel Shilovsky335b7b62019-01-16 11:12:41 -0800197 credits->value =
198 DIV_ROUND_UP(*num, SMB2_MAX_BUFFER_SIZE);
199 credits->instance = server->reconnect_instance;
200 server->credits -= credits->value;
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +0400201 server->in_flight++;
Steve French1b63f182019-09-09 22:57:11 -0500202 if (server->in_flight > server->max_in_flight)
203 server->max_in_flight = server->in_flight;
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +0400204 break;
205 }
206 }
207 spin_unlock(&server->req_lock);
208 return rc;
209}
210
Pavel Shilovsky9a1c67e2019-01-23 18:15:52 -0800211static int
212smb2_adjust_credits(struct TCP_Server_Info *server,
213 struct cifs_credits *credits,
214 const unsigned int payload_size)
215{
216 int new_val = DIV_ROUND_UP(payload_size, SMB2_MAX_BUFFER_SIZE);
217
218 if (!credits->value || credits->value == new_val)
219 return 0;
220
221 if (credits->value < new_val) {
222 WARN_ONCE(1, "request has less credits (%d) than required (%d)",
223 credits->value, new_val);
224 return -ENOTSUPP;
225 }
226
227 spin_lock(&server->req_lock);
228
229 if (server->reconnect_instance != credits->instance) {
230 spin_unlock(&server->req_lock);
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +1000231 cifs_server_dbg(VFS, "trying to return %d credits to old session\n",
Pavel Shilovsky9a1c67e2019-01-23 18:15:52 -0800232 credits->value - new_val);
233 return -EAGAIN;
234 }
235
236 server->credits += credits->value - new_val;
237 spin_unlock(&server->req_lock);
238 wake_up(&server->request_q);
239 credits->value = new_val;
240 return 0;
241}
242
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400243static __u64
244smb2_get_next_mid(struct TCP_Server_Info *server)
245{
246 __u64 mid;
247 /* for SMB2 we need the current value */
248 spin_lock(&GlobalMid_Lock);
249 mid = server->CurrentMid++;
250 spin_unlock(&GlobalMid_Lock);
251 return mid;
252}
Steve French1080ef72011-02-24 18:07:19 +0000253
Pavel Shilovskyc781af72019-03-04 14:02:50 -0800254static void
255smb2_revert_current_mid(struct TCP_Server_Info *server, const unsigned int val)
256{
257 spin_lock(&GlobalMid_Lock);
258 if (server->CurrentMid >= val)
259 server->CurrentMid -= val;
260 spin_unlock(&GlobalMid_Lock);
261}
262
Pavel Shilovsky093b2bd2011-06-08 15:51:07 +0400263static struct mid_q_entry *
264smb2_find_mid(struct TCP_Server_Info *server, char *buf)
265{
266 struct mid_q_entry *mid;
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +1000267 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700268 __u64 wire_mid = le64_to_cpu(shdr->MessageId);
Pavel Shilovsky093b2bd2011-06-08 15:51:07 +0400269
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700270 if (shdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM) {
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +1000271 cifs_server_dbg(VFS, "Encrypted frame parsing not supported yet\n");
Steve French373512e2015-12-18 13:05:30 -0600272 return NULL;
273 }
274
Pavel Shilovsky093b2bd2011-06-08 15:51:07 +0400275 spin_lock(&GlobalMid_Lock);
276 list_for_each_entry(mid, &server->pending_mid_q, qhead) {
Sachin Prabhu9235d092014-12-09 17:37:00 +0000277 if ((mid->mid == wire_mid) &&
Pavel Shilovsky093b2bd2011-06-08 15:51:07 +0400278 (mid->mid_state == MID_REQUEST_SUBMITTED) &&
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700279 (mid->command == shdr->Command)) {
Lars Persson696e4202018-06-25 14:05:25 +0200280 kref_get(&mid->refcount);
Pavel Shilovsky093b2bd2011-06-08 15:51:07 +0400281 spin_unlock(&GlobalMid_Lock);
282 return mid;
283 }
284 }
285 spin_unlock(&GlobalMid_Lock);
286 return NULL;
287}
288
289static void
Ronnie Sahlberg14547f72018-04-22 14:45:53 -0600290smb2_dump_detail(void *buf, struct TCP_Server_Info *server)
Pavel Shilovsky093b2bd2011-06-08 15:51:07 +0400291{
292#ifdef CONFIG_CIFS_DEBUG2
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +1000293 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
Pavel Shilovsky093b2bd2011-06-08 15:51:07 +0400294
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +1000295 cifs_server_dbg(VFS, "Cmd: %d Err: 0x%x Flags: 0x%x Mid: %llu Pid: %d\n",
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700296 shdr->Command, shdr->Status, shdr->Flags, shdr->MessageId,
297 shdr->ProcessId);
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +1000298 cifs_server_dbg(VFS, "smb buf %p len %u\n", buf,
Steve French71992e622018-05-06 15:58:51 -0500299 server->ops->calc_smb_size(buf, server));
Pavel Shilovsky093b2bd2011-06-08 15:51:07 +0400300#endif
301}
302
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400303static bool
304smb2_need_neg(struct TCP_Server_Info *server)
305{
306 return server->max_read == 0;
307}
308
309static int
310smb2_negotiate(const unsigned int xid, struct cifs_ses *ses)
311{
312 int rc;
Christoph Probsta205d502019-05-08 21:36:25 +0200313
Aurelien Aptelf6a6bf72019-09-20 06:22:14 +0200314 cifs_ses_server(ses)->CurrentMid = 0;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400315 rc = SMB2_negotiate(xid, ses);
316 /* BB we probably don't need to retry with modern servers */
317 if (rc == -EAGAIN)
318 rc = -EHOSTDOWN;
319 return rc;
320}
321
Pavel Shilovsky3a3bab52012-09-18 16:20:28 -0700322static unsigned int
323smb2_negotiate_wsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
324{
325 struct TCP_Server_Info *server = tcon->ses->server;
326 unsigned int wsize;
327
328 /* start with specified wsize, or default */
329 wsize = volume_info->wsize ? volume_info->wsize : CIFS_DEFAULT_IOSIZE;
330 wsize = min_t(unsigned int, wsize, server->max_write);
Long Li09902f82017-11-22 17:38:39 -0700331#ifdef CONFIG_CIFS_SMB_DIRECT
Long Libb4c0412018-04-17 12:17:08 -0700332 if (server->rdma) {
333 if (server->sign)
334 wsize = min_t(unsigned int,
335 wsize, server->smbd_conn->max_fragmented_send_size);
336 else
337 wsize = min_t(unsigned int,
Long Li09902f82017-11-22 17:38:39 -0700338 wsize, server->smbd_conn->max_readwrite_size);
Long Libb4c0412018-04-17 12:17:08 -0700339 }
Long Li09902f82017-11-22 17:38:39 -0700340#endif
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +0400341 if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
342 wsize = min_t(unsigned int, wsize, SMB2_MAX_BUFFER_SIZE);
Pavel Shilovsky3a3bab52012-09-18 16:20:28 -0700343
Pavel Shilovsky3a3bab52012-09-18 16:20:28 -0700344 return wsize;
345}
346
347static unsigned int
Steve French3d621232018-09-25 15:33:47 -0500348smb3_negotiate_wsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
349{
350 struct TCP_Server_Info *server = tcon->ses->server;
351 unsigned int wsize;
352
353 /* start with specified wsize, or default */
354 wsize = volume_info->wsize ? volume_info->wsize : SMB3_DEFAULT_IOSIZE;
355 wsize = min_t(unsigned int, wsize, server->max_write);
356#ifdef CONFIG_CIFS_SMB_DIRECT
357 if (server->rdma) {
358 if (server->sign)
359 wsize = min_t(unsigned int,
360 wsize, server->smbd_conn->max_fragmented_send_size);
361 else
362 wsize = min_t(unsigned int,
363 wsize, server->smbd_conn->max_readwrite_size);
364 }
365#endif
366 if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
367 wsize = min_t(unsigned int, wsize, SMB2_MAX_BUFFER_SIZE);
368
369 return wsize;
370}
371
372static unsigned int
Pavel Shilovsky3a3bab52012-09-18 16:20:28 -0700373smb2_negotiate_rsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
374{
375 struct TCP_Server_Info *server = tcon->ses->server;
376 unsigned int rsize;
377
378 /* start with specified rsize, or default */
379 rsize = volume_info->rsize ? volume_info->rsize : CIFS_DEFAULT_IOSIZE;
380 rsize = min_t(unsigned int, rsize, server->max_read);
Long Li09902f82017-11-22 17:38:39 -0700381#ifdef CONFIG_CIFS_SMB_DIRECT
Long Libb4c0412018-04-17 12:17:08 -0700382 if (server->rdma) {
383 if (server->sign)
384 rsize = min_t(unsigned int,
385 rsize, server->smbd_conn->max_fragmented_recv_size);
386 else
387 rsize = min_t(unsigned int,
Long Li09902f82017-11-22 17:38:39 -0700388 rsize, server->smbd_conn->max_readwrite_size);
Long Libb4c0412018-04-17 12:17:08 -0700389 }
Long Li09902f82017-11-22 17:38:39 -0700390#endif
Pavel Shilovskybed9da02014-06-25 11:28:57 +0400391
392 if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
393 rsize = min_t(unsigned int, rsize, SMB2_MAX_BUFFER_SIZE);
Pavel Shilovsky3a3bab52012-09-18 16:20:28 -0700394
Pavel Shilovsky3a3bab52012-09-18 16:20:28 -0700395 return rsize;
396}
397
Steve French3d621232018-09-25 15:33:47 -0500398static unsigned int
399smb3_negotiate_rsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
400{
401 struct TCP_Server_Info *server = tcon->ses->server;
402 unsigned int rsize;
403
404 /* start with specified rsize, or default */
405 rsize = volume_info->rsize ? volume_info->rsize : SMB3_DEFAULT_IOSIZE;
406 rsize = min_t(unsigned int, rsize, server->max_read);
407#ifdef CONFIG_CIFS_SMB_DIRECT
408 if (server->rdma) {
409 if (server->sign)
410 rsize = min_t(unsigned int,
411 rsize, server->smbd_conn->max_fragmented_recv_size);
412 else
413 rsize = min_t(unsigned int,
414 rsize, server->smbd_conn->max_readwrite_size);
415 }
416#endif
417
418 if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
419 rsize = min_t(unsigned int, rsize, SMB2_MAX_BUFFER_SIZE);
420
421 return rsize;
422}
Aurelien Aptelfe856be2018-06-14 17:04:51 +0200423
424static int
425parse_server_interfaces(struct network_interface_info_ioctl_rsp *buf,
426 size_t buf_len,
427 struct cifs_server_iface **iface_list,
428 size_t *iface_count)
429{
430 struct network_interface_info_ioctl_rsp *p;
431 struct sockaddr_in *addr4;
432 struct sockaddr_in6 *addr6;
433 struct iface_info_ipv4 *p4;
434 struct iface_info_ipv6 *p6;
435 struct cifs_server_iface *info;
436 ssize_t bytes_left;
437 size_t next = 0;
438 int nb_iface = 0;
439 int rc = 0;
440
441 *iface_list = NULL;
442 *iface_count = 0;
443
444 /*
445 * Fist pass: count and sanity check
446 */
447
448 bytes_left = buf_len;
449 p = buf;
450 while (bytes_left >= sizeof(*p)) {
451 nb_iface++;
452 next = le32_to_cpu(p->Next);
453 if (!next) {
454 bytes_left -= sizeof(*p);
455 break;
456 }
457 p = (struct network_interface_info_ioctl_rsp *)((u8 *)p+next);
458 bytes_left -= next;
459 }
460
461 if (!nb_iface) {
462 cifs_dbg(VFS, "%s: malformed interface info\n", __func__);
463 rc = -EINVAL;
464 goto out;
465 }
466
467 if (bytes_left || p->Next)
468 cifs_dbg(VFS, "%s: incomplete interface info\n", __func__);
469
470
471 /*
472 * Second pass: extract info to internal structure
473 */
474
475 *iface_list = kcalloc(nb_iface, sizeof(**iface_list), GFP_KERNEL);
476 if (!*iface_list) {
477 rc = -ENOMEM;
478 goto out;
479 }
480
481 info = *iface_list;
482 bytes_left = buf_len;
483 p = buf;
484 while (bytes_left >= sizeof(*p)) {
485 info->speed = le64_to_cpu(p->LinkSpeed);
486 info->rdma_capable = le32_to_cpu(p->Capability & RDMA_CAPABLE);
487 info->rss_capable = le32_to_cpu(p->Capability & RSS_CAPABLE);
488
489 cifs_dbg(FYI, "%s: adding iface %zu\n", __func__, *iface_count);
490 cifs_dbg(FYI, "%s: speed %zu bps\n", __func__, info->speed);
491 cifs_dbg(FYI, "%s: capabilities 0x%08x\n", __func__,
492 le32_to_cpu(p->Capability));
493
494 switch (p->Family) {
495 /*
496 * The kernel and wire socket structures have the same
497 * layout and use network byte order but make the
498 * conversion explicit in case either one changes.
499 */
500 case INTERNETWORK:
501 addr4 = (struct sockaddr_in *)&info->sockaddr;
502 p4 = (struct iface_info_ipv4 *)p->Buffer;
503 addr4->sin_family = AF_INET;
504 memcpy(&addr4->sin_addr, &p4->IPv4Address, 4);
505
506 /* [MS-SMB2] 2.2.32.5.1.1 Clients MUST ignore these */
507 addr4->sin_port = cpu_to_be16(CIFS_PORT);
508
509 cifs_dbg(FYI, "%s: ipv4 %pI4\n", __func__,
510 &addr4->sin_addr);
511 break;
512 case INTERNETWORKV6:
513 addr6 = (struct sockaddr_in6 *)&info->sockaddr;
514 p6 = (struct iface_info_ipv6 *)p->Buffer;
515 addr6->sin6_family = AF_INET6;
516 memcpy(&addr6->sin6_addr, &p6->IPv6Address, 16);
517
518 /* [MS-SMB2] 2.2.32.5.1.2 Clients MUST ignore these */
519 addr6->sin6_flowinfo = 0;
520 addr6->sin6_scope_id = 0;
521 addr6->sin6_port = cpu_to_be16(CIFS_PORT);
522
523 cifs_dbg(FYI, "%s: ipv6 %pI6\n", __func__,
524 &addr6->sin6_addr);
525 break;
526 default:
527 cifs_dbg(VFS,
528 "%s: skipping unsupported socket family\n",
529 __func__);
530 goto next_iface;
531 }
532
533 (*iface_count)++;
534 info++;
535next_iface:
536 next = le32_to_cpu(p->Next);
537 if (!next)
538 break;
539 p = (struct network_interface_info_ioctl_rsp *)((u8 *)p+next);
540 bytes_left -= next;
541 }
542
543 if (!*iface_count) {
544 rc = -EINVAL;
545 goto out;
546 }
547
548out:
549 if (rc) {
550 kfree(*iface_list);
551 *iface_count = 0;
552 *iface_list = NULL;
553 }
554 return rc;
555}
556
Aurelien Aptel35adffe2019-09-20 06:29:39 +0200557static int compare_iface(const void *ia, const void *ib)
558{
559 const struct cifs_server_iface *a = (struct cifs_server_iface *)ia;
560 const struct cifs_server_iface *b = (struct cifs_server_iface *)ib;
561
562 return a->speed == b->speed ? 0 : (a->speed > b->speed ? -1 : 1);
563}
Aurelien Aptelfe856be2018-06-14 17:04:51 +0200564
Steve Frenchc481e9f2013-10-14 01:21:53 -0500565static int
566SMB3_request_interfaces(const unsigned int xid, struct cifs_tcon *tcon)
567{
568 int rc;
569 unsigned int ret_data_len = 0;
Aurelien Aptelfe856be2018-06-14 17:04:51 +0200570 struct network_interface_info_ioctl_rsp *out_buf = NULL;
571 struct cifs_server_iface *iface_list;
572 size_t iface_count;
573 struct cifs_ses *ses = tcon->ses;
Steve Frenchc481e9f2013-10-14 01:21:53 -0500574
575 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
576 FSCTL_QUERY_NETWORK_INTERFACE_INFO, true /* is_fsctl */,
577 NULL /* no data input */, 0 /* no data input */,
Steve French153322f2019-03-28 22:32:49 -0500578 CIFSMaxBufSize, (char **)&out_buf, &ret_data_len);
Steve Frenchc3ed4402018-06-28 22:53:39 -0500579 if (rc == -EOPNOTSUPP) {
580 cifs_dbg(FYI,
581 "server does not support query network interfaces\n");
582 goto out;
583 } else if (rc != 0) {
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +1000584 cifs_tcon_dbg(VFS, "error %d on ioctl to get interface list\n", rc);
Aurelien Aptelfe856be2018-06-14 17:04:51 +0200585 goto out;
Steve French9ffc5412014-10-16 15:13:14 -0500586 }
Aurelien Aptelfe856be2018-06-14 17:04:51 +0200587
588 rc = parse_server_interfaces(out_buf, ret_data_len,
589 &iface_list, &iface_count);
590 if (rc)
591 goto out;
592
Aurelien Aptel35adffe2019-09-20 06:29:39 +0200593 /* sort interfaces from fastest to slowest */
594 sort(iface_list, iface_count, sizeof(*iface_list), compare_iface, NULL);
595
Aurelien Aptelfe856be2018-06-14 17:04:51 +0200596 spin_lock(&ses->iface_lock);
597 kfree(ses->iface_list);
598 ses->iface_list = iface_list;
599 ses->iface_count = iface_count;
600 ses->iface_last_update = jiffies;
601 spin_unlock(&ses->iface_lock);
602
603out:
Steve French24df1482016-09-29 04:20:23 -0500604 kfree(out_buf);
Steve Frenchc481e9f2013-10-14 01:21:53 -0500605 return rc;
606}
Steve Frenchc481e9f2013-10-14 01:21:53 -0500607
Ronnie Sahlberg9da6ec72018-07-31 08:48:22 +1000608static void
609smb2_close_cached_fid(struct kref *ref)
Ronnie Sahlberga93864d2018-06-14 06:48:35 +1000610{
Ronnie Sahlberg9da6ec72018-07-31 08:48:22 +1000611 struct cached_fid *cfid = container_of(ref, struct cached_fid,
612 refcount);
613
Ronnie Sahlberga93864d2018-06-14 06:48:35 +1000614 if (cfid->is_valid) {
615 cifs_dbg(FYI, "clear cached root file handle\n");
616 SMB2_close(0, cfid->tcon, cfid->fid->persistent_fid,
617 cfid->fid->volatile_fid);
618 cfid->is_valid = false;
Ronnie Sahlbergb0f6df72019-03-12 13:58:31 +1000619 cfid->file_all_info_is_valid = false;
Pavel Shilovskyd9191312019-12-10 11:44:52 -0800620 cfid->has_lease = false;
Ronnie Sahlberga93864d2018-06-14 06:48:35 +1000621 }
Ronnie Sahlberg9da6ec72018-07-31 08:48:22 +1000622}
623
624void close_shroot(struct cached_fid *cfid)
625{
626 mutex_lock(&cfid->fid_mutex);
627 kref_put(&cfid->refcount, smb2_close_cached_fid);
Ronnie Sahlberga93864d2018-06-14 06:48:35 +1000628 mutex_unlock(&cfid->fid_mutex);
629}
630
Pavel Shilovskyd9191312019-12-10 11:44:52 -0800631void close_shroot_lease_locked(struct cached_fid *cfid)
632{
633 if (cfid->has_lease) {
634 cfid->has_lease = false;
635 kref_put(&cfid->refcount, smb2_close_cached_fid);
636 }
637}
638
639void close_shroot_lease(struct cached_fid *cfid)
640{
641 mutex_lock(&cfid->fid_mutex);
642 close_shroot_lease_locked(cfid);
643 mutex_unlock(&cfid->fid_mutex);
644}
645
Ronnie Sahlberg9da6ec72018-07-31 08:48:22 +1000646void
647smb2_cached_lease_break(struct work_struct *work)
648{
649 struct cached_fid *cfid = container_of(work,
650 struct cached_fid, lease_break);
651
Pavel Shilovskyd9191312019-12-10 11:44:52 -0800652 close_shroot_lease(cfid);
Ronnie Sahlberg9da6ec72018-07-31 08:48:22 +1000653}
654
Steve French3d4ef9a2018-04-25 22:19:09 -0500655/*
656 * Open the directory at the root of a share
657 */
658int open_shroot(unsigned int xid, struct cifs_tcon *tcon, struct cifs_fid *pfid)
659{
Ronnie Sahlbergb0f6df72019-03-12 13:58:31 +1000660 struct cifs_ses *ses = tcon->ses;
661 struct TCP_Server_Info *server = ses->server;
662 struct cifs_open_parms oparms;
663 struct smb2_create_rsp *o_rsp = NULL;
664 struct smb2_query_info_rsp *qi_rsp = NULL;
665 int resp_buftype[2];
666 struct smb_rqst rqst[2];
667 struct kvec rsp_iov[2];
668 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
669 struct kvec qi_iov[1];
670 int rc, flags = 0;
671 __le16 utf16_path = 0; /* Null - since an open of top of share */
Ronnie Sahlberga93864d2018-06-14 06:48:35 +1000672 u8 oplock = SMB2_OPLOCK_LEVEL_II;
Steve French3d4ef9a2018-04-25 22:19:09 -0500673
Ronnie Sahlberga93864d2018-06-14 06:48:35 +1000674 mutex_lock(&tcon->crfid.fid_mutex);
675 if (tcon->crfid.is_valid) {
Steve French3d4ef9a2018-04-25 22:19:09 -0500676 cifs_dbg(FYI, "found a cached root file handle\n");
Ronnie Sahlberga93864d2018-06-14 06:48:35 +1000677 memcpy(pfid, tcon->crfid.fid, sizeof(struct cifs_fid));
Ronnie Sahlberg9da6ec72018-07-31 08:48:22 +1000678 kref_get(&tcon->crfid.refcount);
Ronnie Sahlberga93864d2018-06-14 06:48:35 +1000679 mutex_unlock(&tcon->crfid.fid_mutex);
Steve French3d4ef9a2018-04-25 22:19:09 -0500680 return 0;
681 }
682
Steve French96d9f7e2019-09-12 17:52:54 -0500683 /*
684 * We do not hold the lock for the open because in case
685 * SMB2_open needs to reconnect, it will end up calling
686 * cifs_mark_open_files_invalid() which takes the lock again
687 * thus causing a deadlock
688 */
689
690 mutex_unlock(&tcon->crfid.fid_mutex);
691
Ronnie Sahlbergb0f6df72019-03-12 13:58:31 +1000692 if (smb3_encryption_required(tcon))
693 flags |= CIFS_TRANSFORM_REQ;
Steve French3d4ef9a2018-04-25 22:19:09 -0500694
Ronnie Sahlbergb0f6df72019-03-12 13:58:31 +1000695 memset(rqst, 0, sizeof(rqst));
696 resp_buftype[0] = resp_buftype[1] = CIFS_NO_BUFFER;
697 memset(rsp_iov, 0, sizeof(rsp_iov));
698
699 /* Open */
700 memset(&open_iov, 0, sizeof(open_iov));
701 rqst[0].rq_iov = open_iov;
702 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
703
704 oparms.tcon = tcon;
705 oparms.create_options = 0;
706 oparms.desired_access = FILE_READ_ATTRIBUTES;
707 oparms.disposition = FILE_OPEN;
708 oparms.fid = pfid;
709 oparms.reconnect = false;
710
711 rc = SMB2_open_init(tcon, &rqst[0], &oplock, &oparms, &utf16_path);
712 if (rc)
Steve French96d9f7e2019-09-12 17:52:54 -0500713 goto oshr_free;
Ronnie Sahlbergb0f6df72019-03-12 13:58:31 +1000714 smb2_set_next_command(tcon, &rqst[0]);
715
716 memset(&qi_iov, 0, sizeof(qi_iov));
717 rqst[1].rq_iov = qi_iov;
718 rqst[1].rq_nvec = 1;
719
720 rc = SMB2_query_info_init(tcon, &rqst[1], COMPOUND_FID,
721 COMPOUND_FID, FILE_ALL_INFORMATION,
722 SMB2_O_INFO_FILE, 0,
723 sizeof(struct smb2_file_all_info) +
724 PATH_MAX * 2, 0, NULL);
725 if (rc)
Steve French96d9f7e2019-09-12 17:52:54 -0500726 goto oshr_free;
Ronnie Sahlbergb0f6df72019-03-12 13:58:31 +1000727
728 smb2_set_related(&rqst[1]);
729
730 rc = compound_send_recv(xid, ses, flags, 2, rqst,
731 resp_buftype, rsp_iov);
Aurelien Aptel7e5a70a2019-07-17 12:46:28 +0200732 mutex_lock(&tcon->crfid.fid_mutex);
733
734 /*
735 * Now we need to check again as the cached root might have
736 * been successfully re-opened from a concurrent process
737 */
738
739 if (tcon->crfid.is_valid) {
740 /* work was already done */
741
742 /* stash fids for close() later */
743 struct cifs_fid fid = {
744 .persistent_fid = pfid->persistent_fid,
745 .volatile_fid = pfid->volatile_fid,
746 };
747
748 /*
749 * caller expects this func to set pfid to a valid
750 * cached root, so we copy the existing one and get a
751 * reference.
752 */
753 memcpy(pfid, tcon->crfid.fid, sizeof(*pfid));
754 kref_get(&tcon->crfid.refcount);
755
756 mutex_unlock(&tcon->crfid.fid_mutex);
757
758 if (rc == 0) {
759 /* close extra handle outside of crit sec */
760 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
761 }
762 goto oshr_free;
763 }
764
765 /* Cached root is still invalid, continue normaly */
766
Steve French7dcc82c2019-09-11 00:07:36 -0500767 if (rc) {
768 if (rc == -EREMCHG) {
769 tcon->need_reconnect = true;
770 printk_once(KERN_WARNING "server share %s deleted\n",
771 tcon->treeName);
772 }
Ronnie Sahlbergb0f6df72019-03-12 13:58:31 +1000773 goto oshr_exit;
Steve French7dcc82c2019-09-11 00:07:36 -0500774 }
Ronnie Sahlbergb0f6df72019-03-12 13:58:31 +1000775
Steve Frenchd2f15422019-09-22 00:55:46 -0500776 atomic_inc(&tcon->num_remote_opens);
777
Ronnie Sahlbergb0f6df72019-03-12 13:58:31 +1000778 o_rsp = (struct smb2_create_rsp *)rsp_iov[0].iov_base;
779 oparms.fid->persistent_fid = o_rsp->PersistentFileId;
780 oparms.fid->volatile_fid = o_rsp->VolatileFileId;
781#ifdef CONFIG_CIFS_DEBUG2
782 oparms.fid->mid = le64_to_cpu(o_rsp->sync_hdr.MessageId);
783#endif /* CIFS_DEBUG2 */
784
Ronnie Sahlbergb0f6df72019-03-12 13:58:31 +1000785 memcpy(tcon->crfid.fid, pfid, sizeof(struct cifs_fid));
786 tcon->crfid.tcon = tcon;
787 tcon->crfid.is_valid = true;
788 kref_init(&tcon->crfid.refcount);
Ronnie Sahlbergb0f6df72019-03-12 13:58:31 +1000789
Steve French89a5bfa2019-07-18 17:22:18 -0500790 /* BB TBD check to see if oplock level check can be removed below */
Ronnie Sahlberg2f94a3122019-03-28 11:20:02 +1000791 if (o_rsp->OplockLevel == SMB2_OPLOCK_LEVEL_LEASE) {
792 kref_get(&tcon->crfid.refcount);
Pavel Shilovskyd9191312019-12-10 11:44:52 -0800793 tcon->crfid.has_lease = true;
Steve French89a5bfa2019-07-18 17:22:18 -0500794 smb2_parse_contexts(server, o_rsp,
795 &oparms.fid->epoch,
796 oparms.fid->lease_key, &oplock, NULL);
Ronnie Sahlberg2f94a3122019-03-28 11:20:02 +1000797 } else
798 goto oshr_exit;
Ronnie Sahlbergb0f6df72019-03-12 13:58:31 +1000799
800 qi_rsp = (struct smb2_query_info_rsp *)rsp_iov[1].iov_base;
801 if (le32_to_cpu(qi_rsp->OutputBufferLength) < sizeof(struct smb2_file_all_info))
802 goto oshr_exit;
Ronnie Sahlberg4811e302019-04-01 09:53:44 +1000803 if (!smb2_validate_and_copy_iov(
Ronnie Sahlbergb0f6df72019-03-12 13:58:31 +1000804 le16_to_cpu(qi_rsp->OutputBufferOffset),
805 sizeof(struct smb2_file_all_info),
806 &rsp_iov[1], sizeof(struct smb2_file_all_info),
Ronnie Sahlberg4811e302019-04-01 09:53:44 +1000807 (char *)&tcon->crfid.file_all_info))
zhengbin720aec02019-12-25 11:30:20 +0800808 tcon->crfid.file_all_info_is_valid = true;
Ronnie Sahlbergb0f6df72019-03-12 13:58:31 +1000809
Aurelien Aptel7e5a70a2019-07-17 12:46:28 +0200810oshr_exit:
Ronnie Sahlberga93864d2018-06-14 06:48:35 +1000811 mutex_unlock(&tcon->crfid.fid_mutex);
Aurelien Aptel7e5a70a2019-07-17 12:46:28 +0200812oshr_free:
Ronnie Sahlbergb0f6df72019-03-12 13:58:31 +1000813 SMB2_open_free(&rqst[0]);
814 SMB2_query_info_free(&rqst[1]);
815 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
816 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
Steve French3d4ef9a2018-04-25 22:19:09 -0500817 return rc;
818}
819
Steve French34f62642013-10-09 02:07:00 -0500820static void
Steven Frenchaf6a12e2013-10-09 20:55:53 -0500821smb3_qfs_tcon(const unsigned int xid, struct cifs_tcon *tcon)
822{
823 int rc;
824 __le16 srch_path = 0; /* Null - open root of share */
825 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
826 struct cifs_open_parms oparms;
827 struct cifs_fid fid;
Steve French3d4ef9a2018-04-25 22:19:09 -0500828 bool no_cached_open = tcon->nohandlecache;
Steven Frenchaf6a12e2013-10-09 20:55:53 -0500829
830 oparms.tcon = tcon;
831 oparms.desired_access = FILE_READ_ATTRIBUTES;
832 oparms.disposition = FILE_OPEN;
833 oparms.create_options = 0;
834 oparms.fid = &fid;
835 oparms.reconnect = false;
836
Steve French3d4ef9a2018-04-25 22:19:09 -0500837 if (no_cached_open)
Ronnie Sahlberg9d874c32018-06-08 13:21:18 +1000838 rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL,
839 NULL);
Steve French3d4ef9a2018-04-25 22:19:09 -0500840 else
841 rc = open_shroot(xid, tcon, &fid);
842
Steven Frenchaf6a12e2013-10-09 20:55:53 -0500843 if (rc)
844 return;
845
Steve Frenchc481e9f2013-10-14 01:21:53 -0500846 SMB3_request_interfaces(xid, tcon);
Steve Frenchc481e9f2013-10-14 01:21:53 -0500847
Steven Frenchaf6a12e2013-10-09 20:55:53 -0500848 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
849 FS_ATTRIBUTE_INFORMATION);
850 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
851 FS_DEVICE_INFORMATION);
852 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
Steve French21ba3842018-06-24 23:18:52 -0500853 FS_VOLUME_INFORMATION);
854 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
Steven Frenchaf6a12e2013-10-09 20:55:53 -0500855 FS_SECTOR_SIZE_INFORMATION); /* SMB3 specific */
Steve French3d4ef9a2018-04-25 22:19:09 -0500856 if (no_cached_open)
857 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
Ronnie Sahlberg9da6ec72018-07-31 08:48:22 +1000858 else
859 close_shroot(&tcon->crfid);
Steven Frenchaf6a12e2013-10-09 20:55:53 -0500860}
861
862static void
Steve French34f62642013-10-09 02:07:00 -0500863smb2_qfs_tcon(const unsigned int xid, struct cifs_tcon *tcon)
864{
865 int rc;
866 __le16 srch_path = 0; /* Null - open root of share */
867 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
868 struct cifs_open_parms oparms;
869 struct cifs_fid fid;
870
871 oparms.tcon = tcon;
872 oparms.desired_access = FILE_READ_ATTRIBUTES;
873 oparms.disposition = FILE_OPEN;
874 oparms.create_options = 0;
875 oparms.fid = &fid;
876 oparms.reconnect = false;
877
Ronnie Sahlberg9d874c32018-06-08 13:21:18 +1000878 rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL, NULL);
Steve French34f62642013-10-09 02:07:00 -0500879 if (rc)
880 return;
881
Steven French21671142013-10-09 13:36:35 -0500882 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
883 FS_ATTRIBUTE_INFORMATION);
884 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
885 FS_DEVICE_INFORMATION);
Steve French34f62642013-10-09 02:07:00 -0500886 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
Steve French34f62642013-10-09 02:07:00 -0500887}
888
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +0400889static int
890smb2_is_path_accessible(const unsigned int xid, struct cifs_tcon *tcon,
891 struct cifs_sb_info *cifs_sb, const char *full_path)
892{
893 int rc;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +0400894 __le16 *utf16_path;
Pavel Shilovsky2e44b282012-09-18 16:20:33 -0700895 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
Pavel Shilovsky064f6042013-07-09 18:20:30 +0400896 struct cifs_open_parms oparms;
897 struct cifs_fid fid;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +0400898
Ronnie Sahlberga93864d2018-06-14 06:48:35 +1000899 if ((*full_path == 0) && tcon->crfid.is_valid)
Steve French3d4ef9a2018-04-25 22:19:09 -0500900 return 0;
901
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +0400902 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
903 if (!utf16_path)
904 return -ENOMEM;
905
Pavel Shilovsky064f6042013-07-09 18:20:30 +0400906 oparms.tcon = tcon;
907 oparms.desired_access = FILE_READ_ATTRIBUTES;
908 oparms.disposition = FILE_OPEN;
Steve French5e196972018-08-27 17:04:13 -0500909 if (backup_cred(cifs_sb))
910 oparms.create_options = CREATE_OPEN_BACKUP_INTENT;
911 else
912 oparms.create_options = 0;
Pavel Shilovsky064f6042013-07-09 18:20:30 +0400913 oparms.fid = &fid;
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +0400914 oparms.reconnect = false;
Pavel Shilovsky064f6042013-07-09 18:20:30 +0400915
Ronnie Sahlberg9d874c32018-06-08 13:21:18 +1000916 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, NULL);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +0400917 if (rc) {
918 kfree(utf16_path);
919 return rc;
920 }
921
Pavel Shilovsky064f6042013-07-09 18:20:30 +0400922 rc = SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +0400923 kfree(utf16_path);
924 return rc;
925}
926
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +0400927static int
928smb2_get_srv_inum(const unsigned int xid, struct cifs_tcon *tcon,
929 struct cifs_sb_info *cifs_sb, const char *full_path,
930 u64 *uniqueid, FILE_ALL_INFO *data)
931{
932 *uniqueid = le64_to_cpu(data->IndexNumber);
933 return 0;
934}
935
Pavel Shilovskyb7546bc2012-09-18 16:20:27 -0700936static int
937smb2_query_file_info(const unsigned int xid, struct cifs_tcon *tcon,
938 struct cifs_fid *fid, FILE_ALL_INFO *data)
939{
940 int rc;
941 struct smb2_file_all_info *smb2_data;
942
Pavel Shilovsky1bbe4992014-08-22 13:32:11 +0400943 smb2_data = kzalloc(sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
Pavel Shilovskyb7546bc2012-09-18 16:20:27 -0700944 GFP_KERNEL);
945 if (smb2_data == NULL)
946 return -ENOMEM;
947
948 rc = SMB2_query_info(xid, tcon, fid->persistent_fid, fid->volatile_fid,
949 smb2_data);
950 if (!rc)
951 move_smb2_info_to_cifs(data, smb2_data);
952 kfree(smb2_data);
953 return rc;
954}
955
Arnd Bergmann1368f152017-09-05 11:24:15 +0200956#ifdef CONFIG_CIFS_XATTR
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +1000957static ssize_t
958move_smb2_ea_to_cifs(char *dst, size_t dst_size,
959 struct smb2_file_full_ea_info *src, size_t src_size,
960 const unsigned char *ea_name)
961{
962 int rc = 0;
963 unsigned int ea_name_len = ea_name ? strlen(ea_name) : 0;
964 char *name, *value;
Ronnie Sahlberg0c5d6cb2018-10-25 15:43:36 +1000965 size_t buf_size = dst_size;
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +1000966 size_t name_len, value_len, user_name_len;
967
968 while (src_size > 0) {
969 name = &src->ea_data[0];
970 name_len = (size_t)src->ea_name_length;
971 value = &src->ea_data[src->ea_name_length + 1];
972 value_len = (size_t)le16_to_cpu(src->ea_value_length);
973
Christoph Probsta205d502019-05-08 21:36:25 +0200974 if (name_len == 0)
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +1000975 break;
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +1000976
977 if (src_size < 8 + name_len + 1 + value_len) {
978 cifs_dbg(FYI, "EA entry goes beyond length of list\n");
979 rc = -EIO;
980 goto out;
981 }
982
983 if (ea_name) {
984 if (ea_name_len == name_len &&
985 memcmp(ea_name, name, name_len) == 0) {
986 rc = value_len;
987 if (dst_size == 0)
988 goto out;
989 if (dst_size < value_len) {
990 rc = -ERANGE;
991 goto out;
992 }
993 memcpy(dst, value, value_len);
994 goto out;
995 }
996 } else {
997 /* 'user.' plus a terminating null */
998 user_name_len = 5 + 1 + name_len;
999
Ronnie Sahlberg0c5d6cb2018-10-25 15:43:36 +10001000 if (buf_size == 0) {
1001 /* skip copy - calc size only */
1002 rc += user_name_len;
1003 } else if (dst_size >= user_name_len) {
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10001004 dst_size -= user_name_len;
1005 memcpy(dst, "user.", 5);
1006 dst += 5;
1007 memcpy(dst, src->ea_data, name_len);
1008 dst += name_len;
1009 *dst = 0;
1010 ++dst;
Ronnie Sahlberg0c5d6cb2018-10-25 15:43:36 +10001011 rc += user_name_len;
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10001012 } else {
1013 /* stop before overrun buffer */
1014 rc = -ERANGE;
1015 break;
1016 }
1017 }
1018
1019 if (!src->next_entry_offset)
1020 break;
1021
1022 if (src_size < le32_to_cpu(src->next_entry_offset)) {
1023 /* stop before overrun buffer */
1024 rc = -ERANGE;
1025 break;
1026 }
1027 src_size -= le32_to_cpu(src->next_entry_offset);
1028 src = (void *)((char *)src +
1029 le32_to_cpu(src->next_entry_offset));
1030 }
1031
1032 /* didn't find the named attribute */
1033 if (ea_name)
1034 rc = -ENODATA;
1035
1036out:
1037 return (ssize_t)rc;
1038}
1039
1040static ssize_t
1041smb2_query_eas(const unsigned int xid, struct cifs_tcon *tcon,
1042 const unsigned char *path, const unsigned char *ea_name,
1043 char *ea_data, size_t buf_size,
1044 struct cifs_sb_info *cifs_sb)
1045{
1046 int rc;
1047 __le16 *utf16_path;
Ronnie Sahlbergf9793b62018-11-27 09:52:04 +10001048 struct kvec rsp_iov = {NULL, 0};
1049 int buftype = CIFS_NO_BUFFER;
1050 struct smb2_query_info_rsp *rsp;
1051 struct smb2_file_full_ea_info *info = NULL;
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10001052
1053 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
1054 if (!utf16_path)
1055 return -ENOMEM;
1056
Ronnie Sahlbergf9793b62018-11-27 09:52:04 +10001057 rc = smb2_query_info_compound(xid, tcon, utf16_path,
1058 FILE_READ_EA,
1059 FILE_FULL_EA_INFORMATION,
1060 SMB2_O_INFO_FILE,
Ronnie Sahlbergc4627e62019-01-29 12:46:17 +10001061 CIFSMaxBufSize -
1062 MAX_SMB2_CREATE_RESPONSE_SIZE -
1063 MAX_SMB2_CLOSE_RESPONSE_SIZE,
Ronnie Sahlbergf9793b62018-11-27 09:52:04 +10001064 &rsp_iov, &buftype, cifs_sb);
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10001065 if (rc) {
Ronnie Sahlbergf9793b62018-11-27 09:52:04 +10001066 /*
1067 * If ea_name is NULL (listxattr) and there are no EAs,
1068 * return 0 as it's not an error. Otherwise, the specified
1069 * ea_name was not found.
1070 */
1071 if (!ea_name && rc == -ENODATA)
1072 rc = 0;
1073 goto qeas_exit;
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10001074 }
1075
Ronnie Sahlbergf9793b62018-11-27 09:52:04 +10001076 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
1077 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
1078 le32_to_cpu(rsp->OutputBufferLength),
1079 &rsp_iov,
1080 sizeof(struct smb2_file_full_ea_info));
1081 if (rc)
1082 goto qeas_exit;
Ronnie Sahlberg7cb3def2017-09-28 09:39:58 +10001083
Ronnie Sahlbergf9793b62018-11-27 09:52:04 +10001084 info = (struct smb2_file_full_ea_info *)(
1085 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
1086 rc = move_smb2_ea_to_cifs(ea_data, buf_size, info,
1087 le32_to_cpu(rsp->OutputBufferLength), ea_name);
Ronnie Sahlberg7cb3def2017-09-28 09:39:58 +10001088
Ronnie Sahlbergf9793b62018-11-27 09:52:04 +10001089 qeas_exit:
1090 kfree(utf16_path);
1091 free_rsp_buf(buftype, rsp_iov.iov_base);
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10001092 return rc;
1093}
1094
Ronnie Sahlberg55175542017-08-24 11:24:56 +10001095
1096static int
1097smb2_set_ea(const unsigned int xid, struct cifs_tcon *tcon,
1098 const char *path, const char *ea_name, const void *ea_value,
1099 const __u16 ea_value_len, const struct nls_table *nls_codepage,
1100 struct cifs_sb_info *cifs_sb)
1101{
Ronnie Sahlberg0967e542018-11-06 22:52:43 +10001102 struct cifs_ses *ses = tcon->ses;
Ronnie Sahlberg0967e542018-11-06 22:52:43 +10001103 __le16 *utf16_path = NULL;
Ronnie Sahlberg55175542017-08-24 11:24:56 +10001104 int ea_name_len = strlen(ea_name);
Ronnie Sahlberg0967e542018-11-06 22:52:43 +10001105 int flags = 0;
Ronnie Sahlberg55175542017-08-24 11:24:56 +10001106 int len;
Ronnie Sahlberg0967e542018-11-06 22:52:43 +10001107 struct smb_rqst rqst[3];
1108 int resp_buftype[3];
1109 struct kvec rsp_iov[3];
1110 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
1111 struct cifs_open_parms oparms;
1112 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
1113 struct cifs_fid fid;
1114 struct kvec si_iov[SMB2_SET_INFO_IOV_SIZE];
1115 unsigned int size[1];
1116 void *data[1];
1117 struct smb2_file_full_ea_info *ea = NULL;
1118 struct kvec close_iov[1];
1119 int rc;
1120
1121 if (smb3_encryption_required(tcon))
1122 flags |= CIFS_TRANSFORM_REQ;
Ronnie Sahlberg55175542017-08-24 11:24:56 +10001123
1124 if (ea_name_len > 255)
1125 return -EINVAL;
1126
1127 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
1128 if (!utf16_path)
1129 return -ENOMEM;
1130
Ronnie Sahlberg0967e542018-11-06 22:52:43 +10001131 memset(rqst, 0, sizeof(rqst));
1132 resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
1133 memset(rsp_iov, 0, sizeof(rsp_iov));
1134
Ronnie Sahlberg21094642019-02-07 15:48:44 +10001135 if (ses->server->ops->query_all_EAs) {
1136 if (!ea_value) {
1137 rc = ses->server->ops->query_all_EAs(xid, tcon, path,
1138 ea_name, NULL, 0,
1139 cifs_sb);
1140 if (rc == -ENODATA)
1141 goto sea_exit;
1142 }
1143 }
1144
Ronnie Sahlberg0967e542018-11-06 22:52:43 +10001145 /* Open */
1146 memset(&open_iov, 0, sizeof(open_iov));
1147 rqst[0].rq_iov = open_iov;
1148 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
1149
1150 memset(&oparms, 0, sizeof(oparms));
Ronnie Sahlberg55175542017-08-24 11:24:56 +10001151 oparms.tcon = tcon;
1152 oparms.desired_access = FILE_WRITE_EA;
1153 oparms.disposition = FILE_OPEN;
Steve French5e196972018-08-27 17:04:13 -05001154 if (backup_cred(cifs_sb))
1155 oparms.create_options = CREATE_OPEN_BACKUP_INTENT;
1156 else
1157 oparms.create_options = 0;
Ronnie Sahlberg55175542017-08-24 11:24:56 +10001158 oparms.fid = &fid;
1159 oparms.reconnect = false;
1160
Ronnie Sahlberg0967e542018-11-06 22:52:43 +10001161 rc = SMB2_open_init(tcon, &rqst[0], &oplock, &oparms, utf16_path);
1162 if (rc)
1163 goto sea_exit;
Ronnie Sahlberge77fe732018-12-31 13:43:40 +10001164 smb2_set_next_command(tcon, &rqst[0]);
Ronnie Sahlberg0967e542018-11-06 22:52:43 +10001165
1166
1167 /* Set Info */
1168 memset(&si_iov, 0, sizeof(si_iov));
1169 rqst[1].rq_iov = si_iov;
1170 rqst[1].rq_nvec = 1;
Ronnie Sahlberg55175542017-08-24 11:24:56 +10001171
1172 len = sizeof(ea) + ea_name_len + ea_value_len + 1;
1173 ea = kzalloc(len, GFP_KERNEL);
1174 if (ea == NULL) {
Ronnie Sahlberg0967e542018-11-06 22:52:43 +10001175 rc = -ENOMEM;
1176 goto sea_exit;
Ronnie Sahlberg55175542017-08-24 11:24:56 +10001177 }
1178
1179 ea->ea_name_length = ea_name_len;
1180 ea->ea_value_length = cpu_to_le16(ea_value_len);
1181 memcpy(ea->ea_data, ea_name, ea_name_len + 1);
1182 memcpy(ea->ea_data + ea_name_len + 1, ea_value, ea_value_len);
1183
Ronnie Sahlberg0967e542018-11-06 22:52:43 +10001184 size[0] = len;
1185 data[0] = ea;
1186
1187 rc = SMB2_set_info_init(tcon, &rqst[1], COMPOUND_FID,
1188 COMPOUND_FID, current->tgid,
1189 FILE_FULL_EA_INFORMATION,
1190 SMB2_O_INFO_FILE, 0, data, size);
Ronnie Sahlberge77fe732018-12-31 13:43:40 +10001191 smb2_set_next_command(tcon, &rqst[1]);
Ronnie Sahlberg0967e542018-11-06 22:52:43 +10001192 smb2_set_related(&rqst[1]);
1193
1194
1195 /* Close */
1196 memset(&close_iov, 0, sizeof(close_iov));
1197 rqst[2].rq_iov = close_iov;
1198 rqst[2].rq_nvec = 1;
Steve French43f8a6a2019-12-02 21:46:54 -06001199 rc = SMB2_close_init(tcon, &rqst[2], COMPOUND_FID, COMPOUND_FID, false);
Ronnie Sahlberg0967e542018-11-06 22:52:43 +10001200 smb2_set_related(&rqst[2]);
1201
1202 rc = compound_send_recv(xid, ses, flags, 3, rqst,
1203 resp_buftype, rsp_iov);
Steve Frenchd2f15422019-09-22 00:55:46 -05001204 /* no need to bump num_remote_opens because handle immediately closed */
Ronnie Sahlberg0967e542018-11-06 22:52:43 +10001205
1206 sea_exit:
Paulo Alcantara6aa0c112018-07-04 14:16:16 -03001207 kfree(ea);
Ronnie Sahlberg0967e542018-11-06 22:52:43 +10001208 kfree(utf16_path);
1209 SMB2_open_free(&rqst[0]);
1210 SMB2_set_info_free(&rqst[1]);
1211 SMB2_close_free(&rqst[2]);
1212 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
1213 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
1214 free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
Ronnie Sahlberg55175542017-08-24 11:24:56 +10001215 return rc;
1216}
Arnd Bergmann1368f152017-09-05 11:24:15 +02001217#endif
Ronnie Sahlberg55175542017-08-24 11:24:56 +10001218
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04001219static bool
1220smb2_can_echo(struct TCP_Server_Info *server)
1221{
1222 return server->echoes;
1223}
1224
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001225static void
1226smb2_clear_stats(struct cifs_tcon *tcon)
1227{
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001228 int i;
Christoph Probsta205d502019-05-08 21:36:25 +02001229
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001230 for (i = 0; i < NUMBER_OF_SMB2_COMMANDS; i++) {
1231 atomic_set(&tcon->stats.smb2_stats.smb2_com_sent[i], 0);
1232 atomic_set(&tcon->stats.smb2_stats.smb2_com_failed[i], 0);
1233 }
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001234}
1235
1236static void
Steve French769ee6a2013-06-19 14:15:30 -05001237smb2_dump_share_caps(struct seq_file *m, struct cifs_tcon *tcon)
1238{
1239 seq_puts(m, "\n\tShare Capabilities:");
1240 if (tcon->capabilities & SMB2_SHARE_CAP_DFS)
1241 seq_puts(m, " DFS,");
1242 if (tcon->capabilities & SMB2_SHARE_CAP_CONTINUOUS_AVAILABILITY)
1243 seq_puts(m, " CONTINUOUS AVAILABILITY,");
1244 if (tcon->capabilities & SMB2_SHARE_CAP_SCALEOUT)
1245 seq_puts(m, " SCALEOUT,");
1246 if (tcon->capabilities & SMB2_SHARE_CAP_CLUSTER)
1247 seq_puts(m, " CLUSTER,");
1248 if (tcon->capabilities & SMB2_SHARE_CAP_ASYMMETRIC)
1249 seq_puts(m, " ASYMMETRIC,");
1250 if (tcon->capabilities == 0)
1251 seq_puts(m, " None");
Steven Frenchaf6a12e2013-10-09 20:55:53 -05001252 if (tcon->ss_flags & SSINFO_FLAGS_ALIGNED_DEVICE)
1253 seq_puts(m, " Aligned,");
1254 if (tcon->ss_flags & SSINFO_FLAGS_PARTITION_ALIGNED_ON_DEVICE)
1255 seq_puts(m, " Partition Aligned,");
1256 if (tcon->ss_flags & SSINFO_FLAGS_NO_SEEK_PENALTY)
1257 seq_puts(m, " SSD,");
1258 if (tcon->ss_flags & SSINFO_FLAGS_TRIM_ENABLED)
1259 seq_puts(m, " TRIM-support,");
1260
Steve French769ee6a2013-06-19 14:15:30 -05001261 seq_printf(m, "\tShare Flags: 0x%x", tcon->share_flags);
Steve Frenche0386e42018-05-20 01:27:03 -05001262 seq_printf(m, "\n\ttid: 0x%x", tcon->tid);
Steven Frenchaf6a12e2013-10-09 20:55:53 -05001263 if (tcon->perf_sector_size)
1264 seq_printf(m, "\tOptimal sector size: 0x%x",
1265 tcon->perf_sector_size);
Steve Frenche0386e42018-05-20 01:27:03 -05001266 seq_printf(m, "\tMaximal Access: 0x%x", tcon->maximal_access);
Steve French769ee6a2013-06-19 14:15:30 -05001267}
1268
1269static void
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001270smb2_print_stats(struct seq_file *m, struct cifs_tcon *tcon)
1271{
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001272 atomic_t *sent = tcon->stats.smb2_stats.smb2_com_sent;
1273 atomic_t *failed = tcon->stats.smb2_stats.smb2_com_failed;
Steve French1995d282018-07-27 15:14:04 -05001274
1275 /*
1276 * Can't display SMB2_NEGOTIATE, SESSION_SETUP, LOGOFF, CANCEL and ECHO
1277 * totals (requests sent) since those SMBs are per-session not per tcon
1278 */
Steve French52ce1ac2018-07-31 01:46:47 -05001279 seq_printf(m, "\nBytes read: %llu Bytes written: %llu",
1280 (long long)(tcon->bytes_read),
1281 (long long)(tcon->bytes_written));
Steve Frenchfae80442018-10-19 17:14:32 -05001282 seq_printf(m, "\nOpen files: %d total (local), %d open on server",
1283 atomic_read(&tcon->num_local_opens),
1284 atomic_read(&tcon->num_remote_opens));
Steve French1995d282018-07-27 15:14:04 -05001285 seq_printf(m, "\nTreeConnects: %d total %d failed",
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001286 atomic_read(&sent[SMB2_TREE_CONNECT_HE]),
1287 atomic_read(&failed[SMB2_TREE_CONNECT_HE]));
Steve French1995d282018-07-27 15:14:04 -05001288 seq_printf(m, "\nTreeDisconnects: %d total %d failed",
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001289 atomic_read(&sent[SMB2_TREE_DISCONNECT_HE]),
1290 atomic_read(&failed[SMB2_TREE_DISCONNECT_HE]));
Steve French1995d282018-07-27 15:14:04 -05001291 seq_printf(m, "\nCreates: %d total %d failed",
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001292 atomic_read(&sent[SMB2_CREATE_HE]),
1293 atomic_read(&failed[SMB2_CREATE_HE]));
Steve French1995d282018-07-27 15:14:04 -05001294 seq_printf(m, "\nCloses: %d total %d failed",
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001295 atomic_read(&sent[SMB2_CLOSE_HE]),
1296 atomic_read(&failed[SMB2_CLOSE_HE]));
Steve French1995d282018-07-27 15:14:04 -05001297 seq_printf(m, "\nFlushes: %d total %d failed",
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001298 atomic_read(&sent[SMB2_FLUSH_HE]),
1299 atomic_read(&failed[SMB2_FLUSH_HE]));
Steve French1995d282018-07-27 15:14:04 -05001300 seq_printf(m, "\nReads: %d total %d failed",
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001301 atomic_read(&sent[SMB2_READ_HE]),
1302 atomic_read(&failed[SMB2_READ_HE]));
Steve French1995d282018-07-27 15:14:04 -05001303 seq_printf(m, "\nWrites: %d total %d failed",
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001304 atomic_read(&sent[SMB2_WRITE_HE]),
1305 atomic_read(&failed[SMB2_WRITE_HE]));
Steve French1995d282018-07-27 15:14:04 -05001306 seq_printf(m, "\nLocks: %d total %d failed",
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001307 atomic_read(&sent[SMB2_LOCK_HE]),
1308 atomic_read(&failed[SMB2_LOCK_HE]));
Steve French1995d282018-07-27 15:14:04 -05001309 seq_printf(m, "\nIOCTLs: %d total %d failed",
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001310 atomic_read(&sent[SMB2_IOCTL_HE]),
1311 atomic_read(&failed[SMB2_IOCTL_HE]));
Steve French1995d282018-07-27 15:14:04 -05001312 seq_printf(m, "\nQueryDirectories: %d total %d failed",
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001313 atomic_read(&sent[SMB2_QUERY_DIRECTORY_HE]),
1314 atomic_read(&failed[SMB2_QUERY_DIRECTORY_HE]));
Steve French1995d282018-07-27 15:14:04 -05001315 seq_printf(m, "\nChangeNotifies: %d total %d failed",
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001316 atomic_read(&sent[SMB2_CHANGE_NOTIFY_HE]),
1317 atomic_read(&failed[SMB2_CHANGE_NOTIFY_HE]));
Steve French1995d282018-07-27 15:14:04 -05001318 seq_printf(m, "\nQueryInfos: %d total %d failed",
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001319 atomic_read(&sent[SMB2_QUERY_INFO_HE]),
1320 atomic_read(&failed[SMB2_QUERY_INFO_HE]));
Steve French1995d282018-07-27 15:14:04 -05001321 seq_printf(m, "\nSetInfos: %d total %d failed",
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001322 atomic_read(&sent[SMB2_SET_INFO_HE]),
1323 atomic_read(&failed[SMB2_SET_INFO_HE]));
1324 seq_printf(m, "\nOplockBreaks: %d sent %d failed",
1325 atomic_read(&sent[SMB2_OPLOCK_BREAK_HE]),
1326 atomic_read(&failed[SMB2_OPLOCK_BREAK_HE]));
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001327}
1328
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001329static void
1330smb2_set_fid(struct cifsFileInfo *cfile, struct cifs_fid *fid, __u32 oplock)
1331{
David Howells2b0143b2015-03-17 22:25:59 +00001332 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04001333 struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server;
1334
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001335 cfile->fid.persistent_fid = fid->persistent_fid;
1336 cfile->fid.volatile_fid = fid->volatile_fid;
Steve Frenchdfe33f92018-10-30 19:50:31 -05001337#ifdef CONFIG_CIFS_DEBUG2
1338 cfile->fid.mid = fid->mid;
1339#endif /* CIFS_DEBUG2 */
Pavel Shilovsky42873b02013-09-05 21:30:16 +04001340 server->ops->set_oplock_level(cinode, oplock, fid->epoch,
1341 &fid->purge_cache);
Pavel Shilovsky18cceb62013-09-05 13:01:06 +04001342 cinode->can_cache_brlcks = CIFS_CACHE_WRITE(cinode);
Aurelien Aptel94f87372016-09-22 07:38:50 +02001343 memcpy(cfile->fid.create_guid, fid->create_guid, 16);
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001344}
1345
Pavel Shilovsky760ad0c2012-09-25 11:00:07 +04001346static void
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001347smb2_close_file(const unsigned int xid, struct cifs_tcon *tcon,
1348 struct cifs_fid *fid)
1349{
Pavel Shilovsky760ad0c2012-09-25 11:00:07 +04001350 SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001351}
1352
Steve French43f8a6a2019-12-02 21:46:54 -06001353static void
1354smb2_close_getattr(const unsigned int xid, struct cifs_tcon *tcon,
1355 struct cifsFileInfo *cfile)
1356{
1357 struct smb2_file_network_open_info file_inf;
1358 struct inode *inode;
1359 int rc;
1360
1361 rc = __SMB2_close(xid, tcon, cfile->fid.persistent_fid,
1362 cfile->fid.volatile_fid, &file_inf);
1363 if (rc)
1364 return;
1365
1366 inode = d_inode(cfile->dentry);
1367
1368 spin_lock(&inode->i_lock);
1369 CIFS_I(inode)->time = jiffies;
1370
1371 /* Creation time should not need to be updated on close */
1372 if (file_inf.LastWriteTime)
1373 inode->i_mtime = cifs_NTtimeToUnix(file_inf.LastWriteTime);
1374 if (file_inf.ChangeTime)
1375 inode->i_ctime = cifs_NTtimeToUnix(file_inf.ChangeTime);
1376 if (file_inf.LastAccessTime)
1377 inode->i_atime = cifs_NTtimeToUnix(file_inf.LastAccessTime);
1378
1379 /*
1380 * i_blocks is not related to (i_size / i_blksize),
1381 * but instead 512 byte (2**9) size is required for
1382 * calculating num blocks.
1383 */
1384 if (le64_to_cpu(file_inf.AllocationSize) > 4096)
1385 inode->i_blocks =
1386 (512 - 1 + le64_to_cpu(file_inf.AllocationSize)) >> 9;
1387
1388 /* End of file and Attributes should not have to be updated on close */
1389 spin_unlock(&inode->i_lock);
1390}
1391
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07001392static int
Steve French41c13582013-11-14 00:05:36 -06001393SMB2_request_res_key(const unsigned int xid, struct cifs_tcon *tcon,
1394 u64 persistent_fid, u64 volatile_fid,
1395 struct copychunk_ioctl *pcchunk)
1396{
1397 int rc;
1398 unsigned int ret_data_len;
1399 struct resume_key_req *res_key;
1400
1401 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
1402 FSCTL_SRV_REQUEST_RESUME_KEY, true /* is_fsctl */,
Steve French153322f2019-03-28 22:32:49 -05001403 NULL, 0 /* no input */, CIFSMaxBufSize,
Steve French41c13582013-11-14 00:05:36 -06001404 (char **)&res_key, &ret_data_len);
1405
1406 if (rc) {
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +10001407 cifs_tcon_dbg(VFS, "refcpy ioctl error %d getting resume key\n", rc);
Steve French41c13582013-11-14 00:05:36 -06001408 goto req_res_key_exit;
1409 }
1410 if (ret_data_len < sizeof(struct resume_key_req)) {
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +10001411 cifs_tcon_dbg(VFS, "Invalid refcopy resume key length\n");
Steve French41c13582013-11-14 00:05:36 -06001412 rc = -EINVAL;
1413 goto req_res_key_exit;
1414 }
1415 memcpy(pcchunk->SourceKey, res_key->ResumeKey, COPY_CHUNK_RES_KEY_SIZE);
1416
1417req_res_key_exit:
1418 kfree(res_key);
1419 return rc;
1420}
1421
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05001422static int
1423smb2_ioctl_query_info(const unsigned int xid,
Ronnie Sahlberg8d8b26e2018-10-17 05:47:58 +10001424 struct cifs_tcon *tcon,
1425 __le16 *path, int is_dir,
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05001426 unsigned long p)
1427{
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05001428 struct cifs_ses *ses = tcon->ses;
1429 char __user *arg = (char __user *)p;
1430 struct smb_query_info qi;
1431 struct smb_query_info __user *pqi;
1432 int rc = 0;
1433 int flags = 0;
Ronnie Sahlbergf5778c32019-03-15 09:07:22 +10001434 struct smb2_query_info_rsp *qi_rsp = NULL;
1435 struct smb2_ioctl_rsp *io_rsp = NULL;
Ronnie Sahlberg8d8b26e2018-10-17 05:47:58 +10001436 void *buffer = NULL;
1437 struct smb_rqst rqst[3];
1438 int resp_buftype[3];
1439 struct kvec rsp_iov[3];
1440 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
1441 struct cifs_open_parms oparms;
1442 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
1443 struct cifs_fid fid;
1444 struct kvec qi_iov[1];
Ronnie Sahlbergf5778c32019-03-15 09:07:22 +10001445 struct kvec io_iov[SMB2_IOCTL_IOV_SIZE];
Ronnie Sahlberg0e906962019-07-25 13:08:43 +10001446 struct kvec si_iov[SMB2_SET_INFO_IOV_SIZE];
Ronnie Sahlberg8d8b26e2018-10-17 05:47:58 +10001447 struct kvec close_iov[1];
Ronnie Sahlberg0e906962019-07-25 13:08:43 +10001448 unsigned int size[2];
1449 void *data[2];
Ronnie Sahlberg8d8b26e2018-10-17 05:47:58 +10001450
1451 memset(rqst, 0, sizeof(rqst));
1452 resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
1453 memset(rsp_iov, 0, sizeof(rsp_iov));
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05001454
1455 if (copy_from_user(&qi, arg, sizeof(struct smb_query_info)))
1456 return -EFAULT;
1457
1458 if (qi.output_buffer_length > 1024)
1459 return -EINVAL;
1460
1461 if (!ses || !(ses->server))
1462 return -EIO;
1463
1464 if (smb3_encryption_required(tcon))
1465 flags |= CIFS_TRANSFORM_REQ;
1466
Markus Elfringcfaa1182019-11-05 21:30:25 +01001467 buffer = memdup_user(arg + sizeof(struct smb_query_info),
1468 qi.output_buffer_length);
1469 if (IS_ERR(buffer))
1470 return PTR_ERR(buffer);
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05001471
Ronnie Sahlberg8d8b26e2018-10-17 05:47:58 +10001472 /* Open */
1473 memset(&open_iov, 0, sizeof(open_iov));
1474 rqst[0].rq_iov = open_iov;
1475 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05001476
Ronnie Sahlberg8d8b26e2018-10-17 05:47:58 +10001477 memset(&oparms, 0, sizeof(oparms));
1478 oparms.tcon = tcon;
Ronnie Sahlberg8d8b26e2018-10-17 05:47:58 +10001479 oparms.disposition = FILE_OPEN;
1480 if (is_dir)
1481 oparms.create_options = CREATE_NOT_FILE;
1482 else
1483 oparms.create_options = CREATE_NOT_DIR;
1484 oparms.fid = &fid;
1485 oparms.reconnect = false;
1486
Ronnie Sahlbergefac7792019-04-11 12:20:17 +10001487 if (qi.flags & PASSTHRU_FSCTL) {
1488 switch (qi.info_type & FSCTL_DEVICE_ACCESS_MASK) {
1489 case FSCTL_DEVICE_ACCESS_FILE_READ_WRITE_ACCESS:
1490 oparms.desired_access = FILE_READ_DATA | FILE_WRITE_DATA | FILE_READ_ATTRIBUTES | SYNCHRONIZE;
Steve French46e66612019-04-11 13:53:17 -05001491 break;
1492 case FSCTL_DEVICE_ACCESS_FILE_ANY_ACCESS:
1493 oparms.desired_access = GENERIC_ALL;
1494 break;
1495 case FSCTL_DEVICE_ACCESS_FILE_READ_ACCESS:
1496 oparms.desired_access = GENERIC_READ;
1497 break;
1498 case FSCTL_DEVICE_ACCESS_FILE_WRITE_ACCESS:
1499 oparms.desired_access = GENERIC_WRITE;
Ronnie Sahlbergefac7792019-04-11 12:20:17 +10001500 break;
1501 }
Ronnie Sahlberg0e906962019-07-25 13:08:43 +10001502 } else if (qi.flags & PASSTHRU_SET_INFO) {
1503 oparms.desired_access = GENERIC_WRITE;
1504 } else {
1505 oparms.desired_access = FILE_READ_ATTRIBUTES | READ_CONTROL;
Ronnie Sahlbergefac7792019-04-11 12:20:17 +10001506 }
1507
Ronnie Sahlberg8d8b26e2018-10-17 05:47:58 +10001508 rc = SMB2_open_init(tcon, &rqst[0], &oplock, &oparms, path);
1509 if (rc)
1510 goto iqinf_exit;
Ronnie Sahlberge77fe732018-12-31 13:43:40 +10001511 smb2_set_next_command(tcon, &rqst[0]);
Ronnie Sahlberg8d8b26e2018-10-17 05:47:58 +10001512
1513 /* Query */
Steve French31ba4332019-03-13 02:40:07 -05001514 if (qi.flags & PASSTHRU_FSCTL) {
1515 /* Can eventually relax perm check since server enforces too */
1516 if (!capable(CAP_SYS_ADMIN))
1517 rc = -EPERM;
Ronnie Sahlbergf5778c32019-03-15 09:07:22 +10001518 else {
1519 memset(&io_iov, 0, sizeof(io_iov));
1520 rqst[1].rq_iov = io_iov;
1521 rqst[1].rq_nvec = SMB2_IOCTL_IOV_SIZE;
1522
1523 rc = SMB2_ioctl_init(tcon, &rqst[1],
1524 COMPOUND_FID, COMPOUND_FID,
Ronnie Sahlbergefac7792019-04-11 12:20:17 +10001525 qi.info_type, true, buffer,
1526 qi.output_buffer_length,
Ronnie Sahlberg731b82b2020-01-08 13:08:07 +10001527 CIFSMaxBufSize -
1528 MAX_SMB2_CREATE_RESPONSE_SIZE -
1529 MAX_SMB2_CLOSE_RESPONSE_SIZE);
Ronnie Sahlbergf5778c32019-03-15 09:07:22 +10001530 }
Ronnie Sahlberg0e906962019-07-25 13:08:43 +10001531 } else if (qi.flags == PASSTHRU_SET_INFO) {
1532 /* Can eventually relax perm check since server enforces too */
1533 if (!capable(CAP_SYS_ADMIN))
1534 rc = -EPERM;
1535 else {
1536 memset(&si_iov, 0, sizeof(si_iov));
1537 rqst[1].rq_iov = si_iov;
1538 rqst[1].rq_nvec = 1;
1539
1540 size[0] = 8;
1541 data[0] = buffer;
1542
1543 rc = SMB2_set_info_init(tcon, &rqst[1],
1544 COMPOUND_FID, COMPOUND_FID,
1545 current->tgid,
1546 FILE_END_OF_FILE_INFORMATION,
1547 SMB2_O_INFO_FILE, 0, data, size);
1548 }
Steve French31ba4332019-03-13 02:40:07 -05001549 } else if (qi.flags == PASSTHRU_QUERY_INFO) {
1550 memset(&qi_iov, 0, sizeof(qi_iov));
1551 rqst[1].rq_iov = qi_iov;
1552 rqst[1].rq_nvec = 1;
Ronnie Sahlberg8d8b26e2018-10-17 05:47:58 +10001553
Steve French31ba4332019-03-13 02:40:07 -05001554 rc = SMB2_query_info_init(tcon, &rqst[1], COMPOUND_FID,
1555 COMPOUND_FID, qi.file_info_class,
1556 qi.info_type, qi.additional_information,
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05001557 qi.input_buffer_length,
1558 qi.output_buffer_length, buffer);
Steve French31ba4332019-03-13 02:40:07 -05001559 } else { /* unknown flags */
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +10001560 cifs_tcon_dbg(VFS, "invalid passthru query flags: 0x%x\n", qi.flags);
Steve French31ba4332019-03-13 02:40:07 -05001561 rc = -EINVAL;
1562 }
1563
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05001564 if (rc)
1565 goto iqinf_exit;
Ronnie Sahlberge77fe732018-12-31 13:43:40 +10001566 smb2_set_next_command(tcon, &rqst[1]);
Ronnie Sahlberg8d8b26e2018-10-17 05:47:58 +10001567 smb2_set_related(&rqst[1]);
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05001568
Ronnie Sahlberg8d8b26e2018-10-17 05:47:58 +10001569 /* Close */
1570 memset(&close_iov, 0, sizeof(close_iov));
1571 rqst[2].rq_iov = close_iov;
1572 rqst[2].rq_nvec = 1;
1573
Steve French43f8a6a2019-12-02 21:46:54 -06001574 rc = SMB2_close_init(tcon, &rqst[2], COMPOUND_FID, COMPOUND_FID, false);
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05001575 if (rc)
1576 goto iqinf_exit;
Ronnie Sahlberg8d8b26e2018-10-17 05:47:58 +10001577 smb2_set_related(&rqst[2]);
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05001578
Ronnie Sahlberg8d8b26e2018-10-17 05:47:58 +10001579 rc = compound_send_recv(xid, ses, flags, 3, rqst,
1580 resp_buftype, rsp_iov);
1581 if (rc)
1582 goto iqinf_exit;
Steve Frenchd2f15422019-09-22 00:55:46 -05001583
1584 /* No need to bump num_remote_opens since handle immediately closed */
Ronnie Sahlbergf5778c32019-03-15 09:07:22 +10001585 if (qi.flags & PASSTHRU_FSCTL) {
1586 pqi = (struct smb_query_info __user *)arg;
1587 io_rsp = (struct smb2_ioctl_rsp *)rsp_iov[1].iov_base;
1588 if (le32_to_cpu(io_rsp->OutputCount) < qi.input_buffer_length)
1589 qi.input_buffer_length = le32_to_cpu(io_rsp->OutputCount);
Ronnie Sahlberg5242fcb2019-04-15 12:13:52 +10001590 if (qi.input_buffer_length > 0 &&
Markus Elfring2b1116b2019-11-05 22:26:53 +01001591 le32_to_cpu(io_rsp->OutputOffset) + qi.input_buffer_length
1592 > rsp_iov[1].iov_len)
1593 goto e_fault;
1594
1595 if (copy_to_user(&pqi->input_buffer_length,
1596 &qi.input_buffer_length,
1597 sizeof(qi.input_buffer_length)))
1598 goto e_fault;
1599
Ronnie Sahlberg5242fcb2019-04-15 12:13:52 +10001600 if (copy_to_user((void __user *)pqi + sizeof(struct smb_query_info),
1601 (const void *)io_rsp + le32_to_cpu(io_rsp->OutputOffset),
Markus Elfring2b1116b2019-11-05 22:26:53 +01001602 qi.input_buffer_length))
1603 goto e_fault;
Ronnie Sahlbergf5778c32019-03-15 09:07:22 +10001604 } else {
1605 pqi = (struct smb_query_info __user *)arg;
1606 qi_rsp = (struct smb2_query_info_rsp *)rsp_iov[1].iov_base;
1607 if (le32_to_cpu(qi_rsp->OutputBufferLength) < qi.input_buffer_length)
1608 qi.input_buffer_length = le32_to_cpu(qi_rsp->OutputBufferLength);
Markus Elfring2b1116b2019-11-05 22:26:53 +01001609 if (copy_to_user(&pqi->input_buffer_length,
1610 &qi.input_buffer_length,
1611 sizeof(qi.input_buffer_length)))
1612 goto e_fault;
1613
1614 if (copy_to_user(pqi + 1, qi_rsp->Buffer,
1615 qi.input_buffer_length))
1616 goto e_fault;
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05001617 }
1618
1619 iqinf_exit:
Ronnie Sahlberg8d8b26e2018-10-17 05:47:58 +10001620 kfree(buffer);
1621 SMB2_open_free(&rqst[0]);
Ronnie Sahlbergf5778c32019-03-15 09:07:22 +10001622 if (qi.flags & PASSTHRU_FSCTL)
1623 SMB2_ioctl_free(&rqst[1]);
1624 else
1625 SMB2_query_info_free(&rqst[1]);
1626
Ronnie Sahlberg8d8b26e2018-10-17 05:47:58 +10001627 SMB2_close_free(&rqst[2]);
1628 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
1629 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
1630 free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05001631 return rc;
Markus Elfring2b1116b2019-11-05 22:26:53 +01001632
1633e_fault:
1634 rc = -EFAULT;
1635 goto iqinf_exit;
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05001636}
1637
Sachin Prabhu620d8742017-02-10 16:03:51 +05301638static ssize_t
Sachin Prabhu312bbc52017-04-04 02:12:04 -05001639smb2_copychunk_range(const unsigned int xid,
Steve French41c13582013-11-14 00:05:36 -06001640 struct cifsFileInfo *srcfile,
1641 struct cifsFileInfo *trgtfile, u64 src_off,
1642 u64 len, u64 dest_off)
1643{
1644 int rc;
1645 unsigned int ret_data_len;
1646 struct copychunk_ioctl *pcchunk;
Steve French9bf0c9c2013-11-16 18:05:28 -06001647 struct copychunk_ioctl_rsp *retbuf = NULL;
1648 struct cifs_tcon *tcon;
1649 int chunks_copied = 0;
1650 bool chunk_sizes_updated = false;
Sachin Prabhu620d8742017-02-10 16:03:51 +05301651 ssize_t bytes_written, total_bytes_written = 0;
Steve French41c13582013-11-14 00:05:36 -06001652
1653 pcchunk = kmalloc(sizeof(struct copychunk_ioctl), GFP_KERNEL);
1654
1655 if (pcchunk == NULL)
1656 return -ENOMEM;
1657
Christoph Probsta205d502019-05-08 21:36:25 +02001658 cifs_dbg(FYI, "%s: about to call request res key\n", __func__);
Steve French41c13582013-11-14 00:05:36 -06001659 /* Request a key from the server to identify the source of the copy */
1660 rc = SMB2_request_res_key(xid, tlink_tcon(srcfile->tlink),
1661 srcfile->fid.persistent_fid,
1662 srcfile->fid.volatile_fid, pcchunk);
1663
1664 /* Note: request_res_key sets res_key null only if rc !=0 */
1665 if (rc)
Steve French9bf0c9c2013-11-16 18:05:28 -06001666 goto cchunk_out;
Steve French41c13582013-11-14 00:05:36 -06001667
1668 /* For now array only one chunk long, will make more flexible later */
Fabian Frederickbc09d142014-12-10 15:41:15 -08001669 pcchunk->ChunkCount = cpu_to_le32(1);
Steve French41c13582013-11-14 00:05:36 -06001670 pcchunk->Reserved = 0;
Steve French41c13582013-11-14 00:05:36 -06001671 pcchunk->Reserved2 = 0;
1672
Steve French9bf0c9c2013-11-16 18:05:28 -06001673 tcon = tlink_tcon(trgtfile->tlink);
1674
1675 while (len > 0) {
1676 pcchunk->SourceOffset = cpu_to_le64(src_off);
1677 pcchunk->TargetOffset = cpu_to_le64(dest_off);
1678 pcchunk->Length =
1679 cpu_to_le32(min_t(u32, len, tcon->max_bytes_chunk));
1680
1681 /* Request server copy to target from src identified by key */
1682 rc = SMB2_ioctl(xid, tcon, trgtfile->fid.persistent_fid,
Steve French41c13582013-11-14 00:05:36 -06001683 trgtfile->fid.volatile_fid, FSCTL_SRV_COPYCHUNK_WRITE,
Aurelien Aptel63a83b82018-01-24 13:46:11 +01001684 true /* is_fsctl */, (char *)pcchunk,
Steve French153322f2019-03-28 22:32:49 -05001685 sizeof(struct copychunk_ioctl), CIFSMaxBufSize,
1686 (char **)&retbuf, &ret_data_len);
Steve French9bf0c9c2013-11-16 18:05:28 -06001687 if (rc == 0) {
1688 if (ret_data_len !=
1689 sizeof(struct copychunk_ioctl_rsp)) {
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +10001690 cifs_tcon_dbg(VFS, "invalid cchunk response size\n");
Steve French9bf0c9c2013-11-16 18:05:28 -06001691 rc = -EIO;
1692 goto cchunk_out;
1693 }
1694 if (retbuf->TotalBytesWritten == 0) {
1695 cifs_dbg(FYI, "no bytes copied\n");
1696 rc = -EIO;
1697 goto cchunk_out;
1698 }
1699 /*
1700 * Check if server claimed to write more than we asked
1701 */
1702 if (le32_to_cpu(retbuf->TotalBytesWritten) >
1703 le32_to_cpu(pcchunk->Length)) {
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +10001704 cifs_tcon_dbg(VFS, "invalid copy chunk response\n");
Steve French9bf0c9c2013-11-16 18:05:28 -06001705 rc = -EIO;
1706 goto cchunk_out;
1707 }
1708 if (le32_to_cpu(retbuf->ChunksWritten) != 1) {
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +10001709 cifs_tcon_dbg(VFS, "invalid num chunks written\n");
Steve French9bf0c9c2013-11-16 18:05:28 -06001710 rc = -EIO;
1711 goto cchunk_out;
1712 }
1713 chunks_copied++;
Steve French41c13582013-11-14 00:05:36 -06001714
Sachin Prabhu620d8742017-02-10 16:03:51 +05301715 bytes_written = le32_to_cpu(retbuf->TotalBytesWritten);
1716 src_off += bytes_written;
1717 dest_off += bytes_written;
1718 len -= bytes_written;
1719 total_bytes_written += bytes_written;
Steve French41c13582013-11-14 00:05:36 -06001720
Sachin Prabhu620d8742017-02-10 16:03:51 +05301721 cifs_dbg(FYI, "Chunks %d PartialChunk %d Total %zu\n",
Steve French9bf0c9c2013-11-16 18:05:28 -06001722 le32_to_cpu(retbuf->ChunksWritten),
1723 le32_to_cpu(retbuf->ChunkBytesWritten),
Sachin Prabhu620d8742017-02-10 16:03:51 +05301724 bytes_written);
Steve French9bf0c9c2013-11-16 18:05:28 -06001725 } else if (rc == -EINVAL) {
1726 if (ret_data_len != sizeof(struct copychunk_ioctl_rsp))
1727 goto cchunk_out;
Steve French41c13582013-11-14 00:05:36 -06001728
Steve French9bf0c9c2013-11-16 18:05:28 -06001729 cifs_dbg(FYI, "MaxChunks %d BytesChunk %d MaxCopy %d\n",
1730 le32_to_cpu(retbuf->ChunksWritten),
1731 le32_to_cpu(retbuf->ChunkBytesWritten),
1732 le32_to_cpu(retbuf->TotalBytesWritten));
1733
1734 /*
1735 * Check if this is the first request using these sizes,
1736 * (ie check if copy succeed once with original sizes
1737 * and check if the server gave us different sizes after
1738 * we already updated max sizes on previous request).
1739 * if not then why is the server returning an error now
1740 */
1741 if ((chunks_copied != 0) || chunk_sizes_updated)
1742 goto cchunk_out;
1743
1744 /* Check that server is not asking us to grow size */
1745 if (le32_to_cpu(retbuf->ChunkBytesWritten) <
1746 tcon->max_bytes_chunk)
1747 tcon->max_bytes_chunk =
1748 le32_to_cpu(retbuf->ChunkBytesWritten);
1749 else
1750 goto cchunk_out; /* server gave us bogus size */
1751
1752 /* No need to change MaxChunks since already set to 1 */
1753 chunk_sizes_updated = true;
Sachin Prabhu2477bc52015-02-04 13:10:26 +00001754 } else
1755 goto cchunk_out;
Steve French9bf0c9c2013-11-16 18:05:28 -06001756 }
1757
1758cchunk_out:
Steve French41c13582013-11-14 00:05:36 -06001759 kfree(pcchunk);
Steve French24df1482016-09-29 04:20:23 -05001760 kfree(retbuf);
Sachin Prabhu620d8742017-02-10 16:03:51 +05301761 if (rc)
1762 return rc;
1763 else
1764 return total_bytes_written;
Steve French41c13582013-11-14 00:05:36 -06001765}
1766
1767static int
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07001768smb2_flush_file(const unsigned int xid, struct cifs_tcon *tcon,
1769 struct cifs_fid *fid)
1770{
1771 return SMB2_flush(xid, tcon, fid->persistent_fid, fid->volatile_fid);
1772}
1773
Pavel Shilovsky09a47072012-09-18 16:20:29 -07001774static unsigned int
1775smb2_read_data_offset(char *buf)
1776{
1777 struct smb2_read_rsp *rsp = (struct smb2_read_rsp *)buf;
Christoph Probsta205d502019-05-08 21:36:25 +02001778
Pavel Shilovsky09a47072012-09-18 16:20:29 -07001779 return rsp->DataOffset;
1780}
1781
1782static unsigned int
Long Li74dcf412017-11-22 17:38:46 -07001783smb2_read_data_length(char *buf, bool in_remaining)
Pavel Shilovsky09a47072012-09-18 16:20:29 -07001784{
1785 struct smb2_read_rsp *rsp = (struct smb2_read_rsp *)buf;
Long Li74dcf412017-11-22 17:38:46 -07001786
1787 if (in_remaining)
1788 return le32_to_cpu(rsp->DataRemaining);
1789
Pavel Shilovsky09a47072012-09-18 16:20:29 -07001790 return le32_to_cpu(rsp->DataLength);
1791}
1792
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07001793
1794static int
Steve Frenchdb8b6312014-09-22 05:13:55 -05001795smb2_sync_read(const unsigned int xid, struct cifs_fid *pfid,
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07001796 struct cifs_io_parms *parms, unsigned int *bytes_read,
1797 char **buf, int *buf_type)
1798{
Steve Frenchdb8b6312014-09-22 05:13:55 -05001799 parms->persistent_fid = pfid->persistent_fid;
1800 parms->volatile_fid = pfid->volatile_fid;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07001801 return SMB2_read(xid, parms, bytes_read, buf, buf_type);
1802}
1803
Pavel Shilovsky009d3442012-09-18 16:20:30 -07001804static int
Steve Frenchdb8b6312014-09-22 05:13:55 -05001805smb2_sync_write(const unsigned int xid, struct cifs_fid *pfid,
Pavel Shilovsky009d3442012-09-18 16:20:30 -07001806 struct cifs_io_parms *parms, unsigned int *written,
1807 struct kvec *iov, unsigned long nr_segs)
1808{
1809
Steve Frenchdb8b6312014-09-22 05:13:55 -05001810 parms->persistent_fid = pfid->persistent_fid;
1811 parms->volatile_fid = pfid->volatile_fid;
Pavel Shilovsky009d3442012-09-18 16:20:30 -07001812 return SMB2_write(xid, parms, written, iov, nr_segs);
1813}
1814
Steve Frenchd43cc792014-08-13 17:16:29 -05001815/* Set or clear the SPARSE_FILE attribute based on value passed in setsparse */
1816static bool smb2_set_sparse(const unsigned int xid, struct cifs_tcon *tcon,
1817 struct cifsFileInfo *cfile, struct inode *inode, __u8 setsparse)
1818{
1819 struct cifsInodeInfo *cifsi;
1820 int rc;
1821
1822 cifsi = CIFS_I(inode);
1823
1824 /* if file already sparse don't bother setting sparse again */
1825 if ((cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) && setsparse)
1826 return true; /* already sparse */
1827
1828 if (!(cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) && !setsparse)
1829 return true; /* already not sparse */
1830
1831 /*
1832 * Can't check for sparse support on share the usual way via the
1833 * FS attribute info (FILE_SUPPORTS_SPARSE_FILES) on the share
1834 * since Samba server doesn't set the flag on the share, yet
1835 * supports the set sparse FSCTL and returns sparse correctly
1836 * in the file attributes. If we fail setting sparse though we
1837 * mark that server does not support sparse files for this share
1838 * to avoid repeatedly sending the unsupported fsctl to server
1839 * if the file is repeatedly extended.
1840 */
1841 if (tcon->broken_sparse_sup)
1842 return false;
1843
1844 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
1845 cfile->fid.volatile_fid, FSCTL_SET_SPARSE,
Aurelien Aptel63a83b82018-01-24 13:46:11 +01001846 true /* is_fctl */,
Steve French153322f2019-03-28 22:32:49 -05001847 &setsparse, 1, CIFSMaxBufSize, NULL, NULL);
Steve Frenchd43cc792014-08-13 17:16:29 -05001848 if (rc) {
1849 tcon->broken_sparse_sup = true;
1850 cifs_dbg(FYI, "set sparse rc = %d\n", rc);
1851 return false;
1852 }
1853
1854 if (setsparse)
1855 cifsi->cifsAttrs |= FILE_ATTRIBUTE_SPARSE_FILE;
1856 else
1857 cifsi->cifsAttrs &= (~FILE_ATTRIBUTE_SPARSE_FILE);
1858
1859 return true;
1860}
1861
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07001862static int
1863smb2_set_file_size(const unsigned int xid, struct cifs_tcon *tcon,
1864 struct cifsFileInfo *cfile, __u64 size, bool set_alloc)
1865{
1866 __le64 eof = cpu_to_le64(size);
Steve French3d1a3742014-08-11 21:05:25 -05001867 struct inode *inode;
1868
1869 /*
1870 * If extending file more than one page make sparse. Many Linux fs
1871 * make files sparse by default when extending via ftruncate
1872 */
David Howells2b0143b2015-03-17 22:25:59 +00001873 inode = d_inode(cfile->dentry);
Steve French3d1a3742014-08-11 21:05:25 -05001874
1875 if (!set_alloc && (size > inode->i_size + 8192)) {
Steve French3d1a3742014-08-11 21:05:25 -05001876 __u8 set_sparse = 1;
Steve French3d1a3742014-08-11 21:05:25 -05001877
Steve Frenchd43cc792014-08-13 17:16:29 -05001878 /* whether set sparse succeeds or not, extend the file */
1879 smb2_set_sparse(xid, tcon, cfile, inode, set_sparse);
Steve French3d1a3742014-08-11 21:05:25 -05001880 }
1881
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07001882 return SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
Ronnie Sahlberg3764cbd2018-09-03 13:33:47 +10001883 cfile->fid.volatile_fid, cfile->pid, &eof);
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07001884}
1885
Steve French02b16662015-06-27 21:18:36 -07001886static int
1887smb2_duplicate_extents(const unsigned int xid,
1888 struct cifsFileInfo *srcfile,
1889 struct cifsFileInfo *trgtfile, u64 src_off,
1890 u64 len, u64 dest_off)
1891{
1892 int rc;
1893 unsigned int ret_data_len;
Steve French02b16662015-06-27 21:18:36 -07001894 struct duplicate_extents_to_file dup_ext_buf;
1895 struct cifs_tcon *tcon = tlink_tcon(trgtfile->tlink);
1896
1897 /* server fileays advertise duplicate extent support with this flag */
1898 if ((le32_to_cpu(tcon->fsAttrInfo.Attributes) &
1899 FILE_SUPPORTS_BLOCK_REFCOUNTING) == 0)
1900 return -EOPNOTSUPP;
1901
1902 dup_ext_buf.VolatileFileHandle = srcfile->fid.volatile_fid;
1903 dup_ext_buf.PersistentFileHandle = srcfile->fid.persistent_fid;
1904 dup_ext_buf.SourceFileOffset = cpu_to_le64(src_off);
1905 dup_ext_buf.TargetFileOffset = cpu_to_le64(dest_off);
1906 dup_ext_buf.ByteCount = cpu_to_le64(len);
Christoph Probsta205d502019-05-08 21:36:25 +02001907 cifs_dbg(FYI, "Duplicate extents: src off %lld dst off %lld len %lld\n",
Steve French02b16662015-06-27 21:18:36 -07001908 src_off, dest_off, len);
1909
1910 rc = smb2_set_file_size(xid, tcon, trgtfile, dest_off + len, false);
1911 if (rc)
1912 goto duplicate_extents_out;
1913
1914 rc = SMB2_ioctl(xid, tcon, trgtfile->fid.persistent_fid,
1915 trgtfile->fid.volatile_fid,
1916 FSCTL_DUPLICATE_EXTENTS_TO_FILE,
Aurelien Aptel63a83b82018-01-24 13:46:11 +01001917 true /* is_fsctl */,
Aurelien Aptel51146622017-02-28 15:08:41 +01001918 (char *)&dup_ext_buf,
Steve French02b16662015-06-27 21:18:36 -07001919 sizeof(struct duplicate_extents_to_file),
Steve French153322f2019-03-28 22:32:49 -05001920 CIFSMaxBufSize, NULL,
Steve French02b16662015-06-27 21:18:36 -07001921 &ret_data_len);
1922
1923 if (ret_data_len > 0)
Christoph Probsta205d502019-05-08 21:36:25 +02001924 cifs_dbg(FYI, "Non-zero response length in duplicate extents\n");
Steve French02b16662015-06-27 21:18:36 -07001925
1926duplicate_extents_out:
1927 return rc;
1928}
Steve French02b16662015-06-27 21:18:36 -07001929
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07001930static int
Steve French64a5cfa2013-10-14 15:31:32 -05001931smb2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
1932 struct cifsFileInfo *cfile)
1933{
1934 return SMB2_set_compression(xid, tcon, cfile->fid.persistent_fid,
1935 cfile->fid.volatile_fid);
1936}
1937
1938static int
Steve Frenchb3152e22015-06-24 03:17:02 -05001939smb3_set_integrity(const unsigned int xid, struct cifs_tcon *tcon,
1940 struct cifsFileInfo *cfile)
1941{
1942 struct fsctl_set_integrity_information_req integr_info;
Steve Frenchb3152e22015-06-24 03:17:02 -05001943 unsigned int ret_data_len;
1944
1945 integr_info.ChecksumAlgorithm = cpu_to_le16(CHECKSUM_TYPE_UNCHANGED);
1946 integr_info.Flags = 0;
1947 integr_info.Reserved = 0;
1948
1949 return SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
1950 cfile->fid.volatile_fid,
1951 FSCTL_SET_INTEGRITY_INFORMATION,
Aurelien Aptel63a83b82018-01-24 13:46:11 +01001952 true /* is_fsctl */,
Aurelien Aptel51146622017-02-28 15:08:41 +01001953 (char *)&integr_info,
Steve Frenchb3152e22015-06-24 03:17:02 -05001954 sizeof(struct fsctl_set_integrity_information_req),
Steve French153322f2019-03-28 22:32:49 -05001955 CIFSMaxBufSize, NULL,
Steve Frenchb3152e22015-06-24 03:17:02 -05001956 &ret_data_len);
1957
1958}
1959
Steve Frenche02789a2018-08-09 14:33:12 -05001960/* GMT Token is @GMT-YYYY.MM.DD-HH.MM.SS Unicode which is 48 bytes + null */
1961#define GMT_TOKEN_SIZE 50
1962
Steve French153322f2019-03-28 22:32:49 -05001963#define MIN_SNAPSHOT_ARRAY_SIZE 16 /* See MS-SMB2 section 3.3.5.15.1 */
1964
Steve Frenche02789a2018-08-09 14:33:12 -05001965/*
1966 * Input buffer contains (empty) struct smb_snapshot array with size filled in
1967 * For output see struct SRV_SNAPSHOT_ARRAY in MS-SMB2 section 2.2.32.2
1968 */
Steve Frenchb3152e22015-06-24 03:17:02 -05001969static int
Steve French834170c2016-09-30 21:14:26 -05001970smb3_enum_snapshots(const unsigned int xid, struct cifs_tcon *tcon,
1971 struct cifsFileInfo *cfile, void __user *ioc_buf)
1972{
1973 char *retbuf = NULL;
1974 unsigned int ret_data_len = 0;
1975 int rc;
Steve French153322f2019-03-28 22:32:49 -05001976 u32 max_response_size;
Steve French834170c2016-09-30 21:14:26 -05001977 struct smb_snapshot_array snapshot_in;
1978
Steve French973189a2019-04-04 00:41:04 -05001979 /*
1980 * On the first query to enumerate the list of snapshots available
1981 * for this volume the buffer begins with 0 (number of snapshots
1982 * which can be returned is zero since at that point we do not know
1983 * how big the buffer needs to be). On the second query,
1984 * it (ret_data_len) is set to number of snapshots so we can
1985 * know to set the maximum response size larger (see below).
1986 */
Steve French153322f2019-03-28 22:32:49 -05001987 if (get_user(ret_data_len, (unsigned int __user *)ioc_buf))
1988 return -EFAULT;
1989
1990 /*
1991 * Note that for snapshot queries that servers like Azure expect that
1992 * the first query be minimal size (and just used to get the number/size
1993 * of previous versions) so response size must be specified as EXACTLY
1994 * sizeof(struct snapshot_array) which is 16 when rounded up to multiple
1995 * of eight bytes.
1996 */
1997 if (ret_data_len == 0)
1998 max_response_size = MIN_SNAPSHOT_ARRAY_SIZE;
1999 else
2000 max_response_size = CIFSMaxBufSize;
2001
Steve French834170c2016-09-30 21:14:26 -05002002 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
2003 cfile->fid.volatile_fid,
2004 FSCTL_SRV_ENUMERATE_SNAPSHOTS,
Aurelien Aptel63a83b82018-01-24 13:46:11 +01002005 true /* is_fsctl */,
Steve French153322f2019-03-28 22:32:49 -05002006 NULL, 0 /* no input data */, max_response_size,
Steve French834170c2016-09-30 21:14:26 -05002007 (char **)&retbuf,
2008 &ret_data_len);
2009 cifs_dbg(FYI, "enum snaphots ioctl returned %d and ret buflen is %d\n",
2010 rc, ret_data_len);
2011 if (rc)
2012 return rc;
2013
2014 if (ret_data_len && (ioc_buf != NULL) && (retbuf != NULL)) {
2015 /* Fixup buffer */
2016 if (copy_from_user(&snapshot_in, ioc_buf,
2017 sizeof(struct smb_snapshot_array))) {
2018 rc = -EFAULT;
2019 kfree(retbuf);
2020 return rc;
2021 }
Steve French834170c2016-09-30 21:14:26 -05002022
Steve Frenche02789a2018-08-09 14:33:12 -05002023 /*
2024 * Check for min size, ie not large enough to fit even one GMT
2025 * token (snapshot). On the first ioctl some users may pass in
2026 * smaller size (or zero) to simply get the size of the array
2027 * so the user space caller can allocate sufficient memory
2028 * and retry the ioctl again with larger array size sufficient
2029 * to hold all of the snapshot GMT tokens on the second try.
2030 */
2031 if (snapshot_in.snapshot_array_size < GMT_TOKEN_SIZE)
2032 ret_data_len = sizeof(struct smb_snapshot_array);
2033
2034 /*
2035 * We return struct SRV_SNAPSHOT_ARRAY, followed by
2036 * the snapshot array (of 50 byte GMT tokens) each
2037 * representing an available previous version of the data
2038 */
2039 if (ret_data_len > (snapshot_in.snapshot_array_size +
2040 sizeof(struct smb_snapshot_array)))
2041 ret_data_len = snapshot_in.snapshot_array_size +
2042 sizeof(struct smb_snapshot_array);
Steve French834170c2016-09-30 21:14:26 -05002043
2044 if (copy_to_user(ioc_buf, retbuf, ret_data_len))
2045 rc = -EFAULT;
2046 }
2047
2048 kfree(retbuf);
2049 return rc;
2050}
2051
2052static int
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002053smb2_query_dir_first(const unsigned int xid, struct cifs_tcon *tcon,
2054 const char *path, struct cifs_sb_info *cifs_sb,
2055 struct cifs_fid *fid, __u16 search_flags,
2056 struct cifs_search_info *srch_inf)
2057{
2058 __le16 *utf16_path;
Ronnie Sahlberg37478602020-01-08 13:08:06 +10002059 struct smb_rqst rqst[2];
2060 struct kvec rsp_iov[2];
2061 int resp_buftype[2];
2062 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
2063 struct kvec qd_iov[SMB2_QUERY_DIRECTORY_IOV_SIZE];
2064 int rc, flags = 0;
2065 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04002066 struct cifs_open_parms oparms;
Ronnie Sahlberg37478602020-01-08 13:08:06 +10002067 struct smb2_query_directory_rsp *qd_rsp = NULL;
2068 struct smb2_create_rsp *op_rsp = NULL;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002069
2070 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
2071 if (!utf16_path)
2072 return -ENOMEM;
2073
Ronnie Sahlberg37478602020-01-08 13:08:06 +10002074 if (smb3_encryption_required(tcon))
2075 flags |= CIFS_TRANSFORM_REQ;
2076
2077 memset(rqst, 0, sizeof(rqst));
2078 resp_buftype[0] = resp_buftype[1] = CIFS_NO_BUFFER;
2079 memset(rsp_iov, 0, sizeof(rsp_iov));
2080
2081 /* Open */
2082 memset(&open_iov, 0, sizeof(open_iov));
2083 rqst[0].rq_iov = open_iov;
2084 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
2085
Pavel Shilovsky064f6042013-07-09 18:20:30 +04002086 oparms.tcon = tcon;
2087 oparms.desired_access = FILE_READ_ATTRIBUTES | FILE_READ_DATA;
2088 oparms.disposition = FILE_OPEN;
Steve French5e196972018-08-27 17:04:13 -05002089 if (backup_cred(cifs_sb))
2090 oparms.create_options = CREATE_OPEN_BACKUP_INTENT;
2091 else
2092 oparms.create_options = 0;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04002093 oparms.fid = fid;
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04002094 oparms.reconnect = false;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04002095
Ronnie Sahlberg37478602020-01-08 13:08:06 +10002096 rc = SMB2_open_init(tcon, &rqst[0], &oplock, &oparms, utf16_path);
2097 if (rc)
2098 goto qdf_free;
2099 smb2_set_next_command(tcon, &rqst[0]);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002100
Ronnie Sahlberg37478602020-01-08 13:08:06 +10002101 /* Query directory */
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002102 srch_inf->entries_in_buffer = 0;
Aurelien Aptel05957512018-05-17 16:35:07 +02002103 srch_inf->index_of_last_entry = 2;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002104
Ronnie Sahlberg37478602020-01-08 13:08:06 +10002105 memset(&qd_iov, 0, sizeof(qd_iov));
2106 rqst[1].rq_iov = qd_iov;
2107 rqst[1].rq_nvec = SMB2_QUERY_DIRECTORY_IOV_SIZE;
2108
2109 rc = SMB2_query_directory_init(xid, tcon, &rqst[1],
2110 COMPOUND_FID, COMPOUND_FID,
2111 0, srch_inf->info_level);
2112 if (rc)
2113 goto qdf_free;
2114
2115 smb2_set_related(&rqst[1]);
2116
2117 rc = compound_send_recv(xid, tcon->ses, flags, 2, rqst,
2118 resp_buftype, rsp_iov);
2119
2120 /* If the open failed there is nothing to do */
2121 op_rsp = (struct smb2_create_rsp *)rsp_iov[0].iov_base;
2122 if (op_rsp == NULL || op_rsp->sync_hdr.Status != STATUS_SUCCESS) {
2123 cifs_dbg(FYI, "query_dir_first: open failed rc=%d\n", rc);
2124 goto qdf_free;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002125 }
Ronnie Sahlberg37478602020-01-08 13:08:06 +10002126 fid->persistent_fid = op_rsp->PersistentFileId;
2127 fid->volatile_fid = op_rsp->VolatileFileId;
2128
2129 /* Anything else than ENODATA means a genuine error */
2130 if (rc && rc != -ENODATA) {
2131 SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
2132 cifs_dbg(FYI, "query_dir_first: query directory failed rc=%d\n", rc);
2133 trace_smb3_query_dir_err(xid, fid->persistent_fid,
2134 tcon->tid, tcon->ses->Suid, 0, 0, rc);
2135 goto qdf_free;
2136 }
2137
2138 qd_rsp = (struct smb2_query_directory_rsp *)rsp_iov[1].iov_base;
2139 if (qd_rsp->sync_hdr.Status == STATUS_NO_MORE_FILES) {
2140 trace_smb3_query_dir_done(xid, fid->persistent_fid,
2141 tcon->tid, tcon->ses->Suid, 0, 0);
2142 srch_inf->endOfSearch = true;
2143 rc = 0;
2144 goto qdf_free;
2145 }
2146
2147 rc = smb2_parse_query_directory(tcon, &rsp_iov[1], resp_buftype[1],
2148 srch_inf);
2149 if (rc) {
2150 trace_smb3_query_dir_err(xid, fid->persistent_fid, tcon->tid,
2151 tcon->ses->Suid, 0, 0, rc);
2152 goto qdf_free;
2153 }
2154 resp_buftype[1] = CIFS_NO_BUFFER;
2155
2156 trace_smb3_query_dir_done(xid, fid->persistent_fid, tcon->tid,
2157 tcon->ses->Suid, 0, srch_inf->entries_in_buffer);
2158
2159 qdf_free:
2160 kfree(utf16_path);
2161 SMB2_open_free(&rqst[0]);
2162 SMB2_query_directory_free(&rqst[1]);
2163 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
2164 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002165 return rc;
2166}
2167
2168static int
2169smb2_query_dir_next(const unsigned int xid, struct cifs_tcon *tcon,
2170 struct cifs_fid *fid, __u16 search_flags,
2171 struct cifs_search_info *srch_inf)
2172{
2173 return SMB2_query_directory(xid, tcon, fid->persistent_fid,
2174 fid->volatile_fid, 0, srch_inf);
2175}
2176
2177static int
2178smb2_close_dir(const unsigned int xid, struct cifs_tcon *tcon,
2179 struct cifs_fid *fid)
2180{
2181 return SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
2182}
2183
Pavel Shilovsky2e44b282012-09-18 16:20:33 -07002184/*
Christoph Probsta205d502019-05-08 21:36:25 +02002185 * If we negotiate SMB2 protocol and get STATUS_PENDING - update
2186 * the number of credits and return true. Otherwise - return false.
2187 */
Pavel Shilovsky2e44b282012-09-18 16:20:33 -07002188static bool
Pavel Shilovsky66265f12019-01-23 17:11:16 -08002189smb2_is_status_pending(char *buf, struct TCP_Server_Info *server)
Pavel Shilovsky2e44b282012-09-18 16:20:33 -07002190{
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10002191 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
Pavel Shilovsky2e44b282012-09-18 16:20:33 -07002192
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002193 if (shdr->Status != STATUS_PENDING)
Pavel Shilovsky2e44b282012-09-18 16:20:33 -07002194 return false;
2195
Pavel Shilovsky66265f12019-01-23 17:11:16 -08002196 if (shdr->CreditRequest) {
Pavel Shilovsky2e44b282012-09-18 16:20:33 -07002197 spin_lock(&server->req_lock);
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002198 server->credits += le16_to_cpu(shdr->CreditRequest);
Pavel Shilovsky2e44b282012-09-18 16:20:33 -07002199 spin_unlock(&server->req_lock);
2200 wake_up(&server->request_q);
2201 }
2202
2203 return true;
2204}
2205
Pavel Shilovsky511c54a2017-07-08 14:32:00 -07002206static bool
2207smb2_is_session_expired(char *buf)
2208{
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10002209 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
Pavel Shilovsky511c54a2017-07-08 14:32:00 -07002210
Mark Symsd81243c2018-05-24 09:47:31 +01002211 if (shdr->Status != STATUS_NETWORK_SESSION_EXPIRED &&
2212 shdr->Status != STATUS_USER_SESSION_DELETED)
Pavel Shilovsky511c54a2017-07-08 14:32:00 -07002213 return false;
2214
Steve Frenche68a9322018-07-30 14:23:58 -05002215 trace_smb3_ses_expired(shdr->TreeId, shdr->SessionId,
2216 le16_to_cpu(shdr->Command),
2217 le64_to_cpu(shdr->MessageId));
Mark Symsd81243c2018-05-24 09:47:31 +01002218 cifs_dbg(FYI, "Session expired or deleted\n");
Steve Frenche68a9322018-07-30 14:23:58 -05002219
Pavel Shilovsky511c54a2017-07-08 14:32:00 -07002220 return true;
2221}
2222
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07002223static int
2224smb2_oplock_response(struct cifs_tcon *tcon, struct cifs_fid *fid,
2225 struct cifsInodeInfo *cinode)
2226{
Pavel Shilovsky0822f512012-09-19 06:22:45 -07002227 if (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_LEASING)
2228 return SMB2_lease_break(0, tcon, cinode->lease_key,
2229 smb2_get_lease_state(cinode));
2230
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07002231 return SMB2_oplock_break(0, tcon, fid->persistent_fid,
2232 fid->volatile_fid,
Pavel Shilovsky18cceb62013-09-05 13:01:06 +04002233 CIFS_CACHE_READ(cinode) ? 1 : 0);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07002234}
2235
Ronnie Sahlbergc5a5f382018-09-03 13:33:41 +10002236void
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10002237smb2_set_related(struct smb_rqst *rqst)
2238{
2239 struct smb2_sync_hdr *shdr;
2240
2241 shdr = (struct smb2_sync_hdr *)(rqst->rq_iov[0].iov_base);
Ronnie Sahlberg88a92c92019-07-16 10:41:46 +10002242 if (shdr == NULL) {
2243 cifs_dbg(FYI, "shdr NULL in smb2_set_related\n");
2244 return;
2245 }
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10002246 shdr->Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
2247}
2248
2249char smb2_padding[7] = {0, 0, 0, 0, 0, 0, 0};
2250
Ronnie Sahlbergc5a5f382018-09-03 13:33:41 +10002251void
Ronnie Sahlberge77fe732018-12-31 13:43:40 +10002252smb2_set_next_command(struct cifs_tcon *tcon, struct smb_rqst *rqst)
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10002253{
2254 struct smb2_sync_hdr *shdr;
Ronnie Sahlberge77fe732018-12-31 13:43:40 +10002255 struct cifs_ses *ses = tcon->ses;
2256 struct TCP_Server_Info *server = ses->server;
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10002257 unsigned long len = smb_rqst_len(server, rqst);
Ronnie Sahlberge77fe732018-12-31 13:43:40 +10002258 int i, num_padding;
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10002259
Ronnie Sahlberg88a92c92019-07-16 10:41:46 +10002260 shdr = (struct smb2_sync_hdr *)(rqst->rq_iov[0].iov_base);
2261 if (shdr == NULL) {
2262 cifs_dbg(FYI, "shdr NULL in smb2_set_next_command\n");
2263 return;
2264 }
2265
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10002266 /* SMB headers in a compound are 8 byte aligned. */
Ronnie Sahlberge77fe732018-12-31 13:43:40 +10002267
2268 /* No padding needed */
2269 if (!(len & 7))
2270 goto finished;
2271
2272 num_padding = 8 - (len & 7);
2273 if (!smb3_encryption_required(tcon)) {
2274 /*
2275 * If we do not have encryption then we can just add an extra
2276 * iov for the padding.
2277 */
2278 rqst->rq_iov[rqst->rq_nvec].iov_base = smb2_padding;
2279 rqst->rq_iov[rqst->rq_nvec].iov_len = num_padding;
2280 rqst->rq_nvec++;
2281 len += num_padding;
2282 } else {
2283 /*
2284 * We can not add a small padding iov for the encryption case
2285 * because the encryption framework can not handle the padding
2286 * iovs.
2287 * We have to flatten this into a single buffer and add
2288 * the padding to it.
2289 */
2290 for (i = 1; i < rqst->rq_nvec; i++) {
2291 memcpy(rqst->rq_iov[0].iov_base +
2292 rqst->rq_iov[0].iov_len,
2293 rqst->rq_iov[i].iov_base,
2294 rqst->rq_iov[i].iov_len);
2295 rqst->rq_iov[0].iov_len += rqst->rq_iov[i].iov_len;
Ronnie Sahlberg271b9c02018-12-18 17:49:05 -06002296 }
Ronnie Sahlberge77fe732018-12-31 13:43:40 +10002297 memset(rqst->rq_iov[0].iov_base + rqst->rq_iov[0].iov_len,
2298 0, num_padding);
2299 rqst->rq_iov[0].iov_len += num_padding;
2300 len += num_padding;
2301 rqst->rq_nvec = 1;
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10002302 }
2303
Ronnie Sahlberge77fe732018-12-31 13:43:40 +10002304 finished:
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10002305 shdr->NextCommand = cpu_to_le32(len);
2306}
2307
Ronnie Sahlberg07d3b2e2018-12-20 22:03:04 -06002308/*
2309 * Passes the query info response back to the caller on success.
2310 * Caller need to free this with free_rsp_buf().
2311 */
Ronnie Sahlbergf9793b62018-11-27 09:52:04 +10002312int
Ronnie Sahlberg07d3b2e2018-12-20 22:03:04 -06002313smb2_query_info_compound(const unsigned int xid, struct cifs_tcon *tcon,
2314 __le16 *utf16_path, u32 desired_access,
2315 u32 class, u32 type, u32 output_len,
Ronnie Sahlbergf9793b62018-11-27 09:52:04 +10002316 struct kvec *rsp, int *buftype,
2317 struct cifs_sb_info *cifs_sb)
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07002318{
Ronnie Sahlberg07d3b2e2018-12-20 22:03:04 -06002319 struct cifs_ses *ses = tcon->ses;
Ronnie Sahlberg07d3b2e2018-12-20 22:03:04 -06002320 int flags = 0;
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10002321 struct smb_rqst rqst[3];
2322 int resp_buftype[3];
2323 struct kvec rsp_iov[3];
Ronnie Sahlberg4d8dfaf2018-08-21 11:49:21 +10002324 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10002325 struct kvec qi_iov[1];
2326 struct kvec close_iov[1];
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07002327 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04002328 struct cifs_open_parms oparms;
2329 struct cifs_fid fid;
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10002330 int rc;
2331
2332 if (smb3_encryption_required(tcon))
2333 flags |= CIFS_TRANSFORM_REQ;
2334
2335 memset(rqst, 0, sizeof(rqst));
Ronnie Sahlbergc5a5f382018-09-03 13:33:41 +10002336 resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10002337 memset(rsp_iov, 0, sizeof(rsp_iov));
2338
2339 memset(&open_iov, 0, sizeof(open_iov));
2340 rqst[0].rq_iov = open_iov;
Ronnie Sahlberg4d8dfaf2018-08-21 11:49:21 +10002341 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07002342
Pavel Shilovsky064f6042013-07-09 18:20:30 +04002343 oparms.tcon = tcon;
Ronnie Sahlberg07d3b2e2018-12-20 22:03:04 -06002344 oparms.desired_access = desired_access;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04002345 oparms.disposition = FILE_OPEN;
Ronnie Sahlbergf9793b62018-11-27 09:52:04 +10002346 if (cifs_sb && backup_cred(cifs_sb))
2347 oparms.create_options = CREATE_OPEN_BACKUP_INTENT;
2348 else
2349 oparms.create_options = 0;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04002350 oparms.fid = &fid;
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04002351 oparms.reconnect = false;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04002352
Ronnie Sahlberg07d3b2e2018-12-20 22:03:04 -06002353 rc = SMB2_open_init(tcon, &rqst[0], &oplock, &oparms, utf16_path);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07002354 if (rc)
Ronnie Sahlberg07d3b2e2018-12-20 22:03:04 -06002355 goto qic_exit;
Ronnie Sahlberge77fe732018-12-31 13:43:40 +10002356 smb2_set_next_command(tcon, &rqst[0]);
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10002357
2358 memset(&qi_iov, 0, sizeof(qi_iov));
2359 rqst[1].rq_iov = qi_iov;
2360 rqst[1].rq_nvec = 1;
2361
2362 rc = SMB2_query_info_init(tcon, &rqst[1], COMPOUND_FID, COMPOUND_FID,
Ronnie Sahlberg07d3b2e2018-12-20 22:03:04 -06002363 class, type, 0,
2364 output_len, 0,
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05002365 NULL);
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10002366 if (rc)
Ronnie Sahlberg07d3b2e2018-12-20 22:03:04 -06002367 goto qic_exit;
Ronnie Sahlberge77fe732018-12-31 13:43:40 +10002368 smb2_set_next_command(tcon, &rqst[1]);
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10002369 smb2_set_related(&rqst[1]);
2370
2371 memset(&close_iov, 0, sizeof(close_iov));
2372 rqst[2].rq_iov = close_iov;
2373 rqst[2].rq_nvec = 1;
2374
Steve French43f8a6a2019-12-02 21:46:54 -06002375 rc = SMB2_close_init(tcon, &rqst[2], COMPOUND_FID, COMPOUND_FID, false);
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10002376 if (rc)
Ronnie Sahlberg07d3b2e2018-12-20 22:03:04 -06002377 goto qic_exit;
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10002378 smb2_set_related(&rqst[2]);
2379
2380 rc = compound_send_recv(xid, ses, flags, 3, rqst,
2381 resp_buftype, rsp_iov);
Ronnie Sahlbergf9793b62018-11-27 09:52:04 +10002382 if (rc) {
2383 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
Steve French7dcc82c2019-09-11 00:07:36 -05002384 if (rc == -EREMCHG) {
2385 tcon->need_reconnect = true;
2386 printk_once(KERN_WARNING "server share %s deleted\n",
2387 tcon->treeName);
2388 }
Ronnie Sahlberg07d3b2e2018-12-20 22:03:04 -06002389 goto qic_exit;
Ronnie Sahlbergf9793b62018-11-27 09:52:04 +10002390 }
Ronnie Sahlberg07d3b2e2018-12-20 22:03:04 -06002391 *rsp = rsp_iov[1];
2392 *buftype = resp_buftype[1];
2393
2394 qic_exit:
2395 SMB2_open_free(&rqst[0]);
2396 SMB2_query_info_free(&rqst[1]);
2397 SMB2_close_free(&rqst[2]);
2398 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
2399 free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
2400 return rc;
2401}
2402
2403static int
2404smb2_queryfs(const unsigned int xid, struct cifs_tcon *tcon,
2405 struct kstatfs *buf)
2406{
2407 struct smb2_query_info_rsp *rsp;
2408 struct smb2_fs_full_size_info *info = NULL;
2409 __le16 utf16_path = 0; /* Null - open root of share */
2410 struct kvec rsp_iov = {NULL, 0};
2411 int buftype = CIFS_NO_BUFFER;
2412 int rc;
2413
2414
2415 rc = smb2_query_info_compound(xid, tcon, &utf16_path,
2416 FILE_READ_ATTRIBUTES,
2417 FS_FULL_SIZE_INFORMATION,
2418 SMB2_O_INFO_FILESYSTEM,
2419 sizeof(struct smb2_fs_full_size_info),
Ronnie Sahlbergf9793b62018-11-27 09:52:04 +10002420 &rsp_iov, &buftype, NULL);
Ronnie Sahlberg07d3b2e2018-12-20 22:03:04 -06002421 if (rc)
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10002422 goto qfs_exit;
2423
Ronnie Sahlberg07d3b2e2018-12-20 22:03:04 -06002424 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07002425 buf->f_type = SMB2_MAGIC_NUMBER;
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10002426 info = (struct smb2_fs_full_size_info *)(
2427 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
2428 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
2429 le32_to_cpu(rsp->OutputBufferLength),
Ronnie Sahlberg07d3b2e2018-12-20 22:03:04 -06002430 &rsp_iov,
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10002431 sizeof(struct smb2_fs_full_size_info));
2432 if (!rc)
2433 smb2_copy_fs_info_to_kstatfs(info, buf);
2434
2435qfs_exit:
Ronnie Sahlberg07d3b2e2018-12-20 22:03:04 -06002436 free_rsp_buf(buftype, rsp_iov.iov_base);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07002437 return rc;
2438}
2439
Steve French2d304212018-06-24 23:28:12 -05002440static int
2441smb311_queryfs(const unsigned int xid, struct cifs_tcon *tcon,
2442 struct kstatfs *buf)
2443{
2444 int rc;
2445 __le16 srch_path = 0; /* Null - open root of share */
2446 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2447 struct cifs_open_parms oparms;
2448 struct cifs_fid fid;
2449
2450 if (!tcon->posix_extensions)
2451 return smb2_queryfs(xid, tcon, buf);
2452
2453 oparms.tcon = tcon;
2454 oparms.desired_access = FILE_READ_ATTRIBUTES;
2455 oparms.disposition = FILE_OPEN;
2456 oparms.create_options = 0;
2457 oparms.fid = &fid;
2458 oparms.reconnect = false;
2459
2460 rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL, NULL);
2461 if (rc)
2462 return rc;
2463
2464 rc = SMB311_posix_qfs_info(xid, tcon, fid.persistent_fid,
2465 fid.volatile_fid, buf);
2466 buf->f_type = SMB2_MAGIC_NUMBER;
2467 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
2468 return rc;
2469}
Steve French2d304212018-06-24 23:28:12 -05002470
Pavel Shilovsky027e8ee2012-09-19 06:22:43 -07002471static bool
2472smb2_compare_fids(struct cifsFileInfo *ob1, struct cifsFileInfo *ob2)
2473{
2474 return ob1->fid.persistent_fid == ob2->fid.persistent_fid &&
2475 ob1->fid.volatile_fid == ob2->fid.volatile_fid;
2476}
2477
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07002478static int
2479smb2_mand_lock(const unsigned int xid, struct cifsFileInfo *cfile, __u64 offset,
2480 __u64 length, __u32 type, int lock, int unlock, bool wait)
2481{
2482 if (unlock && !lock)
2483 type = SMB2_LOCKFLAG_UNLOCK;
2484 return SMB2_lock(xid, tlink_tcon(cfile->tlink),
2485 cfile->fid.persistent_fid, cfile->fid.volatile_fid,
2486 current->tgid, length, offset, type, wait);
2487}
2488
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07002489static void
2490smb2_get_lease_key(struct inode *inode, struct cifs_fid *fid)
2491{
2492 memcpy(fid->lease_key, CIFS_I(inode)->lease_key, SMB2_LEASE_KEY_SIZE);
2493}
2494
2495static void
2496smb2_set_lease_key(struct inode *inode, struct cifs_fid *fid)
2497{
2498 memcpy(CIFS_I(inode)->lease_key, fid->lease_key, SMB2_LEASE_KEY_SIZE);
2499}
2500
2501static void
2502smb2_new_lease_key(struct cifs_fid *fid)
2503{
Steve Frenchfa70b872016-09-22 00:39:34 -05002504 generate_random_uuid(fid->lease_key);
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07002505}
2506
Aurelien Aptel9d496402017-02-13 16:16:49 +01002507static int
2508smb2_get_dfs_refer(const unsigned int xid, struct cifs_ses *ses,
2509 const char *search_name,
2510 struct dfs_info3_param **target_nodes,
2511 unsigned int *num_of_nodes,
2512 const struct nls_table *nls_codepage, int remap)
2513{
2514 int rc;
2515 __le16 *utf16_path = NULL;
2516 int utf16_path_len = 0;
2517 struct cifs_tcon *tcon;
2518 struct fsctl_get_dfs_referral_req *dfs_req = NULL;
2519 struct get_dfs_referral_rsp *dfs_rsp = NULL;
2520 u32 dfs_req_size = 0, dfs_rsp_size = 0;
2521
Christoph Probsta205d502019-05-08 21:36:25 +02002522 cifs_dbg(FYI, "%s: path: %s\n", __func__, search_name);
Aurelien Aptel9d496402017-02-13 16:16:49 +01002523
2524 /*
Aurelien Aptel63a83b82018-01-24 13:46:11 +01002525 * Try to use the IPC tcon, otherwise just use any
Aurelien Aptel9d496402017-02-13 16:16:49 +01002526 */
Aurelien Aptel63a83b82018-01-24 13:46:11 +01002527 tcon = ses->tcon_ipc;
2528 if (tcon == NULL) {
2529 spin_lock(&cifs_tcp_ses_lock);
2530 tcon = list_first_entry_or_null(&ses->tcon_list,
2531 struct cifs_tcon,
2532 tcon_list);
2533 if (tcon)
2534 tcon->tc_count++;
2535 spin_unlock(&cifs_tcp_ses_lock);
2536 }
Aurelien Aptel9d496402017-02-13 16:16:49 +01002537
Aurelien Aptel63a83b82018-01-24 13:46:11 +01002538 if (tcon == NULL) {
Aurelien Aptel9d496402017-02-13 16:16:49 +01002539 cifs_dbg(VFS, "session %p has no tcon available for a dfs referral request\n",
2540 ses);
2541 rc = -ENOTCONN;
2542 goto out;
2543 }
2544
2545 utf16_path = cifs_strndup_to_utf16(search_name, PATH_MAX,
2546 &utf16_path_len,
2547 nls_codepage, remap);
2548 if (!utf16_path) {
2549 rc = -ENOMEM;
2550 goto out;
2551 }
2552
2553 dfs_req_size = sizeof(*dfs_req) + utf16_path_len;
2554 dfs_req = kzalloc(dfs_req_size, GFP_KERNEL);
2555 if (!dfs_req) {
2556 rc = -ENOMEM;
2557 goto out;
2558 }
2559
2560 /* Highest DFS referral version understood */
2561 dfs_req->MaxReferralLevel = DFS_VERSION;
2562
2563 /* Path to resolve in an UTF-16 null-terminated string */
2564 memcpy(dfs_req->RequestFileName, utf16_path, utf16_path_len);
2565
2566 do {
Aurelien Aptel9d496402017-02-13 16:16:49 +01002567 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
2568 FSCTL_DFS_GET_REFERRALS,
Aurelien Aptel63a83b82018-01-24 13:46:11 +01002569 true /* is_fsctl */,
Steve French153322f2019-03-28 22:32:49 -05002570 (char *)dfs_req, dfs_req_size, CIFSMaxBufSize,
Aurelien Aptel9d496402017-02-13 16:16:49 +01002571 (char **)&dfs_rsp, &dfs_rsp_size);
Aurelien Aptel9d496402017-02-13 16:16:49 +01002572 } while (rc == -EAGAIN);
2573
2574 if (rc) {
Steve French2564f2f2018-03-21 23:16:36 -05002575 if ((rc != -ENOENT) && (rc != -EOPNOTSUPP))
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +10002576 cifs_tcon_dbg(VFS, "ioctl error in %s rc=%d\n", __func__, rc);
Aurelien Aptel9d496402017-02-13 16:16:49 +01002577 goto out;
2578 }
2579
2580 rc = parse_dfs_referrals(dfs_rsp, dfs_rsp_size,
2581 num_of_nodes, target_nodes,
2582 nls_codepage, remap, search_name,
2583 true /* is_unicode */);
2584 if (rc) {
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +10002585 cifs_tcon_dbg(VFS, "parse error in %s rc=%d\n", __func__, rc);
Aurelien Aptel9d496402017-02-13 16:16:49 +01002586 goto out;
2587 }
2588
2589 out:
Aurelien Aptel63a83b82018-01-24 13:46:11 +01002590 if (tcon && !tcon->ipc) {
2591 /* ipc tcons are not refcounted */
Aurelien Aptel9d496402017-02-13 16:16:49 +01002592 spin_lock(&cifs_tcp_ses_lock);
2593 tcon->tc_count--;
2594 spin_unlock(&cifs_tcp_ses_lock);
2595 }
2596 kfree(utf16_path);
2597 kfree(dfs_req);
2598 kfree(dfs_rsp);
2599 return rc;
2600}
Ronnie Sahlberg5de254d2019-06-27 14:57:02 +10002601
2602static int
Steve Frenchd5ecebc2019-06-28 02:04:18 -05002603parse_reparse_posix(struct reparse_posix_data *symlink_buf,
2604 u32 plen, char **target_path,
2605 struct cifs_sb_info *cifs_sb)
2606{
2607 unsigned int len;
2608
2609 /* See MS-FSCC 2.1.2.6 for the 'NFS' style reparse tags */
2610 len = le16_to_cpu(symlink_buf->ReparseDataLength);
2611
Steve Frenchd5ecebc2019-06-28 02:04:18 -05002612 if (le64_to_cpu(symlink_buf->InodeType) != NFS_SPECFILE_LNK) {
2613 cifs_dbg(VFS, "%lld not a supported symlink type\n",
2614 le64_to_cpu(symlink_buf->InodeType));
2615 return -EOPNOTSUPP;
2616 }
2617
2618 *target_path = cifs_strndup_from_utf16(
2619 symlink_buf->PathBuffer,
2620 len, true, cifs_sb->local_nls);
2621 if (!(*target_path))
2622 return -ENOMEM;
2623
2624 convert_delimiter(*target_path, '/');
2625 cifs_dbg(FYI, "%s: target path: %s\n", __func__, *target_path);
2626
2627 return 0;
2628}
2629
2630static int
Ronnie Sahlberg5de254d2019-06-27 14:57:02 +10002631parse_reparse_symlink(struct reparse_symlink_data_buffer *symlink_buf,
2632 u32 plen, char **target_path,
2633 struct cifs_sb_info *cifs_sb)
2634{
2635 unsigned int sub_len;
2636 unsigned int sub_offset;
2637
Steve Frenchd5ecebc2019-06-28 02:04:18 -05002638 /* We handle Symbolic Link reparse tag here. See: MS-FSCC 2.1.2.4 */
Ronnie Sahlberg5de254d2019-06-27 14:57:02 +10002639
2640 sub_offset = le16_to_cpu(symlink_buf->SubstituteNameOffset);
2641 sub_len = le16_to_cpu(symlink_buf->SubstituteNameLength);
2642 if (sub_offset + 20 > plen ||
2643 sub_offset + sub_len + 20 > plen) {
2644 cifs_dbg(VFS, "srv returned malformed symlink buffer\n");
2645 return -EIO;
2646 }
2647
2648 *target_path = cifs_strndup_from_utf16(
2649 symlink_buf->PathBuffer + sub_offset,
2650 sub_len, true, cifs_sb->local_nls);
2651 if (!(*target_path))
2652 return -ENOMEM;
2653
2654 convert_delimiter(*target_path, '/');
2655 cifs_dbg(FYI, "%s: target path: %s\n", __func__, *target_path);
2656
2657 return 0;
2658}
2659
Steve Frenchd5ecebc2019-06-28 02:04:18 -05002660static int
Ronnie Sahlbergf5f111c2019-07-07 07:45:42 +10002661parse_reparse_point(struct reparse_data_buffer *buf,
2662 u32 plen, char **target_path,
2663 struct cifs_sb_info *cifs_sb)
Steve Frenchd5ecebc2019-06-28 02:04:18 -05002664{
Ronnie Sahlbergf5f111c2019-07-07 07:45:42 +10002665 if (plen < sizeof(struct reparse_data_buffer)) {
2666 cifs_dbg(VFS, "reparse buffer is too small. Must be "
2667 "at least 8 bytes but was %d\n", plen);
2668 return -EIO;
2669 }
2670
2671 if (plen < le16_to_cpu(buf->ReparseDataLength) +
2672 sizeof(struct reparse_data_buffer)) {
2673 cifs_dbg(VFS, "srv returned invalid reparse buf "
2674 "length: %d\n", plen);
2675 return -EIO;
2676 }
2677
Steve Frenchd5ecebc2019-06-28 02:04:18 -05002678 /* See MS-FSCC 2.1.2 */
Ronnie Sahlbergf5f111c2019-07-07 07:45:42 +10002679 switch (le32_to_cpu(buf->ReparseTag)) {
2680 case IO_REPARSE_TAG_NFS:
2681 return parse_reparse_posix(
2682 (struct reparse_posix_data *)buf,
Steve Frenchd5ecebc2019-06-28 02:04:18 -05002683 plen, target_path, cifs_sb);
Ronnie Sahlbergf5f111c2019-07-07 07:45:42 +10002684 case IO_REPARSE_TAG_SYMLINK:
2685 return parse_reparse_symlink(
2686 (struct reparse_symlink_data_buffer *)buf,
2687 plen, target_path, cifs_sb);
2688 default:
2689 cifs_dbg(VFS, "srv returned unknown symlink buffer "
2690 "tag:0x%08x\n", le32_to_cpu(buf->ReparseTag));
2691 return -EOPNOTSUPP;
2692 }
Steve Frenchd5ecebc2019-06-28 02:04:18 -05002693}
2694
Pavel Shilovsky78932422016-07-24 10:37:38 +03002695#define SMB2_SYMLINK_STRUCT_SIZE \
2696 (sizeof(struct smb2_err_rsp) - 1 + sizeof(struct smb2_symlink_err_rsp))
2697
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04002698static int
2699smb2_query_symlink(const unsigned int xid, struct cifs_tcon *tcon,
Ronnie Sahlbergebaf5462019-04-10 08:44:46 +10002700 struct cifs_sb_info *cifs_sb, const char *full_path,
2701 char **target_path, bool is_reparse_point)
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04002702{
2703 int rc;
Ronnie Sahlbergebaf5462019-04-10 08:44:46 +10002704 __le16 *utf16_path = NULL;
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04002705 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2706 struct cifs_open_parms oparms;
2707 struct cifs_fid fid;
Ronnie Sahlberg91cb74f2018-04-13 09:03:19 +10002708 struct kvec err_iov = {NULL, 0};
Ronnie Sahlberg9d874c32018-06-08 13:21:18 +10002709 struct smb2_err_rsp *err_buf = NULL;
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04002710 struct smb2_symlink_err_rsp *symlink;
Pavel Shilovsky78932422016-07-24 10:37:38 +03002711 unsigned int sub_len;
2712 unsigned int sub_offset;
2713 unsigned int print_len;
2714 unsigned int print_offset;
Ronnie Sahlbergebaf5462019-04-10 08:44:46 +10002715 int flags = 0;
2716 struct smb_rqst rqst[3];
2717 int resp_buftype[3];
2718 struct kvec rsp_iov[3];
2719 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
2720 struct kvec io_iov[SMB2_IOCTL_IOV_SIZE];
2721 struct kvec close_iov[1];
2722 struct smb2_create_rsp *create_rsp;
2723 struct smb2_ioctl_rsp *ioctl_rsp;
Ronnie Sahlberg5de254d2019-06-27 14:57:02 +10002724 struct reparse_data_buffer *reparse_buf;
Ronnie Sahlbergebaf5462019-04-10 08:44:46 +10002725 u32 plen;
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04002726
2727 cifs_dbg(FYI, "%s: path: %s\n", __func__, full_path);
2728
Ronnie Sahlberg5de254d2019-06-27 14:57:02 +10002729 *target_path = NULL;
2730
Ronnie Sahlbergebaf5462019-04-10 08:44:46 +10002731 if (smb3_encryption_required(tcon))
2732 flags |= CIFS_TRANSFORM_REQ;
2733
2734 memset(rqst, 0, sizeof(rqst));
2735 resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
2736 memset(rsp_iov, 0, sizeof(rsp_iov));
2737
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04002738 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
2739 if (!utf16_path)
2740 return -ENOMEM;
2741
Ronnie Sahlbergebaf5462019-04-10 08:44:46 +10002742 /* Open */
2743 memset(&open_iov, 0, sizeof(open_iov));
2744 rqst[0].rq_iov = open_iov;
2745 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
2746
2747 memset(&oparms, 0, sizeof(oparms));
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04002748 oparms.tcon = tcon;
2749 oparms.desired_access = FILE_READ_ATTRIBUTES;
2750 oparms.disposition = FILE_OPEN;
Ronnie Sahlbergebaf5462019-04-10 08:44:46 +10002751
Steve French5e196972018-08-27 17:04:13 -05002752 if (backup_cred(cifs_sb))
2753 oparms.create_options = CREATE_OPEN_BACKUP_INTENT;
2754 else
2755 oparms.create_options = 0;
Ronnie Sahlbergebaf5462019-04-10 08:44:46 +10002756 if (is_reparse_point)
2757 oparms.create_options = OPEN_REPARSE_POINT;
2758
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04002759 oparms.fid = &fid;
2760 oparms.reconnect = false;
2761
Ronnie Sahlbergebaf5462019-04-10 08:44:46 +10002762 rc = SMB2_open_init(tcon, &rqst[0], &oplock, &oparms, utf16_path);
2763 if (rc)
2764 goto querty_exit;
2765 smb2_set_next_command(tcon, &rqst[0]);
2766
2767
2768 /* IOCTL */
2769 memset(&io_iov, 0, sizeof(io_iov));
2770 rqst[1].rq_iov = io_iov;
2771 rqst[1].rq_nvec = SMB2_IOCTL_IOV_SIZE;
2772
2773 rc = SMB2_ioctl_init(tcon, &rqst[1], fid.persistent_fid,
2774 fid.volatile_fid, FSCTL_GET_REPARSE_POINT,
Ronnie Sahlberg731b82b2020-01-08 13:08:07 +10002775 true /* is_fctl */, NULL, 0,
2776 CIFSMaxBufSize -
2777 MAX_SMB2_CREATE_RESPONSE_SIZE -
2778 MAX_SMB2_CLOSE_RESPONSE_SIZE);
Ronnie Sahlbergebaf5462019-04-10 08:44:46 +10002779 if (rc)
2780 goto querty_exit;
2781
2782 smb2_set_next_command(tcon, &rqst[1]);
2783 smb2_set_related(&rqst[1]);
2784
2785
2786 /* Close */
2787 memset(&close_iov, 0, sizeof(close_iov));
2788 rqst[2].rq_iov = close_iov;
2789 rqst[2].rq_nvec = 1;
2790
Steve French43f8a6a2019-12-02 21:46:54 -06002791 rc = SMB2_close_init(tcon, &rqst[2], COMPOUND_FID, COMPOUND_FID, false);
Ronnie Sahlbergebaf5462019-04-10 08:44:46 +10002792 if (rc)
2793 goto querty_exit;
2794
2795 smb2_set_related(&rqst[2]);
2796
2797 rc = compound_send_recv(xid, tcon->ses, flags, 3, rqst,
2798 resp_buftype, rsp_iov);
2799
2800 create_rsp = rsp_iov[0].iov_base;
2801 if (create_rsp && create_rsp->sync_hdr.Status)
2802 err_iov = rsp_iov[0];
2803 ioctl_rsp = rsp_iov[1].iov_base;
2804
2805 /*
2806 * Open was successful and we got an ioctl response.
2807 */
2808 if ((rc == 0) && (is_reparse_point)) {
2809 /* See MS-FSCC 2.3.23 */
2810
Ronnie Sahlberg5de254d2019-06-27 14:57:02 +10002811 reparse_buf = (struct reparse_data_buffer *)
2812 ((char *)ioctl_rsp +
2813 le32_to_cpu(ioctl_rsp->OutputOffset));
Ronnie Sahlbergebaf5462019-04-10 08:44:46 +10002814 plen = le32_to_cpu(ioctl_rsp->OutputCount);
2815
2816 if (plen + le32_to_cpu(ioctl_rsp->OutputOffset) >
2817 rsp_iov[1].iov_len) {
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +10002818 cifs_tcon_dbg(VFS, "srv returned invalid ioctl len: %d\n",
Ronnie Sahlberg5de254d2019-06-27 14:57:02 +10002819 plen);
Ronnie Sahlbergebaf5462019-04-10 08:44:46 +10002820 rc = -EIO;
2821 goto querty_exit;
2822 }
2823
Ronnie Sahlbergf5f111c2019-07-07 07:45:42 +10002824 rc = parse_reparse_point(reparse_buf, plen, target_path,
2825 cifs_sb);
Ronnie Sahlbergebaf5462019-04-10 08:44:46 +10002826 goto querty_exit;
2827 }
2828
Gustavo A. R. Silva0d568cd2018-04-13 10:13:29 -05002829 if (!rc || !err_iov.iov_base) {
Ronnie Sahlberg9d874c32018-06-08 13:21:18 +10002830 rc = -ENOENT;
Ronnie Sahlbergebaf5462019-04-10 08:44:46 +10002831 goto querty_exit;
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04002832 }
Pavel Shilovsky78932422016-07-24 10:37:38 +03002833
Ronnie Sahlberg91cb74f2018-04-13 09:03:19 +10002834 err_buf = err_iov.iov_base;
Pavel Shilovsky78932422016-07-24 10:37:38 +03002835 if (le32_to_cpu(err_buf->ByteCount) < sizeof(struct smb2_symlink_err_rsp) ||
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10002836 err_iov.iov_len < SMB2_SYMLINK_STRUCT_SIZE) {
Ronnie Sahlbergdf070af2019-07-09 18:41:11 +10002837 rc = -EINVAL;
2838 goto querty_exit;
2839 }
2840
2841 symlink = (struct smb2_symlink_err_rsp *)err_buf->ErrorData;
2842 if (le32_to_cpu(symlink->SymLinkErrorTag) != SYMLINK_ERROR_TAG ||
2843 le32_to_cpu(symlink->ReparseTag) != IO_REPARSE_TAG_SYMLINK) {
2844 rc = -EINVAL;
Ronnie Sahlberg9d874c32018-06-08 13:21:18 +10002845 goto querty_exit;
Pavel Shilovsky78932422016-07-24 10:37:38 +03002846 }
2847
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04002848 /* open must fail on symlink - reset rc */
2849 rc = 0;
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04002850 sub_len = le16_to_cpu(symlink->SubstituteNameLength);
2851 sub_offset = le16_to_cpu(symlink->SubstituteNameOffset);
Pavel Shilovsky78932422016-07-24 10:37:38 +03002852 print_len = le16_to_cpu(symlink->PrintNameLength);
2853 print_offset = le16_to_cpu(symlink->PrintNameOffset);
2854
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10002855 if (err_iov.iov_len < SMB2_SYMLINK_STRUCT_SIZE + sub_offset + sub_len) {
Ronnie Sahlbergdf070af2019-07-09 18:41:11 +10002856 rc = -EINVAL;
Ronnie Sahlberg9d874c32018-06-08 13:21:18 +10002857 goto querty_exit;
Pavel Shilovsky78932422016-07-24 10:37:38 +03002858 }
2859
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10002860 if (err_iov.iov_len <
2861 SMB2_SYMLINK_STRUCT_SIZE + print_offset + print_len) {
Ronnie Sahlbergdf070af2019-07-09 18:41:11 +10002862 rc = -EINVAL;
Ronnie Sahlberg9d874c32018-06-08 13:21:18 +10002863 goto querty_exit;
Pavel Shilovsky78932422016-07-24 10:37:38 +03002864 }
2865
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04002866 *target_path = cifs_strndup_from_utf16(
2867 (char *)symlink->PathBuffer + sub_offset,
2868 sub_len, true, cifs_sb->local_nls);
2869 if (!(*target_path)) {
Ronnie Sahlberg9d874c32018-06-08 13:21:18 +10002870 rc = -ENOMEM;
2871 goto querty_exit;
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04002872 }
2873 convert_delimiter(*target_path, '/');
2874 cifs_dbg(FYI, "%s: target path: %s\n", __func__, *target_path);
Ronnie Sahlberg9d874c32018-06-08 13:21:18 +10002875
2876 querty_exit:
Ronnie Sahlbergebaf5462019-04-10 08:44:46 +10002877 cifs_dbg(FYI, "query symlink rc %d\n", rc);
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04002878 kfree(utf16_path);
Ronnie Sahlbergebaf5462019-04-10 08:44:46 +10002879 SMB2_open_free(&rqst[0]);
2880 SMB2_ioctl_free(&rqst[1]);
2881 SMB2_close_free(&rqst[2]);
2882 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
2883 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
2884 free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04002885 return rc;
2886}
2887
Shirish Pargaonkar2f1afe22017-06-22 22:52:05 -05002888static struct cifs_ntsd *
2889get_smb2_acl_by_fid(struct cifs_sb_info *cifs_sb,
2890 const struct cifs_fid *cifsfid, u32 *pacllen)
2891{
2892 struct cifs_ntsd *pntsd = NULL;
2893 unsigned int xid;
2894 int rc = -EOPNOTSUPP;
2895 struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
2896
2897 if (IS_ERR(tlink))
2898 return ERR_CAST(tlink);
2899
2900 xid = get_xid();
2901 cifs_dbg(FYI, "trying to get acl\n");
2902
2903 rc = SMB2_query_acl(xid, tlink_tcon(tlink), cifsfid->persistent_fid,
2904 cifsfid->volatile_fid, (void **)&pntsd, pacllen);
2905 free_xid(xid);
2906
2907 cifs_put_tlink(tlink);
2908
2909 cifs_dbg(FYI, "%s: rc = %d ACL len %d\n", __func__, rc, *pacllen);
2910 if (rc)
2911 return ERR_PTR(rc);
2912 return pntsd;
2913
2914}
2915
2916static struct cifs_ntsd *
2917get_smb2_acl_by_path(struct cifs_sb_info *cifs_sb,
2918 const char *path, u32 *pacllen)
2919{
2920 struct cifs_ntsd *pntsd = NULL;
2921 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2922 unsigned int xid;
2923 int rc;
2924 struct cifs_tcon *tcon;
2925 struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
2926 struct cifs_fid fid;
2927 struct cifs_open_parms oparms;
2928 __le16 *utf16_path;
2929
2930 cifs_dbg(FYI, "get smb3 acl for path %s\n", path);
2931 if (IS_ERR(tlink))
2932 return ERR_CAST(tlink);
2933
2934 tcon = tlink_tcon(tlink);
2935 xid = get_xid();
2936
2937 if (backup_cred(cifs_sb))
Colin Ian King709340a2017-07-05 13:47:34 +01002938 oparms.create_options = CREATE_OPEN_BACKUP_INTENT;
Shirish Pargaonkar2f1afe22017-06-22 22:52:05 -05002939 else
2940 oparms.create_options = 0;
2941
2942 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
Steve Frenchcfe89092018-05-19 02:04:55 -05002943 if (!utf16_path) {
2944 rc = -ENOMEM;
2945 free_xid(xid);
2946 return ERR_PTR(rc);
2947 }
Shirish Pargaonkar2f1afe22017-06-22 22:52:05 -05002948
2949 oparms.tcon = tcon;
2950 oparms.desired_access = READ_CONTROL;
2951 oparms.disposition = FILE_OPEN;
2952 oparms.fid = &fid;
2953 oparms.reconnect = false;
2954
Ronnie Sahlberg9d874c32018-06-08 13:21:18 +10002955 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, NULL);
Shirish Pargaonkar2f1afe22017-06-22 22:52:05 -05002956 kfree(utf16_path);
2957 if (!rc) {
2958 rc = SMB2_query_acl(xid, tlink_tcon(tlink), fid.persistent_fid,
2959 fid.volatile_fid, (void **)&pntsd, pacllen);
2960 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
2961 }
2962
2963 cifs_put_tlink(tlink);
2964 free_xid(xid);
2965
2966 cifs_dbg(FYI, "%s: rc = %d ACL len %d\n", __func__, rc, *pacllen);
2967 if (rc)
2968 return ERR_PTR(rc);
2969 return pntsd;
2970}
2971
Shirish Pargaonkar366ed842017-06-28 22:37:32 -05002972static int
2973set_smb2_acl(struct cifs_ntsd *pnntsd, __u32 acllen,
2974 struct inode *inode, const char *path, int aclflag)
2975{
2976 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2977 unsigned int xid;
2978 int rc, access_flags = 0;
2979 struct cifs_tcon *tcon;
2980 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
2981 struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
2982 struct cifs_fid fid;
2983 struct cifs_open_parms oparms;
2984 __le16 *utf16_path;
2985
2986 cifs_dbg(FYI, "set smb3 acl for path %s\n", path);
2987 if (IS_ERR(tlink))
2988 return PTR_ERR(tlink);
2989
2990 tcon = tlink_tcon(tlink);
2991 xid = get_xid();
2992
2993 if (backup_cred(cifs_sb))
2994 oparms.create_options = CREATE_OPEN_BACKUP_INTENT;
2995 else
2996 oparms.create_options = 0;
2997
2998 if (aclflag == CIFS_ACL_OWNER || aclflag == CIFS_ACL_GROUP)
2999 access_flags = WRITE_OWNER;
3000 else
3001 access_flags = WRITE_DAC;
3002
3003 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
Steve Frenchcfe89092018-05-19 02:04:55 -05003004 if (!utf16_path) {
3005 rc = -ENOMEM;
3006 free_xid(xid);
3007 return rc;
3008 }
Shirish Pargaonkar366ed842017-06-28 22:37:32 -05003009
3010 oparms.tcon = tcon;
3011 oparms.desired_access = access_flags;
3012 oparms.disposition = FILE_OPEN;
3013 oparms.path = path;
3014 oparms.fid = &fid;
3015 oparms.reconnect = false;
3016
Ronnie Sahlberg9d874c32018-06-08 13:21:18 +10003017 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, NULL);
Shirish Pargaonkar366ed842017-06-28 22:37:32 -05003018 kfree(utf16_path);
3019 if (!rc) {
3020 rc = SMB2_set_acl(xid, tlink_tcon(tlink), fid.persistent_fid,
3021 fid.volatile_fid, pnntsd, acllen, aclflag);
3022 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
3023 }
3024
3025 cifs_put_tlink(tlink);
3026 free_xid(xid);
3027 return rc;
3028}
Shirish Pargaonkar366ed842017-06-28 22:37:32 -05003029
Shirish Pargaonkar2f1afe22017-06-22 22:52:05 -05003030/* Retrieve an ACL from the server */
3031static struct cifs_ntsd *
3032get_smb2_acl(struct cifs_sb_info *cifs_sb,
3033 struct inode *inode, const char *path,
3034 u32 *pacllen)
3035{
3036 struct cifs_ntsd *pntsd = NULL;
3037 struct cifsFileInfo *open_file = NULL;
3038
3039 if (inode)
3040 open_file = find_readable_file(CIFS_I(inode), true);
3041 if (!open_file)
3042 return get_smb2_acl_by_path(cifs_sb, path, pacllen);
3043
3044 pntsd = get_smb2_acl_by_fid(cifs_sb, &open_file->fid, pacllen);
3045 cifsFileInfo_put(open_file);
3046 return pntsd;
3047}
Shirish Pargaonkar2f1afe22017-06-22 22:52:05 -05003048
Steve French30175622014-08-17 18:16:40 -05003049static long smb3_zero_range(struct file *file, struct cifs_tcon *tcon,
3050 loff_t offset, loff_t len, bool keep_size)
3051{
Ronnie Sahlberg72c419d2019-03-13 14:37:49 +10003052 struct cifs_ses *ses = tcon->ses;
Steve French30175622014-08-17 18:16:40 -05003053 struct inode *inode;
3054 struct cifsInodeInfo *cifsi;
3055 struct cifsFileInfo *cfile = file->private_data;
3056 struct file_zero_data_information fsctl_buf;
3057 long rc;
3058 unsigned int xid;
Ronnie Sahlberg72c419d2019-03-13 14:37:49 +10003059 __le64 eof;
Steve French30175622014-08-17 18:16:40 -05003060
3061 xid = get_xid();
3062
David Howells2b0143b2015-03-17 22:25:59 +00003063 inode = d_inode(cfile->dentry);
Steve French30175622014-08-17 18:16:40 -05003064 cifsi = CIFS_I(inode);
3065
Christoph Probsta205d502019-05-08 21:36:25 +02003066 trace_smb3_zero_enter(xid, cfile->fid.persistent_fid, tcon->tid,
Steve French779ede02019-03-13 01:41:49 -05003067 ses->Suid, offset, len);
3068
3069
Steve French30175622014-08-17 18:16:40 -05003070 /* if file not oplocked can't be sure whether asking to extend size */
3071 if (!CIFS_CACHE_READ(cifsi))
Steve Frenchcfe89092018-05-19 02:04:55 -05003072 if (keep_size == false) {
3073 rc = -EOPNOTSUPP;
Steve French779ede02019-03-13 01:41:49 -05003074 trace_smb3_zero_err(xid, cfile->fid.persistent_fid,
3075 tcon->tid, ses->Suid, offset, len, rc);
Steve Frenchcfe89092018-05-19 02:04:55 -05003076 free_xid(xid);
3077 return rc;
3078 }
Steve French30175622014-08-17 18:16:40 -05003079
Steve Frenchd1c35af2019-05-09 00:09:37 -05003080 cifs_dbg(FYI, "Offset %lld len %lld\n", offset, len);
Steve French30175622014-08-17 18:16:40 -05003081
3082 fsctl_buf.FileOffset = cpu_to_le64(offset);
3083 fsctl_buf.BeyondFinalZero = cpu_to_le64(offset + len);
3084
Ronnie Sahlbergc4250142019-05-02 15:52:57 +10003085 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
3086 cfile->fid.volatile_fid, FSCTL_SET_ZERO_DATA, true,
3087 (char *)&fsctl_buf,
3088 sizeof(struct file_zero_data_information),
3089 0, NULL, NULL);
Ronnie Sahlberg72c419d2019-03-13 14:37:49 +10003090 if (rc)
3091 goto zero_range_exit;
3092
3093 /*
3094 * do we also need to change the size of the file?
3095 */
3096 if (keep_size == false && i_size_read(inode) < offset + len) {
Ronnie Sahlberg72c419d2019-03-13 14:37:49 +10003097 eof = cpu_to_le64(offset + len);
Ronnie Sahlbergc4250142019-05-02 15:52:57 +10003098 rc = SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
3099 cfile->fid.volatile_fid, cfile->pid, &eof);
Ronnie Sahlberg72c419d2019-03-13 14:37:49 +10003100 }
3101
Ronnie Sahlberg72c419d2019-03-13 14:37:49 +10003102 zero_range_exit:
Steve French30175622014-08-17 18:16:40 -05003103 free_xid(xid);
Steve French779ede02019-03-13 01:41:49 -05003104 if (rc)
3105 trace_smb3_zero_err(xid, cfile->fid.persistent_fid, tcon->tid,
3106 ses->Suid, offset, len, rc);
3107 else
3108 trace_smb3_zero_done(xid, cfile->fid.persistent_fid, tcon->tid,
3109 ses->Suid, offset, len);
Steve French30175622014-08-17 18:16:40 -05003110 return rc;
3111}
3112
Steve French31742c52014-08-17 08:38:47 -05003113static long smb3_punch_hole(struct file *file, struct cifs_tcon *tcon,
3114 loff_t offset, loff_t len)
3115{
3116 struct inode *inode;
Steve French31742c52014-08-17 08:38:47 -05003117 struct cifsFileInfo *cfile = file->private_data;
3118 struct file_zero_data_information fsctl_buf;
3119 long rc;
3120 unsigned int xid;
3121 __u8 set_sparse = 1;
3122
3123 xid = get_xid();
3124
David Howells2b0143b2015-03-17 22:25:59 +00003125 inode = d_inode(cfile->dentry);
Steve French31742c52014-08-17 08:38:47 -05003126
3127 /* Need to make file sparse, if not already, before freeing range. */
3128 /* Consider adding equivalent for compressed since it could also work */
Steve Frenchcfe89092018-05-19 02:04:55 -05003129 if (!smb2_set_sparse(xid, tcon, cfile, inode, set_sparse)) {
3130 rc = -EOPNOTSUPP;
3131 free_xid(xid);
3132 return rc;
3133 }
Steve French31742c52014-08-17 08:38:47 -05003134
Christoph Probsta205d502019-05-08 21:36:25 +02003135 cifs_dbg(FYI, "Offset %lld len %lld\n", offset, len);
Steve French31742c52014-08-17 08:38:47 -05003136
3137 fsctl_buf.FileOffset = cpu_to_le64(offset);
3138 fsctl_buf.BeyondFinalZero = cpu_to_le64(offset + len);
3139
3140 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
3141 cfile->fid.volatile_fid, FSCTL_SET_ZERO_DATA,
Aurelien Aptel63a83b82018-01-24 13:46:11 +01003142 true /* is_fctl */, (char *)&fsctl_buf,
Steve French153322f2019-03-28 22:32:49 -05003143 sizeof(struct file_zero_data_information),
3144 CIFSMaxBufSize, NULL, NULL);
Steve French31742c52014-08-17 08:38:47 -05003145 free_xid(xid);
3146 return rc;
3147}
3148
Steve French9ccf3212014-10-18 17:01:15 -05003149static long smb3_simple_falloc(struct file *file, struct cifs_tcon *tcon,
3150 loff_t off, loff_t len, bool keep_size)
3151{
3152 struct inode *inode;
3153 struct cifsInodeInfo *cifsi;
3154 struct cifsFileInfo *cfile = file->private_data;
3155 long rc = -EOPNOTSUPP;
3156 unsigned int xid;
Ronnie Sahlbergf1699472019-03-15 00:08:48 +10003157 __le64 eof;
Steve French9ccf3212014-10-18 17:01:15 -05003158
3159 xid = get_xid();
3160
David Howells2b0143b2015-03-17 22:25:59 +00003161 inode = d_inode(cfile->dentry);
Steve French9ccf3212014-10-18 17:01:15 -05003162 cifsi = CIFS_I(inode);
3163
Steve French779ede02019-03-13 01:41:49 -05003164 trace_smb3_falloc_enter(xid, cfile->fid.persistent_fid, tcon->tid,
3165 tcon->ses->Suid, off, len);
Steve French9ccf3212014-10-18 17:01:15 -05003166 /* if file not oplocked can't be sure whether asking to extend size */
3167 if (!CIFS_CACHE_READ(cifsi))
Steve Frenchcfe89092018-05-19 02:04:55 -05003168 if (keep_size == false) {
Steve French779ede02019-03-13 01:41:49 -05003169 trace_smb3_falloc_err(xid, cfile->fid.persistent_fid,
3170 tcon->tid, tcon->ses->Suid, off, len, rc);
Steve Frenchcfe89092018-05-19 02:04:55 -05003171 free_xid(xid);
3172 return rc;
3173 }
Steve French9ccf3212014-10-18 17:01:15 -05003174
3175 /*
Ronnie Sahlberg8bd0d702020-01-17 11:45:02 +10003176 * Extending the file
3177 */
3178 if ((keep_size == false) && i_size_read(inode) < off + len) {
3179 if ((cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) == 0)
3180 smb2_set_sparse(xid, tcon, cfile, inode, false);
3181
3182 eof = cpu_to_le64(off + len);
3183 rc = SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
3184 cfile->fid.volatile_fid, cfile->pid, &eof);
3185 if (rc == 0) {
3186 cifsi->server_eof = off + len;
3187 cifs_setsize(inode, off + len);
3188 cifs_truncate_page(inode->i_mapping, inode->i_size);
3189 truncate_setsize(inode, off + len);
3190 }
3191 goto out;
3192 }
3193
3194 /*
Steve French9ccf3212014-10-18 17:01:15 -05003195 * Files are non-sparse by default so falloc may be a no-op
Ronnie Sahlberg8bd0d702020-01-17 11:45:02 +10003196 * Must check if file sparse. If not sparse, and since we are not
3197 * extending then no need to do anything since file already allocated
Steve French9ccf3212014-10-18 17:01:15 -05003198 */
3199 if ((cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) == 0) {
Ronnie Sahlberg8bd0d702020-01-17 11:45:02 +10003200 rc = 0;
3201 goto out;
Steve French9ccf3212014-10-18 17:01:15 -05003202 }
3203
3204 if ((keep_size == true) || (i_size_read(inode) >= off + len)) {
3205 /*
3206 * Check if falloc starts within first few pages of file
3207 * and ends within a few pages of the end of file to
3208 * ensure that most of file is being forced to be
3209 * fallocated now. If so then setting whole file sparse
3210 * ie potentially making a few extra pages at the beginning
3211 * or end of the file non-sparse via set_sparse is harmless.
3212 */
Steve Frenchcfe89092018-05-19 02:04:55 -05003213 if ((off > 8192) || (off + len + 8192 < i_size_read(inode))) {
3214 rc = -EOPNOTSUPP;
Ronnie Sahlberg8bd0d702020-01-17 11:45:02 +10003215 goto out;
Ronnie Sahlbergf1699472019-03-15 00:08:48 +10003216 }
Steve French9ccf3212014-10-18 17:01:15 -05003217 }
Steve French9ccf3212014-10-18 17:01:15 -05003218
Ronnie Sahlberg8bd0d702020-01-17 11:45:02 +10003219 smb2_set_sparse(xid, tcon, cfile, inode, false);
3220 rc = 0;
3221
3222out:
Steve French779ede02019-03-13 01:41:49 -05003223 if (rc)
3224 trace_smb3_falloc_err(xid, cfile->fid.persistent_fid, tcon->tid,
3225 tcon->ses->Suid, off, len, rc);
3226 else
3227 trace_smb3_falloc_done(xid, cfile->fid.persistent_fid, tcon->tid,
3228 tcon->ses->Suid, off, len);
Steve French9ccf3212014-10-18 17:01:15 -05003229
3230 free_xid(xid);
3231 return rc;
3232}
3233
Ronnie Sahlbergdece44e2019-05-15 07:17:02 +10003234static loff_t smb3_llseek(struct file *file, struct cifs_tcon *tcon, loff_t offset, int whence)
3235{
3236 struct cifsFileInfo *wrcfile, *cfile = file->private_data;
3237 struct cifsInodeInfo *cifsi;
3238 struct inode *inode;
3239 int rc = 0;
3240 struct file_allocated_range_buffer in_data, *out_data = NULL;
3241 u32 out_data_len;
3242 unsigned int xid;
3243
3244 if (whence != SEEK_HOLE && whence != SEEK_DATA)
3245 return generic_file_llseek(file, offset, whence);
3246
3247 inode = d_inode(cfile->dentry);
3248 cifsi = CIFS_I(inode);
3249
3250 if (offset < 0 || offset >= i_size_read(inode))
3251 return -ENXIO;
3252
3253 xid = get_xid();
3254 /*
3255 * We need to be sure that all dirty pages are written as they
3256 * might fill holes on the server.
3257 * Note that we also MUST flush any written pages since at least
3258 * some servers (Windows2016) will not reflect recent writes in
3259 * QUERY_ALLOCATED_RANGES until SMB2_flush is called.
3260 */
3261 wrcfile = find_writable_file(cifsi, false);
3262 if (wrcfile) {
3263 filemap_write_and_wait(inode->i_mapping);
3264 smb2_flush_file(xid, tcon, &wrcfile->fid);
3265 cifsFileInfo_put(wrcfile);
3266 }
3267
3268 if (!(cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE)) {
3269 if (whence == SEEK_HOLE)
3270 offset = i_size_read(inode);
3271 goto lseek_exit;
3272 }
3273
3274 in_data.file_offset = cpu_to_le64(offset);
3275 in_data.length = cpu_to_le64(i_size_read(inode));
3276
3277 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
3278 cfile->fid.volatile_fid,
3279 FSCTL_QUERY_ALLOCATED_RANGES, true,
3280 (char *)&in_data, sizeof(in_data),
3281 sizeof(struct file_allocated_range_buffer),
3282 (char **)&out_data, &out_data_len);
3283 if (rc == -E2BIG)
3284 rc = 0;
3285 if (rc)
3286 goto lseek_exit;
3287
3288 if (whence == SEEK_HOLE && out_data_len == 0)
3289 goto lseek_exit;
3290
3291 if (whence == SEEK_DATA && out_data_len == 0) {
3292 rc = -ENXIO;
3293 goto lseek_exit;
3294 }
3295
3296 if (out_data_len < sizeof(struct file_allocated_range_buffer)) {
3297 rc = -EINVAL;
3298 goto lseek_exit;
3299 }
3300 if (whence == SEEK_DATA) {
3301 offset = le64_to_cpu(out_data->file_offset);
3302 goto lseek_exit;
3303 }
3304 if (offset < le64_to_cpu(out_data->file_offset))
3305 goto lseek_exit;
3306
3307 offset = le64_to_cpu(out_data->file_offset) + le64_to_cpu(out_data->length);
3308
3309 lseek_exit:
3310 free_xid(xid);
3311 kfree(out_data);
3312 if (!rc)
3313 return vfs_setpos(file, offset, inode->i_sb->s_maxbytes);
3314 else
3315 return rc;
3316}
3317
Ronnie Sahlberg2f3ebab2019-04-25 16:45:29 +10003318static int smb3_fiemap(struct cifs_tcon *tcon,
3319 struct cifsFileInfo *cfile,
3320 struct fiemap_extent_info *fei, u64 start, u64 len)
3321{
3322 unsigned int xid;
3323 struct file_allocated_range_buffer in_data, *out_data;
3324 u32 out_data_len;
3325 int i, num, rc, flags, last_blob;
3326 u64 next;
3327
3328 if (fiemap_check_flags(fei, FIEMAP_FLAG_SYNC))
3329 return -EBADR;
3330
3331 xid = get_xid();
3332 again:
3333 in_data.file_offset = cpu_to_le64(start);
3334 in_data.length = cpu_to_le64(len);
3335
3336 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
3337 cfile->fid.volatile_fid,
3338 FSCTL_QUERY_ALLOCATED_RANGES, true,
3339 (char *)&in_data, sizeof(in_data),
3340 1024 * sizeof(struct file_allocated_range_buffer),
3341 (char **)&out_data, &out_data_len);
3342 if (rc == -E2BIG) {
3343 last_blob = 0;
3344 rc = 0;
3345 } else
3346 last_blob = 1;
3347 if (rc)
3348 goto out;
3349
3350 if (out_data_len < sizeof(struct file_allocated_range_buffer)) {
3351 rc = -EINVAL;
3352 goto out;
3353 }
3354 if (out_data_len % sizeof(struct file_allocated_range_buffer)) {
3355 rc = -EINVAL;
3356 goto out;
3357 }
3358
3359 num = out_data_len / sizeof(struct file_allocated_range_buffer);
3360 for (i = 0; i < num; i++) {
3361 flags = 0;
3362 if (i == num - 1 && last_blob)
3363 flags |= FIEMAP_EXTENT_LAST;
3364
3365 rc = fiemap_fill_next_extent(fei,
3366 le64_to_cpu(out_data[i].file_offset),
3367 le64_to_cpu(out_data[i].file_offset),
3368 le64_to_cpu(out_data[i].length),
3369 flags);
3370 if (rc < 0)
3371 goto out;
3372 if (rc == 1) {
3373 rc = 0;
3374 goto out;
3375 }
3376 }
3377
3378 if (!last_blob) {
3379 next = le64_to_cpu(out_data[num - 1].file_offset) +
3380 le64_to_cpu(out_data[num - 1].length);
3381 len = len - (next - start);
3382 start = next;
3383 goto again;
3384 }
3385
3386 out:
3387 free_xid(xid);
3388 kfree(out_data);
3389 return rc;
3390}
Steve French9ccf3212014-10-18 17:01:15 -05003391
Steve French31742c52014-08-17 08:38:47 -05003392static long smb3_fallocate(struct file *file, struct cifs_tcon *tcon, int mode,
3393 loff_t off, loff_t len)
3394{
3395 /* KEEP_SIZE already checked for by do_fallocate */
3396 if (mode & FALLOC_FL_PUNCH_HOLE)
3397 return smb3_punch_hole(file, tcon, off, len);
Steve French30175622014-08-17 18:16:40 -05003398 else if (mode & FALLOC_FL_ZERO_RANGE) {
3399 if (mode & FALLOC_FL_KEEP_SIZE)
3400 return smb3_zero_range(file, tcon, off, len, true);
3401 return smb3_zero_range(file, tcon, off, len, false);
Steve French9ccf3212014-10-18 17:01:15 -05003402 } else if (mode == FALLOC_FL_KEEP_SIZE)
3403 return smb3_simple_falloc(file, tcon, off, len, true);
3404 else if (mode == 0)
3405 return smb3_simple_falloc(file, tcon, off, len, false);
Steve French31742c52014-08-17 08:38:47 -05003406
3407 return -EOPNOTSUPP;
3408}
3409
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04003410static void
Sachin Prabhuc11f1df2014-03-11 16:11:47 +00003411smb2_downgrade_oplock(struct TCP_Server_Info *server,
Pavel Shilovsky9bd45402019-10-29 16:51:19 -07003412 struct cifsInodeInfo *cinode, __u32 oplock,
3413 unsigned int epoch, bool *purge_cache)
Sachin Prabhuc11f1df2014-03-11 16:11:47 +00003414{
Pavel Shilovsky9bd45402019-10-29 16:51:19 -07003415 server->ops->set_oplock_level(cinode, oplock, 0, NULL);
Sachin Prabhuc11f1df2014-03-11 16:11:47 +00003416}
3417
3418static void
Pavel Shilovsky9bd45402019-10-29 16:51:19 -07003419smb21_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
3420 unsigned int epoch, bool *purge_cache);
3421
3422static void
3423smb3_downgrade_oplock(struct TCP_Server_Info *server,
3424 struct cifsInodeInfo *cinode, __u32 oplock,
3425 unsigned int epoch, bool *purge_cache)
Pavel Shilovsky7b9b9ed2019-02-13 15:43:08 -08003426{
Pavel Shilovsky9bd45402019-10-29 16:51:19 -07003427 unsigned int old_state = cinode->oplock;
3428 unsigned int old_epoch = cinode->epoch;
3429 unsigned int new_state;
3430
3431 if (epoch > old_epoch) {
3432 smb21_set_oplock_level(cinode, oplock, 0, NULL);
3433 cinode->epoch = epoch;
3434 }
3435
3436 new_state = cinode->oplock;
3437 *purge_cache = false;
3438
3439 if ((old_state & CIFS_CACHE_READ_FLG) != 0 &&
3440 (new_state & CIFS_CACHE_READ_FLG) == 0)
3441 *purge_cache = true;
3442 else if (old_state == new_state && (epoch - old_epoch > 1))
3443 *purge_cache = true;
Pavel Shilovsky7b9b9ed2019-02-13 15:43:08 -08003444}
3445
3446static void
Pavel Shilovsky42873b02013-09-05 21:30:16 +04003447smb2_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
3448 unsigned int epoch, bool *purge_cache)
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04003449{
3450 oplock &= 0xFF;
3451 if (oplock == SMB2_OPLOCK_LEVEL_NOCHANGE)
3452 return;
3453 if (oplock == SMB2_OPLOCK_LEVEL_BATCH) {
Pavel Shilovsky42873b02013-09-05 21:30:16 +04003454 cinode->oplock = CIFS_CACHE_RHW_FLG;
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04003455 cifs_dbg(FYI, "Batch Oplock granted on inode %p\n",
3456 &cinode->vfs_inode);
3457 } else if (oplock == SMB2_OPLOCK_LEVEL_EXCLUSIVE) {
Pavel Shilovsky42873b02013-09-05 21:30:16 +04003458 cinode->oplock = CIFS_CACHE_RW_FLG;
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04003459 cifs_dbg(FYI, "Exclusive Oplock granted on inode %p\n",
3460 &cinode->vfs_inode);
3461 } else if (oplock == SMB2_OPLOCK_LEVEL_II) {
3462 cinode->oplock = CIFS_CACHE_READ_FLG;
3463 cifs_dbg(FYI, "Level II Oplock granted on inode %p\n",
3464 &cinode->vfs_inode);
3465 } else
3466 cinode->oplock = 0;
3467}
3468
3469static void
Pavel Shilovsky42873b02013-09-05 21:30:16 +04003470smb21_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
3471 unsigned int epoch, bool *purge_cache)
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04003472{
3473 char message[5] = {0};
Christoph Probst6a54b2e2019-05-07 17:16:40 +02003474 unsigned int new_oplock = 0;
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04003475
3476 oplock &= 0xFF;
3477 if (oplock == SMB2_OPLOCK_LEVEL_NOCHANGE)
3478 return;
3479
Pavel Shilovskya016e272019-09-26 12:31:20 -07003480 /* Check if the server granted an oplock rather than a lease */
3481 if (oplock & SMB2_OPLOCK_LEVEL_EXCLUSIVE)
3482 return smb2_set_oplock_level(cinode, oplock, epoch,
3483 purge_cache);
3484
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04003485 if (oplock & SMB2_LEASE_READ_CACHING_HE) {
Christoph Probst6a54b2e2019-05-07 17:16:40 +02003486 new_oplock |= CIFS_CACHE_READ_FLG;
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04003487 strcat(message, "R");
3488 }
3489 if (oplock & SMB2_LEASE_HANDLE_CACHING_HE) {
Christoph Probst6a54b2e2019-05-07 17:16:40 +02003490 new_oplock |= CIFS_CACHE_HANDLE_FLG;
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04003491 strcat(message, "H");
3492 }
3493 if (oplock & SMB2_LEASE_WRITE_CACHING_HE) {
Christoph Probst6a54b2e2019-05-07 17:16:40 +02003494 new_oplock |= CIFS_CACHE_WRITE_FLG;
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04003495 strcat(message, "W");
3496 }
Christoph Probst6a54b2e2019-05-07 17:16:40 +02003497 if (!new_oplock)
3498 strncpy(message, "None", sizeof(message));
3499
3500 cinode->oplock = new_oplock;
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04003501 cifs_dbg(FYI, "%s Lease granted on inode %p\n", message,
3502 &cinode->vfs_inode);
3503}
3504
Pavel Shilovsky42873b02013-09-05 21:30:16 +04003505static void
3506smb3_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
3507 unsigned int epoch, bool *purge_cache)
3508{
3509 unsigned int old_oplock = cinode->oplock;
3510
3511 smb21_set_oplock_level(cinode, oplock, epoch, purge_cache);
3512
3513 if (purge_cache) {
3514 *purge_cache = false;
3515 if (old_oplock == CIFS_CACHE_READ_FLG) {
3516 if (cinode->oplock == CIFS_CACHE_READ_FLG &&
3517 (epoch - cinode->epoch > 0))
3518 *purge_cache = true;
3519 else if (cinode->oplock == CIFS_CACHE_RH_FLG &&
3520 (epoch - cinode->epoch > 1))
3521 *purge_cache = true;
3522 else if (cinode->oplock == CIFS_CACHE_RHW_FLG &&
3523 (epoch - cinode->epoch > 1))
3524 *purge_cache = true;
3525 else if (cinode->oplock == 0 &&
3526 (epoch - cinode->epoch > 0))
3527 *purge_cache = true;
3528 } else if (old_oplock == CIFS_CACHE_RH_FLG) {
3529 if (cinode->oplock == CIFS_CACHE_RH_FLG &&
3530 (epoch - cinode->epoch > 0))
3531 *purge_cache = true;
3532 else if (cinode->oplock == CIFS_CACHE_RHW_FLG &&
3533 (epoch - cinode->epoch > 1))
3534 *purge_cache = true;
3535 }
3536 cinode->epoch = epoch;
3537 }
3538}
3539
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04003540static bool
3541smb2_is_read_op(__u32 oplock)
3542{
3543 return oplock == SMB2_OPLOCK_LEVEL_II;
3544}
3545
3546static bool
3547smb21_is_read_op(__u32 oplock)
3548{
3549 return (oplock & SMB2_LEASE_READ_CACHING_HE) &&
3550 !(oplock & SMB2_LEASE_WRITE_CACHING_HE);
3551}
3552
Pavel Shilovskyf0473902013-09-04 13:44:05 +04003553static __le32
3554map_oplock_to_lease(u8 oplock)
3555{
3556 if (oplock == SMB2_OPLOCK_LEVEL_EXCLUSIVE)
3557 return SMB2_LEASE_WRITE_CACHING | SMB2_LEASE_READ_CACHING;
3558 else if (oplock == SMB2_OPLOCK_LEVEL_II)
3559 return SMB2_LEASE_READ_CACHING;
3560 else if (oplock == SMB2_OPLOCK_LEVEL_BATCH)
3561 return SMB2_LEASE_HANDLE_CACHING | SMB2_LEASE_READ_CACHING |
3562 SMB2_LEASE_WRITE_CACHING;
3563 return 0;
3564}
3565
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04003566static char *
3567smb2_create_lease_buf(u8 *lease_key, u8 oplock)
3568{
3569 struct create_lease *buf;
3570
3571 buf = kzalloc(sizeof(struct create_lease), GFP_KERNEL);
3572 if (!buf)
3573 return NULL;
3574
Stefano Brivio729c0c92018-07-05 15:10:02 +02003575 memcpy(&buf->lcontext.LeaseKey, lease_key, SMB2_LEASE_KEY_SIZE);
Pavel Shilovskyf0473902013-09-04 13:44:05 +04003576 buf->lcontext.LeaseState = map_oplock_to_lease(oplock);
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04003577
3578 buf->ccontext.DataOffset = cpu_to_le16(offsetof
3579 (struct create_lease, lcontext));
3580 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context));
3581 buf->ccontext.NameOffset = cpu_to_le16(offsetof
3582 (struct create_lease, Name));
3583 buf->ccontext.NameLength = cpu_to_le16(4);
Steve French12197a72014-05-14 05:29:40 -07003584 /* SMB2_CREATE_REQUEST_LEASE is "RqLs" */
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04003585 buf->Name[0] = 'R';
3586 buf->Name[1] = 'q';
3587 buf->Name[2] = 'L';
3588 buf->Name[3] = 's';
3589 return (char *)buf;
3590}
3591
Pavel Shilovskyf0473902013-09-04 13:44:05 +04003592static char *
3593smb3_create_lease_buf(u8 *lease_key, u8 oplock)
3594{
3595 struct create_lease_v2 *buf;
3596
3597 buf = kzalloc(sizeof(struct create_lease_v2), GFP_KERNEL);
3598 if (!buf)
3599 return NULL;
3600
Stefano Brivio729c0c92018-07-05 15:10:02 +02003601 memcpy(&buf->lcontext.LeaseKey, lease_key, SMB2_LEASE_KEY_SIZE);
Pavel Shilovskyf0473902013-09-04 13:44:05 +04003602 buf->lcontext.LeaseState = map_oplock_to_lease(oplock);
3603
3604 buf->ccontext.DataOffset = cpu_to_le16(offsetof
3605 (struct create_lease_v2, lcontext));
3606 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context_v2));
3607 buf->ccontext.NameOffset = cpu_to_le16(offsetof
3608 (struct create_lease_v2, Name));
3609 buf->ccontext.NameLength = cpu_to_le16(4);
Steve French12197a72014-05-14 05:29:40 -07003610 /* SMB2_CREATE_REQUEST_LEASE is "RqLs" */
Pavel Shilovskyf0473902013-09-04 13:44:05 +04003611 buf->Name[0] = 'R';
3612 buf->Name[1] = 'q';
3613 buf->Name[2] = 'L';
3614 buf->Name[3] = 's';
3615 return (char *)buf;
3616}
3617
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04003618static __u8
Ronnie Sahlberg96164ab2018-04-26 08:10:18 -06003619smb2_parse_lease_buf(void *buf, unsigned int *epoch, char *lease_key)
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04003620{
3621 struct create_lease *lc = (struct create_lease *)buf;
3622
Pavel Shilovsky42873b02013-09-05 21:30:16 +04003623 *epoch = 0; /* not used */
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04003624 if (lc->lcontext.LeaseFlags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS)
3625 return SMB2_OPLOCK_LEVEL_NOCHANGE;
3626 return le32_to_cpu(lc->lcontext.LeaseState);
3627}
3628
Pavel Shilovskyf0473902013-09-04 13:44:05 +04003629static __u8
Ronnie Sahlberg96164ab2018-04-26 08:10:18 -06003630smb3_parse_lease_buf(void *buf, unsigned int *epoch, char *lease_key)
Pavel Shilovskyf0473902013-09-04 13:44:05 +04003631{
3632 struct create_lease_v2 *lc = (struct create_lease_v2 *)buf;
3633
Pavel Shilovsky42873b02013-09-05 21:30:16 +04003634 *epoch = le16_to_cpu(lc->lcontext.Epoch);
Pavel Shilovskyf0473902013-09-04 13:44:05 +04003635 if (lc->lcontext.LeaseFlags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS)
3636 return SMB2_OPLOCK_LEVEL_NOCHANGE;
Ronnie Sahlberg96164ab2018-04-26 08:10:18 -06003637 if (lease_key)
Stefano Brivio729c0c92018-07-05 15:10:02 +02003638 memcpy(lease_key, &lc->lcontext.LeaseKey, SMB2_LEASE_KEY_SIZE);
Pavel Shilovskyf0473902013-09-04 13:44:05 +04003639 return le32_to_cpu(lc->lcontext.LeaseState);
3640}
3641
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04003642static unsigned int
3643smb2_wp_retry_size(struct inode *inode)
3644{
3645 return min_t(unsigned int, CIFS_SB(inode->i_sb)->wsize,
3646 SMB2_MAX_BUFFER_SIZE);
3647}
3648
Pavel Shilovsky52755802014-08-18 20:49:57 +04003649static bool
3650smb2_dir_needs_close(struct cifsFileInfo *cfile)
3651{
3652 return !cfile->invalidHandle;
3653}
3654
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07003655static void
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10003656fill_transform_hdr(struct smb2_transform_hdr *tr_hdr, unsigned int orig_len,
Steve French2b2f7542019-06-07 15:16:10 -05003657 struct smb_rqst *old_rq, __le16 cipher_type)
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07003658{
3659 struct smb2_sync_hdr *shdr =
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10003660 (struct smb2_sync_hdr *)old_rq->rq_iov[0].iov_base;
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07003661
3662 memset(tr_hdr, 0, sizeof(struct smb2_transform_hdr));
3663 tr_hdr->ProtocolId = SMB2_TRANSFORM_PROTO_NUM;
3664 tr_hdr->OriginalMessageSize = cpu_to_le32(orig_len);
3665 tr_hdr->Flags = cpu_to_le16(0x01);
Steve French2b2f7542019-06-07 15:16:10 -05003666 if (cipher_type == SMB2_ENCRYPTION_AES128_GCM)
3667 get_random_bytes(&tr_hdr->Nonce, SMB3_AES128GCM_NONCE);
3668 else
3669 get_random_bytes(&tr_hdr->Nonce, SMB3_AES128CCM_NONCE);
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07003670 memcpy(&tr_hdr->SessionId, &shdr->SessionId, 8);
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07003671}
3672
Ronnie Sahlberg262916b2018-02-20 12:45:21 +11003673/* We can not use the normal sg_set_buf() as we will sometimes pass a
3674 * stack object as buf.
3675 */
3676static inline void smb2_sg_set_buf(struct scatterlist *sg, const void *buf,
3677 unsigned int buflen)
3678{
Sebastien Tisserantee9d6612019-08-01 12:06:08 -05003679 void *addr;
3680 /*
3681 * VMAP_STACK (at least) puts stack into the vmalloc address space
3682 */
3683 if (is_vmalloc_addr(buf))
3684 addr = vmalloc_to_page(buf);
3685 else
3686 addr = virt_to_page(buf);
3687 sg_set_page(sg, addr, buflen, offset_in_page(buf));
Ronnie Sahlberg262916b2018-02-20 12:45:21 +11003688}
3689
Ronnie Sahlbergb2c96de2018-08-01 09:26:11 +10003690/* Assumes the first rqst has a transform header as the first iov.
3691 * I.e.
3692 * rqst[0].rq_iov[0] is transform header
3693 * rqst[0].rq_iov[1+] data to be encrypted/decrypted
3694 * rqst[1+].rq_iov[0+] data to be encrypted/decrypted
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10003695 */
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07003696static struct scatterlist *
Ronnie Sahlbergb2c96de2018-08-01 09:26:11 +10003697init_sg(int num_rqst, struct smb_rqst *rqst, u8 *sign)
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07003698{
Ronnie Sahlbergb2c96de2018-08-01 09:26:11 +10003699 unsigned int sg_len;
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07003700 struct scatterlist *sg;
3701 unsigned int i;
3702 unsigned int j;
Ronnie Sahlbergb2c96de2018-08-01 09:26:11 +10003703 unsigned int idx = 0;
3704 int skip;
3705
3706 sg_len = 1;
3707 for (i = 0; i < num_rqst; i++)
3708 sg_len += rqst[i].rq_nvec + rqst[i].rq_npages;
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07003709
3710 sg = kmalloc_array(sg_len, sizeof(struct scatterlist), GFP_KERNEL);
3711 if (!sg)
3712 return NULL;
3713
3714 sg_init_table(sg, sg_len);
Ronnie Sahlbergb2c96de2018-08-01 09:26:11 +10003715 for (i = 0; i < num_rqst; i++) {
3716 for (j = 0; j < rqst[i].rq_nvec; j++) {
3717 /*
3718 * The first rqst has a transform header where the
3719 * first 20 bytes are not part of the encrypted blob
3720 */
3721 skip = (i == 0) && (j == 0) ? 20 : 0;
3722 smb2_sg_set_buf(&sg[idx++],
3723 rqst[i].rq_iov[j].iov_base + skip,
3724 rqst[i].rq_iov[j].iov_len - skip);
Ronnie Sahlberge77fe732018-12-31 13:43:40 +10003725 }
Steve Frenchd5f07fb2018-06-05 17:46:24 -05003726
Ronnie Sahlbergb2c96de2018-08-01 09:26:11 +10003727 for (j = 0; j < rqst[i].rq_npages; j++) {
3728 unsigned int len, offset;
3729
3730 rqst_page_get_length(&rqst[i], j, &len, &offset);
3731 sg_set_page(&sg[idx++], rqst[i].rq_pages[j], len, offset);
3732 }
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07003733 }
Ronnie Sahlbergb2c96de2018-08-01 09:26:11 +10003734 smb2_sg_set_buf(&sg[idx], sign, SMB2_SIGNATURE_SIZE);
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07003735 return sg;
3736}
3737
Pavel Shilovsky61cfac6f2017-02-28 16:05:19 -08003738static int
3739smb2_get_enc_key(struct TCP_Server_Info *server, __u64 ses_id, int enc, u8 *key)
3740{
3741 struct cifs_ses *ses;
3742 u8 *ses_enc_key;
3743
3744 spin_lock(&cifs_tcp_ses_lock);
Aurelien Apteld70e9fa2019-09-20 06:31:10 +02003745 list_for_each_entry(server, &cifs_tcp_ses_list, tcp_ses_list) {
3746 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
3747 if (ses->Suid == ses_id) {
3748 ses_enc_key = enc ? ses->smb3encryptionkey :
3749 ses->smb3decryptionkey;
3750 memcpy(key, ses_enc_key, SMB3_SIGN_KEY_SIZE);
3751 spin_unlock(&cifs_tcp_ses_lock);
3752 return 0;
3753 }
3754 }
Pavel Shilovsky61cfac6f2017-02-28 16:05:19 -08003755 }
3756 spin_unlock(&cifs_tcp_ses_lock);
3757
3758 return 1;
3759}
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07003760/*
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10003761 * Encrypt or decrypt @rqst message. @rqst[0] has the following format:
3762 * iov[0] - transform header (associate data),
3763 * iov[1-N] - SMB2 header and pages - data to encrypt.
3764 * On success return encrypted data in iov[1-N] and pages, leave iov[0]
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07003765 * untouched.
3766 */
3767static int
Ronnie Sahlbergb2c96de2018-08-01 09:26:11 +10003768crypt_message(struct TCP_Server_Info *server, int num_rqst,
3769 struct smb_rqst *rqst, int enc)
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07003770{
3771 struct smb2_transform_hdr *tr_hdr =
Ronnie Sahlbergb2c96de2018-08-01 09:26:11 +10003772 (struct smb2_transform_hdr *)rqst[0].rq_iov[0].iov_base;
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10003773 unsigned int assoc_data_len = sizeof(struct smb2_transform_hdr) - 20;
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07003774 int rc = 0;
3775 struct scatterlist *sg;
3776 u8 sign[SMB2_SIGNATURE_SIZE] = {};
Pavel Shilovsky61cfac6f2017-02-28 16:05:19 -08003777 u8 key[SMB3_SIGN_KEY_SIZE];
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07003778 struct aead_request *req;
3779 char *iv;
3780 unsigned int iv_len;
Gilad Ben-Yossefa5186b82017-10-18 08:00:46 +01003781 DECLARE_CRYPTO_WAIT(wait);
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07003782 struct crypto_aead *tfm;
3783 unsigned int crypt_len = le32_to_cpu(tr_hdr->OriginalMessageSize);
3784
Pavel Shilovsky61cfac6f2017-02-28 16:05:19 -08003785 rc = smb2_get_enc_key(server, tr_hdr->SessionId, enc, key);
3786 if (rc) {
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +10003787 cifs_server_dbg(VFS, "%s: Could not get %scryption key\n", __func__,
Pavel Shilovsky61cfac6f2017-02-28 16:05:19 -08003788 enc ? "en" : "de");
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07003789 return 0;
3790 }
3791
3792 rc = smb3_crypto_aead_allocate(server);
3793 if (rc) {
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +10003794 cifs_server_dbg(VFS, "%s: crypto alloc failed\n", __func__);
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07003795 return rc;
3796 }
3797
3798 tfm = enc ? server->secmech.ccmaesencrypt :
3799 server->secmech.ccmaesdecrypt;
Pavel Shilovsky61cfac6f2017-02-28 16:05:19 -08003800 rc = crypto_aead_setkey(tfm, key, SMB3_SIGN_KEY_SIZE);
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07003801 if (rc) {
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +10003802 cifs_server_dbg(VFS, "%s: Failed to set aead key %d\n", __func__, rc);
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07003803 return rc;
3804 }
3805
3806 rc = crypto_aead_setauthsize(tfm, SMB2_SIGNATURE_SIZE);
3807 if (rc) {
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +10003808 cifs_server_dbg(VFS, "%s: Failed to set authsize %d\n", __func__, rc);
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07003809 return rc;
3810 }
3811
3812 req = aead_request_alloc(tfm, GFP_KERNEL);
3813 if (!req) {
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +10003814 cifs_server_dbg(VFS, "%s: Failed to alloc aead request\n", __func__);
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07003815 return -ENOMEM;
3816 }
3817
3818 if (!enc) {
3819 memcpy(sign, &tr_hdr->Signature, SMB2_SIGNATURE_SIZE);
3820 crypt_len += SMB2_SIGNATURE_SIZE;
3821 }
3822
Ronnie Sahlbergb2c96de2018-08-01 09:26:11 +10003823 sg = init_sg(num_rqst, rqst, sign);
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07003824 if (!sg) {
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +10003825 cifs_server_dbg(VFS, "%s: Failed to init sg\n", __func__);
Christophe Jaillet517a6e42017-06-11 09:12:47 +02003826 rc = -ENOMEM;
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07003827 goto free_req;
3828 }
3829
3830 iv_len = crypto_aead_ivsize(tfm);
3831 iv = kzalloc(iv_len, GFP_KERNEL);
3832 if (!iv) {
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +10003833 cifs_server_dbg(VFS, "%s: Failed to alloc iv\n", __func__);
Christophe Jaillet517a6e42017-06-11 09:12:47 +02003834 rc = -ENOMEM;
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07003835 goto free_sg;
3836 }
Steve French2b2f7542019-06-07 15:16:10 -05003837
3838 if (server->cipher_type == SMB2_ENCRYPTION_AES128_GCM)
3839 memcpy(iv, (char *)tr_hdr->Nonce, SMB3_AES128GCM_NONCE);
3840 else {
3841 iv[0] = 3;
3842 memcpy(iv + 1, (char *)tr_hdr->Nonce, SMB3_AES128CCM_NONCE);
3843 }
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07003844
3845 aead_request_set_crypt(req, sg, sg, crypt_len, iv);
3846 aead_request_set_ad(req, assoc_data_len);
3847
3848 aead_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
Gilad Ben-Yossefa5186b82017-10-18 08:00:46 +01003849 crypto_req_done, &wait);
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07003850
Gilad Ben-Yossefa5186b82017-10-18 08:00:46 +01003851 rc = crypto_wait_req(enc ? crypto_aead_encrypt(req)
3852 : crypto_aead_decrypt(req), &wait);
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07003853
3854 if (!rc && enc)
3855 memcpy(&tr_hdr->Signature, sign, SMB2_SIGNATURE_SIZE);
3856
3857 kfree(iv);
3858free_sg:
3859 kfree(sg);
3860free_req:
3861 kfree(req);
3862 return rc;
3863}
3864
Ronnie Sahlbergb2c96de2018-08-01 09:26:11 +10003865void
3866smb3_free_compound_rqst(int num_rqst, struct smb_rqst *rqst)
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07003867{
Ronnie Sahlbergb2c96de2018-08-01 09:26:11 +10003868 int i, j;
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07003869
Ronnie Sahlbergb2c96de2018-08-01 09:26:11 +10003870 for (i = 0; i < num_rqst; i++) {
3871 if (rqst[i].rq_pages) {
3872 for (j = rqst[i].rq_npages - 1; j >= 0; j--)
3873 put_page(rqst[i].rq_pages[j]);
3874 kfree(rqst[i].rq_pages);
3875 }
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07003876 }
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07003877}
3878
Ronnie Sahlbergb2c96de2018-08-01 09:26:11 +10003879/*
3880 * This function will initialize new_rq and encrypt the content.
3881 * The first entry, new_rq[0], only contains a single iov which contains
3882 * a smb2_transform_hdr and is pre-allocated by the caller.
3883 * This function then populates new_rq[1+] with the content from olq_rq[0+].
3884 *
3885 * The end result is an array of smb_rqst structures where the first structure
3886 * only contains a single iov for the transform header which we then can pass
3887 * to crypt_message().
3888 *
3889 * new_rq[0].rq_iov[0] : smb2_transform_hdr pre-allocated by the caller
3890 * new_rq[1+].rq_iov[*] == old_rq[0+].rq_iov[*] : SMB2/3 requests
3891 */
3892static int
3893smb3_init_transform_rq(struct TCP_Server_Info *server, int num_rqst,
3894 struct smb_rqst *new_rq, struct smb_rqst *old_rq)
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07003895{
Ronnie Sahlbergb2c96de2018-08-01 09:26:11 +10003896 struct page **pages;
3897 struct smb2_transform_hdr *tr_hdr = new_rq[0].rq_iov[0].iov_base;
3898 unsigned int npages;
3899 unsigned int orig_len = 0;
3900 int i, j;
3901 int rc = -ENOMEM;
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07003902
Ronnie Sahlbergb2c96de2018-08-01 09:26:11 +10003903 for (i = 1; i < num_rqst; i++) {
3904 npages = old_rq[i - 1].rq_npages;
3905 pages = kmalloc_array(npages, sizeof(struct page *),
3906 GFP_KERNEL);
3907 if (!pages)
3908 goto err_free;
3909
3910 new_rq[i].rq_pages = pages;
3911 new_rq[i].rq_npages = npages;
3912 new_rq[i].rq_offset = old_rq[i - 1].rq_offset;
3913 new_rq[i].rq_pagesz = old_rq[i - 1].rq_pagesz;
3914 new_rq[i].rq_tailsz = old_rq[i - 1].rq_tailsz;
3915 new_rq[i].rq_iov = old_rq[i - 1].rq_iov;
3916 new_rq[i].rq_nvec = old_rq[i - 1].rq_nvec;
3917
3918 orig_len += smb_rqst_len(server, &old_rq[i - 1]);
3919
3920 for (j = 0; j < npages; j++) {
3921 pages[j] = alloc_page(GFP_KERNEL|__GFP_HIGHMEM);
3922 if (!pages[j])
3923 goto err_free;
3924 }
3925
3926 /* copy pages form the old */
3927 for (j = 0; j < npages; j++) {
3928 char *dst, *src;
3929 unsigned int offset, len;
3930
3931 rqst_page_get_length(&new_rq[i], j, &len, &offset);
3932
3933 dst = (char *) kmap(new_rq[i].rq_pages[j]) + offset;
3934 src = (char *) kmap(old_rq[i - 1].rq_pages[j]) + offset;
3935
3936 memcpy(dst, src, len);
3937 kunmap(new_rq[i].rq_pages[j]);
3938 kunmap(old_rq[i - 1].rq_pages[j]);
3939 }
3940 }
3941
3942 /* fill the 1st iov with a transform header */
Steve French2b2f7542019-06-07 15:16:10 -05003943 fill_transform_hdr(tr_hdr, orig_len, old_rq, server->cipher_type);
Ronnie Sahlbergb2c96de2018-08-01 09:26:11 +10003944
3945 rc = crypt_message(server, num_rqst, new_rq, 1);
Christoph Probsta205d502019-05-08 21:36:25 +02003946 cifs_dbg(FYI, "Encrypt message returned %d\n", rc);
Ronnie Sahlbergb2c96de2018-08-01 09:26:11 +10003947 if (rc)
3948 goto err_free;
3949
3950 return rc;
3951
3952err_free:
3953 smb3_free_compound_rqst(num_rqst - 1, &new_rq[1]);
3954 return rc;
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07003955}
3956
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08003957static int
3958smb3_is_transform_hdr(void *buf)
3959{
3960 struct smb2_transform_hdr *trhdr = buf;
3961
3962 return trhdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM;
3963}
3964
3965static int
3966decrypt_raw_data(struct TCP_Server_Info *server, char *buf,
3967 unsigned int buf_data_size, struct page **pages,
3968 unsigned int npages, unsigned int page_data_size)
3969{
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10003970 struct kvec iov[2];
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08003971 struct smb_rqst rqst = {NULL};
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08003972 int rc;
3973
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10003974 iov[0].iov_base = buf;
3975 iov[0].iov_len = sizeof(struct smb2_transform_hdr);
3976 iov[1].iov_base = buf + sizeof(struct smb2_transform_hdr);
3977 iov[1].iov_len = buf_data_size;
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08003978
3979 rqst.rq_iov = iov;
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10003980 rqst.rq_nvec = 2;
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08003981 rqst.rq_pages = pages;
3982 rqst.rq_npages = npages;
3983 rqst.rq_pagesz = PAGE_SIZE;
3984 rqst.rq_tailsz = (page_data_size % PAGE_SIZE) ? : PAGE_SIZE;
3985
Ronnie Sahlbergb2c96de2018-08-01 09:26:11 +10003986 rc = crypt_message(server, 1, &rqst, 0);
Christoph Probsta205d502019-05-08 21:36:25 +02003987 cifs_dbg(FYI, "Decrypt message returned %d\n", rc);
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08003988
3989 if (rc)
3990 return rc;
3991
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10003992 memmove(buf, iov[1].iov_base, buf_data_size);
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10003993
3994 server->total_read = buf_data_size + page_data_size;
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08003995
3996 return rc;
3997}
3998
3999static int
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08004000read_data_into_pages(struct TCP_Server_Info *server, struct page **pages,
4001 unsigned int npages, unsigned int len)
4002{
4003 int i;
4004 int length;
4005
4006 for (i = 0; i < npages; i++) {
4007 struct page *page = pages[i];
4008 size_t n;
4009
4010 n = len;
4011 if (len >= PAGE_SIZE) {
4012 /* enough data to fill the page */
4013 n = PAGE_SIZE;
4014 len -= n;
4015 } else {
4016 zero_user(page, len, PAGE_SIZE - len);
4017 len = 0;
4018 }
Long Li1dbe3462018-05-30 12:47:55 -07004019 length = cifs_read_page_from_socket(server, page, 0, n);
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08004020 if (length < 0)
4021 return length;
4022 server->total_read += length;
4023 }
4024
4025 return 0;
4026}
4027
4028static int
4029init_read_bvec(struct page **pages, unsigned int npages, unsigned int data_size,
4030 unsigned int cur_off, struct bio_vec **page_vec)
4031{
4032 struct bio_vec *bvec;
4033 int i;
4034
4035 bvec = kcalloc(npages, sizeof(struct bio_vec), GFP_KERNEL);
4036 if (!bvec)
4037 return -ENOMEM;
4038
4039 for (i = 0; i < npages; i++) {
4040 bvec[i].bv_page = pages[i];
4041 bvec[i].bv_offset = (i == 0) ? cur_off : 0;
4042 bvec[i].bv_len = min_t(unsigned int, PAGE_SIZE, data_size);
4043 data_size -= bvec[i].bv_len;
4044 }
4045
4046 if (data_size != 0) {
4047 cifs_dbg(VFS, "%s: something went wrong\n", __func__);
4048 kfree(bvec);
4049 return -EIO;
4050 }
4051
4052 *page_vec = bvec;
4053 return 0;
4054}
4055
4056static int
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004057handle_read_data(struct TCP_Server_Info *server, struct mid_q_entry *mid,
4058 char *buf, unsigned int buf_len, struct page **pages,
4059 unsigned int npages, unsigned int page_data_size)
4060{
4061 unsigned int data_offset;
4062 unsigned int data_len;
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08004063 unsigned int cur_off;
4064 unsigned int cur_page_idx;
4065 unsigned int pad_len;
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004066 struct cifs_readdata *rdata = mid->callback_data;
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10004067 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004068 struct bio_vec *bvec = NULL;
4069 struct iov_iter iter;
4070 struct kvec iov;
4071 int length;
Long Li74dcf412017-11-22 17:38:46 -07004072 bool use_rdma_mr = false;
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004073
4074 if (shdr->Command != SMB2_READ) {
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +10004075 cifs_server_dbg(VFS, "only big read responses are supported\n");
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004076 return -ENOTSUPP;
4077 }
4078
Pavel Shilovsky511c54a2017-07-08 14:32:00 -07004079 if (server->ops->is_session_expired &&
4080 server->ops->is_session_expired(buf)) {
4081 cifs_reconnect(server);
4082 wake_up(&server->response_q);
4083 return -1;
4084 }
4085
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004086 if (server->ops->is_status_pending &&
Pavel Shilovsky66265f12019-01-23 17:11:16 -08004087 server->ops->is_status_pending(buf, server))
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004088 return -1;
4089
Pavel Shilovskyec678ea2019-01-18 15:38:11 -08004090 /* set up first two iov to get credits */
4091 rdata->iov[0].iov_base = buf;
Pavel Shilovskybb1bccb2019-01-17 16:18:38 -08004092 rdata->iov[0].iov_len = 0;
4093 rdata->iov[1].iov_base = buf;
Pavel Shilovskyec678ea2019-01-18 15:38:11 -08004094 rdata->iov[1].iov_len =
Pavel Shilovskybb1bccb2019-01-17 16:18:38 -08004095 min_t(unsigned int, buf_len, server->vals->read_rsp_size);
Pavel Shilovskyec678ea2019-01-18 15:38:11 -08004096 cifs_dbg(FYI, "0: iov_base=%p iov_len=%zu\n",
4097 rdata->iov[0].iov_base, rdata->iov[0].iov_len);
4098 cifs_dbg(FYI, "1: iov_base=%p iov_len=%zu\n",
4099 rdata->iov[1].iov_base, rdata->iov[1].iov_len);
4100
4101 rdata->result = server->ops->map_error(buf, true);
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004102 if (rdata->result != 0) {
4103 cifs_dbg(FYI, "%s: server returned error %d\n",
4104 __func__, rdata->result);
Pavel Shilovskyec678ea2019-01-18 15:38:11 -08004105 /* normal error on read response */
4106 dequeue_mid(mid, false);
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004107 return 0;
4108 }
4109
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10004110 data_offset = server->ops->read_data_offset(buf);
Long Li74dcf412017-11-22 17:38:46 -07004111#ifdef CONFIG_CIFS_SMB_DIRECT
4112 use_rdma_mr = rdata->mr;
4113#endif
4114 data_len = server->ops->read_data_length(buf, use_rdma_mr);
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004115
4116 if (data_offset < server->vals->read_rsp_size) {
4117 /*
4118 * win2k8 sometimes sends an offset of 0 when the read
4119 * is beyond the EOF. Treat it as if the data starts just after
4120 * the header.
4121 */
4122 cifs_dbg(FYI, "%s: data offset (%u) inside read response header\n",
4123 __func__, data_offset);
4124 data_offset = server->vals->read_rsp_size;
4125 } else if (data_offset > MAX_CIFS_SMALL_BUFFER_SIZE) {
4126 /* data_offset is beyond the end of smallbuf */
4127 cifs_dbg(FYI, "%s: data offset (%u) beyond end of smallbuf\n",
4128 __func__, data_offset);
4129 rdata->result = -EIO;
4130 dequeue_mid(mid, rdata->result);
4131 return 0;
4132 }
4133
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08004134 pad_len = data_offset - server->vals->read_rsp_size;
4135
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004136 if (buf_len <= data_offset) {
4137 /* read response payload is in pages */
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08004138 cur_page_idx = pad_len / PAGE_SIZE;
4139 cur_off = pad_len % PAGE_SIZE;
4140
4141 if (cur_page_idx != 0) {
4142 /* data offset is beyond the 1st page of response */
4143 cifs_dbg(FYI, "%s: data offset (%u) beyond 1st page of response\n",
4144 __func__, data_offset);
4145 rdata->result = -EIO;
4146 dequeue_mid(mid, rdata->result);
4147 return 0;
4148 }
4149
4150 if (data_len > page_data_size - pad_len) {
4151 /* data_len is corrupt -- discard frame */
4152 rdata->result = -EIO;
4153 dequeue_mid(mid, rdata->result);
4154 return 0;
4155 }
4156
4157 rdata->result = init_read_bvec(pages, npages, page_data_size,
4158 cur_off, &bvec);
4159 if (rdata->result != 0) {
4160 dequeue_mid(mid, rdata->result);
4161 return 0;
4162 }
4163
David Howellsaa563d72018-10-20 00:57:56 +01004164 iov_iter_bvec(&iter, WRITE, bvec, npages, data_len);
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004165 } else if (buf_len >= data_offset + data_len) {
4166 /* read response payload is in buf */
4167 WARN_ONCE(npages > 0, "read data can be either in buf or in pages");
4168 iov.iov_base = buf + data_offset;
4169 iov.iov_len = data_len;
David Howellsaa563d72018-10-20 00:57:56 +01004170 iov_iter_kvec(&iter, WRITE, &iov, 1, data_len);
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004171 } else {
4172 /* read response payload cannot be in both buf and pages */
4173 WARN_ONCE(1, "buf can not contain only a part of read data");
4174 rdata->result = -EIO;
4175 dequeue_mid(mid, rdata->result);
4176 return 0;
4177 }
4178
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004179 length = rdata->copy_into_pages(server, rdata, &iter);
4180
4181 kfree(bvec);
4182
4183 if (length < 0)
4184 return length;
4185
4186 dequeue_mid(mid, false);
4187 return length;
4188}
4189
Steve French35cf94a2019-09-07 01:09:49 -05004190struct smb2_decrypt_work {
4191 struct work_struct decrypt;
4192 struct TCP_Server_Info *server;
4193 struct page **ppages;
4194 char *buf;
4195 unsigned int npages;
4196 unsigned int len;
4197};
4198
4199
4200static void smb2_decrypt_offload(struct work_struct *work)
4201{
4202 struct smb2_decrypt_work *dw = container_of(work,
4203 struct smb2_decrypt_work, decrypt);
4204 int i, rc;
4205 struct mid_q_entry *mid;
4206
4207 rc = decrypt_raw_data(dw->server, dw->buf, dw->server->vals->read_rsp_size,
4208 dw->ppages, dw->npages, dw->len);
4209 if (rc) {
4210 cifs_dbg(VFS, "error decrypting rc=%d\n", rc);
4211 goto free_pages;
4212 }
4213
Steve French22553972019-09-13 16:47:31 -05004214 dw->server->lstrp = jiffies;
Steve French35cf94a2019-09-07 01:09:49 -05004215 mid = smb2_find_mid(dw->server, dw->buf);
4216 if (mid == NULL)
4217 cifs_dbg(FYI, "mid not found\n");
4218 else {
4219 mid->decrypted = true;
4220 rc = handle_read_data(dw->server, mid, dw->buf,
4221 dw->server->vals->read_rsp_size,
4222 dw->ppages, dw->npages, dw->len);
Steve French22553972019-09-13 16:47:31 -05004223 mid->callback(mid);
4224 cifs_mid_q_entry_release(mid);
Steve French35cf94a2019-09-07 01:09:49 -05004225 }
4226
Steve French35cf94a2019-09-07 01:09:49 -05004227free_pages:
4228 for (i = dw->npages-1; i >= 0; i--)
4229 put_page(dw->ppages[i]);
4230
4231 kfree(dw->ppages);
4232 cifs_small_buf_release(dw->buf);
Steve Frencha08d8972019-10-26 16:00:44 -05004233 kfree(dw);
Steve French35cf94a2019-09-07 01:09:49 -05004234}
4235
4236
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004237static int
Steve French35cf94a2019-09-07 01:09:49 -05004238receive_encrypted_read(struct TCP_Server_Info *server, struct mid_q_entry **mid,
4239 int *num_mids)
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08004240{
4241 char *buf = server->smallbuf;
4242 struct smb2_transform_hdr *tr_hdr = (struct smb2_transform_hdr *)buf;
4243 unsigned int npages;
4244 struct page **pages;
4245 unsigned int len;
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10004246 unsigned int buflen = server->pdu_size;
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08004247 int rc;
4248 int i = 0;
Steve French35cf94a2019-09-07 01:09:49 -05004249 struct smb2_decrypt_work *dw;
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08004250
Steve French35cf94a2019-09-07 01:09:49 -05004251 *num_mids = 1;
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10004252 len = min_t(unsigned int, buflen, server->vals->read_rsp_size +
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08004253 sizeof(struct smb2_transform_hdr)) - HEADER_SIZE(server) + 1;
4254
4255 rc = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1, len);
4256 if (rc < 0)
4257 return rc;
4258 server->total_read += rc;
4259
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10004260 len = le32_to_cpu(tr_hdr->OriginalMessageSize) -
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +11004261 server->vals->read_rsp_size;
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08004262 npages = DIV_ROUND_UP(len, PAGE_SIZE);
4263
4264 pages = kmalloc_array(npages, sizeof(struct page *), GFP_KERNEL);
4265 if (!pages) {
4266 rc = -ENOMEM;
4267 goto discard_data;
4268 }
4269
4270 for (; i < npages; i++) {
4271 pages[i] = alloc_page(GFP_KERNEL|__GFP_HIGHMEM);
4272 if (!pages[i]) {
4273 rc = -ENOMEM;
4274 goto discard_data;
4275 }
4276 }
4277
4278 /* read read data into pages */
4279 rc = read_data_into_pages(server, pages, npages, len);
4280 if (rc)
4281 goto free_pages;
4282
Pavel Shilovsky350be252017-04-10 10:31:33 -07004283 rc = cifs_discard_remaining_data(server);
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08004284 if (rc)
4285 goto free_pages;
4286
Steve French35cf94a2019-09-07 01:09:49 -05004287 /*
4288 * For large reads, offload to different thread for better performance,
4289 * use more cores decrypting which can be expensive
4290 */
4291
Steve French10328c42019-09-09 13:30:15 -05004292 if ((server->min_offload) && (server->in_flight > 1) &&
Steve French563317e2019-09-08 23:22:02 -05004293 (server->pdu_size >= server->min_offload)) {
Steve French35cf94a2019-09-07 01:09:49 -05004294 dw = kmalloc(sizeof(struct smb2_decrypt_work), GFP_KERNEL);
4295 if (dw == NULL)
4296 goto non_offloaded_decrypt;
4297
4298 dw->buf = server->smallbuf;
4299 server->smallbuf = (char *)cifs_small_buf_get();
4300
4301 INIT_WORK(&dw->decrypt, smb2_decrypt_offload);
4302
4303 dw->npages = npages;
4304 dw->server = server;
4305 dw->ppages = pages;
4306 dw->len = len;
Steve Frencha08d8972019-10-26 16:00:44 -05004307 queue_work(decrypt_wq, &dw->decrypt);
Steve French35cf94a2019-09-07 01:09:49 -05004308 *num_mids = 0; /* worker thread takes care of finding mid */
4309 return -1;
4310 }
4311
4312non_offloaded_decrypt:
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10004313 rc = decrypt_raw_data(server, buf, server->vals->read_rsp_size,
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08004314 pages, npages, len);
4315 if (rc)
4316 goto free_pages;
4317
4318 *mid = smb2_find_mid(server, buf);
4319 if (*mid == NULL)
4320 cifs_dbg(FYI, "mid not found\n");
4321 else {
4322 cifs_dbg(FYI, "mid found\n");
4323 (*mid)->decrypted = true;
4324 rc = handle_read_data(server, *mid, buf,
4325 server->vals->read_rsp_size,
4326 pages, npages, len);
4327 }
4328
4329free_pages:
4330 for (i = i - 1; i >= 0; i--)
4331 put_page(pages[i]);
4332 kfree(pages);
4333 return rc;
4334discard_data:
Pavel Shilovsky350be252017-04-10 10:31:33 -07004335 cifs_discard_remaining_data(server);
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08004336 goto free_pages;
4337}
4338
4339static int
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004340receive_encrypted_standard(struct TCP_Server_Info *server,
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10004341 struct mid_q_entry **mids, char **bufs,
4342 int *num_mids)
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004343{
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10004344 int ret, length;
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004345 char *buf = server->smallbuf;
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10004346 struct smb2_sync_hdr *shdr;
Ronnie Sahlberg2e964672018-04-09 18:06:26 +10004347 unsigned int pdu_length = server->pdu_size;
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004348 unsigned int buf_size;
4349 struct mid_q_entry *mid_entry;
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10004350 int next_is_large;
4351 char *next_buffer = NULL;
4352
4353 *num_mids = 0;
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004354
4355 /* switch to large buffer if too big for a small one */
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10004356 if (pdu_length > MAX_CIFS_SMALL_BUFFER_SIZE) {
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004357 server->large_buf = true;
4358 memcpy(server->bigbuf, buf, server->total_read);
4359 buf = server->bigbuf;
4360 }
4361
4362 /* now read the rest */
4363 length = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1,
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10004364 pdu_length - HEADER_SIZE(server) + 1);
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004365 if (length < 0)
4366 return length;
4367 server->total_read += length;
4368
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10004369 buf_size = pdu_length - sizeof(struct smb2_transform_hdr);
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004370 length = decrypt_raw_data(server, buf, buf_size, NULL, 0, 0);
4371 if (length)
4372 return length;
4373
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10004374 next_is_large = server->large_buf;
Pavel Shilovsky3edeb4a2019-07-22 11:38:22 -07004375one_more:
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10004376 shdr = (struct smb2_sync_hdr *)buf;
4377 if (shdr->NextCommand) {
Pavel Shilovsky3edeb4a2019-07-22 11:38:22 -07004378 if (next_is_large)
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10004379 next_buffer = (char *)cifs_buf_get();
Pavel Shilovsky3edeb4a2019-07-22 11:38:22 -07004380 else
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10004381 next_buffer = (char *)cifs_small_buf_get();
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10004382 memcpy(next_buffer,
Pavel Shilovsky3edeb4a2019-07-22 11:38:22 -07004383 buf + le32_to_cpu(shdr->NextCommand),
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10004384 pdu_length - le32_to_cpu(shdr->NextCommand));
4385 }
4386
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004387 mid_entry = smb2_find_mid(server, buf);
4388 if (mid_entry == NULL)
4389 cifs_dbg(FYI, "mid not found\n");
4390 else {
4391 cifs_dbg(FYI, "mid found\n");
4392 mid_entry->decrypted = true;
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10004393 mid_entry->resp_buf_size = server->pdu_size;
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004394 }
4395
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10004396 if (*num_mids >= MAX_COMPOUND) {
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +10004397 cifs_server_dbg(VFS, "too many PDUs in compound\n");
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10004398 return -1;
4399 }
4400 bufs[*num_mids] = buf;
4401 mids[(*num_mids)++] = mid_entry;
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004402
4403 if (mid_entry && mid_entry->handle)
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10004404 ret = mid_entry->handle(server, mid_entry);
4405 else
4406 ret = cifs_handle_standard(server, mid_entry);
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004407
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10004408 if (ret == 0 && shdr->NextCommand) {
4409 pdu_length -= le32_to_cpu(shdr->NextCommand);
4410 server->large_buf = next_is_large;
4411 if (next_is_large)
Pavel Shilovsky3edeb4a2019-07-22 11:38:22 -07004412 server->bigbuf = buf = next_buffer;
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10004413 else
Pavel Shilovsky3edeb4a2019-07-22 11:38:22 -07004414 server->smallbuf = buf = next_buffer;
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10004415 goto one_more;
Pavel Shilovsky3edeb4a2019-07-22 11:38:22 -07004416 } else if (ret != 0) {
4417 /*
4418 * ret != 0 here means that we didn't get to handle_mid() thus
4419 * server->smallbuf and server->bigbuf are still valid. We need
4420 * to free next_buffer because it is not going to be used
4421 * anywhere.
4422 */
4423 if (next_is_large)
4424 free_rsp_buf(CIFS_LARGE_BUFFER, next_buffer);
4425 else
4426 free_rsp_buf(CIFS_SMALL_BUFFER, next_buffer);
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10004427 }
4428
4429 return ret;
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004430}
4431
4432static int
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10004433smb3_receive_transform(struct TCP_Server_Info *server,
4434 struct mid_q_entry **mids, char **bufs, int *num_mids)
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004435{
4436 char *buf = server->smallbuf;
Ronnie Sahlberg2e964672018-04-09 18:06:26 +10004437 unsigned int pdu_length = server->pdu_size;
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004438 struct smb2_transform_hdr *tr_hdr = (struct smb2_transform_hdr *)buf;
4439 unsigned int orig_len = le32_to_cpu(tr_hdr->OriginalMessageSize);
4440
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10004441 if (pdu_length < sizeof(struct smb2_transform_hdr) +
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004442 sizeof(struct smb2_sync_hdr)) {
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +10004443 cifs_server_dbg(VFS, "Transform message is too small (%u)\n",
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004444 pdu_length);
4445 cifs_reconnect(server);
4446 wake_up(&server->response_q);
4447 return -ECONNABORTED;
4448 }
4449
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10004450 if (pdu_length < orig_len + sizeof(struct smb2_transform_hdr)) {
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +10004451 cifs_server_dbg(VFS, "Transform message is broken\n");
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004452 cifs_reconnect(server);
4453 wake_up(&server->response_q);
4454 return -ECONNABORTED;
4455 }
4456
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10004457 /* TODO: add support for compounds containing READ. */
Paul Aurich6d2f84e2018-12-31 14:13:34 -08004458 if (pdu_length > CIFSMaxBufSize + MAX_HEADER_SIZE(server)) {
Steve French35cf94a2019-09-07 01:09:49 -05004459 return receive_encrypted_read(server, &mids[0], num_mids);
Paul Aurich6d2f84e2018-12-31 14:13:34 -08004460 }
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004461
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10004462 return receive_encrypted_standard(server, mids, bufs, num_mids);
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004463}
4464
4465int
4466smb3_handle_read_data(struct TCP_Server_Info *server, struct mid_q_entry *mid)
4467{
4468 char *buf = server->large_buf ? server->bigbuf : server->smallbuf;
4469
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10004470 return handle_read_data(server, mid, buf, server->pdu_size,
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004471 NULL, 0, 0);
4472}
4473
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +10004474static int
4475smb2_next_header(char *buf)
4476{
4477 struct smb2_sync_hdr *hdr = (struct smb2_sync_hdr *)buf;
4478 struct smb2_transform_hdr *t_hdr = (struct smb2_transform_hdr *)buf;
4479
4480 if (hdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM)
4481 return sizeof(struct smb2_transform_hdr) +
4482 le32_to_cpu(t_hdr->OriginalMessageSize);
4483
4484 return le32_to_cpu(hdr->NextCommand);
4485}
4486
Aurelien Aptelc847dcc2019-03-14 00:29:17 -05004487static int
4488smb2_make_node(unsigned int xid, struct inode *inode,
4489 struct dentry *dentry, struct cifs_tcon *tcon,
4490 char *full_path, umode_t mode, dev_t dev)
4491{
4492 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
4493 int rc = -EPERM;
4494 int create_options = CREATE_NOT_DIR | CREATE_OPTION_SPECIAL;
4495 FILE_ALL_INFO *buf = NULL;
4496 struct cifs_io_parms io_parms;
4497 __u32 oplock = 0;
4498 struct cifs_fid fid;
4499 struct cifs_open_parms oparms;
4500 unsigned int bytes_written;
4501 struct win_dev *pdev;
4502 struct kvec iov[2];
4503
4504 /*
4505 * Check if mounted with mount parm 'sfu' mount parm.
4506 * SFU emulation should work with all servers, but only
4507 * supports block and char device (no socket & fifo),
4508 * and was used by default in earlier versions of Windows
4509 */
4510 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL))
4511 goto out;
4512
4513 /*
4514 * TODO: Add ability to create instead via reparse point. Windows (e.g.
4515 * their current NFS server) uses this approach to expose special files
4516 * over SMB2/SMB3 and Samba will do this with SMB3.1.1 POSIX Extensions
4517 */
4518
4519 if (!S_ISCHR(mode) && !S_ISBLK(mode))
4520 goto out;
4521
4522 cifs_dbg(FYI, "sfu compat create special file\n");
4523
4524 buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL);
4525 if (buf == NULL) {
4526 rc = -ENOMEM;
4527 goto out;
4528 }
4529
4530 if (backup_cred(cifs_sb))
4531 create_options |= CREATE_OPEN_BACKUP_INTENT;
4532
4533 oparms.tcon = tcon;
4534 oparms.cifs_sb = cifs_sb;
4535 oparms.desired_access = GENERIC_WRITE;
4536 oparms.create_options = create_options;
4537 oparms.disposition = FILE_CREATE;
4538 oparms.path = full_path;
4539 oparms.fid = &fid;
4540 oparms.reconnect = false;
4541
4542 if (tcon->ses->server->oplocks)
4543 oplock = REQ_OPLOCK;
4544 else
4545 oplock = 0;
4546 rc = tcon->ses->server->ops->open(xid, &oparms, &oplock, buf);
4547 if (rc)
4548 goto out;
4549
4550 /*
4551 * BB Do not bother to decode buf since no local inode yet to put
4552 * timestamps in, but we can reuse it safely.
4553 */
4554
4555 pdev = (struct win_dev *)buf;
4556 io_parms.pid = current->tgid;
4557 io_parms.tcon = tcon;
4558 io_parms.offset = 0;
4559 io_parms.length = sizeof(struct win_dev);
4560 iov[1].iov_base = buf;
4561 iov[1].iov_len = sizeof(struct win_dev);
4562 if (S_ISCHR(mode)) {
4563 memcpy(pdev->type, "IntxCHR", 8);
4564 pdev->major = cpu_to_le64(MAJOR(dev));
4565 pdev->minor = cpu_to_le64(MINOR(dev));
4566 rc = tcon->ses->server->ops->sync_write(xid, &fid, &io_parms,
4567 &bytes_written, iov, 1);
4568 } else if (S_ISBLK(mode)) {
4569 memcpy(pdev->type, "IntxBLK", 8);
4570 pdev->major = cpu_to_le64(MAJOR(dev));
4571 pdev->minor = cpu_to_le64(MINOR(dev));
4572 rc = tcon->ses->server->ops->sync_write(xid, &fid, &io_parms,
4573 &bytes_written, iov, 1);
4574 }
4575 tcon->ses->server->ops->close(xid, tcon, &fid);
4576 d_drop(dentry);
4577
4578 /* FIXME: add code here to set EAs */
4579out:
4580 kfree(buf);
4581 return rc;
4582}
4583
4584
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04004585struct smb_version_operations smb20_operations = {
4586 .compare_fids = smb2_compare_fids,
4587 .setup_request = smb2_setup_request,
4588 .setup_async_request = smb2_setup_async_request,
4589 .check_receive = smb2_check_receive,
4590 .add_credits = smb2_add_credits,
4591 .set_credits = smb2_set_credits,
4592 .get_credits_field = smb2_get_credits_field,
4593 .get_credits = smb2_get_credits,
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04004594 .wait_mtu_credits = cifs_wait_mtu_credits,
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04004595 .get_next_mid = smb2_get_next_mid,
Pavel Shilovskyc781af72019-03-04 14:02:50 -08004596 .revert_current_mid = smb2_revert_current_mid,
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04004597 .read_data_offset = smb2_read_data_offset,
4598 .read_data_length = smb2_read_data_length,
4599 .map_error = map_smb2_to_linux_error,
4600 .find_mid = smb2_find_mid,
4601 .check_message = smb2_check_message,
4602 .dump_detail = smb2_dump_detail,
4603 .clear_stats = smb2_clear_stats,
4604 .print_stats = smb2_print_stats,
4605 .is_oplock_break = smb2_is_valid_oplock_break,
Sachin Prabhu38bd4902017-03-03 15:41:38 -08004606 .handle_cancelled_mid = smb2_handle_cancelled_mid,
Sachin Prabhuc11f1df2014-03-11 16:11:47 +00004607 .downgrade_oplock = smb2_downgrade_oplock,
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04004608 .need_neg = smb2_need_neg,
4609 .negotiate = smb2_negotiate,
4610 .negotiate_wsize = smb2_negotiate_wsize,
4611 .negotiate_rsize = smb2_negotiate_rsize,
4612 .sess_setup = SMB2_sess_setup,
4613 .logoff = SMB2_logoff,
4614 .tree_connect = SMB2_tcon,
4615 .tree_disconnect = SMB2_tdis,
Steve French34f62642013-10-09 02:07:00 -05004616 .qfs_tcon = smb2_qfs_tcon,
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04004617 .is_path_accessible = smb2_is_path_accessible,
4618 .can_echo = smb2_can_echo,
4619 .echo = SMB2_echo,
4620 .query_path_info = smb2_query_path_info,
4621 .get_srv_inum = smb2_get_srv_inum,
4622 .query_file_info = smb2_query_file_info,
4623 .set_path_size = smb2_set_path_size,
4624 .set_file_size = smb2_set_file_size,
4625 .set_file_info = smb2_set_file_info,
Steve French64a5cfa2013-10-14 15:31:32 -05004626 .set_compression = smb2_set_compression,
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04004627 .mkdir = smb2_mkdir,
4628 .mkdir_setinfo = smb2_mkdir_setinfo,
4629 .rmdir = smb2_rmdir,
4630 .unlink = smb2_unlink,
4631 .rename = smb2_rename_path,
4632 .create_hardlink = smb2_create_hardlink,
4633 .query_symlink = smb2_query_symlink,
Sachin Prabhu5b23c972016-07-11 16:53:20 +01004634 .query_mf_symlink = smb3_query_mf_symlink,
4635 .create_mf_symlink = smb3_create_mf_symlink,
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04004636 .open = smb2_open_file,
4637 .set_fid = smb2_set_fid,
4638 .close = smb2_close_file,
4639 .flush = smb2_flush_file,
4640 .async_readv = smb2_async_readv,
4641 .async_writev = smb2_async_writev,
4642 .sync_read = smb2_sync_read,
4643 .sync_write = smb2_sync_write,
4644 .query_dir_first = smb2_query_dir_first,
4645 .query_dir_next = smb2_query_dir_next,
4646 .close_dir = smb2_close_dir,
4647 .calc_smb_size = smb2_calc_size,
4648 .is_status_pending = smb2_is_status_pending,
Pavel Shilovsky511c54a2017-07-08 14:32:00 -07004649 .is_session_expired = smb2_is_session_expired,
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04004650 .oplock_response = smb2_oplock_response,
4651 .queryfs = smb2_queryfs,
4652 .mand_lock = smb2_mand_lock,
4653 .mand_unlock_range = smb2_unlock_range,
4654 .push_mand_locks = smb2_push_mandatory_locks,
4655 .get_lease_key = smb2_get_lease_key,
4656 .set_lease_key = smb2_set_lease_key,
4657 .new_lease_key = smb2_new_lease_key,
4658 .calc_signature = smb2_calc_signature,
4659 .is_read_op = smb2_is_read_op,
4660 .set_oplock_level = smb2_set_oplock_level,
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04004661 .create_lease_buf = smb2_create_lease_buf,
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04004662 .parse_lease_buf = smb2_parse_lease_buf,
Sachin Prabhu312bbc52017-04-04 02:12:04 -05004663 .copychunk_range = smb2_copychunk_range,
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04004664 .wp_retry_size = smb2_wp_retry_size,
Pavel Shilovsky52755802014-08-18 20:49:57 +04004665 .dir_needs_close = smb2_dir_needs_close,
Aurelien Aptel9d496402017-02-13 16:16:49 +01004666 .get_dfs_refer = smb2_get_dfs_refer,
Sachin Prabhuef65aae2017-01-18 15:35:57 +05304667 .select_sectype = smb2_select_sectype,
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10004668#ifdef CONFIG_CIFS_XATTR
4669 .query_all_EAs = smb2_query_eas,
Ronnie Sahlberg55175542017-08-24 11:24:56 +10004670 .set_EA = smb2_set_ea,
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10004671#endif /* CIFS_XATTR */
Shirish Pargaonkar2f1afe22017-06-22 22:52:05 -05004672 .get_acl = get_smb2_acl,
4673 .get_acl_by_fid = get_smb2_acl_by_fid,
Shirish Pargaonkar366ed842017-06-28 22:37:32 -05004674 .set_acl = set_smb2_acl,
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +10004675 .next_header = smb2_next_header,
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05004676 .ioctl_query_info = smb2_ioctl_query_info,
Aurelien Aptelc847dcc2019-03-14 00:29:17 -05004677 .make_node = smb2_make_node,
Ronnie Sahlberg2f3ebab2019-04-25 16:45:29 +10004678 .fiemap = smb3_fiemap,
Ronnie Sahlbergdece44e2019-05-15 07:17:02 +10004679 .llseek = smb3_llseek,
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04004680};
4681
Steve French1080ef72011-02-24 18:07:19 +00004682struct smb_version_operations smb21_operations = {
Pavel Shilovsky027e8ee2012-09-19 06:22:43 -07004683 .compare_fids = smb2_compare_fids,
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +04004684 .setup_request = smb2_setup_request,
Pavel Shilovskyc95b8ee2012-07-11 14:45:28 +04004685 .setup_async_request = smb2_setup_async_request,
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +04004686 .check_receive = smb2_check_receive,
Pavel Shilovsky28ea5292012-05-23 16:18:00 +04004687 .add_credits = smb2_add_credits,
4688 .set_credits = smb2_set_credits,
4689 .get_credits_field = smb2_get_credits_field,
4690 .get_credits = smb2_get_credits,
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04004691 .wait_mtu_credits = smb2_wait_mtu_credits,
Pavel Shilovsky9a1c67e2019-01-23 18:15:52 -08004692 .adjust_credits = smb2_adjust_credits,
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +04004693 .get_next_mid = smb2_get_next_mid,
Pavel Shilovskyc781af72019-03-04 14:02:50 -08004694 .revert_current_mid = smb2_revert_current_mid,
Pavel Shilovsky09a47072012-09-18 16:20:29 -07004695 .read_data_offset = smb2_read_data_offset,
4696 .read_data_length = smb2_read_data_length,
4697 .map_error = map_smb2_to_linux_error,
Pavel Shilovsky093b2bd2011-06-08 15:51:07 +04004698 .find_mid = smb2_find_mid,
4699 .check_message = smb2_check_message,
4700 .dump_detail = smb2_dump_detail,
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04004701 .clear_stats = smb2_clear_stats,
4702 .print_stats = smb2_print_stats,
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07004703 .is_oplock_break = smb2_is_valid_oplock_break,
Sachin Prabhu38bd4902017-03-03 15:41:38 -08004704 .handle_cancelled_mid = smb2_handle_cancelled_mid,
Pavel Shilovsky9bd45402019-10-29 16:51:19 -07004705 .downgrade_oplock = smb2_downgrade_oplock,
Pavel Shilovskyec2e4522011-12-27 16:12:43 +04004706 .need_neg = smb2_need_neg,
4707 .negotiate = smb2_negotiate,
Pavel Shilovsky3a3bab52012-09-18 16:20:28 -07004708 .negotiate_wsize = smb2_negotiate_wsize,
4709 .negotiate_rsize = smb2_negotiate_rsize,
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04004710 .sess_setup = SMB2_sess_setup,
4711 .logoff = SMB2_logoff,
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04004712 .tree_connect = SMB2_tcon,
4713 .tree_disconnect = SMB2_tdis,
Steve French34f62642013-10-09 02:07:00 -05004714 .qfs_tcon = smb2_qfs_tcon,
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04004715 .is_path_accessible = smb2_is_path_accessible,
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04004716 .can_echo = smb2_can_echo,
4717 .echo = SMB2_echo,
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04004718 .query_path_info = smb2_query_path_info,
4719 .get_srv_inum = smb2_get_srv_inum,
Pavel Shilovskyb7546bc2012-09-18 16:20:27 -07004720 .query_file_info = smb2_query_file_info,
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07004721 .set_path_size = smb2_set_path_size,
4722 .set_file_size = smb2_set_file_size,
Pavel Shilovsky1feeaac2012-09-18 16:20:32 -07004723 .set_file_info = smb2_set_file_info,
Steve French64a5cfa2013-10-14 15:31:32 -05004724 .set_compression = smb2_set_compression,
Pavel Shilovskya0e73182011-07-19 12:56:37 +04004725 .mkdir = smb2_mkdir,
4726 .mkdir_setinfo = smb2_mkdir_setinfo,
Pavel Shilovsky1a500f02012-07-10 16:14:38 +04004727 .rmdir = smb2_rmdir,
Pavel Shilovskycbe6f432012-09-18 16:20:25 -07004728 .unlink = smb2_unlink,
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07004729 .rename = smb2_rename_path,
Pavel Shilovsky568798c2012-09-18 16:20:31 -07004730 .create_hardlink = smb2_create_hardlink,
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04004731 .query_symlink = smb2_query_symlink,
Steve Frenchc22870e2014-09-16 07:18:19 -05004732 .query_mf_symlink = smb3_query_mf_symlink,
Steve French5ab97572014-09-15 04:49:28 -05004733 .create_mf_symlink = smb3_create_mf_symlink,
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07004734 .open = smb2_open_file,
4735 .set_fid = smb2_set_fid,
4736 .close = smb2_close_file,
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07004737 .flush = smb2_flush_file,
Pavel Shilovsky09a47072012-09-18 16:20:29 -07004738 .async_readv = smb2_async_readv,
Pavel Shilovsky33319142012-09-18 16:20:29 -07004739 .async_writev = smb2_async_writev,
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07004740 .sync_read = smb2_sync_read,
Pavel Shilovsky009d3442012-09-18 16:20:30 -07004741 .sync_write = smb2_sync_write,
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07004742 .query_dir_first = smb2_query_dir_first,
4743 .query_dir_next = smb2_query_dir_next,
4744 .close_dir = smb2_close_dir,
4745 .calc_smb_size = smb2_calc_size,
Pavel Shilovsky2e44b282012-09-18 16:20:33 -07004746 .is_status_pending = smb2_is_status_pending,
Pavel Shilovsky511c54a2017-07-08 14:32:00 -07004747 .is_session_expired = smb2_is_session_expired,
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07004748 .oplock_response = smb2_oplock_response,
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07004749 .queryfs = smb2_queryfs,
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07004750 .mand_lock = smb2_mand_lock,
4751 .mand_unlock_range = smb2_unlock_range,
Pavel Shilovskyb1407992012-09-19 06:22:44 -07004752 .push_mand_locks = smb2_push_mandatory_locks,
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07004753 .get_lease_key = smb2_get_lease_key,
4754 .set_lease_key = smb2_set_lease_key,
4755 .new_lease_key = smb2_new_lease_key,
Steve French38107d42012-12-08 22:08:06 -06004756 .calc_signature = smb2_calc_signature,
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04004757 .is_read_op = smb21_is_read_op,
4758 .set_oplock_level = smb21_set_oplock_level,
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04004759 .create_lease_buf = smb2_create_lease_buf,
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04004760 .parse_lease_buf = smb2_parse_lease_buf,
Sachin Prabhu312bbc52017-04-04 02:12:04 -05004761 .copychunk_range = smb2_copychunk_range,
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04004762 .wp_retry_size = smb2_wp_retry_size,
Pavel Shilovsky52755802014-08-18 20:49:57 +04004763 .dir_needs_close = smb2_dir_needs_close,
Steve French834170c2016-09-30 21:14:26 -05004764 .enum_snapshots = smb3_enum_snapshots,
Aurelien Aptel9d496402017-02-13 16:16:49 +01004765 .get_dfs_refer = smb2_get_dfs_refer,
Sachin Prabhuef65aae2017-01-18 15:35:57 +05304766 .select_sectype = smb2_select_sectype,
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10004767#ifdef CONFIG_CIFS_XATTR
4768 .query_all_EAs = smb2_query_eas,
Ronnie Sahlberg55175542017-08-24 11:24:56 +10004769 .set_EA = smb2_set_ea,
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10004770#endif /* CIFS_XATTR */
Shirish Pargaonkar2f1afe22017-06-22 22:52:05 -05004771 .get_acl = get_smb2_acl,
4772 .get_acl_by_fid = get_smb2_acl_by_fid,
Shirish Pargaonkar366ed842017-06-28 22:37:32 -05004773 .set_acl = set_smb2_acl,
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +10004774 .next_header = smb2_next_header,
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05004775 .ioctl_query_info = smb2_ioctl_query_info,
Aurelien Aptelc847dcc2019-03-14 00:29:17 -05004776 .make_node = smb2_make_node,
Ronnie Sahlberg2f3ebab2019-04-25 16:45:29 +10004777 .fiemap = smb3_fiemap,
Ronnie Sahlbergdece44e2019-05-15 07:17:02 +10004778 .llseek = smb3_llseek,
Steve French38107d42012-12-08 22:08:06 -06004779};
4780
Steve French38107d42012-12-08 22:08:06 -06004781struct smb_version_operations smb30_operations = {
4782 .compare_fids = smb2_compare_fids,
4783 .setup_request = smb2_setup_request,
4784 .setup_async_request = smb2_setup_async_request,
4785 .check_receive = smb2_check_receive,
4786 .add_credits = smb2_add_credits,
4787 .set_credits = smb2_set_credits,
4788 .get_credits_field = smb2_get_credits_field,
4789 .get_credits = smb2_get_credits,
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04004790 .wait_mtu_credits = smb2_wait_mtu_credits,
Pavel Shilovsky9a1c67e2019-01-23 18:15:52 -08004791 .adjust_credits = smb2_adjust_credits,
Steve French38107d42012-12-08 22:08:06 -06004792 .get_next_mid = smb2_get_next_mid,
Pavel Shilovskyc781af72019-03-04 14:02:50 -08004793 .revert_current_mid = smb2_revert_current_mid,
Steve French38107d42012-12-08 22:08:06 -06004794 .read_data_offset = smb2_read_data_offset,
4795 .read_data_length = smb2_read_data_length,
4796 .map_error = map_smb2_to_linux_error,
4797 .find_mid = smb2_find_mid,
4798 .check_message = smb2_check_message,
4799 .dump_detail = smb2_dump_detail,
4800 .clear_stats = smb2_clear_stats,
4801 .print_stats = smb2_print_stats,
Steve French769ee6a2013-06-19 14:15:30 -05004802 .dump_share_caps = smb2_dump_share_caps,
Steve French38107d42012-12-08 22:08:06 -06004803 .is_oplock_break = smb2_is_valid_oplock_break,
Sachin Prabhu38bd4902017-03-03 15:41:38 -08004804 .handle_cancelled_mid = smb2_handle_cancelled_mid,
Pavel Shilovsky9bd45402019-10-29 16:51:19 -07004805 .downgrade_oplock = smb3_downgrade_oplock,
Steve French38107d42012-12-08 22:08:06 -06004806 .need_neg = smb2_need_neg,
4807 .negotiate = smb2_negotiate,
Steve French3d621232018-09-25 15:33:47 -05004808 .negotiate_wsize = smb3_negotiate_wsize,
4809 .negotiate_rsize = smb3_negotiate_rsize,
Steve French38107d42012-12-08 22:08:06 -06004810 .sess_setup = SMB2_sess_setup,
4811 .logoff = SMB2_logoff,
4812 .tree_connect = SMB2_tcon,
4813 .tree_disconnect = SMB2_tdis,
Steven Frenchaf6a12e2013-10-09 20:55:53 -05004814 .qfs_tcon = smb3_qfs_tcon,
Steve French38107d42012-12-08 22:08:06 -06004815 .is_path_accessible = smb2_is_path_accessible,
4816 .can_echo = smb2_can_echo,
4817 .echo = SMB2_echo,
4818 .query_path_info = smb2_query_path_info,
4819 .get_srv_inum = smb2_get_srv_inum,
4820 .query_file_info = smb2_query_file_info,
4821 .set_path_size = smb2_set_path_size,
4822 .set_file_size = smb2_set_file_size,
4823 .set_file_info = smb2_set_file_info,
Steve French64a5cfa2013-10-14 15:31:32 -05004824 .set_compression = smb2_set_compression,
Steve French38107d42012-12-08 22:08:06 -06004825 .mkdir = smb2_mkdir,
4826 .mkdir_setinfo = smb2_mkdir_setinfo,
4827 .rmdir = smb2_rmdir,
4828 .unlink = smb2_unlink,
4829 .rename = smb2_rename_path,
4830 .create_hardlink = smb2_create_hardlink,
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04004831 .query_symlink = smb2_query_symlink,
Steve Frenchc22870e2014-09-16 07:18:19 -05004832 .query_mf_symlink = smb3_query_mf_symlink,
Steve French5ab97572014-09-15 04:49:28 -05004833 .create_mf_symlink = smb3_create_mf_symlink,
Steve French38107d42012-12-08 22:08:06 -06004834 .open = smb2_open_file,
4835 .set_fid = smb2_set_fid,
4836 .close = smb2_close_file,
Steve French43f8a6a2019-12-02 21:46:54 -06004837 .close_getattr = smb2_close_getattr,
Steve French38107d42012-12-08 22:08:06 -06004838 .flush = smb2_flush_file,
4839 .async_readv = smb2_async_readv,
4840 .async_writev = smb2_async_writev,
4841 .sync_read = smb2_sync_read,
4842 .sync_write = smb2_sync_write,
4843 .query_dir_first = smb2_query_dir_first,
4844 .query_dir_next = smb2_query_dir_next,
4845 .close_dir = smb2_close_dir,
4846 .calc_smb_size = smb2_calc_size,
4847 .is_status_pending = smb2_is_status_pending,
Pavel Shilovsky511c54a2017-07-08 14:32:00 -07004848 .is_session_expired = smb2_is_session_expired,
Steve French38107d42012-12-08 22:08:06 -06004849 .oplock_response = smb2_oplock_response,
4850 .queryfs = smb2_queryfs,
4851 .mand_lock = smb2_mand_lock,
4852 .mand_unlock_range = smb2_unlock_range,
4853 .push_mand_locks = smb2_push_mandatory_locks,
4854 .get_lease_key = smb2_get_lease_key,
4855 .set_lease_key = smb2_set_lease_key,
4856 .new_lease_key = smb2_new_lease_key,
Steve French373512e2015-12-18 13:05:30 -06004857 .generate_signingkey = generate_smb30signingkey,
Steve French38107d42012-12-08 22:08:06 -06004858 .calc_signature = smb3_calc_signature,
Steve Frenchb3152e22015-06-24 03:17:02 -05004859 .set_integrity = smb3_set_integrity,
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04004860 .is_read_op = smb21_is_read_op,
Pavel Shilovsky42873b02013-09-05 21:30:16 +04004861 .set_oplock_level = smb3_set_oplock_level,
Pavel Shilovskyf0473902013-09-04 13:44:05 +04004862 .create_lease_buf = smb3_create_lease_buf,
4863 .parse_lease_buf = smb3_parse_lease_buf,
Sachin Prabhu312bbc52017-04-04 02:12:04 -05004864 .copychunk_range = smb2_copychunk_range,
Steve Frenchca9e7a12015-10-01 21:40:10 -05004865 .duplicate_extents = smb2_duplicate_extents,
Steve Frenchff1c0382013-11-19 23:44:46 -06004866 .validate_negotiate = smb3_validate_negotiate,
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04004867 .wp_retry_size = smb2_wp_retry_size,
Pavel Shilovsky52755802014-08-18 20:49:57 +04004868 .dir_needs_close = smb2_dir_needs_close,
Steve French31742c52014-08-17 08:38:47 -05004869 .fallocate = smb3_fallocate,
Steve French834170c2016-09-30 21:14:26 -05004870 .enum_snapshots = smb3_enum_snapshots,
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07004871 .init_transform_rq = smb3_init_transform_rq,
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004872 .is_transform_hdr = smb3_is_transform_hdr,
4873 .receive_transform = smb3_receive_transform,
Aurelien Aptel9d496402017-02-13 16:16:49 +01004874 .get_dfs_refer = smb2_get_dfs_refer,
Sachin Prabhuef65aae2017-01-18 15:35:57 +05304875 .select_sectype = smb2_select_sectype,
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10004876#ifdef CONFIG_CIFS_XATTR
4877 .query_all_EAs = smb2_query_eas,
Ronnie Sahlberg55175542017-08-24 11:24:56 +10004878 .set_EA = smb2_set_ea,
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10004879#endif /* CIFS_XATTR */
Shirish Pargaonkar2f1afe22017-06-22 22:52:05 -05004880 .get_acl = get_smb2_acl,
4881 .get_acl_by_fid = get_smb2_acl_by_fid,
Shirish Pargaonkar366ed842017-06-28 22:37:32 -05004882 .set_acl = set_smb2_acl,
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +10004883 .next_header = smb2_next_header,
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05004884 .ioctl_query_info = smb2_ioctl_query_info,
Aurelien Aptelc847dcc2019-03-14 00:29:17 -05004885 .make_node = smb2_make_node,
Ronnie Sahlberg2f3ebab2019-04-25 16:45:29 +10004886 .fiemap = smb3_fiemap,
Ronnie Sahlbergdece44e2019-05-15 07:17:02 +10004887 .llseek = smb3_llseek,
Steve French1080ef72011-02-24 18:07:19 +00004888};
4889
Steve Frenchaab18932015-06-23 23:37:11 -05004890struct smb_version_operations smb311_operations = {
4891 .compare_fids = smb2_compare_fids,
4892 .setup_request = smb2_setup_request,
4893 .setup_async_request = smb2_setup_async_request,
4894 .check_receive = smb2_check_receive,
4895 .add_credits = smb2_add_credits,
4896 .set_credits = smb2_set_credits,
4897 .get_credits_field = smb2_get_credits_field,
4898 .get_credits = smb2_get_credits,
4899 .wait_mtu_credits = smb2_wait_mtu_credits,
Pavel Shilovsky9a1c67e2019-01-23 18:15:52 -08004900 .adjust_credits = smb2_adjust_credits,
Steve Frenchaab18932015-06-23 23:37:11 -05004901 .get_next_mid = smb2_get_next_mid,
Pavel Shilovskyc781af72019-03-04 14:02:50 -08004902 .revert_current_mid = smb2_revert_current_mid,
Steve Frenchaab18932015-06-23 23:37:11 -05004903 .read_data_offset = smb2_read_data_offset,
4904 .read_data_length = smb2_read_data_length,
4905 .map_error = map_smb2_to_linux_error,
4906 .find_mid = smb2_find_mid,
4907 .check_message = smb2_check_message,
4908 .dump_detail = smb2_dump_detail,
4909 .clear_stats = smb2_clear_stats,
4910 .print_stats = smb2_print_stats,
4911 .dump_share_caps = smb2_dump_share_caps,
4912 .is_oplock_break = smb2_is_valid_oplock_break,
Sachin Prabhu38bd4902017-03-03 15:41:38 -08004913 .handle_cancelled_mid = smb2_handle_cancelled_mid,
Pavel Shilovsky9bd45402019-10-29 16:51:19 -07004914 .downgrade_oplock = smb3_downgrade_oplock,
Steve Frenchaab18932015-06-23 23:37:11 -05004915 .need_neg = smb2_need_neg,
4916 .negotiate = smb2_negotiate,
Steve French3d621232018-09-25 15:33:47 -05004917 .negotiate_wsize = smb3_negotiate_wsize,
4918 .negotiate_rsize = smb3_negotiate_rsize,
Steve Frenchaab18932015-06-23 23:37:11 -05004919 .sess_setup = SMB2_sess_setup,
4920 .logoff = SMB2_logoff,
4921 .tree_connect = SMB2_tcon,
4922 .tree_disconnect = SMB2_tdis,
4923 .qfs_tcon = smb3_qfs_tcon,
4924 .is_path_accessible = smb2_is_path_accessible,
4925 .can_echo = smb2_can_echo,
4926 .echo = SMB2_echo,
4927 .query_path_info = smb2_query_path_info,
4928 .get_srv_inum = smb2_get_srv_inum,
4929 .query_file_info = smb2_query_file_info,
4930 .set_path_size = smb2_set_path_size,
4931 .set_file_size = smb2_set_file_size,
4932 .set_file_info = smb2_set_file_info,
4933 .set_compression = smb2_set_compression,
4934 .mkdir = smb2_mkdir,
4935 .mkdir_setinfo = smb2_mkdir_setinfo,
Steve Frenchbea851b2018-06-14 21:56:32 -05004936 .posix_mkdir = smb311_posix_mkdir,
Steve Frenchaab18932015-06-23 23:37:11 -05004937 .rmdir = smb2_rmdir,
4938 .unlink = smb2_unlink,
4939 .rename = smb2_rename_path,
4940 .create_hardlink = smb2_create_hardlink,
4941 .query_symlink = smb2_query_symlink,
4942 .query_mf_symlink = smb3_query_mf_symlink,
4943 .create_mf_symlink = smb3_create_mf_symlink,
4944 .open = smb2_open_file,
4945 .set_fid = smb2_set_fid,
4946 .close = smb2_close_file,
Steve French43f8a6a2019-12-02 21:46:54 -06004947 .close_getattr = smb2_close_getattr,
Steve Frenchaab18932015-06-23 23:37:11 -05004948 .flush = smb2_flush_file,
4949 .async_readv = smb2_async_readv,
4950 .async_writev = smb2_async_writev,
4951 .sync_read = smb2_sync_read,
4952 .sync_write = smb2_sync_write,
4953 .query_dir_first = smb2_query_dir_first,
4954 .query_dir_next = smb2_query_dir_next,
4955 .close_dir = smb2_close_dir,
4956 .calc_smb_size = smb2_calc_size,
4957 .is_status_pending = smb2_is_status_pending,
Pavel Shilovsky511c54a2017-07-08 14:32:00 -07004958 .is_session_expired = smb2_is_session_expired,
Steve Frenchaab18932015-06-23 23:37:11 -05004959 .oplock_response = smb2_oplock_response,
Steve French2d304212018-06-24 23:28:12 -05004960 .queryfs = smb311_queryfs,
Steve Frenchaab18932015-06-23 23:37:11 -05004961 .mand_lock = smb2_mand_lock,
4962 .mand_unlock_range = smb2_unlock_range,
4963 .push_mand_locks = smb2_push_mandatory_locks,
4964 .get_lease_key = smb2_get_lease_key,
4965 .set_lease_key = smb2_set_lease_key,
4966 .new_lease_key = smb2_new_lease_key,
Steve French373512e2015-12-18 13:05:30 -06004967 .generate_signingkey = generate_smb311signingkey,
Steve Frenchaab18932015-06-23 23:37:11 -05004968 .calc_signature = smb3_calc_signature,
Steve Frenchb3152e22015-06-24 03:17:02 -05004969 .set_integrity = smb3_set_integrity,
Steve Frenchaab18932015-06-23 23:37:11 -05004970 .is_read_op = smb21_is_read_op,
4971 .set_oplock_level = smb3_set_oplock_level,
4972 .create_lease_buf = smb3_create_lease_buf,
4973 .parse_lease_buf = smb3_parse_lease_buf,
Sachin Prabhu312bbc52017-04-04 02:12:04 -05004974 .copychunk_range = smb2_copychunk_range,
Steve French02b16662015-06-27 21:18:36 -07004975 .duplicate_extents = smb2_duplicate_extents,
Steve Frenchaab18932015-06-23 23:37:11 -05004976/* .validate_negotiate = smb3_validate_negotiate, */ /* not used in 3.11 */
4977 .wp_retry_size = smb2_wp_retry_size,
4978 .dir_needs_close = smb2_dir_needs_close,
4979 .fallocate = smb3_fallocate,
Steve French834170c2016-09-30 21:14:26 -05004980 .enum_snapshots = smb3_enum_snapshots,
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07004981 .init_transform_rq = smb3_init_transform_rq,
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004982 .is_transform_hdr = smb3_is_transform_hdr,
4983 .receive_transform = smb3_receive_transform,
Aurelien Aptel9d496402017-02-13 16:16:49 +01004984 .get_dfs_refer = smb2_get_dfs_refer,
Sachin Prabhuef65aae2017-01-18 15:35:57 +05304985 .select_sectype = smb2_select_sectype,
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10004986#ifdef CONFIG_CIFS_XATTR
4987 .query_all_EAs = smb2_query_eas,
Ronnie Sahlberg55175542017-08-24 11:24:56 +10004988 .set_EA = smb2_set_ea,
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10004989#endif /* CIFS_XATTR */
Ronnie Sahlbergc1777df2018-08-10 11:03:55 +10004990 .get_acl = get_smb2_acl,
4991 .get_acl_by_fid = get_smb2_acl_by_fid,
4992 .set_acl = set_smb2_acl,
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +10004993 .next_header = smb2_next_header,
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05004994 .ioctl_query_info = smb2_ioctl_query_info,
Aurelien Aptelc847dcc2019-03-14 00:29:17 -05004995 .make_node = smb2_make_node,
Ronnie Sahlberg2f3ebab2019-04-25 16:45:29 +10004996 .fiemap = smb3_fiemap,
Ronnie Sahlbergdece44e2019-05-15 07:17:02 +10004997 .llseek = smb3_llseek,
Steve Frenchaab18932015-06-23 23:37:11 -05004998};
Steve Frenchaab18932015-06-23 23:37:11 -05004999
Steve Frenchdd446b12012-11-28 23:21:06 -06005000struct smb_version_values smb20_values = {
5001 .version_string = SMB20_VERSION_STRING,
5002 .protocol_id = SMB20_PROT_ID,
5003 .req_capabilities = 0, /* MBZ */
5004 .large_lock_type = 0,
5005 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
5006 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
5007 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10005008 .header_size = sizeof(struct smb2_sync_hdr),
5009 .header_preamble_size = 0,
Steve Frenchdd446b12012-11-28 23:21:06 -06005010 .max_header_size = MAX_SMB2_HDR_SIZE,
5011 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
5012 .lock_cmd = SMB2_LOCK,
5013 .cap_unix = 0,
5014 .cap_nt_find = SMB2_NT_FIND,
5015 .cap_large_files = SMB2_LARGE_FILES,
Jeff Layton502858822013-06-27 12:45:00 -04005016 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5017 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04005018 .create_lease_size = sizeof(struct create_lease),
Steve Frenchdd446b12012-11-28 23:21:06 -06005019};
5020
Steve French1080ef72011-02-24 18:07:19 +00005021struct smb_version_values smb21_values = {
5022 .version_string = SMB21_VERSION_STRING,
Steve Frenche4aa25e2012-10-01 12:26:22 -05005023 .protocol_id = SMB21_PROT_ID,
5024 .req_capabilities = 0, /* MBZ on negotiate req until SMB3 dialect */
5025 .large_lock_type = 0,
5026 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
5027 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
5028 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10005029 .header_size = sizeof(struct smb2_sync_hdr),
5030 .header_preamble_size = 0,
Steve Frenche4aa25e2012-10-01 12:26:22 -05005031 .max_header_size = MAX_SMB2_HDR_SIZE,
5032 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
5033 .lock_cmd = SMB2_LOCK,
5034 .cap_unix = 0,
5035 .cap_nt_find = SMB2_NT_FIND,
5036 .cap_large_files = SMB2_LARGE_FILES,
Jeff Layton502858822013-06-27 12:45:00 -04005037 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5038 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04005039 .create_lease_size = sizeof(struct create_lease),
Steve Frenche4aa25e2012-10-01 12:26:22 -05005040};
5041
Steve French9764c022017-09-17 10:41:35 -05005042struct smb_version_values smb3any_values = {
5043 .version_string = SMB3ANY_VERSION_STRING,
5044 .protocol_id = SMB302_PROT_ID, /* doesn't matter, send protocol array */
Steve Frenchf8015682018-08-31 15:12:10 -05005045 .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 -05005046 .large_lock_type = 0,
5047 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
5048 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
5049 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10005050 .header_size = sizeof(struct smb2_sync_hdr),
5051 .header_preamble_size = 0,
Steve French9764c022017-09-17 10:41:35 -05005052 .max_header_size = MAX_SMB2_HDR_SIZE,
5053 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
5054 .lock_cmd = SMB2_LOCK,
5055 .cap_unix = 0,
5056 .cap_nt_find = SMB2_NT_FIND,
5057 .cap_large_files = SMB2_LARGE_FILES,
5058 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5059 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5060 .create_lease_size = sizeof(struct create_lease_v2),
5061};
5062
5063struct smb_version_values smbdefault_values = {
5064 .version_string = SMBDEFAULT_VERSION_STRING,
5065 .protocol_id = SMB302_PROT_ID, /* doesn't matter, send protocol array */
Steve Frenchf8015682018-08-31 15:12:10 -05005066 .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 -05005067 .large_lock_type = 0,
5068 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
5069 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
5070 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10005071 .header_size = sizeof(struct smb2_sync_hdr),
5072 .header_preamble_size = 0,
Steve French9764c022017-09-17 10:41:35 -05005073 .max_header_size = MAX_SMB2_HDR_SIZE,
5074 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
5075 .lock_cmd = SMB2_LOCK,
5076 .cap_unix = 0,
5077 .cap_nt_find = SMB2_NT_FIND,
5078 .cap_large_files = SMB2_LARGE_FILES,
5079 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5080 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5081 .create_lease_size = sizeof(struct create_lease_v2),
5082};
5083
Steve Frenche4aa25e2012-10-01 12:26:22 -05005084struct smb_version_values smb30_values = {
5085 .version_string = SMB30_VERSION_STRING,
5086 .protocol_id = SMB30_PROT_ID,
Steve Frenchf8015682018-08-31 15:12:10 -05005087 .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 -07005088 .large_lock_type = 0,
5089 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
5090 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
5091 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10005092 .header_size = sizeof(struct smb2_sync_hdr),
5093 .header_preamble_size = 0,
Pavel Shilovsky093b2bd2011-06-08 15:51:07 +04005094 .max_header_size = MAX_SMB2_HDR_SIZE,
Pavel Shilovsky09a47072012-09-18 16:20:29 -07005095 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +04005096 .lock_cmd = SMB2_LOCK,
Pavel Shilovsky29e20f92012-07-13 13:58:14 +04005097 .cap_unix = 0,
5098 .cap_nt_find = SMB2_NT_FIND,
5099 .cap_large_files = SMB2_LARGE_FILES,
Jeff Layton502858822013-06-27 12:45:00 -04005100 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5101 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
Pavel Shilovskyf0473902013-09-04 13:44:05 +04005102 .create_lease_size = sizeof(struct create_lease_v2),
Steve French1080ef72011-02-24 18:07:19 +00005103};
Steve French20b6d8b2013-06-12 22:48:41 -05005104
5105struct smb_version_values smb302_values = {
5106 .version_string = SMB302_VERSION_STRING,
5107 .protocol_id = SMB302_PROT_ID,
Steve Frenchf8015682018-08-31 15:12:10 -05005108 .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 -05005109 .large_lock_type = 0,
5110 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
5111 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
5112 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10005113 .header_size = sizeof(struct smb2_sync_hdr),
5114 .header_preamble_size = 0,
Steve French20b6d8b2013-06-12 22:48:41 -05005115 .max_header_size = MAX_SMB2_HDR_SIZE,
5116 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
5117 .lock_cmd = SMB2_LOCK,
5118 .cap_unix = 0,
5119 .cap_nt_find = SMB2_NT_FIND,
5120 .cap_large_files = SMB2_LARGE_FILES,
Jeff Layton502858822013-06-27 12:45:00 -04005121 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5122 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
Pavel Shilovskyf0473902013-09-04 13:44:05 +04005123 .create_lease_size = sizeof(struct create_lease_v2),
Steve French20b6d8b2013-06-12 22:48:41 -05005124};
Steve French5f7fbf72014-12-17 22:52:58 -06005125
Steve French5f7fbf72014-12-17 22:52:58 -06005126struct smb_version_values smb311_values = {
5127 .version_string = SMB311_VERSION_STRING,
5128 .protocol_id = SMB311_PROT_ID,
Steve Frenchf8015682018-08-31 15:12:10 -05005129 .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 -06005130 .large_lock_type = 0,
5131 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
5132 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
5133 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10005134 .header_size = sizeof(struct smb2_sync_hdr),
5135 .header_preamble_size = 0,
Steve French5f7fbf72014-12-17 22:52:58 -06005136 .max_header_size = MAX_SMB2_HDR_SIZE,
5137 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
5138 .lock_cmd = SMB2_LOCK,
5139 .cap_unix = 0,
5140 .cap_nt_find = SMB2_NT_FIND,
5141 .cap_large_files = SMB2_LARGE_FILES,
5142 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5143 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5144 .create_lease_size = sizeof(struct create_lease_v2),
5145};