blob: 3d914d7d0d110013864bc568f914940679743fd3 [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>
Christoph Hellwig10c5db22020-05-23 09:30:11 +020015#include <linux/fiemap.h>
Ronnie Sahlberg8bd0d702020-01-17 11:45:02 +100016#include "cifsfs.h"
Steve French1080ef72011-02-24 18:07:19 +000017#include "cifsglob.h"
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +040018#include "smb2pdu.h"
19#include "smb2proto.h"
Pavel Shilovsky28ea5292012-05-23 16:18:00 +040020#include "cifsproto.h"
21#include "cifs_debug.h"
Pavel Shilovskyb42bf882013-08-14 19:25:21 +040022#include "cifs_unicode.h"
Pavel Shilovsky2e44b282012-09-18 16:20:33 -070023#include "smb2status.h"
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -070024#include "smb2glob.h"
Steve French834170c2016-09-30 21:14:26 -050025#include "cifs_ioctl.h"
Long Li09902f82017-11-22 17:38:39 -070026#include "smbdirect.h"
Pavel Shilovsky28ea5292012-05-23 16:18:00 +040027
Pavel Shilovskyef68e832019-01-18 17:25:36 -080028/* Change credits for different ops and return the total number of credits */
Pavel Shilovsky28ea5292012-05-23 16:18:00 +040029static int
30change_conf(struct TCP_Server_Info *server)
31{
32 server->credits += server->echo_credits + server->oplock_credits;
33 server->oplock_credits = server->echo_credits = 0;
34 switch (server->credits) {
35 case 0:
Pavel Shilovskyef68e832019-01-18 17:25:36 -080036 return 0;
Pavel Shilovsky28ea5292012-05-23 16:18:00 +040037 case 1:
38 server->echoes = false;
39 server->oplocks = false;
Pavel Shilovsky28ea5292012-05-23 16:18:00 +040040 break;
41 case 2:
42 server->echoes = true;
43 server->oplocks = false;
44 server->echo_credits = 1;
Pavel Shilovsky28ea5292012-05-23 16:18:00 +040045 break;
46 default:
47 server->echoes = true;
Steve Frenche0ddde92015-09-22 09:29:38 -050048 if (enable_oplocks) {
49 server->oplocks = true;
50 server->oplock_credits = 1;
51 } else
52 server->oplocks = false;
53
Pavel Shilovsky28ea5292012-05-23 16:18:00 +040054 server->echo_credits = 1;
Pavel Shilovsky28ea5292012-05-23 16:18:00 +040055 }
56 server->credits -= server->echo_credits + server->oplock_credits;
Pavel Shilovskyef68e832019-01-18 17:25:36 -080057 return server->credits + server->echo_credits + server->oplock_credits;
Pavel Shilovsky28ea5292012-05-23 16:18:00 +040058}
59
60static void
Pavel Shilovsky335b7b62019-01-16 11:12:41 -080061smb2_add_credits(struct TCP_Server_Info *server,
62 const struct cifs_credits *credits, const int optype)
Pavel Shilovsky28ea5292012-05-23 16:18:00 +040063{
Pavel Shilovskyef68e832019-01-18 17:25:36 -080064 int *val, rc = -1;
Pavel Shilovsky335b7b62019-01-16 11:12:41 -080065 unsigned int add = credits->value;
66 unsigned int instance = credits->instance;
67 bool reconnect_detected = false;
Pavel Shilovskyef68e832019-01-18 17:25:36 -080068
Pavel Shilovsky28ea5292012-05-23 16:18:00 +040069 spin_lock(&server->req_lock);
70 val = server->ops->get_credits_field(server, optype);
Steve Frenchb340a4d2018-09-01 01:10:17 -050071
72 /* eg found case where write overlapping reconnect messed up credits */
73 if (((optype & CIFS_OP_MASK) == CIFS_NEG_OP) && (*val != 0))
74 trace_smb3_reconnect_with_invalid_credits(server->CurrentMid,
Steve French9eec21b2020-10-19 18:18:15 -050075 server->hostname, *val, add);
Pavel Shilovsky335b7b62019-01-16 11:12:41 -080076 if ((instance == 0) || (instance == server->reconnect_instance))
77 *val += add;
78 else
79 reconnect_detected = true;
Steve Frenchb340a4d2018-09-01 01:10:17 -050080
Steve French141891f2016-09-23 00:44:16 -050081 if (*val > 65000) {
82 *val = 65000; /* Don't get near 64K credits, avoid srv bugs */
Joe Perchesa0a30362020-04-14 22:42:53 -070083 pr_warn_once("server overflowed SMB3 credits\n");
Steve French141891f2016-09-23 00:44:16 -050084 }
Pavel Shilovsky28ea5292012-05-23 16:18:00 +040085 server->in_flight--;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +040086 if (server->in_flight == 0 && (optype & CIFS_OP_MASK) != CIFS_NEG_OP)
Pavel Shilovsky28ea5292012-05-23 16:18:00 +040087 rc = change_conf(server);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -070088 /*
89 * Sometimes server returns 0 credits on oplock break ack - we need to
90 * rebalance credits in this case.
91 */
92 else if (server->in_flight > 0 && server->oplock_credits == 0 &&
93 server->oplocks) {
94 if (server->credits > 1) {
95 server->credits--;
96 server->oplock_credits++;
97 }
98 }
Pavel Shilovsky28ea5292012-05-23 16:18:00 +040099 spin_unlock(&server->req_lock);
100 wake_up(&server->request_q);
Pavel Shilovskyef68e832019-01-18 17:25:36 -0800101
Pavel Shilovsky335b7b62019-01-16 11:12:41 -0800102 if (reconnect_detected)
103 cifs_dbg(FYI, "trying to put %d credits from the old server instance %d\n",
104 add, instance);
105
Pavel Shilovsky82e04572019-01-25 10:56:41 -0800106 if (server->tcpStatus == CifsNeedReconnect
107 || server->tcpStatus == CifsExiting)
Pavel Shilovskyef68e832019-01-18 17:25:36 -0800108 return;
109
110 switch (rc) {
111 case -1:
112 /* change_conf hasn't been executed */
113 break;
114 case 0:
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +1000115 cifs_server_dbg(VFS, "Possible client or server bug - zero credits\n");
Pavel Shilovskyef68e832019-01-18 17:25:36 -0800116 break;
117 case 1:
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +1000118 cifs_server_dbg(VFS, "disabling echoes and oplocks\n");
Pavel Shilovskyef68e832019-01-18 17:25:36 -0800119 break;
120 case 2:
121 cifs_dbg(FYI, "disabling oplocks\n");
122 break;
123 default:
Steve French9eec21b2020-10-19 18:18:15 -0500124 trace_smb3_add_credits(server->CurrentMid,
125 server->hostname, rc, add);
Pavel Shilovskyef68e832019-01-18 17:25:36 -0800126 cifs_dbg(FYI, "add %u credits total=%d\n", add, rc);
127 }
Pavel Shilovsky28ea5292012-05-23 16:18:00 +0400128}
129
130static void
131smb2_set_credits(struct TCP_Server_Info *server, const int val)
132{
133 spin_lock(&server->req_lock);
134 server->credits = val;
Steve French9e1a37d2018-09-19 02:38:17 -0500135 if (val == 1)
136 server->reconnect_instance++;
Pavel Shilovsky28ea5292012-05-23 16:18:00 +0400137 spin_unlock(&server->req_lock);
Steve French6e4d3bb2018-09-22 11:25:04 -0500138 /* don't log while holding the lock */
139 if (val == 1)
140 cifs_dbg(FYI, "set credits to 1 due to smb2 reconnect\n");
Pavel Shilovsky28ea5292012-05-23 16:18:00 +0400141}
142
143static int *
144smb2_get_credits_field(struct TCP_Server_Info *server, const int optype)
145{
146 switch (optype) {
147 case CIFS_ECHO_OP:
148 return &server->echo_credits;
149 case CIFS_OBREAK_OP:
150 return &server->oplock_credits;
151 default:
152 return &server->credits;
153 }
154}
155
156static unsigned int
157smb2_get_credits(struct mid_q_entry *mid)
158{
Pavel Shilovsky86a79642019-11-21 11:35:13 -0800159 return mid->credits_received;
Pavel Shilovsky28ea5292012-05-23 16:18:00 +0400160}
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400161
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +0400162static int
163smb2_wait_mtu_credits(struct TCP_Server_Info *server, unsigned int size,
Pavel Shilovsky335b7b62019-01-16 11:12:41 -0800164 unsigned int *num, struct cifs_credits *credits)
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +0400165{
166 int rc = 0;
167 unsigned int scredits;
168
169 spin_lock(&server->req_lock);
170 while (1) {
171 if (server->credits <= 0) {
172 spin_unlock(&server->req_lock);
173 cifs_num_waiters_inc(server);
174 rc = wait_event_killable(server->request_q,
Ronnie Sahlbergb227d212019-03-08 12:58:20 +1000175 has_credits(server, &server->credits, 1));
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +0400176 cifs_num_waiters_dec(server);
177 if (rc)
178 return rc;
179 spin_lock(&server->req_lock);
180 } else {
181 if (server->tcpStatus == CifsExiting) {
182 spin_unlock(&server->req_lock);
183 return -ENOENT;
184 }
185
186 scredits = server->credits;
187 /* can deadlock with reopen */
Pavel Shilovskyacc58d02019-01-17 08:21:24 -0800188 if (scredits <= 8) {
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +0400189 *num = SMB2_MAX_BUFFER_SIZE;
Pavel Shilovsky335b7b62019-01-16 11:12:41 -0800190 credits->value = 0;
191 credits->instance = 0;
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +0400192 break;
193 }
194
Pavel Shilovskyacc58d02019-01-17 08:21:24 -0800195 /* leave some credits for reopen and other ops */
196 scredits -= 8;
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +0400197 *num = min_t(unsigned int, size,
198 scredits * SMB2_MAX_BUFFER_SIZE);
199
Pavel Shilovsky335b7b62019-01-16 11:12:41 -0800200 credits->value =
201 DIV_ROUND_UP(*num, SMB2_MAX_BUFFER_SIZE);
202 credits->instance = server->reconnect_instance;
203 server->credits -= credits->value;
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +0400204 server->in_flight++;
Steve French1b63f182019-09-09 22:57:11 -0500205 if (server->in_flight > server->max_in_flight)
206 server->max_in_flight = server->in_flight;
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +0400207 break;
208 }
209 }
210 spin_unlock(&server->req_lock);
211 return rc;
212}
213
Pavel Shilovsky9a1c67e2019-01-23 18:15:52 -0800214static int
215smb2_adjust_credits(struct TCP_Server_Info *server,
216 struct cifs_credits *credits,
217 const unsigned int payload_size)
218{
219 int new_val = DIV_ROUND_UP(payload_size, SMB2_MAX_BUFFER_SIZE);
220
221 if (!credits->value || credits->value == new_val)
222 return 0;
223
224 if (credits->value < new_val) {
225 WARN_ONCE(1, "request has less credits (%d) than required (%d)",
226 credits->value, new_val);
227 return -ENOTSUPP;
228 }
229
230 spin_lock(&server->req_lock);
231
232 if (server->reconnect_instance != credits->instance) {
233 spin_unlock(&server->req_lock);
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +1000234 cifs_server_dbg(VFS, "trying to return %d credits to old session\n",
Pavel Shilovsky9a1c67e2019-01-23 18:15:52 -0800235 credits->value - new_val);
236 return -EAGAIN;
237 }
238
239 server->credits += credits->value - new_val;
240 spin_unlock(&server->req_lock);
241 wake_up(&server->request_q);
242 credits->value = new_val;
243 return 0;
244}
245
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400246static __u64
247smb2_get_next_mid(struct TCP_Server_Info *server)
248{
249 __u64 mid;
250 /* for SMB2 we need the current value */
251 spin_lock(&GlobalMid_Lock);
252 mid = server->CurrentMid++;
253 spin_unlock(&GlobalMid_Lock);
254 return mid;
255}
Steve French1080ef72011-02-24 18:07:19 +0000256
Pavel Shilovskyc781af72019-03-04 14:02:50 -0800257static void
258smb2_revert_current_mid(struct TCP_Server_Info *server, const unsigned int val)
259{
260 spin_lock(&GlobalMid_Lock);
261 if (server->CurrentMid >= val)
262 server->CurrentMid -= val;
263 spin_unlock(&GlobalMid_Lock);
264}
265
Pavel Shilovsky093b2bd2011-06-08 15:51:07 +0400266static struct mid_q_entry *
Rohith Surabattulaac873aa2020-10-29 05:03:10 +0000267__smb2_find_mid(struct TCP_Server_Info *server, char *buf, bool dequeue)
Pavel Shilovsky093b2bd2011-06-08 15:51:07 +0400268{
269 struct mid_q_entry *mid;
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +1000270 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700271 __u64 wire_mid = le64_to_cpu(shdr->MessageId);
Pavel Shilovsky093b2bd2011-06-08 15:51:07 +0400272
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700273 if (shdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM) {
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +1000274 cifs_server_dbg(VFS, "Encrypted frame parsing not supported yet\n");
Steve French373512e2015-12-18 13:05:30 -0600275 return NULL;
276 }
277
Pavel Shilovsky093b2bd2011-06-08 15:51:07 +0400278 spin_lock(&GlobalMid_Lock);
279 list_for_each_entry(mid, &server->pending_mid_q, qhead) {
Sachin Prabhu9235d092014-12-09 17:37:00 +0000280 if ((mid->mid == wire_mid) &&
Pavel Shilovsky093b2bd2011-06-08 15:51:07 +0400281 (mid->mid_state == MID_REQUEST_SUBMITTED) &&
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700282 (mid->command == shdr->Command)) {
Lars Persson696e4202018-06-25 14:05:25 +0200283 kref_get(&mid->refcount);
Rohith Surabattulaac873aa2020-10-29 05:03:10 +0000284 if (dequeue) {
285 list_del_init(&mid->qhead);
286 mid->mid_flags |= MID_DELETED;
287 }
Pavel Shilovsky093b2bd2011-06-08 15:51:07 +0400288 spin_unlock(&GlobalMid_Lock);
289 return mid;
290 }
291 }
292 spin_unlock(&GlobalMid_Lock);
293 return NULL;
294}
295
Rohith Surabattulaac873aa2020-10-29 05:03:10 +0000296static struct mid_q_entry *
297smb2_find_mid(struct TCP_Server_Info *server, char *buf)
298{
299 return __smb2_find_mid(server, buf, false);
300}
301
302static struct mid_q_entry *
303smb2_find_dequeue_mid(struct TCP_Server_Info *server, char *buf)
304{
305 return __smb2_find_mid(server, buf, true);
306}
307
Pavel Shilovsky093b2bd2011-06-08 15:51:07 +0400308static void
Ronnie Sahlberg14547f72018-04-22 14:45:53 -0600309smb2_dump_detail(void *buf, struct TCP_Server_Info *server)
Pavel Shilovsky093b2bd2011-06-08 15:51:07 +0400310{
311#ifdef CONFIG_CIFS_DEBUG2
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +1000312 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
Pavel Shilovsky093b2bd2011-06-08 15:51:07 +0400313
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +1000314 cifs_server_dbg(VFS, "Cmd: %d Err: 0x%x Flags: 0x%x Mid: %llu Pid: %d\n",
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700315 shdr->Command, shdr->Status, shdr->Flags, shdr->MessageId,
316 shdr->ProcessId);
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +1000317 cifs_server_dbg(VFS, "smb buf %p len %u\n", buf,
Steve French71992e622018-05-06 15:58:51 -0500318 server->ops->calc_smb_size(buf, server));
Pavel Shilovsky093b2bd2011-06-08 15:51:07 +0400319#endif
320}
321
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400322static bool
323smb2_need_neg(struct TCP_Server_Info *server)
324{
325 return server->max_read == 0;
326}
327
328static int
329smb2_negotiate(const unsigned int xid, struct cifs_ses *ses)
330{
331 int rc;
Christoph Probsta205d502019-05-08 21:36:25 +0200332
Aurelien Aptelf6a6bf72019-09-20 06:22:14 +0200333 cifs_ses_server(ses)->CurrentMid = 0;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400334 rc = SMB2_negotiate(xid, ses);
335 /* BB we probably don't need to retry with modern servers */
336 if (rc == -EAGAIN)
337 rc = -EHOSTDOWN;
338 return rc;
339}
340
Pavel Shilovsky3a3bab52012-09-18 16:20:28 -0700341static unsigned int
342smb2_negotiate_wsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
343{
344 struct TCP_Server_Info *server = tcon->ses->server;
345 unsigned int wsize;
346
347 /* start with specified wsize, or default */
348 wsize = volume_info->wsize ? volume_info->wsize : CIFS_DEFAULT_IOSIZE;
349 wsize = min_t(unsigned int, wsize, server->max_write);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +0400350 if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
351 wsize = min_t(unsigned int, wsize, SMB2_MAX_BUFFER_SIZE);
Pavel Shilovsky3a3bab52012-09-18 16:20:28 -0700352
Pavel Shilovsky3a3bab52012-09-18 16:20:28 -0700353 return wsize;
354}
355
356static unsigned int
Steve French3d621232018-09-25 15:33:47 -0500357smb3_negotiate_wsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
358{
359 struct TCP_Server_Info *server = tcon->ses->server;
360 unsigned int wsize;
361
362 /* start with specified wsize, or default */
363 wsize = volume_info->wsize ? volume_info->wsize : SMB3_DEFAULT_IOSIZE;
364 wsize = min_t(unsigned int, wsize, server->max_write);
365#ifdef CONFIG_CIFS_SMB_DIRECT
366 if (server->rdma) {
367 if (server->sign)
Long Lif7950cb2020-03-26 19:42:24 -0700368 /*
369 * Account for SMB2 data transfer packet header and
370 * possible encryption header
371 */
Steve French3d621232018-09-25 15:33:47 -0500372 wsize = min_t(unsigned int,
Long Lif7950cb2020-03-26 19:42:24 -0700373 wsize,
374 server->smbd_conn->max_fragmented_send_size -
375 SMB2_READWRITE_PDU_HEADER_SIZE -
376 sizeof(struct smb2_transform_hdr));
Steve French3d621232018-09-25 15:33:47 -0500377 else
378 wsize = min_t(unsigned int,
379 wsize, server->smbd_conn->max_readwrite_size);
380 }
381#endif
382 if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
383 wsize = min_t(unsigned int, wsize, SMB2_MAX_BUFFER_SIZE);
384
385 return wsize;
386}
387
388static unsigned int
Pavel Shilovsky3a3bab52012-09-18 16:20:28 -0700389smb2_negotiate_rsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
390{
391 struct TCP_Server_Info *server = tcon->ses->server;
392 unsigned int rsize;
393
394 /* start with specified rsize, or default */
395 rsize = volume_info->rsize ? volume_info->rsize : CIFS_DEFAULT_IOSIZE;
396 rsize = min_t(unsigned int, rsize, server->max_read);
Pavel Shilovskybed9da02014-06-25 11:28:57 +0400397
398 if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
399 rsize = min_t(unsigned int, rsize, SMB2_MAX_BUFFER_SIZE);
Pavel Shilovsky3a3bab52012-09-18 16:20:28 -0700400
Pavel Shilovsky3a3bab52012-09-18 16:20:28 -0700401 return rsize;
402}
403
Steve French3d621232018-09-25 15:33:47 -0500404static unsigned int
405smb3_negotiate_rsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
406{
407 struct TCP_Server_Info *server = tcon->ses->server;
408 unsigned int rsize;
409
410 /* start with specified rsize, or default */
411 rsize = volume_info->rsize ? volume_info->rsize : SMB3_DEFAULT_IOSIZE;
412 rsize = min_t(unsigned int, rsize, server->max_read);
413#ifdef CONFIG_CIFS_SMB_DIRECT
414 if (server->rdma) {
415 if (server->sign)
Long Lif7950cb2020-03-26 19:42:24 -0700416 /*
417 * Account for SMB2 data transfer packet header and
418 * possible encryption header
419 */
Steve French3d621232018-09-25 15:33:47 -0500420 rsize = min_t(unsigned int,
Long Lif7950cb2020-03-26 19:42:24 -0700421 rsize,
422 server->smbd_conn->max_fragmented_recv_size -
423 SMB2_READWRITE_PDU_HEADER_SIZE -
424 sizeof(struct smb2_transform_hdr));
Steve French3d621232018-09-25 15:33:47 -0500425 else
426 rsize = min_t(unsigned int,
427 rsize, server->smbd_conn->max_readwrite_size);
428 }
429#endif
430
431 if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
432 rsize = min_t(unsigned int, rsize, SMB2_MAX_BUFFER_SIZE);
433
434 return rsize;
435}
Aurelien Aptelfe856be2018-06-14 17:04:51 +0200436
437static int
438parse_server_interfaces(struct network_interface_info_ioctl_rsp *buf,
439 size_t buf_len,
440 struct cifs_server_iface **iface_list,
441 size_t *iface_count)
442{
443 struct network_interface_info_ioctl_rsp *p;
444 struct sockaddr_in *addr4;
445 struct sockaddr_in6 *addr6;
446 struct iface_info_ipv4 *p4;
447 struct iface_info_ipv6 *p6;
448 struct cifs_server_iface *info;
449 ssize_t bytes_left;
450 size_t next = 0;
451 int nb_iface = 0;
452 int rc = 0;
453
454 *iface_list = NULL;
455 *iface_count = 0;
456
457 /*
458 * Fist pass: count and sanity check
459 */
460
461 bytes_left = buf_len;
462 p = buf;
463 while (bytes_left >= sizeof(*p)) {
464 nb_iface++;
465 next = le32_to_cpu(p->Next);
466 if (!next) {
467 bytes_left -= sizeof(*p);
468 break;
469 }
470 p = (struct network_interface_info_ioctl_rsp *)((u8 *)p+next);
471 bytes_left -= next;
472 }
473
474 if (!nb_iface) {
475 cifs_dbg(VFS, "%s: malformed interface info\n", __func__);
476 rc = -EINVAL;
477 goto out;
478 }
479
480 if (bytes_left || p->Next)
481 cifs_dbg(VFS, "%s: incomplete interface info\n", __func__);
482
483
484 /*
485 * Second pass: extract info to internal structure
486 */
487
488 *iface_list = kcalloc(nb_iface, sizeof(**iface_list), GFP_KERNEL);
489 if (!*iface_list) {
490 rc = -ENOMEM;
491 goto out;
492 }
493
494 info = *iface_list;
495 bytes_left = buf_len;
496 p = buf;
497 while (bytes_left >= sizeof(*p)) {
498 info->speed = le64_to_cpu(p->LinkSpeed);
499 info->rdma_capable = le32_to_cpu(p->Capability & RDMA_CAPABLE);
500 info->rss_capable = le32_to_cpu(p->Capability & RSS_CAPABLE);
501
502 cifs_dbg(FYI, "%s: adding iface %zu\n", __func__, *iface_count);
503 cifs_dbg(FYI, "%s: speed %zu bps\n", __func__, info->speed);
504 cifs_dbg(FYI, "%s: capabilities 0x%08x\n", __func__,
505 le32_to_cpu(p->Capability));
506
507 switch (p->Family) {
508 /*
509 * The kernel and wire socket structures have the same
510 * layout and use network byte order but make the
511 * conversion explicit in case either one changes.
512 */
513 case INTERNETWORK:
514 addr4 = (struct sockaddr_in *)&info->sockaddr;
515 p4 = (struct iface_info_ipv4 *)p->Buffer;
516 addr4->sin_family = AF_INET;
517 memcpy(&addr4->sin_addr, &p4->IPv4Address, 4);
518
519 /* [MS-SMB2] 2.2.32.5.1.1 Clients MUST ignore these */
520 addr4->sin_port = cpu_to_be16(CIFS_PORT);
521
522 cifs_dbg(FYI, "%s: ipv4 %pI4\n", __func__,
523 &addr4->sin_addr);
524 break;
525 case INTERNETWORKV6:
526 addr6 = (struct sockaddr_in6 *)&info->sockaddr;
527 p6 = (struct iface_info_ipv6 *)p->Buffer;
528 addr6->sin6_family = AF_INET6;
529 memcpy(&addr6->sin6_addr, &p6->IPv6Address, 16);
530
531 /* [MS-SMB2] 2.2.32.5.1.2 Clients MUST ignore these */
532 addr6->sin6_flowinfo = 0;
533 addr6->sin6_scope_id = 0;
534 addr6->sin6_port = cpu_to_be16(CIFS_PORT);
535
536 cifs_dbg(FYI, "%s: ipv6 %pI6\n", __func__,
537 &addr6->sin6_addr);
538 break;
539 default:
540 cifs_dbg(VFS,
541 "%s: skipping unsupported socket family\n",
542 __func__);
543 goto next_iface;
544 }
545
546 (*iface_count)++;
547 info++;
548next_iface:
549 next = le32_to_cpu(p->Next);
550 if (!next)
551 break;
552 p = (struct network_interface_info_ioctl_rsp *)((u8 *)p+next);
553 bytes_left -= next;
554 }
555
556 if (!*iface_count) {
557 rc = -EINVAL;
558 goto out;
559 }
560
561out:
562 if (rc) {
563 kfree(*iface_list);
564 *iface_count = 0;
565 *iface_list = NULL;
566 }
567 return rc;
568}
569
Aurelien Aptel35adffe2019-09-20 06:29:39 +0200570static int compare_iface(const void *ia, const void *ib)
571{
572 const struct cifs_server_iface *a = (struct cifs_server_iface *)ia;
573 const struct cifs_server_iface *b = (struct cifs_server_iface *)ib;
574
575 return a->speed == b->speed ? 0 : (a->speed > b->speed ? -1 : 1);
576}
Aurelien Aptelfe856be2018-06-14 17:04:51 +0200577
Steve Frenchc481e9f2013-10-14 01:21:53 -0500578static int
579SMB3_request_interfaces(const unsigned int xid, struct cifs_tcon *tcon)
580{
581 int rc;
582 unsigned int ret_data_len = 0;
Aurelien Aptelfe856be2018-06-14 17:04:51 +0200583 struct network_interface_info_ioctl_rsp *out_buf = NULL;
584 struct cifs_server_iface *iface_list;
585 size_t iface_count;
586 struct cifs_ses *ses = tcon->ses;
Steve Frenchc481e9f2013-10-14 01:21:53 -0500587
588 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
589 FSCTL_QUERY_NETWORK_INTERFACE_INFO, true /* is_fsctl */,
590 NULL /* no data input */, 0 /* no data input */,
Steve French153322f2019-03-28 22:32:49 -0500591 CIFSMaxBufSize, (char **)&out_buf, &ret_data_len);
Steve Frenchc3ed4402018-06-28 22:53:39 -0500592 if (rc == -EOPNOTSUPP) {
593 cifs_dbg(FYI,
594 "server does not support query network interfaces\n");
595 goto out;
596 } else if (rc != 0) {
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +1000597 cifs_tcon_dbg(VFS, "error %d on ioctl to get interface list\n", rc);
Aurelien Aptelfe856be2018-06-14 17:04:51 +0200598 goto out;
Steve French9ffc5412014-10-16 15:13:14 -0500599 }
Aurelien Aptelfe856be2018-06-14 17:04:51 +0200600
601 rc = parse_server_interfaces(out_buf, ret_data_len,
602 &iface_list, &iface_count);
603 if (rc)
604 goto out;
605
Aurelien Aptel35adffe2019-09-20 06:29:39 +0200606 /* sort interfaces from fastest to slowest */
607 sort(iface_list, iface_count, sizeof(*iface_list), compare_iface, NULL);
608
Aurelien Aptelfe856be2018-06-14 17:04:51 +0200609 spin_lock(&ses->iface_lock);
610 kfree(ses->iface_list);
611 ses->iface_list = iface_list;
612 ses->iface_count = iface_count;
613 ses->iface_last_update = jiffies;
614 spin_unlock(&ses->iface_lock);
615
616out:
Steve French24df1482016-09-29 04:20:23 -0500617 kfree(out_buf);
Steve Frenchc481e9f2013-10-14 01:21:53 -0500618 return rc;
619}
Steve Frenchc481e9f2013-10-14 01:21:53 -0500620
Ronnie Sahlberg9da6ec72018-07-31 08:48:22 +1000621static void
622smb2_close_cached_fid(struct kref *ref)
Ronnie Sahlberga93864d2018-06-14 06:48:35 +1000623{
Ronnie Sahlberg9da6ec72018-07-31 08:48:22 +1000624 struct cached_fid *cfid = container_of(ref, struct cached_fid,
625 refcount);
626
Ronnie Sahlberga93864d2018-06-14 06:48:35 +1000627 if (cfid->is_valid) {
628 cifs_dbg(FYI, "clear cached root file handle\n");
629 SMB2_close(0, cfid->tcon, cfid->fid->persistent_fid,
630 cfid->fid->volatile_fid);
631 cfid->is_valid = false;
Ronnie Sahlbergb0f6df72019-03-12 13:58:31 +1000632 cfid->file_all_info_is_valid = false;
Pavel Shilovskyd9191312019-12-10 11:44:52 -0800633 cfid->has_lease = false;
Ronnie Sahlberga93864d2018-06-14 06:48:35 +1000634 }
Ronnie Sahlberg9da6ec72018-07-31 08:48:22 +1000635}
636
637void close_shroot(struct cached_fid *cfid)
638{
639 mutex_lock(&cfid->fid_mutex);
640 kref_put(&cfid->refcount, smb2_close_cached_fid);
Ronnie Sahlberga93864d2018-06-14 06:48:35 +1000641 mutex_unlock(&cfid->fid_mutex);
642}
643
Pavel Shilovskyd9191312019-12-10 11:44:52 -0800644void close_shroot_lease_locked(struct cached_fid *cfid)
645{
646 if (cfid->has_lease) {
647 cfid->has_lease = false;
648 kref_put(&cfid->refcount, smb2_close_cached_fid);
649 }
650}
651
652void close_shroot_lease(struct cached_fid *cfid)
653{
654 mutex_lock(&cfid->fid_mutex);
655 close_shroot_lease_locked(cfid);
656 mutex_unlock(&cfid->fid_mutex);
657}
658
Ronnie Sahlberg9da6ec72018-07-31 08:48:22 +1000659void
660smb2_cached_lease_break(struct work_struct *work)
661{
662 struct cached_fid *cfid = container_of(work,
663 struct cached_fid, lease_break);
664
Pavel Shilovskyd9191312019-12-10 11:44:52 -0800665 close_shroot_lease(cfid);
Ronnie Sahlberg9da6ec72018-07-31 08:48:22 +1000666}
667
Steve French3d4ef9a2018-04-25 22:19:09 -0500668/*
669 * Open the directory at the root of a share
670 */
Amir Goldstein0f060932020-02-03 21:46:43 +0200671int open_shroot(unsigned int xid, struct cifs_tcon *tcon,
Ronnie Sahlberg9e81e8f2020-10-05 12:37:52 +1000672 struct cifs_sb_info *cifs_sb,
673 struct cached_fid **cfid)
Steve French3d4ef9a2018-04-25 22:19:09 -0500674{
Ronnie Sahlbergb0f6df72019-03-12 13:58:31 +1000675 struct cifs_ses *ses = tcon->ses;
676 struct TCP_Server_Info *server = ses->server;
677 struct cifs_open_parms oparms;
678 struct smb2_create_rsp *o_rsp = NULL;
679 struct smb2_query_info_rsp *qi_rsp = NULL;
680 int resp_buftype[2];
681 struct smb_rqst rqst[2];
682 struct kvec rsp_iov[2];
683 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
684 struct kvec qi_iov[1];
685 int rc, flags = 0;
686 __le16 utf16_path = 0; /* Null - since an open of top of share */
Ronnie Sahlberga93864d2018-06-14 06:48:35 +1000687 u8 oplock = SMB2_OPLOCK_LEVEL_II;
Ronnie Sahlberg9e81e8f2020-10-05 12:37:52 +1000688 struct cifs_fid *pfid;
Steve French3d4ef9a2018-04-25 22:19:09 -0500689
Ronnie Sahlberga93864d2018-06-14 06:48:35 +1000690 mutex_lock(&tcon->crfid.fid_mutex);
691 if (tcon->crfid.is_valid) {
Steve French3d4ef9a2018-04-25 22:19:09 -0500692 cifs_dbg(FYI, "found a cached root file handle\n");
Ronnie Sahlberg9e81e8f2020-10-05 12:37:52 +1000693 *cfid = &tcon->crfid;
Ronnie Sahlberg9da6ec72018-07-31 08:48:22 +1000694 kref_get(&tcon->crfid.refcount);
Ronnie Sahlberga93864d2018-06-14 06:48:35 +1000695 mutex_unlock(&tcon->crfid.fid_mutex);
Steve French3d4ef9a2018-04-25 22:19:09 -0500696 return 0;
697 }
698
Steve French96d9f7e2019-09-12 17:52:54 -0500699 /*
700 * We do not hold the lock for the open because in case
701 * SMB2_open needs to reconnect, it will end up calling
702 * cifs_mark_open_files_invalid() which takes the lock again
703 * thus causing a deadlock
704 */
705
706 mutex_unlock(&tcon->crfid.fid_mutex);
707
Ronnie Sahlbergb0f6df72019-03-12 13:58:31 +1000708 if (smb3_encryption_required(tcon))
709 flags |= CIFS_TRANSFORM_REQ;
Steve French3d4ef9a2018-04-25 22:19:09 -0500710
Paulo Alcantara0fe07812020-04-20 23:44:24 -0300711 if (!server->ops->new_lease_key)
712 return -EIO;
713
Ronnie Sahlberg9e81e8f2020-10-05 12:37:52 +1000714 pfid = tcon->crfid.fid;
Paulo Alcantara0fe07812020-04-20 23:44:24 -0300715 server->ops->new_lease_key(pfid);
716
Ronnie Sahlbergb0f6df72019-03-12 13:58:31 +1000717 memset(rqst, 0, sizeof(rqst));
718 resp_buftype[0] = resp_buftype[1] = CIFS_NO_BUFFER;
719 memset(rsp_iov, 0, sizeof(rsp_iov));
720
721 /* Open */
722 memset(&open_iov, 0, sizeof(open_iov));
723 rqst[0].rq_iov = open_iov;
724 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
725
726 oparms.tcon = tcon;
Amir Goldstein0f060932020-02-03 21:46:43 +0200727 oparms.create_options = cifs_create_options(cifs_sb, 0);
Ronnie Sahlbergb0f6df72019-03-12 13:58:31 +1000728 oparms.desired_access = FILE_READ_ATTRIBUTES;
729 oparms.disposition = FILE_OPEN;
730 oparms.fid = pfid;
731 oparms.reconnect = false;
732
Aurelien Aptel352d96f2020-05-31 12:38:22 -0500733 rc = SMB2_open_init(tcon, server,
734 &rqst[0], &oplock, &oparms, &utf16_path);
Ronnie Sahlbergb0f6df72019-03-12 13:58:31 +1000735 if (rc)
Steve French96d9f7e2019-09-12 17:52:54 -0500736 goto oshr_free;
Ronnie Sahlbergb0f6df72019-03-12 13:58:31 +1000737 smb2_set_next_command(tcon, &rqst[0]);
738
739 memset(&qi_iov, 0, sizeof(qi_iov));
740 rqst[1].rq_iov = qi_iov;
741 rqst[1].rq_nvec = 1;
742
Aurelien Aptel352d96f2020-05-31 12:38:22 -0500743 rc = SMB2_query_info_init(tcon, server,
744 &rqst[1], COMPOUND_FID,
Ronnie Sahlbergb0f6df72019-03-12 13:58:31 +1000745 COMPOUND_FID, FILE_ALL_INFORMATION,
746 SMB2_O_INFO_FILE, 0,
747 sizeof(struct smb2_file_all_info) +
748 PATH_MAX * 2, 0, NULL);
749 if (rc)
Steve French96d9f7e2019-09-12 17:52:54 -0500750 goto oshr_free;
Ronnie Sahlbergb0f6df72019-03-12 13:58:31 +1000751
752 smb2_set_related(&rqst[1]);
753
Aurelien Aptel352d96f2020-05-31 12:38:22 -0500754 rc = compound_send_recv(xid, ses, server,
755 flags, 2, rqst,
Ronnie Sahlbergb0f6df72019-03-12 13:58:31 +1000756 resp_buftype, rsp_iov);
Aurelien Aptel7e5a70a2019-07-17 12:46:28 +0200757 mutex_lock(&tcon->crfid.fid_mutex);
758
759 /*
760 * Now we need to check again as the cached root might have
761 * been successfully re-opened from a concurrent process
762 */
763
764 if (tcon->crfid.is_valid) {
765 /* work was already done */
766
767 /* stash fids for close() later */
768 struct cifs_fid fid = {
769 .persistent_fid = pfid->persistent_fid,
770 .volatile_fid = pfid->volatile_fid,
771 };
772
773 /*
774 * caller expects this func to set pfid to a valid
775 * cached root, so we copy the existing one and get a
776 * reference.
777 */
778 memcpy(pfid, tcon->crfid.fid, sizeof(*pfid));
779 kref_get(&tcon->crfid.refcount);
780
781 mutex_unlock(&tcon->crfid.fid_mutex);
782
783 if (rc == 0) {
784 /* close extra handle outside of crit sec */
785 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
786 }
Xiyu Yang77577de2020-06-13 20:27:09 +0800787 rc = 0;
Aurelien Aptel7e5a70a2019-07-17 12:46:28 +0200788 goto oshr_free;
789 }
790
791 /* Cached root is still invalid, continue normaly */
792
Steve French7dcc82c2019-09-11 00:07:36 -0500793 if (rc) {
794 if (rc == -EREMCHG) {
795 tcon->need_reconnect = true;
Joe Perchesa0a30362020-04-14 22:42:53 -0700796 pr_warn_once("server share %s deleted\n",
797 tcon->treeName);
Steve French7dcc82c2019-09-11 00:07:36 -0500798 }
Ronnie Sahlbergb0f6df72019-03-12 13:58:31 +1000799 goto oshr_exit;
Steve French7dcc82c2019-09-11 00:07:36 -0500800 }
Ronnie Sahlbergb0f6df72019-03-12 13:58:31 +1000801
Steve Frenchd2f15422019-09-22 00:55:46 -0500802 atomic_inc(&tcon->num_remote_opens);
803
Ronnie Sahlbergb0f6df72019-03-12 13:58:31 +1000804 o_rsp = (struct smb2_create_rsp *)rsp_iov[0].iov_base;
805 oparms.fid->persistent_fid = o_rsp->PersistentFileId;
806 oparms.fid->volatile_fid = o_rsp->VolatileFileId;
807#ifdef CONFIG_CIFS_DEBUG2
808 oparms.fid->mid = le64_to_cpu(o_rsp->sync_hdr.MessageId);
809#endif /* CIFS_DEBUG2 */
810
Ronnie Sahlbergb0f6df72019-03-12 13:58:31 +1000811 memcpy(tcon->crfid.fid, pfid, sizeof(struct cifs_fid));
812 tcon->crfid.tcon = tcon;
813 tcon->crfid.is_valid = true;
814 kref_init(&tcon->crfid.refcount);
Ronnie Sahlbergb0f6df72019-03-12 13:58:31 +1000815
Steve French89a5bfa2019-07-18 17:22:18 -0500816 /* BB TBD check to see if oplock level check can be removed below */
Ronnie Sahlberg2f94a3122019-03-28 11:20:02 +1000817 if (o_rsp->OplockLevel == SMB2_OPLOCK_LEVEL_LEASE) {
818 kref_get(&tcon->crfid.refcount);
Pavel Shilovskyd9191312019-12-10 11:44:52 -0800819 tcon->crfid.has_lease = true;
Steve French89a5bfa2019-07-18 17:22:18 -0500820 smb2_parse_contexts(server, o_rsp,
821 &oparms.fid->epoch,
Aurelien Aptel69dda302020-03-02 17:53:22 +0100822 oparms.fid->lease_key, &oplock,
823 NULL, NULL);
Ronnie Sahlberg2f94a3122019-03-28 11:20:02 +1000824 } else
825 goto oshr_exit;
Ronnie Sahlbergb0f6df72019-03-12 13:58:31 +1000826
827 qi_rsp = (struct smb2_query_info_rsp *)rsp_iov[1].iov_base;
828 if (le32_to_cpu(qi_rsp->OutputBufferLength) < sizeof(struct smb2_file_all_info))
829 goto oshr_exit;
Ronnie Sahlberg4811e302019-04-01 09:53:44 +1000830 if (!smb2_validate_and_copy_iov(
Ronnie Sahlbergb0f6df72019-03-12 13:58:31 +1000831 le16_to_cpu(qi_rsp->OutputBufferOffset),
832 sizeof(struct smb2_file_all_info),
833 &rsp_iov[1], sizeof(struct smb2_file_all_info),
Ronnie Sahlberg4811e302019-04-01 09:53:44 +1000834 (char *)&tcon->crfid.file_all_info))
zhengbin720aec02019-12-25 11:30:20 +0800835 tcon->crfid.file_all_info_is_valid = true;
Ronnie Sahlbergb0f6df72019-03-12 13:58:31 +1000836
Aurelien Aptel7e5a70a2019-07-17 12:46:28 +0200837oshr_exit:
Ronnie Sahlberga93864d2018-06-14 06:48:35 +1000838 mutex_unlock(&tcon->crfid.fid_mutex);
Aurelien Aptel7e5a70a2019-07-17 12:46:28 +0200839oshr_free:
Ronnie Sahlbergb0f6df72019-03-12 13:58:31 +1000840 SMB2_open_free(&rqst[0]);
841 SMB2_query_info_free(&rqst[1]);
842 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
843 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
Ronnie Sahlberg9e81e8f2020-10-05 12:37:52 +1000844 if (rc == 0)
845 *cfid = &tcon->crfid;
Steve French3d4ef9a2018-04-25 22:19:09 -0500846 return rc;
847}
848
Steve French34f62642013-10-09 02:07:00 -0500849static void
Amir Goldstein0f060932020-02-03 21:46:43 +0200850smb3_qfs_tcon(const unsigned int xid, struct cifs_tcon *tcon,
851 struct cifs_sb_info *cifs_sb)
Steven Frenchaf6a12e2013-10-09 20:55:53 -0500852{
853 int rc;
854 __le16 srch_path = 0; /* Null - open root of share */
855 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
856 struct cifs_open_parms oparms;
857 struct cifs_fid fid;
Steve French3d4ef9a2018-04-25 22:19:09 -0500858 bool no_cached_open = tcon->nohandlecache;
Ronnie Sahlberg9e81e8f2020-10-05 12:37:52 +1000859 struct cached_fid *cfid = NULL;
Steven Frenchaf6a12e2013-10-09 20:55:53 -0500860
861 oparms.tcon = tcon;
862 oparms.desired_access = FILE_READ_ATTRIBUTES;
863 oparms.disposition = FILE_OPEN;
Amir Goldstein0f060932020-02-03 21:46:43 +0200864 oparms.create_options = cifs_create_options(cifs_sb, 0);
Steven Frenchaf6a12e2013-10-09 20:55:53 -0500865 oparms.fid = &fid;
866 oparms.reconnect = false;
867
Ronnie Sahlberg9e81e8f2020-10-05 12:37:52 +1000868 if (no_cached_open) {
Ronnie Sahlberg9d874c32018-06-08 13:21:18 +1000869 rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL,
Aurelien Aptel69dda302020-03-02 17:53:22 +0100870 NULL, NULL);
Ronnie Sahlberg9e81e8f2020-10-05 12:37:52 +1000871 } else {
872 rc = open_shroot(xid, tcon, cifs_sb, &cfid);
873 if (rc == 0)
874 memcpy(&fid, cfid->fid, sizeof(struct cifs_fid));
875 }
Steven Frenchaf6a12e2013-10-09 20:55:53 -0500876 if (rc)
877 return;
878
Steve Frenchc481e9f2013-10-14 01:21:53 -0500879 SMB3_request_interfaces(xid, tcon);
Steve Frenchc481e9f2013-10-14 01:21:53 -0500880
Steven Frenchaf6a12e2013-10-09 20:55:53 -0500881 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
882 FS_ATTRIBUTE_INFORMATION);
883 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
884 FS_DEVICE_INFORMATION);
885 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
Steve French21ba3842018-06-24 23:18:52 -0500886 FS_VOLUME_INFORMATION);
887 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
Steven Frenchaf6a12e2013-10-09 20:55:53 -0500888 FS_SECTOR_SIZE_INFORMATION); /* SMB3 specific */
Steve French3d4ef9a2018-04-25 22:19:09 -0500889 if (no_cached_open)
890 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
Ronnie Sahlberg9da6ec72018-07-31 08:48:22 +1000891 else
Ronnie Sahlberg9e81e8f2020-10-05 12:37:52 +1000892 close_shroot(cfid);
Steven Frenchaf6a12e2013-10-09 20:55:53 -0500893}
894
895static void
Amir Goldstein0f060932020-02-03 21:46:43 +0200896smb2_qfs_tcon(const unsigned int xid, struct cifs_tcon *tcon,
897 struct cifs_sb_info *cifs_sb)
Steve French34f62642013-10-09 02:07:00 -0500898{
899 int rc;
900 __le16 srch_path = 0; /* Null - open root of share */
901 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
902 struct cifs_open_parms oparms;
903 struct cifs_fid fid;
904
905 oparms.tcon = tcon;
906 oparms.desired_access = FILE_READ_ATTRIBUTES;
907 oparms.disposition = FILE_OPEN;
Amir Goldstein0f060932020-02-03 21:46:43 +0200908 oparms.create_options = cifs_create_options(cifs_sb, 0);
Steve French34f62642013-10-09 02:07:00 -0500909 oparms.fid = &fid;
910 oparms.reconnect = false;
911
Aurelien Aptel69dda302020-03-02 17:53:22 +0100912 rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL,
913 NULL, NULL);
Steve French34f62642013-10-09 02:07:00 -0500914 if (rc)
915 return;
916
Steven French21671142013-10-09 13:36:35 -0500917 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
918 FS_ATTRIBUTE_INFORMATION);
919 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
920 FS_DEVICE_INFORMATION);
Steve French34f62642013-10-09 02:07:00 -0500921 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
Steve French34f62642013-10-09 02:07:00 -0500922}
923
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +0400924static int
925smb2_is_path_accessible(const unsigned int xid, struct cifs_tcon *tcon,
926 struct cifs_sb_info *cifs_sb, const char *full_path)
927{
928 int rc;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +0400929 __le16 *utf16_path;
Pavel Shilovsky2e44b282012-09-18 16:20:33 -0700930 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
Pavel Shilovsky064f6042013-07-09 18:20:30 +0400931 struct cifs_open_parms oparms;
932 struct cifs_fid fid;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +0400933
Ronnie Sahlberga93864d2018-06-14 06:48:35 +1000934 if ((*full_path == 0) && tcon->crfid.is_valid)
Steve French3d4ef9a2018-04-25 22:19:09 -0500935 return 0;
936
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +0400937 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
938 if (!utf16_path)
939 return -ENOMEM;
940
Pavel Shilovsky064f6042013-07-09 18:20:30 +0400941 oparms.tcon = tcon;
942 oparms.desired_access = FILE_READ_ATTRIBUTES;
943 oparms.disposition = FILE_OPEN;
Amir Goldstein0f060932020-02-03 21:46:43 +0200944 oparms.create_options = cifs_create_options(cifs_sb, 0);
Pavel Shilovsky064f6042013-07-09 18:20:30 +0400945 oparms.fid = &fid;
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +0400946 oparms.reconnect = false;
Pavel Shilovsky064f6042013-07-09 18:20:30 +0400947
Aurelien Aptel69dda302020-03-02 17:53:22 +0100948 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, NULL,
949 NULL);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +0400950 if (rc) {
951 kfree(utf16_path);
952 return rc;
953 }
954
Pavel Shilovsky064f6042013-07-09 18:20:30 +0400955 rc = SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +0400956 kfree(utf16_path);
957 return rc;
958}
959
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +0400960static int
961smb2_get_srv_inum(const unsigned int xid, struct cifs_tcon *tcon,
962 struct cifs_sb_info *cifs_sb, const char *full_path,
963 u64 *uniqueid, FILE_ALL_INFO *data)
964{
965 *uniqueid = le64_to_cpu(data->IndexNumber);
966 return 0;
967}
968
Pavel Shilovskyb7546bc2012-09-18 16:20:27 -0700969static int
970smb2_query_file_info(const unsigned int xid, struct cifs_tcon *tcon,
971 struct cifs_fid *fid, FILE_ALL_INFO *data)
972{
973 int rc;
974 struct smb2_file_all_info *smb2_data;
975
Pavel Shilovsky1bbe4992014-08-22 13:32:11 +0400976 smb2_data = kzalloc(sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
Pavel Shilovskyb7546bc2012-09-18 16:20:27 -0700977 GFP_KERNEL);
978 if (smb2_data == NULL)
979 return -ENOMEM;
980
981 rc = SMB2_query_info(xid, tcon, fid->persistent_fid, fid->volatile_fid,
982 smb2_data);
983 if (!rc)
984 move_smb2_info_to_cifs(data, smb2_data);
985 kfree(smb2_data);
986 return rc;
987}
988
Arnd Bergmann1368f152017-09-05 11:24:15 +0200989#ifdef CONFIG_CIFS_XATTR
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +1000990static ssize_t
991move_smb2_ea_to_cifs(char *dst, size_t dst_size,
992 struct smb2_file_full_ea_info *src, size_t src_size,
993 const unsigned char *ea_name)
994{
995 int rc = 0;
996 unsigned int ea_name_len = ea_name ? strlen(ea_name) : 0;
997 char *name, *value;
Ronnie Sahlberg0c5d6cb2018-10-25 15:43:36 +1000998 size_t buf_size = dst_size;
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +1000999 size_t name_len, value_len, user_name_len;
1000
1001 while (src_size > 0) {
1002 name = &src->ea_data[0];
1003 name_len = (size_t)src->ea_name_length;
1004 value = &src->ea_data[src->ea_name_length + 1];
1005 value_len = (size_t)le16_to_cpu(src->ea_value_length);
1006
Christoph Probsta205d502019-05-08 21:36:25 +02001007 if (name_len == 0)
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10001008 break;
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10001009
1010 if (src_size < 8 + name_len + 1 + value_len) {
1011 cifs_dbg(FYI, "EA entry goes beyond length of list\n");
1012 rc = -EIO;
1013 goto out;
1014 }
1015
1016 if (ea_name) {
1017 if (ea_name_len == name_len &&
1018 memcmp(ea_name, name, name_len) == 0) {
1019 rc = value_len;
1020 if (dst_size == 0)
1021 goto out;
1022 if (dst_size < value_len) {
1023 rc = -ERANGE;
1024 goto out;
1025 }
1026 memcpy(dst, value, value_len);
1027 goto out;
1028 }
1029 } else {
1030 /* 'user.' plus a terminating null */
1031 user_name_len = 5 + 1 + name_len;
1032
Ronnie Sahlberg0c5d6cb2018-10-25 15:43:36 +10001033 if (buf_size == 0) {
1034 /* skip copy - calc size only */
1035 rc += user_name_len;
1036 } else if (dst_size >= user_name_len) {
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10001037 dst_size -= user_name_len;
1038 memcpy(dst, "user.", 5);
1039 dst += 5;
1040 memcpy(dst, src->ea_data, name_len);
1041 dst += name_len;
1042 *dst = 0;
1043 ++dst;
Ronnie Sahlberg0c5d6cb2018-10-25 15:43:36 +10001044 rc += user_name_len;
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10001045 } else {
1046 /* stop before overrun buffer */
1047 rc = -ERANGE;
1048 break;
1049 }
1050 }
1051
1052 if (!src->next_entry_offset)
1053 break;
1054
1055 if (src_size < le32_to_cpu(src->next_entry_offset)) {
1056 /* stop before overrun buffer */
1057 rc = -ERANGE;
1058 break;
1059 }
1060 src_size -= le32_to_cpu(src->next_entry_offset);
1061 src = (void *)((char *)src +
1062 le32_to_cpu(src->next_entry_offset));
1063 }
1064
1065 /* didn't find the named attribute */
1066 if (ea_name)
1067 rc = -ENODATA;
1068
1069out:
1070 return (ssize_t)rc;
1071}
1072
1073static ssize_t
1074smb2_query_eas(const unsigned int xid, struct cifs_tcon *tcon,
1075 const unsigned char *path, const unsigned char *ea_name,
1076 char *ea_data, size_t buf_size,
1077 struct cifs_sb_info *cifs_sb)
1078{
1079 int rc;
1080 __le16 *utf16_path;
Ronnie Sahlbergf9793b62018-11-27 09:52:04 +10001081 struct kvec rsp_iov = {NULL, 0};
1082 int buftype = CIFS_NO_BUFFER;
1083 struct smb2_query_info_rsp *rsp;
1084 struct smb2_file_full_ea_info *info = NULL;
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10001085
1086 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
1087 if (!utf16_path)
1088 return -ENOMEM;
1089
Ronnie Sahlbergf9793b62018-11-27 09:52:04 +10001090 rc = smb2_query_info_compound(xid, tcon, utf16_path,
1091 FILE_READ_EA,
1092 FILE_FULL_EA_INFORMATION,
1093 SMB2_O_INFO_FILE,
Ronnie Sahlbergc4627e62019-01-29 12:46:17 +10001094 CIFSMaxBufSize -
1095 MAX_SMB2_CREATE_RESPONSE_SIZE -
1096 MAX_SMB2_CLOSE_RESPONSE_SIZE,
Ronnie Sahlbergf9793b62018-11-27 09:52:04 +10001097 &rsp_iov, &buftype, cifs_sb);
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10001098 if (rc) {
Ronnie Sahlbergf9793b62018-11-27 09:52:04 +10001099 /*
1100 * If ea_name is NULL (listxattr) and there are no EAs,
1101 * return 0 as it's not an error. Otherwise, the specified
1102 * ea_name was not found.
1103 */
1104 if (!ea_name && rc == -ENODATA)
1105 rc = 0;
1106 goto qeas_exit;
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10001107 }
1108
Ronnie Sahlbergf9793b62018-11-27 09:52:04 +10001109 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
1110 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
1111 le32_to_cpu(rsp->OutputBufferLength),
1112 &rsp_iov,
1113 sizeof(struct smb2_file_full_ea_info));
1114 if (rc)
1115 goto qeas_exit;
Ronnie Sahlberg7cb3def2017-09-28 09:39:58 +10001116
Ronnie Sahlbergf9793b62018-11-27 09:52:04 +10001117 info = (struct smb2_file_full_ea_info *)(
1118 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
1119 rc = move_smb2_ea_to_cifs(ea_data, buf_size, info,
1120 le32_to_cpu(rsp->OutputBufferLength), ea_name);
Ronnie Sahlberg7cb3def2017-09-28 09:39:58 +10001121
Ronnie Sahlbergf9793b62018-11-27 09:52:04 +10001122 qeas_exit:
1123 kfree(utf16_path);
1124 free_rsp_buf(buftype, rsp_iov.iov_base);
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10001125 return rc;
1126}
1127
Ronnie Sahlberg55175542017-08-24 11:24:56 +10001128
1129static int
1130smb2_set_ea(const unsigned int xid, struct cifs_tcon *tcon,
1131 const char *path, const char *ea_name, const void *ea_value,
1132 const __u16 ea_value_len, const struct nls_table *nls_codepage,
1133 struct cifs_sb_info *cifs_sb)
1134{
Ronnie Sahlberg0967e542018-11-06 22:52:43 +10001135 struct cifs_ses *ses = tcon->ses;
Aurelien Aptel352d96f2020-05-31 12:38:22 -05001136 struct TCP_Server_Info *server = cifs_pick_channel(ses);
Ronnie Sahlberg0967e542018-11-06 22:52:43 +10001137 __le16 *utf16_path = NULL;
Ronnie Sahlberg55175542017-08-24 11:24:56 +10001138 int ea_name_len = strlen(ea_name);
Ronnie Sahlberg0967e542018-11-06 22:52:43 +10001139 int flags = 0;
Ronnie Sahlberg55175542017-08-24 11:24:56 +10001140 int len;
Ronnie Sahlberg0967e542018-11-06 22:52:43 +10001141 struct smb_rqst rqst[3];
1142 int resp_buftype[3];
1143 struct kvec rsp_iov[3];
1144 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
1145 struct cifs_open_parms oparms;
1146 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
1147 struct cifs_fid fid;
1148 struct kvec si_iov[SMB2_SET_INFO_IOV_SIZE];
1149 unsigned int size[1];
1150 void *data[1];
1151 struct smb2_file_full_ea_info *ea = NULL;
1152 struct kvec close_iov[1];
Ronnie Sahlberg85db6b72020-02-13 12:14:47 +10001153 struct smb2_query_info_rsp *rsp;
1154 int rc, used_len = 0;
Ronnie Sahlberg0967e542018-11-06 22:52:43 +10001155
1156 if (smb3_encryption_required(tcon))
1157 flags |= CIFS_TRANSFORM_REQ;
Ronnie Sahlberg55175542017-08-24 11:24:56 +10001158
1159 if (ea_name_len > 255)
1160 return -EINVAL;
1161
1162 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
1163 if (!utf16_path)
1164 return -ENOMEM;
1165
Ronnie Sahlberg0967e542018-11-06 22:52:43 +10001166 memset(rqst, 0, sizeof(rqst));
1167 resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
1168 memset(rsp_iov, 0, sizeof(rsp_iov));
1169
Ronnie Sahlberg21094642019-02-07 15:48:44 +10001170 if (ses->server->ops->query_all_EAs) {
1171 if (!ea_value) {
1172 rc = ses->server->ops->query_all_EAs(xid, tcon, path,
1173 ea_name, NULL, 0,
1174 cifs_sb);
1175 if (rc == -ENODATA)
1176 goto sea_exit;
Ronnie Sahlberg85db6b72020-02-13 12:14:47 +10001177 } else {
1178 /* If we are adding a attribute we should first check
1179 * if there will be enough space available to store
1180 * the new EA. If not we should not add it since we
1181 * would not be able to even read the EAs back.
1182 */
1183 rc = smb2_query_info_compound(xid, tcon, utf16_path,
1184 FILE_READ_EA,
1185 FILE_FULL_EA_INFORMATION,
1186 SMB2_O_INFO_FILE,
1187 CIFSMaxBufSize -
1188 MAX_SMB2_CREATE_RESPONSE_SIZE -
1189 MAX_SMB2_CLOSE_RESPONSE_SIZE,
1190 &rsp_iov[1], &resp_buftype[1], cifs_sb);
1191 if (rc == 0) {
1192 rsp = (struct smb2_query_info_rsp *)rsp_iov[1].iov_base;
1193 used_len = le32_to_cpu(rsp->OutputBufferLength);
1194 }
1195 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
1196 resp_buftype[1] = CIFS_NO_BUFFER;
1197 memset(&rsp_iov[1], 0, sizeof(rsp_iov[1]));
1198 rc = 0;
1199
1200 /* Use a fudge factor of 256 bytes in case we collide
1201 * with a different set_EAs command.
1202 */
1203 if(CIFSMaxBufSize - MAX_SMB2_CREATE_RESPONSE_SIZE -
1204 MAX_SMB2_CLOSE_RESPONSE_SIZE - 256 <
1205 used_len + ea_name_len + ea_value_len + 1) {
1206 rc = -ENOSPC;
1207 goto sea_exit;
1208 }
Ronnie Sahlberg21094642019-02-07 15:48:44 +10001209 }
1210 }
1211
Ronnie Sahlberg0967e542018-11-06 22:52:43 +10001212 /* Open */
1213 memset(&open_iov, 0, sizeof(open_iov));
1214 rqst[0].rq_iov = open_iov;
1215 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
1216
1217 memset(&oparms, 0, sizeof(oparms));
Ronnie Sahlberg55175542017-08-24 11:24:56 +10001218 oparms.tcon = tcon;
1219 oparms.desired_access = FILE_WRITE_EA;
1220 oparms.disposition = FILE_OPEN;
Amir Goldstein0f060932020-02-03 21:46:43 +02001221 oparms.create_options = cifs_create_options(cifs_sb, 0);
Ronnie Sahlberg55175542017-08-24 11:24:56 +10001222 oparms.fid = &fid;
1223 oparms.reconnect = false;
1224
Aurelien Aptel352d96f2020-05-31 12:38:22 -05001225 rc = SMB2_open_init(tcon, server,
1226 &rqst[0], &oplock, &oparms, utf16_path);
Ronnie Sahlberg0967e542018-11-06 22:52:43 +10001227 if (rc)
1228 goto sea_exit;
Ronnie Sahlberge77fe732018-12-31 13:43:40 +10001229 smb2_set_next_command(tcon, &rqst[0]);
Ronnie Sahlberg0967e542018-11-06 22:52:43 +10001230
1231
1232 /* Set Info */
1233 memset(&si_iov, 0, sizeof(si_iov));
1234 rqst[1].rq_iov = si_iov;
1235 rqst[1].rq_nvec = 1;
Ronnie Sahlberg55175542017-08-24 11:24:56 +10001236
Vladimir Zapolskiy64b7f672020-10-10 21:25:54 +03001237 len = sizeof(*ea) + ea_name_len + ea_value_len + 1;
Ronnie Sahlberg55175542017-08-24 11:24:56 +10001238 ea = kzalloc(len, GFP_KERNEL);
1239 if (ea == NULL) {
Ronnie Sahlberg0967e542018-11-06 22:52:43 +10001240 rc = -ENOMEM;
1241 goto sea_exit;
Ronnie Sahlberg55175542017-08-24 11:24:56 +10001242 }
1243
1244 ea->ea_name_length = ea_name_len;
1245 ea->ea_value_length = cpu_to_le16(ea_value_len);
1246 memcpy(ea->ea_data, ea_name, ea_name_len + 1);
1247 memcpy(ea->ea_data + ea_name_len + 1, ea_value, ea_value_len);
1248
Ronnie Sahlberg0967e542018-11-06 22:52:43 +10001249 size[0] = len;
1250 data[0] = ea;
1251
Aurelien Aptel352d96f2020-05-31 12:38:22 -05001252 rc = SMB2_set_info_init(tcon, server,
1253 &rqst[1], COMPOUND_FID,
Ronnie Sahlberg0967e542018-11-06 22:52:43 +10001254 COMPOUND_FID, current->tgid,
1255 FILE_FULL_EA_INFORMATION,
1256 SMB2_O_INFO_FILE, 0, data, size);
Ronnie Sahlberge77fe732018-12-31 13:43:40 +10001257 smb2_set_next_command(tcon, &rqst[1]);
Ronnie Sahlberg0967e542018-11-06 22:52:43 +10001258 smb2_set_related(&rqst[1]);
1259
1260
1261 /* Close */
1262 memset(&close_iov, 0, sizeof(close_iov));
1263 rqst[2].rq_iov = close_iov;
1264 rqst[2].rq_nvec = 1;
Aurelien Aptel352d96f2020-05-31 12:38:22 -05001265 rc = SMB2_close_init(tcon, server,
1266 &rqst[2], COMPOUND_FID, COMPOUND_FID, false);
Ronnie Sahlberg0967e542018-11-06 22:52:43 +10001267 smb2_set_related(&rqst[2]);
1268
Aurelien Aptel352d96f2020-05-31 12:38:22 -05001269 rc = compound_send_recv(xid, ses, server,
1270 flags, 3, rqst,
Ronnie Sahlberg0967e542018-11-06 22:52:43 +10001271 resp_buftype, rsp_iov);
Steve Frenchd2f15422019-09-22 00:55:46 -05001272 /* no need to bump num_remote_opens because handle immediately closed */
Ronnie Sahlberg0967e542018-11-06 22:52:43 +10001273
1274 sea_exit:
Paulo Alcantara6aa0c112018-07-04 14:16:16 -03001275 kfree(ea);
Ronnie Sahlberg0967e542018-11-06 22:52:43 +10001276 kfree(utf16_path);
1277 SMB2_open_free(&rqst[0]);
1278 SMB2_set_info_free(&rqst[1]);
1279 SMB2_close_free(&rqst[2]);
1280 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
1281 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
1282 free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
Ronnie Sahlberg55175542017-08-24 11:24:56 +10001283 return rc;
1284}
Arnd Bergmann1368f152017-09-05 11:24:15 +02001285#endif
Ronnie Sahlberg55175542017-08-24 11:24:56 +10001286
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04001287static bool
1288smb2_can_echo(struct TCP_Server_Info *server)
1289{
1290 return server->echoes;
1291}
1292
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001293static void
1294smb2_clear_stats(struct cifs_tcon *tcon)
1295{
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001296 int i;
Christoph Probsta205d502019-05-08 21:36:25 +02001297
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001298 for (i = 0; i < NUMBER_OF_SMB2_COMMANDS; i++) {
1299 atomic_set(&tcon->stats.smb2_stats.smb2_com_sent[i], 0);
1300 atomic_set(&tcon->stats.smb2_stats.smb2_com_failed[i], 0);
1301 }
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001302}
1303
1304static void
Steve French769ee6a2013-06-19 14:15:30 -05001305smb2_dump_share_caps(struct seq_file *m, struct cifs_tcon *tcon)
1306{
1307 seq_puts(m, "\n\tShare Capabilities:");
1308 if (tcon->capabilities & SMB2_SHARE_CAP_DFS)
1309 seq_puts(m, " DFS,");
1310 if (tcon->capabilities & SMB2_SHARE_CAP_CONTINUOUS_AVAILABILITY)
1311 seq_puts(m, " CONTINUOUS AVAILABILITY,");
1312 if (tcon->capabilities & SMB2_SHARE_CAP_SCALEOUT)
1313 seq_puts(m, " SCALEOUT,");
1314 if (tcon->capabilities & SMB2_SHARE_CAP_CLUSTER)
1315 seq_puts(m, " CLUSTER,");
1316 if (tcon->capabilities & SMB2_SHARE_CAP_ASYMMETRIC)
1317 seq_puts(m, " ASYMMETRIC,");
1318 if (tcon->capabilities == 0)
1319 seq_puts(m, " None");
Steven Frenchaf6a12e2013-10-09 20:55:53 -05001320 if (tcon->ss_flags & SSINFO_FLAGS_ALIGNED_DEVICE)
1321 seq_puts(m, " Aligned,");
1322 if (tcon->ss_flags & SSINFO_FLAGS_PARTITION_ALIGNED_ON_DEVICE)
1323 seq_puts(m, " Partition Aligned,");
1324 if (tcon->ss_flags & SSINFO_FLAGS_NO_SEEK_PENALTY)
1325 seq_puts(m, " SSD,");
1326 if (tcon->ss_flags & SSINFO_FLAGS_TRIM_ENABLED)
1327 seq_puts(m, " TRIM-support,");
1328
Steve French769ee6a2013-06-19 14:15:30 -05001329 seq_printf(m, "\tShare Flags: 0x%x", tcon->share_flags);
Steve Frenche0386e42018-05-20 01:27:03 -05001330 seq_printf(m, "\n\ttid: 0x%x", tcon->tid);
Steven Frenchaf6a12e2013-10-09 20:55:53 -05001331 if (tcon->perf_sector_size)
1332 seq_printf(m, "\tOptimal sector size: 0x%x",
1333 tcon->perf_sector_size);
Steve Frenche0386e42018-05-20 01:27:03 -05001334 seq_printf(m, "\tMaximal Access: 0x%x", tcon->maximal_access);
Steve French769ee6a2013-06-19 14:15:30 -05001335}
1336
1337static void
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001338smb2_print_stats(struct seq_file *m, struct cifs_tcon *tcon)
1339{
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001340 atomic_t *sent = tcon->stats.smb2_stats.smb2_com_sent;
1341 atomic_t *failed = tcon->stats.smb2_stats.smb2_com_failed;
Steve French1995d282018-07-27 15:14:04 -05001342
1343 /*
1344 * Can't display SMB2_NEGOTIATE, SESSION_SETUP, LOGOFF, CANCEL and ECHO
1345 * totals (requests sent) since those SMBs are per-session not per tcon
1346 */
Steve French52ce1ac2018-07-31 01:46:47 -05001347 seq_printf(m, "\nBytes read: %llu Bytes written: %llu",
1348 (long long)(tcon->bytes_read),
1349 (long long)(tcon->bytes_written));
Steve Frenchfae80442018-10-19 17:14:32 -05001350 seq_printf(m, "\nOpen files: %d total (local), %d open on server",
1351 atomic_read(&tcon->num_local_opens),
1352 atomic_read(&tcon->num_remote_opens));
Steve French1995d282018-07-27 15:14:04 -05001353 seq_printf(m, "\nTreeConnects: %d total %d failed",
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001354 atomic_read(&sent[SMB2_TREE_CONNECT_HE]),
1355 atomic_read(&failed[SMB2_TREE_CONNECT_HE]));
Steve French1995d282018-07-27 15:14:04 -05001356 seq_printf(m, "\nTreeDisconnects: %d total %d failed",
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001357 atomic_read(&sent[SMB2_TREE_DISCONNECT_HE]),
1358 atomic_read(&failed[SMB2_TREE_DISCONNECT_HE]));
Steve French1995d282018-07-27 15:14:04 -05001359 seq_printf(m, "\nCreates: %d total %d failed",
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001360 atomic_read(&sent[SMB2_CREATE_HE]),
1361 atomic_read(&failed[SMB2_CREATE_HE]));
Steve French1995d282018-07-27 15:14:04 -05001362 seq_printf(m, "\nCloses: %d total %d failed",
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001363 atomic_read(&sent[SMB2_CLOSE_HE]),
1364 atomic_read(&failed[SMB2_CLOSE_HE]));
Steve French1995d282018-07-27 15:14:04 -05001365 seq_printf(m, "\nFlushes: %d total %d failed",
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001366 atomic_read(&sent[SMB2_FLUSH_HE]),
1367 atomic_read(&failed[SMB2_FLUSH_HE]));
Steve French1995d282018-07-27 15:14:04 -05001368 seq_printf(m, "\nReads: %d total %d failed",
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001369 atomic_read(&sent[SMB2_READ_HE]),
1370 atomic_read(&failed[SMB2_READ_HE]));
Steve French1995d282018-07-27 15:14:04 -05001371 seq_printf(m, "\nWrites: %d total %d failed",
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001372 atomic_read(&sent[SMB2_WRITE_HE]),
1373 atomic_read(&failed[SMB2_WRITE_HE]));
Steve French1995d282018-07-27 15:14:04 -05001374 seq_printf(m, "\nLocks: %d total %d failed",
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001375 atomic_read(&sent[SMB2_LOCK_HE]),
1376 atomic_read(&failed[SMB2_LOCK_HE]));
Steve French1995d282018-07-27 15:14:04 -05001377 seq_printf(m, "\nIOCTLs: %d total %d failed",
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001378 atomic_read(&sent[SMB2_IOCTL_HE]),
1379 atomic_read(&failed[SMB2_IOCTL_HE]));
Steve French1995d282018-07-27 15:14:04 -05001380 seq_printf(m, "\nQueryDirectories: %d total %d failed",
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001381 atomic_read(&sent[SMB2_QUERY_DIRECTORY_HE]),
1382 atomic_read(&failed[SMB2_QUERY_DIRECTORY_HE]));
Steve French1995d282018-07-27 15:14:04 -05001383 seq_printf(m, "\nChangeNotifies: %d total %d failed",
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001384 atomic_read(&sent[SMB2_CHANGE_NOTIFY_HE]),
1385 atomic_read(&failed[SMB2_CHANGE_NOTIFY_HE]));
Steve French1995d282018-07-27 15:14:04 -05001386 seq_printf(m, "\nQueryInfos: %d total %d failed",
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001387 atomic_read(&sent[SMB2_QUERY_INFO_HE]),
1388 atomic_read(&failed[SMB2_QUERY_INFO_HE]));
Steve French1995d282018-07-27 15:14:04 -05001389 seq_printf(m, "\nSetInfos: %d total %d failed",
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001390 atomic_read(&sent[SMB2_SET_INFO_HE]),
1391 atomic_read(&failed[SMB2_SET_INFO_HE]));
1392 seq_printf(m, "\nOplockBreaks: %d sent %d failed",
1393 atomic_read(&sent[SMB2_OPLOCK_BREAK_HE]),
1394 atomic_read(&failed[SMB2_OPLOCK_BREAK_HE]));
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001395}
1396
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001397static void
1398smb2_set_fid(struct cifsFileInfo *cfile, struct cifs_fid *fid, __u32 oplock)
1399{
David Howells2b0143b2015-03-17 22:25:59 +00001400 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04001401 struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server;
1402
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001403 cfile->fid.persistent_fid = fid->persistent_fid;
1404 cfile->fid.volatile_fid = fid->volatile_fid;
Aurelien Aptel86f740f2020-02-21 11:19:06 +01001405 cfile->fid.access = fid->access;
Steve Frenchdfe33f92018-10-30 19:50:31 -05001406#ifdef CONFIG_CIFS_DEBUG2
1407 cfile->fid.mid = fid->mid;
1408#endif /* CIFS_DEBUG2 */
Pavel Shilovsky42873b02013-09-05 21:30:16 +04001409 server->ops->set_oplock_level(cinode, oplock, fid->epoch,
1410 &fid->purge_cache);
Pavel Shilovsky18cceb62013-09-05 13:01:06 +04001411 cinode->can_cache_brlcks = CIFS_CACHE_WRITE(cinode);
Aurelien Aptel94f87372016-09-22 07:38:50 +02001412 memcpy(cfile->fid.create_guid, fid->create_guid, 16);
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001413}
1414
Pavel Shilovsky760ad0c2012-09-25 11:00:07 +04001415static void
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001416smb2_close_file(const unsigned int xid, struct cifs_tcon *tcon,
1417 struct cifs_fid *fid)
1418{
Pavel Shilovsky760ad0c2012-09-25 11:00:07 +04001419 SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001420}
1421
Steve French43f8a6a2019-12-02 21:46:54 -06001422static void
1423smb2_close_getattr(const unsigned int xid, struct cifs_tcon *tcon,
1424 struct cifsFileInfo *cfile)
1425{
1426 struct smb2_file_network_open_info file_inf;
1427 struct inode *inode;
1428 int rc;
1429
1430 rc = __SMB2_close(xid, tcon, cfile->fid.persistent_fid,
1431 cfile->fid.volatile_fid, &file_inf);
1432 if (rc)
1433 return;
1434
1435 inode = d_inode(cfile->dentry);
1436
1437 spin_lock(&inode->i_lock);
1438 CIFS_I(inode)->time = jiffies;
1439
1440 /* Creation time should not need to be updated on close */
1441 if (file_inf.LastWriteTime)
1442 inode->i_mtime = cifs_NTtimeToUnix(file_inf.LastWriteTime);
1443 if (file_inf.ChangeTime)
1444 inode->i_ctime = cifs_NTtimeToUnix(file_inf.ChangeTime);
1445 if (file_inf.LastAccessTime)
1446 inode->i_atime = cifs_NTtimeToUnix(file_inf.LastAccessTime);
1447
1448 /*
1449 * i_blocks is not related to (i_size / i_blksize),
1450 * but instead 512 byte (2**9) size is required for
1451 * calculating num blocks.
1452 */
1453 if (le64_to_cpu(file_inf.AllocationSize) > 4096)
1454 inode->i_blocks =
1455 (512 - 1 + le64_to_cpu(file_inf.AllocationSize)) >> 9;
1456
1457 /* End of file and Attributes should not have to be updated on close */
1458 spin_unlock(&inode->i_lock);
1459}
1460
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07001461static int
Steve French41c13582013-11-14 00:05:36 -06001462SMB2_request_res_key(const unsigned int xid, struct cifs_tcon *tcon,
1463 u64 persistent_fid, u64 volatile_fid,
1464 struct copychunk_ioctl *pcchunk)
1465{
1466 int rc;
1467 unsigned int ret_data_len;
1468 struct resume_key_req *res_key;
1469
1470 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
1471 FSCTL_SRV_REQUEST_RESUME_KEY, true /* is_fsctl */,
Steve French153322f2019-03-28 22:32:49 -05001472 NULL, 0 /* no input */, CIFSMaxBufSize,
Steve French41c13582013-11-14 00:05:36 -06001473 (char **)&res_key, &ret_data_len);
1474
1475 if (rc) {
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +10001476 cifs_tcon_dbg(VFS, "refcpy ioctl error %d getting resume key\n", rc);
Steve French41c13582013-11-14 00:05:36 -06001477 goto req_res_key_exit;
1478 }
1479 if (ret_data_len < sizeof(struct resume_key_req)) {
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +10001480 cifs_tcon_dbg(VFS, "Invalid refcopy resume key length\n");
Steve French41c13582013-11-14 00:05:36 -06001481 rc = -EINVAL;
1482 goto req_res_key_exit;
1483 }
1484 memcpy(pcchunk->SourceKey, res_key->ResumeKey, COPY_CHUNK_RES_KEY_SIZE);
1485
1486req_res_key_exit:
1487 kfree(res_key);
1488 return rc;
1489}
1490
Ronnie Sahlbergb2ca6c22020-05-21 15:03:15 +10001491struct iqi_vars {
1492 struct smb_rqst rqst[3];
1493 struct kvec rsp_iov[3];
1494 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
1495 struct kvec qi_iov[1];
1496 struct kvec io_iov[SMB2_IOCTL_IOV_SIZE];
1497 struct kvec si_iov[SMB2_SET_INFO_IOV_SIZE];
1498 struct kvec close_iov[1];
1499};
1500
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05001501static int
1502smb2_ioctl_query_info(const unsigned int xid,
Ronnie Sahlberg8d8b26e2018-10-17 05:47:58 +10001503 struct cifs_tcon *tcon,
Amir Goldstein0f060932020-02-03 21:46:43 +02001504 struct cifs_sb_info *cifs_sb,
Ronnie Sahlberg8d8b26e2018-10-17 05:47:58 +10001505 __le16 *path, int is_dir,
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05001506 unsigned long p)
1507{
Ronnie Sahlbergb2ca6c22020-05-21 15:03:15 +10001508 struct iqi_vars *vars;
1509 struct smb_rqst *rqst;
1510 struct kvec *rsp_iov;
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05001511 struct cifs_ses *ses = tcon->ses;
Aurelien Aptel352d96f2020-05-31 12:38:22 -05001512 struct TCP_Server_Info *server = cifs_pick_channel(ses);
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05001513 char __user *arg = (char __user *)p;
1514 struct smb_query_info qi;
1515 struct smb_query_info __user *pqi;
1516 int rc = 0;
1517 int flags = 0;
Ronnie Sahlbergf5778c32019-03-15 09:07:22 +10001518 struct smb2_query_info_rsp *qi_rsp = NULL;
1519 struct smb2_ioctl_rsp *io_rsp = NULL;
Ronnie Sahlberg8d8b26e2018-10-17 05:47:58 +10001520 void *buffer = NULL;
Ronnie Sahlberg8d8b26e2018-10-17 05:47:58 +10001521 int resp_buftype[3];
Ronnie Sahlberg8d8b26e2018-10-17 05:47:58 +10001522 struct cifs_open_parms oparms;
1523 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
1524 struct cifs_fid fid;
Ronnie Sahlberg0e906962019-07-25 13:08:43 +10001525 unsigned int size[2];
1526 void *data[2];
Amir Goldstein0f060932020-02-03 21:46:43 +02001527 int create_options = is_dir ? CREATE_NOT_FILE : CREATE_NOT_DIR;
Ronnie Sahlberg8d8b26e2018-10-17 05:47:58 +10001528
Ronnie Sahlbergb2ca6c22020-05-21 15:03:15 +10001529 vars = kzalloc(sizeof(*vars), GFP_ATOMIC);
1530 if (vars == NULL)
1531 return -ENOMEM;
1532 rqst = &vars->rqst[0];
1533 rsp_iov = &vars->rsp_iov[0];
1534
Ronnie Sahlberg8d8b26e2018-10-17 05:47:58 +10001535 resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05001536
1537 if (copy_from_user(&qi, arg, sizeof(struct smb_query_info)))
Ronnie Sahlbergb2ca6c22020-05-21 15:03:15 +10001538 goto e_fault;
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05001539
Ronnie Sahlbergb2ca6c22020-05-21 15:03:15 +10001540 if (qi.output_buffer_length > 1024) {
1541 kfree(vars);
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05001542 return -EINVAL;
Ronnie Sahlbergb2ca6c22020-05-21 15:03:15 +10001543 }
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05001544
Aurelien Aptel352d96f2020-05-31 12:38:22 -05001545 if (!ses || !server) {
Ronnie Sahlbergb2ca6c22020-05-21 15:03:15 +10001546 kfree(vars);
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05001547 return -EIO;
Ronnie Sahlbergb2ca6c22020-05-21 15:03:15 +10001548 }
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05001549
1550 if (smb3_encryption_required(tcon))
1551 flags |= CIFS_TRANSFORM_REQ;
1552
Markus Elfringcfaa1182019-11-05 21:30:25 +01001553 buffer = memdup_user(arg + sizeof(struct smb_query_info),
1554 qi.output_buffer_length);
Ronnie Sahlbergb2ca6c22020-05-21 15:03:15 +10001555 if (IS_ERR(buffer)) {
1556 kfree(vars);
Markus Elfringcfaa1182019-11-05 21:30:25 +01001557 return PTR_ERR(buffer);
Ronnie Sahlbergb2ca6c22020-05-21 15:03:15 +10001558 }
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05001559
Ronnie Sahlberg8d8b26e2018-10-17 05:47:58 +10001560 /* Open */
Ronnie Sahlbergb2ca6c22020-05-21 15:03:15 +10001561 rqst[0].rq_iov = &vars->open_iov[0];
Ronnie Sahlberg8d8b26e2018-10-17 05:47:58 +10001562 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05001563
Ronnie Sahlberg8d8b26e2018-10-17 05:47:58 +10001564 memset(&oparms, 0, sizeof(oparms));
1565 oparms.tcon = tcon;
Ronnie Sahlberg8d8b26e2018-10-17 05:47:58 +10001566 oparms.disposition = FILE_OPEN;
Amir Goldstein0f060932020-02-03 21:46:43 +02001567 oparms.create_options = cifs_create_options(cifs_sb, create_options);
Ronnie Sahlberg8d8b26e2018-10-17 05:47:58 +10001568 oparms.fid = &fid;
1569 oparms.reconnect = false;
1570
Ronnie Sahlbergefac7792019-04-11 12:20:17 +10001571 if (qi.flags & PASSTHRU_FSCTL) {
1572 switch (qi.info_type & FSCTL_DEVICE_ACCESS_MASK) {
1573 case FSCTL_DEVICE_ACCESS_FILE_READ_WRITE_ACCESS:
1574 oparms.desired_access = FILE_READ_DATA | FILE_WRITE_DATA | FILE_READ_ATTRIBUTES | SYNCHRONIZE;
Steve French46e66612019-04-11 13:53:17 -05001575 break;
1576 case FSCTL_DEVICE_ACCESS_FILE_ANY_ACCESS:
1577 oparms.desired_access = GENERIC_ALL;
1578 break;
1579 case FSCTL_DEVICE_ACCESS_FILE_READ_ACCESS:
1580 oparms.desired_access = GENERIC_READ;
1581 break;
1582 case FSCTL_DEVICE_ACCESS_FILE_WRITE_ACCESS:
1583 oparms.desired_access = GENERIC_WRITE;
Ronnie Sahlbergefac7792019-04-11 12:20:17 +10001584 break;
1585 }
Ronnie Sahlberg0e906962019-07-25 13:08:43 +10001586 } else if (qi.flags & PASSTHRU_SET_INFO) {
1587 oparms.desired_access = GENERIC_WRITE;
1588 } else {
1589 oparms.desired_access = FILE_READ_ATTRIBUTES | READ_CONTROL;
Ronnie Sahlbergefac7792019-04-11 12:20:17 +10001590 }
1591
Aurelien Aptel352d96f2020-05-31 12:38:22 -05001592 rc = SMB2_open_init(tcon, server,
1593 &rqst[0], &oplock, &oparms, path);
Ronnie Sahlberg8d8b26e2018-10-17 05:47:58 +10001594 if (rc)
1595 goto iqinf_exit;
Ronnie Sahlberge77fe732018-12-31 13:43:40 +10001596 smb2_set_next_command(tcon, &rqst[0]);
Ronnie Sahlberg8d8b26e2018-10-17 05:47:58 +10001597
1598 /* Query */
Steve French31ba4332019-03-13 02:40:07 -05001599 if (qi.flags & PASSTHRU_FSCTL) {
1600 /* Can eventually relax perm check since server enforces too */
1601 if (!capable(CAP_SYS_ADMIN))
1602 rc = -EPERM;
Ronnie Sahlbergf5778c32019-03-15 09:07:22 +10001603 else {
Ronnie Sahlbergb2ca6c22020-05-21 15:03:15 +10001604 rqst[1].rq_iov = &vars->io_iov[0];
Ronnie Sahlbergf5778c32019-03-15 09:07:22 +10001605 rqst[1].rq_nvec = SMB2_IOCTL_IOV_SIZE;
1606
Aurelien Aptel352d96f2020-05-31 12:38:22 -05001607 rc = SMB2_ioctl_init(tcon, server,
1608 &rqst[1],
Ronnie Sahlbergf5778c32019-03-15 09:07:22 +10001609 COMPOUND_FID, COMPOUND_FID,
Ronnie Sahlbergefac7792019-04-11 12:20:17 +10001610 qi.info_type, true, buffer,
1611 qi.output_buffer_length,
Ronnie Sahlberg731b82b2020-01-08 13:08:07 +10001612 CIFSMaxBufSize -
1613 MAX_SMB2_CREATE_RESPONSE_SIZE -
1614 MAX_SMB2_CLOSE_RESPONSE_SIZE);
Ronnie Sahlbergf5778c32019-03-15 09:07:22 +10001615 }
Ronnie Sahlberg0e906962019-07-25 13:08:43 +10001616 } else if (qi.flags == PASSTHRU_SET_INFO) {
1617 /* Can eventually relax perm check since server enforces too */
1618 if (!capable(CAP_SYS_ADMIN))
1619 rc = -EPERM;
1620 else {
Ronnie Sahlbergb2ca6c22020-05-21 15:03:15 +10001621 rqst[1].rq_iov = &vars->si_iov[0];
Ronnie Sahlberg0e906962019-07-25 13:08:43 +10001622 rqst[1].rq_nvec = 1;
1623
1624 size[0] = 8;
1625 data[0] = buffer;
1626
Aurelien Aptel352d96f2020-05-31 12:38:22 -05001627 rc = SMB2_set_info_init(tcon, server,
1628 &rqst[1],
Ronnie Sahlberg0e906962019-07-25 13:08:43 +10001629 COMPOUND_FID, COMPOUND_FID,
1630 current->tgid,
1631 FILE_END_OF_FILE_INFORMATION,
1632 SMB2_O_INFO_FILE, 0, data, size);
1633 }
Steve French31ba4332019-03-13 02:40:07 -05001634 } else if (qi.flags == PASSTHRU_QUERY_INFO) {
Ronnie Sahlbergb2ca6c22020-05-21 15:03:15 +10001635 rqst[1].rq_iov = &vars->qi_iov[0];
Steve French31ba4332019-03-13 02:40:07 -05001636 rqst[1].rq_nvec = 1;
Ronnie Sahlberg8d8b26e2018-10-17 05:47:58 +10001637
Aurelien Aptel352d96f2020-05-31 12:38:22 -05001638 rc = SMB2_query_info_init(tcon, server,
1639 &rqst[1], COMPOUND_FID,
Steve French31ba4332019-03-13 02:40:07 -05001640 COMPOUND_FID, qi.file_info_class,
1641 qi.info_type, qi.additional_information,
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05001642 qi.input_buffer_length,
1643 qi.output_buffer_length, buffer);
Steve French31ba4332019-03-13 02:40:07 -05001644 } else { /* unknown flags */
Joe Perchesa0a30362020-04-14 22:42:53 -07001645 cifs_tcon_dbg(VFS, "Invalid passthru query flags: 0x%x\n",
1646 qi.flags);
Steve French31ba4332019-03-13 02:40:07 -05001647 rc = -EINVAL;
1648 }
1649
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05001650 if (rc)
1651 goto iqinf_exit;
Ronnie Sahlberge77fe732018-12-31 13:43:40 +10001652 smb2_set_next_command(tcon, &rqst[1]);
Ronnie Sahlberg8d8b26e2018-10-17 05:47:58 +10001653 smb2_set_related(&rqst[1]);
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05001654
Ronnie Sahlberg8d8b26e2018-10-17 05:47:58 +10001655 /* Close */
Ronnie Sahlbergb2ca6c22020-05-21 15:03:15 +10001656 rqst[2].rq_iov = &vars->close_iov[0];
Ronnie Sahlberg8d8b26e2018-10-17 05:47:58 +10001657 rqst[2].rq_nvec = 1;
1658
Aurelien Aptel352d96f2020-05-31 12:38:22 -05001659 rc = SMB2_close_init(tcon, server,
1660 &rqst[2], COMPOUND_FID, COMPOUND_FID, false);
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05001661 if (rc)
1662 goto iqinf_exit;
Ronnie Sahlberg8d8b26e2018-10-17 05:47:58 +10001663 smb2_set_related(&rqst[2]);
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05001664
Aurelien Aptel352d96f2020-05-31 12:38:22 -05001665 rc = compound_send_recv(xid, ses, server,
1666 flags, 3, rqst,
Ronnie Sahlberg8d8b26e2018-10-17 05:47:58 +10001667 resp_buftype, rsp_iov);
1668 if (rc)
1669 goto iqinf_exit;
Steve Frenchd2f15422019-09-22 00:55:46 -05001670
1671 /* No need to bump num_remote_opens since handle immediately closed */
Ronnie Sahlbergf5778c32019-03-15 09:07:22 +10001672 if (qi.flags & PASSTHRU_FSCTL) {
1673 pqi = (struct smb_query_info __user *)arg;
1674 io_rsp = (struct smb2_ioctl_rsp *)rsp_iov[1].iov_base;
1675 if (le32_to_cpu(io_rsp->OutputCount) < qi.input_buffer_length)
1676 qi.input_buffer_length = le32_to_cpu(io_rsp->OutputCount);
Ronnie Sahlberg5242fcb2019-04-15 12:13:52 +10001677 if (qi.input_buffer_length > 0 &&
Markus Elfring2b1116b2019-11-05 22:26:53 +01001678 le32_to_cpu(io_rsp->OutputOffset) + qi.input_buffer_length
1679 > rsp_iov[1].iov_len)
1680 goto e_fault;
1681
1682 if (copy_to_user(&pqi->input_buffer_length,
1683 &qi.input_buffer_length,
1684 sizeof(qi.input_buffer_length)))
1685 goto e_fault;
1686
Ronnie Sahlberg5242fcb2019-04-15 12:13:52 +10001687 if (copy_to_user((void __user *)pqi + sizeof(struct smb_query_info),
1688 (const void *)io_rsp + le32_to_cpu(io_rsp->OutputOffset),
Markus Elfring2b1116b2019-11-05 22:26:53 +01001689 qi.input_buffer_length))
1690 goto e_fault;
Ronnie Sahlbergf5778c32019-03-15 09:07:22 +10001691 } else {
1692 pqi = (struct smb_query_info __user *)arg;
1693 qi_rsp = (struct smb2_query_info_rsp *)rsp_iov[1].iov_base;
1694 if (le32_to_cpu(qi_rsp->OutputBufferLength) < qi.input_buffer_length)
1695 qi.input_buffer_length = le32_to_cpu(qi_rsp->OutputBufferLength);
Markus Elfring2b1116b2019-11-05 22:26:53 +01001696 if (copy_to_user(&pqi->input_buffer_length,
1697 &qi.input_buffer_length,
1698 sizeof(qi.input_buffer_length)))
1699 goto e_fault;
1700
1701 if (copy_to_user(pqi + 1, qi_rsp->Buffer,
1702 qi.input_buffer_length))
1703 goto e_fault;
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05001704 }
1705
1706 iqinf_exit:
Ronnie Sahlbergb2ca6c22020-05-21 15:03:15 +10001707 kfree(vars);
Ronnie Sahlberg8d8b26e2018-10-17 05:47:58 +10001708 kfree(buffer);
1709 SMB2_open_free(&rqst[0]);
Ronnie Sahlbergf5778c32019-03-15 09:07:22 +10001710 if (qi.flags & PASSTHRU_FSCTL)
1711 SMB2_ioctl_free(&rqst[1]);
1712 else
1713 SMB2_query_info_free(&rqst[1]);
1714
Ronnie Sahlberg8d8b26e2018-10-17 05:47:58 +10001715 SMB2_close_free(&rqst[2]);
1716 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
1717 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
1718 free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05001719 return rc;
Markus Elfring2b1116b2019-11-05 22:26:53 +01001720
1721e_fault:
1722 rc = -EFAULT;
1723 goto iqinf_exit;
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05001724}
1725
Sachin Prabhu620d8742017-02-10 16:03:51 +05301726static ssize_t
Sachin Prabhu312bbc52017-04-04 02:12:04 -05001727smb2_copychunk_range(const unsigned int xid,
Steve French41c13582013-11-14 00:05:36 -06001728 struct cifsFileInfo *srcfile,
1729 struct cifsFileInfo *trgtfile, u64 src_off,
1730 u64 len, u64 dest_off)
1731{
1732 int rc;
1733 unsigned int ret_data_len;
1734 struct copychunk_ioctl *pcchunk;
Steve French9bf0c9c2013-11-16 18:05:28 -06001735 struct copychunk_ioctl_rsp *retbuf = NULL;
1736 struct cifs_tcon *tcon;
1737 int chunks_copied = 0;
1738 bool chunk_sizes_updated = false;
Sachin Prabhu620d8742017-02-10 16:03:51 +05301739 ssize_t bytes_written, total_bytes_written = 0;
Steve French41c13582013-11-14 00:05:36 -06001740
1741 pcchunk = kmalloc(sizeof(struct copychunk_ioctl), GFP_KERNEL);
1742
1743 if (pcchunk == NULL)
1744 return -ENOMEM;
1745
Christoph Probsta205d502019-05-08 21:36:25 +02001746 cifs_dbg(FYI, "%s: about to call request res key\n", __func__);
Steve French41c13582013-11-14 00:05:36 -06001747 /* Request a key from the server to identify the source of the copy */
1748 rc = SMB2_request_res_key(xid, tlink_tcon(srcfile->tlink),
1749 srcfile->fid.persistent_fid,
1750 srcfile->fid.volatile_fid, pcchunk);
1751
1752 /* Note: request_res_key sets res_key null only if rc !=0 */
1753 if (rc)
Steve French9bf0c9c2013-11-16 18:05:28 -06001754 goto cchunk_out;
Steve French41c13582013-11-14 00:05:36 -06001755
1756 /* For now array only one chunk long, will make more flexible later */
Fabian Frederickbc09d142014-12-10 15:41:15 -08001757 pcchunk->ChunkCount = cpu_to_le32(1);
Steve French41c13582013-11-14 00:05:36 -06001758 pcchunk->Reserved = 0;
Steve French41c13582013-11-14 00:05:36 -06001759 pcchunk->Reserved2 = 0;
1760
Steve French9bf0c9c2013-11-16 18:05:28 -06001761 tcon = tlink_tcon(trgtfile->tlink);
1762
1763 while (len > 0) {
1764 pcchunk->SourceOffset = cpu_to_le64(src_off);
1765 pcchunk->TargetOffset = cpu_to_le64(dest_off);
1766 pcchunk->Length =
1767 cpu_to_le32(min_t(u32, len, tcon->max_bytes_chunk));
1768
1769 /* Request server copy to target from src identified by key */
1770 rc = SMB2_ioctl(xid, tcon, trgtfile->fid.persistent_fid,
Steve French41c13582013-11-14 00:05:36 -06001771 trgtfile->fid.volatile_fid, FSCTL_SRV_COPYCHUNK_WRITE,
Aurelien Aptel63a83b82018-01-24 13:46:11 +01001772 true /* is_fsctl */, (char *)pcchunk,
Steve French153322f2019-03-28 22:32:49 -05001773 sizeof(struct copychunk_ioctl), CIFSMaxBufSize,
1774 (char **)&retbuf, &ret_data_len);
Steve French9bf0c9c2013-11-16 18:05:28 -06001775 if (rc == 0) {
1776 if (ret_data_len !=
1777 sizeof(struct copychunk_ioctl_rsp)) {
Joe Perchesa0a30362020-04-14 22:42:53 -07001778 cifs_tcon_dbg(VFS, "Invalid cchunk response size\n");
Steve French9bf0c9c2013-11-16 18:05:28 -06001779 rc = -EIO;
1780 goto cchunk_out;
1781 }
1782 if (retbuf->TotalBytesWritten == 0) {
1783 cifs_dbg(FYI, "no bytes copied\n");
1784 rc = -EIO;
1785 goto cchunk_out;
1786 }
1787 /*
1788 * Check if server claimed to write more than we asked
1789 */
1790 if (le32_to_cpu(retbuf->TotalBytesWritten) >
1791 le32_to_cpu(pcchunk->Length)) {
Joe Perchesa0a30362020-04-14 22:42:53 -07001792 cifs_tcon_dbg(VFS, "Invalid copy chunk response\n");
Steve French9bf0c9c2013-11-16 18:05:28 -06001793 rc = -EIO;
1794 goto cchunk_out;
1795 }
1796 if (le32_to_cpu(retbuf->ChunksWritten) != 1) {
Joe Perchesa0a30362020-04-14 22:42:53 -07001797 cifs_tcon_dbg(VFS, "Invalid num chunks written\n");
Steve French9bf0c9c2013-11-16 18:05:28 -06001798 rc = -EIO;
1799 goto cchunk_out;
1800 }
1801 chunks_copied++;
Steve French41c13582013-11-14 00:05:36 -06001802
Sachin Prabhu620d8742017-02-10 16:03:51 +05301803 bytes_written = le32_to_cpu(retbuf->TotalBytesWritten);
1804 src_off += bytes_written;
1805 dest_off += bytes_written;
1806 len -= bytes_written;
1807 total_bytes_written += bytes_written;
Steve French41c13582013-11-14 00:05:36 -06001808
Sachin Prabhu620d8742017-02-10 16:03:51 +05301809 cifs_dbg(FYI, "Chunks %d PartialChunk %d Total %zu\n",
Steve French9bf0c9c2013-11-16 18:05:28 -06001810 le32_to_cpu(retbuf->ChunksWritten),
1811 le32_to_cpu(retbuf->ChunkBytesWritten),
Sachin Prabhu620d8742017-02-10 16:03:51 +05301812 bytes_written);
Steve French9bf0c9c2013-11-16 18:05:28 -06001813 } else if (rc == -EINVAL) {
1814 if (ret_data_len != sizeof(struct copychunk_ioctl_rsp))
1815 goto cchunk_out;
Steve French41c13582013-11-14 00:05:36 -06001816
Steve French9bf0c9c2013-11-16 18:05:28 -06001817 cifs_dbg(FYI, "MaxChunks %d BytesChunk %d MaxCopy %d\n",
1818 le32_to_cpu(retbuf->ChunksWritten),
1819 le32_to_cpu(retbuf->ChunkBytesWritten),
1820 le32_to_cpu(retbuf->TotalBytesWritten));
1821
1822 /*
1823 * Check if this is the first request using these sizes,
1824 * (ie check if copy succeed once with original sizes
1825 * and check if the server gave us different sizes after
1826 * we already updated max sizes on previous request).
1827 * if not then why is the server returning an error now
1828 */
1829 if ((chunks_copied != 0) || chunk_sizes_updated)
1830 goto cchunk_out;
1831
1832 /* Check that server is not asking us to grow size */
1833 if (le32_to_cpu(retbuf->ChunkBytesWritten) <
1834 tcon->max_bytes_chunk)
1835 tcon->max_bytes_chunk =
1836 le32_to_cpu(retbuf->ChunkBytesWritten);
1837 else
1838 goto cchunk_out; /* server gave us bogus size */
1839
1840 /* No need to change MaxChunks since already set to 1 */
1841 chunk_sizes_updated = true;
Sachin Prabhu2477bc52015-02-04 13:10:26 +00001842 } else
1843 goto cchunk_out;
Steve French9bf0c9c2013-11-16 18:05:28 -06001844 }
1845
1846cchunk_out:
Steve French41c13582013-11-14 00:05:36 -06001847 kfree(pcchunk);
Steve French24df1482016-09-29 04:20:23 -05001848 kfree(retbuf);
Sachin Prabhu620d8742017-02-10 16:03:51 +05301849 if (rc)
1850 return rc;
1851 else
1852 return total_bytes_written;
Steve French41c13582013-11-14 00:05:36 -06001853}
1854
1855static int
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07001856smb2_flush_file(const unsigned int xid, struct cifs_tcon *tcon,
1857 struct cifs_fid *fid)
1858{
1859 return SMB2_flush(xid, tcon, fid->persistent_fid, fid->volatile_fid);
1860}
1861
Pavel Shilovsky09a47072012-09-18 16:20:29 -07001862static unsigned int
1863smb2_read_data_offset(char *buf)
1864{
1865 struct smb2_read_rsp *rsp = (struct smb2_read_rsp *)buf;
Christoph Probsta205d502019-05-08 21:36:25 +02001866
Pavel Shilovsky09a47072012-09-18 16:20:29 -07001867 return rsp->DataOffset;
1868}
1869
1870static unsigned int
Long Li74dcf412017-11-22 17:38:46 -07001871smb2_read_data_length(char *buf, bool in_remaining)
Pavel Shilovsky09a47072012-09-18 16:20:29 -07001872{
1873 struct smb2_read_rsp *rsp = (struct smb2_read_rsp *)buf;
Long Li74dcf412017-11-22 17:38:46 -07001874
1875 if (in_remaining)
1876 return le32_to_cpu(rsp->DataRemaining);
1877
Pavel Shilovsky09a47072012-09-18 16:20:29 -07001878 return le32_to_cpu(rsp->DataLength);
1879}
1880
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07001881
1882static int
Steve Frenchdb8b6312014-09-22 05:13:55 -05001883smb2_sync_read(const unsigned int xid, struct cifs_fid *pfid,
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07001884 struct cifs_io_parms *parms, unsigned int *bytes_read,
1885 char **buf, int *buf_type)
1886{
Steve Frenchdb8b6312014-09-22 05:13:55 -05001887 parms->persistent_fid = pfid->persistent_fid;
1888 parms->volatile_fid = pfid->volatile_fid;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07001889 return SMB2_read(xid, parms, bytes_read, buf, buf_type);
1890}
1891
Pavel Shilovsky009d3442012-09-18 16:20:30 -07001892static int
Steve Frenchdb8b6312014-09-22 05:13:55 -05001893smb2_sync_write(const unsigned int xid, struct cifs_fid *pfid,
Pavel Shilovsky009d3442012-09-18 16:20:30 -07001894 struct cifs_io_parms *parms, unsigned int *written,
1895 struct kvec *iov, unsigned long nr_segs)
1896{
1897
Steve Frenchdb8b6312014-09-22 05:13:55 -05001898 parms->persistent_fid = pfid->persistent_fid;
1899 parms->volatile_fid = pfid->volatile_fid;
Pavel Shilovsky009d3442012-09-18 16:20:30 -07001900 return SMB2_write(xid, parms, written, iov, nr_segs);
1901}
1902
Steve Frenchd43cc792014-08-13 17:16:29 -05001903/* Set or clear the SPARSE_FILE attribute based on value passed in setsparse */
1904static bool smb2_set_sparse(const unsigned int xid, struct cifs_tcon *tcon,
1905 struct cifsFileInfo *cfile, struct inode *inode, __u8 setsparse)
1906{
1907 struct cifsInodeInfo *cifsi;
1908 int rc;
1909
1910 cifsi = CIFS_I(inode);
1911
1912 /* if file already sparse don't bother setting sparse again */
1913 if ((cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) && setsparse)
1914 return true; /* already sparse */
1915
1916 if (!(cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) && !setsparse)
1917 return true; /* already not sparse */
1918
1919 /*
1920 * Can't check for sparse support on share the usual way via the
1921 * FS attribute info (FILE_SUPPORTS_SPARSE_FILES) on the share
1922 * since Samba server doesn't set the flag on the share, yet
1923 * supports the set sparse FSCTL and returns sparse correctly
1924 * in the file attributes. If we fail setting sparse though we
1925 * mark that server does not support sparse files for this share
1926 * to avoid repeatedly sending the unsupported fsctl to server
1927 * if the file is repeatedly extended.
1928 */
1929 if (tcon->broken_sparse_sup)
1930 return false;
1931
1932 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
1933 cfile->fid.volatile_fid, FSCTL_SET_SPARSE,
Aurelien Aptel63a83b82018-01-24 13:46:11 +01001934 true /* is_fctl */,
Steve French153322f2019-03-28 22:32:49 -05001935 &setsparse, 1, CIFSMaxBufSize, NULL, NULL);
Steve Frenchd43cc792014-08-13 17:16:29 -05001936 if (rc) {
1937 tcon->broken_sparse_sup = true;
1938 cifs_dbg(FYI, "set sparse rc = %d\n", rc);
1939 return false;
1940 }
1941
1942 if (setsparse)
1943 cifsi->cifsAttrs |= FILE_ATTRIBUTE_SPARSE_FILE;
1944 else
1945 cifsi->cifsAttrs &= (~FILE_ATTRIBUTE_SPARSE_FILE);
1946
1947 return true;
1948}
1949
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07001950static int
1951smb2_set_file_size(const unsigned int xid, struct cifs_tcon *tcon,
1952 struct cifsFileInfo *cfile, __u64 size, bool set_alloc)
1953{
1954 __le64 eof = cpu_to_le64(size);
Steve French3d1a3742014-08-11 21:05:25 -05001955 struct inode *inode;
1956
1957 /*
1958 * If extending file more than one page make sparse. Many Linux fs
1959 * make files sparse by default when extending via ftruncate
1960 */
David Howells2b0143b2015-03-17 22:25:59 +00001961 inode = d_inode(cfile->dentry);
Steve French3d1a3742014-08-11 21:05:25 -05001962
1963 if (!set_alloc && (size > inode->i_size + 8192)) {
Steve French3d1a3742014-08-11 21:05:25 -05001964 __u8 set_sparse = 1;
Steve French3d1a3742014-08-11 21:05:25 -05001965
Steve Frenchd43cc792014-08-13 17:16:29 -05001966 /* whether set sparse succeeds or not, extend the file */
1967 smb2_set_sparse(xid, tcon, cfile, inode, set_sparse);
Steve French3d1a3742014-08-11 21:05:25 -05001968 }
1969
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07001970 return SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
Ronnie Sahlberg3764cbd2018-09-03 13:33:47 +10001971 cfile->fid.volatile_fid, cfile->pid, &eof);
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07001972}
1973
Steve French02b16662015-06-27 21:18:36 -07001974static int
1975smb2_duplicate_extents(const unsigned int xid,
1976 struct cifsFileInfo *srcfile,
1977 struct cifsFileInfo *trgtfile, u64 src_off,
1978 u64 len, u64 dest_off)
1979{
1980 int rc;
1981 unsigned int ret_data_len;
Steve French02b16662015-06-27 21:18:36 -07001982 struct duplicate_extents_to_file dup_ext_buf;
1983 struct cifs_tcon *tcon = tlink_tcon(trgtfile->tlink);
1984
1985 /* server fileays advertise duplicate extent support with this flag */
1986 if ((le32_to_cpu(tcon->fsAttrInfo.Attributes) &
1987 FILE_SUPPORTS_BLOCK_REFCOUNTING) == 0)
1988 return -EOPNOTSUPP;
1989
1990 dup_ext_buf.VolatileFileHandle = srcfile->fid.volatile_fid;
1991 dup_ext_buf.PersistentFileHandle = srcfile->fid.persistent_fid;
1992 dup_ext_buf.SourceFileOffset = cpu_to_le64(src_off);
1993 dup_ext_buf.TargetFileOffset = cpu_to_le64(dest_off);
1994 dup_ext_buf.ByteCount = cpu_to_le64(len);
Christoph Probsta205d502019-05-08 21:36:25 +02001995 cifs_dbg(FYI, "Duplicate extents: src off %lld dst off %lld len %lld\n",
Steve French02b16662015-06-27 21:18:36 -07001996 src_off, dest_off, len);
1997
1998 rc = smb2_set_file_size(xid, tcon, trgtfile, dest_off + len, false);
1999 if (rc)
2000 goto duplicate_extents_out;
2001
2002 rc = SMB2_ioctl(xid, tcon, trgtfile->fid.persistent_fid,
2003 trgtfile->fid.volatile_fid,
2004 FSCTL_DUPLICATE_EXTENTS_TO_FILE,
Aurelien Aptel63a83b82018-01-24 13:46:11 +01002005 true /* is_fsctl */,
Aurelien Aptel51146622017-02-28 15:08:41 +01002006 (char *)&dup_ext_buf,
Steve French02b16662015-06-27 21:18:36 -07002007 sizeof(struct duplicate_extents_to_file),
Steve French153322f2019-03-28 22:32:49 -05002008 CIFSMaxBufSize, NULL,
Steve French02b16662015-06-27 21:18:36 -07002009 &ret_data_len);
2010
2011 if (ret_data_len > 0)
Christoph Probsta205d502019-05-08 21:36:25 +02002012 cifs_dbg(FYI, "Non-zero response length in duplicate extents\n");
Steve French02b16662015-06-27 21:18:36 -07002013
2014duplicate_extents_out:
2015 return rc;
2016}
Steve French02b16662015-06-27 21:18:36 -07002017
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002018static int
Steve French64a5cfa2013-10-14 15:31:32 -05002019smb2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
2020 struct cifsFileInfo *cfile)
2021{
2022 return SMB2_set_compression(xid, tcon, cfile->fid.persistent_fid,
2023 cfile->fid.volatile_fid);
2024}
2025
2026static int
Steve Frenchb3152e22015-06-24 03:17:02 -05002027smb3_set_integrity(const unsigned int xid, struct cifs_tcon *tcon,
2028 struct cifsFileInfo *cfile)
2029{
2030 struct fsctl_set_integrity_information_req integr_info;
Steve Frenchb3152e22015-06-24 03:17:02 -05002031 unsigned int ret_data_len;
2032
2033 integr_info.ChecksumAlgorithm = cpu_to_le16(CHECKSUM_TYPE_UNCHANGED);
2034 integr_info.Flags = 0;
2035 integr_info.Reserved = 0;
2036
2037 return SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
2038 cfile->fid.volatile_fid,
2039 FSCTL_SET_INTEGRITY_INFORMATION,
Aurelien Aptel63a83b82018-01-24 13:46:11 +01002040 true /* is_fsctl */,
Aurelien Aptel51146622017-02-28 15:08:41 +01002041 (char *)&integr_info,
Steve Frenchb3152e22015-06-24 03:17:02 -05002042 sizeof(struct fsctl_set_integrity_information_req),
Steve French153322f2019-03-28 22:32:49 -05002043 CIFSMaxBufSize, NULL,
Steve Frenchb3152e22015-06-24 03:17:02 -05002044 &ret_data_len);
2045
2046}
2047
Steve Frenche02789a2018-08-09 14:33:12 -05002048/* GMT Token is @GMT-YYYY.MM.DD-HH.MM.SS Unicode which is 48 bytes + null */
2049#define GMT_TOKEN_SIZE 50
2050
Steve French153322f2019-03-28 22:32:49 -05002051#define MIN_SNAPSHOT_ARRAY_SIZE 16 /* See MS-SMB2 section 3.3.5.15.1 */
2052
Steve Frenche02789a2018-08-09 14:33:12 -05002053/*
2054 * Input buffer contains (empty) struct smb_snapshot array with size filled in
2055 * For output see struct SRV_SNAPSHOT_ARRAY in MS-SMB2 section 2.2.32.2
2056 */
Steve Frenchb3152e22015-06-24 03:17:02 -05002057static int
Steve French834170c2016-09-30 21:14:26 -05002058smb3_enum_snapshots(const unsigned int xid, struct cifs_tcon *tcon,
2059 struct cifsFileInfo *cfile, void __user *ioc_buf)
2060{
2061 char *retbuf = NULL;
2062 unsigned int ret_data_len = 0;
2063 int rc;
Steve French153322f2019-03-28 22:32:49 -05002064 u32 max_response_size;
Steve French834170c2016-09-30 21:14:26 -05002065 struct smb_snapshot_array snapshot_in;
2066
Steve French973189a2019-04-04 00:41:04 -05002067 /*
2068 * On the first query to enumerate the list of snapshots available
2069 * for this volume the buffer begins with 0 (number of snapshots
2070 * which can be returned is zero since at that point we do not know
2071 * how big the buffer needs to be). On the second query,
2072 * it (ret_data_len) is set to number of snapshots so we can
2073 * know to set the maximum response size larger (see below).
2074 */
Steve French153322f2019-03-28 22:32:49 -05002075 if (get_user(ret_data_len, (unsigned int __user *)ioc_buf))
2076 return -EFAULT;
2077
2078 /*
2079 * Note that for snapshot queries that servers like Azure expect that
2080 * the first query be minimal size (and just used to get the number/size
2081 * of previous versions) so response size must be specified as EXACTLY
2082 * sizeof(struct snapshot_array) which is 16 when rounded up to multiple
2083 * of eight bytes.
2084 */
2085 if (ret_data_len == 0)
2086 max_response_size = MIN_SNAPSHOT_ARRAY_SIZE;
2087 else
2088 max_response_size = CIFSMaxBufSize;
2089
Steve French834170c2016-09-30 21:14:26 -05002090 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
2091 cfile->fid.volatile_fid,
2092 FSCTL_SRV_ENUMERATE_SNAPSHOTS,
Aurelien Aptel63a83b82018-01-24 13:46:11 +01002093 true /* is_fsctl */,
Steve French153322f2019-03-28 22:32:49 -05002094 NULL, 0 /* no input data */, max_response_size,
Steve French834170c2016-09-30 21:14:26 -05002095 (char **)&retbuf,
2096 &ret_data_len);
2097 cifs_dbg(FYI, "enum snaphots ioctl returned %d and ret buflen is %d\n",
2098 rc, ret_data_len);
2099 if (rc)
2100 return rc;
2101
2102 if (ret_data_len && (ioc_buf != NULL) && (retbuf != NULL)) {
2103 /* Fixup buffer */
2104 if (copy_from_user(&snapshot_in, ioc_buf,
2105 sizeof(struct smb_snapshot_array))) {
2106 rc = -EFAULT;
2107 kfree(retbuf);
2108 return rc;
2109 }
Steve French834170c2016-09-30 21:14:26 -05002110
Steve Frenche02789a2018-08-09 14:33:12 -05002111 /*
2112 * Check for min size, ie not large enough to fit even one GMT
2113 * token (snapshot). On the first ioctl some users may pass in
2114 * smaller size (or zero) to simply get the size of the array
2115 * so the user space caller can allocate sufficient memory
2116 * and retry the ioctl again with larger array size sufficient
2117 * to hold all of the snapshot GMT tokens on the second try.
2118 */
2119 if (snapshot_in.snapshot_array_size < GMT_TOKEN_SIZE)
2120 ret_data_len = sizeof(struct smb_snapshot_array);
2121
2122 /*
2123 * We return struct SRV_SNAPSHOT_ARRAY, followed by
2124 * the snapshot array (of 50 byte GMT tokens) each
2125 * representing an available previous version of the data
2126 */
2127 if (ret_data_len > (snapshot_in.snapshot_array_size +
2128 sizeof(struct smb_snapshot_array)))
2129 ret_data_len = snapshot_in.snapshot_array_size +
2130 sizeof(struct smb_snapshot_array);
Steve French834170c2016-09-30 21:14:26 -05002131
2132 if (copy_to_user(ioc_buf, retbuf, ret_data_len))
2133 rc = -EFAULT;
2134 }
2135
2136 kfree(retbuf);
2137 return rc;
2138}
2139
Steve Frenchd26c2dd2020-02-06 06:00:14 -06002140
2141
2142static int
2143smb3_notify(const unsigned int xid, struct file *pfile,
2144 void __user *ioc_buf)
2145{
2146 struct smb3_notify notify;
2147 struct dentry *dentry = pfile->f_path.dentry;
2148 struct inode *inode = file_inode(pfile);
2149 struct cifs_sb_info *cifs_sb;
2150 struct cifs_open_parms oparms;
2151 struct cifs_fid fid;
2152 struct cifs_tcon *tcon;
2153 unsigned char *path = NULL;
2154 __le16 *utf16_path = NULL;
2155 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2156 int rc = 0;
2157
2158 path = build_path_from_dentry(dentry);
2159 if (path == NULL)
2160 return -ENOMEM;
2161
2162 cifs_sb = CIFS_SB(inode->i_sb);
2163
2164 utf16_path = cifs_convert_path_to_utf16(path + 1, cifs_sb);
2165 if (utf16_path == NULL) {
2166 rc = -ENOMEM;
2167 goto notify_exit;
2168 }
2169
2170 if (copy_from_user(&notify, ioc_buf, sizeof(struct smb3_notify))) {
2171 rc = -EFAULT;
2172 goto notify_exit;
2173 }
2174
2175 tcon = cifs_sb_master_tcon(cifs_sb);
2176 oparms.tcon = tcon;
Steve French4ef9b4f2020-07-07 18:08:46 -05002177 oparms.desired_access = FILE_READ_ATTRIBUTES | FILE_READ_DATA;
Steve Frenchd26c2dd2020-02-06 06:00:14 -06002178 oparms.disposition = FILE_OPEN;
2179 oparms.create_options = cifs_create_options(cifs_sb, 0);
2180 oparms.fid = &fid;
2181 oparms.reconnect = false;
2182
Aurelien Aptel69dda302020-03-02 17:53:22 +01002183 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, NULL,
2184 NULL);
Steve Frenchd26c2dd2020-02-06 06:00:14 -06002185 if (rc)
2186 goto notify_exit;
2187
2188 rc = SMB2_change_notify(xid, tcon, fid.persistent_fid, fid.volatile_fid,
2189 notify.watch_tree, notify.completion_filter);
2190
2191 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
2192
2193 cifs_dbg(FYI, "change notify for path %s rc %d\n", path, rc);
2194
2195notify_exit:
2196 kfree(path);
2197 kfree(utf16_path);
2198 return rc;
2199}
2200
Steve French834170c2016-09-30 21:14:26 -05002201static int
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002202smb2_query_dir_first(const unsigned int xid, struct cifs_tcon *tcon,
2203 const char *path, struct cifs_sb_info *cifs_sb,
2204 struct cifs_fid *fid, __u16 search_flags,
2205 struct cifs_search_info *srch_inf)
2206{
2207 __le16 *utf16_path;
Ronnie Sahlberg37478602020-01-08 13:08:06 +10002208 struct smb_rqst rqst[2];
2209 struct kvec rsp_iov[2];
2210 int resp_buftype[2];
2211 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
2212 struct kvec qd_iov[SMB2_QUERY_DIRECTORY_IOV_SIZE];
2213 int rc, flags = 0;
2214 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04002215 struct cifs_open_parms oparms;
Ronnie Sahlberg37478602020-01-08 13:08:06 +10002216 struct smb2_query_directory_rsp *qd_rsp = NULL;
2217 struct smb2_create_rsp *op_rsp = NULL;
Aurelien Aptel352d96f2020-05-31 12:38:22 -05002218 struct TCP_Server_Info *server = cifs_pick_channel(tcon->ses);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002219
2220 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
2221 if (!utf16_path)
2222 return -ENOMEM;
2223
Ronnie Sahlberg37478602020-01-08 13:08:06 +10002224 if (smb3_encryption_required(tcon))
2225 flags |= CIFS_TRANSFORM_REQ;
2226
2227 memset(rqst, 0, sizeof(rqst));
2228 resp_buftype[0] = resp_buftype[1] = CIFS_NO_BUFFER;
2229 memset(rsp_iov, 0, sizeof(rsp_iov));
2230
2231 /* Open */
2232 memset(&open_iov, 0, sizeof(open_iov));
2233 rqst[0].rq_iov = open_iov;
2234 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
2235
Pavel Shilovsky064f6042013-07-09 18:20:30 +04002236 oparms.tcon = tcon;
2237 oparms.desired_access = FILE_READ_ATTRIBUTES | FILE_READ_DATA;
2238 oparms.disposition = FILE_OPEN;
Amir Goldstein0f060932020-02-03 21:46:43 +02002239 oparms.create_options = cifs_create_options(cifs_sb, 0);
Pavel Shilovsky064f6042013-07-09 18:20:30 +04002240 oparms.fid = fid;
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04002241 oparms.reconnect = false;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04002242
Aurelien Aptel352d96f2020-05-31 12:38:22 -05002243 rc = SMB2_open_init(tcon, server,
2244 &rqst[0], &oplock, &oparms, utf16_path);
Ronnie Sahlberg37478602020-01-08 13:08:06 +10002245 if (rc)
2246 goto qdf_free;
2247 smb2_set_next_command(tcon, &rqst[0]);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002248
Ronnie Sahlberg37478602020-01-08 13:08:06 +10002249 /* Query directory */
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002250 srch_inf->entries_in_buffer = 0;
Aurelien Aptel05957512018-05-17 16:35:07 +02002251 srch_inf->index_of_last_entry = 2;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002252
Ronnie Sahlberg37478602020-01-08 13:08:06 +10002253 memset(&qd_iov, 0, sizeof(qd_iov));
2254 rqst[1].rq_iov = qd_iov;
2255 rqst[1].rq_nvec = SMB2_QUERY_DIRECTORY_IOV_SIZE;
2256
Aurelien Aptel352d96f2020-05-31 12:38:22 -05002257 rc = SMB2_query_directory_init(xid, tcon, server,
2258 &rqst[1],
Ronnie Sahlberg37478602020-01-08 13:08:06 +10002259 COMPOUND_FID, COMPOUND_FID,
2260 0, srch_inf->info_level);
2261 if (rc)
2262 goto qdf_free;
2263
2264 smb2_set_related(&rqst[1]);
2265
Aurelien Aptel352d96f2020-05-31 12:38:22 -05002266 rc = compound_send_recv(xid, tcon->ses, server,
2267 flags, 2, rqst,
Ronnie Sahlberg37478602020-01-08 13:08:06 +10002268 resp_buftype, rsp_iov);
2269
2270 /* If the open failed there is nothing to do */
2271 op_rsp = (struct smb2_create_rsp *)rsp_iov[0].iov_base;
2272 if (op_rsp == NULL || op_rsp->sync_hdr.Status != STATUS_SUCCESS) {
2273 cifs_dbg(FYI, "query_dir_first: open failed rc=%d\n", rc);
2274 goto qdf_free;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002275 }
Ronnie Sahlberg37478602020-01-08 13:08:06 +10002276 fid->persistent_fid = op_rsp->PersistentFileId;
2277 fid->volatile_fid = op_rsp->VolatileFileId;
2278
2279 /* Anything else than ENODATA means a genuine error */
2280 if (rc && rc != -ENODATA) {
2281 SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
2282 cifs_dbg(FYI, "query_dir_first: query directory failed rc=%d\n", rc);
2283 trace_smb3_query_dir_err(xid, fid->persistent_fid,
2284 tcon->tid, tcon->ses->Suid, 0, 0, rc);
2285 goto qdf_free;
2286 }
2287
Shyam Prasad N1be1fa42020-03-09 01:35:09 -07002288 atomic_inc(&tcon->num_remote_opens);
2289
Ronnie Sahlberg37478602020-01-08 13:08:06 +10002290 qd_rsp = (struct smb2_query_directory_rsp *)rsp_iov[1].iov_base;
2291 if (qd_rsp->sync_hdr.Status == STATUS_NO_MORE_FILES) {
2292 trace_smb3_query_dir_done(xid, fid->persistent_fid,
2293 tcon->tid, tcon->ses->Suid, 0, 0);
2294 srch_inf->endOfSearch = true;
2295 rc = 0;
2296 goto qdf_free;
2297 }
2298
2299 rc = smb2_parse_query_directory(tcon, &rsp_iov[1], resp_buftype[1],
2300 srch_inf);
2301 if (rc) {
2302 trace_smb3_query_dir_err(xid, fid->persistent_fid, tcon->tid,
2303 tcon->ses->Suid, 0, 0, rc);
2304 goto qdf_free;
2305 }
2306 resp_buftype[1] = CIFS_NO_BUFFER;
2307
2308 trace_smb3_query_dir_done(xid, fid->persistent_fid, tcon->tid,
2309 tcon->ses->Suid, 0, srch_inf->entries_in_buffer);
2310
2311 qdf_free:
2312 kfree(utf16_path);
2313 SMB2_open_free(&rqst[0]);
2314 SMB2_query_directory_free(&rqst[1]);
2315 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
2316 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002317 return rc;
2318}
2319
2320static int
2321smb2_query_dir_next(const unsigned int xid, struct cifs_tcon *tcon,
2322 struct cifs_fid *fid, __u16 search_flags,
2323 struct cifs_search_info *srch_inf)
2324{
2325 return SMB2_query_directory(xid, tcon, fid->persistent_fid,
2326 fid->volatile_fid, 0, srch_inf);
2327}
2328
2329static int
2330smb2_close_dir(const unsigned int xid, struct cifs_tcon *tcon,
2331 struct cifs_fid *fid)
2332{
2333 return SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
2334}
2335
Pavel Shilovsky2e44b282012-09-18 16:20:33 -07002336/*
Christoph Probsta205d502019-05-08 21:36:25 +02002337 * If we negotiate SMB2 protocol and get STATUS_PENDING - update
2338 * the number of credits and return true. Otherwise - return false.
2339 */
Pavel Shilovsky2e44b282012-09-18 16:20:33 -07002340static bool
Pavel Shilovsky66265f12019-01-23 17:11:16 -08002341smb2_is_status_pending(char *buf, struct TCP_Server_Info *server)
Pavel Shilovsky2e44b282012-09-18 16:20:33 -07002342{
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10002343 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
Pavel Shilovsky2e44b282012-09-18 16:20:33 -07002344
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002345 if (shdr->Status != STATUS_PENDING)
Pavel Shilovsky2e44b282012-09-18 16:20:33 -07002346 return false;
2347
Pavel Shilovsky66265f12019-01-23 17:11:16 -08002348 if (shdr->CreditRequest) {
Pavel Shilovsky2e44b282012-09-18 16:20:33 -07002349 spin_lock(&server->req_lock);
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002350 server->credits += le16_to_cpu(shdr->CreditRequest);
Pavel Shilovsky2e44b282012-09-18 16:20:33 -07002351 spin_unlock(&server->req_lock);
2352 wake_up(&server->request_q);
2353 }
2354
2355 return true;
2356}
2357
Pavel Shilovsky511c54a2017-07-08 14:32:00 -07002358static bool
2359smb2_is_session_expired(char *buf)
2360{
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10002361 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
Pavel Shilovsky511c54a2017-07-08 14:32:00 -07002362
Mark Symsd81243c2018-05-24 09:47:31 +01002363 if (shdr->Status != STATUS_NETWORK_SESSION_EXPIRED &&
2364 shdr->Status != STATUS_USER_SESSION_DELETED)
Pavel Shilovsky511c54a2017-07-08 14:32:00 -07002365 return false;
2366
Steve Frenche68a9322018-07-30 14:23:58 -05002367 trace_smb3_ses_expired(shdr->TreeId, shdr->SessionId,
2368 le16_to_cpu(shdr->Command),
2369 le64_to_cpu(shdr->MessageId));
Mark Symsd81243c2018-05-24 09:47:31 +01002370 cifs_dbg(FYI, "Session expired or deleted\n");
Steve Frenche68a9322018-07-30 14:23:58 -05002371
Pavel Shilovsky511c54a2017-07-08 14:32:00 -07002372 return true;
2373}
2374
Rohith Surabattula8e670f72020-09-18 05:37:28 +00002375static bool
2376smb2_is_status_io_timeout(char *buf)
2377{
2378 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
2379
2380 if (shdr->Status == STATUS_IO_TIMEOUT)
2381 return true;
2382 else
2383 return false;
2384}
2385
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07002386static int
2387smb2_oplock_response(struct cifs_tcon *tcon, struct cifs_fid *fid,
2388 struct cifsInodeInfo *cinode)
2389{
Pavel Shilovsky0822f512012-09-19 06:22:45 -07002390 if (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_LEASING)
2391 return SMB2_lease_break(0, tcon, cinode->lease_key,
2392 smb2_get_lease_state(cinode));
2393
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07002394 return SMB2_oplock_break(0, tcon, fid->persistent_fid,
2395 fid->volatile_fid,
Pavel Shilovsky18cceb62013-09-05 13:01:06 +04002396 CIFS_CACHE_READ(cinode) ? 1 : 0);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07002397}
2398
Ronnie Sahlbergc5a5f382018-09-03 13:33:41 +10002399void
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10002400smb2_set_related(struct smb_rqst *rqst)
2401{
2402 struct smb2_sync_hdr *shdr;
2403
2404 shdr = (struct smb2_sync_hdr *)(rqst->rq_iov[0].iov_base);
Ronnie Sahlberg88a92c92019-07-16 10:41:46 +10002405 if (shdr == NULL) {
2406 cifs_dbg(FYI, "shdr NULL in smb2_set_related\n");
2407 return;
2408 }
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10002409 shdr->Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
2410}
2411
2412char smb2_padding[7] = {0, 0, 0, 0, 0, 0, 0};
2413
Ronnie Sahlbergc5a5f382018-09-03 13:33:41 +10002414void
Ronnie Sahlberge77fe732018-12-31 13:43:40 +10002415smb2_set_next_command(struct cifs_tcon *tcon, struct smb_rqst *rqst)
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10002416{
2417 struct smb2_sync_hdr *shdr;
Ronnie Sahlberge77fe732018-12-31 13:43:40 +10002418 struct cifs_ses *ses = tcon->ses;
2419 struct TCP_Server_Info *server = ses->server;
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10002420 unsigned long len = smb_rqst_len(server, rqst);
Ronnie Sahlberge77fe732018-12-31 13:43:40 +10002421 int i, num_padding;
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10002422
Ronnie Sahlberg88a92c92019-07-16 10:41:46 +10002423 shdr = (struct smb2_sync_hdr *)(rqst->rq_iov[0].iov_base);
2424 if (shdr == NULL) {
2425 cifs_dbg(FYI, "shdr NULL in smb2_set_next_command\n");
2426 return;
2427 }
2428
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10002429 /* SMB headers in a compound are 8 byte aligned. */
Ronnie Sahlberge77fe732018-12-31 13:43:40 +10002430
2431 /* No padding needed */
2432 if (!(len & 7))
2433 goto finished;
2434
2435 num_padding = 8 - (len & 7);
2436 if (!smb3_encryption_required(tcon)) {
2437 /*
2438 * If we do not have encryption then we can just add an extra
2439 * iov for the padding.
2440 */
2441 rqst->rq_iov[rqst->rq_nvec].iov_base = smb2_padding;
2442 rqst->rq_iov[rqst->rq_nvec].iov_len = num_padding;
2443 rqst->rq_nvec++;
2444 len += num_padding;
2445 } else {
2446 /*
2447 * We can not add a small padding iov for the encryption case
2448 * because the encryption framework can not handle the padding
2449 * iovs.
2450 * We have to flatten this into a single buffer and add
2451 * the padding to it.
2452 */
2453 for (i = 1; i < rqst->rq_nvec; i++) {
2454 memcpy(rqst->rq_iov[0].iov_base +
2455 rqst->rq_iov[0].iov_len,
2456 rqst->rq_iov[i].iov_base,
2457 rqst->rq_iov[i].iov_len);
2458 rqst->rq_iov[0].iov_len += rqst->rq_iov[i].iov_len;
Ronnie Sahlberg271b9c02018-12-18 17:49:05 -06002459 }
Ronnie Sahlberge77fe732018-12-31 13:43:40 +10002460 memset(rqst->rq_iov[0].iov_base + rqst->rq_iov[0].iov_len,
2461 0, num_padding);
2462 rqst->rq_iov[0].iov_len += num_padding;
2463 len += num_padding;
2464 rqst->rq_nvec = 1;
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10002465 }
2466
Ronnie Sahlberge77fe732018-12-31 13:43:40 +10002467 finished:
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10002468 shdr->NextCommand = cpu_to_le32(len);
2469}
2470
Ronnie Sahlberg07d3b2e2018-12-20 22:03:04 -06002471/*
2472 * Passes the query info response back to the caller on success.
2473 * Caller need to free this with free_rsp_buf().
2474 */
Ronnie Sahlbergf9793b62018-11-27 09:52:04 +10002475int
Ronnie Sahlberg07d3b2e2018-12-20 22:03:04 -06002476smb2_query_info_compound(const unsigned int xid, struct cifs_tcon *tcon,
2477 __le16 *utf16_path, u32 desired_access,
2478 u32 class, u32 type, u32 output_len,
Ronnie Sahlbergf9793b62018-11-27 09:52:04 +10002479 struct kvec *rsp, int *buftype,
2480 struct cifs_sb_info *cifs_sb)
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07002481{
Ronnie Sahlberg07d3b2e2018-12-20 22:03:04 -06002482 struct cifs_ses *ses = tcon->ses;
Aurelien Aptel352d96f2020-05-31 12:38:22 -05002483 struct TCP_Server_Info *server = cifs_pick_channel(ses);
Ronnie Sahlberg07d3b2e2018-12-20 22:03:04 -06002484 int flags = 0;
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10002485 struct smb_rqst rqst[3];
2486 int resp_buftype[3];
2487 struct kvec rsp_iov[3];
Ronnie Sahlberg4d8dfaf2018-08-21 11:49:21 +10002488 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10002489 struct kvec qi_iov[1];
2490 struct kvec close_iov[1];
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07002491 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04002492 struct cifs_open_parms oparms;
2493 struct cifs_fid fid;
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10002494 int rc;
2495
2496 if (smb3_encryption_required(tcon))
2497 flags |= CIFS_TRANSFORM_REQ;
2498
2499 memset(rqst, 0, sizeof(rqst));
Ronnie Sahlbergc5a5f382018-09-03 13:33:41 +10002500 resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10002501 memset(rsp_iov, 0, sizeof(rsp_iov));
2502
2503 memset(&open_iov, 0, sizeof(open_iov));
2504 rqst[0].rq_iov = open_iov;
Ronnie Sahlberg4d8dfaf2018-08-21 11:49:21 +10002505 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07002506
Pavel Shilovsky064f6042013-07-09 18:20:30 +04002507 oparms.tcon = tcon;
Ronnie Sahlberg07d3b2e2018-12-20 22:03:04 -06002508 oparms.desired_access = desired_access;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04002509 oparms.disposition = FILE_OPEN;
Amir Goldstein0f060932020-02-03 21:46:43 +02002510 oparms.create_options = cifs_create_options(cifs_sb, 0);
Pavel Shilovsky064f6042013-07-09 18:20:30 +04002511 oparms.fid = &fid;
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04002512 oparms.reconnect = false;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04002513
Aurelien Aptel352d96f2020-05-31 12:38:22 -05002514 rc = SMB2_open_init(tcon, server,
2515 &rqst[0], &oplock, &oparms, utf16_path);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07002516 if (rc)
Ronnie Sahlberg07d3b2e2018-12-20 22:03:04 -06002517 goto qic_exit;
Ronnie Sahlberge77fe732018-12-31 13:43:40 +10002518 smb2_set_next_command(tcon, &rqst[0]);
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10002519
2520 memset(&qi_iov, 0, sizeof(qi_iov));
2521 rqst[1].rq_iov = qi_iov;
2522 rqst[1].rq_nvec = 1;
2523
Aurelien Aptel352d96f2020-05-31 12:38:22 -05002524 rc = SMB2_query_info_init(tcon, server,
2525 &rqst[1], COMPOUND_FID, COMPOUND_FID,
Ronnie Sahlberg07d3b2e2018-12-20 22:03:04 -06002526 class, type, 0,
2527 output_len, 0,
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05002528 NULL);
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10002529 if (rc)
Ronnie Sahlberg07d3b2e2018-12-20 22:03:04 -06002530 goto qic_exit;
Ronnie Sahlberge77fe732018-12-31 13:43:40 +10002531 smb2_set_next_command(tcon, &rqst[1]);
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10002532 smb2_set_related(&rqst[1]);
2533
2534 memset(&close_iov, 0, sizeof(close_iov));
2535 rqst[2].rq_iov = close_iov;
2536 rqst[2].rq_nvec = 1;
2537
Aurelien Aptel352d96f2020-05-31 12:38:22 -05002538 rc = SMB2_close_init(tcon, server,
2539 &rqst[2], COMPOUND_FID, COMPOUND_FID, false);
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10002540 if (rc)
Ronnie Sahlberg07d3b2e2018-12-20 22:03:04 -06002541 goto qic_exit;
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10002542 smb2_set_related(&rqst[2]);
2543
Aurelien Aptel352d96f2020-05-31 12:38:22 -05002544 rc = compound_send_recv(xid, ses, server,
2545 flags, 3, rqst,
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10002546 resp_buftype, rsp_iov);
Ronnie Sahlbergf9793b62018-11-27 09:52:04 +10002547 if (rc) {
2548 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
Steve French7dcc82c2019-09-11 00:07:36 -05002549 if (rc == -EREMCHG) {
2550 tcon->need_reconnect = true;
Joe Perchesa0a30362020-04-14 22:42:53 -07002551 pr_warn_once("server share %s deleted\n",
2552 tcon->treeName);
Steve French7dcc82c2019-09-11 00:07:36 -05002553 }
Ronnie Sahlberg07d3b2e2018-12-20 22:03:04 -06002554 goto qic_exit;
Ronnie Sahlbergf9793b62018-11-27 09:52:04 +10002555 }
Ronnie Sahlberg07d3b2e2018-12-20 22:03:04 -06002556 *rsp = rsp_iov[1];
2557 *buftype = resp_buftype[1];
2558
2559 qic_exit:
2560 SMB2_open_free(&rqst[0]);
2561 SMB2_query_info_free(&rqst[1]);
2562 SMB2_close_free(&rqst[2]);
2563 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
2564 free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
2565 return rc;
2566}
2567
2568static int
2569smb2_queryfs(const unsigned int xid, struct cifs_tcon *tcon,
Amir Goldstein0f060932020-02-03 21:46:43 +02002570 struct cifs_sb_info *cifs_sb, struct kstatfs *buf)
Ronnie Sahlberg07d3b2e2018-12-20 22:03:04 -06002571{
2572 struct smb2_query_info_rsp *rsp;
2573 struct smb2_fs_full_size_info *info = NULL;
2574 __le16 utf16_path = 0; /* Null - open root of share */
2575 struct kvec rsp_iov = {NULL, 0};
2576 int buftype = CIFS_NO_BUFFER;
2577 int rc;
2578
2579
2580 rc = smb2_query_info_compound(xid, tcon, &utf16_path,
2581 FILE_READ_ATTRIBUTES,
2582 FS_FULL_SIZE_INFORMATION,
2583 SMB2_O_INFO_FILESYSTEM,
2584 sizeof(struct smb2_fs_full_size_info),
Steve French87f93d82020-02-04 13:02:59 -06002585 &rsp_iov, &buftype, cifs_sb);
Ronnie Sahlberg07d3b2e2018-12-20 22:03:04 -06002586 if (rc)
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10002587 goto qfs_exit;
2588
Ronnie Sahlberg07d3b2e2018-12-20 22:03:04 -06002589 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07002590 buf->f_type = SMB2_MAGIC_NUMBER;
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10002591 info = (struct smb2_fs_full_size_info *)(
2592 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
2593 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
2594 le32_to_cpu(rsp->OutputBufferLength),
Ronnie Sahlberg07d3b2e2018-12-20 22:03:04 -06002595 &rsp_iov,
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10002596 sizeof(struct smb2_fs_full_size_info));
2597 if (!rc)
2598 smb2_copy_fs_info_to_kstatfs(info, buf);
2599
2600qfs_exit:
Ronnie Sahlberg07d3b2e2018-12-20 22:03:04 -06002601 free_rsp_buf(buftype, rsp_iov.iov_base);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07002602 return rc;
2603}
2604
Steve French2d304212018-06-24 23:28:12 -05002605static int
2606smb311_queryfs(const unsigned int xid, struct cifs_tcon *tcon,
Amir Goldstein0f060932020-02-03 21:46:43 +02002607 struct cifs_sb_info *cifs_sb, struct kstatfs *buf)
Steve French2d304212018-06-24 23:28:12 -05002608{
2609 int rc;
2610 __le16 srch_path = 0; /* Null - open root of share */
2611 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2612 struct cifs_open_parms oparms;
2613 struct cifs_fid fid;
2614
2615 if (!tcon->posix_extensions)
Amir Goldstein0f060932020-02-03 21:46:43 +02002616 return smb2_queryfs(xid, tcon, cifs_sb, buf);
Steve French2d304212018-06-24 23:28:12 -05002617
2618 oparms.tcon = tcon;
2619 oparms.desired_access = FILE_READ_ATTRIBUTES;
2620 oparms.disposition = FILE_OPEN;
Amir Goldstein0f060932020-02-03 21:46:43 +02002621 oparms.create_options = cifs_create_options(cifs_sb, 0);
Steve French2d304212018-06-24 23:28:12 -05002622 oparms.fid = &fid;
2623 oparms.reconnect = false;
2624
Aurelien Aptel69dda302020-03-02 17:53:22 +01002625 rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL,
2626 NULL, NULL);
Steve French2d304212018-06-24 23:28:12 -05002627 if (rc)
2628 return rc;
2629
2630 rc = SMB311_posix_qfs_info(xid, tcon, fid.persistent_fid,
2631 fid.volatile_fid, buf);
2632 buf->f_type = SMB2_MAGIC_NUMBER;
2633 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
2634 return rc;
2635}
Steve French2d304212018-06-24 23:28:12 -05002636
Pavel Shilovsky027e8ee2012-09-19 06:22:43 -07002637static bool
2638smb2_compare_fids(struct cifsFileInfo *ob1, struct cifsFileInfo *ob2)
2639{
2640 return ob1->fid.persistent_fid == ob2->fid.persistent_fid &&
2641 ob1->fid.volatile_fid == ob2->fid.volatile_fid;
2642}
2643
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07002644static int
2645smb2_mand_lock(const unsigned int xid, struct cifsFileInfo *cfile, __u64 offset,
2646 __u64 length, __u32 type, int lock, int unlock, bool wait)
2647{
2648 if (unlock && !lock)
2649 type = SMB2_LOCKFLAG_UNLOCK;
2650 return SMB2_lock(xid, tlink_tcon(cfile->tlink),
2651 cfile->fid.persistent_fid, cfile->fid.volatile_fid,
2652 current->tgid, length, offset, type, wait);
2653}
2654
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07002655static void
2656smb2_get_lease_key(struct inode *inode, struct cifs_fid *fid)
2657{
2658 memcpy(fid->lease_key, CIFS_I(inode)->lease_key, SMB2_LEASE_KEY_SIZE);
2659}
2660
2661static void
2662smb2_set_lease_key(struct inode *inode, struct cifs_fid *fid)
2663{
2664 memcpy(CIFS_I(inode)->lease_key, fid->lease_key, SMB2_LEASE_KEY_SIZE);
2665}
2666
2667static void
2668smb2_new_lease_key(struct cifs_fid *fid)
2669{
Steve Frenchfa70b872016-09-22 00:39:34 -05002670 generate_random_uuid(fid->lease_key);
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07002671}
2672
Aurelien Aptel9d496402017-02-13 16:16:49 +01002673static int
2674smb2_get_dfs_refer(const unsigned int xid, struct cifs_ses *ses,
2675 const char *search_name,
2676 struct dfs_info3_param **target_nodes,
2677 unsigned int *num_of_nodes,
2678 const struct nls_table *nls_codepage, int remap)
2679{
2680 int rc;
2681 __le16 *utf16_path = NULL;
2682 int utf16_path_len = 0;
2683 struct cifs_tcon *tcon;
2684 struct fsctl_get_dfs_referral_req *dfs_req = NULL;
2685 struct get_dfs_referral_rsp *dfs_rsp = NULL;
2686 u32 dfs_req_size = 0, dfs_rsp_size = 0;
2687
Christoph Probsta205d502019-05-08 21:36:25 +02002688 cifs_dbg(FYI, "%s: path: %s\n", __func__, search_name);
Aurelien Aptel9d496402017-02-13 16:16:49 +01002689
2690 /*
Aurelien Aptel63a83b82018-01-24 13:46:11 +01002691 * Try to use the IPC tcon, otherwise just use any
Aurelien Aptel9d496402017-02-13 16:16:49 +01002692 */
Aurelien Aptel63a83b82018-01-24 13:46:11 +01002693 tcon = ses->tcon_ipc;
2694 if (tcon == NULL) {
2695 spin_lock(&cifs_tcp_ses_lock);
2696 tcon = list_first_entry_or_null(&ses->tcon_list,
2697 struct cifs_tcon,
2698 tcon_list);
2699 if (tcon)
2700 tcon->tc_count++;
2701 spin_unlock(&cifs_tcp_ses_lock);
2702 }
Aurelien Aptel9d496402017-02-13 16:16:49 +01002703
Aurelien Aptel63a83b82018-01-24 13:46:11 +01002704 if (tcon == NULL) {
Aurelien Aptel9d496402017-02-13 16:16:49 +01002705 cifs_dbg(VFS, "session %p has no tcon available for a dfs referral request\n",
2706 ses);
2707 rc = -ENOTCONN;
2708 goto out;
2709 }
2710
2711 utf16_path = cifs_strndup_to_utf16(search_name, PATH_MAX,
2712 &utf16_path_len,
2713 nls_codepage, remap);
2714 if (!utf16_path) {
2715 rc = -ENOMEM;
2716 goto out;
2717 }
2718
2719 dfs_req_size = sizeof(*dfs_req) + utf16_path_len;
2720 dfs_req = kzalloc(dfs_req_size, GFP_KERNEL);
2721 if (!dfs_req) {
2722 rc = -ENOMEM;
2723 goto out;
2724 }
2725
2726 /* Highest DFS referral version understood */
2727 dfs_req->MaxReferralLevel = DFS_VERSION;
2728
2729 /* Path to resolve in an UTF-16 null-terminated string */
2730 memcpy(dfs_req->RequestFileName, utf16_path, utf16_path_len);
2731
2732 do {
Aurelien Aptel9d496402017-02-13 16:16:49 +01002733 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
2734 FSCTL_DFS_GET_REFERRALS,
Aurelien Aptel63a83b82018-01-24 13:46:11 +01002735 true /* is_fsctl */,
Steve French153322f2019-03-28 22:32:49 -05002736 (char *)dfs_req, dfs_req_size, CIFSMaxBufSize,
Aurelien Aptel9d496402017-02-13 16:16:49 +01002737 (char **)&dfs_rsp, &dfs_rsp_size);
Aurelien Aptel9d496402017-02-13 16:16:49 +01002738 } while (rc == -EAGAIN);
2739
2740 if (rc) {
Steve French2564f2f2018-03-21 23:16:36 -05002741 if ((rc != -ENOENT) && (rc != -EOPNOTSUPP))
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +10002742 cifs_tcon_dbg(VFS, "ioctl error in %s rc=%d\n", __func__, rc);
Aurelien Aptel9d496402017-02-13 16:16:49 +01002743 goto out;
2744 }
2745
2746 rc = parse_dfs_referrals(dfs_rsp, dfs_rsp_size,
2747 num_of_nodes, target_nodes,
2748 nls_codepage, remap, search_name,
2749 true /* is_unicode */);
2750 if (rc) {
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +10002751 cifs_tcon_dbg(VFS, "parse error in %s rc=%d\n", __func__, rc);
Aurelien Aptel9d496402017-02-13 16:16:49 +01002752 goto out;
2753 }
2754
2755 out:
Aurelien Aptel63a83b82018-01-24 13:46:11 +01002756 if (tcon && !tcon->ipc) {
2757 /* ipc tcons are not refcounted */
Aurelien Aptel9d496402017-02-13 16:16:49 +01002758 spin_lock(&cifs_tcp_ses_lock);
2759 tcon->tc_count--;
2760 spin_unlock(&cifs_tcp_ses_lock);
2761 }
2762 kfree(utf16_path);
2763 kfree(dfs_req);
2764 kfree(dfs_rsp);
2765 return rc;
2766}
Ronnie Sahlberg5de254d2019-06-27 14:57:02 +10002767
2768static int
Steve Frenchd5ecebc2019-06-28 02:04:18 -05002769parse_reparse_posix(struct reparse_posix_data *symlink_buf,
2770 u32 plen, char **target_path,
2771 struct cifs_sb_info *cifs_sb)
2772{
2773 unsigned int len;
2774
2775 /* See MS-FSCC 2.1.2.6 for the 'NFS' style reparse tags */
2776 len = le16_to_cpu(symlink_buf->ReparseDataLength);
2777
Steve Frenchd5ecebc2019-06-28 02:04:18 -05002778 if (le64_to_cpu(symlink_buf->InodeType) != NFS_SPECFILE_LNK) {
2779 cifs_dbg(VFS, "%lld not a supported symlink type\n",
2780 le64_to_cpu(symlink_buf->InodeType));
2781 return -EOPNOTSUPP;
2782 }
2783
2784 *target_path = cifs_strndup_from_utf16(
2785 symlink_buf->PathBuffer,
2786 len, true, cifs_sb->local_nls);
2787 if (!(*target_path))
2788 return -ENOMEM;
2789
2790 convert_delimiter(*target_path, '/');
2791 cifs_dbg(FYI, "%s: target path: %s\n", __func__, *target_path);
2792
2793 return 0;
2794}
2795
2796static int
Ronnie Sahlberg5de254d2019-06-27 14:57:02 +10002797parse_reparse_symlink(struct reparse_symlink_data_buffer *symlink_buf,
2798 u32 plen, char **target_path,
2799 struct cifs_sb_info *cifs_sb)
2800{
2801 unsigned int sub_len;
2802 unsigned int sub_offset;
2803
Steve Frenchd5ecebc2019-06-28 02:04:18 -05002804 /* We handle Symbolic Link reparse tag here. See: MS-FSCC 2.1.2.4 */
Ronnie Sahlberg5de254d2019-06-27 14:57:02 +10002805
2806 sub_offset = le16_to_cpu(symlink_buf->SubstituteNameOffset);
2807 sub_len = le16_to_cpu(symlink_buf->SubstituteNameLength);
2808 if (sub_offset + 20 > plen ||
2809 sub_offset + sub_len + 20 > plen) {
2810 cifs_dbg(VFS, "srv returned malformed symlink buffer\n");
2811 return -EIO;
2812 }
2813
2814 *target_path = cifs_strndup_from_utf16(
2815 symlink_buf->PathBuffer + sub_offset,
2816 sub_len, true, cifs_sb->local_nls);
2817 if (!(*target_path))
2818 return -ENOMEM;
2819
2820 convert_delimiter(*target_path, '/');
2821 cifs_dbg(FYI, "%s: target path: %s\n", __func__, *target_path);
2822
2823 return 0;
2824}
2825
Steve Frenchd5ecebc2019-06-28 02:04:18 -05002826static int
Ronnie Sahlbergf5f111c2019-07-07 07:45:42 +10002827parse_reparse_point(struct reparse_data_buffer *buf,
2828 u32 plen, char **target_path,
2829 struct cifs_sb_info *cifs_sb)
Steve Frenchd5ecebc2019-06-28 02:04:18 -05002830{
Ronnie Sahlbergf5f111c2019-07-07 07:45:42 +10002831 if (plen < sizeof(struct reparse_data_buffer)) {
Joe Perchesa0a30362020-04-14 22:42:53 -07002832 cifs_dbg(VFS, "reparse buffer is too small. Must be at least 8 bytes but was %d\n",
2833 plen);
Ronnie Sahlbergf5f111c2019-07-07 07:45:42 +10002834 return -EIO;
2835 }
2836
2837 if (plen < le16_to_cpu(buf->ReparseDataLength) +
2838 sizeof(struct reparse_data_buffer)) {
Joe Perchesa0a30362020-04-14 22:42:53 -07002839 cifs_dbg(VFS, "srv returned invalid reparse buf length: %d\n",
2840 plen);
Ronnie Sahlbergf5f111c2019-07-07 07:45:42 +10002841 return -EIO;
2842 }
2843
Steve Frenchd5ecebc2019-06-28 02:04:18 -05002844 /* See MS-FSCC 2.1.2 */
Ronnie Sahlbergf5f111c2019-07-07 07:45:42 +10002845 switch (le32_to_cpu(buf->ReparseTag)) {
2846 case IO_REPARSE_TAG_NFS:
2847 return parse_reparse_posix(
2848 (struct reparse_posix_data *)buf,
Steve Frenchd5ecebc2019-06-28 02:04:18 -05002849 plen, target_path, cifs_sb);
Ronnie Sahlbergf5f111c2019-07-07 07:45:42 +10002850 case IO_REPARSE_TAG_SYMLINK:
2851 return parse_reparse_symlink(
2852 (struct reparse_symlink_data_buffer *)buf,
2853 plen, target_path, cifs_sb);
2854 default:
Joe Perchesa0a30362020-04-14 22:42:53 -07002855 cifs_dbg(VFS, "srv returned unknown symlink buffer tag:0x%08x\n",
2856 le32_to_cpu(buf->ReparseTag));
Ronnie Sahlbergf5f111c2019-07-07 07:45:42 +10002857 return -EOPNOTSUPP;
2858 }
Steve Frenchd5ecebc2019-06-28 02:04:18 -05002859}
2860
Pavel Shilovsky78932422016-07-24 10:37:38 +03002861#define SMB2_SYMLINK_STRUCT_SIZE \
2862 (sizeof(struct smb2_err_rsp) - 1 + sizeof(struct smb2_symlink_err_rsp))
2863
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04002864static int
2865smb2_query_symlink(const unsigned int xid, struct cifs_tcon *tcon,
Ronnie Sahlbergebaf5462019-04-10 08:44:46 +10002866 struct cifs_sb_info *cifs_sb, const char *full_path,
2867 char **target_path, bool is_reparse_point)
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04002868{
2869 int rc;
Ronnie Sahlbergebaf5462019-04-10 08:44:46 +10002870 __le16 *utf16_path = NULL;
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04002871 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2872 struct cifs_open_parms oparms;
2873 struct cifs_fid fid;
Ronnie Sahlberg91cb74f2018-04-13 09:03:19 +10002874 struct kvec err_iov = {NULL, 0};
Ronnie Sahlberg9d874c32018-06-08 13:21:18 +10002875 struct smb2_err_rsp *err_buf = NULL;
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04002876 struct smb2_symlink_err_rsp *symlink;
Aurelien Aptel352d96f2020-05-31 12:38:22 -05002877 struct TCP_Server_Info *server = cifs_pick_channel(tcon->ses);
Pavel Shilovsky78932422016-07-24 10:37:38 +03002878 unsigned int sub_len;
2879 unsigned int sub_offset;
2880 unsigned int print_len;
2881 unsigned int print_offset;
Ronnie Sahlbergebaf5462019-04-10 08:44:46 +10002882 int flags = 0;
2883 struct smb_rqst rqst[3];
2884 int resp_buftype[3];
2885 struct kvec rsp_iov[3];
2886 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
2887 struct kvec io_iov[SMB2_IOCTL_IOV_SIZE];
2888 struct kvec close_iov[1];
2889 struct smb2_create_rsp *create_rsp;
2890 struct smb2_ioctl_rsp *ioctl_rsp;
Ronnie Sahlberg5de254d2019-06-27 14:57:02 +10002891 struct reparse_data_buffer *reparse_buf;
Amir Goldstein0f060932020-02-03 21:46:43 +02002892 int create_options = is_reparse_point ? OPEN_REPARSE_POINT : 0;
Ronnie Sahlbergebaf5462019-04-10 08:44:46 +10002893 u32 plen;
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04002894
2895 cifs_dbg(FYI, "%s: path: %s\n", __func__, full_path);
2896
Ronnie Sahlberg5de254d2019-06-27 14:57:02 +10002897 *target_path = NULL;
2898
Ronnie Sahlbergebaf5462019-04-10 08:44:46 +10002899 if (smb3_encryption_required(tcon))
2900 flags |= CIFS_TRANSFORM_REQ;
2901
2902 memset(rqst, 0, sizeof(rqst));
2903 resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
2904 memset(rsp_iov, 0, sizeof(rsp_iov));
2905
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04002906 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
2907 if (!utf16_path)
2908 return -ENOMEM;
2909
Ronnie Sahlbergebaf5462019-04-10 08:44:46 +10002910 /* Open */
2911 memset(&open_iov, 0, sizeof(open_iov));
2912 rqst[0].rq_iov = open_iov;
2913 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
2914
2915 memset(&oparms, 0, sizeof(oparms));
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04002916 oparms.tcon = tcon;
2917 oparms.desired_access = FILE_READ_ATTRIBUTES;
2918 oparms.disposition = FILE_OPEN;
Amir Goldstein0f060932020-02-03 21:46:43 +02002919 oparms.create_options = cifs_create_options(cifs_sb, create_options);
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04002920 oparms.fid = &fid;
2921 oparms.reconnect = false;
2922
Aurelien Aptel352d96f2020-05-31 12:38:22 -05002923 rc = SMB2_open_init(tcon, server,
2924 &rqst[0], &oplock, &oparms, utf16_path);
Ronnie Sahlbergebaf5462019-04-10 08:44:46 +10002925 if (rc)
2926 goto querty_exit;
2927 smb2_set_next_command(tcon, &rqst[0]);
2928
2929
2930 /* IOCTL */
2931 memset(&io_iov, 0, sizeof(io_iov));
2932 rqst[1].rq_iov = io_iov;
2933 rqst[1].rq_nvec = SMB2_IOCTL_IOV_SIZE;
2934
Aurelien Aptel352d96f2020-05-31 12:38:22 -05002935 rc = SMB2_ioctl_init(tcon, server,
2936 &rqst[1], fid.persistent_fid,
Ronnie Sahlbergebaf5462019-04-10 08:44:46 +10002937 fid.volatile_fid, FSCTL_GET_REPARSE_POINT,
Ronnie Sahlberg731b82b2020-01-08 13:08:07 +10002938 true /* is_fctl */, NULL, 0,
2939 CIFSMaxBufSize -
2940 MAX_SMB2_CREATE_RESPONSE_SIZE -
2941 MAX_SMB2_CLOSE_RESPONSE_SIZE);
Ronnie Sahlbergebaf5462019-04-10 08:44:46 +10002942 if (rc)
2943 goto querty_exit;
2944
2945 smb2_set_next_command(tcon, &rqst[1]);
2946 smb2_set_related(&rqst[1]);
2947
2948
2949 /* Close */
2950 memset(&close_iov, 0, sizeof(close_iov));
2951 rqst[2].rq_iov = close_iov;
2952 rqst[2].rq_nvec = 1;
2953
Aurelien Aptel352d96f2020-05-31 12:38:22 -05002954 rc = SMB2_close_init(tcon, server,
2955 &rqst[2], COMPOUND_FID, COMPOUND_FID, false);
Ronnie Sahlbergebaf5462019-04-10 08:44:46 +10002956 if (rc)
2957 goto querty_exit;
2958
2959 smb2_set_related(&rqst[2]);
2960
Aurelien Aptel352d96f2020-05-31 12:38:22 -05002961 rc = compound_send_recv(xid, tcon->ses, server,
2962 flags, 3, rqst,
Ronnie Sahlbergebaf5462019-04-10 08:44:46 +10002963 resp_buftype, rsp_iov);
2964
2965 create_rsp = rsp_iov[0].iov_base;
2966 if (create_rsp && create_rsp->sync_hdr.Status)
2967 err_iov = rsp_iov[0];
2968 ioctl_rsp = rsp_iov[1].iov_base;
2969
2970 /*
2971 * Open was successful and we got an ioctl response.
2972 */
2973 if ((rc == 0) && (is_reparse_point)) {
2974 /* See MS-FSCC 2.3.23 */
2975
Ronnie Sahlberg5de254d2019-06-27 14:57:02 +10002976 reparse_buf = (struct reparse_data_buffer *)
2977 ((char *)ioctl_rsp +
2978 le32_to_cpu(ioctl_rsp->OutputOffset));
Ronnie Sahlbergebaf5462019-04-10 08:44:46 +10002979 plen = le32_to_cpu(ioctl_rsp->OutputCount);
2980
2981 if (plen + le32_to_cpu(ioctl_rsp->OutputOffset) >
2982 rsp_iov[1].iov_len) {
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +10002983 cifs_tcon_dbg(VFS, "srv returned invalid ioctl len: %d\n",
Ronnie Sahlberg5de254d2019-06-27 14:57:02 +10002984 plen);
Ronnie Sahlbergebaf5462019-04-10 08:44:46 +10002985 rc = -EIO;
2986 goto querty_exit;
2987 }
2988
Ronnie Sahlbergf5f111c2019-07-07 07:45:42 +10002989 rc = parse_reparse_point(reparse_buf, plen, target_path,
2990 cifs_sb);
Ronnie Sahlbergebaf5462019-04-10 08:44:46 +10002991 goto querty_exit;
2992 }
2993
Gustavo A. R. Silva0d568cd2018-04-13 10:13:29 -05002994 if (!rc || !err_iov.iov_base) {
Ronnie Sahlberg9d874c32018-06-08 13:21:18 +10002995 rc = -ENOENT;
Ronnie Sahlbergebaf5462019-04-10 08:44:46 +10002996 goto querty_exit;
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04002997 }
Pavel Shilovsky78932422016-07-24 10:37:38 +03002998
Ronnie Sahlberg91cb74f2018-04-13 09:03:19 +10002999 err_buf = err_iov.iov_base;
Pavel Shilovsky78932422016-07-24 10:37:38 +03003000 if (le32_to_cpu(err_buf->ByteCount) < sizeof(struct smb2_symlink_err_rsp) ||
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10003001 err_iov.iov_len < SMB2_SYMLINK_STRUCT_SIZE) {
Ronnie Sahlbergdf070af2019-07-09 18:41:11 +10003002 rc = -EINVAL;
3003 goto querty_exit;
3004 }
3005
3006 symlink = (struct smb2_symlink_err_rsp *)err_buf->ErrorData;
3007 if (le32_to_cpu(symlink->SymLinkErrorTag) != SYMLINK_ERROR_TAG ||
3008 le32_to_cpu(symlink->ReparseTag) != IO_REPARSE_TAG_SYMLINK) {
3009 rc = -EINVAL;
Ronnie Sahlberg9d874c32018-06-08 13:21:18 +10003010 goto querty_exit;
Pavel Shilovsky78932422016-07-24 10:37:38 +03003011 }
3012
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04003013 /* open must fail on symlink - reset rc */
3014 rc = 0;
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04003015 sub_len = le16_to_cpu(symlink->SubstituteNameLength);
3016 sub_offset = le16_to_cpu(symlink->SubstituteNameOffset);
Pavel Shilovsky78932422016-07-24 10:37:38 +03003017 print_len = le16_to_cpu(symlink->PrintNameLength);
3018 print_offset = le16_to_cpu(symlink->PrintNameOffset);
3019
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10003020 if (err_iov.iov_len < SMB2_SYMLINK_STRUCT_SIZE + sub_offset + sub_len) {
Ronnie Sahlbergdf070af2019-07-09 18:41:11 +10003021 rc = -EINVAL;
Ronnie Sahlberg9d874c32018-06-08 13:21:18 +10003022 goto querty_exit;
Pavel Shilovsky78932422016-07-24 10:37:38 +03003023 }
3024
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10003025 if (err_iov.iov_len <
3026 SMB2_SYMLINK_STRUCT_SIZE + print_offset + print_len) {
Ronnie Sahlbergdf070af2019-07-09 18:41:11 +10003027 rc = -EINVAL;
Ronnie Sahlberg9d874c32018-06-08 13:21:18 +10003028 goto querty_exit;
Pavel Shilovsky78932422016-07-24 10:37:38 +03003029 }
3030
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04003031 *target_path = cifs_strndup_from_utf16(
3032 (char *)symlink->PathBuffer + sub_offset,
3033 sub_len, true, cifs_sb->local_nls);
3034 if (!(*target_path)) {
Ronnie Sahlberg9d874c32018-06-08 13:21:18 +10003035 rc = -ENOMEM;
3036 goto querty_exit;
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04003037 }
3038 convert_delimiter(*target_path, '/');
3039 cifs_dbg(FYI, "%s: target path: %s\n", __func__, *target_path);
Ronnie Sahlberg9d874c32018-06-08 13:21:18 +10003040
3041 querty_exit:
Ronnie Sahlbergebaf5462019-04-10 08:44:46 +10003042 cifs_dbg(FYI, "query symlink rc %d\n", rc);
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04003043 kfree(utf16_path);
Ronnie Sahlbergebaf5462019-04-10 08:44:46 +10003044 SMB2_open_free(&rqst[0]);
3045 SMB2_ioctl_free(&rqst[1]);
3046 SMB2_close_free(&rqst[2]);
3047 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
3048 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
3049 free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04003050 return rc;
3051}
3052
Steve French2e4564b2020-10-22 22:03:14 -05003053int
3054smb2_query_reparse_tag(const unsigned int xid, struct cifs_tcon *tcon,
3055 struct cifs_sb_info *cifs_sb, const char *full_path,
3056 __u32 *tag)
3057{
3058 int rc;
3059 __le16 *utf16_path = NULL;
3060 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
3061 struct cifs_open_parms oparms;
3062 struct cifs_fid fid;
Steve French2e4564b2020-10-22 22:03:14 -05003063 struct TCP_Server_Info *server = cifs_pick_channel(tcon->ses);
3064 int flags = 0;
3065 struct smb_rqst rqst[3];
3066 int resp_buftype[3];
3067 struct kvec rsp_iov[3];
3068 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
3069 struct kvec io_iov[SMB2_IOCTL_IOV_SIZE];
3070 struct kvec close_iov[1];
Steve French2e4564b2020-10-22 22:03:14 -05003071 struct smb2_ioctl_rsp *ioctl_rsp;
3072 struct reparse_data_buffer *reparse_buf;
3073 u32 plen;
3074
3075 cifs_dbg(FYI, "%s: path: %s\n", __func__, full_path);
3076
3077 if (smb3_encryption_required(tcon))
3078 flags |= CIFS_TRANSFORM_REQ;
3079
3080 memset(rqst, 0, sizeof(rqst));
3081 resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
3082 memset(rsp_iov, 0, sizeof(rsp_iov));
3083
3084 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
3085 if (!utf16_path)
3086 return -ENOMEM;
3087
3088 /*
3089 * setup smb2open - TODO add optimization to call cifs_get_readable_path
3090 * to see if there is a handle already open that we can use
3091 */
3092 memset(&open_iov, 0, sizeof(open_iov));
3093 rqst[0].rq_iov = open_iov;
3094 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
3095
3096 memset(&oparms, 0, sizeof(oparms));
3097 oparms.tcon = tcon;
3098 oparms.desired_access = FILE_READ_ATTRIBUTES;
3099 oparms.disposition = FILE_OPEN;
3100 oparms.create_options = cifs_create_options(cifs_sb, OPEN_REPARSE_POINT);
3101 oparms.fid = &fid;
3102 oparms.reconnect = false;
3103
3104 rc = SMB2_open_init(tcon, server,
3105 &rqst[0], &oplock, &oparms, utf16_path);
3106 if (rc)
3107 goto query_rp_exit;
3108 smb2_set_next_command(tcon, &rqst[0]);
3109
3110
3111 /* IOCTL */
3112 memset(&io_iov, 0, sizeof(io_iov));
3113 rqst[1].rq_iov = io_iov;
3114 rqst[1].rq_nvec = SMB2_IOCTL_IOV_SIZE;
3115
3116 rc = SMB2_ioctl_init(tcon, server,
Namjae Jeon79631782020-12-03 12:31:36 +09003117 &rqst[1], COMPOUND_FID,
3118 COMPOUND_FID, FSCTL_GET_REPARSE_POINT,
Steve French2e4564b2020-10-22 22:03:14 -05003119 true /* is_fctl */, NULL, 0,
3120 CIFSMaxBufSize -
3121 MAX_SMB2_CREATE_RESPONSE_SIZE -
3122 MAX_SMB2_CLOSE_RESPONSE_SIZE);
3123 if (rc)
3124 goto query_rp_exit;
3125
3126 smb2_set_next_command(tcon, &rqst[1]);
3127 smb2_set_related(&rqst[1]);
3128
3129
3130 /* Close */
3131 memset(&close_iov, 0, sizeof(close_iov));
3132 rqst[2].rq_iov = close_iov;
3133 rqst[2].rq_nvec = 1;
3134
3135 rc = SMB2_close_init(tcon, server,
3136 &rqst[2], COMPOUND_FID, COMPOUND_FID, false);
3137 if (rc)
3138 goto query_rp_exit;
3139
3140 smb2_set_related(&rqst[2]);
3141
3142 rc = compound_send_recv(xid, tcon->ses, server,
3143 flags, 3, rqst,
3144 resp_buftype, rsp_iov);
3145
Steve French2e4564b2020-10-22 22:03:14 -05003146 ioctl_rsp = rsp_iov[1].iov_base;
3147
3148 /*
3149 * Open was successful and we got an ioctl response.
3150 */
3151 if (rc == 0) {
3152 /* See MS-FSCC 2.3.23 */
3153
3154 reparse_buf = (struct reparse_data_buffer *)
3155 ((char *)ioctl_rsp +
3156 le32_to_cpu(ioctl_rsp->OutputOffset));
3157 plen = le32_to_cpu(ioctl_rsp->OutputCount);
3158
3159 if (plen + le32_to_cpu(ioctl_rsp->OutputOffset) >
3160 rsp_iov[1].iov_len) {
3161 cifs_tcon_dbg(FYI, "srv returned invalid ioctl len: %d\n",
3162 plen);
3163 rc = -EIO;
3164 goto query_rp_exit;
3165 }
3166 *tag = le32_to_cpu(reparse_buf->ReparseTag);
3167 }
3168
3169 query_rp_exit:
3170 kfree(utf16_path);
3171 SMB2_open_free(&rqst[0]);
3172 SMB2_ioctl_free(&rqst[1]);
3173 SMB2_close_free(&rqst[2]);
3174 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
3175 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
3176 free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
3177 return rc;
3178}
3179
Shirish Pargaonkar2f1afe22017-06-22 22:52:05 -05003180static struct cifs_ntsd *
3181get_smb2_acl_by_fid(struct cifs_sb_info *cifs_sb,
3182 const struct cifs_fid *cifsfid, u32 *pacllen)
3183{
3184 struct cifs_ntsd *pntsd = NULL;
3185 unsigned int xid;
3186 int rc = -EOPNOTSUPP;
3187 struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
3188
3189 if (IS_ERR(tlink))
3190 return ERR_CAST(tlink);
3191
3192 xid = get_xid();
3193 cifs_dbg(FYI, "trying to get acl\n");
3194
3195 rc = SMB2_query_acl(xid, tlink_tcon(tlink), cifsfid->persistent_fid,
3196 cifsfid->volatile_fid, (void **)&pntsd, pacllen);
3197 free_xid(xid);
3198
3199 cifs_put_tlink(tlink);
3200
3201 cifs_dbg(FYI, "%s: rc = %d ACL len %d\n", __func__, rc, *pacllen);
3202 if (rc)
3203 return ERR_PTR(rc);
3204 return pntsd;
3205
3206}
3207
3208static struct cifs_ntsd *
3209get_smb2_acl_by_path(struct cifs_sb_info *cifs_sb,
3210 const char *path, u32 *pacllen)
3211{
3212 struct cifs_ntsd *pntsd = NULL;
3213 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
3214 unsigned int xid;
3215 int rc;
3216 struct cifs_tcon *tcon;
3217 struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
3218 struct cifs_fid fid;
3219 struct cifs_open_parms oparms;
3220 __le16 *utf16_path;
3221
3222 cifs_dbg(FYI, "get smb3 acl for path %s\n", path);
3223 if (IS_ERR(tlink))
3224 return ERR_CAST(tlink);
3225
3226 tcon = tlink_tcon(tlink);
3227 xid = get_xid();
3228
Shirish Pargaonkar2f1afe22017-06-22 22:52:05 -05003229 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
Steve Frenchcfe89092018-05-19 02:04:55 -05003230 if (!utf16_path) {
3231 rc = -ENOMEM;
3232 free_xid(xid);
3233 return ERR_PTR(rc);
3234 }
Shirish Pargaonkar2f1afe22017-06-22 22:52:05 -05003235
3236 oparms.tcon = tcon;
3237 oparms.desired_access = READ_CONTROL;
3238 oparms.disposition = FILE_OPEN;
Steve French3c3317d2020-10-21 13:12:08 -05003239 /*
3240 * When querying an ACL, even if the file is a symlink we want to open
3241 * the source not the target, and so the protocol requires that the
3242 * client specify this flag when opening a reparse point
3243 */
3244 oparms.create_options = cifs_create_options(cifs_sb, 0) | OPEN_REPARSE_POINT;
Shirish Pargaonkar2f1afe22017-06-22 22:52:05 -05003245 oparms.fid = &fid;
3246 oparms.reconnect = false;
3247
Aurelien Aptel69dda302020-03-02 17:53:22 +01003248 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, NULL,
3249 NULL);
Shirish Pargaonkar2f1afe22017-06-22 22:52:05 -05003250 kfree(utf16_path);
3251 if (!rc) {
3252 rc = SMB2_query_acl(xid, tlink_tcon(tlink), fid.persistent_fid,
3253 fid.volatile_fid, (void **)&pntsd, pacllen);
3254 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
3255 }
3256
3257 cifs_put_tlink(tlink);
3258 free_xid(xid);
3259
3260 cifs_dbg(FYI, "%s: rc = %d ACL len %d\n", __func__, rc, *pacllen);
3261 if (rc)
3262 return ERR_PTR(rc);
3263 return pntsd;
3264}
3265
Shirish Pargaonkar366ed842017-06-28 22:37:32 -05003266static int
3267set_smb2_acl(struct cifs_ntsd *pnntsd, __u32 acllen,
3268 struct inode *inode, const char *path, int aclflag)
3269{
3270 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
3271 unsigned int xid;
3272 int rc, access_flags = 0;
3273 struct cifs_tcon *tcon;
3274 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
3275 struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
3276 struct cifs_fid fid;
3277 struct cifs_open_parms oparms;
3278 __le16 *utf16_path;
3279
3280 cifs_dbg(FYI, "set smb3 acl for path %s\n", path);
3281 if (IS_ERR(tlink))
3282 return PTR_ERR(tlink);
3283
3284 tcon = tlink_tcon(tlink);
3285 xid = get_xid();
3286
Shirish Pargaonkar366ed842017-06-28 22:37:32 -05003287 if (aclflag == CIFS_ACL_OWNER || aclflag == CIFS_ACL_GROUP)
3288 access_flags = WRITE_OWNER;
3289 else
3290 access_flags = WRITE_DAC;
3291
3292 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
Steve Frenchcfe89092018-05-19 02:04:55 -05003293 if (!utf16_path) {
3294 rc = -ENOMEM;
3295 free_xid(xid);
3296 return rc;
3297 }
Shirish Pargaonkar366ed842017-06-28 22:37:32 -05003298
3299 oparms.tcon = tcon;
3300 oparms.desired_access = access_flags;
Amir Goldstein0f060932020-02-03 21:46:43 +02003301 oparms.create_options = cifs_create_options(cifs_sb, 0);
Shirish Pargaonkar366ed842017-06-28 22:37:32 -05003302 oparms.disposition = FILE_OPEN;
3303 oparms.path = path;
3304 oparms.fid = &fid;
3305 oparms.reconnect = false;
3306
Aurelien Aptel69dda302020-03-02 17:53:22 +01003307 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL,
3308 NULL, NULL);
Shirish Pargaonkar366ed842017-06-28 22:37:32 -05003309 kfree(utf16_path);
3310 if (!rc) {
3311 rc = SMB2_set_acl(xid, tlink_tcon(tlink), fid.persistent_fid,
3312 fid.volatile_fid, pnntsd, acllen, aclflag);
3313 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
3314 }
3315
3316 cifs_put_tlink(tlink);
3317 free_xid(xid);
3318 return rc;
3319}
Shirish Pargaonkar366ed842017-06-28 22:37:32 -05003320
Shirish Pargaonkar2f1afe22017-06-22 22:52:05 -05003321/* Retrieve an ACL from the server */
3322static struct cifs_ntsd *
3323get_smb2_acl(struct cifs_sb_info *cifs_sb,
3324 struct inode *inode, const char *path,
3325 u32 *pacllen)
3326{
3327 struct cifs_ntsd *pntsd = NULL;
3328 struct cifsFileInfo *open_file = NULL;
3329
3330 if (inode)
3331 open_file = find_readable_file(CIFS_I(inode), true);
3332 if (!open_file)
3333 return get_smb2_acl_by_path(cifs_sb, path, pacllen);
3334
3335 pntsd = get_smb2_acl_by_fid(cifs_sb, &open_file->fid, pacllen);
3336 cifsFileInfo_put(open_file);
3337 return pntsd;
3338}
Shirish Pargaonkar2f1afe22017-06-22 22:52:05 -05003339
Steve French30175622014-08-17 18:16:40 -05003340static long smb3_zero_range(struct file *file, struct cifs_tcon *tcon,
3341 loff_t offset, loff_t len, bool keep_size)
3342{
Ronnie Sahlberg72c419d2019-03-13 14:37:49 +10003343 struct cifs_ses *ses = tcon->ses;
Steve French30175622014-08-17 18:16:40 -05003344 struct inode *inode;
3345 struct cifsInodeInfo *cifsi;
3346 struct cifsFileInfo *cfile = file->private_data;
3347 struct file_zero_data_information fsctl_buf;
3348 long rc;
3349 unsigned int xid;
Ronnie Sahlberg72c419d2019-03-13 14:37:49 +10003350 __le64 eof;
Steve French30175622014-08-17 18:16:40 -05003351
3352 xid = get_xid();
3353
David Howells2b0143b2015-03-17 22:25:59 +00003354 inode = d_inode(cfile->dentry);
Steve French30175622014-08-17 18:16:40 -05003355 cifsi = CIFS_I(inode);
3356
Christoph Probsta205d502019-05-08 21:36:25 +02003357 trace_smb3_zero_enter(xid, cfile->fid.persistent_fid, tcon->tid,
Steve French779ede02019-03-13 01:41:49 -05003358 ses->Suid, offset, len);
3359
Zhang Xiaoxu6b690402020-06-23 07:31:54 -04003360 /*
3361 * We zero the range through ioctl, so we need remove the page caches
3362 * first, otherwise the data may be inconsistent with the server.
3363 */
3364 truncate_pagecache_range(inode, offset, offset + len - 1);
Steve French779ede02019-03-13 01:41:49 -05003365
Steve French30175622014-08-17 18:16:40 -05003366 /* if file not oplocked can't be sure whether asking to extend size */
3367 if (!CIFS_CACHE_READ(cifsi))
Steve Frenchcfe89092018-05-19 02:04:55 -05003368 if (keep_size == false) {
3369 rc = -EOPNOTSUPP;
Steve French779ede02019-03-13 01:41:49 -05003370 trace_smb3_zero_err(xid, cfile->fid.persistent_fid,
3371 tcon->tid, ses->Suid, offset, len, rc);
Steve Frenchcfe89092018-05-19 02:04:55 -05003372 free_xid(xid);
3373 return rc;
3374 }
Steve French30175622014-08-17 18:16:40 -05003375
Steve Frenchd1c35af2019-05-09 00:09:37 -05003376 cifs_dbg(FYI, "Offset %lld len %lld\n", offset, len);
Steve French30175622014-08-17 18:16:40 -05003377
3378 fsctl_buf.FileOffset = cpu_to_le64(offset);
3379 fsctl_buf.BeyondFinalZero = cpu_to_le64(offset + len);
3380
Ronnie Sahlbergc4250142019-05-02 15:52:57 +10003381 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
3382 cfile->fid.volatile_fid, FSCTL_SET_ZERO_DATA, true,
3383 (char *)&fsctl_buf,
3384 sizeof(struct file_zero_data_information),
3385 0, NULL, NULL);
Ronnie Sahlberg72c419d2019-03-13 14:37:49 +10003386 if (rc)
3387 goto zero_range_exit;
3388
3389 /*
3390 * do we also need to change the size of the file?
3391 */
3392 if (keep_size == false && i_size_read(inode) < offset + len) {
Ronnie Sahlberg72c419d2019-03-13 14:37:49 +10003393 eof = cpu_to_le64(offset + len);
Ronnie Sahlbergc4250142019-05-02 15:52:57 +10003394 rc = SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
3395 cfile->fid.volatile_fid, cfile->pid, &eof);
Ronnie Sahlberg72c419d2019-03-13 14:37:49 +10003396 }
3397
Ronnie Sahlberg72c419d2019-03-13 14:37:49 +10003398 zero_range_exit:
Steve French30175622014-08-17 18:16:40 -05003399 free_xid(xid);
Steve French779ede02019-03-13 01:41:49 -05003400 if (rc)
3401 trace_smb3_zero_err(xid, cfile->fid.persistent_fid, tcon->tid,
3402 ses->Suid, offset, len, rc);
3403 else
3404 trace_smb3_zero_done(xid, cfile->fid.persistent_fid, tcon->tid,
3405 ses->Suid, offset, len);
Steve French30175622014-08-17 18:16:40 -05003406 return rc;
3407}
3408
Steve French31742c52014-08-17 08:38:47 -05003409static long smb3_punch_hole(struct file *file, struct cifs_tcon *tcon,
3410 loff_t offset, loff_t len)
3411{
3412 struct inode *inode;
Steve French31742c52014-08-17 08:38:47 -05003413 struct cifsFileInfo *cfile = file->private_data;
3414 struct file_zero_data_information fsctl_buf;
3415 long rc;
3416 unsigned int xid;
3417 __u8 set_sparse = 1;
3418
3419 xid = get_xid();
3420
David Howells2b0143b2015-03-17 22:25:59 +00003421 inode = d_inode(cfile->dentry);
Steve French31742c52014-08-17 08:38:47 -05003422
3423 /* Need to make file sparse, if not already, before freeing range. */
3424 /* Consider adding equivalent for compressed since it could also work */
Steve Frenchcfe89092018-05-19 02:04:55 -05003425 if (!smb2_set_sparse(xid, tcon, cfile, inode, set_sparse)) {
3426 rc = -EOPNOTSUPP;
3427 free_xid(xid);
3428 return rc;
3429 }
Steve French31742c52014-08-17 08:38:47 -05003430
Zhang Xiaoxuacc91c22020-06-23 07:31:53 -04003431 /*
3432 * We implement the punch hole through ioctl, so we need remove the page
3433 * caches first, otherwise the data may be inconsistent with the server.
3434 */
3435 truncate_pagecache_range(inode, offset, offset + len - 1);
3436
Christoph Probsta205d502019-05-08 21:36:25 +02003437 cifs_dbg(FYI, "Offset %lld len %lld\n", offset, len);
Steve French31742c52014-08-17 08:38:47 -05003438
3439 fsctl_buf.FileOffset = cpu_to_le64(offset);
3440 fsctl_buf.BeyondFinalZero = cpu_to_le64(offset + len);
3441
3442 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
3443 cfile->fid.volatile_fid, FSCTL_SET_ZERO_DATA,
Aurelien Aptel63a83b82018-01-24 13:46:11 +01003444 true /* is_fctl */, (char *)&fsctl_buf,
Steve French153322f2019-03-28 22:32:49 -05003445 sizeof(struct file_zero_data_information),
3446 CIFSMaxBufSize, NULL, NULL);
Steve French31742c52014-08-17 08:38:47 -05003447 free_xid(xid);
3448 return rc;
3449}
3450
Steve French9ccf3212014-10-18 17:01:15 -05003451static long smb3_simple_falloc(struct file *file, struct cifs_tcon *tcon,
3452 loff_t off, loff_t len, bool keep_size)
3453{
3454 struct inode *inode;
3455 struct cifsInodeInfo *cifsi;
3456 struct cifsFileInfo *cfile = file->private_data;
3457 long rc = -EOPNOTSUPP;
3458 unsigned int xid;
Ronnie Sahlbergf1699472019-03-15 00:08:48 +10003459 __le64 eof;
Steve French9ccf3212014-10-18 17:01:15 -05003460
3461 xid = get_xid();
3462
David Howells2b0143b2015-03-17 22:25:59 +00003463 inode = d_inode(cfile->dentry);
Steve French9ccf3212014-10-18 17:01:15 -05003464 cifsi = CIFS_I(inode);
3465
Steve French779ede02019-03-13 01:41:49 -05003466 trace_smb3_falloc_enter(xid, cfile->fid.persistent_fid, tcon->tid,
3467 tcon->ses->Suid, off, len);
Steve French9ccf3212014-10-18 17:01:15 -05003468 /* if file not oplocked can't be sure whether asking to extend size */
3469 if (!CIFS_CACHE_READ(cifsi))
Steve Frenchcfe89092018-05-19 02:04:55 -05003470 if (keep_size == false) {
Steve French779ede02019-03-13 01:41:49 -05003471 trace_smb3_falloc_err(xid, cfile->fid.persistent_fid,
3472 tcon->tid, tcon->ses->Suid, off, len, rc);
Steve Frenchcfe89092018-05-19 02:04:55 -05003473 free_xid(xid);
3474 return rc;
3475 }
Steve French9ccf3212014-10-18 17:01:15 -05003476
3477 /*
Ronnie Sahlberg8bd0d702020-01-17 11:45:02 +10003478 * Extending the file
3479 */
3480 if ((keep_size == false) && i_size_read(inode) < off + len) {
Murphy Zhouef4a6322020-03-18 20:43:38 +08003481 rc = inode_newsize_ok(inode, off + len);
3482 if (rc)
3483 goto out;
3484
Ronnie Sahlberg8bd0d702020-01-17 11:45:02 +10003485 if ((cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) == 0)
3486 smb2_set_sparse(xid, tcon, cfile, inode, false);
3487
3488 eof = cpu_to_le64(off + len);
3489 rc = SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
3490 cfile->fid.volatile_fid, cfile->pid, &eof);
3491 if (rc == 0) {
3492 cifsi->server_eof = off + len;
3493 cifs_setsize(inode, off + len);
3494 cifs_truncate_page(inode->i_mapping, inode->i_size);
3495 truncate_setsize(inode, off + len);
3496 }
3497 goto out;
3498 }
3499
3500 /*
Steve French9ccf3212014-10-18 17:01:15 -05003501 * Files are non-sparse by default so falloc may be a no-op
Ronnie Sahlberg8bd0d702020-01-17 11:45:02 +10003502 * Must check if file sparse. If not sparse, and since we are not
3503 * extending then no need to do anything since file already allocated
Steve French9ccf3212014-10-18 17:01:15 -05003504 */
3505 if ((cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) == 0) {
Ronnie Sahlberg8bd0d702020-01-17 11:45:02 +10003506 rc = 0;
3507 goto out;
Steve French9ccf3212014-10-18 17:01:15 -05003508 }
3509
3510 if ((keep_size == true) || (i_size_read(inode) >= off + len)) {
3511 /*
3512 * Check if falloc starts within first few pages of file
3513 * and ends within a few pages of the end of file to
3514 * ensure that most of file is being forced to be
3515 * fallocated now. If so then setting whole file sparse
3516 * ie potentially making a few extra pages at the beginning
3517 * or end of the file non-sparse via set_sparse is harmless.
3518 */
Steve Frenchcfe89092018-05-19 02:04:55 -05003519 if ((off > 8192) || (off + len + 8192 < i_size_read(inode))) {
3520 rc = -EOPNOTSUPP;
Ronnie Sahlberg8bd0d702020-01-17 11:45:02 +10003521 goto out;
Ronnie Sahlbergf1699472019-03-15 00:08:48 +10003522 }
Steve French9ccf3212014-10-18 17:01:15 -05003523 }
Steve French9ccf3212014-10-18 17:01:15 -05003524
Ronnie Sahlberg8bd0d702020-01-17 11:45:02 +10003525 smb2_set_sparse(xid, tcon, cfile, inode, false);
3526 rc = 0;
3527
3528out:
Steve French779ede02019-03-13 01:41:49 -05003529 if (rc)
3530 trace_smb3_falloc_err(xid, cfile->fid.persistent_fid, tcon->tid,
3531 tcon->ses->Suid, off, len, rc);
3532 else
3533 trace_smb3_falloc_done(xid, cfile->fid.persistent_fid, tcon->tid,
3534 tcon->ses->Suid, off, len);
Steve French9ccf3212014-10-18 17:01:15 -05003535
3536 free_xid(xid);
3537 return rc;
3538}
3539
Ronnie Sahlbergdece44e2019-05-15 07:17:02 +10003540static loff_t smb3_llseek(struct file *file, struct cifs_tcon *tcon, loff_t offset, int whence)
3541{
3542 struct cifsFileInfo *wrcfile, *cfile = file->private_data;
3543 struct cifsInodeInfo *cifsi;
3544 struct inode *inode;
3545 int rc = 0;
3546 struct file_allocated_range_buffer in_data, *out_data = NULL;
3547 u32 out_data_len;
3548 unsigned int xid;
3549
3550 if (whence != SEEK_HOLE && whence != SEEK_DATA)
3551 return generic_file_llseek(file, offset, whence);
3552
3553 inode = d_inode(cfile->dentry);
3554 cifsi = CIFS_I(inode);
3555
3556 if (offset < 0 || offset >= i_size_read(inode))
3557 return -ENXIO;
3558
3559 xid = get_xid();
3560 /*
3561 * We need to be sure that all dirty pages are written as they
3562 * might fill holes on the server.
3563 * Note that we also MUST flush any written pages since at least
3564 * some servers (Windows2016) will not reflect recent writes in
3565 * QUERY_ALLOCATED_RANGES until SMB2_flush is called.
3566 */
Aurelien Aptel86f740f2020-02-21 11:19:06 +01003567 wrcfile = find_writable_file(cifsi, FIND_WR_ANY);
Ronnie Sahlbergdece44e2019-05-15 07:17:02 +10003568 if (wrcfile) {
3569 filemap_write_and_wait(inode->i_mapping);
3570 smb2_flush_file(xid, tcon, &wrcfile->fid);
3571 cifsFileInfo_put(wrcfile);
3572 }
3573
3574 if (!(cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE)) {
3575 if (whence == SEEK_HOLE)
3576 offset = i_size_read(inode);
3577 goto lseek_exit;
3578 }
3579
3580 in_data.file_offset = cpu_to_le64(offset);
3581 in_data.length = cpu_to_le64(i_size_read(inode));
3582
3583 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
3584 cfile->fid.volatile_fid,
3585 FSCTL_QUERY_ALLOCATED_RANGES, true,
3586 (char *)&in_data, sizeof(in_data),
3587 sizeof(struct file_allocated_range_buffer),
3588 (char **)&out_data, &out_data_len);
3589 if (rc == -E2BIG)
3590 rc = 0;
3591 if (rc)
3592 goto lseek_exit;
3593
3594 if (whence == SEEK_HOLE && out_data_len == 0)
3595 goto lseek_exit;
3596
3597 if (whence == SEEK_DATA && out_data_len == 0) {
3598 rc = -ENXIO;
3599 goto lseek_exit;
3600 }
3601
3602 if (out_data_len < sizeof(struct file_allocated_range_buffer)) {
3603 rc = -EINVAL;
3604 goto lseek_exit;
3605 }
3606 if (whence == SEEK_DATA) {
3607 offset = le64_to_cpu(out_data->file_offset);
3608 goto lseek_exit;
3609 }
3610 if (offset < le64_to_cpu(out_data->file_offset))
3611 goto lseek_exit;
3612
3613 offset = le64_to_cpu(out_data->file_offset) + le64_to_cpu(out_data->length);
3614
3615 lseek_exit:
3616 free_xid(xid);
3617 kfree(out_data);
3618 if (!rc)
3619 return vfs_setpos(file, offset, inode->i_sb->s_maxbytes);
3620 else
3621 return rc;
3622}
3623
Ronnie Sahlberg2f3ebab2019-04-25 16:45:29 +10003624static int smb3_fiemap(struct cifs_tcon *tcon,
3625 struct cifsFileInfo *cfile,
3626 struct fiemap_extent_info *fei, u64 start, u64 len)
3627{
3628 unsigned int xid;
3629 struct file_allocated_range_buffer in_data, *out_data;
3630 u32 out_data_len;
3631 int i, num, rc, flags, last_blob;
3632 u64 next;
3633
Christoph Hellwig45dd0522020-05-23 09:30:14 +02003634 rc = fiemap_prep(d_inode(cfile->dentry), fei, start, &len, 0);
Christoph Hellwigcddf8a22020-05-23 09:30:13 +02003635 if (rc)
3636 return rc;
Ronnie Sahlberg2f3ebab2019-04-25 16:45:29 +10003637
3638 xid = get_xid();
3639 again:
3640 in_data.file_offset = cpu_to_le64(start);
3641 in_data.length = cpu_to_le64(len);
3642
3643 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
3644 cfile->fid.volatile_fid,
3645 FSCTL_QUERY_ALLOCATED_RANGES, true,
3646 (char *)&in_data, sizeof(in_data),
3647 1024 * sizeof(struct file_allocated_range_buffer),
3648 (char **)&out_data, &out_data_len);
3649 if (rc == -E2BIG) {
3650 last_blob = 0;
3651 rc = 0;
3652 } else
3653 last_blob = 1;
3654 if (rc)
3655 goto out;
3656
Murphy Zhou979a2662020-03-14 11:38:31 +08003657 if (out_data_len && out_data_len < sizeof(struct file_allocated_range_buffer)) {
Ronnie Sahlberg2f3ebab2019-04-25 16:45:29 +10003658 rc = -EINVAL;
3659 goto out;
3660 }
3661 if (out_data_len % sizeof(struct file_allocated_range_buffer)) {
3662 rc = -EINVAL;
3663 goto out;
3664 }
3665
3666 num = out_data_len / sizeof(struct file_allocated_range_buffer);
3667 for (i = 0; i < num; i++) {
3668 flags = 0;
3669 if (i == num - 1 && last_blob)
3670 flags |= FIEMAP_EXTENT_LAST;
3671
3672 rc = fiemap_fill_next_extent(fei,
3673 le64_to_cpu(out_data[i].file_offset),
3674 le64_to_cpu(out_data[i].file_offset),
3675 le64_to_cpu(out_data[i].length),
3676 flags);
3677 if (rc < 0)
3678 goto out;
3679 if (rc == 1) {
3680 rc = 0;
3681 goto out;
3682 }
3683 }
3684
3685 if (!last_blob) {
3686 next = le64_to_cpu(out_data[num - 1].file_offset) +
3687 le64_to_cpu(out_data[num - 1].length);
3688 len = len - (next - start);
3689 start = next;
3690 goto again;
3691 }
3692
3693 out:
3694 free_xid(xid);
3695 kfree(out_data);
3696 return rc;
3697}
Steve French9ccf3212014-10-18 17:01:15 -05003698
Steve French31742c52014-08-17 08:38:47 -05003699static long smb3_fallocate(struct file *file, struct cifs_tcon *tcon, int mode,
3700 loff_t off, loff_t len)
3701{
3702 /* KEEP_SIZE already checked for by do_fallocate */
3703 if (mode & FALLOC_FL_PUNCH_HOLE)
3704 return smb3_punch_hole(file, tcon, off, len);
Steve French30175622014-08-17 18:16:40 -05003705 else if (mode & FALLOC_FL_ZERO_RANGE) {
3706 if (mode & FALLOC_FL_KEEP_SIZE)
3707 return smb3_zero_range(file, tcon, off, len, true);
3708 return smb3_zero_range(file, tcon, off, len, false);
Steve French9ccf3212014-10-18 17:01:15 -05003709 } else if (mode == FALLOC_FL_KEEP_SIZE)
3710 return smb3_simple_falloc(file, tcon, off, len, true);
3711 else if (mode == 0)
3712 return smb3_simple_falloc(file, tcon, off, len, false);
Steve French31742c52014-08-17 08:38:47 -05003713
3714 return -EOPNOTSUPP;
3715}
3716
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04003717static void
Sachin Prabhuc11f1df2014-03-11 16:11:47 +00003718smb2_downgrade_oplock(struct TCP_Server_Info *server,
Pavel Shilovsky9bd45402019-10-29 16:51:19 -07003719 struct cifsInodeInfo *cinode, __u32 oplock,
3720 unsigned int epoch, bool *purge_cache)
Sachin Prabhuc11f1df2014-03-11 16:11:47 +00003721{
Pavel Shilovsky9bd45402019-10-29 16:51:19 -07003722 server->ops->set_oplock_level(cinode, oplock, 0, NULL);
Sachin Prabhuc11f1df2014-03-11 16:11:47 +00003723}
3724
3725static void
Pavel Shilovsky9bd45402019-10-29 16:51:19 -07003726smb21_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
3727 unsigned int epoch, bool *purge_cache);
3728
3729static void
3730smb3_downgrade_oplock(struct TCP_Server_Info *server,
3731 struct cifsInodeInfo *cinode, __u32 oplock,
3732 unsigned int epoch, bool *purge_cache)
Pavel Shilovsky7b9b9ed2019-02-13 15:43:08 -08003733{
Pavel Shilovsky9bd45402019-10-29 16:51:19 -07003734 unsigned int old_state = cinode->oplock;
3735 unsigned int old_epoch = cinode->epoch;
3736 unsigned int new_state;
3737
3738 if (epoch > old_epoch) {
3739 smb21_set_oplock_level(cinode, oplock, 0, NULL);
3740 cinode->epoch = epoch;
3741 }
3742
3743 new_state = cinode->oplock;
3744 *purge_cache = false;
3745
3746 if ((old_state & CIFS_CACHE_READ_FLG) != 0 &&
3747 (new_state & CIFS_CACHE_READ_FLG) == 0)
3748 *purge_cache = true;
3749 else if (old_state == new_state && (epoch - old_epoch > 1))
3750 *purge_cache = true;
Pavel Shilovsky7b9b9ed2019-02-13 15:43:08 -08003751}
3752
3753static void
Pavel Shilovsky42873b02013-09-05 21:30:16 +04003754smb2_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
3755 unsigned int epoch, bool *purge_cache)
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04003756{
3757 oplock &= 0xFF;
3758 if (oplock == SMB2_OPLOCK_LEVEL_NOCHANGE)
3759 return;
3760 if (oplock == SMB2_OPLOCK_LEVEL_BATCH) {
Pavel Shilovsky42873b02013-09-05 21:30:16 +04003761 cinode->oplock = CIFS_CACHE_RHW_FLG;
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04003762 cifs_dbg(FYI, "Batch Oplock granted on inode %p\n",
3763 &cinode->vfs_inode);
3764 } else if (oplock == SMB2_OPLOCK_LEVEL_EXCLUSIVE) {
Pavel Shilovsky42873b02013-09-05 21:30:16 +04003765 cinode->oplock = CIFS_CACHE_RW_FLG;
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04003766 cifs_dbg(FYI, "Exclusive Oplock granted on inode %p\n",
3767 &cinode->vfs_inode);
3768 } else if (oplock == SMB2_OPLOCK_LEVEL_II) {
3769 cinode->oplock = CIFS_CACHE_READ_FLG;
3770 cifs_dbg(FYI, "Level II Oplock granted on inode %p\n",
3771 &cinode->vfs_inode);
3772 } else
3773 cinode->oplock = 0;
3774}
3775
3776static void
Pavel Shilovsky42873b02013-09-05 21:30:16 +04003777smb21_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
3778 unsigned int epoch, bool *purge_cache)
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04003779{
3780 char message[5] = {0};
Christoph Probst6a54b2e2019-05-07 17:16:40 +02003781 unsigned int new_oplock = 0;
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04003782
3783 oplock &= 0xFF;
3784 if (oplock == SMB2_OPLOCK_LEVEL_NOCHANGE)
3785 return;
3786
Pavel Shilovskya016e272019-09-26 12:31:20 -07003787 /* Check if the server granted an oplock rather than a lease */
3788 if (oplock & SMB2_OPLOCK_LEVEL_EXCLUSIVE)
3789 return smb2_set_oplock_level(cinode, oplock, epoch,
3790 purge_cache);
3791
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04003792 if (oplock & SMB2_LEASE_READ_CACHING_HE) {
Christoph Probst6a54b2e2019-05-07 17:16:40 +02003793 new_oplock |= CIFS_CACHE_READ_FLG;
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04003794 strcat(message, "R");
3795 }
3796 if (oplock & SMB2_LEASE_HANDLE_CACHING_HE) {
Christoph Probst6a54b2e2019-05-07 17:16:40 +02003797 new_oplock |= CIFS_CACHE_HANDLE_FLG;
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04003798 strcat(message, "H");
3799 }
3800 if (oplock & SMB2_LEASE_WRITE_CACHING_HE) {
Christoph Probst6a54b2e2019-05-07 17:16:40 +02003801 new_oplock |= CIFS_CACHE_WRITE_FLG;
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04003802 strcat(message, "W");
3803 }
Christoph Probst6a54b2e2019-05-07 17:16:40 +02003804 if (!new_oplock)
3805 strncpy(message, "None", sizeof(message));
3806
3807 cinode->oplock = new_oplock;
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04003808 cifs_dbg(FYI, "%s Lease granted on inode %p\n", message,
3809 &cinode->vfs_inode);
3810}
3811
Pavel Shilovsky42873b02013-09-05 21:30:16 +04003812static void
3813smb3_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
3814 unsigned int epoch, bool *purge_cache)
3815{
3816 unsigned int old_oplock = cinode->oplock;
3817
3818 smb21_set_oplock_level(cinode, oplock, epoch, purge_cache);
3819
3820 if (purge_cache) {
3821 *purge_cache = false;
3822 if (old_oplock == CIFS_CACHE_READ_FLG) {
3823 if (cinode->oplock == CIFS_CACHE_READ_FLG &&
3824 (epoch - cinode->epoch > 0))
3825 *purge_cache = true;
3826 else if (cinode->oplock == CIFS_CACHE_RH_FLG &&
3827 (epoch - cinode->epoch > 1))
3828 *purge_cache = true;
3829 else if (cinode->oplock == CIFS_CACHE_RHW_FLG &&
3830 (epoch - cinode->epoch > 1))
3831 *purge_cache = true;
3832 else if (cinode->oplock == 0 &&
3833 (epoch - cinode->epoch > 0))
3834 *purge_cache = true;
3835 } else if (old_oplock == CIFS_CACHE_RH_FLG) {
3836 if (cinode->oplock == CIFS_CACHE_RH_FLG &&
3837 (epoch - cinode->epoch > 0))
3838 *purge_cache = true;
3839 else if (cinode->oplock == CIFS_CACHE_RHW_FLG &&
3840 (epoch - cinode->epoch > 1))
3841 *purge_cache = true;
3842 }
3843 cinode->epoch = epoch;
3844 }
3845}
3846
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04003847static bool
3848smb2_is_read_op(__u32 oplock)
3849{
3850 return oplock == SMB2_OPLOCK_LEVEL_II;
3851}
3852
3853static bool
3854smb21_is_read_op(__u32 oplock)
3855{
3856 return (oplock & SMB2_LEASE_READ_CACHING_HE) &&
3857 !(oplock & SMB2_LEASE_WRITE_CACHING_HE);
3858}
3859
Pavel Shilovskyf0473902013-09-04 13:44:05 +04003860static __le32
3861map_oplock_to_lease(u8 oplock)
3862{
3863 if (oplock == SMB2_OPLOCK_LEVEL_EXCLUSIVE)
3864 return SMB2_LEASE_WRITE_CACHING | SMB2_LEASE_READ_CACHING;
3865 else if (oplock == SMB2_OPLOCK_LEVEL_II)
3866 return SMB2_LEASE_READ_CACHING;
3867 else if (oplock == SMB2_OPLOCK_LEVEL_BATCH)
3868 return SMB2_LEASE_HANDLE_CACHING | SMB2_LEASE_READ_CACHING |
3869 SMB2_LEASE_WRITE_CACHING;
3870 return 0;
3871}
3872
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04003873static char *
3874smb2_create_lease_buf(u8 *lease_key, u8 oplock)
3875{
3876 struct create_lease *buf;
3877
3878 buf = kzalloc(sizeof(struct create_lease), GFP_KERNEL);
3879 if (!buf)
3880 return NULL;
3881
Stefano Brivio729c0c92018-07-05 15:10:02 +02003882 memcpy(&buf->lcontext.LeaseKey, lease_key, SMB2_LEASE_KEY_SIZE);
Pavel Shilovskyf0473902013-09-04 13:44:05 +04003883 buf->lcontext.LeaseState = map_oplock_to_lease(oplock);
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04003884
3885 buf->ccontext.DataOffset = cpu_to_le16(offsetof
3886 (struct create_lease, lcontext));
3887 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context));
3888 buf->ccontext.NameOffset = cpu_to_le16(offsetof
3889 (struct create_lease, Name));
3890 buf->ccontext.NameLength = cpu_to_le16(4);
Steve French12197a72014-05-14 05:29:40 -07003891 /* SMB2_CREATE_REQUEST_LEASE is "RqLs" */
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04003892 buf->Name[0] = 'R';
3893 buf->Name[1] = 'q';
3894 buf->Name[2] = 'L';
3895 buf->Name[3] = 's';
3896 return (char *)buf;
3897}
3898
Pavel Shilovskyf0473902013-09-04 13:44:05 +04003899static char *
3900smb3_create_lease_buf(u8 *lease_key, u8 oplock)
3901{
3902 struct create_lease_v2 *buf;
3903
3904 buf = kzalloc(sizeof(struct create_lease_v2), GFP_KERNEL);
3905 if (!buf)
3906 return NULL;
3907
Stefano Brivio729c0c92018-07-05 15:10:02 +02003908 memcpy(&buf->lcontext.LeaseKey, lease_key, SMB2_LEASE_KEY_SIZE);
Pavel Shilovskyf0473902013-09-04 13:44:05 +04003909 buf->lcontext.LeaseState = map_oplock_to_lease(oplock);
3910
3911 buf->ccontext.DataOffset = cpu_to_le16(offsetof
3912 (struct create_lease_v2, lcontext));
3913 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context_v2));
3914 buf->ccontext.NameOffset = cpu_to_le16(offsetof
3915 (struct create_lease_v2, Name));
3916 buf->ccontext.NameLength = cpu_to_le16(4);
Steve French12197a72014-05-14 05:29:40 -07003917 /* SMB2_CREATE_REQUEST_LEASE is "RqLs" */
Pavel Shilovskyf0473902013-09-04 13:44:05 +04003918 buf->Name[0] = 'R';
3919 buf->Name[1] = 'q';
3920 buf->Name[2] = 'L';
3921 buf->Name[3] = 's';
3922 return (char *)buf;
3923}
3924
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04003925static __u8
Ronnie Sahlberg96164ab2018-04-26 08:10:18 -06003926smb2_parse_lease_buf(void *buf, unsigned int *epoch, char *lease_key)
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04003927{
3928 struct create_lease *lc = (struct create_lease *)buf;
3929
Pavel Shilovsky42873b02013-09-05 21:30:16 +04003930 *epoch = 0; /* not used */
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04003931 if (lc->lcontext.LeaseFlags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS)
3932 return SMB2_OPLOCK_LEVEL_NOCHANGE;
3933 return le32_to_cpu(lc->lcontext.LeaseState);
3934}
3935
Pavel Shilovskyf0473902013-09-04 13:44:05 +04003936static __u8
Ronnie Sahlberg96164ab2018-04-26 08:10:18 -06003937smb3_parse_lease_buf(void *buf, unsigned int *epoch, char *lease_key)
Pavel Shilovskyf0473902013-09-04 13:44:05 +04003938{
3939 struct create_lease_v2 *lc = (struct create_lease_v2 *)buf;
3940
Pavel Shilovsky42873b02013-09-05 21:30:16 +04003941 *epoch = le16_to_cpu(lc->lcontext.Epoch);
Pavel Shilovskyf0473902013-09-04 13:44:05 +04003942 if (lc->lcontext.LeaseFlags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS)
3943 return SMB2_OPLOCK_LEVEL_NOCHANGE;
Ronnie Sahlberg96164ab2018-04-26 08:10:18 -06003944 if (lease_key)
Stefano Brivio729c0c92018-07-05 15:10:02 +02003945 memcpy(lease_key, &lc->lcontext.LeaseKey, SMB2_LEASE_KEY_SIZE);
Pavel Shilovskyf0473902013-09-04 13:44:05 +04003946 return le32_to_cpu(lc->lcontext.LeaseState);
3947}
3948
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04003949static unsigned int
3950smb2_wp_retry_size(struct inode *inode)
3951{
3952 return min_t(unsigned int, CIFS_SB(inode->i_sb)->wsize,
3953 SMB2_MAX_BUFFER_SIZE);
3954}
3955
Pavel Shilovsky52755802014-08-18 20:49:57 +04003956static bool
3957smb2_dir_needs_close(struct cifsFileInfo *cfile)
3958{
3959 return !cfile->invalidHandle;
3960}
3961
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07003962static void
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10003963fill_transform_hdr(struct smb2_transform_hdr *tr_hdr, unsigned int orig_len,
Steve French2b2f7542019-06-07 15:16:10 -05003964 struct smb_rqst *old_rq, __le16 cipher_type)
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07003965{
3966 struct smb2_sync_hdr *shdr =
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10003967 (struct smb2_sync_hdr *)old_rq->rq_iov[0].iov_base;
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07003968
3969 memset(tr_hdr, 0, sizeof(struct smb2_transform_hdr));
3970 tr_hdr->ProtocolId = SMB2_TRANSFORM_PROTO_NUM;
3971 tr_hdr->OriginalMessageSize = cpu_to_le32(orig_len);
3972 tr_hdr->Flags = cpu_to_le16(0x01);
Steve French63ca5652020-10-15 23:41:40 -05003973 if ((cipher_type == SMB2_ENCRYPTION_AES128_GCM) ||
3974 (cipher_type == SMB2_ENCRYPTION_AES256_GCM))
Steve Frenchfd08f2d2020-10-15 00:25:02 -05003975 get_random_bytes(&tr_hdr->Nonce, SMB3_AES_GCM_NONCE);
Steve French2b2f7542019-06-07 15:16:10 -05003976 else
Steve Frenchfd08f2d2020-10-15 00:25:02 -05003977 get_random_bytes(&tr_hdr->Nonce, SMB3_AES_CCM_NONCE);
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07003978 memcpy(&tr_hdr->SessionId, &shdr->SessionId, 8);
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07003979}
3980
Ronnie Sahlberg262916b2018-02-20 12:45:21 +11003981/* We can not use the normal sg_set_buf() as we will sometimes pass a
3982 * stack object as buf.
3983 */
3984static inline void smb2_sg_set_buf(struct scatterlist *sg, const void *buf,
3985 unsigned int buflen)
3986{
Sebastien Tisserantee9d6612019-08-01 12:06:08 -05003987 void *addr;
3988 /*
3989 * VMAP_STACK (at least) puts stack into the vmalloc address space
3990 */
3991 if (is_vmalloc_addr(buf))
3992 addr = vmalloc_to_page(buf);
3993 else
3994 addr = virt_to_page(buf);
3995 sg_set_page(sg, addr, buflen, offset_in_page(buf));
Ronnie Sahlberg262916b2018-02-20 12:45:21 +11003996}
3997
Ronnie Sahlbergb2c96de2018-08-01 09:26:11 +10003998/* Assumes the first rqst has a transform header as the first iov.
3999 * I.e.
4000 * rqst[0].rq_iov[0] is transform header
4001 * rqst[0].rq_iov[1+] data to be encrypted/decrypted
4002 * rqst[1+].rq_iov[0+] data to be encrypted/decrypted
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10004003 */
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07004004static struct scatterlist *
Ronnie Sahlbergb2c96de2018-08-01 09:26:11 +10004005init_sg(int num_rqst, struct smb_rqst *rqst, u8 *sign)
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07004006{
Ronnie Sahlbergb2c96de2018-08-01 09:26:11 +10004007 unsigned int sg_len;
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07004008 struct scatterlist *sg;
4009 unsigned int i;
4010 unsigned int j;
Ronnie Sahlbergb2c96de2018-08-01 09:26:11 +10004011 unsigned int idx = 0;
4012 int skip;
4013
4014 sg_len = 1;
4015 for (i = 0; i < num_rqst; i++)
4016 sg_len += rqst[i].rq_nvec + rqst[i].rq_npages;
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07004017
4018 sg = kmalloc_array(sg_len, sizeof(struct scatterlist), GFP_KERNEL);
4019 if (!sg)
4020 return NULL;
4021
4022 sg_init_table(sg, sg_len);
Ronnie Sahlbergb2c96de2018-08-01 09:26:11 +10004023 for (i = 0; i < num_rqst; i++) {
4024 for (j = 0; j < rqst[i].rq_nvec; j++) {
4025 /*
4026 * The first rqst has a transform header where the
4027 * first 20 bytes are not part of the encrypted blob
4028 */
4029 skip = (i == 0) && (j == 0) ? 20 : 0;
4030 smb2_sg_set_buf(&sg[idx++],
4031 rqst[i].rq_iov[j].iov_base + skip,
4032 rqst[i].rq_iov[j].iov_len - skip);
Ronnie Sahlberge77fe732018-12-31 13:43:40 +10004033 }
Steve Frenchd5f07fb2018-06-05 17:46:24 -05004034
Ronnie Sahlbergb2c96de2018-08-01 09:26:11 +10004035 for (j = 0; j < rqst[i].rq_npages; j++) {
4036 unsigned int len, offset;
4037
4038 rqst_page_get_length(&rqst[i], j, &len, &offset);
4039 sg_set_page(&sg[idx++], rqst[i].rq_pages[j], len, offset);
4040 }
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07004041 }
Ronnie Sahlbergb2c96de2018-08-01 09:26:11 +10004042 smb2_sg_set_buf(&sg[idx], sign, SMB2_SIGNATURE_SIZE);
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07004043 return sg;
4044}
4045
Pavel Shilovsky61cfac6f2017-02-28 16:05:19 -08004046static int
4047smb2_get_enc_key(struct TCP_Server_Info *server, __u64 ses_id, int enc, u8 *key)
4048{
4049 struct cifs_ses *ses;
4050 u8 *ses_enc_key;
4051
4052 spin_lock(&cifs_tcp_ses_lock);
Aurelien Apteld70e9fa2019-09-20 06:31:10 +02004053 list_for_each_entry(server, &cifs_tcp_ses_list, tcp_ses_list) {
4054 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
4055 if (ses->Suid == ses_id) {
4056 ses_enc_key = enc ? ses->smb3encryptionkey :
4057 ses->smb3decryptionkey;
4058 memcpy(key, ses_enc_key, SMB3_SIGN_KEY_SIZE);
4059 spin_unlock(&cifs_tcp_ses_lock);
4060 return 0;
4061 }
4062 }
Pavel Shilovsky61cfac6f2017-02-28 16:05:19 -08004063 }
4064 spin_unlock(&cifs_tcp_ses_lock);
4065
4066 return 1;
4067}
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07004068/*
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10004069 * Encrypt or decrypt @rqst message. @rqst[0] has the following format:
4070 * iov[0] - transform header (associate data),
4071 * iov[1-N] - SMB2 header and pages - data to encrypt.
4072 * On success return encrypted data in iov[1-N] and pages, leave iov[0]
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07004073 * untouched.
4074 */
4075static int
Ronnie Sahlbergb2c96de2018-08-01 09:26:11 +10004076crypt_message(struct TCP_Server_Info *server, int num_rqst,
4077 struct smb_rqst *rqst, int enc)
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07004078{
4079 struct smb2_transform_hdr *tr_hdr =
Ronnie Sahlbergb2c96de2018-08-01 09:26:11 +10004080 (struct smb2_transform_hdr *)rqst[0].rq_iov[0].iov_base;
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10004081 unsigned int assoc_data_len = sizeof(struct smb2_transform_hdr) - 20;
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07004082 int rc = 0;
4083 struct scatterlist *sg;
4084 u8 sign[SMB2_SIGNATURE_SIZE] = {};
Pavel Shilovsky61cfac6f2017-02-28 16:05:19 -08004085 u8 key[SMB3_SIGN_KEY_SIZE];
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07004086 struct aead_request *req;
4087 char *iv;
4088 unsigned int iv_len;
Gilad Ben-Yossefa5186b82017-10-18 08:00:46 +01004089 DECLARE_CRYPTO_WAIT(wait);
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07004090 struct crypto_aead *tfm;
4091 unsigned int crypt_len = le32_to_cpu(tr_hdr->OriginalMessageSize);
4092
Pavel Shilovsky61cfac6f2017-02-28 16:05:19 -08004093 rc = smb2_get_enc_key(server, tr_hdr->SessionId, enc, key);
4094 if (rc) {
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +10004095 cifs_server_dbg(VFS, "%s: Could not get %scryption key\n", __func__,
Pavel Shilovsky61cfac6f2017-02-28 16:05:19 -08004096 enc ? "en" : "de");
Shyam Prasad N0bd294b2020-10-15 10:41:31 -07004097 return rc;
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07004098 }
4099
4100 rc = smb3_crypto_aead_allocate(server);
4101 if (rc) {
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +10004102 cifs_server_dbg(VFS, "%s: crypto alloc failed\n", __func__);
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07004103 return rc;
4104 }
4105
4106 tfm = enc ? server->secmech.ccmaesencrypt :
4107 server->secmech.ccmaesdecrypt;
Steve French63ca5652020-10-15 23:41:40 -05004108
4109 if (server->cipher_type == SMB2_ENCRYPTION_AES256_GCM)
4110 rc = crypto_aead_setkey(tfm, key, SMB3_GCM256_CRYPTKEY_SIZE);
4111 else
4112 rc = crypto_aead_setkey(tfm, key, SMB3_SIGN_KEY_SIZE);
4113
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07004114 if (rc) {
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +10004115 cifs_server_dbg(VFS, "%s: Failed to set aead key %d\n", __func__, rc);
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07004116 return rc;
4117 }
4118
4119 rc = crypto_aead_setauthsize(tfm, SMB2_SIGNATURE_SIZE);
4120 if (rc) {
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +10004121 cifs_server_dbg(VFS, "%s: Failed to set authsize %d\n", __func__, rc);
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07004122 return rc;
4123 }
4124
4125 req = aead_request_alloc(tfm, GFP_KERNEL);
4126 if (!req) {
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +10004127 cifs_server_dbg(VFS, "%s: Failed to alloc aead request\n", __func__);
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07004128 return -ENOMEM;
4129 }
4130
4131 if (!enc) {
4132 memcpy(sign, &tr_hdr->Signature, SMB2_SIGNATURE_SIZE);
4133 crypt_len += SMB2_SIGNATURE_SIZE;
4134 }
4135
Ronnie Sahlbergb2c96de2018-08-01 09:26:11 +10004136 sg = init_sg(num_rqst, rqst, sign);
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07004137 if (!sg) {
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +10004138 cifs_server_dbg(VFS, "%s: Failed to init sg\n", __func__);
Christophe Jaillet517a6e42017-06-11 09:12:47 +02004139 rc = -ENOMEM;
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07004140 goto free_req;
4141 }
4142
4143 iv_len = crypto_aead_ivsize(tfm);
4144 iv = kzalloc(iv_len, GFP_KERNEL);
4145 if (!iv) {
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +10004146 cifs_server_dbg(VFS, "%s: Failed to alloc iv\n", __func__);
Christophe Jaillet517a6e42017-06-11 09:12:47 +02004147 rc = -ENOMEM;
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07004148 goto free_sg;
4149 }
Steve French2b2f7542019-06-07 15:16:10 -05004150
Steve French63ca5652020-10-15 23:41:40 -05004151 if ((server->cipher_type == SMB2_ENCRYPTION_AES128_GCM) ||
4152 (server->cipher_type == SMB2_ENCRYPTION_AES256_GCM))
Steve Frenchfd08f2d2020-10-15 00:25:02 -05004153 memcpy(iv, (char *)tr_hdr->Nonce, SMB3_AES_GCM_NONCE);
Steve French2b2f7542019-06-07 15:16:10 -05004154 else {
4155 iv[0] = 3;
Steve Frenchfd08f2d2020-10-15 00:25:02 -05004156 memcpy(iv + 1, (char *)tr_hdr->Nonce, SMB3_AES_CCM_NONCE);
Steve French2b2f7542019-06-07 15:16:10 -05004157 }
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07004158
4159 aead_request_set_crypt(req, sg, sg, crypt_len, iv);
4160 aead_request_set_ad(req, assoc_data_len);
4161
4162 aead_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
Gilad Ben-Yossefa5186b82017-10-18 08:00:46 +01004163 crypto_req_done, &wait);
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07004164
Gilad Ben-Yossefa5186b82017-10-18 08:00:46 +01004165 rc = crypto_wait_req(enc ? crypto_aead_encrypt(req)
4166 : crypto_aead_decrypt(req), &wait);
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07004167
4168 if (!rc && enc)
4169 memcpy(&tr_hdr->Signature, sign, SMB2_SIGNATURE_SIZE);
4170
4171 kfree(iv);
4172free_sg:
4173 kfree(sg);
4174free_req:
4175 kfree(req);
4176 return rc;
4177}
4178
Ronnie Sahlbergb2c96de2018-08-01 09:26:11 +10004179void
4180smb3_free_compound_rqst(int num_rqst, struct smb_rqst *rqst)
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07004181{
Ronnie Sahlbergb2c96de2018-08-01 09:26:11 +10004182 int i, j;
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07004183
Ronnie Sahlbergb2c96de2018-08-01 09:26:11 +10004184 for (i = 0; i < num_rqst; i++) {
4185 if (rqst[i].rq_pages) {
4186 for (j = rqst[i].rq_npages - 1; j >= 0; j--)
4187 put_page(rqst[i].rq_pages[j]);
4188 kfree(rqst[i].rq_pages);
4189 }
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07004190 }
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07004191}
4192
Ronnie Sahlbergb2c96de2018-08-01 09:26:11 +10004193/*
4194 * This function will initialize new_rq and encrypt the content.
4195 * The first entry, new_rq[0], only contains a single iov which contains
4196 * a smb2_transform_hdr and is pre-allocated by the caller.
4197 * This function then populates new_rq[1+] with the content from olq_rq[0+].
4198 *
4199 * The end result is an array of smb_rqst structures where the first structure
4200 * only contains a single iov for the transform header which we then can pass
4201 * to crypt_message().
4202 *
4203 * new_rq[0].rq_iov[0] : smb2_transform_hdr pre-allocated by the caller
4204 * new_rq[1+].rq_iov[*] == old_rq[0+].rq_iov[*] : SMB2/3 requests
4205 */
4206static int
4207smb3_init_transform_rq(struct TCP_Server_Info *server, int num_rqst,
4208 struct smb_rqst *new_rq, struct smb_rqst *old_rq)
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07004209{
Ronnie Sahlbergb2c96de2018-08-01 09:26:11 +10004210 struct page **pages;
4211 struct smb2_transform_hdr *tr_hdr = new_rq[0].rq_iov[0].iov_base;
4212 unsigned int npages;
4213 unsigned int orig_len = 0;
4214 int i, j;
4215 int rc = -ENOMEM;
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07004216
Ronnie Sahlbergb2c96de2018-08-01 09:26:11 +10004217 for (i = 1; i < num_rqst; i++) {
4218 npages = old_rq[i - 1].rq_npages;
4219 pages = kmalloc_array(npages, sizeof(struct page *),
4220 GFP_KERNEL);
4221 if (!pages)
4222 goto err_free;
4223
4224 new_rq[i].rq_pages = pages;
4225 new_rq[i].rq_npages = npages;
4226 new_rq[i].rq_offset = old_rq[i - 1].rq_offset;
4227 new_rq[i].rq_pagesz = old_rq[i - 1].rq_pagesz;
4228 new_rq[i].rq_tailsz = old_rq[i - 1].rq_tailsz;
4229 new_rq[i].rq_iov = old_rq[i - 1].rq_iov;
4230 new_rq[i].rq_nvec = old_rq[i - 1].rq_nvec;
4231
4232 orig_len += smb_rqst_len(server, &old_rq[i - 1]);
4233
4234 for (j = 0; j < npages; j++) {
4235 pages[j] = alloc_page(GFP_KERNEL|__GFP_HIGHMEM);
4236 if (!pages[j])
4237 goto err_free;
4238 }
4239
4240 /* copy pages form the old */
4241 for (j = 0; j < npages; j++) {
4242 char *dst, *src;
4243 unsigned int offset, len;
4244
4245 rqst_page_get_length(&new_rq[i], j, &len, &offset);
4246
4247 dst = (char *) kmap(new_rq[i].rq_pages[j]) + offset;
4248 src = (char *) kmap(old_rq[i - 1].rq_pages[j]) + offset;
4249
4250 memcpy(dst, src, len);
4251 kunmap(new_rq[i].rq_pages[j]);
4252 kunmap(old_rq[i - 1].rq_pages[j]);
4253 }
4254 }
4255
4256 /* fill the 1st iov with a transform header */
Steve French2b2f7542019-06-07 15:16:10 -05004257 fill_transform_hdr(tr_hdr, orig_len, old_rq, server->cipher_type);
Ronnie Sahlbergb2c96de2018-08-01 09:26:11 +10004258
4259 rc = crypt_message(server, num_rqst, new_rq, 1);
Christoph Probsta205d502019-05-08 21:36:25 +02004260 cifs_dbg(FYI, "Encrypt message returned %d\n", rc);
Ronnie Sahlbergb2c96de2018-08-01 09:26:11 +10004261 if (rc)
4262 goto err_free;
4263
4264 return rc;
4265
4266err_free:
4267 smb3_free_compound_rqst(num_rqst - 1, &new_rq[1]);
4268 return rc;
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07004269}
4270
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004271static int
4272smb3_is_transform_hdr(void *buf)
4273{
4274 struct smb2_transform_hdr *trhdr = buf;
4275
4276 return trhdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM;
4277}
4278
4279static int
4280decrypt_raw_data(struct TCP_Server_Info *server, char *buf,
4281 unsigned int buf_data_size, struct page **pages,
Rohith Surabattula62593012020-10-08 09:58:41 +00004282 unsigned int npages, unsigned int page_data_size,
4283 bool is_offloaded)
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004284{
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10004285 struct kvec iov[2];
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004286 struct smb_rqst rqst = {NULL};
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004287 int rc;
4288
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10004289 iov[0].iov_base = buf;
4290 iov[0].iov_len = sizeof(struct smb2_transform_hdr);
4291 iov[1].iov_base = buf + sizeof(struct smb2_transform_hdr);
4292 iov[1].iov_len = buf_data_size;
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004293
4294 rqst.rq_iov = iov;
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10004295 rqst.rq_nvec = 2;
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004296 rqst.rq_pages = pages;
4297 rqst.rq_npages = npages;
4298 rqst.rq_pagesz = PAGE_SIZE;
4299 rqst.rq_tailsz = (page_data_size % PAGE_SIZE) ? : PAGE_SIZE;
4300
Ronnie Sahlbergb2c96de2018-08-01 09:26:11 +10004301 rc = crypt_message(server, 1, &rqst, 0);
Christoph Probsta205d502019-05-08 21:36:25 +02004302 cifs_dbg(FYI, "Decrypt message returned %d\n", rc);
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004303
4304 if (rc)
4305 return rc;
4306
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10004307 memmove(buf, iov[1].iov_base, buf_data_size);
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10004308
Rohith Surabattula62593012020-10-08 09:58:41 +00004309 if (!is_offloaded)
4310 server->total_read = buf_data_size + page_data_size;
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004311
4312 return rc;
4313}
4314
4315static int
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08004316read_data_into_pages(struct TCP_Server_Info *server, struct page **pages,
4317 unsigned int npages, unsigned int len)
4318{
4319 int i;
4320 int length;
4321
4322 for (i = 0; i < npages; i++) {
4323 struct page *page = pages[i];
4324 size_t n;
4325
4326 n = len;
4327 if (len >= PAGE_SIZE) {
4328 /* enough data to fill the page */
4329 n = PAGE_SIZE;
4330 len -= n;
4331 } else {
4332 zero_user(page, len, PAGE_SIZE - len);
4333 len = 0;
4334 }
Long Li1dbe3462018-05-30 12:47:55 -07004335 length = cifs_read_page_from_socket(server, page, 0, n);
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08004336 if (length < 0)
4337 return length;
4338 server->total_read += length;
4339 }
4340
4341 return 0;
4342}
4343
4344static int
4345init_read_bvec(struct page **pages, unsigned int npages, unsigned int data_size,
4346 unsigned int cur_off, struct bio_vec **page_vec)
4347{
4348 struct bio_vec *bvec;
4349 int i;
4350
4351 bvec = kcalloc(npages, sizeof(struct bio_vec), GFP_KERNEL);
4352 if (!bvec)
4353 return -ENOMEM;
4354
4355 for (i = 0; i < npages; i++) {
4356 bvec[i].bv_page = pages[i];
4357 bvec[i].bv_offset = (i == 0) ? cur_off : 0;
4358 bvec[i].bv_len = min_t(unsigned int, PAGE_SIZE, data_size);
4359 data_size -= bvec[i].bv_len;
4360 }
4361
4362 if (data_size != 0) {
4363 cifs_dbg(VFS, "%s: something went wrong\n", __func__);
4364 kfree(bvec);
4365 return -EIO;
4366 }
4367
4368 *page_vec = bvec;
4369 return 0;
4370}
4371
4372static int
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004373handle_read_data(struct TCP_Server_Info *server, struct mid_q_entry *mid,
4374 char *buf, unsigned int buf_len, struct page **pages,
Rohith Surabattulade9ac0a2020-10-28 13:42:21 +00004375 unsigned int npages, unsigned int page_data_size,
4376 bool is_offloaded)
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004377{
4378 unsigned int data_offset;
4379 unsigned int data_len;
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08004380 unsigned int cur_off;
4381 unsigned int cur_page_idx;
4382 unsigned int pad_len;
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004383 struct cifs_readdata *rdata = mid->callback_data;
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10004384 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004385 struct bio_vec *bvec = NULL;
4386 struct iov_iter iter;
4387 struct kvec iov;
4388 int length;
Long Li74dcf412017-11-22 17:38:46 -07004389 bool use_rdma_mr = false;
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004390
4391 if (shdr->Command != SMB2_READ) {
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +10004392 cifs_server_dbg(VFS, "only big read responses are supported\n");
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004393 return -ENOTSUPP;
4394 }
4395
Pavel Shilovsky511c54a2017-07-08 14:32:00 -07004396 if (server->ops->is_session_expired &&
4397 server->ops->is_session_expired(buf)) {
Rohith Surabattulade9ac0a2020-10-28 13:42:21 +00004398 if (!is_offloaded)
4399 cifs_reconnect(server);
Pavel Shilovsky511c54a2017-07-08 14:32:00 -07004400 return -1;
4401 }
4402
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004403 if (server->ops->is_status_pending &&
Pavel Shilovsky66265f12019-01-23 17:11:16 -08004404 server->ops->is_status_pending(buf, server))
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004405 return -1;
4406
Pavel Shilovskyec678ea2019-01-18 15:38:11 -08004407 /* set up first two iov to get credits */
4408 rdata->iov[0].iov_base = buf;
Pavel Shilovskybb1bccb2019-01-17 16:18:38 -08004409 rdata->iov[0].iov_len = 0;
4410 rdata->iov[1].iov_base = buf;
Pavel Shilovskyec678ea2019-01-18 15:38:11 -08004411 rdata->iov[1].iov_len =
Pavel Shilovskybb1bccb2019-01-17 16:18:38 -08004412 min_t(unsigned int, buf_len, server->vals->read_rsp_size);
Pavel Shilovskyec678ea2019-01-18 15:38:11 -08004413 cifs_dbg(FYI, "0: iov_base=%p iov_len=%zu\n",
4414 rdata->iov[0].iov_base, rdata->iov[0].iov_len);
4415 cifs_dbg(FYI, "1: iov_base=%p iov_len=%zu\n",
4416 rdata->iov[1].iov_base, rdata->iov[1].iov_len);
4417
4418 rdata->result = server->ops->map_error(buf, true);
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004419 if (rdata->result != 0) {
4420 cifs_dbg(FYI, "%s: server returned error %d\n",
4421 __func__, rdata->result);
Pavel Shilovskyec678ea2019-01-18 15:38:11 -08004422 /* normal error on read response */
Rohith Surabattulaac873aa2020-10-29 05:03:10 +00004423 if (is_offloaded)
4424 mid->mid_state = MID_RESPONSE_RECEIVED;
4425 else
4426 dequeue_mid(mid, false);
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004427 return 0;
4428 }
4429
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10004430 data_offset = server->ops->read_data_offset(buf);
Long Li74dcf412017-11-22 17:38:46 -07004431#ifdef CONFIG_CIFS_SMB_DIRECT
4432 use_rdma_mr = rdata->mr;
4433#endif
4434 data_len = server->ops->read_data_length(buf, use_rdma_mr);
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004435
4436 if (data_offset < server->vals->read_rsp_size) {
4437 /*
4438 * win2k8 sometimes sends an offset of 0 when the read
4439 * is beyond the EOF. Treat it as if the data starts just after
4440 * the header.
4441 */
4442 cifs_dbg(FYI, "%s: data offset (%u) inside read response header\n",
4443 __func__, data_offset);
4444 data_offset = server->vals->read_rsp_size;
4445 } else if (data_offset > MAX_CIFS_SMALL_BUFFER_SIZE) {
4446 /* data_offset is beyond the end of smallbuf */
4447 cifs_dbg(FYI, "%s: data offset (%u) beyond end of smallbuf\n",
4448 __func__, data_offset);
4449 rdata->result = -EIO;
Rohith Surabattulaac873aa2020-10-29 05:03:10 +00004450 if (is_offloaded)
4451 mid->mid_state = MID_RESPONSE_MALFORMED;
4452 else
4453 dequeue_mid(mid, rdata->result);
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004454 return 0;
4455 }
4456
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08004457 pad_len = data_offset - server->vals->read_rsp_size;
4458
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004459 if (buf_len <= data_offset) {
4460 /* read response payload is in pages */
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08004461 cur_page_idx = pad_len / PAGE_SIZE;
4462 cur_off = pad_len % PAGE_SIZE;
4463
4464 if (cur_page_idx != 0) {
4465 /* data offset is beyond the 1st page of response */
4466 cifs_dbg(FYI, "%s: data offset (%u) beyond 1st page of response\n",
4467 __func__, data_offset);
4468 rdata->result = -EIO;
Rohith Surabattulaac873aa2020-10-29 05:03:10 +00004469 if (is_offloaded)
4470 mid->mid_state = MID_RESPONSE_MALFORMED;
4471 else
4472 dequeue_mid(mid, rdata->result);
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08004473 return 0;
4474 }
4475
4476 if (data_len > page_data_size - pad_len) {
4477 /* data_len is corrupt -- discard frame */
4478 rdata->result = -EIO;
Rohith Surabattulaac873aa2020-10-29 05:03:10 +00004479 if (is_offloaded)
4480 mid->mid_state = MID_RESPONSE_MALFORMED;
4481 else
4482 dequeue_mid(mid, rdata->result);
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08004483 return 0;
4484 }
4485
4486 rdata->result = init_read_bvec(pages, npages, page_data_size,
4487 cur_off, &bvec);
4488 if (rdata->result != 0) {
Rohith Surabattulaac873aa2020-10-29 05:03:10 +00004489 if (is_offloaded)
4490 mid->mid_state = MID_RESPONSE_MALFORMED;
4491 else
4492 dequeue_mid(mid, rdata->result);
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08004493 return 0;
4494 }
4495
David Howellsaa563d72018-10-20 00:57:56 +01004496 iov_iter_bvec(&iter, WRITE, bvec, npages, data_len);
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004497 } else if (buf_len >= data_offset + data_len) {
4498 /* read response payload is in buf */
4499 WARN_ONCE(npages > 0, "read data can be either in buf or in pages");
4500 iov.iov_base = buf + data_offset;
4501 iov.iov_len = data_len;
David Howellsaa563d72018-10-20 00:57:56 +01004502 iov_iter_kvec(&iter, WRITE, &iov, 1, data_len);
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004503 } else {
4504 /* read response payload cannot be in both buf and pages */
4505 WARN_ONCE(1, "buf can not contain only a part of read data");
4506 rdata->result = -EIO;
Rohith Surabattulaac873aa2020-10-29 05:03:10 +00004507 if (is_offloaded)
4508 mid->mid_state = MID_RESPONSE_MALFORMED;
4509 else
4510 dequeue_mid(mid, rdata->result);
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004511 return 0;
4512 }
4513
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004514 length = rdata->copy_into_pages(server, rdata, &iter);
4515
4516 kfree(bvec);
4517
4518 if (length < 0)
4519 return length;
4520
Rohith Surabattulaac873aa2020-10-29 05:03:10 +00004521 if (is_offloaded)
4522 mid->mid_state = MID_RESPONSE_RECEIVED;
4523 else
4524 dequeue_mid(mid, false);
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004525 return length;
4526}
4527
Steve French35cf94a2019-09-07 01:09:49 -05004528struct smb2_decrypt_work {
4529 struct work_struct decrypt;
4530 struct TCP_Server_Info *server;
4531 struct page **ppages;
4532 char *buf;
4533 unsigned int npages;
4534 unsigned int len;
4535};
4536
4537
4538static void smb2_decrypt_offload(struct work_struct *work)
4539{
4540 struct smb2_decrypt_work *dw = container_of(work,
4541 struct smb2_decrypt_work, decrypt);
4542 int i, rc;
4543 struct mid_q_entry *mid;
4544
4545 rc = decrypt_raw_data(dw->server, dw->buf, dw->server->vals->read_rsp_size,
Rohith Surabattula62593012020-10-08 09:58:41 +00004546 dw->ppages, dw->npages, dw->len, true);
Steve French35cf94a2019-09-07 01:09:49 -05004547 if (rc) {
4548 cifs_dbg(VFS, "error decrypting rc=%d\n", rc);
4549 goto free_pages;
4550 }
4551
Steve French22553972019-09-13 16:47:31 -05004552 dw->server->lstrp = jiffies;
Rohith Surabattulaac873aa2020-10-29 05:03:10 +00004553 mid = smb2_find_dequeue_mid(dw->server, dw->buf);
Steve French35cf94a2019-09-07 01:09:49 -05004554 if (mid == NULL)
4555 cifs_dbg(FYI, "mid not found\n");
4556 else {
4557 mid->decrypted = true;
4558 rc = handle_read_data(dw->server, mid, dw->buf,
4559 dw->server->vals->read_rsp_size,
Rohith Surabattulade9ac0a2020-10-28 13:42:21 +00004560 dw->ppages, dw->npages, dw->len,
4561 true);
Rohith Surabattula12541002020-10-29 06:07:56 +00004562 if (rc >= 0) {
4563#ifdef CONFIG_CIFS_STATS2
4564 mid->when_received = jiffies;
4565#endif
4566 mid->callback(mid);
4567 } else {
4568 spin_lock(&GlobalMid_Lock);
4569 if (dw->server->tcpStatus == CifsNeedReconnect) {
4570 mid->mid_state = MID_RETRY_NEEDED;
4571 spin_unlock(&GlobalMid_Lock);
4572 mid->callback(mid);
4573 } else {
4574 mid->mid_state = MID_REQUEST_SUBMITTED;
4575 mid->mid_flags &= ~(MID_DELETED);
4576 list_add_tail(&mid->qhead,
4577 &dw->server->pending_mid_q);
4578 spin_unlock(&GlobalMid_Lock);
4579 }
4580 }
Steve French22553972019-09-13 16:47:31 -05004581 cifs_mid_q_entry_release(mid);
Steve French35cf94a2019-09-07 01:09:49 -05004582 }
4583
Steve French35cf94a2019-09-07 01:09:49 -05004584free_pages:
4585 for (i = dw->npages-1; i >= 0; i--)
4586 put_page(dw->ppages[i]);
4587
4588 kfree(dw->ppages);
4589 cifs_small_buf_release(dw->buf);
Steve Frencha08d8972019-10-26 16:00:44 -05004590 kfree(dw);
Steve French35cf94a2019-09-07 01:09:49 -05004591}
4592
4593
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004594static int
Steve French35cf94a2019-09-07 01:09:49 -05004595receive_encrypted_read(struct TCP_Server_Info *server, struct mid_q_entry **mid,
4596 int *num_mids)
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08004597{
4598 char *buf = server->smallbuf;
4599 struct smb2_transform_hdr *tr_hdr = (struct smb2_transform_hdr *)buf;
4600 unsigned int npages;
4601 struct page **pages;
4602 unsigned int len;
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10004603 unsigned int buflen = server->pdu_size;
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08004604 int rc;
4605 int i = 0;
Steve French35cf94a2019-09-07 01:09:49 -05004606 struct smb2_decrypt_work *dw;
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08004607
Steve French35cf94a2019-09-07 01:09:49 -05004608 *num_mids = 1;
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10004609 len = min_t(unsigned int, buflen, server->vals->read_rsp_size +
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08004610 sizeof(struct smb2_transform_hdr)) - HEADER_SIZE(server) + 1;
4611
4612 rc = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1, len);
4613 if (rc < 0)
4614 return rc;
4615 server->total_read += rc;
4616
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10004617 len = le32_to_cpu(tr_hdr->OriginalMessageSize) -
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +11004618 server->vals->read_rsp_size;
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08004619 npages = DIV_ROUND_UP(len, PAGE_SIZE);
4620
4621 pages = kmalloc_array(npages, sizeof(struct page *), GFP_KERNEL);
4622 if (!pages) {
4623 rc = -ENOMEM;
4624 goto discard_data;
4625 }
4626
4627 for (; i < npages; i++) {
4628 pages[i] = alloc_page(GFP_KERNEL|__GFP_HIGHMEM);
4629 if (!pages[i]) {
4630 rc = -ENOMEM;
4631 goto discard_data;
4632 }
4633 }
4634
4635 /* read read data into pages */
4636 rc = read_data_into_pages(server, pages, npages, len);
4637 if (rc)
4638 goto free_pages;
4639
Pavel Shilovsky350be252017-04-10 10:31:33 -07004640 rc = cifs_discard_remaining_data(server);
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08004641 if (rc)
4642 goto free_pages;
4643
Steve French35cf94a2019-09-07 01:09:49 -05004644 /*
4645 * For large reads, offload to different thread for better performance,
4646 * use more cores decrypting which can be expensive
4647 */
4648
Steve French10328c42019-09-09 13:30:15 -05004649 if ((server->min_offload) && (server->in_flight > 1) &&
Steve French563317e2019-09-08 23:22:02 -05004650 (server->pdu_size >= server->min_offload)) {
Steve French35cf94a2019-09-07 01:09:49 -05004651 dw = kmalloc(sizeof(struct smb2_decrypt_work), GFP_KERNEL);
4652 if (dw == NULL)
4653 goto non_offloaded_decrypt;
4654
4655 dw->buf = server->smallbuf;
4656 server->smallbuf = (char *)cifs_small_buf_get();
4657
4658 INIT_WORK(&dw->decrypt, smb2_decrypt_offload);
4659
4660 dw->npages = npages;
4661 dw->server = server;
4662 dw->ppages = pages;
4663 dw->len = len;
Steve Frencha08d8972019-10-26 16:00:44 -05004664 queue_work(decrypt_wq, &dw->decrypt);
Steve French35cf94a2019-09-07 01:09:49 -05004665 *num_mids = 0; /* worker thread takes care of finding mid */
4666 return -1;
4667 }
4668
4669non_offloaded_decrypt:
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10004670 rc = decrypt_raw_data(server, buf, server->vals->read_rsp_size,
Rohith Surabattula62593012020-10-08 09:58:41 +00004671 pages, npages, len, false);
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08004672 if (rc)
4673 goto free_pages;
4674
4675 *mid = smb2_find_mid(server, buf);
4676 if (*mid == NULL)
4677 cifs_dbg(FYI, "mid not found\n");
4678 else {
4679 cifs_dbg(FYI, "mid found\n");
4680 (*mid)->decrypted = true;
4681 rc = handle_read_data(server, *mid, buf,
4682 server->vals->read_rsp_size,
Rohith Surabattulade9ac0a2020-10-28 13:42:21 +00004683 pages, npages, len, false);
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08004684 }
4685
4686free_pages:
4687 for (i = i - 1; i >= 0; i--)
4688 put_page(pages[i]);
4689 kfree(pages);
4690 return rc;
4691discard_data:
Pavel Shilovsky350be252017-04-10 10:31:33 -07004692 cifs_discard_remaining_data(server);
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08004693 goto free_pages;
4694}
4695
4696static int
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004697receive_encrypted_standard(struct TCP_Server_Info *server,
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10004698 struct mid_q_entry **mids, char **bufs,
4699 int *num_mids)
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004700{
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10004701 int ret, length;
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004702 char *buf = server->smallbuf;
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10004703 struct smb2_sync_hdr *shdr;
Ronnie Sahlberg2e964672018-04-09 18:06:26 +10004704 unsigned int pdu_length = server->pdu_size;
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004705 unsigned int buf_size;
4706 struct mid_q_entry *mid_entry;
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10004707 int next_is_large;
4708 char *next_buffer = NULL;
4709
4710 *num_mids = 0;
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004711
4712 /* switch to large buffer if too big for a small one */
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10004713 if (pdu_length > MAX_CIFS_SMALL_BUFFER_SIZE) {
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004714 server->large_buf = true;
4715 memcpy(server->bigbuf, buf, server->total_read);
4716 buf = server->bigbuf;
4717 }
4718
4719 /* now read the rest */
4720 length = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1,
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10004721 pdu_length - HEADER_SIZE(server) + 1);
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004722 if (length < 0)
4723 return length;
4724 server->total_read += length;
4725
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10004726 buf_size = pdu_length - sizeof(struct smb2_transform_hdr);
Rohith Surabattula62593012020-10-08 09:58:41 +00004727 length = decrypt_raw_data(server, buf, buf_size, NULL, 0, 0, false);
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004728 if (length)
4729 return length;
4730
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10004731 next_is_large = server->large_buf;
Pavel Shilovsky3edeb4a2019-07-22 11:38:22 -07004732one_more:
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10004733 shdr = (struct smb2_sync_hdr *)buf;
4734 if (shdr->NextCommand) {
Pavel Shilovsky3edeb4a2019-07-22 11:38:22 -07004735 if (next_is_large)
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10004736 next_buffer = (char *)cifs_buf_get();
Pavel Shilovsky3edeb4a2019-07-22 11:38:22 -07004737 else
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10004738 next_buffer = (char *)cifs_small_buf_get();
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10004739 memcpy(next_buffer,
Pavel Shilovsky3edeb4a2019-07-22 11:38:22 -07004740 buf + le32_to_cpu(shdr->NextCommand),
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10004741 pdu_length - le32_to_cpu(shdr->NextCommand));
4742 }
4743
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004744 mid_entry = smb2_find_mid(server, buf);
4745 if (mid_entry == NULL)
4746 cifs_dbg(FYI, "mid not found\n");
4747 else {
4748 cifs_dbg(FYI, "mid found\n");
4749 mid_entry->decrypted = true;
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10004750 mid_entry->resp_buf_size = server->pdu_size;
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004751 }
4752
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10004753 if (*num_mids >= MAX_COMPOUND) {
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +10004754 cifs_server_dbg(VFS, "too many PDUs in compound\n");
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10004755 return -1;
4756 }
4757 bufs[*num_mids] = buf;
4758 mids[(*num_mids)++] = mid_entry;
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004759
4760 if (mid_entry && mid_entry->handle)
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10004761 ret = mid_entry->handle(server, mid_entry);
4762 else
4763 ret = cifs_handle_standard(server, mid_entry);
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004764
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10004765 if (ret == 0 && shdr->NextCommand) {
4766 pdu_length -= le32_to_cpu(shdr->NextCommand);
4767 server->large_buf = next_is_large;
4768 if (next_is_large)
Pavel Shilovsky3edeb4a2019-07-22 11:38:22 -07004769 server->bigbuf = buf = next_buffer;
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10004770 else
Pavel Shilovsky3edeb4a2019-07-22 11:38:22 -07004771 server->smallbuf = buf = next_buffer;
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10004772 goto one_more;
Pavel Shilovsky3edeb4a2019-07-22 11:38:22 -07004773 } else if (ret != 0) {
4774 /*
4775 * ret != 0 here means that we didn't get to handle_mid() thus
4776 * server->smallbuf and server->bigbuf are still valid. We need
4777 * to free next_buffer because it is not going to be used
4778 * anywhere.
4779 */
4780 if (next_is_large)
4781 free_rsp_buf(CIFS_LARGE_BUFFER, next_buffer);
4782 else
4783 free_rsp_buf(CIFS_SMALL_BUFFER, next_buffer);
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10004784 }
4785
4786 return ret;
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004787}
4788
4789static int
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10004790smb3_receive_transform(struct TCP_Server_Info *server,
4791 struct mid_q_entry **mids, char **bufs, int *num_mids)
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004792{
4793 char *buf = server->smallbuf;
Ronnie Sahlberg2e964672018-04-09 18:06:26 +10004794 unsigned int pdu_length = server->pdu_size;
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004795 struct smb2_transform_hdr *tr_hdr = (struct smb2_transform_hdr *)buf;
4796 unsigned int orig_len = le32_to_cpu(tr_hdr->OriginalMessageSize);
4797
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10004798 if (pdu_length < sizeof(struct smb2_transform_hdr) +
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004799 sizeof(struct smb2_sync_hdr)) {
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +10004800 cifs_server_dbg(VFS, "Transform message is too small (%u)\n",
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004801 pdu_length);
4802 cifs_reconnect(server);
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004803 return -ECONNABORTED;
4804 }
4805
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10004806 if (pdu_length < orig_len + sizeof(struct smb2_transform_hdr)) {
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +10004807 cifs_server_dbg(VFS, "Transform message is broken\n");
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004808 cifs_reconnect(server);
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004809 return -ECONNABORTED;
4810 }
4811
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10004812 /* TODO: add support for compounds containing READ. */
Paul Aurich6d2f84e2018-12-31 14:13:34 -08004813 if (pdu_length > CIFSMaxBufSize + MAX_HEADER_SIZE(server)) {
Steve French35cf94a2019-09-07 01:09:49 -05004814 return receive_encrypted_read(server, &mids[0], num_mids);
Paul Aurich6d2f84e2018-12-31 14:13:34 -08004815 }
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004816
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10004817 return receive_encrypted_standard(server, mids, bufs, num_mids);
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004818}
4819
4820int
4821smb3_handle_read_data(struct TCP_Server_Info *server, struct mid_q_entry *mid)
4822{
4823 char *buf = server->large_buf ? server->bigbuf : server->smallbuf;
4824
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10004825 return handle_read_data(server, mid, buf, server->pdu_size,
Rohith Surabattulade9ac0a2020-10-28 13:42:21 +00004826 NULL, 0, 0, false);
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004827}
4828
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +10004829static int
4830smb2_next_header(char *buf)
4831{
4832 struct smb2_sync_hdr *hdr = (struct smb2_sync_hdr *)buf;
4833 struct smb2_transform_hdr *t_hdr = (struct smb2_transform_hdr *)buf;
4834
4835 if (hdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM)
4836 return sizeof(struct smb2_transform_hdr) +
4837 le32_to_cpu(t_hdr->OriginalMessageSize);
4838
4839 return le32_to_cpu(hdr->NextCommand);
4840}
4841
Aurelien Aptelc847dcc2019-03-14 00:29:17 -05004842static int
4843smb2_make_node(unsigned int xid, struct inode *inode,
4844 struct dentry *dentry, struct cifs_tcon *tcon,
4845 char *full_path, umode_t mode, dev_t dev)
4846{
4847 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
4848 int rc = -EPERM;
Aurelien Aptelc847dcc2019-03-14 00:29:17 -05004849 FILE_ALL_INFO *buf = NULL;
Aurelien Aptel7c065142020-06-04 17:23:55 +02004850 struct cifs_io_parms io_parms = {0};
Aurelien Aptelc847dcc2019-03-14 00:29:17 -05004851 __u32 oplock = 0;
4852 struct cifs_fid fid;
4853 struct cifs_open_parms oparms;
4854 unsigned int bytes_written;
4855 struct win_dev *pdev;
4856 struct kvec iov[2];
4857
4858 /*
4859 * Check if mounted with mount parm 'sfu' mount parm.
4860 * SFU emulation should work with all servers, but only
4861 * supports block and char device (no socket & fifo),
4862 * and was used by default in earlier versions of Windows
4863 */
4864 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL))
4865 goto out;
4866
4867 /*
4868 * TODO: Add ability to create instead via reparse point. Windows (e.g.
4869 * their current NFS server) uses this approach to expose special files
4870 * over SMB2/SMB3 and Samba will do this with SMB3.1.1 POSIX Extensions
4871 */
4872
4873 if (!S_ISCHR(mode) && !S_ISBLK(mode))
4874 goto out;
4875
4876 cifs_dbg(FYI, "sfu compat create special file\n");
4877
4878 buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL);
4879 if (buf == NULL) {
4880 rc = -ENOMEM;
4881 goto out;
4882 }
4883
Aurelien Aptelc847dcc2019-03-14 00:29:17 -05004884 oparms.tcon = tcon;
4885 oparms.cifs_sb = cifs_sb;
4886 oparms.desired_access = GENERIC_WRITE;
Amir Goldstein0f060932020-02-03 21:46:43 +02004887 oparms.create_options = cifs_create_options(cifs_sb, CREATE_NOT_DIR |
4888 CREATE_OPTION_SPECIAL);
Aurelien Aptelc847dcc2019-03-14 00:29:17 -05004889 oparms.disposition = FILE_CREATE;
4890 oparms.path = full_path;
4891 oparms.fid = &fid;
4892 oparms.reconnect = false;
4893
4894 if (tcon->ses->server->oplocks)
4895 oplock = REQ_OPLOCK;
4896 else
4897 oplock = 0;
4898 rc = tcon->ses->server->ops->open(xid, &oparms, &oplock, buf);
4899 if (rc)
4900 goto out;
4901
4902 /*
4903 * BB Do not bother to decode buf since no local inode yet to put
4904 * timestamps in, but we can reuse it safely.
4905 */
4906
4907 pdev = (struct win_dev *)buf;
4908 io_parms.pid = current->tgid;
4909 io_parms.tcon = tcon;
4910 io_parms.offset = 0;
4911 io_parms.length = sizeof(struct win_dev);
4912 iov[1].iov_base = buf;
4913 iov[1].iov_len = sizeof(struct win_dev);
4914 if (S_ISCHR(mode)) {
4915 memcpy(pdev->type, "IntxCHR", 8);
4916 pdev->major = cpu_to_le64(MAJOR(dev));
4917 pdev->minor = cpu_to_le64(MINOR(dev));
4918 rc = tcon->ses->server->ops->sync_write(xid, &fid, &io_parms,
4919 &bytes_written, iov, 1);
4920 } else if (S_ISBLK(mode)) {
4921 memcpy(pdev->type, "IntxBLK", 8);
4922 pdev->major = cpu_to_le64(MAJOR(dev));
4923 pdev->minor = cpu_to_le64(MINOR(dev));
4924 rc = tcon->ses->server->ops->sync_write(xid, &fid, &io_parms,
4925 &bytes_written, iov, 1);
4926 }
4927 tcon->ses->server->ops->close(xid, tcon, &fid);
4928 d_drop(dentry);
4929
4930 /* FIXME: add code here to set EAs */
4931out:
4932 kfree(buf);
4933 return rc;
4934}
4935
4936
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04004937struct smb_version_operations smb20_operations = {
4938 .compare_fids = smb2_compare_fids,
4939 .setup_request = smb2_setup_request,
4940 .setup_async_request = smb2_setup_async_request,
4941 .check_receive = smb2_check_receive,
4942 .add_credits = smb2_add_credits,
4943 .set_credits = smb2_set_credits,
4944 .get_credits_field = smb2_get_credits_field,
4945 .get_credits = smb2_get_credits,
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04004946 .wait_mtu_credits = cifs_wait_mtu_credits,
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04004947 .get_next_mid = smb2_get_next_mid,
Pavel Shilovskyc781af72019-03-04 14:02:50 -08004948 .revert_current_mid = smb2_revert_current_mid,
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04004949 .read_data_offset = smb2_read_data_offset,
4950 .read_data_length = smb2_read_data_length,
4951 .map_error = map_smb2_to_linux_error,
4952 .find_mid = smb2_find_mid,
4953 .check_message = smb2_check_message,
4954 .dump_detail = smb2_dump_detail,
4955 .clear_stats = smb2_clear_stats,
4956 .print_stats = smb2_print_stats,
4957 .is_oplock_break = smb2_is_valid_oplock_break,
Sachin Prabhu38bd4902017-03-03 15:41:38 -08004958 .handle_cancelled_mid = smb2_handle_cancelled_mid,
Sachin Prabhuc11f1df2014-03-11 16:11:47 +00004959 .downgrade_oplock = smb2_downgrade_oplock,
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04004960 .need_neg = smb2_need_neg,
4961 .negotiate = smb2_negotiate,
4962 .negotiate_wsize = smb2_negotiate_wsize,
4963 .negotiate_rsize = smb2_negotiate_rsize,
4964 .sess_setup = SMB2_sess_setup,
4965 .logoff = SMB2_logoff,
4966 .tree_connect = SMB2_tcon,
4967 .tree_disconnect = SMB2_tdis,
Steve French34f62642013-10-09 02:07:00 -05004968 .qfs_tcon = smb2_qfs_tcon,
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04004969 .is_path_accessible = smb2_is_path_accessible,
4970 .can_echo = smb2_can_echo,
4971 .echo = SMB2_echo,
4972 .query_path_info = smb2_query_path_info,
4973 .get_srv_inum = smb2_get_srv_inum,
4974 .query_file_info = smb2_query_file_info,
4975 .set_path_size = smb2_set_path_size,
4976 .set_file_size = smb2_set_file_size,
4977 .set_file_info = smb2_set_file_info,
Steve French64a5cfa2013-10-14 15:31:32 -05004978 .set_compression = smb2_set_compression,
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04004979 .mkdir = smb2_mkdir,
4980 .mkdir_setinfo = smb2_mkdir_setinfo,
4981 .rmdir = smb2_rmdir,
4982 .unlink = smb2_unlink,
4983 .rename = smb2_rename_path,
4984 .create_hardlink = smb2_create_hardlink,
4985 .query_symlink = smb2_query_symlink,
Sachin Prabhu5b23c972016-07-11 16:53:20 +01004986 .query_mf_symlink = smb3_query_mf_symlink,
4987 .create_mf_symlink = smb3_create_mf_symlink,
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04004988 .open = smb2_open_file,
4989 .set_fid = smb2_set_fid,
4990 .close = smb2_close_file,
4991 .flush = smb2_flush_file,
4992 .async_readv = smb2_async_readv,
4993 .async_writev = smb2_async_writev,
4994 .sync_read = smb2_sync_read,
4995 .sync_write = smb2_sync_write,
4996 .query_dir_first = smb2_query_dir_first,
4997 .query_dir_next = smb2_query_dir_next,
4998 .close_dir = smb2_close_dir,
4999 .calc_smb_size = smb2_calc_size,
5000 .is_status_pending = smb2_is_status_pending,
Pavel Shilovsky511c54a2017-07-08 14:32:00 -07005001 .is_session_expired = smb2_is_session_expired,
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04005002 .oplock_response = smb2_oplock_response,
5003 .queryfs = smb2_queryfs,
5004 .mand_lock = smb2_mand_lock,
5005 .mand_unlock_range = smb2_unlock_range,
5006 .push_mand_locks = smb2_push_mandatory_locks,
5007 .get_lease_key = smb2_get_lease_key,
5008 .set_lease_key = smb2_set_lease_key,
5009 .new_lease_key = smb2_new_lease_key,
5010 .calc_signature = smb2_calc_signature,
5011 .is_read_op = smb2_is_read_op,
5012 .set_oplock_level = smb2_set_oplock_level,
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04005013 .create_lease_buf = smb2_create_lease_buf,
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04005014 .parse_lease_buf = smb2_parse_lease_buf,
Sachin Prabhu312bbc52017-04-04 02:12:04 -05005015 .copychunk_range = smb2_copychunk_range,
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04005016 .wp_retry_size = smb2_wp_retry_size,
Pavel Shilovsky52755802014-08-18 20:49:57 +04005017 .dir_needs_close = smb2_dir_needs_close,
Aurelien Aptel9d496402017-02-13 16:16:49 +01005018 .get_dfs_refer = smb2_get_dfs_refer,
Sachin Prabhuef65aae2017-01-18 15:35:57 +05305019 .select_sectype = smb2_select_sectype,
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10005020#ifdef CONFIG_CIFS_XATTR
5021 .query_all_EAs = smb2_query_eas,
Ronnie Sahlberg55175542017-08-24 11:24:56 +10005022 .set_EA = smb2_set_ea,
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10005023#endif /* CIFS_XATTR */
Shirish Pargaonkar2f1afe22017-06-22 22:52:05 -05005024 .get_acl = get_smb2_acl,
5025 .get_acl_by_fid = get_smb2_acl_by_fid,
Shirish Pargaonkar366ed842017-06-28 22:37:32 -05005026 .set_acl = set_smb2_acl,
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +10005027 .next_header = smb2_next_header,
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05005028 .ioctl_query_info = smb2_ioctl_query_info,
Aurelien Aptelc847dcc2019-03-14 00:29:17 -05005029 .make_node = smb2_make_node,
Ronnie Sahlberg2f3ebab2019-04-25 16:45:29 +10005030 .fiemap = smb3_fiemap,
Ronnie Sahlbergdece44e2019-05-15 07:17:02 +10005031 .llseek = smb3_llseek,
Rohith Surabattula8e670f72020-09-18 05:37:28 +00005032 .is_status_io_timeout = smb2_is_status_io_timeout,
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04005033};
5034
Steve French1080ef72011-02-24 18:07:19 +00005035struct smb_version_operations smb21_operations = {
Pavel Shilovsky027e8ee2012-09-19 06:22:43 -07005036 .compare_fids = smb2_compare_fids,
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +04005037 .setup_request = smb2_setup_request,
Pavel Shilovskyc95b8ee2012-07-11 14:45:28 +04005038 .setup_async_request = smb2_setup_async_request,
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +04005039 .check_receive = smb2_check_receive,
Pavel Shilovsky28ea5292012-05-23 16:18:00 +04005040 .add_credits = smb2_add_credits,
5041 .set_credits = smb2_set_credits,
5042 .get_credits_field = smb2_get_credits_field,
5043 .get_credits = smb2_get_credits,
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04005044 .wait_mtu_credits = smb2_wait_mtu_credits,
Pavel Shilovsky9a1c67e2019-01-23 18:15:52 -08005045 .adjust_credits = smb2_adjust_credits,
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +04005046 .get_next_mid = smb2_get_next_mid,
Pavel Shilovskyc781af72019-03-04 14:02:50 -08005047 .revert_current_mid = smb2_revert_current_mid,
Pavel Shilovsky09a47072012-09-18 16:20:29 -07005048 .read_data_offset = smb2_read_data_offset,
5049 .read_data_length = smb2_read_data_length,
5050 .map_error = map_smb2_to_linux_error,
Pavel Shilovsky093b2bd2011-06-08 15:51:07 +04005051 .find_mid = smb2_find_mid,
5052 .check_message = smb2_check_message,
5053 .dump_detail = smb2_dump_detail,
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04005054 .clear_stats = smb2_clear_stats,
5055 .print_stats = smb2_print_stats,
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07005056 .is_oplock_break = smb2_is_valid_oplock_break,
Sachin Prabhu38bd4902017-03-03 15:41:38 -08005057 .handle_cancelled_mid = smb2_handle_cancelled_mid,
Pavel Shilovsky9bd45402019-10-29 16:51:19 -07005058 .downgrade_oplock = smb2_downgrade_oplock,
Pavel Shilovskyec2e4522011-12-27 16:12:43 +04005059 .need_neg = smb2_need_neg,
5060 .negotiate = smb2_negotiate,
Pavel Shilovsky3a3bab52012-09-18 16:20:28 -07005061 .negotiate_wsize = smb2_negotiate_wsize,
5062 .negotiate_rsize = smb2_negotiate_rsize,
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04005063 .sess_setup = SMB2_sess_setup,
5064 .logoff = SMB2_logoff,
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04005065 .tree_connect = SMB2_tcon,
5066 .tree_disconnect = SMB2_tdis,
Steve French34f62642013-10-09 02:07:00 -05005067 .qfs_tcon = smb2_qfs_tcon,
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04005068 .is_path_accessible = smb2_is_path_accessible,
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04005069 .can_echo = smb2_can_echo,
5070 .echo = SMB2_echo,
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04005071 .query_path_info = smb2_query_path_info,
5072 .get_srv_inum = smb2_get_srv_inum,
Pavel Shilovskyb7546bc2012-09-18 16:20:27 -07005073 .query_file_info = smb2_query_file_info,
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07005074 .set_path_size = smb2_set_path_size,
5075 .set_file_size = smb2_set_file_size,
Pavel Shilovsky1feeaac2012-09-18 16:20:32 -07005076 .set_file_info = smb2_set_file_info,
Steve French64a5cfa2013-10-14 15:31:32 -05005077 .set_compression = smb2_set_compression,
Pavel Shilovskya0e73182011-07-19 12:56:37 +04005078 .mkdir = smb2_mkdir,
5079 .mkdir_setinfo = smb2_mkdir_setinfo,
Pavel Shilovsky1a500f02012-07-10 16:14:38 +04005080 .rmdir = smb2_rmdir,
Pavel Shilovskycbe6f432012-09-18 16:20:25 -07005081 .unlink = smb2_unlink,
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07005082 .rename = smb2_rename_path,
Pavel Shilovsky568798c2012-09-18 16:20:31 -07005083 .create_hardlink = smb2_create_hardlink,
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04005084 .query_symlink = smb2_query_symlink,
Steve Frenchc22870e2014-09-16 07:18:19 -05005085 .query_mf_symlink = smb3_query_mf_symlink,
Steve French5ab97572014-09-15 04:49:28 -05005086 .create_mf_symlink = smb3_create_mf_symlink,
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07005087 .open = smb2_open_file,
5088 .set_fid = smb2_set_fid,
5089 .close = smb2_close_file,
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07005090 .flush = smb2_flush_file,
Pavel Shilovsky09a47072012-09-18 16:20:29 -07005091 .async_readv = smb2_async_readv,
Pavel Shilovsky33319142012-09-18 16:20:29 -07005092 .async_writev = smb2_async_writev,
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07005093 .sync_read = smb2_sync_read,
Pavel Shilovsky009d3442012-09-18 16:20:30 -07005094 .sync_write = smb2_sync_write,
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07005095 .query_dir_first = smb2_query_dir_first,
5096 .query_dir_next = smb2_query_dir_next,
5097 .close_dir = smb2_close_dir,
5098 .calc_smb_size = smb2_calc_size,
Pavel Shilovsky2e44b282012-09-18 16:20:33 -07005099 .is_status_pending = smb2_is_status_pending,
Pavel Shilovsky511c54a2017-07-08 14:32:00 -07005100 .is_session_expired = smb2_is_session_expired,
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07005101 .oplock_response = smb2_oplock_response,
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07005102 .queryfs = smb2_queryfs,
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07005103 .mand_lock = smb2_mand_lock,
5104 .mand_unlock_range = smb2_unlock_range,
Pavel Shilovskyb1407992012-09-19 06:22:44 -07005105 .push_mand_locks = smb2_push_mandatory_locks,
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07005106 .get_lease_key = smb2_get_lease_key,
5107 .set_lease_key = smb2_set_lease_key,
5108 .new_lease_key = smb2_new_lease_key,
Steve French38107d42012-12-08 22:08:06 -06005109 .calc_signature = smb2_calc_signature,
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04005110 .is_read_op = smb21_is_read_op,
5111 .set_oplock_level = smb21_set_oplock_level,
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04005112 .create_lease_buf = smb2_create_lease_buf,
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04005113 .parse_lease_buf = smb2_parse_lease_buf,
Sachin Prabhu312bbc52017-04-04 02:12:04 -05005114 .copychunk_range = smb2_copychunk_range,
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04005115 .wp_retry_size = smb2_wp_retry_size,
Pavel Shilovsky52755802014-08-18 20:49:57 +04005116 .dir_needs_close = smb2_dir_needs_close,
Steve French834170c2016-09-30 21:14:26 -05005117 .enum_snapshots = smb3_enum_snapshots,
Steve French2c6251a2020-02-12 22:37:08 -06005118 .notify = smb3_notify,
Aurelien Aptel9d496402017-02-13 16:16:49 +01005119 .get_dfs_refer = smb2_get_dfs_refer,
Sachin Prabhuef65aae2017-01-18 15:35:57 +05305120 .select_sectype = smb2_select_sectype,
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10005121#ifdef CONFIG_CIFS_XATTR
5122 .query_all_EAs = smb2_query_eas,
Ronnie Sahlberg55175542017-08-24 11:24:56 +10005123 .set_EA = smb2_set_ea,
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10005124#endif /* CIFS_XATTR */
Shirish Pargaonkar2f1afe22017-06-22 22:52:05 -05005125 .get_acl = get_smb2_acl,
5126 .get_acl_by_fid = get_smb2_acl_by_fid,
Shirish Pargaonkar366ed842017-06-28 22:37:32 -05005127 .set_acl = set_smb2_acl,
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +10005128 .next_header = smb2_next_header,
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05005129 .ioctl_query_info = smb2_ioctl_query_info,
Aurelien Aptelc847dcc2019-03-14 00:29:17 -05005130 .make_node = smb2_make_node,
Ronnie Sahlberg2f3ebab2019-04-25 16:45:29 +10005131 .fiemap = smb3_fiemap,
Ronnie Sahlbergdece44e2019-05-15 07:17:02 +10005132 .llseek = smb3_llseek,
Rohith Surabattula8e670f72020-09-18 05:37:28 +00005133 .is_status_io_timeout = smb2_is_status_io_timeout,
Steve French38107d42012-12-08 22:08:06 -06005134};
5135
Steve French38107d42012-12-08 22:08:06 -06005136struct smb_version_operations smb30_operations = {
5137 .compare_fids = smb2_compare_fids,
5138 .setup_request = smb2_setup_request,
5139 .setup_async_request = smb2_setup_async_request,
5140 .check_receive = smb2_check_receive,
5141 .add_credits = smb2_add_credits,
5142 .set_credits = smb2_set_credits,
5143 .get_credits_field = smb2_get_credits_field,
5144 .get_credits = smb2_get_credits,
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04005145 .wait_mtu_credits = smb2_wait_mtu_credits,
Pavel Shilovsky9a1c67e2019-01-23 18:15:52 -08005146 .adjust_credits = smb2_adjust_credits,
Steve French38107d42012-12-08 22:08:06 -06005147 .get_next_mid = smb2_get_next_mid,
Pavel Shilovskyc781af72019-03-04 14:02:50 -08005148 .revert_current_mid = smb2_revert_current_mid,
Steve French38107d42012-12-08 22:08:06 -06005149 .read_data_offset = smb2_read_data_offset,
5150 .read_data_length = smb2_read_data_length,
5151 .map_error = map_smb2_to_linux_error,
5152 .find_mid = smb2_find_mid,
5153 .check_message = smb2_check_message,
5154 .dump_detail = smb2_dump_detail,
5155 .clear_stats = smb2_clear_stats,
5156 .print_stats = smb2_print_stats,
Steve French769ee6a2013-06-19 14:15:30 -05005157 .dump_share_caps = smb2_dump_share_caps,
Steve French38107d42012-12-08 22:08:06 -06005158 .is_oplock_break = smb2_is_valid_oplock_break,
Sachin Prabhu38bd4902017-03-03 15:41:38 -08005159 .handle_cancelled_mid = smb2_handle_cancelled_mid,
Pavel Shilovsky9bd45402019-10-29 16:51:19 -07005160 .downgrade_oplock = smb3_downgrade_oplock,
Steve French38107d42012-12-08 22:08:06 -06005161 .need_neg = smb2_need_neg,
5162 .negotiate = smb2_negotiate,
Steve French3d621232018-09-25 15:33:47 -05005163 .negotiate_wsize = smb3_negotiate_wsize,
5164 .negotiate_rsize = smb3_negotiate_rsize,
Steve French38107d42012-12-08 22:08:06 -06005165 .sess_setup = SMB2_sess_setup,
5166 .logoff = SMB2_logoff,
5167 .tree_connect = SMB2_tcon,
5168 .tree_disconnect = SMB2_tdis,
Steven Frenchaf6a12e2013-10-09 20:55:53 -05005169 .qfs_tcon = smb3_qfs_tcon,
Steve French38107d42012-12-08 22:08:06 -06005170 .is_path_accessible = smb2_is_path_accessible,
5171 .can_echo = smb2_can_echo,
5172 .echo = SMB2_echo,
5173 .query_path_info = smb2_query_path_info,
Steve French2e4564b2020-10-22 22:03:14 -05005174 /* WSL tags introduced long after smb2.1, enable for SMB3, 3.11 only */
5175 .query_reparse_tag = smb2_query_reparse_tag,
Steve French38107d42012-12-08 22:08:06 -06005176 .get_srv_inum = smb2_get_srv_inum,
5177 .query_file_info = smb2_query_file_info,
5178 .set_path_size = smb2_set_path_size,
5179 .set_file_size = smb2_set_file_size,
5180 .set_file_info = smb2_set_file_info,
Steve French64a5cfa2013-10-14 15:31:32 -05005181 .set_compression = smb2_set_compression,
Steve French38107d42012-12-08 22:08:06 -06005182 .mkdir = smb2_mkdir,
5183 .mkdir_setinfo = smb2_mkdir_setinfo,
5184 .rmdir = smb2_rmdir,
5185 .unlink = smb2_unlink,
5186 .rename = smb2_rename_path,
5187 .create_hardlink = smb2_create_hardlink,
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04005188 .query_symlink = smb2_query_symlink,
Steve Frenchc22870e2014-09-16 07:18:19 -05005189 .query_mf_symlink = smb3_query_mf_symlink,
Steve French5ab97572014-09-15 04:49:28 -05005190 .create_mf_symlink = smb3_create_mf_symlink,
Steve French38107d42012-12-08 22:08:06 -06005191 .open = smb2_open_file,
5192 .set_fid = smb2_set_fid,
5193 .close = smb2_close_file,
Steve French43f8a6a2019-12-02 21:46:54 -06005194 .close_getattr = smb2_close_getattr,
Steve French38107d42012-12-08 22:08:06 -06005195 .flush = smb2_flush_file,
5196 .async_readv = smb2_async_readv,
5197 .async_writev = smb2_async_writev,
5198 .sync_read = smb2_sync_read,
5199 .sync_write = smb2_sync_write,
5200 .query_dir_first = smb2_query_dir_first,
5201 .query_dir_next = smb2_query_dir_next,
5202 .close_dir = smb2_close_dir,
5203 .calc_smb_size = smb2_calc_size,
5204 .is_status_pending = smb2_is_status_pending,
Pavel Shilovsky511c54a2017-07-08 14:32:00 -07005205 .is_session_expired = smb2_is_session_expired,
Steve French38107d42012-12-08 22:08:06 -06005206 .oplock_response = smb2_oplock_response,
5207 .queryfs = smb2_queryfs,
5208 .mand_lock = smb2_mand_lock,
5209 .mand_unlock_range = smb2_unlock_range,
5210 .push_mand_locks = smb2_push_mandatory_locks,
5211 .get_lease_key = smb2_get_lease_key,
5212 .set_lease_key = smb2_set_lease_key,
5213 .new_lease_key = smb2_new_lease_key,
Steve French373512e2015-12-18 13:05:30 -06005214 .generate_signingkey = generate_smb30signingkey,
Steve French38107d42012-12-08 22:08:06 -06005215 .calc_signature = smb3_calc_signature,
Steve Frenchb3152e22015-06-24 03:17:02 -05005216 .set_integrity = smb3_set_integrity,
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04005217 .is_read_op = smb21_is_read_op,
Pavel Shilovsky42873b02013-09-05 21:30:16 +04005218 .set_oplock_level = smb3_set_oplock_level,
Pavel Shilovskyf0473902013-09-04 13:44:05 +04005219 .create_lease_buf = smb3_create_lease_buf,
5220 .parse_lease_buf = smb3_parse_lease_buf,
Sachin Prabhu312bbc52017-04-04 02:12:04 -05005221 .copychunk_range = smb2_copychunk_range,
Steve Frenchca9e7a12015-10-01 21:40:10 -05005222 .duplicate_extents = smb2_duplicate_extents,
Steve Frenchff1c0382013-11-19 23:44:46 -06005223 .validate_negotiate = smb3_validate_negotiate,
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04005224 .wp_retry_size = smb2_wp_retry_size,
Pavel Shilovsky52755802014-08-18 20:49:57 +04005225 .dir_needs_close = smb2_dir_needs_close,
Steve French31742c52014-08-17 08:38:47 -05005226 .fallocate = smb3_fallocate,
Steve French834170c2016-09-30 21:14:26 -05005227 .enum_snapshots = smb3_enum_snapshots,
Steve Frenchd26c2dd2020-02-06 06:00:14 -06005228 .notify = smb3_notify,
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07005229 .init_transform_rq = smb3_init_transform_rq,
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08005230 .is_transform_hdr = smb3_is_transform_hdr,
5231 .receive_transform = smb3_receive_transform,
Aurelien Aptel9d496402017-02-13 16:16:49 +01005232 .get_dfs_refer = smb2_get_dfs_refer,
Sachin Prabhuef65aae2017-01-18 15:35:57 +05305233 .select_sectype = smb2_select_sectype,
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10005234#ifdef CONFIG_CIFS_XATTR
5235 .query_all_EAs = smb2_query_eas,
Ronnie Sahlberg55175542017-08-24 11:24:56 +10005236 .set_EA = smb2_set_ea,
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10005237#endif /* CIFS_XATTR */
Shirish Pargaonkar2f1afe22017-06-22 22:52:05 -05005238 .get_acl = get_smb2_acl,
5239 .get_acl_by_fid = get_smb2_acl_by_fid,
Shirish Pargaonkar366ed842017-06-28 22:37:32 -05005240 .set_acl = set_smb2_acl,
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +10005241 .next_header = smb2_next_header,
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05005242 .ioctl_query_info = smb2_ioctl_query_info,
Aurelien Aptelc847dcc2019-03-14 00:29:17 -05005243 .make_node = smb2_make_node,
Ronnie Sahlberg2f3ebab2019-04-25 16:45:29 +10005244 .fiemap = smb3_fiemap,
Ronnie Sahlbergdece44e2019-05-15 07:17:02 +10005245 .llseek = smb3_llseek,
Rohith Surabattula8e670f72020-09-18 05:37:28 +00005246 .is_status_io_timeout = smb2_is_status_io_timeout,
Steve French1080ef72011-02-24 18:07:19 +00005247};
5248
Steve Frenchaab18932015-06-23 23:37:11 -05005249struct smb_version_operations smb311_operations = {
5250 .compare_fids = smb2_compare_fids,
5251 .setup_request = smb2_setup_request,
5252 .setup_async_request = smb2_setup_async_request,
5253 .check_receive = smb2_check_receive,
5254 .add_credits = smb2_add_credits,
5255 .set_credits = smb2_set_credits,
5256 .get_credits_field = smb2_get_credits_field,
5257 .get_credits = smb2_get_credits,
5258 .wait_mtu_credits = smb2_wait_mtu_credits,
Pavel Shilovsky9a1c67e2019-01-23 18:15:52 -08005259 .adjust_credits = smb2_adjust_credits,
Steve Frenchaab18932015-06-23 23:37:11 -05005260 .get_next_mid = smb2_get_next_mid,
Pavel Shilovskyc781af72019-03-04 14:02:50 -08005261 .revert_current_mid = smb2_revert_current_mid,
Steve Frenchaab18932015-06-23 23:37:11 -05005262 .read_data_offset = smb2_read_data_offset,
5263 .read_data_length = smb2_read_data_length,
5264 .map_error = map_smb2_to_linux_error,
5265 .find_mid = smb2_find_mid,
5266 .check_message = smb2_check_message,
5267 .dump_detail = smb2_dump_detail,
5268 .clear_stats = smb2_clear_stats,
5269 .print_stats = smb2_print_stats,
5270 .dump_share_caps = smb2_dump_share_caps,
5271 .is_oplock_break = smb2_is_valid_oplock_break,
Sachin Prabhu38bd4902017-03-03 15:41:38 -08005272 .handle_cancelled_mid = smb2_handle_cancelled_mid,
Pavel Shilovsky9bd45402019-10-29 16:51:19 -07005273 .downgrade_oplock = smb3_downgrade_oplock,
Steve Frenchaab18932015-06-23 23:37:11 -05005274 .need_neg = smb2_need_neg,
5275 .negotiate = smb2_negotiate,
Steve French3d621232018-09-25 15:33:47 -05005276 .negotiate_wsize = smb3_negotiate_wsize,
5277 .negotiate_rsize = smb3_negotiate_rsize,
Steve Frenchaab18932015-06-23 23:37:11 -05005278 .sess_setup = SMB2_sess_setup,
5279 .logoff = SMB2_logoff,
5280 .tree_connect = SMB2_tcon,
5281 .tree_disconnect = SMB2_tdis,
5282 .qfs_tcon = smb3_qfs_tcon,
5283 .is_path_accessible = smb2_is_path_accessible,
5284 .can_echo = smb2_can_echo,
5285 .echo = SMB2_echo,
5286 .query_path_info = smb2_query_path_info,
Steve French2e4564b2020-10-22 22:03:14 -05005287 .query_reparse_tag = smb2_query_reparse_tag,
Steve Frenchaab18932015-06-23 23:37:11 -05005288 .get_srv_inum = smb2_get_srv_inum,
5289 .query_file_info = smb2_query_file_info,
5290 .set_path_size = smb2_set_path_size,
5291 .set_file_size = smb2_set_file_size,
5292 .set_file_info = smb2_set_file_info,
5293 .set_compression = smb2_set_compression,
5294 .mkdir = smb2_mkdir,
5295 .mkdir_setinfo = smb2_mkdir_setinfo,
Steve Frenchbea851b2018-06-14 21:56:32 -05005296 .posix_mkdir = smb311_posix_mkdir,
Steve Frenchaab18932015-06-23 23:37:11 -05005297 .rmdir = smb2_rmdir,
5298 .unlink = smb2_unlink,
5299 .rename = smb2_rename_path,
5300 .create_hardlink = smb2_create_hardlink,
5301 .query_symlink = smb2_query_symlink,
5302 .query_mf_symlink = smb3_query_mf_symlink,
5303 .create_mf_symlink = smb3_create_mf_symlink,
5304 .open = smb2_open_file,
5305 .set_fid = smb2_set_fid,
5306 .close = smb2_close_file,
Steve French43f8a6a2019-12-02 21:46:54 -06005307 .close_getattr = smb2_close_getattr,
Steve Frenchaab18932015-06-23 23:37:11 -05005308 .flush = smb2_flush_file,
5309 .async_readv = smb2_async_readv,
5310 .async_writev = smb2_async_writev,
5311 .sync_read = smb2_sync_read,
5312 .sync_write = smb2_sync_write,
5313 .query_dir_first = smb2_query_dir_first,
5314 .query_dir_next = smb2_query_dir_next,
5315 .close_dir = smb2_close_dir,
5316 .calc_smb_size = smb2_calc_size,
5317 .is_status_pending = smb2_is_status_pending,
Pavel Shilovsky511c54a2017-07-08 14:32:00 -07005318 .is_session_expired = smb2_is_session_expired,
Steve Frenchaab18932015-06-23 23:37:11 -05005319 .oplock_response = smb2_oplock_response,
Steve French2d304212018-06-24 23:28:12 -05005320 .queryfs = smb311_queryfs,
Steve Frenchaab18932015-06-23 23:37:11 -05005321 .mand_lock = smb2_mand_lock,
5322 .mand_unlock_range = smb2_unlock_range,
5323 .push_mand_locks = smb2_push_mandatory_locks,
5324 .get_lease_key = smb2_get_lease_key,
5325 .set_lease_key = smb2_set_lease_key,
5326 .new_lease_key = smb2_new_lease_key,
Steve French373512e2015-12-18 13:05:30 -06005327 .generate_signingkey = generate_smb311signingkey,
Steve Frenchaab18932015-06-23 23:37:11 -05005328 .calc_signature = smb3_calc_signature,
Steve Frenchb3152e22015-06-24 03:17:02 -05005329 .set_integrity = smb3_set_integrity,
Steve Frenchaab18932015-06-23 23:37:11 -05005330 .is_read_op = smb21_is_read_op,
5331 .set_oplock_level = smb3_set_oplock_level,
5332 .create_lease_buf = smb3_create_lease_buf,
5333 .parse_lease_buf = smb3_parse_lease_buf,
Sachin Prabhu312bbc52017-04-04 02:12:04 -05005334 .copychunk_range = smb2_copychunk_range,
Steve French02b16662015-06-27 21:18:36 -07005335 .duplicate_extents = smb2_duplicate_extents,
Steve Frenchaab18932015-06-23 23:37:11 -05005336/* .validate_negotiate = smb3_validate_negotiate, */ /* not used in 3.11 */
5337 .wp_retry_size = smb2_wp_retry_size,
5338 .dir_needs_close = smb2_dir_needs_close,
5339 .fallocate = smb3_fallocate,
Steve French834170c2016-09-30 21:14:26 -05005340 .enum_snapshots = smb3_enum_snapshots,
Steve Frenchd26c2dd2020-02-06 06:00:14 -06005341 .notify = smb3_notify,
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07005342 .init_transform_rq = smb3_init_transform_rq,
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08005343 .is_transform_hdr = smb3_is_transform_hdr,
5344 .receive_transform = smb3_receive_transform,
Aurelien Aptel9d496402017-02-13 16:16:49 +01005345 .get_dfs_refer = smb2_get_dfs_refer,
Sachin Prabhuef65aae2017-01-18 15:35:57 +05305346 .select_sectype = smb2_select_sectype,
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10005347#ifdef CONFIG_CIFS_XATTR
5348 .query_all_EAs = smb2_query_eas,
Ronnie Sahlberg55175542017-08-24 11:24:56 +10005349 .set_EA = smb2_set_ea,
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10005350#endif /* CIFS_XATTR */
Ronnie Sahlbergc1777df2018-08-10 11:03:55 +10005351 .get_acl = get_smb2_acl,
5352 .get_acl_by_fid = get_smb2_acl_by_fid,
5353 .set_acl = set_smb2_acl,
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +10005354 .next_header = smb2_next_header,
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05005355 .ioctl_query_info = smb2_ioctl_query_info,
Aurelien Aptelc847dcc2019-03-14 00:29:17 -05005356 .make_node = smb2_make_node,
Ronnie Sahlberg2f3ebab2019-04-25 16:45:29 +10005357 .fiemap = smb3_fiemap,
Ronnie Sahlbergdece44e2019-05-15 07:17:02 +10005358 .llseek = smb3_llseek,
Rohith Surabattula8e670f72020-09-18 05:37:28 +00005359 .is_status_io_timeout = smb2_is_status_io_timeout,
Steve Frenchaab18932015-06-23 23:37:11 -05005360};
Steve Frenchaab18932015-06-23 23:37:11 -05005361
Steve Frenchdd446b12012-11-28 23:21:06 -06005362struct smb_version_values smb20_values = {
5363 .version_string = SMB20_VERSION_STRING,
5364 .protocol_id = SMB20_PROT_ID,
5365 .req_capabilities = 0, /* MBZ */
5366 .large_lock_type = 0,
5367 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
5368 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
5369 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10005370 .header_size = sizeof(struct smb2_sync_hdr),
5371 .header_preamble_size = 0,
Steve Frenchdd446b12012-11-28 23:21:06 -06005372 .max_header_size = MAX_SMB2_HDR_SIZE,
5373 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
5374 .lock_cmd = SMB2_LOCK,
5375 .cap_unix = 0,
5376 .cap_nt_find = SMB2_NT_FIND,
5377 .cap_large_files = SMB2_LARGE_FILES,
Jeff Layton502858822013-06-27 12:45:00 -04005378 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5379 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04005380 .create_lease_size = sizeof(struct create_lease),
Steve Frenchdd446b12012-11-28 23:21:06 -06005381};
5382
Steve French1080ef72011-02-24 18:07:19 +00005383struct smb_version_values smb21_values = {
5384 .version_string = SMB21_VERSION_STRING,
Steve Frenche4aa25e2012-10-01 12:26:22 -05005385 .protocol_id = SMB21_PROT_ID,
5386 .req_capabilities = 0, /* MBZ on negotiate req until SMB3 dialect */
5387 .large_lock_type = 0,
5388 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
5389 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
5390 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10005391 .header_size = sizeof(struct smb2_sync_hdr),
5392 .header_preamble_size = 0,
Steve Frenche4aa25e2012-10-01 12:26:22 -05005393 .max_header_size = MAX_SMB2_HDR_SIZE,
5394 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
5395 .lock_cmd = SMB2_LOCK,
5396 .cap_unix = 0,
5397 .cap_nt_find = SMB2_NT_FIND,
5398 .cap_large_files = SMB2_LARGE_FILES,
Jeff Layton502858822013-06-27 12:45:00 -04005399 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5400 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04005401 .create_lease_size = sizeof(struct create_lease),
Steve Frenche4aa25e2012-10-01 12:26:22 -05005402};
5403
Steve French9764c022017-09-17 10:41:35 -05005404struct smb_version_values smb3any_values = {
5405 .version_string = SMB3ANY_VERSION_STRING,
5406 .protocol_id = SMB302_PROT_ID, /* doesn't matter, send protocol array */
Steve Frenchf8015682018-08-31 15:12:10 -05005407 .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 -05005408 .large_lock_type = 0,
5409 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
5410 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
5411 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10005412 .header_size = sizeof(struct smb2_sync_hdr),
5413 .header_preamble_size = 0,
Steve French9764c022017-09-17 10:41:35 -05005414 .max_header_size = MAX_SMB2_HDR_SIZE,
5415 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
5416 .lock_cmd = SMB2_LOCK,
5417 .cap_unix = 0,
5418 .cap_nt_find = SMB2_NT_FIND,
5419 .cap_large_files = SMB2_LARGE_FILES,
5420 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5421 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5422 .create_lease_size = sizeof(struct create_lease_v2),
5423};
5424
5425struct smb_version_values smbdefault_values = {
5426 .version_string = SMBDEFAULT_VERSION_STRING,
5427 .protocol_id = SMB302_PROT_ID, /* doesn't matter, send protocol array */
Steve Frenchf8015682018-08-31 15:12:10 -05005428 .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 -05005429 .large_lock_type = 0,
5430 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
5431 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
5432 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10005433 .header_size = sizeof(struct smb2_sync_hdr),
5434 .header_preamble_size = 0,
Steve French9764c022017-09-17 10:41:35 -05005435 .max_header_size = MAX_SMB2_HDR_SIZE,
5436 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
5437 .lock_cmd = SMB2_LOCK,
5438 .cap_unix = 0,
5439 .cap_nt_find = SMB2_NT_FIND,
5440 .cap_large_files = SMB2_LARGE_FILES,
5441 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5442 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5443 .create_lease_size = sizeof(struct create_lease_v2),
5444};
5445
Steve Frenche4aa25e2012-10-01 12:26:22 -05005446struct smb_version_values smb30_values = {
5447 .version_string = SMB30_VERSION_STRING,
5448 .protocol_id = SMB30_PROT_ID,
Steve Frenchf8015682018-08-31 15:12:10 -05005449 .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 -07005450 .large_lock_type = 0,
5451 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
5452 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
5453 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10005454 .header_size = sizeof(struct smb2_sync_hdr),
5455 .header_preamble_size = 0,
Pavel Shilovsky093b2bd2011-06-08 15:51:07 +04005456 .max_header_size = MAX_SMB2_HDR_SIZE,
Pavel Shilovsky09a47072012-09-18 16:20:29 -07005457 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +04005458 .lock_cmd = SMB2_LOCK,
Pavel Shilovsky29e20f92012-07-13 13:58:14 +04005459 .cap_unix = 0,
5460 .cap_nt_find = SMB2_NT_FIND,
5461 .cap_large_files = SMB2_LARGE_FILES,
Jeff Layton502858822013-06-27 12:45:00 -04005462 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5463 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
Pavel Shilovskyf0473902013-09-04 13:44:05 +04005464 .create_lease_size = sizeof(struct create_lease_v2),
Steve French1080ef72011-02-24 18:07:19 +00005465};
Steve French20b6d8b2013-06-12 22:48:41 -05005466
5467struct smb_version_values smb302_values = {
5468 .version_string = SMB302_VERSION_STRING,
5469 .protocol_id = SMB302_PROT_ID,
Steve Frenchf8015682018-08-31 15:12:10 -05005470 .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 -05005471 .large_lock_type = 0,
5472 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
5473 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
5474 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10005475 .header_size = sizeof(struct smb2_sync_hdr),
5476 .header_preamble_size = 0,
Steve French20b6d8b2013-06-12 22:48:41 -05005477 .max_header_size = MAX_SMB2_HDR_SIZE,
5478 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
5479 .lock_cmd = SMB2_LOCK,
5480 .cap_unix = 0,
5481 .cap_nt_find = SMB2_NT_FIND,
5482 .cap_large_files = SMB2_LARGE_FILES,
Jeff Layton502858822013-06-27 12:45:00 -04005483 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5484 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
Pavel Shilovskyf0473902013-09-04 13:44:05 +04005485 .create_lease_size = sizeof(struct create_lease_v2),
Steve French20b6d8b2013-06-12 22:48:41 -05005486};
Steve French5f7fbf72014-12-17 22:52:58 -06005487
Steve French5f7fbf72014-12-17 22:52:58 -06005488struct smb_version_values smb311_values = {
5489 .version_string = SMB311_VERSION_STRING,
5490 .protocol_id = SMB311_PROT_ID,
Steve Frenchf8015682018-08-31 15:12:10 -05005491 .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 -06005492 .large_lock_type = 0,
5493 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
5494 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
5495 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10005496 .header_size = sizeof(struct smb2_sync_hdr),
5497 .header_preamble_size = 0,
Steve French5f7fbf72014-12-17 22:52:58 -06005498 .max_header_size = MAX_SMB2_HDR_SIZE,
5499 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
5500 .lock_cmd = SMB2_LOCK,
5501 .cap_unix = 0,
5502 .cap_nt_find = SMB2_NT_FIND,
5503 .cap_large_files = SMB2_LARGE_FILES,
5504 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5505 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5506 .create_lease_size = sizeof(struct create_lease_v2),
5507};