blob: 89269a5c02c7e0e6dccf82e61f9dd1352bcf8823 [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"
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -060027#include "fs_context.h"
Pavel Shilovsky28ea5292012-05-23 16:18:00 +040028
Pavel Shilovskyef68e832019-01-18 17:25:36 -080029/* Change credits for different ops and return the total number of credits */
Pavel Shilovsky28ea5292012-05-23 16:18:00 +040030static int
31change_conf(struct TCP_Server_Info *server)
32{
33 server->credits += server->echo_credits + server->oplock_credits;
34 server->oplock_credits = server->echo_credits = 0;
35 switch (server->credits) {
36 case 0:
Pavel Shilovskyef68e832019-01-18 17:25:36 -080037 return 0;
Pavel Shilovsky28ea5292012-05-23 16:18:00 +040038 case 1:
39 server->echoes = false;
40 server->oplocks = false;
Pavel Shilovsky28ea5292012-05-23 16:18:00 +040041 break;
42 case 2:
43 server->echoes = true;
44 server->oplocks = false;
45 server->echo_credits = 1;
Pavel Shilovsky28ea5292012-05-23 16:18:00 +040046 break;
47 default:
48 server->echoes = true;
Steve Frenche0ddde92015-09-22 09:29:38 -050049 if (enable_oplocks) {
50 server->oplocks = true;
51 server->oplock_credits = 1;
52 } else
53 server->oplocks = false;
54
Pavel Shilovsky28ea5292012-05-23 16:18:00 +040055 server->echo_credits = 1;
Pavel Shilovsky28ea5292012-05-23 16:18:00 +040056 }
57 server->credits -= server->echo_credits + server->oplock_credits;
Pavel Shilovskyef68e832019-01-18 17:25:36 -080058 return server->credits + server->echo_credits + server->oplock_credits;
Pavel Shilovsky28ea5292012-05-23 16:18:00 +040059}
60
61static void
Pavel Shilovsky335b7b62019-01-16 11:12:41 -080062smb2_add_credits(struct TCP_Server_Info *server,
63 const struct cifs_credits *credits, const int optype)
Pavel Shilovsky28ea5292012-05-23 16:18:00 +040064{
Pavel Shilovskyef68e832019-01-18 17:25:36 -080065 int *val, rc = -1;
Shyam Prasad N6d82c272021-02-03 23:20:46 -080066 int scredits, in_flight;
Pavel Shilovsky335b7b62019-01-16 11:12:41 -080067 unsigned int add = credits->value;
68 unsigned int instance = credits->instance;
69 bool reconnect_detected = false;
Shyam Prasad N6d82c272021-02-03 23:20:46 -080070 bool reconnect_with_invalid_credits = false;
Pavel Shilovskyef68e832019-01-18 17:25:36 -080071
Pavel Shilovsky28ea5292012-05-23 16:18:00 +040072 spin_lock(&server->req_lock);
73 val = server->ops->get_credits_field(server, optype);
Steve Frenchb340a4d2018-09-01 01:10:17 -050074
75 /* eg found case where write overlapping reconnect messed up credits */
76 if (((optype & CIFS_OP_MASK) == CIFS_NEG_OP) && (*val != 0))
Shyam Prasad N6d82c272021-02-03 23:20:46 -080077 reconnect_with_invalid_credits = true;
78
Pavel Shilovsky335b7b62019-01-16 11:12:41 -080079 if ((instance == 0) || (instance == server->reconnect_instance))
80 *val += add;
81 else
82 reconnect_detected = true;
Steve Frenchb340a4d2018-09-01 01:10:17 -050083
Steve French141891f2016-09-23 00:44:16 -050084 if (*val > 65000) {
85 *val = 65000; /* Don't get near 64K credits, avoid srv bugs */
Joe Perchesa0a30362020-04-14 22:42:53 -070086 pr_warn_once("server overflowed SMB3 credits\n");
Steve French141891f2016-09-23 00:44:16 -050087 }
Pavel Shilovsky28ea5292012-05-23 16:18:00 +040088 server->in_flight--;
Shyam Prasad N0f56db82021-02-03 22:49:52 -080089 if (server->in_flight == 0 &&
90 ((optype & CIFS_OP_MASK) != CIFS_NEG_OP) &&
91 ((optype & CIFS_OP_MASK) != CIFS_SESS_OP))
Pavel Shilovsky28ea5292012-05-23 16:18:00 +040092 rc = change_conf(server);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -070093 /*
94 * Sometimes server returns 0 credits on oplock break ack - we need to
95 * rebalance credits in this case.
96 */
97 else if (server->in_flight > 0 && server->oplock_credits == 0 &&
98 server->oplocks) {
99 if (server->credits > 1) {
100 server->credits--;
101 server->oplock_credits++;
102 }
103 }
Shyam Prasad N6d82c272021-02-03 23:20:46 -0800104 scredits = *val;
105 in_flight = server->in_flight;
Pavel Shilovsky28ea5292012-05-23 16:18:00 +0400106 spin_unlock(&server->req_lock);
107 wake_up(&server->request_q);
Pavel Shilovskyef68e832019-01-18 17:25:36 -0800108
Shyam Prasad Ncd7b6992020-11-12 08:56:49 -0800109 if (reconnect_detected) {
Shyam Prasad N6d82c272021-02-03 23:20:46 -0800110 trace_smb3_reconnect_detected(server->CurrentMid,
111 server->conn_id, server->hostname, scredits, add, in_flight);
112
Pavel Shilovsky335b7b62019-01-16 11:12:41 -0800113 cifs_dbg(FYI, "trying to put %d credits from the old server instance %d\n",
114 add, instance);
Shyam Prasad Ncd7b6992020-11-12 08:56:49 -0800115 }
Pavel Shilovsky335b7b62019-01-16 11:12:41 -0800116
Shyam Prasad N6d82c272021-02-03 23:20:46 -0800117 if (reconnect_with_invalid_credits) {
118 trace_smb3_reconnect_with_invalid_credits(server->CurrentMid,
119 server->conn_id, server->hostname, scredits, add, in_flight);
120 cifs_dbg(FYI, "Negotiate operation when server credits is non-zero. Optype: %d, server credits: %d, credits added: %d\n",
121 optype, scredits, add);
122 }
123
Pavel Shilovsky82e04572019-01-25 10:56:41 -0800124 if (server->tcpStatus == CifsNeedReconnect
125 || server->tcpStatus == CifsExiting)
Pavel Shilovskyef68e832019-01-18 17:25:36 -0800126 return;
127
128 switch (rc) {
129 case -1:
130 /* change_conf hasn't been executed */
131 break;
132 case 0:
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +1000133 cifs_server_dbg(VFS, "Possible client or server bug - zero credits\n");
Pavel Shilovskyef68e832019-01-18 17:25:36 -0800134 break;
135 case 1:
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +1000136 cifs_server_dbg(VFS, "disabling echoes and oplocks\n");
Pavel Shilovskyef68e832019-01-18 17:25:36 -0800137 break;
138 case 2:
139 cifs_dbg(FYI, "disabling oplocks\n");
140 break;
141 default:
Shyam Prasad N6d82c272021-02-03 23:20:46 -0800142 /* change_conf rebalanced credits for different types */
143 break;
Pavel Shilovskyef68e832019-01-18 17:25:36 -0800144 }
Shyam Prasad N6d82c272021-02-03 23:20:46 -0800145
146 trace_smb3_add_credits(server->CurrentMid,
147 server->conn_id, server->hostname, scredits, add, in_flight);
148 cifs_dbg(FYI, "%s: added %u credits total=%d\n", __func__, add, scredits);
Pavel Shilovsky28ea5292012-05-23 16:18:00 +0400149}
150
151static void
152smb2_set_credits(struct TCP_Server_Info *server, const int val)
153{
Shyam Prasad N6d82c272021-02-03 23:20:46 -0800154 int scredits, in_flight;
155
Pavel Shilovsky28ea5292012-05-23 16:18:00 +0400156 spin_lock(&server->req_lock);
157 server->credits = val;
Steve French9e1a37d2018-09-19 02:38:17 -0500158 if (val == 1)
159 server->reconnect_instance++;
Shyam Prasad N6d82c272021-02-03 23:20:46 -0800160 scredits = server->credits;
161 in_flight = server->in_flight;
Pavel Shilovsky28ea5292012-05-23 16:18:00 +0400162 spin_unlock(&server->req_lock);
Shyam Prasad Ncd7b6992020-11-12 08:56:49 -0800163
164 trace_smb3_set_credits(server->CurrentMid,
Shyam Prasad N6d82c272021-02-03 23:20:46 -0800165 server->conn_id, server->hostname, scredits, val, in_flight);
Shyam Prasad Ncd7b6992020-11-12 08:56:49 -0800166 cifs_dbg(FYI, "%s: set %u credits\n", __func__, val);
167
Steve French6e4d3bb2018-09-22 11:25:04 -0500168 /* don't log while holding the lock */
169 if (val == 1)
170 cifs_dbg(FYI, "set credits to 1 due to smb2 reconnect\n");
Pavel Shilovsky28ea5292012-05-23 16:18:00 +0400171}
172
173static int *
174smb2_get_credits_field(struct TCP_Server_Info *server, const int optype)
175{
176 switch (optype) {
177 case CIFS_ECHO_OP:
178 return &server->echo_credits;
179 case CIFS_OBREAK_OP:
180 return &server->oplock_credits;
181 default:
182 return &server->credits;
183 }
184}
185
186static unsigned int
187smb2_get_credits(struct mid_q_entry *mid)
188{
Pavel Shilovsky86a79642019-11-21 11:35:13 -0800189 return mid->credits_received;
Pavel Shilovsky28ea5292012-05-23 16:18:00 +0400190}
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400191
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +0400192static int
193smb2_wait_mtu_credits(struct TCP_Server_Info *server, unsigned int size,
Pavel Shilovsky335b7b62019-01-16 11:12:41 -0800194 unsigned int *num, struct cifs_credits *credits)
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +0400195{
196 int rc = 0;
Shyam Prasad N6d82c272021-02-03 23:20:46 -0800197 unsigned int scredits, in_flight;
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +0400198
199 spin_lock(&server->req_lock);
200 while (1) {
201 if (server->credits <= 0) {
202 spin_unlock(&server->req_lock);
203 cifs_num_waiters_inc(server);
204 rc = wait_event_killable(server->request_q,
Ronnie Sahlbergb227d212019-03-08 12:58:20 +1000205 has_credits(server, &server->credits, 1));
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +0400206 cifs_num_waiters_dec(server);
207 if (rc)
208 return rc;
209 spin_lock(&server->req_lock);
210 } else {
211 if (server->tcpStatus == CifsExiting) {
212 spin_unlock(&server->req_lock);
213 return -ENOENT;
214 }
215
216 scredits = server->credits;
217 /* can deadlock with reopen */
Pavel Shilovskyacc58d02019-01-17 08:21:24 -0800218 if (scredits <= 8) {
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +0400219 *num = SMB2_MAX_BUFFER_SIZE;
Pavel Shilovsky335b7b62019-01-16 11:12:41 -0800220 credits->value = 0;
221 credits->instance = 0;
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +0400222 break;
223 }
224
Pavel Shilovskyacc58d02019-01-17 08:21:24 -0800225 /* leave some credits for reopen and other ops */
226 scredits -= 8;
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +0400227 *num = min_t(unsigned int, size,
228 scredits * SMB2_MAX_BUFFER_SIZE);
229
Pavel Shilovsky335b7b62019-01-16 11:12:41 -0800230 credits->value =
231 DIV_ROUND_UP(*num, SMB2_MAX_BUFFER_SIZE);
232 credits->instance = server->reconnect_instance;
233 server->credits -= credits->value;
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +0400234 server->in_flight++;
Steve French1b63f182019-09-09 22:57:11 -0500235 if (server->in_flight > server->max_in_flight)
236 server->max_in_flight = server->in_flight;
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +0400237 break;
238 }
239 }
Shyam Prasad N6d82c272021-02-03 23:20:46 -0800240 scredits = server->credits;
241 in_flight = server->in_flight;
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +0400242 spin_unlock(&server->req_lock);
Shyam Prasad Ncd7b6992020-11-12 08:56:49 -0800243
244 trace_smb3_add_credits(server->CurrentMid,
Shyam Prasad N6d82c272021-02-03 23:20:46 -0800245 server->conn_id, server->hostname, scredits, -(credits->value), in_flight);
Shyam Prasad Ncd7b6992020-11-12 08:56:49 -0800246 cifs_dbg(FYI, "%s: removed %u credits total=%d\n",
247 __func__, credits->value, scredits);
248
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +0400249 return rc;
250}
251
Pavel Shilovsky9a1c67e2019-01-23 18:15:52 -0800252static int
253smb2_adjust_credits(struct TCP_Server_Info *server,
254 struct cifs_credits *credits,
255 const unsigned int payload_size)
256{
257 int new_val = DIV_ROUND_UP(payload_size, SMB2_MAX_BUFFER_SIZE);
Shyam Prasad N6d82c272021-02-03 23:20:46 -0800258 int scredits, in_flight;
Pavel Shilovsky9a1c67e2019-01-23 18:15:52 -0800259
260 if (!credits->value || credits->value == new_val)
261 return 0;
262
263 if (credits->value < new_val) {
Shyam Prasad Ncd7b6992020-11-12 08:56:49 -0800264 trace_smb3_too_many_credits(server->CurrentMid,
Shyam Prasad N6d82c272021-02-03 23:20:46 -0800265 server->conn_id, server->hostname, 0, credits->value - new_val, 0);
Shyam Prasad Ncd7b6992020-11-12 08:56:49 -0800266 cifs_server_dbg(VFS, "request has less credits (%d) than required (%d)",
267 credits->value, new_val);
268
Pavel Shilovsky9a1c67e2019-01-23 18:15:52 -0800269 return -ENOTSUPP;
270 }
271
272 spin_lock(&server->req_lock);
273
274 if (server->reconnect_instance != credits->instance) {
Shyam Prasad N6d82c272021-02-03 23:20:46 -0800275 scredits = server->credits;
276 in_flight = server->in_flight;
Pavel Shilovsky9a1c67e2019-01-23 18:15:52 -0800277 spin_unlock(&server->req_lock);
Shyam Prasad N6d82c272021-02-03 23:20:46 -0800278
Shyam Prasad Ncd7b6992020-11-12 08:56:49 -0800279 trace_smb3_reconnect_detected(server->CurrentMid,
Shyam Prasad N6d82c272021-02-03 23:20:46 -0800280 server->conn_id, server->hostname, scredits,
281 credits->value - new_val, in_flight);
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +1000282 cifs_server_dbg(VFS, "trying to return %d credits to old session\n",
Pavel Shilovsky9a1c67e2019-01-23 18:15:52 -0800283 credits->value - new_val);
284 return -EAGAIN;
285 }
286
287 server->credits += credits->value - new_val;
Shyam Prasad Ncd7b6992020-11-12 08:56:49 -0800288 scredits = server->credits;
Shyam Prasad N6d82c272021-02-03 23:20:46 -0800289 in_flight = server->in_flight;
Pavel Shilovsky9a1c67e2019-01-23 18:15:52 -0800290 spin_unlock(&server->req_lock);
291 wake_up(&server->request_q);
Shyam Prasad Ncd7b6992020-11-12 08:56:49 -0800292
293 trace_smb3_add_credits(server->CurrentMid,
Shyam Prasad N6d82c272021-02-03 23:20:46 -0800294 server->conn_id, server->hostname, scredits,
295 credits->value - new_val, in_flight);
Shyam Prasad Ncd7b6992020-11-12 08:56:49 -0800296 cifs_dbg(FYI, "%s: adjust added %u credits total=%d\n",
297 __func__, credits->value - new_val, scredits);
298
Shyam Prasad N6d82c272021-02-03 23:20:46 -0800299 credits->value = new_val;
300
Pavel Shilovsky9a1c67e2019-01-23 18:15:52 -0800301 return 0;
302}
303
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400304static __u64
305smb2_get_next_mid(struct TCP_Server_Info *server)
306{
307 __u64 mid;
308 /* for SMB2 we need the current value */
309 spin_lock(&GlobalMid_Lock);
310 mid = server->CurrentMid++;
311 spin_unlock(&GlobalMid_Lock);
312 return mid;
313}
Steve French1080ef72011-02-24 18:07:19 +0000314
Pavel Shilovskyc781af72019-03-04 14:02:50 -0800315static void
316smb2_revert_current_mid(struct TCP_Server_Info *server, const unsigned int val)
317{
318 spin_lock(&GlobalMid_Lock);
319 if (server->CurrentMid >= val)
320 server->CurrentMid -= val;
321 spin_unlock(&GlobalMid_Lock);
322}
323
Pavel Shilovsky093b2bd2011-06-08 15:51:07 +0400324static struct mid_q_entry *
Rohith Surabattulaac873aa2020-10-29 05:03:10 +0000325__smb2_find_mid(struct TCP_Server_Info *server, char *buf, bool dequeue)
Pavel Shilovsky093b2bd2011-06-08 15:51:07 +0400326{
327 struct mid_q_entry *mid;
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +1000328 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700329 __u64 wire_mid = le64_to_cpu(shdr->MessageId);
Pavel Shilovsky093b2bd2011-06-08 15:51:07 +0400330
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700331 if (shdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM) {
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +1000332 cifs_server_dbg(VFS, "Encrypted frame parsing not supported yet\n");
Steve French373512e2015-12-18 13:05:30 -0600333 return NULL;
334 }
335
Pavel Shilovsky093b2bd2011-06-08 15:51:07 +0400336 spin_lock(&GlobalMid_Lock);
337 list_for_each_entry(mid, &server->pending_mid_q, qhead) {
Sachin Prabhu9235d092014-12-09 17:37:00 +0000338 if ((mid->mid == wire_mid) &&
Pavel Shilovsky093b2bd2011-06-08 15:51:07 +0400339 (mid->mid_state == MID_REQUEST_SUBMITTED) &&
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700340 (mid->command == shdr->Command)) {
Lars Persson696e4202018-06-25 14:05:25 +0200341 kref_get(&mid->refcount);
Rohith Surabattulaac873aa2020-10-29 05:03:10 +0000342 if (dequeue) {
343 list_del_init(&mid->qhead);
344 mid->mid_flags |= MID_DELETED;
345 }
Pavel Shilovsky093b2bd2011-06-08 15:51:07 +0400346 spin_unlock(&GlobalMid_Lock);
347 return mid;
348 }
349 }
350 spin_unlock(&GlobalMid_Lock);
351 return NULL;
352}
353
Rohith Surabattulaac873aa2020-10-29 05:03:10 +0000354static struct mid_q_entry *
355smb2_find_mid(struct TCP_Server_Info *server, char *buf)
356{
357 return __smb2_find_mid(server, buf, false);
358}
359
360static struct mid_q_entry *
361smb2_find_dequeue_mid(struct TCP_Server_Info *server, char *buf)
362{
363 return __smb2_find_mid(server, buf, true);
364}
365
Pavel Shilovsky093b2bd2011-06-08 15:51:07 +0400366static void
Ronnie Sahlberg14547f72018-04-22 14:45:53 -0600367smb2_dump_detail(void *buf, struct TCP_Server_Info *server)
Pavel Shilovsky093b2bd2011-06-08 15:51:07 +0400368{
369#ifdef CONFIG_CIFS_DEBUG2
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +1000370 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
Pavel Shilovsky093b2bd2011-06-08 15:51:07 +0400371
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +1000372 cifs_server_dbg(VFS, "Cmd: %d Err: 0x%x Flags: 0x%x Mid: %llu Pid: %d\n",
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700373 shdr->Command, shdr->Status, shdr->Flags, shdr->MessageId,
374 shdr->ProcessId);
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +1000375 cifs_server_dbg(VFS, "smb buf %p len %u\n", buf,
Steve French71992e622018-05-06 15:58:51 -0500376 server->ops->calc_smb_size(buf, server));
Pavel Shilovsky093b2bd2011-06-08 15:51:07 +0400377#endif
378}
379
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400380static bool
381smb2_need_neg(struct TCP_Server_Info *server)
382{
383 return server->max_read == 0;
384}
385
386static int
387smb2_negotiate(const unsigned int xid, struct cifs_ses *ses)
388{
389 int rc;
Christoph Probsta205d502019-05-08 21:36:25 +0200390
Aurelien Aptelf6a6bf72019-09-20 06:22:14 +0200391 cifs_ses_server(ses)->CurrentMid = 0;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400392 rc = SMB2_negotiate(xid, ses);
393 /* BB we probably don't need to retry with modern servers */
394 if (rc == -EAGAIN)
395 rc = -EHOSTDOWN;
396 return rc;
397}
398
Pavel Shilovsky3a3bab52012-09-18 16:20:28 -0700399static unsigned int
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -0600400smb2_negotiate_wsize(struct cifs_tcon *tcon, struct smb3_fs_context *ctx)
Pavel Shilovsky3a3bab52012-09-18 16:20:28 -0700401{
402 struct TCP_Server_Info *server = tcon->ses->server;
403 unsigned int wsize;
404
405 /* start with specified wsize, or default */
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -0600406 wsize = ctx->wsize ? ctx->wsize : CIFS_DEFAULT_IOSIZE;
Pavel Shilovsky3a3bab52012-09-18 16:20:28 -0700407 wsize = min_t(unsigned int, wsize, server->max_write);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +0400408 if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
409 wsize = min_t(unsigned int, wsize, SMB2_MAX_BUFFER_SIZE);
Pavel Shilovsky3a3bab52012-09-18 16:20:28 -0700410
Pavel Shilovsky3a3bab52012-09-18 16:20:28 -0700411 return wsize;
412}
413
414static unsigned int
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -0600415smb3_negotiate_wsize(struct cifs_tcon *tcon, struct smb3_fs_context *ctx)
Steve French3d621232018-09-25 15:33:47 -0500416{
417 struct TCP_Server_Info *server = tcon->ses->server;
418 unsigned int wsize;
419
420 /* start with specified wsize, or default */
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -0600421 wsize = ctx->wsize ? ctx->wsize : SMB3_DEFAULT_IOSIZE;
Steve French3d621232018-09-25 15:33:47 -0500422 wsize = min_t(unsigned int, wsize, server->max_write);
423#ifdef CONFIG_CIFS_SMB_DIRECT
424 if (server->rdma) {
425 if (server->sign)
Long Lif7950cb2020-03-26 19:42:24 -0700426 /*
427 * Account for SMB2 data transfer packet header and
428 * possible encryption header
429 */
Steve French3d621232018-09-25 15:33:47 -0500430 wsize = min_t(unsigned int,
Long Lif7950cb2020-03-26 19:42:24 -0700431 wsize,
432 server->smbd_conn->max_fragmented_send_size -
433 SMB2_READWRITE_PDU_HEADER_SIZE -
434 sizeof(struct smb2_transform_hdr));
Steve French3d621232018-09-25 15:33:47 -0500435 else
436 wsize = min_t(unsigned int,
437 wsize, server->smbd_conn->max_readwrite_size);
438 }
439#endif
440 if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
441 wsize = min_t(unsigned int, wsize, SMB2_MAX_BUFFER_SIZE);
442
443 return wsize;
444}
445
446static unsigned int
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -0600447smb2_negotiate_rsize(struct cifs_tcon *tcon, struct smb3_fs_context *ctx)
Pavel Shilovsky3a3bab52012-09-18 16:20:28 -0700448{
449 struct TCP_Server_Info *server = tcon->ses->server;
450 unsigned int rsize;
451
452 /* start with specified rsize, or default */
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -0600453 rsize = ctx->rsize ? ctx->rsize : CIFS_DEFAULT_IOSIZE;
Pavel Shilovsky3a3bab52012-09-18 16:20:28 -0700454 rsize = min_t(unsigned int, rsize, server->max_read);
Pavel Shilovskybed9da02014-06-25 11:28:57 +0400455
456 if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
457 rsize = min_t(unsigned int, rsize, SMB2_MAX_BUFFER_SIZE);
Pavel Shilovsky3a3bab52012-09-18 16:20:28 -0700458
Pavel Shilovsky3a3bab52012-09-18 16:20:28 -0700459 return rsize;
460}
461
Steve French3d621232018-09-25 15:33:47 -0500462static unsigned int
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -0600463smb3_negotiate_rsize(struct cifs_tcon *tcon, struct smb3_fs_context *ctx)
Steve French3d621232018-09-25 15:33:47 -0500464{
465 struct TCP_Server_Info *server = tcon->ses->server;
466 unsigned int rsize;
467
468 /* start with specified rsize, or default */
Ronnie Sahlberg3fa1c6d2020-12-09 23:07:12 -0600469 rsize = ctx->rsize ? ctx->rsize : SMB3_DEFAULT_IOSIZE;
Steve French3d621232018-09-25 15:33:47 -0500470 rsize = min_t(unsigned int, rsize, server->max_read);
471#ifdef CONFIG_CIFS_SMB_DIRECT
472 if (server->rdma) {
473 if (server->sign)
Long Lif7950cb2020-03-26 19:42:24 -0700474 /*
475 * Account for SMB2 data transfer packet header and
476 * possible encryption header
477 */
Steve French3d621232018-09-25 15:33:47 -0500478 rsize = min_t(unsigned int,
Long Lif7950cb2020-03-26 19:42:24 -0700479 rsize,
480 server->smbd_conn->max_fragmented_recv_size -
481 SMB2_READWRITE_PDU_HEADER_SIZE -
482 sizeof(struct smb2_transform_hdr));
Steve French3d621232018-09-25 15:33:47 -0500483 else
484 rsize = min_t(unsigned int,
485 rsize, server->smbd_conn->max_readwrite_size);
486 }
487#endif
488
489 if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
490 rsize = min_t(unsigned int, rsize, SMB2_MAX_BUFFER_SIZE);
491
492 return rsize;
493}
Aurelien Aptelfe856be2018-06-14 17:04:51 +0200494
495static int
496parse_server_interfaces(struct network_interface_info_ioctl_rsp *buf,
497 size_t buf_len,
498 struct cifs_server_iface **iface_list,
499 size_t *iface_count)
500{
501 struct network_interface_info_ioctl_rsp *p;
502 struct sockaddr_in *addr4;
503 struct sockaddr_in6 *addr6;
504 struct iface_info_ipv4 *p4;
505 struct iface_info_ipv6 *p6;
506 struct cifs_server_iface *info;
507 ssize_t bytes_left;
508 size_t next = 0;
509 int nb_iface = 0;
510 int rc = 0;
511
512 *iface_list = NULL;
513 *iface_count = 0;
514
515 /*
516 * Fist pass: count and sanity check
517 */
518
519 bytes_left = buf_len;
520 p = buf;
521 while (bytes_left >= sizeof(*p)) {
522 nb_iface++;
523 next = le32_to_cpu(p->Next);
524 if (!next) {
525 bytes_left -= sizeof(*p);
526 break;
527 }
528 p = (struct network_interface_info_ioctl_rsp *)((u8 *)p+next);
529 bytes_left -= next;
530 }
531
532 if (!nb_iface) {
533 cifs_dbg(VFS, "%s: malformed interface info\n", __func__);
534 rc = -EINVAL;
535 goto out;
536 }
537
Steve Frenchebcd6de2020-12-08 21:13:31 -0600538 /* Azure rounds the buffer size up 8, to a 16 byte boundary */
539 if ((bytes_left > 8) || p->Next)
Aurelien Aptelfe856be2018-06-14 17:04:51 +0200540 cifs_dbg(VFS, "%s: incomplete interface info\n", __func__);
541
542
543 /*
544 * Second pass: extract info to internal structure
545 */
546
547 *iface_list = kcalloc(nb_iface, sizeof(**iface_list), GFP_KERNEL);
548 if (!*iface_list) {
549 rc = -ENOMEM;
550 goto out;
551 }
552
553 info = *iface_list;
554 bytes_left = buf_len;
555 p = buf;
556 while (bytes_left >= sizeof(*p)) {
557 info->speed = le64_to_cpu(p->LinkSpeed);
558 info->rdma_capable = le32_to_cpu(p->Capability & RDMA_CAPABLE);
559 info->rss_capable = le32_to_cpu(p->Capability & RSS_CAPABLE);
560
561 cifs_dbg(FYI, "%s: adding iface %zu\n", __func__, *iface_count);
562 cifs_dbg(FYI, "%s: speed %zu bps\n", __func__, info->speed);
563 cifs_dbg(FYI, "%s: capabilities 0x%08x\n", __func__,
564 le32_to_cpu(p->Capability));
565
566 switch (p->Family) {
567 /*
568 * The kernel and wire socket structures have the same
569 * layout and use network byte order but make the
570 * conversion explicit in case either one changes.
571 */
572 case INTERNETWORK:
573 addr4 = (struct sockaddr_in *)&info->sockaddr;
574 p4 = (struct iface_info_ipv4 *)p->Buffer;
575 addr4->sin_family = AF_INET;
576 memcpy(&addr4->sin_addr, &p4->IPv4Address, 4);
577
578 /* [MS-SMB2] 2.2.32.5.1.1 Clients MUST ignore these */
579 addr4->sin_port = cpu_to_be16(CIFS_PORT);
580
581 cifs_dbg(FYI, "%s: ipv4 %pI4\n", __func__,
582 &addr4->sin_addr);
583 break;
584 case INTERNETWORKV6:
585 addr6 = (struct sockaddr_in6 *)&info->sockaddr;
586 p6 = (struct iface_info_ipv6 *)p->Buffer;
587 addr6->sin6_family = AF_INET6;
588 memcpy(&addr6->sin6_addr, &p6->IPv6Address, 16);
589
590 /* [MS-SMB2] 2.2.32.5.1.2 Clients MUST ignore these */
591 addr6->sin6_flowinfo = 0;
592 addr6->sin6_scope_id = 0;
593 addr6->sin6_port = cpu_to_be16(CIFS_PORT);
594
595 cifs_dbg(FYI, "%s: ipv6 %pI6\n", __func__,
596 &addr6->sin6_addr);
597 break;
598 default:
599 cifs_dbg(VFS,
600 "%s: skipping unsupported socket family\n",
601 __func__);
602 goto next_iface;
603 }
604
605 (*iface_count)++;
606 info++;
607next_iface:
608 next = le32_to_cpu(p->Next);
609 if (!next)
610 break;
611 p = (struct network_interface_info_ioctl_rsp *)((u8 *)p+next);
612 bytes_left -= next;
613 }
614
615 if (!*iface_count) {
616 rc = -EINVAL;
617 goto out;
618 }
619
620out:
621 if (rc) {
622 kfree(*iface_list);
623 *iface_count = 0;
624 *iface_list = NULL;
625 }
626 return rc;
627}
628
Aurelien Aptel35adffe2019-09-20 06:29:39 +0200629static int compare_iface(const void *ia, const void *ib)
630{
631 const struct cifs_server_iface *a = (struct cifs_server_iface *)ia;
632 const struct cifs_server_iface *b = (struct cifs_server_iface *)ib;
633
634 return a->speed == b->speed ? 0 : (a->speed > b->speed ? -1 : 1);
635}
Aurelien Aptelfe856be2018-06-14 17:04:51 +0200636
Steve Frenchc481e9f2013-10-14 01:21:53 -0500637static int
638SMB3_request_interfaces(const unsigned int xid, struct cifs_tcon *tcon)
639{
640 int rc;
641 unsigned int ret_data_len = 0;
Aurelien Aptelfe856be2018-06-14 17:04:51 +0200642 struct network_interface_info_ioctl_rsp *out_buf = NULL;
643 struct cifs_server_iface *iface_list;
644 size_t iface_count;
645 struct cifs_ses *ses = tcon->ses;
Steve Frenchc481e9f2013-10-14 01:21:53 -0500646
647 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
648 FSCTL_QUERY_NETWORK_INTERFACE_INFO, true /* is_fsctl */,
649 NULL /* no data input */, 0 /* no data input */,
Steve French153322f2019-03-28 22:32:49 -0500650 CIFSMaxBufSize, (char **)&out_buf, &ret_data_len);
Steve Frenchc3ed4402018-06-28 22:53:39 -0500651 if (rc == -EOPNOTSUPP) {
652 cifs_dbg(FYI,
653 "server does not support query network interfaces\n");
654 goto out;
655 } else if (rc != 0) {
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +1000656 cifs_tcon_dbg(VFS, "error %d on ioctl to get interface list\n", rc);
Aurelien Aptelfe856be2018-06-14 17:04:51 +0200657 goto out;
Steve French9ffc5412014-10-16 15:13:14 -0500658 }
Aurelien Aptelfe856be2018-06-14 17:04:51 +0200659
660 rc = parse_server_interfaces(out_buf, ret_data_len,
661 &iface_list, &iface_count);
662 if (rc)
663 goto out;
664
Aurelien Aptel35adffe2019-09-20 06:29:39 +0200665 /* sort interfaces from fastest to slowest */
666 sort(iface_list, iface_count, sizeof(*iface_list), compare_iface, NULL);
667
Aurelien Aptelfe856be2018-06-14 17:04:51 +0200668 spin_lock(&ses->iface_lock);
669 kfree(ses->iface_list);
670 ses->iface_list = iface_list;
671 ses->iface_count = iface_count;
672 ses->iface_last_update = jiffies;
673 spin_unlock(&ses->iface_lock);
674
675out:
Steve French24df1482016-09-29 04:20:23 -0500676 kfree(out_buf);
Steve Frenchc481e9f2013-10-14 01:21:53 -0500677 return rc;
678}
Steve Frenchc481e9f2013-10-14 01:21:53 -0500679
Ronnie Sahlberg9da6ec72018-07-31 08:48:22 +1000680static void
681smb2_close_cached_fid(struct kref *ref)
Ronnie Sahlberga93864d2018-06-14 06:48:35 +1000682{
Ronnie Sahlberg9da6ec72018-07-31 08:48:22 +1000683 struct cached_fid *cfid = container_of(ref, struct cached_fid,
684 refcount);
685
Ronnie Sahlberga93864d2018-06-14 06:48:35 +1000686 if (cfid->is_valid) {
687 cifs_dbg(FYI, "clear cached root file handle\n");
688 SMB2_close(0, cfid->tcon, cfid->fid->persistent_fid,
689 cfid->fid->volatile_fid);
690 cfid->is_valid = false;
Ronnie Sahlbergb0f6df72019-03-12 13:58:31 +1000691 cfid->file_all_info_is_valid = false;
Pavel Shilovskyd9191312019-12-10 11:44:52 -0800692 cfid->has_lease = false;
Ronnie Sahlberga93864d2018-06-14 06:48:35 +1000693 }
Ronnie Sahlberg9da6ec72018-07-31 08:48:22 +1000694}
695
Ronnie Sahlberg45c0f1a2021-03-09 09:07:29 +1000696void close_cached_dir(struct cached_fid *cfid)
Ronnie Sahlberg9da6ec72018-07-31 08:48:22 +1000697{
698 mutex_lock(&cfid->fid_mutex);
699 kref_put(&cfid->refcount, smb2_close_cached_fid);
Ronnie Sahlberga93864d2018-06-14 06:48:35 +1000700 mutex_unlock(&cfid->fid_mutex);
701}
702
Ronnie Sahlberg45c0f1a2021-03-09 09:07:29 +1000703void close_cached_dir_lease_locked(struct cached_fid *cfid)
Pavel Shilovskyd9191312019-12-10 11:44:52 -0800704{
705 if (cfid->has_lease) {
706 cfid->has_lease = false;
707 kref_put(&cfid->refcount, smb2_close_cached_fid);
708 }
709}
710
Ronnie Sahlberg45c0f1a2021-03-09 09:07:29 +1000711void close_cached_dir_lease(struct cached_fid *cfid)
Pavel Shilovskyd9191312019-12-10 11:44:52 -0800712{
713 mutex_lock(&cfid->fid_mutex);
Ronnie Sahlberg45c0f1a2021-03-09 09:07:29 +1000714 close_cached_dir_lease_locked(cfid);
Pavel Shilovskyd9191312019-12-10 11:44:52 -0800715 mutex_unlock(&cfid->fid_mutex);
716}
717
Ronnie Sahlberg9da6ec72018-07-31 08:48:22 +1000718void
719smb2_cached_lease_break(struct work_struct *work)
720{
721 struct cached_fid *cfid = container_of(work,
722 struct cached_fid, lease_break);
723
Ronnie Sahlberg45c0f1a2021-03-09 09:07:29 +1000724 close_cached_dir_lease(cfid);
Ronnie Sahlberg9da6ec72018-07-31 08:48:22 +1000725}
726
Steve French3d4ef9a2018-04-25 22:19:09 -0500727/*
Ronnie Sahlberg45c0f1a2021-03-09 09:07:29 +1000728 * Open the and cache a directory handle.
729 * Only supported for the root handle.
Steve French3d4ef9a2018-04-25 22:19:09 -0500730 */
Ronnie Sahlberg45c0f1a2021-03-09 09:07:29 +1000731int open_cached_dir(unsigned int xid, struct cifs_tcon *tcon,
Ronnie Sahlberge6eb1952021-03-09 09:07:28 +1000732 const char *path,
Ronnie Sahlberg9e81e8f2020-10-05 12:37:52 +1000733 struct cifs_sb_info *cifs_sb,
734 struct cached_fid **cfid)
Steve French3d4ef9a2018-04-25 22:19:09 -0500735{
Ronnie Sahlbergb0f6df72019-03-12 13:58:31 +1000736 struct cifs_ses *ses = tcon->ses;
737 struct TCP_Server_Info *server = ses->server;
738 struct cifs_open_parms oparms;
739 struct smb2_create_rsp *o_rsp = NULL;
740 struct smb2_query_info_rsp *qi_rsp = NULL;
741 int resp_buftype[2];
742 struct smb_rqst rqst[2];
743 struct kvec rsp_iov[2];
744 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
745 struct kvec qi_iov[1];
746 int rc, flags = 0;
747 __le16 utf16_path = 0; /* Null - since an open of top of share */
Ronnie Sahlberga93864d2018-06-14 06:48:35 +1000748 u8 oplock = SMB2_OPLOCK_LEVEL_II;
Ronnie Sahlberg9e81e8f2020-10-05 12:37:52 +1000749 struct cifs_fid *pfid;
Steve French3d4ef9a2018-04-25 22:19:09 -0500750
Ronnie Sahlberg4df3d972021-03-09 09:07:27 +1000751 if (tcon->nohandlecache)
752 return -ENOTSUPP;
753
Ronnie Sahlberge6eb1952021-03-09 09:07:28 +1000754 if (strlen(path))
755 return -ENOTSUPP;
756
Ronnie Sahlberga93864d2018-06-14 06:48:35 +1000757 mutex_lock(&tcon->crfid.fid_mutex);
758 if (tcon->crfid.is_valid) {
Steve French3d4ef9a2018-04-25 22:19:09 -0500759 cifs_dbg(FYI, "found a cached root file handle\n");
Ronnie Sahlberg9e81e8f2020-10-05 12:37:52 +1000760 *cfid = &tcon->crfid;
Ronnie Sahlberg9da6ec72018-07-31 08:48:22 +1000761 kref_get(&tcon->crfid.refcount);
Ronnie Sahlberga93864d2018-06-14 06:48:35 +1000762 mutex_unlock(&tcon->crfid.fid_mutex);
Steve French3d4ef9a2018-04-25 22:19:09 -0500763 return 0;
764 }
765
Steve French96d9f7e2019-09-12 17:52:54 -0500766 /*
767 * We do not hold the lock for the open because in case
768 * SMB2_open needs to reconnect, it will end up calling
769 * cifs_mark_open_files_invalid() which takes the lock again
770 * thus causing a deadlock
771 */
772
773 mutex_unlock(&tcon->crfid.fid_mutex);
774
Ronnie Sahlbergb0f6df72019-03-12 13:58:31 +1000775 if (smb3_encryption_required(tcon))
776 flags |= CIFS_TRANSFORM_REQ;
Steve French3d4ef9a2018-04-25 22:19:09 -0500777
Paulo Alcantara0fe07812020-04-20 23:44:24 -0300778 if (!server->ops->new_lease_key)
779 return -EIO;
780
Ronnie Sahlberg9e81e8f2020-10-05 12:37:52 +1000781 pfid = tcon->crfid.fid;
Paulo Alcantara0fe07812020-04-20 23:44:24 -0300782 server->ops->new_lease_key(pfid);
783
Ronnie Sahlbergb0f6df72019-03-12 13:58:31 +1000784 memset(rqst, 0, sizeof(rqst));
785 resp_buftype[0] = resp_buftype[1] = CIFS_NO_BUFFER;
786 memset(rsp_iov, 0, sizeof(rsp_iov));
787
788 /* Open */
789 memset(&open_iov, 0, sizeof(open_iov));
790 rqst[0].rq_iov = open_iov;
791 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
792
793 oparms.tcon = tcon;
Amir Goldstein0f060932020-02-03 21:46:43 +0200794 oparms.create_options = cifs_create_options(cifs_sb, 0);
Ronnie Sahlbergb0f6df72019-03-12 13:58:31 +1000795 oparms.desired_access = FILE_READ_ATTRIBUTES;
796 oparms.disposition = FILE_OPEN;
797 oparms.fid = pfid;
798 oparms.reconnect = false;
799
Aurelien Aptel352d96f2020-05-31 12:38:22 -0500800 rc = SMB2_open_init(tcon, server,
801 &rqst[0], &oplock, &oparms, &utf16_path);
Ronnie Sahlbergb0f6df72019-03-12 13:58:31 +1000802 if (rc)
Steve French96d9f7e2019-09-12 17:52:54 -0500803 goto oshr_free;
Ronnie Sahlbergb0f6df72019-03-12 13:58:31 +1000804 smb2_set_next_command(tcon, &rqst[0]);
805
806 memset(&qi_iov, 0, sizeof(qi_iov));
807 rqst[1].rq_iov = qi_iov;
808 rqst[1].rq_nvec = 1;
809
Aurelien Aptel352d96f2020-05-31 12:38:22 -0500810 rc = SMB2_query_info_init(tcon, server,
811 &rqst[1], COMPOUND_FID,
Ronnie Sahlbergb0f6df72019-03-12 13:58:31 +1000812 COMPOUND_FID, FILE_ALL_INFORMATION,
813 SMB2_O_INFO_FILE, 0,
814 sizeof(struct smb2_file_all_info) +
815 PATH_MAX * 2, 0, NULL);
816 if (rc)
Steve French96d9f7e2019-09-12 17:52:54 -0500817 goto oshr_free;
Ronnie Sahlbergb0f6df72019-03-12 13:58:31 +1000818
819 smb2_set_related(&rqst[1]);
820
Aurelien Aptel352d96f2020-05-31 12:38:22 -0500821 rc = compound_send_recv(xid, ses, server,
822 flags, 2, rqst,
Ronnie Sahlbergb0f6df72019-03-12 13:58:31 +1000823 resp_buftype, rsp_iov);
Aurelien Aptel7e5a70a2019-07-17 12:46:28 +0200824 mutex_lock(&tcon->crfid.fid_mutex);
825
826 /*
827 * Now we need to check again as the cached root might have
828 * been successfully re-opened from a concurrent process
829 */
830
831 if (tcon->crfid.is_valid) {
832 /* work was already done */
833
834 /* stash fids for close() later */
835 struct cifs_fid fid = {
836 .persistent_fid = pfid->persistent_fid,
837 .volatile_fid = pfid->volatile_fid,
838 };
839
840 /*
841 * caller expects this func to set pfid to a valid
842 * cached root, so we copy the existing one and get a
843 * reference.
844 */
845 memcpy(pfid, tcon->crfid.fid, sizeof(*pfid));
846 kref_get(&tcon->crfid.refcount);
847
848 mutex_unlock(&tcon->crfid.fid_mutex);
849
850 if (rc == 0) {
851 /* close extra handle outside of crit sec */
852 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
853 }
Xiyu Yang77577de2020-06-13 20:27:09 +0800854 rc = 0;
Aurelien Aptel7e5a70a2019-07-17 12:46:28 +0200855 goto oshr_free;
856 }
857
858 /* Cached root is still invalid, continue normaly */
859
Steve French7dcc82c2019-09-11 00:07:36 -0500860 if (rc) {
861 if (rc == -EREMCHG) {
862 tcon->need_reconnect = true;
Joe Perchesa0a30362020-04-14 22:42:53 -0700863 pr_warn_once("server share %s deleted\n",
864 tcon->treeName);
Steve French7dcc82c2019-09-11 00:07:36 -0500865 }
Ronnie Sahlbergb0f6df72019-03-12 13:58:31 +1000866 goto oshr_exit;
Steve French7dcc82c2019-09-11 00:07:36 -0500867 }
Ronnie Sahlbergb0f6df72019-03-12 13:58:31 +1000868
Steve Frenchd2f15422019-09-22 00:55:46 -0500869 atomic_inc(&tcon->num_remote_opens);
870
Ronnie Sahlbergb0f6df72019-03-12 13:58:31 +1000871 o_rsp = (struct smb2_create_rsp *)rsp_iov[0].iov_base;
872 oparms.fid->persistent_fid = o_rsp->PersistentFileId;
873 oparms.fid->volatile_fid = o_rsp->VolatileFileId;
874#ifdef CONFIG_CIFS_DEBUG2
875 oparms.fid->mid = le64_to_cpu(o_rsp->sync_hdr.MessageId);
876#endif /* CIFS_DEBUG2 */
877
Ronnie Sahlbergb0f6df72019-03-12 13:58:31 +1000878 memcpy(tcon->crfid.fid, pfid, sizeof(struct cifs_fid));
879 tcon->crfid.tcon = tcon;
880 tcon->crfid.is_valid = true;
881 kref_init(&tcon->crfid.refcount);
Ronnie Sahlbergb0f6df72019-03-12 13:58:31 +1000882
Steve French89a5bfa2019-07-18 17:22:18 -0500883 /* BB TBD check to see if oplock level check can be removed below */
Ronnie Sahlberg2f94a3122019-03-28 11:20:02 +1000884 if (o_rsp->OplockLevel == SMB2_OPLOCK_LEVEL_LEASE) {
885 kref_get(&tcon->crfid.refcount);
Pavel Shilovskyd9191312019-12-10 11:44:52 -0800886 tcon->crfid.has_lease = true;
Steve French89a5bfa2019-07-18 17:22:18 -0500887 smb2_parse_contexts(server, o_rsp,
888 &oparms.fid->epoch,
Aurelien Aptel69dda302020-03-02 17:53:22 +0100889 oparms.fid->lease_key, &oplock,
890 NULL, NULL);
Ronnie Sahlberg2f94a3122019-03-28 11:20:02 +1000891 } else
892 goto oshr_exit;
Ronnie Sahlbergb0f6df72019-03-12 13:58:31 +1000893
894 qi_rsp = (struct smb2_query_info_rsp *)rsp_iov[1].iov_base;
895 if (le32_to_cpu(qi_rsp->OutputBufferLength) < sizeof(struct smb2_file_all_info))
896 goto oshr_exit;
Ronnie Sahlberg4811e302019-04-01 09:53:44 +1000897 if (!smb2_validate_and_copy_iov(
Ronnie Sahlbergb0f6df72019-03-12 13:58:31 +1000898 le16_to_cpu(qi_rsp->OutputBufferOffset),
899 sizeof(struct smb2_file_all_info),
900 &rsp_iov[1], sizeof(struct smb2_file_all_info),
Ronnie Sahlberg4811e302019-04-01 09:53:44 +1000901 (char *)&tcon->crfid.file_all_info))
zhengbin720aec02019-12-25 11:30:20 +0800902 tcon->crfid.file_all_info_is_valid = true;
Ronnie Sahlbergb0f6df72019-03-12 13:58:31 +1000903
Aurelien Aptel7e5a70a2019-07-17 12:46:28 +0200904oshr_exit:
Ronnie Sahlberga93864d2018-06-14 06:48:35 +1000905 mutex_unlock(&tcon->crfid.fid_mutex);
Aurelien Aptel7e5a70a2019-07-17 12:46:28 +0200906oshr_free:
Ronnie Sahlbergb0f6df72019-03-12 13:58:31 +1000907 SMB2_open_free(&rqst[0]);
908 SMB2_query_info_free(&rqst[1]);
909 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
910 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
Ronnie Sahlberg9e81e8f2020-10-05 12:37:52 +1000911 if (rc == 0)
912 *cfid = &tcon->crfid;
Steve French3d4ef9a2018-04-25 22:19:09 -0500913 return rc;
914}
915
Steve French34f62642013-10-09 02:07:00 -0500916static void
Amir Goldstein0f060932020-02-03 21:46:43 +0200917smb3_qfs_tcon(const unsigned int xid, struct cifs_tcon *tcon,
918 struct cifs_sb_info *cifs_sb)
Steven Frenchaf6a12e2013-10-09 20:55:53 -0500919{
920 int rc;
921 __le16 srch_path = 0; /* Null - open root of share */
922 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
923 struct cifs_open_parms oparms;
924 struct cifs_fid fid;
Ronnie Sahlberg9e81e8f2020-10-05 12:37:52 +1000925 struct cached_fid *cfid = NULL;
Steven Frenchaf6a12e2013-10-09 20:55:53 -0500926
927 oparms.tcon = tcon;
928 oparms.desired_access = FILE_READ_ATTRIBUTES;
929 oparms.disposition = FILE_OPEN;
Amir Goldstein0f060932020-02-03 21:46:43 +0200930 oparms.create_options = cifs_create_options(cifs_sb, 0);
Steven Frenchaf6a12e2013-10-09 20:55:53 -0500931 oparms.fid = &fid;
932 oparms.reconnect = false;
933
Ronnie Sahlberg45c0f1a2021-03-09 09:07:29 +1000934 rc = open_cached_dir(xid, tcon, "", cifs_sb, &cfid);
Ronnie Sahlberg4df3d972021-03-09 09:07:27 +1000935 if (rc == 0)
936 memcpy(&fid, cfid->fid, sizeof(struct cifs_fid));
937 else
Ronnie Sahlberg9d874c32018-06-08 13:21:18 +1000938 rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL,
Aurelien Aptel69dda302020-03-02 17:53:22 +0100939 NULL, NULL);
Steven Frenchaf6a12e2013-10-09 20:55:53 -0500940 if (rc)
941 return;
942
Steve Frenchc481e9f2013-10-14 01:21:53 -0500943 SMB3_request_interfaces(xid, tcon);
Steve Frenchc481e9f2013-10-14 01:21:53 -0500944
Steven Frenchaf6a12e2013-10-09 20:55:53 -0500945 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
946 FS_ATTRIBUTE_INFORMATION);
947 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
948 FS_DEVICE_INFORMATION);
949 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
Steve French21ba3842018-06-24 23:18:52 -0500950 FS_VOLUME_INFORMATION);
951 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
Steven Frenchaf6a12e2013-10-09 20:55:53 -0500952 FS_SECTOR_SIZE_INFORMATION); /* SMB3 specific */
Ronnie Sahlberg4df3d972021-03-09 09:07:27 +1000953 if (cfid == NULL)
Steve French3d4ef9a2018-04-25 22:19:09 -0500954 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
Ronnie Sahlberg9da6ec72018-07-31 08:48:22 +1000955 else
Ronnie Sahlberg45c0f1a2021-03-09 09:07:29 +1000956 close_cached_dir(cfid);
Steven Frenchaf6a12e2013-10-09 20:55:53 -0500957}
958
959static void
Amir Goldstein0f060932020-02-03 21:46:43 +0200960smb2_qfs_tcon(const unsigned int xid, struct cifs_tcon *tcon,
961 struct cifs_sb_info *cifs_sb)
Steve French34f62642013-10-09 02:07:00 -0500962{
963 int rc;
964 __le16 srch_path = 0; /* Null - open root of share */
965 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
966 struct cifs_open_parms oparms;
967 struct cifs_fid fid;
968
969 oparms.tcon = tcon;
970 oparms.desired_access = FILE_READ_ATTRIBUTES;
971 oparms.disposition = FILE_OPEN;
Amir Goldstein0f060932020-02-03 21:46:43 +0200972 oparms.create_options = cifs_create_options(cifs_sb, 0);
Steve French34f62642013-10-09 02:07:00 -0500973 oparms.fid = &fid;
974 oparms.reconnect = false;
975
Aurelien Aptel69dda302020-03-02 17:53:22 +0100976 rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL,
977 NULL, NULL);
Steve French34f62642013-10-09 02:07:00 -0500978 if (rc)
979 return;
980
Steven French21671142013-10-09 13:36:35 -0500981 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
982 FS_ATTRIBUTE_INFORMATION);
983 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
984 FS_DEVICE_INFORMATION);
Steve French34f62642013-10-09 02:07:00 -0500985 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
Steve French34f62642013-10-09 02:07:00 -0500986}
987
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +0400988static int
989smb2_is_path_accessible(const unsigned int xid, struct cifs_tcon *tcon,
990 struct cifs_sb_info *cifs_sb, const char *full_path)
991{
992 int rc;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +0400993 __le16 *utf16_path;
Pavel Shilovsky2e44b282012-09-18 16:20:33 -0700994 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
Pavel Shilovsky064f6042013-07-09 18:20:30 +0400995 struct cifs_open_parms oparms;
996 struct cifs_fid fid;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +0400997
Ronnie Sahlberga93864d2018-06-14 06:48:35 +1000998 if ((*full_path == 0) && tcon->crfid.is_valid)
Steve French3d4ef9a2018-04-25 22:19:09 -0500999 return 0;
1000
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001001 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
1002 if (!utf16_path)
1003 return -ENOMEM;
1004
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001005 oparms.tcon = tcon;
1006 oparms.desired_access = FILE_READ_ATTRIBUTES;
1007 oparms.disposition = FILE_OPEN;
Amir Goldstein0f060932020-02-03 21:46:43 +02001008 oparms.create_options = cifs_create_options(cifs_sb, 0);
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001009 oparms.fid = &fid;
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001010 oparms.reconnect = false;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001011
Aurelien Aptel69dda302020-03-02 17:53:22 +01001012 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, NULL,
1013 NULL);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001014 if (rc) {
1015 kfree(utf16_path);
1016 return rc;
1017 }
1018
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001019 rc = SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001020 kfree(utf16_path);
1021 return rc;
1022}
1023
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001024static int
1025smb2_get_srv_inum(const unsigned int xid, struct cifs_tcon *tcon,
1026 struct cifs_sb_info *cifs_sb, const char *full_path,
1027 u64 *uniqueid, FILE_ALL_INFO *data)
1028{
1029 *uniqueid = le64_to_cpu(data->IndexNumber);
1030 return 0;
1031}
1032
Pavel Shilovskyb7546bc2012-09-18 16:20:27 -07001033static int
1034smb2_query_file_info(const unsigned int xid, struct cifs_tcon *tcon,
1035 struct cifs_fid *fid, FILE_ALL_INFO *data)
1036{
1037 int rc;
1038 struct smb2_file_all_info *smb2_data;
1039
Pavel Shilovsky1bbe4992014-08-22 13:32:11 +04001040 smb2_data = kzalloc(sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
Pavel Shilovskyb7546bc2012-09-18 16:20:27 -07001041 GFP_KERNEL);
1042 if (smb2_data == NULL)
1043 return -ENOMEM;
1044
1045 rc = SMB2_query_info(xid, tcon, fid->persistent_fid, fid->volatile_fid,
1046 smb2_data);
1047 if (!rc)
1048 move_smb2_info_to_cifs(data, smb2_data);
1049 kfree(smb2_data);
1050 return rc;
1051}
1052
Arnd Bergmann1368f152017-09-05 11:24:15 +02001053#ifdef CONFIG_CIFS_XATTR
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10001054static ssize_t
1055move_smb2_ea_to_cifs(char *dst, size_t dst_size,
1056 struct smb2_file_full_ea_info *src, size_t src_size,
1057 const unsigned char *ea_name)
1058{
1059 int rc = 0;
1060 unsigned int ea_name_len = ea_name ? strlen(ea_name) : 0;
1061 char *name, *value;
Ronnie Sahlberg0c5d6cb2018-10-25 15:43:36 +10001062 size_t buf_size = dst_size;
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10001063 size_t name_len, value_len, user_name_len;
1064
1065 while (src_size > 0) {
1066 name = &src->ea_data[0];
1067 name_len = (size_t)src->ea_name_length;
1068 value = &src->ea_data[src->ea_name_length + 1];
1069 value_len = (size_t)le16_to_cpu(src->ea_value_length);
1070
Christoph Probsta205d502019-05-08 21:36:25 +02001071 if (name_len == 0)
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10001072 break;
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10001073
1074 if (src_size < 8 + name_len + 1 + value_len) {
1075 cifs_dbg(FYI, "EA entry goes beyond length of list\n");
1076 rc = -EIO;
1077 goto out;
1078 }
1079
1080 if (ea_name) {
1081 if (ea_name_len == name_len &&
1082 memcmp(ea_name, name, name_len) == 0) {
1083 rc = value_len;
1084 if (dst_size == 0)
1085 goto out;
1086 if (dst_size < value_len) {
1087 rc = -ERANGE;
1088 goto out;
1089 }
1090 memcpy(dst, value, value_len);
1091 goto out;
1092 }
1093 } else {
1094 /* 'user.' plus a terminating null */
1095 user_name_len = 5 + 1 + name_len;
1096
Ronnie Sahlberg0c5d6cb2018-10-25 15:43:36 +10001097 if (buf_size == 0) {
1098 /* skip copy - calc size only */
1099 rc += user_name_len;
1100 } else if (dst_size >= user_name_len) {
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10001101 dst_size -= user_name_len;
1102 memcpy(dst, "user.", 5);
1103 dst += 5;
1104 memcpy(dst, src->ea_data, name_len);
1105 dst += name_len;
1106 *dst = 0;
1107 ++dst;
Ronnie Sahlberg0c5d6cb2018-10-25 15:43:36 +10001108 rc += user_name_len;
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10001109 } else {
1110 /* stop before overrun buffer */
1111 rc = -ERANGE;
1112 break;
1113 }
1114 }
1115
1116 if (!src->next_entry_offset)
1117 break;
1118
1119 if (src_size < le32_to_cpu(src->next_entry_offset)) {
1120 /* stop before overrun buffer */
1121 rc = -ERANGE;
1122 break;
1123 }
1124 src_size -= le32_to_cpu(src->next_entry_offset);
1125 src = (void *)((char *)src +
1126 le32_to_cpu(src->next_entry_offset));
1127 }
1128
1129 /* didn't find the named attribute */
1130 if (ea_name)
1131 rc = -ENODATA;
1132
1133out:
1134 return (ssize_t)rc;
1135}
1136
1137static ssize_t
1138smb2_query_eas(const unsigned int xid, struct cifs_tcon *tcon,
1139 const unsigned char *path, const unsigned char *ea_name,
1140 char *ea_data, size_t buf_size,
1141 struct cifs_sb_info *cifs_sb)
1142{
1143 int rc;
1144 __le16 *utf16_path;
Ronnie Sahlbergf9793b62018-11-27 09:52:04 +10001145 struct kvec rsp_iov = {NULL, 0};
1146 int buftype = CIFS_NO_BUFFER;
1147 struct smb2_query_info_rsp *rsp;
1148 struct smb2_file_full_ea_info *info = NULL;
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10001149
1150 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
1151 if (!utf16_path)
1152 return -ENOMEM;
1153
Ronnie Sahlbergf9793b62018-11-27 09:52:04 +10001154 rc = smb2_query_info_compound(xid, tcon, utf16_path,
1155 FILE_READ_EA,
1156 FILE_FULL_EA_INFORMATION,
1157 SMB2_O_INFO_FILE,
Ronnie Sahlbergc4627e62019-01-29 12:46:17 +10001158 CIFSMaxBufSize -
1159 MAX_SMB2_CREATE_RESPONSE_SIZE -
1160 MAX_SMB2_CLOSE_RESPONSE_SIZE,
Ronnie Sahlbergf9793b62018-11-27 09:52:04 +10001161 &rsp_iov, &buftype, cifs_sb);
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10001162 if (rc) {
Ronnie Sahlbergf9793b62018-11-27 09:52:04 +10001163 /*
1164 * If ea_name is NULL (listxattr) and there are no EAs,
1165 * return 0 as it's not an error. Otherwise, the specified
1166 * ea_name was not found.
1167 */
1168 if (!ea_name && rc == -ENODATA)
1169 rc = 0;
1170 goto qeas_exit;
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10001171 }
1172
Ronnie Sahlbergf9793b62018-11-27 09:52:04 +10001173 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
1174 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
1175 le32_to_cpu(rsp->OutputBufferLength),
1176 &rsp_iov,
1177 sizeof(struct smb2_file_full_ea_info));
1178 if (rc)
1179 goto qeas_exit;
Ronnie Sahlberg7cb3def2017-09-28 09:39:58 +10001180
Ronnie Sahlbergf9793b62018-11-27 09:52:04 +10001181 info = (struct smb2_file_full_ea_info *)(
1182 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
1183 rc = move_smb2_ea_to_cifs(ea_data, buf_size, info,
1184 le32_to_cpu(rsp->OutputBufferLength), ea_name);
Ronnie Sahlberg7cb3def2017-09-28 09:39:58 +10001185
Ronnie Sahlbergf9793b62018-11-27 09:52:04 +10001186 qeas_exit:
1187 kfree(utf16_path);
1188 free_rsp_buf(buftype, rsp_iov.iov_base);
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10001189 return rc;
1190}
1191
Ronnie Sahlberg55175542017-08-24 11:24:56 +10001192
1193static int
1194smb2_set_ea(const unsigned int xid, struct cifs_tcon *tcon,
1195 const char *path, const char *ea_name, const void *ea_value,
1196 const __u16 ea_value_len, const struct nls_table *nls_codepage,
1197 struct cifs_sb_info *cifs_sb)
1198{
Ronnie Sahlberg0967e542018-11-06 22:52:43 +10001199 struct cifs_ses *ses = tcon->ses;
Aurelien Aptel352d96f2020-05-31 12:38:22 -05001200 struct TCP_Server_Info *server = cifs_pick_channel(ses);
Ronnie Sahlberg0967e542018-11-06 22:52:43 +10001201 __le16 *utf16_path = NULL;
Ronnie Sahlberg55175542017-08-24 11:24:56 +10001202 int ea_name_len = strlen(ea_name);
Paulo Alcantara04ad69c2021-03-08 12:00:50 -03001203 int flags = CIFS_CP_CREATE_CLOSE_OP;
Ronnie Sahlberg55175542017-08-24 11:24:56 +10001204 int len;
Ronnie Sahlberg0967e542018-11-06 22:52:43 +10001205 struct smb_rqst rqst[3];
1206 int resp_buftype[3];
1207 struct kvec rsp_iov[3];
1208 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
1209 struct cifs_open_parms oparms;
1210 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
1211 struct cifs_fid fid;
1212 struct kvec si_iov[SMB2_SET_INFO_IOV_SIZE];
1213 unsigned int size[1];
1214 void *data[1];
1215 struct smb2_file_full_ea_info *ea = NULL;
1216 struct kvec close_iov[1];
Ronnie Sahlberg85db6b72020-02-13 12:14:47 +10001217 struct smb2_query_info_rsp *rsp;
1218 int rc, used_len = 0;
Ronnie Sahlberg0967e542018-11-06 22:52:43 +10001219
1220 if (smb3_encryption_required(tcon))
1221 flags |= CIFS_TRANSFORM_REQ;
Ronnie Sahlberg55175542017-08-24 11:24:56 +10001222
1223 if (ea_name_len > 255)
1224 return -EINVAL;
1225
1226 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
1227 if (!utf16_path)
1228 return -ENOMEM;
1229
Ronnie Sahlberg0967e542018-11-06 22:52:43 +10001230 memset(rqst, 0, sizeof(rqst));
1231 resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
1232 memset(rsp_iov, 0, sizeof(rsp_iov));
1233
Ronnie Sahlberg21094642019-02-07 15:48:44 +10001234 if (ses->server->ops->query_all_EAs) {
1235 if (!ea_value) {
1236 rc = ses->server->ops->query_all_EAs(xid, tcon, path,
1237 ea_name, NULL, 0,
1238 cifs_sb);
1239 if (rc == -ENODATA)
1240 goto sea_exit;
Ronnie Sahlberg85db6b72020-02-13 12:14:47 +10001241 } else {
1242 /* If we are adding a attribute we should first check
1243 * if there will be enough space available to store
1244 * the new EA. If not we should not add it since we
1245 * would not be able to even read the EAs back.
1246 */
1247 rc = smb2_query_info_compound(xid, tcon, utf16_path,
1248 FILE_READ_EA,
1249 FILE_FULL_EA_INFORMATION,
1250 SMB2_O_INFO_FILE,
1251 CIFSMaxBufSize -
1252 MAX_SMB2_CREATE_RESPONSE_SIZE -
1253 MAX_SMB2_CLOSE_RESPONSE_SIZE,
1254 &rsp_iov[1], &resp_buftype[1], cifs_sb);
1255 if (rc == 0) {
1256 rsp = (struct smb2_query_info_rsp *)rsp_iov[1].iov_base;
1257 used_len = le32_to_cpu(rsp->OutputBufferLength);
1258 }
1259 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
1260 resp_buftype[1] = CIFS_NO_BUFFER;
1261 memset(&rsp_iov[1], 0, sizeof(rsp_iov[1]));
1262 rc = 0;
1263
1264 /* Use a fudge factor of 256 bytes in case we collide
1265 * with a different set_EAs command.
1266 */
1267 if(CIFSMaxBufSize - MAX_SMB2_CREATE_RESPONSE_SIZE -
1268 MAX_SMB2_CLOSE_RESPONSE_SIZE - 256 <
1269 used_len + ea_name_len + ea_value_len + 1) {
1270 rc = -ENOSPC;
1271 goto sea_exit;
1272 }
Ronnie Sahlberg21094642019-02-07 15:48:44 +10001273 }
1274 }
1275
Ronnie Sahlberg0967e542018-11-06 22:52:43 +10001276 /* Open */
1277 memset(&open_iov, 0, sizeof(open_iov));
1278 rqst[0].rq_iov = open_iov;
1279 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
1280
1281 memset(&oparms, 0, sizeof(oparms));
Ronnie Sahlberg55175542017-08-24 11:24:56 +10001282 oparms.tcon = tcon;
1283 oparms.desired_access = FILE_WRITE_EA;
1284 oparms.disposition = FILE_OPEN;
Amir Goldstein0f060932020-02-03 21:46:43 +02001285 oparms.create_options = cifs_create_options(cifs_sb, 0);
Ronnie Sahlberg55175542017-08-24 11:24:56 +10001286 oparms.fid = &fid;
1287 oparms.reconnect = false;
1288
Aurelien Aptel352d96f2020-05-31 12:38:22 -05001289 rc = SMB2_open_init(tcon, server,
1290 &rqst[0], &oplock, &oparms, utf16_path);
Ronnie Sahlberg0967e542018-11-06 22:52:43 +10001291 if (rc)
1292 goto sea_exit;
Ronnie Sahlberge77fe732018-12-31 13:43:40 +10001293 smb2_set_next_command(tcon, &rqst[0]);
Ronnie Sahlberg0967e542018-11-06 22:52:43 +10001294
1295
1296 /* Set Info */
1297 memset(&si_iov, 0, sizeof(si_iov));
1298 rqst[1].rq_iov = si_iov;
1299 rqst[1].rq_nvec = 1;
Ronnie Sahlberg55175542017-08-24 11:24:56 +10001300
Vladimir Zapolskiy64b7f672020-10-10 21:25:54 +03001301 len = sizeof(*ea) + ea_name_len + ea_value_len + 1;
Ronnie Sahlberg55175542017-08-24 11:24:56 +10001302 ea = kzalloc(len, GFP_KERNEL);
1303 if (ea == NULL) {
Ronnie Sahlberg0967e542018-11-06 22:52:43 +10001304 rc = -ENOMEM;
1305 goto sea_exit;
Ronnie Sahlberg55175542017-08-24 11:24:56 +10001306 }
1307
1308 ea->ea_name_length = ea_name_len;
1309 ea->ea_value_length = cpu_to_le16(ea_value_len);
1310 memcpy(ea->ea_data, ea_name, ea_name_len + 1);
1311 memcpy(ea->ea_data + ea_name_len + 1, ea_value, ea_value_len);
1312
Ronnie Sahlberg0967e542018-11-06 22:52:43 +10001313 size[0] = len;
1314 data[0] = ea;
1315
Aurelien Aptel352d96f2020-05-31 12:38:22 -05001316 rc = SMB2_set_info_init(tcon, server,
1317 &rqst[1], COMPOUND_FID,
Ronnie Sahlberg0967e542018-11-06 22:52:43 +10001318 COMPOUND_FID, current->tgid,
1319 FILE_FULL_EA_INFORMATION,
1320 SMB2_O_INFO_FILE, 0, data, size);
Ronnie Sahlberge77fe732018-12-31 13:43:40 +10001321 smb2_set_next_command(tcon, &rqst[1]);
Ronnie Sahlberg0967e542018-11-06 22:52:43 +10001322 smb2_set_related(&rqst[1]);
1323
1324
1325 /* Close */
1326 memset(&close_iov, 0, sizeof(close_iov));
1327 rqst[2].rq_iov = close_iov;
1328 rqst[2].rq_nvec = 1;
Aurelien Aptel352d96f2020-05-31 12:38:22 -05001329 rc = SMB2_close_init(tcon, server,
1330 &rqst[2], COMPOUND_FID, COMPOUND_FID, false);
Ronnie Sahlberg0967e542018-11-06 22:52:43 +10001331 smb2_set_related(&rqst[2]);
1332
Aurelien Aptel352d96f2020-05-31 12:38:22 -05001333 rc = compound_send_recv(xid, ses, server,
1334 flags, 3, rqst,
Ronnie Sahlberg0967e542018-11-06 22:52:43 +10001335 resp_buftype, rsp_iov);
Steve Frenchd2f15422019-09-22 00:55:46 -05001336 /* no need to bump num_remote_opens because handle immediately closed */
Ronnie Sahlberg0967e542018-11-06 22:52:43 +10001337
1338 sea_exit:
Paulo Alcantara6aa0c112018-07-04 14:16:16 -03001339 kfree(ea);
Ronnie Sahlberg0967e542018-11-06 22:52:43 +10001340 kfree(utf16_path);
1341 SMB2_open_free(&rqst[0]);
1342 SMB2_set_info_free(&rqst[1]);
1343 SMB2_close_free(&rqst[2]);
1344 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
1345 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
1346 free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
Ronnie Sahlberg55175542017-08-24 11:24:56 +10001347 return rc;
1348}
Arnd Bergmann1368f152017-09-05 11:24:15 +02001349#endif
Ronnie Sahlberg55175542017-08-24 11:24:56 +10001350
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04001351static bool
1352smb2_can_echo(struct TCP_Server_Info *server)
1353{
1354 return server->echoes;
1355}
1356
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001357static void
1358smb2_clear_stats(struct cifs_tcon *tcon)
1359{
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001360 int i;
Christoph Probsta205d502019-05-08 21:36:25 +02001361
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001362 for (i = 0; i < NUMBER_OF_SMB2_COMMANDS; i++) {
1363 atomic_set(&tcon->stats.smb2_stats.smb2_com_sent[i], 0);
1364 atomic_set(&tcon->stats.smb2_stats.smb2_com_failed[i], 0);
1365 }
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001366}
1367
1368static void
Steve French769ee6a2013-06-19 14:15:30 -05001369smb2_dump_share_caps(struct seq_file *m, struct cifs_tcon *tcon)
1370{
1371 seq_puts(m, "\n\tShare Capabilities:");
1372 if (tcon->capabilities & SMB2_SHARE_CAP_DFS)
1373 seq_puts(m, " DFS,");
1374 if (tcon->capabilities & SMB2_SHARE_CAP_CONTINUOUS_AVAILABILITY)
1375 seq_puts(m, " CONTINUOUS AVAILABILITY,");
1376 if (tcon->capabilities & SMB2_SHARE_CAP_SCALEOUT)
1377 seq_puts(m, " SCALEOUT,");
1378 if (tcon->capabilities & SMB2_SHARE_CAP_CLUSTER)
1379 seq_puts(m, " CLUSTER,");
1380 if (tcon->capabilities & SMB2_SHARE_CAP_ASYMMETRIC)
1381 seq_puts(m, " ASYMMETRIC,");
1382 if (tcon->capabilities == 0)
1383 seq_puts(m, " None");
Steven Frenchaf6a12e2013-10-09 20:55:53 -05001384 if (tcon->ss_flags & SSINFO_FLAGS_ALIGNED_DEVICE)
1385 seq_puts(m, " Aligned,");
1386 if (tcon->ss_flags & SSINFO_FLAGS_PARTITION_ALIGNED_ON_DEVICE)
1387 seq_puts(m, " Partition Aligned,");
1388 if (tcon->ss_flags & SSINFO_FLAGS_NO_SEEK_PENALTY)
1389 seq_puts(m, " SSD,");
1390 if (tcon->ss_flags & SSINFO_FLAGS_TRIM_ENABLED)
1391 seq_puts(m, " TRIM-support,");
1392
Steve French769ee6a2013-06-19 14:15:30 -05001393 seq_printf(m, "\tShare Flags: 0x%x", tcon->share_flags);
Steve Frenche0386e42018-05-20 01:27:03 -05001394 seq_printf(m, "\n\ttid: 0x%x", tcon->tid);
Steven Frenchaf6a12e2013-10-09 20:55:53 -05001395 if (tcon->perf_sector_size)
1396 seq_printf(m, "\tOptimal sector size: 0x%x",
1397 tcon->perf_sector_size);
Steve Frenche0386e42018-05-20 01:27:03 -05001398 seq_printf(m, "\tMaximal Access: 0x%x", tcon->maximal_access);
Steve French769ee6a2013-06-19 14:15:30 -05001399}
1400
1401static void
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001402smb2_print_stats(struct seq_file *m, struct cifs_tcon *tcon)
1403{
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001404 atomic_t *sent = tcon->stats.smb2_stats.smb2_com_sent;
1405 atomic_t *failed = tcon->stats.smb2_stats.smb2_com_failed;
Steve French1995d282018-07-27 15:14:04 -05001406
1407 /*
1408 * Can't display SMB2_NEGOTIATE, SESSION_SETUP, LOGOFF, CANCEL and ECHO
1409 * totals (requests sent) since those SMBs are per-session not per tcon
1410 */
Steve French52ce1ac2018-07-31 01:46:47 -05001411 seq_printf(m, "\nBytes read: %llu Bytes written: %llu",
1412 (long long)(tcon->bytes_read),
1413 (long long)(tcon->bytes_written));
Steve Frenchfae80442018-10-19 17:14:32 -05001414 seq_printf(m, "\nOpen files: %d total (local), %d open on server",
1415 atomic_read(&tcon->num_local_opens),
1416 atomic_read(&tcon->num_remote_opens));
Steve French1995d282018-07-27 15:14:04 -05001417 seq_printf(m, "\nTreeConnects: %d total %d failed",
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001418 atomic_read(&sent[SMB2_TREE_CONNECT_HE]),
1419 atomic_read(&failed[SMB2_TREE_CONNECT_HE]));
Steve French1995d282018-07-27 15:14:04 -05001420 seq_printf(m, "\nTreeDisconnects: %d total %d failed",
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001421 atomic_read(&sent[SMB2_TREE_DISCONNECT_HE]),
1422 atomic_read(&failed[SMB2_TREE_DISCONNECT_HE]));
Steve French1995d282018-07-27 15:14:04 -05001423 seq_printf(m, "\nCreates: %d total %d failed",
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001424 atomic_read(&sent[SMB2_CREATE_HE]),
1425 atomic_read(&failed[SMB2_CREATE_HE]));
Steve French1995d282018-07-27 15:14:04 -05001426 seq_printf(m, "\nCloses: %d total %d failed",
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001427 atomic_read(&sent[SMB2_CLOSE_HE]),
1428 atomic_read(&failed[SMB2_CLOSE_HE]));
Steve French1995d282018-07-27 15:14:04 -05001429 seq_printf(m, "\nFlushes: %d total %d failed",
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001430 atomic_read(&sent[SMB2_FLUSH_HE]),
1431 atomic_read(&failed[SMB2_FLUSH_HE]));
Steve French1995d282018-07-27 15:14:04 -05001432 seq_printf(m, "\nReads: %d total %d failed",
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001433 atomic_read(&sent[SMB2_READ_HE]),
1434 atomic_read(&failed[SMB2_READ_HE]));
Steve French1995d282018-07-27 15:14:04 -05001435 seq_printf(m, "\nWrites: %d total %d failed",
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001436 atomic_read(&sent[SMB2_WRITE_HE]),
1437 atomic_read(&failed[SMB2_WRITE_HE]));
Steve French1995d282018-07-27 15:14:04 -05001438 seq_printf(m, "\nLocks: %d total %d failed",
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001439 atomic_read(&sent[SMB2_LOCK_HE]),
1440 atomic_read(&failed[SMB2_LOCK_HE]));
Steve French1995d282018-07-27 15:14:04 -05001441 seq_printf(m, "\nIOCTLs: %d total %d failed",
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001442 atomic_read(&sent[SMB2_IOCTL_HE]),
1443 atomic_read(&failed[SMB2_IOCTL_HE]));
Steve French1995d282018-07-27 15:14:04 -05001444 seq_printf(m, "\nQueryDirectories: %d total %d failed",
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001445 atomic_read(&sent[SMB2_QUERY_DIRECTORY_HE]),
1446 atomic_read(&failed[SMB2_QUERY_DIRECTORY_HE]));
Steve French1995d282018-07-27 15:14:04 -05001447 seq_printf(m, "\nChangeNotifies: %d total %d failed",
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001448 atomic_read(&sent[SMB2_CHANGE_NOTIFY_HE]),
1449 atomic_read(&failed[SMB2_CHANGE_NOTIFY_HE]));
Steve French1995d282018-07-27 15:14:04 -05001450 seq_printf(m, "\nQueryInfos: %d total %d failed",
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001451 atomic_read(&sent[SMB2_QUERY_INFO_HE]),
1452 atomic_read(&failed[SMB2_QUERY_INFO_HE]));
Steve French1995d282018-07-27 15:14:04 -05001453 seq_printf(m, "\nSetInfos: %d total %d failed",
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001454 atomic_read(&sent[SMB2_SET_INFO_HE]),
1455 atomic_read(&failed[SMB2_SET_INFO_HE]));
1456 seq_printf(m, "\nOplockBreaks: %d sent %d failed",
1457 atomic_read(&sent[SMB2_OPLOCK_BREAK_HE]),
1458 atomic_read(&failed[SMB2_OPLOCK_BREAK_HE]));
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001459}
1460
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001461static void
1462smb2_set_fid(struct cifsFileInfo *cfile, struct cifs_fid *fid, __u32 oplock)
1463{
David Howells2b0143b2015-03-17 22:25:59 +00001464 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04001465 struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server;
1466
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001467 cfile->fid.persistent_fid = fid->persistent_fid;
1468 cfile->fid.volatile_fid = fid->volatile_fid;
Aurelien Aptel86f740f2020-02-21 11:19:06 +01001469 cfile->fid.access = fid->access;
Steve Frenchdfe33f92018-10-30 19:50:31 -05001470#ifdef CONFIG_CIFS_DEBUG2
1471 cfile->fid.mid = fid->mid;
1472#endif /* CIFS_DEBUG2 */
Pavel Shilovsky42873b02013-09-05 21:30:16 +04001473 server->ops->set_oplock_level(cinode, oplock, fid->epoch,
1474 &fid->purge_cache);
Pavel Shilovsky18cceb62013-09-05 13:01:06 +04001475 cinode->can_cache_brlcks = CIFS_CACHE_WRITE(cinode);
Aurelien Aptel94f87372016-09-22 07:38:50 +02001476 memcpy(cfile->fid.create_guid, fid->create_guid, 16);
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001477}
1478
Pavel Shilovsky760ad0c2012-09-25 11:00:07 +04001479static void
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001480smb2_close_file(const unsigned int xid, struct cifs_tcon *tcon,
1481 struct cifs_fid *fid)
1482{
Pavel Shilovsky760ad0c2012-09-25 11:00:07 +04001483 SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001484}
1485
Steve French43f8a6a2019-12-02 21:46:54 -06001486static void
1487smb2_close_getattr(const unsigned int xid, struct cifs_tcon *tcon,
1488 struct cifsFileInfo *cfile)
1489{
1490 struct smb2_file_network_open_info file_inf;
1491 struct inode *inode;
1492 int rc;
1493
1494 rc = __SMB2_close(xid, tcon, cfile->fid.persistent_fid,
1495 cfile->fid.volatile_fid, &file_inf);
1496 if (rc)
1497 return;
1498
1499 inode = d_inode(cfile->dentry);
1500
1501 spin_lock(&inode->i_lock);
1502 CIFS_I(inode)->time = jiffies;
1503
1504 /* Creation time should not need to be updated on close */
1505 if (file_inf.LastWriteTime)
1506 inode->i_mtime = cifs_NTtimeToUnix(file_inf.LastWriteTime);
1507 if (file_inf.ChangeTime)
1508 inode->i_ctime = cifs_NTtimeToUnix(file_inf.ChangeTime);
1509 if (file_inf.LastAccessTime)
1510 inode->i_atime = cifs_NTtimeToUnix(file_inf.LastAccessTime);
1511
1512 /*
1513 * i_blocks is not related to (i_size / i_blksize),
1514 * but instead 512 byte (2**9) size is required for
1515 * calculating num blocks.
1516 */
1517 if (le64_to_cpu(file_inf.AllocationSize) > 4096)
1518 inode->i_blocks =
1519 (512 - 1 + le64_to_cpu(file_inf.AllocationSize)) >> 9;
1520
1521 /* End of file and Attributes should not have to be updated on close */
1522 spin_unlock(&inode->i_lock);
1523}
1524
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07001525static int
Steve French41c13582013-11-14 00:05:36 -06001526SMB2_request_res_key(const unsigned int xid, struct cifs_tcon *tcon,
1527 u64 persistent_fid, u64 volatile_fid,
1528 struct copychunk_ioctl *pcchunk)
1529{
1530 int rc;
1531 unsigned int ret_data_len;
1532 struct resume_key_req *res_key;
1533
1534 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
1535 FSCTL_SRV_REQUEST_RESUME_KEY, true /* is_fsctl */,
Steve French153322f2019-03-28 22:32:49 -05001536 NULL, 0 /* no input */, CIFSMaxBufSize,
Steve French41c13582013-11-14 00:05:36 -06001537 (char **)&res_key, &ret_data_len);
1538
1539 if (rc) {
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +10001540 cifs_tcon_dbg(VFS, "refcpy ioctl error %d getting resume key\n", rc);
Steve French41c13582013-11-14 00:05:36 -06001541 goto req_res_key_exit;
1542 }
1543 if (ret_data_len < sizeof(struct resume_key_req)) {
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +10001544 cifs_tcon_dbg(VFS, "Invalid refcopy resume key length\n");
Steve French41c13582013-11-14 00:05:36 -06001545 rc = -EINVAL;
1546 goto req_res_key_exit;
1547 }
1548 memcpy(pcchunk->SourceKey, res_key->ResumeKey, COPY_CHUNK_RES_KEY_SIZE);
1549
1550req_res_key_exit:
1551 kfree(res_key);
1552 return rc;
1553}
1554
Ronnie Sahlbergb2ca6c22020-05-21 15:03:15 +10001555struct iqi_vars {
1556 struct smb_rqst rqst[3];
1557 struct kvec rsp_iov[3];
1558 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
1559 struct kvec qi_iov[1];
1560 struct kvec io_iov[SMB2_IOCTL_IOV_SIZE];
1561 struct kvec si_iov[SMB2_SET_INFO_IOV_SIZE];
1562 struct kvec close_iov[1];
1563};
1564
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05001565static int
1566smb2_ioctl_query_info(const unsigned int xid,
Ronnie Sahlberg8d8b26e2018-10-17 05:47:58 +10001567 struct cifs_tcon *tcon,
Amir Goldstein0f060932020-02-03 21:46:43 +02001568 struct cifs_sb_info *cifs_sb,
Ronnie Sahlberg8d8b26e2018-10-17 05:47:58 +10001569 __le16 *path, int is_dir,
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05001570 unsigned long p)
1571{
Ronnie Sahlbergb2ca6c22020-05-21 15:03:15 +10001572 struct iqi_vars *vars;
1573 struct smb_rqst *rqst;
1574 struct kvec *rsp_iov;
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05001575 struct cifs_ses *ses = tcon->ses;
Aurelien Aptel352d96f2020-05-31 12:38:22 -05001576 struct TCP_Server_Info *server = cifs_pick_channel(ses);
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05001577 char __user *arg = (char __user *)p;
1578 struct smb_query_info qi;
1579 struct smb_query_info __user *pqi;
1580 int rc = 0;
Paulo Alcantara04ad69c2021-03-08 12:00:50 -03001581 int flags = CIFS_CP_CREATE_CLOSE_OP;
Ronnie Sahlbergf5778c32019-03-15 09:07:22 +10001582 struct smb2_query_info_rsp *qi_rsp = NULL;
1583 struct smb2_ioctl_rsp *io_rsp = NULL;
Ronnie Sahlberg8d8b26e2018-10-17 05:47:58 +10001584 void *buffer = NULL;
Ronnie Sahlberg8d8b26e2018-10-17 05:47:58 +10001585 int resp_buftype[3];
Ronnie Sahlberg8d8b26e2018-10-17 05:47:58 +10001586 struct cifs_open_parms oparms;
1587 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
1588 struct cifs_fid fid;
Ronnie Sahlberg0e906962019-07-25 13:08:43 +10001589 unsigned int size[2];
1590 void *data[2];
Amir Goldstein0f060932020-02-03 21:46:43 +02001591 int create_options = is_dir ? CREATE_NOT_FILE : CREATE_NOT_DIR;
Ronnie Sahlberg8d8b26e2018-10-17 05:47:58 +10001592
Ronnie Sahlbergb2ca6c22020-05-21 15:03:15 +10001593 vars = kzalloc(sizeof(*vars), GFP_ATOMIC);
1594 if (vars == NULL)
1595 return -ENOMEM;
1596 rqst = &vars->rqst[0];
1597 rsp_iov = &vars->rsp_iov[0];
1598
Ronnie Sahlberg8d8b26e2018-10-17 05:47:58 +10001599 resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05001600
1601 if (copy_from_user(&qi, arg, sizeof(struct smb_query_info)))
Ronnie Sahlbergb2ca6c22020-05-21 15:03:15 +10001602 goto e_fault;
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05001603
Ronnie Sahlbergb2ca6c22020-05-21 15:03:15 +10001604 if (qi.output_buffer_length > 1024) {
1605 kfree(vars);
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05001606 return -EINVAL;
Ronnie Sahlbergb2ca6c22020-05-21 15:03:15 +10001607 }
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05001608
Aurelien Aptel352d96f2020-05-31 12:38:22 -05001609 if (!ses || !server) {
Ronnie Sahlbergb2ca6c22020-05-21 15:03:15 +10001610 kfree(vars);
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05001611 return -EIO;
Ronnie Sahlbergb2ca6c22020-05-21 15:03:15 +10001612 }
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05001613
1614 if (smb3_encryption_required(tcon))
1615 flags |= CIFS_TRANSFORM_REQ;
1616
Markus Elfringcfaa1182019-11-05 21:30:25 +01001617 buffer = memdup_user(arg + sizeof(struct smb_query_info),
1618 qi.output_buffer_length);
Ronnie Sahlbergb2ca6c22020-05-21 15:03:15 +10001619 if (IS_ERR(buffer)) {
1620 kfree(vars);
Markus Elfringcfaa1182019-11-05 21:30:25 +01001621 return PTR_ERR(buffer);
Ronnie Sahlbergb2ca6c22020-05-21 15:03:15 +10001622 }
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05001623
Ronnie Sahlberg8d8b26e2018-10-17 05:47:58 +10001624 /* Open */
Ronnie Sahlbergb2ca6c22020-05-21 15:03:15 +10001625 rqst[0].rq_iov = &vars->open_iov[0];
Ronnie Sahlberg8d8b26e2018-10-17 05:47:58 +10001626 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05001627
Ronnie Sahlberg8d8b26e2018-10-17 05:47:58 +10001628 memset(&oparms, 0, sizeof(oparms));
1629 oparms.tcon = tcon;
Ronnie Sahlberg8d8b26e2018-10-17 05:47:58 +10001630 oparms.disposition = FILE_OPEN;
Amir Goldstein0f060932020-02-03 21:46:43 +02001631 oparms.create_options = cifs_create_options(cifs_sb, create_options);
Ronnie Sahlberg8d8b26e2018-10-17 05:47:58 +10001632 oparms.fid = &fid;
1633 oparms.reconnect = false;
1634
Ronnie Sahlbergefac7792019-04-11 12:20:17 +10001635 if (qi.flags & PASSTHRU_FSCTL) {
1636 switch (qi.info_type & FSCTL_DEVICE_ACCESS_MASK) {
1637 case FSCTL_DEVICE_ACCESS_FILE_READ_WRITE_ACCESS:
1638 oparms.desired_access = FILE_READ_DATA | FILE_WRITE_DATA | FILE_READ_ATTRIBUTES | SYNCHRONIZE;
Steve French46e66612019-04-11 13:53:17 -05001639 break;
1640 case FSCTL_DEVICE_ACCESS_FILE_ANY_ACCESS:
1641 oparms.desired_access = GENERIC_ALL;
1642 break;
1643 case FSCTL_DEVICE_ACCESS_FILE_READ_ACCESS:
1644 oparms.desired_access = GENERIC_READ;
1645 break;
1646 case FSCTL_DEVICE_ACCESS_FILE_WRITE_ACCESS:
1647 oparms.desired_access = GENERIC_WRITE;
Ronnie Sahlbergefac7792019-04-11 12:20:17 +10001648 break;
1649 }
Ronnie Sahlberg0e906962019-07-25 13:08:43 +10001650 } else if (qi.flags & PASSTHRU_SET_INFO) {
1651 oparms.desired_access = GENERIC_WRITE;
1652 } else {
1653 oparms.desired_access = FILE_READ_ATTRIBUTES | READ_CONTROL;
Ronnie Sahlbergefac7792019-04-11 12:20:17 +10001654 }
1655
Aurelien Aptel352d96f2020-05-31 12:38:22 -05001656 rc = SMB2_open_init(tcon, server,
1657 &rqst[0], &oplock, &oparms, path);
Ronnie Sahlberg8d8b26e2018-10-17 05:47:58 +10001658 if (rc)
1659 goto iqinf_exit;
Ronnie Sahlberge77fe732018-12-31 13:43:40 +10001660 smb2_set_next_command(tcon, &rqst[0]);
Ronnie Sahlberg8d8b26e2018-10-17 05:47:58 +10001661
1662 /* Query */
Steve French31ba4332019-03-13 02:40:07 -05001663 if (qi.flags & PASSTHRU_FSCTL) {
1664 /* Can eventually relax perm check since server enforces too */
1665 if (!capable(CAP_SYS_ADMIN))
1666 rc = -EPERM;
Ronnie Sahlbergf5778c32019-03-15 09:07:22 +10001667 else {
Ronnie Sahlbergb2ca6c22020-05-21 15:03:15 +10001668 rqst[1].rq_iov = &vars->io_iov[0];
Ronnie Sahlbergf5778c32019-03-15 09:07:22 +10001669 rqst[1].rq_nvec = SMB2_IOCTL_IOV_SIZE;
1670
Aurelien Aptel352d96f2020-05-31 12:38:22 -05001671 rc = SMB2_ioctl_init(tcon, server,
1672 &rqst[1],
Ronnie Sahlbergf5778c32019-03-15 09:07:22 +10001673 COMPOUND_FID, COMPOUND_FID,
Ronnie Sahlbergefac7792019-04-11 12:20:17 +10001674 qi.info_type, true, buffer,
1675 qi.output_buffer_length,
Ronnie Sahlberg731b82b2020-01-08 13:08:07 +10001676 CIFSMaxBufSize -
1677 MAX_SMB2_CREATE_RESPONSE_SIZE -
1678 MAX_SMB2_CLOSE_RESPONSE_SIZE);
Ronnie Sahlbergf5778c32019-03-15 09:07:22 +10001679 }
Ronnie Sahlberg0e906962019-07-25 13:08:43 +10001680 } else if (qi.flags == PASSTHRU_SET_INFO) {
1681 /* Can eventually relax perm check since server enforces too */
1682 if (!capable(CAP_SYS_ADMIN))
1683 rc = -EPERM;
1684 else {
Ronnie Sahlbergb2ca6c22020-05-21 15:03:15 +10001685 rqst[1].rq_iov = &vars->si_iov[0];
Ronnie Sahlberg0e906962019-07-25 13:08:43 +10001686 rqst[1].rq_nvec = 1;
1687
1688 size[0] = 8;
1689 data[0] = buffer;
1690
Aurelien Aptel352d96f2020-05-31 12:38:22 -05001691 rc = SMB2_set_info_init(tcon, server,
1692 &rqst[1],
Ronnie Sahlberg0e906962019-07-25 13:08:43 +10001693 COMPOUND_FID, COMPOUND_FID,
1694 current->tgid,
1695 FILE_END_OF_FILE_INFORMATION,
1696 SMB2_O_INFO_FILE, 0, data, size);
1697 }
Steve French31ba4332019-03-13 02:40:07 -05001698 } else if (qi.flags == PASSTHRU_QUERY_INFO) {
Ronnie Sahlbergb2ca6c22020-05-21 15:03:15 +10001699 rqst[1].rq_iov = &vars->qi_iov[0];
Steve French31ba4332019-03-13 02:40:07 -05001700 rqst[1].rq_nvec = 1;
Ronnie Sahlberg8d8b26e2018-10-17 05:47:58 +10001701
Aurelien Aptel352d96f2020-05-31 12:38:22 -05001702 rc = SMB2_query_info_init(tcon, server,
1703 &rqst[1], COMPOUND_FID,
Steve French31ba4332019-03-13 02:40:07 -05001704 COMPOUND_FID, qi.file_info_class,
1705 qi.info_type, qi.additional_information,
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05001706 qi.input_buffer_length,
1707 qi.output_buffer_length, buffer);
Steve French31ba4332019-03-13 02:40:07 -05001708 } else { /* unknown flags */
Joe Perchesa0a30362020-04-14 22:42:53 -07001709 cifs_tcon_dbg(VFS, "Invalid passthru query flags: 0x%x\n",
1710 qi.flags);
Steve French31ba4332019-03-13 02:40:07 -05001711 rc = -EINVAL;
1712 }
1713
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05001714 if (rc)
1715 goto iqinf_exit;
Ronnie Sahlberge77fe732018-12-31 13:43:40 +10001716 smb2_set_next_command(tcon, &rqst[1]);
Ronnie Sahlberg8d8b26e2018-10-17 05:47:58 +10001717 smb2_set_related(&rqst[1]);
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05001718
Ronnie Sahlberg8d8b26e2018-10-17 05:47:58 +10001719 /* Close */
Ronnie Sahlbergb2ca6c22020-05-21 15:03:15 +10001720 rqst[2].rq_iov = &vars->close_iov[0];
Ronnie Sahlberg8d8b26e2018-10-17 05:47:58 +10001721 rqst[2].rq_nvec = 1;
1722
Aurelien Aptel352d96f2020-05-31 12:38:22 -05001723 rc = SMB2_close_init(tcon, server,
1724 &rqst[2], COMPOUND_FID, COMPOUND_FID, false);
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05001725 if (rc)
1726 goto iqinf_exit;
Ronnie Sahlberg8d8b26e2018-10-17 05:47:58 +10001727 smb2_set_related(&rqst[2]);
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05001728
Aurelien Aptel352d96f2020-05-31 12:38:22 -05001729 rc = compound_send_recv(xid, ses, server,
1730 flags, 3, rqst,
Ronnie Sahlberg8d8b26e2018-10-17 05:47:58 +10001731 resp_buftype, rsp_iov);
1732 if (rc)
1733 goto iqinf_exit;
Steve Frenchd2f15422019-09-22 00:55:46 -05001734
1735 /* No need to bump num_remote_opens since handle immediately closed */
Ronnie Sahlbergf5778c32019-03-15 09:07:22 +10001736 if (qi.flags & PASSTHRU_FSCTL) {
1737 pqi = (struct smb_query_info __user *)arg;
1738 io_rsp = (struct smb2_ioctl_rsp *)rsp_iov[1].iov_base;
1739 if (le32_to_cpu(io_rsp->OutputCount) < qi.input_buffer_length)
1740 qi.input_buffer_length = le32_to_cpu(io_rsp->OutputCount);
Ronnie Sahlberg5242fcb2019-04-15 12:13:52 +10001741 if (qi.input_buffer_length > 0 &&
Markus Elfring2b1116b2019-11-05 22:26:53 +01001742 le32_to_cpu(io_rsp->OutputOffset) + qi.input_buffer_length
1743 > rsp_iov[1].iov_len)
1744 goto e_fault;
1745
1746 if (copy_to_user(&pqi->input_buffer_length,
1747 &qi.input_buffer_length,
1748 sizeof(qi.input_buffer_length)))
1749 goto e_fault;
1750
Ronnie Sahlberg5242fcb2019-04-15 12:13:52 +10001751 if (copy_to_user((void __user *)pqi + sizeof(struct smb_query_info),
1752 (const void *)io_rsp + le32_to_cpu(io_rsp->OutputOffset),
Markus Elfring2b1116b2019-11-05 22:26:53 +01001753 qi.input_buffer_length))
1754 goto e_fault;
Ronnie Sahlbergf5778c32019-03-15 09:07:22 +10001755 } else {
1756 pqi = (struct smb_query_info __user *)arg;
1757 qi_rsp = (struct smb2_query_info_rsp *)rsp_iov[1].iov_base;
1758 if (le32_to_cpu(qi_rsp->OutputBufferLength) < qi.input_buffer_length)
1759 qi.input_buffer_length = le32_to_cpu(qi_rsp->OutputBufferLength);
Markus Elfring2b1116b2019-11-05 22:26:53 +01001760 if (copy_to_user(&pqi->input_buffer_length,
1761 &qi.input_buffer_length,
1762 sizeof(qi.input_buffer_length)))
1763 goto e_fault;
1764
1765 if (copy_to_user(pqi + 1, qi_rsp->Buffer,
1766 qi.input_buffer_length))
1767 goto e_fault;
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05001768 }
1769
1770 iqinf_exit:
Ronnie Sahlbergb2ca6c22020-05-21 15:03:15 +10001771 kfree(vars);
Ronnie Sahlberg8d8b26e2018-10-17 05:47:58 +10001772 kfree(buffer);
1773 SMB2_open_free(&rqst[0]);
Ronnie Sahlbergf5778c32019-03-15 09:07:22 +10001774 if (qi.flags & PASSTHRU_FSCTL)
1775 SMB2_ioctl_free(&rqst[1]);
1776 else
1777 SMB2_query_info_free(&rqst[1]);
1778
Ronnie Sahlberg8d8b26e2018-10-17 05:47:58 +10001779 SMB2_close_free(&rqst[2]);
1780 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
1781 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
1782 free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05001783 return rc;
Markus Elfring2b1116b2019-11-05 22:26:53 +01001784
1785e_fault:
1786 rc = -EFAULT;
1787 goto iqinf_exit;
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05001788}
1789
Sachin Prabhu620d8742017-02-10 16:03:51 +05301790static ssize_t
Sachin Prabhu312bbc52017-04-04 02:12:04 -05001791smb2_copychunk_range(const unsigned int xid,
Steve French41c13582013-11-14 00:05:36 -06001792 struct cifsFileInfo *srcfile,
1793 struct cifsFileInfo *trgtfile, u64 src_off,
1794 u64 len, u64 dest_off)
1795{
1796 int rc;
1797 unsigned int ret_data_len;
1798 struct copychunk_ioctl *pcchunk;
Steve French9bf0c9c2013-11-16 18:05:28 -06001799 struct copychunk_ioctl_rsp *retbuf = NULL;
1800 struct cifs_tcon *tcon;
1801 int chunks_copied = 0;
1802 bool chunk_sizes_updated = false;
Sachin Prabhu620d8742017-02-10 16:03:51 +05301803 ssize_t bytes_written, total_bytes_written = 0;
Steve French41c13582013-11-14 00:05:36 -06001804
1805 pcchunk = kmalloc(sizeof(struct copychunk_ioctl), GFP_KERNEL);
1806
1807 if (pcchunk == NULL)
1808 return -ENOMEM;
1809
Christoph Probsta205d502019-05-08 21:36:25 +02001810 cifs_dbg(FYI, "%s: about to call request res key\n", __func__);
Steve French41c13582013-11-14 00:05:36 -06001811 /* Request a key from the server to identify the source of the copy */
1812 rc = SMB2_request_res_key(xid, tlink_tcon(srcfile->tlink),
1813 srcfile->fid.persistent_fid,
1814 srcfile->fid.volatile_fid, pcchunk);
1815
1816 /* Note: request_res_key sets res_key null only if rc !=0 */
1817 if (rc)
Steve French9bf0c9c2013-11-16 18:05:28 -06001818 goto cchunk_out;
Steve French41c13582013-11-14 00:05:36 -06001819
1820 /* For now array only one chunk long, will make more flexible later */
Fabian Frederickbc09d142014-12-10 15:41:15 -08001821 pcchunk->ChunkCount = cpu_to_le32(1);
Steve French41c13582013-11-14 00:05:36 -06001822 pcchunk->Reserved = 0;
Steve French41c13582013-11-14 00:05:36 -06001823 pcchunk->Reserved2 = 0;
1824
Steve French9bf0c9c2013-11-16 18:05:28 -06001825 tcon = tlink_tcon(trgtfile->tlink);
1826
1827 while (len > 0) {
1828 pcchunk->SourceOffset = cpu_to_le64(src_off);
1829 pcchunk->TargetOffset = cpu_to_le64(dest_off);
1830 pcchunk->Length =
1831 cpu_to_le32(min_t(u32, len, tcon->max_bytes_chunk));
1832
1833 /* Request server copy to target from src identified by key */
1834 rc = SMB2_ioctl(xid, tcon, trgtfile->fid.persistent_fid,
Steve French41c13582013-11-14 00:05:36 -06001835 trgtfile->fid.volatile_fid, FSCTL_SRV_COPYCHUNK_WRITE,
Aurelien Aptel63a83b82018-01-24 13:46:11 +01001836 true /* is_fsctl */, (char *)pcchunk,
Steve French153322f2019-03-28 22:32:49 -05001837 sizeof(struct copychunk_ioctl), CIFSMaxBufSize,
1838 (char **)&retbuf, &ret_data_len);
Steve French9bf0c9c2013-11-16 18:05:28 -06001839 if (rc == 0) {
1840 if (ret_data_len !=
1841 sizeof(struct copychunk_ioctl_rsp)) {
Joe Perchesa0a30362020-04-14 22:42:53 -07001842 cifs_tcon_dbg(VFS, "Invalid cchunk response size\n");
Steve French9bf0c9c2013-11-16 18:05:28 -06001843 rc = -EIO;
1844 goto cchunk_out;
1845 }
1846 if (retbuf->TotalBytesWritten == 0) {
1847 cifs_dbg(FYI, "no bytes copied\n");
1848 rc = -EIO;
1849 goto cchunk_out;
1850 }
1851 /*
1852 * Check if server claimed to write more than we asked
1853 */
1854 if (le32_to_cpu(retbuf->TotalBytesWritten) >
1855 le32_to_cpu(pcchunk->Length)) {
Joe Perchesa0a30362020-04-14 22:42:53 -07001856 cifs_tcon_dbg(VFS, "Invalid copy chunk response\n");
Steve French9bf0c9c2013-11-16 18:05:28 -06001857 rc = -EIO;
1858 goto cchunk_out;
1859 }
1860 if (le32_to_cpu(retbuf->ChunksWritten) != 1) {
Joe Perchesa0a30362020-04-14 22:42:53 -07001861 cifs_tcon_dbg(VFS, "Invalid num chunks written\n");
Steve French9bf0c9c2013-11-16 18:05:28 -06001862 rc = -EIO;
1863 goto cchunk_out;
1864 }
1865 chunks_copied++;
Steve French41c13582013-11-14 00:05:36 -06001866
Sachin Prabhu620d8742017-02-10 16:03:51 +05301867 bytes_written = le32_to_cpu(retbuf->TotalBytesWritten);
1868 src_off += bytes_written;
1869 dest_off += bytes_written;
1870 len -= bytes_written;
1871 total_bytes_written += bytes_written;
Steve French41c13582013-11-14 00:05:36 -06001872
Sachin Prabhu620d8742017-02-10 16:03:51 +05301873 cifs_dbg(FYI, "Chunks %d PartialChunk %d Total %zu\n",
Steve French9bf0c9c2013-11-16 18:05:28 -06001874 le32_to_cpu(retbuf->ChunksWritten),
1875 le32_to_cpu(retbuf->ChunkBytesWritten),
Sachin Prabhu620d8742017-02-10 16:03:51 +05301876 bytes_written);
Steve French9bf0c9c2013-11-16 18:05:28 -06001877 } else if (rc == -EINVAL) {
1878 if (ret_data_len != sizeof(struct copychunk_ioctl_rsp))
1879 goto cchunk_out;
Steve French41c13582013-11-14 00:05:36 -06001880
Steve French9bf0c9c2013-11-16 18:05:28 -06001881 cifs_dbg(FYI, "MaxChunks %d BytesChunk %d MaxCopy %d\n",
1882 le32_to_cpu(retbuf->ChunksWritten),
1883 le32_to_cpu(retbuf->ChunkBytesWritten),
1884 le32_to_cpu(retbuf->TotalBytesWritten));
1885
1886 /*
1887 * Check if this is the first request using these sizes,
1888 * (ie check if copy succeed once with original sizes
1889 * and check if the server gave us different sizes after
1890 * we already updated max sizes on previous request).
1891 * if not then why is the server returning an error now
1892 */
1893 if ((chunks_copied != 0) || chunk_sizes_updated)
1894 goto cchunk_out;
1895
1896 /* Check that server is not asking us to grow size */
1897 if (le32_to_cpu(retbuf->ChunkBytesWritten) <
1898 tcon->max_bytes_chunk)
1899 tcon->max_bytes_chunk =
1900 le32_to_cpu(retbuf->ChunkBytesWritten);
1901 else
1902 goto cchunk_out; /* server gave us bogus size */
1903
1904 /* No need to change MaxChunks since already set to 1 */
1905 chunk_sizes_updated = true;
Sachin Prabhu2477bc52015-02-04 13:10:26 +00001906 } else
1907 goto cchunk_out;
Steve French9bf0c9c2013-11-16 18:05:28 -06001908 }
1909
1910cchunk_out:
Steve French41c13582013-11-14 00:05:36 -06001911 kfree(pcchunk);
Steve French24df1482016-09-29 04:20:23 -05001912 kfree(retbuf);
Sachin Prabhu620d8742017-02-10 16:03:51 +05301913 if (rc)
1914 return rc;
1915 else
1916 return total_bytes_written;
Steve French41c13582013-11-14 00:05:36 -06001917}
1918
1919static int
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07001920smb2_flush_file(const unsigned int xid, struct cifs_tcon *tcon,
1921 struct cifs_fid *fid)
1922{
1923 return SMB2_flush(xid, tcon, fid->persistent_fid, fid->volatile_fid);
1924}
1925
Pavel Shilovsky09a47072012-09-18 16:20:29 -07001926static unsigned int
1927smb2_read_data_offset(char *buf)
1928{
1929 struct smb2_read_rsp *rsp = (struct smb2_read_rsp *)buf;
Christoph Probsta205d502019-05-08 21:36:25 +02001930
Pavel Shilovsky09a47072012-09-18 16:20:29 -07001931 return rsp->DataOffset;
1932}
1933
1934static unsigned int
Long Li74dcf412017-11-22 17:38:46 -07001935smb2_read_data_length(char *buf, bool in_remaining)
Pavel Shilovsky09a47072012-09-18 16:20:29 -07001936{
1937 struct smb2_read_rsp *rsp = (struct smb2_read_rsp *)buf;
Long Li74dcf412017-11-22 17:38:46 -07001938
1939 if (in_remaining)
1940 return le32_to_cpu(rsp->DataRemaining);
1941
Pavel Shilovsky09a47072012-09-18 16:20:29 -07001942 return le32_to_cpu(rsp->DataLength);
1943}
1944
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07001945
1946static int
Steve Frenchdb8b6312014-09-22 05:13:55 -05001947smb2_sync_read(const unsigned int xid, struct cifs_fid *pfid,
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07001948 struct cifs_io_parms *parms, unsigned int *bytes_read,
1949 char **buf, int *buf_type)
1950{
Steve Frenchdb8b6312014-09-22 05:13:55 -05001951 parms->persistent_fid = pfid->persistent_fid;
1952 parms->volatile_fid = pfid->volatile_fid;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07001953 return SMB2_read(xid, parms, bytes_read, buf, buf_type);
1954}
1955
Pavel Shilovsky009d3442012-09-18 16:20:30 -07001956static int
Steve Frenchdb8b6312014-09-22 05:13:55 -05001957smb2_sync_write(const unsigned int xid, struct cifs_fid *pfid,
Pavel Shilovsky009d3442012-09-18 16:20:30 -07001958 struct cifs_io_parms *parms, unsigned int *written,
1959 struct kvec *iov, unsigned long nr_segs)
1960{
1961
Steve Frenchdb8b6312014-09-22 05:13:55 -05001962 parms->persistent_fid = pfid->persistent_fid;
1963 parms->volatile_fid = pfid->volatile_fid;
Pavel Shilovsky009d3442012-09-18 16:20:30 -07001964 return SMB2_write(xid, parms, written, iov, nr_segs);
1965}
1966
Steve Frenchd43cc792014-08-13 17:16:29 -05001967/* Set or clear the SPARSE_FILE attribute based on value passed in setsparse */
1968static bool smb2_set_sparse(const unsigned int xid, struct cifs_tcon *tcon,
1969 struct cifsFileInfo *cfile, struct inode *inode, __u8 setsparse)
1970{
1971 struct cifsInodeInfo *cifsi;
1972 int rc;
1973
1974 cifsi = CIFS_I(inode);
1975
1976 /* if file already sparse don't bother setting sparse again */
1977 if ((cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) && setsparse)
1978 return true; /* already sparse */
1979
1980 if (!(cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) && !setsparse)
1981 return true; /* already not sparse */
1982
1983 /*
1984 * Can't check for sparse support on share the usual way via the
1985 * FS attribute info (FILE_SUPPORTS_SPARSE_FILES) on the share
1986 * since Samba server doesn't set the flag on the share, yet
1987 * supports the set sparse FSCTL and returns sparse correctly
1988 * in the file attributes. If we fail setting sparse though we
1989 * mark that server does not support sparse files for this share
1990 * to avoid repeatedly sending the unsupported fsctl to server
1991 * if the file is repeatedly extended.
1992 */
1993 if (tcon->broken_sparse_sup)
1994 return false;
1995
1996 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
1997 cfile->fid.volatile_fid, FSCTL_SET_SPARSE,
Aurelien Aptel63a83b82018-01-24 13:46:11 +01001998 true /* is_fctl */,
Steve French153322f2019-03-28 22:32:49 -05001999 &setsparse, 1, CIFSMaxBufSize, NULL, NULL);
Steve Frenchd43cc792014-08-13 17:16:29 -05002000 if (rc) {
2001 tcon->broken_sparse_sup = true;
2002 cifs_dbg(FYI, "set sparse rc = %d\n", rc);
2003 return false;
2004 }
2005
2006 if (setsparse)
2007 cifsi->cifsAttrs |= FILE_ATTRIBUTE_SPARSE_FILE;
2008 else
2009 cifsi->cifsAttrs &= (~FILE_ATTRIBUTE_SPARSE_FILE);
2010
2011 return true;
2012}
2013
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07002014static int
2015smb2_set_file_size(const unsigned int xid, struct cifs_tcon *tcon,
2016 struct cifsFileInfo *cfile, __u64 size, bool set_alloc)
2017{
2018 __le64 eof = cpu_to_le64(size);
Steve French3d1a3742014-08-11 21:05:25 -05002019 struct inode *inode;
2020
2021 /*
2022 * If extending file more than one page make sparse. Many Linux fs
2023 * make files sparse by default when extending via ftruncate
2024 */
David Howells2b0143b2015-03-17 22:25:59 +00002025 inode = d_inode(cfile->dentry);
Steve French3d1a3742014-08-11 21:05:25 -05002026
2027 if (!set_alloc && (size > inode->i_size + 8192)) {
Steve French3d1a3742014-08-11 21:05:25 -05002028 __u8 set_sparse = 1;
Steve French3d1a3742014-08-11 21:05:25 -05002029
Steve Frenchd43cc792014-08-13 17:16:29 -05002030 /* whether set sparse succeeds or not, extend the file */
2031 smb2_set_sparse(xid, tcon, cfile, inode, set_sparse);
Steve French3d1a3742014-08-11 21:05:25 -05002032 }
2033
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07002034 return SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
Ronnie Sahlberg3764cbd2018-09-03 13:33:47 +10002035 cfile->fid.volatile_fid, cfile->pid, &eof);
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07002036}
2037
Steve French02b16662015-06-27 21:18:36 -07002038static int
2039smb2_duplicate_extents(const unsigned int xid,
2040 struct cifsFileInfo *srcfile,
2041 struct cifsFileInfo *trgtfile, u64 src_off,
2042 u64 len, u64 dest_off)
2043{
2044 int rc;
2045 unsigned int ret_data_len;
Steve Frenchcfc63fc2021-03-26 18:41:55 -05002046 struct inode *inode;
Steve French02b16662015-06-27 21:18:36 -07002047 struct duplicate_extents_to_file dup_ext_buf;
2048 struct cifs_tcon *tcon = tlink_tcon(trgtfile->tlink);
2049
2050 /* server fileays advertise duplicate extent support with this flag */
2051 if ((le32_to_cpu(tcon->fsAttrInfo.Attributes) &
2052 FILE_SUPPORTS_BLOCK_REFCOUNTING) == 0)
2053 return -EOPNOTSUPP;
2054
2055 dup_ext_buf.VolatileFileHandle = srcfile->fid.volatile_fid;
2056 dup_ext_buf.PersistentFileHandle = srcfile->fid.persistent_fid;
2057 dup_ext_buf.SourceFileOffset = cpu_to_le64(src_off);
2058 dup_ext_buf.TargetFileOffset = cpu_to_le64(dest_off);
2059 dup_ext_buf.ByteCount = cpu_to_le64(len);
Christoph Probsta205d502019-05-08 21:36:25 +02002060 cifs_dbg(FYI, "Duplicate extents: src off %lld dst off %lld len %lld\n",
Steve French02b16662015-06-27 21:18:36 -07002061 src_off, dest_off, len);
2062
Steve Frenchcfc63fc2021-03-26 18:41:55 -05002063 inode = d_inode(trgtfile->dentry);
2064 if (inode->i_size < dest_off + len) {
2065 rc = smb2_set_file_size(xid, tcon, trgtfile, dest_off + len, false);
2066 if (rc)
2067 goto duplicate_extents_out;
Steve French02b16662015-06-27 21:18:36 -07002068
Steve Frenchcfc63fc2021-03-26 18:41:55 -05002069 /*
2070 * Although also could set plausible allocation size (i_blocks)
2071 * here in addition to setting the file size, in reflink
2072 * it is likely that the target file is sparse. Its allocation
2073 * size will be queried on next revalidate, but it is important
2074 * to make sure that file's cached size is updated immediately
2075 */
2076 cifs_setsize(inode, dest_off + len);
2077 }
Steve French02b16662015-06-27 21:18:36 -07002078 rc = SMB2_ioctl(xid, tcon, trgtfile->fid.persistent_fid,
2079 trgtfile->fid.volatile_fid,
2080 FSCTL_DUPLICATE_EXTENTS_TO_FILE,
Aurelien Aptel63a83b82018-01-24 13:46:11 +01002081 true /* is_fsctl */,
Aurelien Aptel51146622017-02-28 15:08:41 +01002082 (char *)&dup_ext_buf,
Steve French02b16662015-06-27 21:18:36 -07002083 sizeof(struct duplicate_extents_to_file),
Steve French153322f2019-03-28 22:32:49 -05002084 CIFSMaxBufSize, NULL,
Steve French02b16662015-06-27 21:18:36 -07002085 &ret_data_len);
2086
2087 if (ret_data_len > 0)
Christoph Probsta205d502019-05-08 21:36:25 +02002088 cifs_dbg(FYI, "Non-zero response length in duplicate extents\n");
Steve French02b16662015-06-27 21:18:36 -07002089
2090duplicate_extents_out:
2091 return rc;
2092}
Steve French02b16662015-06-27 21:18:36 -07002093
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002094static int
Steve French64a5cfa2013-10-14 15:31:32 -05002095smb2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
2096 struct cifsFileInfo *cfile)
2097{
2098 return SMB2_set_compression(xid, tcon, cfile->fid.persistent_fid,
2099 cfile->fid.volatile_fid);
2100}
2101
2102static int
Steve Frenchb3152e22015-06-24 03:17:02 -05002103smb3_set_integrity(const unsigned int xid, struct cifs_tcon *tcon,
2104 struct cifsFileInfo *cfile)
2105{
2106 struct fsctl_set_integrity_information_req integr_info;
Steve Frenchb3152e22015-06-24 03:17:02 -05002107 unsigned int ret_data_len;
2108
2109 integr_info.ChecksumAlgorithm = cpu_to_le16(CHECKSUM_TYPE_UNCHANGED);
2110 integr_info.Flags = 0;
2111 integr_info.Reserved = 0;
2112
2113 return SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
2114 cfile->fid.volatile_fid,
2115 FSCTL_SET_INTEGRITY_INFORMATION,
Aurelien Aptel63a83b82018-01-24 13:46:11 +01002116 true /* is_fsctl */,
Aurelien Aptel51146622017-02-28 15:08:41 +01002117 (char *)&integr_info,
Steve Frenchb3152e22015-06-24 03:17:02 -05002118 sizeof(struct fsctl_set_integrity_information_req),
Steve French153322f2019-03-28 22:32:49 -05002119 CIFSMaxBufSize, NULL,
Steve Frenchb3152e22015-06-24 03:17:02 -05002120 &ret_data_len);
2121
2122}
2123
Steve Frenche02789a2018-08-09 14:33:12 -05002124/* GMT Token is @GMT-YYYY.MM.DD-HH.MM.SS Unicode which is 48 bytes + null */
2125#define GMT_TOKEN_SIZE 50
2126
Steve French153322f2019-03-28 22:32:49 -05002127#define MIN_SNAPSHOT_ARRAY_SIZE 16 /* See MS-SMB2 section 3.3.5.15.1 */
2128
Steve Frenche02789a2018-08-09 14:33:12 -05002129/*
2130 * Input buffer contains (empty) struct smb_snapshot array with size filled in
2131 * For output see struct SRV_SNAPSHOT_ARRAY in MS-SMB2 section 2.2.32.2
2132 */
Steve Frenchb3152e22015-06-24 03:17:02 -05002133static int
Steve French834170c2016-09-30 21:14:26 -05002134smb3_enum_snapshots(const unsigned int xid, struct cifs_tcon *tcon,
2135 struct cifsFileInfo *cfile, void __user *ioc_buf)
2136{
2137 char *retbuf = NULL;
2138 unsigned int ret_data_len = 0;
2139 int rc;
Steve French153322f2019-03-28 22:32:49 -05002140 u32 max_response_size;
Steve French834170c2016-09-30 21:14:26 -05002141 struct smb_snapshot_array snapshot_in;
2142
Steve French973189a2019-04-04 00:41:04 -05002143 /*
2144 * On the first query to enumerate the list of snapshots available
2145 * for this volume the buffer begins with 0 (number of snapshots
2146 * which can be returned is zero since at that point we do not know
2147 * how big the buffer needs to be). On the second query,
2148 * it (ret_data_len) is set to number of snapshots so we can
2149 * know to set the maximum response size larger (see below).
2150 */
Steve French153322f2019-03-28 22:32:49 -05002151 if (get_user(ret_data_len, (unsigned int __user *)ioc_buf))
2152 return -EFAULT;
2153
2154 /*
2155 * Note that for snapshot queries that servers like Azure expect that
2156 * the first query be minimal size (and just used to get the number/size
2157 * of previous versions) so response size must be specified as EXACTLY
2158 * sizeof(struct snapshot_array) which is 16 when rounded up to multiple
2159 * of eight bytes.
2160 */
2161 if (ret_data_len == 0)
2162 max_response_size = MIN_SNAPSHOT_ARRAY_SIZE;
2163 else
2164 max_response_size = CIFSMaxBufSize;
2165
Steve French834170c2016-09-30 21:14:26 -05002166 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
2167 cfile->fid.volatile_fid,
2168 FSCTL_SRV_ENUMERATE_SNAPSHOTS,
Aurelien Aptel63a83b82018-01-24 13:46:11 +01002169 true /* is_fsctl */,
Steve French153322f2019-03-28 22:32:49 -05002170 NULL, 0 /* no input data */, max_response_size,
Steve French834170c2016-09-30 21:14:26 -05002171 (char **)&retbuf,
2172 &ret_data_len);
2173 cifs_dbg(FYI, "enum snaphots ioctl returned %d and ret buflen is %d\n",
2174 rc, ret_data_len);
2175 if (rc)
2176 return rc;
2177
2178 if (ret_data_len && (ioc_buf != NULL) && (retbuf != NULL)) {
2179 /* Fixup buffer */
2180 if (copy_from_user(&snapshot_in, ioc_buf,
2181 sizeof(struct smb_snapshot_array))) {
2182 rc = -EFAULT;
2183 kfree(retbuf);
2184 return rc;
2185 }
Steve French834170c2016-09-30 21:14:26 -05002186
Steve Frenche02789a2018-08-09 14:33:12 -05002187 /*
2188 * Check for min size, ie not large enough to fit even one GMT
2189 * token (snapshot). On the first ioctl some users may pass in
2190 * smaller size (or zero) to simply get the size of the array
2191 * so the user space caller can allocate sufficient memory
2192 * and retry the ioctl again with larger array size sufficient
2193 * to hold all of the snapshot GMT tokens on the second try.
2194 */
2195 if (snapshot_in.snapshot_array_size < GMT_TOKEN_SIZE)
2196 ret_data_len = sizeof(struct smb_snapshot_array);
2197
2198 /*
2199 * We return struct SRV_SNAPSHOT_ARRAY, followed by
2200 * the snapshot array (of 50 byte GMT tokens) each
2201 * representing an available previous version of the data
2202 */
2203 if (ret_data_len > (snapshot_in.snapshot_array_size +
2204 sizeof(struct smb_snapshot_array)))
2205 ret_data_len = snapshot_in.snapshot_array_size +
2206 sizeof(struct smb_snapshot_array);
Steve French834170c2016-09-30 21:14:26 -05002207
2208 if (copy_to_user(ioc_buf, retbuf, ret_data_len))
2209 rc = -EFAULT;
2210 }
2211
2212 kfree(retbuf);
2213 return rc;
2214}
2215
Steve Frenchd26c2dd2020-02-06 06:00:14 -06002216
2217
2218static int
2219smb3_notify(const unsigned int xid, struct file *pfile,
2220 void __user *ioc_buf)
2221{
2222 struct smb3_notify notify;
2223 struct dentry *dentry = pfile->f_path.dentry;
2224 struct inode *inode = file_inode(pfile);
Al Virof6a9bc32021-03-05 17:36:04 -05002225 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
Steve Frenchd26c2dd2020-02-06 06:00:14 -06002226 struct cifs_open_parms oparms;
2227 struct cifs_fid fid;
2228 struct cifs_tcon *tcon;
Al Virof6a9bc32021-03-05 17:36:04 -05002229 const unsigned char *path;
2230 void *page = alloc_dentry_path();
Steve Frenchd26c2dd2020-02-06 06:00:14 -06002231 __le16 *utf16_path = NULL;
2232 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2233 int rc = 0;
2234
Al Virof6a9bc32021-03-05 17:36:04 -05002235 path = build_path_from_dentry(dentry, page);
2236 if (IS_ERR(path)) {
2237 rc = PTR_ERR(path);
2238 goto notify_exit;
2239 }
Steve Frenchd26c2dd2020-02-06 06:00:14 -06002240
2241 utf16_path = cifs_convert_path_to_utf16(path + 1, cifs_sb);
2242 if (utf16_path == NULL) {
2243 rc = -ENOMEM;
2244 goto notify_exit;
2245 }
2246
2247 if (copy_from_user(&notify, ioc_buf, sizeof(struct smb3_notify))) {
2248 rc = -EFAULT;
2249 goto notify_exit;
2250 }
2251
2252 tcon = cifs_sb_master_tcon(cifs_sb);
2253 oparms.tcon = tcon;
Steve French4ef9b4f2020-07-07 18:08:46 -05002254 oparms.desired_access = FILE_READ_ATTRIBUTES | FILE_READ_DATA;
Steve Frenchd26c2dd2020-02-06 06:00:14 -06002255 oparms.disposition = FILE_OPEN;
2256 oparms.create_options = cifs_create_options(cifs_sb, 0);
2257 oparms.fid = &fid;
2258 oparms.reconnect = false;
2259
Aurelien Aptel69dda302020-03-02 17:53:22 +01002260 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, NULL,
2261 NULL);
Steve Frenchd26c2dd2020-02-06 06:00:14 -06002262 if (rc)
2263 goto notify_exit;
2264
2265 rc = SMB2_change_notify(xid, tcon, fid.persistent_fid, fid.volatile_fid,
2266 notify.watch_tree, notify.completion_filter);
2267
2268 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
2269
2270 cifs_dbg(FYI, "change notify for path %s rc %d\n", path, rc);
2271
2272notify_exit:
Al Virof6a9bc32021-03-05 17:36:04 -05002273 free_dentry_path(page);
Steve Frenchd26c2dd2020-02-06 06:00:14 -06002274 kfree(utf16_path);
2275 return rc;
2276}
2277
Steve French834170c2016-09-30 21:14:26 -05002278static int
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002279smb2_query_dir_first(const unsigned int xid, struct cifs_tcon *tcon,
2280 const char *path, struct cifs_sb_info *cifs_sb,
2281 struct cifs_fid *fid, __u16 search_flags,
2282 struct cifs_search_info *srch_inf)
2283{
2284 __le16 *utf16_path;
Ronnie Sahlberg37478602020-01-08 13:08:06 +10002285 struct smb_rqst rqst[2];
2286 struct kvec rsp_iov[2];
2287 int resp_buftype[2];
2288 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
2289 struct kvec qd_iov[SMB2_QUERY_DIRECTORY_IOV_SIZE];
2290 int rc, flags = 0;
2291 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04002292 struct cifs_open_parms oparms;
Ronnie Sahlberg37478602020-01-08 13:08:06 +10002293 struct smb2_query_directory_rsp *qd_rsp = NULL;
2294 struct smb2_create_rsp *op_rsp = NULL;
Aurelien Aptel352d96f2020-05-31 12:38:22 -05002295 struct TCP_Server_Info *server = cifs_pick_channel(tcon->ses);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002296
2297 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
2298 if (!utf16_path)
2299 return -ENOMEM;
2300
Ronnie Sahlberg37478602020-01-08 13:08:06 +10002301 if (smb3_encryption_required(tcon))
2302 flags |= CIFS_TRANSFORM_REQ;
2303
2304 memset(rqst, 0, sizeof(rqst));
2305 resp_buftype[0] = resp_buftype[1] = CIFS_NO_BUFFER;
2306 memset(rsp_iov, 0, sizeof(rsp_iov));
2307
2308 /* Open */
2309 memset(&open_iov, 0, sizeof(open_iov));
2310 rqst[0].rq_iov = open_iov;
2311 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
2312
Pavel Shilovsky064f6042013-07-09 18:20:30 +04002313 oparms.tcon = tcon;
2314 oparms.desired_access = FILE_READ_ATTRIBUTES | FILE_READ_DATA;
2315 oparms.disposition = FILE_OPEN;
Amir Goldstein0f060932020-02-03 21:46:43 +02002316 oparms.create_options = cifs_create_options(cifs_sb, 0);
Pavel Shilovsky064f6042013-07-09 18:20:30 +04002317 oparms.fid = fid;
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04002318 oparms.reconnect = false;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04002319
Aurelien Aptel352d96f2020-05-31 12:38:22 -05002320 rc = SMB2_open_init(tcon, server,
2321 &rqst[0], &oplock, &oparms, utf16_path);
Ronnie Sahlberg37478602020-01-08 13:08:06 +10002322 if (rc)
2323 goto qdf_free;
2324 smb2_set_next_command(tcon, &rqst[0]);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002325
Ronnie Sahlberg37478602020-01-08 13:08:06 +10002326 /* Query directory */
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002327 srch_inf->entries_in_buffer = 0;
Aurelien Aptel05957512018-05-17 16:35:07 +02002328 srch_inf->index_of_last_entry = 2;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002329
Ronnie Sahlberg37478602020-01-08 13:08:06 +10002330 memset(&qd_iov, 0, sizeof(qd_iov));
2331 rqst[1].rq_iov = qd_iov;
2332 rqst[1].rq_nvec = SMB2_QUERY_DIRECTORY_IOV_SIZE;
2333
Aurelien Aptel352d96f2020-05-31 12:38:22 -05002334 rc = SMB2_query_directory_init(xid, tcon, server,
2335 &rqst[1],
Ronnie Sahlberg37478602020-01-08 13:08:06 +10002336 COMPOUND_FID, COMPOUND_FID,
2337 0, srch_inf->info_level);
2338 if (rc)
2339 goto qdf_free;
2340
2341 smb2_set_related(&rqst[1]);
2342
Aurelien Aptel352d96f2020-05-31 12:38:22 -05002343 rc = compound_send_recv(xid, tcon->ses, server,
2344 flags, 2, rqst,
Ronnie Sahlberg37478602020-01-08 13:08:06 +10002345 resp_buftype, rsp_iov);
2346
2347 /* If the open failed there is nothing to do */
2348 op_rsp = (struct smb2_create_rsp *)rsp_iov[0].iov_base;
2349 if (op_rsp == NULL || op_rsp->sync_hdr.Status != STATUS_SUCCESS) {
2350 cifs_dbg(FYI, "query_dir_first: open failed rc=%d\n", rc);
2351 goto qdf_free;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002352 }
Ronnie Sahlberg37478602020-01-08 13:08:06 +10002353 fid->persistent_fid = op_rsp->PersistentFileId;
2354 fid->volatile_fid = op_rsp->VolatileFileId;
2355
2356 /* Anything else than ENODATA means a genuine error */
2357 if (rc && rc != -ENODATA) {
2358 SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
2359 cifs_dbg(FYI, "query_dir_first: query directory failed rc=%d\n", rc);
2360 trace_smb3_query_dir_err(xid, fid->persistent_fid,
2361 tcon->tid, tcon->ses->Suid, 0, 0, rc);
2362 goto qdf_free;
2363 }
2364
Shyam Prasad N1be1fa42020-03-09 01:35:09 -07002365 atomic_inc(&tcon->num_remote_opens);
2366
Ronnie Sahlberg37478602020-01-08 13:08:06 +10002367 qd_rsp = (struct smb2_query_directory_rsp *)rsp_iov[1].iov_base;
2368 if (qd_rsp->sync_hdr.Status == STATUS_NO_MORE_FILES) {
2369 trace_smb3_query_dir_done(xid, fid->persistent_fid,
2370 tcon->tid, tcon->ses->Suid, 0, 0);
2371 srch_inf->endOfSearch = true;
2372 rc = 0;
2373 goto qdf_free;
2374 }
2375
2376 rc = smb2_parse_query_directory(tcon, &rsp_iov[1], resp_buftype[1],
2377 srch_inf);
2378 if (rc) {
2379 trace_smb3_query_dir_err(xid, fid->persistent_fid, tcon->tid,
2380 tcon->ses->Suid, 0, 0, rc);
2381 goto qdf_free;
2382 }
2383 resp_buftype[1] = CIFS_NO_BUFFER;
2384
2385 trace_smb3_query_dir_done(xid, fid->persistent_fid, tcon->tid,
2386 tcon->ses->Suid, 0, srch_inf->entries_in_buffer);
2387
2388 qdf_free:
2389 kfree(utf16_path);
2390 SMB2_open_free(&rqst[0]);
2391 SMB2_query_directory_free(&rqst[1]);
2392 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
2393 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002394 return rc;
2395}
2396
2397static int
2398smb2_query_dir_next(const unsigned int xid, struct cifs_tcon *tcon,
2399 struct cifs_fid *fid, __u16 search_flags,
2400 struct cifs_search_info *srch_inf)
2401{
2402 return SMB2_query_directory(xid, tcon, fid->persistent_fid,
2403 fid->volatile_fid, 0, srch_inf);
2404}
2405
2406static int
2407smb2_close_dir(const unsigned int xid, struct cifs_tcon *tcon,
2408 struct cifs_fid *fid)
2409{
2410 return SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
2411}
2412
Pavel Shilovsky2e44b282012-09-18 16:20:33 -07002413/*
Christoph Probsta205d502019-05-08 21:36:25 +02002414 * If we negotiate SMB2 protocol and get STATUS_PENDING - update
2415 * the number of credits and return true. Otherwise - return false.
2416 */
Pavel Shilovsky2e44b282012-09-18 16:20:33 -07002417static bool
Pavel Shilovsky66265f12019-01-23 17:11:16 -08002418smb2_is_status_pending(char *buf, struct TCP_Server_Info *server)
Pavel Shilovsky2e44b282012-09-18 16:20:33 -07002419{
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10002420 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
Shyam Prasad N6d82c272021-02-03 23:20:46 -08002421 int scredits, in_flight;
Pavel Shilovsky2e44b282012-09-18 16:20:33 -07002422
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002423 if (shdr->Status != STATUS_PENDING)
Pavel Shilovsky2e44b282012-09-18 16:20:33 -07002424 return false;
2425
Pavel Shilovsky66265f12019-01-23 17:11:16 -08002426 if (shdr->CreditRequest) {
Pavel Shilovsky2e44b282012-09-18 16:20:33 -07002427 spin_lock(&server->req_lock);
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07002428 server->credits += le16_to_cpu(shdr->CreditRequest);
Shyam Prasad Ncd7b6992020-11-12 08:56:49 -08002429 scredits = server->credits;
Shyam Prasad N6d82c272021-02-03 23:20:46 -08002430 in_flight = server->in_flight;
Pavel Shilovsky2e44b282012-09-18 16:20:33 -07002431 spin_unlock(&server->req_lock);
2432 wake_up(&server->request_q);
Shyam Prasad Ncd7b6992020-11-12 08:56:49 -08002433
2434 trace_smb3_add_credits(server->CurrentMid,
Shyam Prasad N6d82c272021-02-03 23:20:46 -08002435 server->conn_id, server->hostname, scredits,
2436 le16_to_cpu(shdr->CreditRequest), in_flight);
Shyam Prasad Ncd7b6992020-11-12 08:56:49 -08002437 cifs_dbg(FYI, "%s: status pending add %u credits total=%d\n",
2438 __func__, le16_to_cpu(shdr->CreditRequest), scredits);
Pavel Shilovsky2e44b282012-09-18 16:20:33 -07002439 }
2440
2441 return true;
2442}
2443
Pavel Shilovsky511c54a2017-07-08 14:32:00 -07002444static bool
2445smb2_is_session_expired(char *buf)
2446{
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10002447 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
Pavel Shilovsky511c54a2017-07-08 14:32:00 -07002448
Mark Symsd81243c2018-05-24 09:47:31 +01002449 if (shdr->Status != STATUS_NETWORK_SESSION_EXPIRED &&
2450 shdr->Status != STATUS_USER_SESSION_DELETED)
Pavel Shilovsky511c54a2017-07-08 14:32:00 -07002451 return false;
2452
Steve Frenche68a9322018-07-30 14:23:58 -05002453 trace_smb3_ses_expired(shdr->TreeId, shdr->SessionId,
2454 le16_to_cpu(shdr->Command),
2455 le64_to_cpu(shdr->MessageId));
Mark Symsd81243c2018-05-24 09:47:31 +01002456 cifs_dbg(FYI, "Session expired or deleted\n");
Steve Frenche68a9322018-07-30 14:23:58 -05002457
Pavel Shilovsky511c54a2017-07-08 14:32:00 -07002458 return true;
2459}
2460
Rohith Surabattula8e670f72020-09-18 05:37:28 +00002461static bool
2462smb2_is_status_io_timeout(char *buf)
2463{
2464 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
2465
2466 if (shdr->Status == STATUS_IO_TIMEOUT)
2467 return true;
2468 else
2469 return false;
2470}
2471
Rohith Surabattula9e550b02021-02-16 10:40:45 +00002472static void
2473smb2_is_network_name_deleted(char *buf, struct TCP_Server_Info *server)
2474{
2475 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
2476 struct list_head *tmp, *tmp1;
2477 struct cifs_ses *ses;
2478 struct cifs_tcon *tcon;
2479
Steve Frenchf1a08652021-02-20 18:52:15 -06002480 if (shdr->Status != STATUS_NETWORK_NAME_DELETED)
2481 return;
2482
2483 spin_lock(&cifs_tcp_ses_lock);
2484 list_for_each(tmp, &server->smb_ses_list) {
2485 ses = list_entry(tmp, struct cifs_ses, smb_ses_list);
2486 list_for_each(tmp1, &ses->tcon_list) {
2487 tcon = list_entry(tmp1, struct cifs_tcon, tcon_list);
2488 if (tcon->tid == shdr->TreeId) {
2489 tcon->need_reconnect = true;
2490 spin_unlock(&cifs_tcp_ses_lock);
2491 pr_warn_once("Server share %s deleted.\n",
2492 tcon->treeName);
2493 return;
Rohith Surabattula9e550b02021-02-16 10:40:45 +00002494 }
2495 }
Rohith Surabattula9e550b02021-02-16 10:40:45 +00002496 }
Steve Frenchf1a08652021-02-20 18:52:15 -06002497 spin_unlock(&cifs_tcp_ses_lock);
Rohith Surabattula9e550b02021-02-16 10:40:45 +00002498}
2499
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07002500static int
2501smb2_oplock_response(struct cifs_tcon *tcon, struct cifs_fid *fid,
2502 struct cifsInodeInfo *cinode)
2503{
Pavel Shilovsky0822f512012-09-19 06:22:45 -07002504 if (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_LEASING)
2505 return SMB2_lease_break(0, tcon, cinode->lease_key,
2506 smb2_get_lease_state(cinode));
2507
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07002508 return SMB2_oplock_break(0, tcon, fid->persistent_fid,
2509 fid->volatile_fid,
Pavel Shilovsky18cceb62013-09-05 13:01:06 +04002510 CIFS_CACHE_READ(cinode) ? 1 : 0);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07002511}
2512
Ronnie Sahlbergc5a5f382018-09-03 13:33:41 +10002513void
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10002514smb2_set_related(struct smb_rqst *rqst)
2515{
2516 struct smb2_sync_hdr *shdr;
2517
2518 shdr = (struct smb2_sync_hdr *)(rqst->rq_iov[0].iov_base);
Ronnie Sahlberg88a92c92019-07-16 10:41:46 +10002519 if (shdr == NULL) {
2520 cifs_dbg(FYI, "shdr NULL in smb2_set_related\n");
2521 return;
2522 }
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10002523 shdr->Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
2524}
2525
2526char smb2_padding[7] = {0, 0, 0, 0, 0, 0, 0};
2527
Ronnie Sahlbergc5a5f382018-09-03 13:33:41 +10002528void
Ronnie Sahlberge77fe732018-12-31 13:43:40 +10002529smb2_set_next_command(struct cifs_tcon *tcon, struct smb_rqst *rqst)
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10002530{
2531 struct smb2_sync_hdr *shdr;
Ronnie Sahlberge77fe732018-12-31 13:43:40 +10002532 struct cifs_ses *ses = tcon->ses;
2533 struct TCP_Server_Info *server = ses->server;
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10002534 unsigned long len = smb_rqst_len(server, rqst);
Ronnie Sahlberge77fe732018-12-31 13:43:40 +10002535 int i, num_padding;
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10002536
Ronnie Sahlberg88a92c92019-07-16 10:41:46 +10002537 shdr = (struct smb2_sync_hdr *)(rqst->rq_iov[0].iov_base);
2538 if (shdr == NULL) {
2539 cifs_dbg(FYI, "shdr NULL in smb2_set_next_command\n");
2540 return;
2541 }
2542
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10002543 /* SMB headers in a compound are 8 byte aligned. */
Ronnie Sahlberge77fe732018-12-31 13:43:40 +10002544
2545 /* No padding needed */
2546 if (!(len & 7))
2547 goto finished;
2548
2549 num_padding = 8 - (len & 7);
2550 if (!smb3_encryption_required(tcon)) {
2551 /*
2552 * If we do not have encryption then we can just add an extra
2553 * iov for the padding.
2554 */
2555 rqst->rq_iov[rqst->rq_nvec].iov_base = smb2_padding;
2556 rqst->rq_iov[rqst->rq_nvec].iov_len = num_padding;
2557 rqst->rq_nvec++;
2558 len += num_padding;
2559 } else {
2560 /*
2561 * We can not add a small padding iov for the encryption case
2562 * because the encryption framework can not handle the padding
2563 * iovs.
2564 * We have to flatten this into a single buffer and add
2565 * the padding to it.
2566 */
2567 for (i = 1; i < rqst->rq_nvec; i++) {
2568 memcpy(rqst->rq_iov[0].iov_base +
2569 rqst->rq_iov[0].iov_len,
2570 rqst->rq_iov[i].iov_base,
2571 rqst->rq_iov[i].iov_len);
2572 rqst->rq_iov[0].iov_len += rqst->rq_iov[i].iov_len;
Ronnie Sahlberg271b9c02018-12-18 17:49:05 -06002573 }
Ronnie Sahlberge77fe732018-12-31 13:43:40 +10002574 memset(rqst->rq_iov[0].iov_base + rqst->rq_iov[0].iov_len,
2575 0, num_padding);
2576 rqst->rq_iov[0].iov_len += num_padding;
2577 len += num_padding;
2578 rqst->rq_nvec = 1;
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10002579 }
2580
Ronnie Sahlberge77fe732018-12-31 13:43:40 +10002581 finished:
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10002582 shdr->NextCommand = cpu_to_le32(len);
2583}
2584
Ronnie Sahlberg07d3b2e2018-12-20 22:03:04 -06002585/*
2586 * Passes the query info response back to the caller on success.
2587 * Caller need to free this with free_rsp_buf().
2588 */
Ronnie Sahlbergf9793b62018-11-27 09:52:04 +10002589int
Ronnie Sahlberg07d3b2e2018-12-20 22:03:04 -06002590smb2_query_info_compound(const unsigned int xid, struct cifs_tcon *tcon,
2591 __le16 *utf16_path, u32 desired_access,
2592 u32 class, u32 type, u32 output_len,
Ronnie Sahlbergf9793b62018-11-27 09:52:04 +10002593 struct kvec *rsp, int *buftype,
2594 struct cifs_sb_info *cifs_sb)
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07002595{
Ronnie Sahlberg07d3b2e2018-12-20 22:03:04 -06002596 struct cifs_ses *ses = tcon->ses;
Aurelien Aptel352d96f2020-05-31 12:38:22 -05002597 struct TCP_Server_Info *server = cifs_pick_channel(ses);
Paulo Alcantara04ad69c2021-03-08 12:00:50 -03002598 int flags = CIFS_CP_CREATE_CLOSE_OP;
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10002599 struct smb_rqst rqst[3];
2600 int resp_buftype[3];
2601 struct kvec rsp_iov[3];
Ronnie Sahlberg4d8dfaf2018-08-21 11:49:21 +10002602 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10002603 struct kvec qi_iov[1];
2604 struct kvec close_iov[1];
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07002605 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04002606 struct cifs_open_parms oparms;
2607 struct cifs_fid fid;
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10002608 int rc;
2609
2610 if (smb3_encryption_required(tcon))
2611 flags |= CIFS_TRANSFORM_REQ;
2612
2613 memset(rqst, 0, sizeof(rqst));
Ronnie Sahlbergc5a5f382018-09-03 13:33:41 +10002614 resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10002615 memset(rsp_iov, 0, sizeof(rsp_iov));
2616
2617 memset(&open_iov, 0, sizeof(open_iov));
2618 rqst[0].rq_iov = open_iov;
Ronnie Sahlberg4d8dfaf2018-08-21 11:49:21 +10002619 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07002620
Pavel Shilovsky064f6042013-07-09 18:20:30 +04002621 oparms.tcon = tcon;
Ronnie Sahlberg07d3b2e2018-12-20 22:03:04 -06002622 oparms.desired_access = desired_access;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04002623 oparms.disposition = FILE_OPEN;
Amir Goldstein0f060932020-02-03 21:46:43 +02002624 oparms.create_options = cifs_create_options(cifs_sb, 0);
Pavel Shilovsky064f6042013-07-09 18:20:30 +04002625 oparms.fid = &fid;
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04002626 oparms.reconnect = false;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04002627
Aurelien Aptel352d96f2020-05-31 12:38:22 -05002628 rc = SMB2_open_init(tcon, server,
2629 &rqst[0], &oplock, &oparms, utf16_path);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07002630 if (rc)
Ronnie Sahlberg07d3b2e2018-12-20 22:03:04 -06002631 goto qic_exit;
Ronnie Sahlberge77fe732018-12-31 13:43:40 +10002632 smb2_set_next_command(tcon, &rqst[0]);
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10002633
2634 memset(&qi_iov, 0, sizeof(qi_iov));
2635 rqst[1].rq_iov = qi_iov;
2636 rqst[1].rq_nvec = 1;
2637
Aurelien Aptel352d96f2020-05-31 12:38:22 -05002638 rc = SMB2_query_info_init(tcon, server,
2639 &rqst[1], COMPOUND_FID, COMPOUND_FID,
Ronnie Sahlberg07d3b2e2018-12-20 22:03:04 -06002640 class, type, 0,
2641 output_len, 0,
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05002642 NULL);
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10002643 if (rc)
Ronnie Sahlberg07d3b2e2018-12-20 22:03:04 -06002644 goto qic_exit;
Ronnie Sahlberge77fe732018-12-31 13:43:40 +10002645 smb2_set_next_command(tcon, &rqst[1]);
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10002646 smb2_set_related(&rqst[1]);
2647
2648 memset(&close_iov, 0, sizeof(close_iov));
2649 rqst[2].rq_iov = close_iov;
2650 rqst[2].rq_nvec = 1;
2651
Aurelien Aptel352d96f2020-05-31 12:38:22 -05002652 rc = SMB2_close_init(tcon, server,
2653 &rqst[2], COMPOUND_FID, COMPOUND_FID, false);
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10002654 if (rc)
Ronnie Sahlberg07d3b2e2018-12-20 22:03:04 -06002655 goto qic_exit;
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10002656 smb2_set_related(&rqst[2]);
2657
Aurelien Aptel352d96f2020-05-31 12:38:22 -05002658 rc = compound_send_recv(xid, ses, server,
2659 flags, 3, rqst,
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10002660 resp_buftype, rsp_iov);
Ronnie Sahlbergf9793b62018-11-27 09:52:04 +10002661 if (rc) {
2662 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
Steve French7dcc82c2019-09-11 00:07:36 -05002663 if (rc == -EREMCHG) {
2664 tcon->need_reconnect = true;
Joe Perchesa0a30362020-04-14 22:42:53 -07002665 pr_warn_once("server share %s deleted\n",
2666 tcon->treeName);
Steve French7dcc82c2019-09-11 00:07:36 -05002667 }
Ronnie Sahlberg07d3b2e2018-12-20 22:03:04 -06002668 goto qic_exit;
Ronnie Sahlbergf9793b62018-11-27 09:52:04 +10002669 }
Ronnie Sahlberg07d3b2e2018-12-20 22:03:04 -06002670 *rsp = rsp_iov[1];
2671 *buftype = resp_buftype[1];
2672
2673 qic_exit:
2674 SMB2_open_free(&rqst[0]);
2675 SMB2_query_info_free(&rqst[1]);
2676 SMB2_close_free(&rqst[2]);
2677 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
2678 free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
2679 return rc;
2680}
2681
2682static int
2683smb2_queryfs(const unsigned int xid, struct cifs_tcon *tcon,
Amir Goldstein0f060932020-02-03 21:46:43 +02002684 struct cifs_sb_info *cifs_sb, struct kstatfs *buf)
Ronnie Sahlberg07d3b2e2018-12-20 22:03:04 -06002685{
2686 struct smb2_query_info_rsp *rsp;
2687 struct smb2_fs_full_size_info *info = NULL;
2688 __le16 utf16_path = 0; /* Null - open root of share */
2689 struct kvec rsp_iov = {NULL, 0};
2690 int buftype = CIFS_NO_BUFFER;
2691 int rc;
2692
2693
2694 rc = smb2_query_info_compound(xid, tcon, &utf16_path,
2695 FILE_READ_ATTRIBUTES,
2696 FS_FULL_SIZE_INFORMATION,
2697 SMB2_O_INFO_FILESYSTEM,
2698 sizeof(struct smb2_fs_full_size_info),
Steve French87f93d82020-02-04 13:02:59 -06002699 &rsp_iov, &buftype, cifs_sb);
Ronnie Sahlberg07d3b2e2018-12-20 22:03:04 -06002700 if (rc)
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10002701 goto qfs_exit;
2702
Ronnie Sahlberg07d3b2e2018-12-20 22:03:04 -06002703 rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07002704 buf->f_type = SMB2_MAGIC_NUMBER;
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10002705 info = (struct smb2_fs_full_size_info *)(
2706 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
2707 rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
2708 le32_to_cpu(rsp->OutputBufferLength),
Ronnie Sahlberg07d3b2e2018-12-20 22:03:04 -06002709 &rsp_iov,
Ronnie Sahlberg730928c2018-08-08 15:07:49 +10002710 sizeof(struct smb2_fs_full_size_info));
2711 if (!rc)
2712 smb2_copy_fs_info_to_kstatfs(info, buf);
2713
2714qfs_exit:
Ronnie Sahlberg07d3b2e2018-12-20 22:03:04 -06002715 free_rsp_buf(buftype, rsp_iov.iov_base);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07002716 return rc;
2717}
2718
Steve French2d304212018-06-24 23:28:12 -05002719static int
2720smb311_queryfs(const unsigned int xid, struct cifs_tcon *tcon,
Amir Goldstein0f060932020-02-03 21:46:43 +02002721 struct cifs_sb_info *cifs_sb, struct kstatfs *buf)
Steve French2d304212018-06-24 23:28:12 -05002722{
2723 int rc;
2724 __le16 srch_path = 0; /* Null - open root of share */
2725 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2726 struct cifs_open_parms oparms;
2727 struct cifs_fid fid;
2728
2729 if (!tcon->posix_extensions)
Amir Goldstein0f060932020-02-03 21:46:43 +02002730 return smb2_queryfs(xid, tcon, cifs_sb, buf);
Steve French2d304212018-06-24 23:28:12 -05002731
2732 oparms.tcon = tcon;
2733 oparms.desired_access = FILE_READ_ATTRIBUTES;
2734 oparms.disposition = FILE_OPEN;
Amir Goldstein0f060932020-02-03 21:46:43 +02002735 oparms.create_options = cifs_create_options(cifs_sb, 0);
Steve French2d304212018-06-24 23:28:12 -05002736 oparms.fid = &fid;
2737 oparms.reconnect = false;
2738
Aurelien Aptel69dda302020-03-02 17:53:22 +01002739 rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL,
2740 NULL, NULL);
Steve French2d304212018-06-24 23:28:12 -05002741 if (rc)
2742 return rc;
2743
2744 rc = SMB311_posix_qfs_info(xid, tcon, fid.persistent_fid,
2745 fid.volatile_fid, buf);
2746 buf->f_type = SMB2_MAGIC_NUMBER;
2747 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
2748 return rc;
2749}
Steve French2d304212018-06-24 23:28:12 -05002750
Pavel Shilovsky027e8ee2012-09-19 06:22:43 -07002751static bool
2752smb2_compare_fids(struct cifsFileInfo *ob1, struct cifsFileInfo *ob2)
2753{
2754 return ob1->fid.persistent_fid == ob2->fid.persistent_fid &&
2755 ob1->fid.volatile_fid == ob2->fid.volatile_fid;
2756}
2757
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07002758static int
2759smb2_mand_lock(const unsigned int xid, struct cifsFileInfo *cfile, __u64 offset,
2760 __u64 length, __u32 type, int lock, int unlock, bool wait)
2761{
2762 if (unlock && !lock)
2763 type = SMB2_LOCKFLAG_UNLOCK;
2764 return SMB2_lock(xid, tlink_tcon(cfile->tlink),
2765 cfile->fid.persistent_fid, cfile->fid.volatile_fid,
2766 current->tgid, length, offset, type, wait);
2767}
2768
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07002769static void
2770smb2_get_lease_key(struct inode *inode, struct cifs_fid *fid)
2771{
2772 memcpy(fid->lease_key, CIFS_I(inode)->lease_key, SMB2_LEASE_KEY_SIZE);
2773}
2774
2775static void
2776smb2_set_lease_key(struct inode *inode, struct cifs_fid *fid)
2777{
2778 memcpy(CIFS_I(inode)->lease_key, fid->lease_key, SMB2_LEASE_KEY_SIZE);
2779}
2780
2781static void
2782smb2_new_lease_key(struct cifs_fid *fid)
2783{
Steve Frenchfa70b872016-09-22 00:39:34 -05002784 generate_random_uuid(fid->lease_key);
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07002785}
2786
Aurelien Aptel9d496402017-02-13 16:16:49 +01002787static int
2788smb2_get_dfs_refer(const unsigned int xid, struct cifs_ses *ses,
2789 const char *search_name,
2790 struct dfs_info3_param **target_nodes,
2791 unsigned int *num_of_nodes,
2792 const struct nls_table *nls_codepage, int remap)
2793{
2794 int rc;
2795 __le16 *utf16_path = NULL;
2796 int utf16_path_len = 0;
2797 struct cifs_tcon *tcon;
2798 struct fsctl_get_dfs_referral_req *dfs_req = NULL;
2799 struct get_dfs_referral_rsp *dfs_rsp = NULL;
2800 u32 dfs_req_size = 0, dfs_rsp_size = 0;
2801
Christoph Probsta205d502019-05-08 21:36:25 +02002802 cifs_dbg(FYI, "%s: path: %s\n", __func__, search_name);
Aurelien Aptel9d496402017-02-13 16:16:49 +01002803
2804 /*
Aurelien Aptel63a83b82018-01-24 13:46:11 +01002805 * Try to use the IPC tcon, otherwise just use any
Aurelien Aptel9d496402017-02-13 16:16:49 +01002806 */
Aurelien Aptel63a83b82018-01-24 13:46:11 +01002807 tcon = ses->tcon_ipc;
2808 if (tcon == NULL) {
2809 spin_lock(&cifs_tcp_ses_lock);
2810 tcon = list_first_entry_or_null(&ses->tcon_list,
2811 struct cifs_tcon,
2812 tcon_list);
2813 if (tcon)
2814 tcon->tc_count++;
2815 spin_unlock(&cifs_tcp_ses_lock);
2816 }
Aurelien Aptel9d496402017-02-13 16:16:49 +01002817
Aurelien Aptel63a83b82018-01-24 13:46:11 +01002818 if (tcon == NULL) {
Aurelien Aptel9d496402017-02-13 16:16:49 +01002819 cifs_dbg(VFS, "session %p has no tcon available for a dfs referral request\n",
2820 ses);
2821 rc = -ENOTCONN;
2822 goto out;
2823 }
2824
2825 utf16_path = cifs_strndup_to_utf16(search_name, PATH_MAX,
2826 &utf16_path_len,
2827 nls_codepage, remap);
2828 if (!utf16_path) {
2829 rc = -ENOMEM;
2830 goto out;
2831 }
2832
2833 dfs_req_size = sizeof(*dfs_req) + utf16_path_len;
2834 dfs_req = kzalloc(dfs_req_size, GFP_KERNEL);
2835 if (!dfs_req) {
2836 rc = -ENOMEM;
2837 goto out;
2838 }
2839
2840 /* Highest DFS referral version understood */
2841 dfs_req->MaxReferralLevel = DFS_VERSION;
2842
2843 /* Path to resolve in an UTF-16 null-terminated string */
2844 memcpy(dfs_req->RequestFileName, utf16_path, utf16_path_len);
2845
2846 do {
Aurelien Aptel9d496402017-02-13 16:16:49 +01002847 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
2848 FSCTL_DFS_GET_REFERRALS,
Aurelien Aptel63a83b82018-01-24 13:46:11 +01002849 true /* is_fsctl */,
Steve French153322f2019-03-28 22:32:49 -05002850 (char *)dfs_req, dfs_req_size, CIFSMaxBufSize,
Aurelien Aptel9d496402017-02-13 16:16:49 +01002851 (char **)&dfs_rsp, &dfs_rsp_size);
Aurelien Aptel9d496402017-02-13 16:16:49 +01002852 } while (rc == -EAGAIN);
2853
2854 if (rc) {
Steve French2564f2f2018-03-21 23:16:36 -05002855 if ((rc != -ENOENT) && (rc != -EOPNOTSUPP))
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +10002856 cifs_tcon_dbg(VFS, "ioctl error in %s rc=%d\n", __func__, rc);
Aurelien Aptel9d496402017-02-13 16:16:49 +01002857 goto out;
2858 }
2859
2860 rc = parse_dfs_referrals(dfs_rsp, dfs_rsp_size,
2861 num_of_nodes, target_nodes,
2862 nls_codepage, remap, search_name,
2863 true /* is_unicode */);
2864 if (rc) {
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +10002865 cifs_tcon_dbg(VFS, "parse error in %s rc=%d\n", __func__, rc);
Aurelien Aptel9d496402017-02-13 16:16:49 +01002866 goto out;
2867 }
2868
2869 out:
Aurelien Aptel63a83b82018-01-24 13:46:11 +01002870 if (tcon && !tcon->ipc) {
2871 /* ipc tcons are not refcounted */
Aurelien Aptel9d496402017-02-13 16:16:49 +01002872 spin_lock(&cifs_tcp_ses_lock);
2873 tcon->tc_count--;
2874 spin_unlock(&cifs_tcp_ses_lock);
2875 }
2876 kfree(utf16_path);
2877 kfree(dfs_req);
2878 kfree(dfs_rsp);
2879 return rc;
2880}
Ronnie Sahlberg5de254d2019-06-27 14:57:02 +10002881
2882static int
Steve Frenchd5ecebc2019-06-28 02:04:18 -05002883parse_reparse_posix(struct reparse_posix_data *symlink_buf,
2884 u32 plen, char **target_path,
2885 struct cifs_sb_info *cifs_sb)
2886{
2887 unsigned int len;
2888
2889 /* See MS-FSCC 2.1.2.6 for the 'NFS' style reparse tags */
2890 len = le16_to_cpu(symlink_buf->ReparseDataLength);
2891
Steve Frenchd5ecebc2019-06-28 02:04:18 -05002892 if (le64_to_cpu(symlink_buf->InodeType) != NFS_SPECFILE_LNK) {
2893 cifs_dbg(VFS, "%lld not a supported symlink type\n",
2894 le64_to_cpu(symlink_buf->InodeType));
2895 return -EOPNOTSUPP;
2896 }
2897
2898 *target_path = cifs_strndup_from_utf16(
2899 symlink_buf->PathBuffer,
2900 len, true, cifs_sb->local_nls);
2901 if (!(*target_path))
2902 return -ENOMEM;
2903
2904 convert_delimiter(*target_path, '/');
2905 cifs_dbg(FYI, "%s: target path: %s\n", __func__, *target_path);
2906
2907 return 0;
2908}
2909
2910static int
Ronnie Sahlberg5de254d2019-06-27 14:57:02 +10002911parse_reparse_symlink(struct reparse_symlink_data_buffer *symlink_buf,
2912 u32 plen, char **target_path,
2913 struct cifs_sb_info *cifs_sb)
2914{
2915 unsigned int sub_len;
2916 unsigned int sub_offset;
2917
Steve Frenchd5ecebc2019-06-28 02:04:18 -05002918 /* We handle Symbolic Link reparse tag here. See: MS-FSCC 2.1.2.4 */
Ronnie Sahlberg5de254d2019-06-27 14:57:02 +10002919
2920 sub_offset = le16_to_cpu(symlink_buf->SubstituteNameOffset);
2921 sub_len = le16_to_cpu(symlink_buf->SubstituteNameLength);
2922 if (sub_offset + 20 > plen ||
2923 sub_offset + sub_len + 20 > plen) {
2924 cifs_dbg(VFS, "srv returned malformed symlink buffer\n");
2925 return -EIO;
2926 }
2927
2928 *target_path = cifs_strndup_from_utf16(
2929 symlink_buf->PathBuffer + sub_offset,
2930 sub_len, true, cifs_sb->local_nls);
2931 if (!(*target_path))
2932 return -ENOMEM;
2933
2934 convert_delimiter(*target_path, '/');
2935 cifs_dbg(FYI, "%s: target path: %s\n", __func__, *target_path);
2936
2937 return 0;
2938}
2939
Steve Frenchd5ecebc2019-06-28 02:04:18 -05002940static int
Ronnie Sahlbergf5f111c2019-07-07 07:45:42 +10002941parse_reparse_point(struct reparse_data_buffer *buf,
2942 u32 plen, char **target_path,
2943 struct cifs_sb_info *cifs_sb)
Steve Frenchd5ecebc2019-06-28 02:04:18 -05002944{
Ronnie Sahlbergf5f111c2019-07-07 07:45:42 +10002945 if (plen < sizeof(struct reparse_data_buffer)) {
Joe Perchesa0a30362020-04-14 22:42:53 -07002946 cifs_dbg(VFS, "reparse buffer is too small. Must be at least 8 bytes but was %d\n",
2947 plen);
Ronnie Sahlbergf5f111c2019-07-07 07:45:42 +10002948 return -EIO;
2949 }
2950
2951 if (plen < le16_to_cpu(buf->ReparseDataLength) +
2952 sizeof(struct reparse_data_buffer)) {
Joe Perchesa0a30362020-04-14 22:42:53 -07002953 cifs_dbg(VFS, "srv returned invalid reparse buf length: %d\n",
2954 plen);
Ronnie Sahlbergf5f111c2019-07-07 07:45:42 +10002955 return -EIO;
2956 }
2957
Steve Frenchd5ecebc2019-06-28 02:04:18 -05002958 /* See MS-FSCC 2.1.2 */
Ronnie Sahlbergf5f111c2019-07-07 07:45:42 +10002959 switch (le32_to_cpu(buf->ReparseTag)) {
2960 case IO_REPARSE_TAG_NFS:
2961 return parse_reparse_posix(
2962 (struct reparse_posix_data *)buf,
Steve Frenchd5ecebc2019-06-28 02:04:18 -05002963 plen, target_path, cifs_sb);
Ronnie Sahlbergf5f111c2019-07-07 07:45:42 +10002964 case IO_REPARSE_TAG_SYMLINK:
2965 return parse_reparse_symlink(
2966 (struct reparse_symlink_data_buffer *)buf,
2967 plen, target_path, cifs_sb);
2968 default:
Joe Perchesa0a30362020-04-14 22:42:53 -07002969 cifs_dbg(VFS, "srv returned unknown symlink buffer tag:0x%08x\n",
2970 le32_to_cpu(buf->ReparseTag));
Ronnie Sahlbergf5f111c2019-07-07 07:45:42 +10002971 return -EOPNOTSUPP;
2972 }
Steve Frenchd5ecebc2019-06-28 02:04:18 -05002973}
2974
Pavel Shilovsky78932422016-07-24 10:37:38 +03002975#define SMB2_SYMLINK_STRUCT_SIZE \
2976 (sizeof(struct smb2_err_rsp) - 1 + sizeof(struct smb2_symlink_err_rsp))
2977
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04002978static int
2979smb2_query_symlink(const unsigned int xid, struct cifs_tcon *tcon,
Ronnie Sahlbergebaf5462019-04-10 08:44:46 +10002980 struct cifs_sb_info *cifs_sb, const char *full_path,
2981 char **target_path, bool is_reparse_point)
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04002982{
2983 int rc;
Ronnie Sahlbergebaf5462019-04-10 08:44:46 +10002984 __le16 *utf16_path = NULL;
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04002985 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2986 struct cifs_open_parms oparms;
2987 struct cifs_fid fid;
Ronnie Sahlberg91cb74f2018-04-13 09:03:19 +10002988 struct kvec err_iov = {NULL, 0};
Ronnie Sahlberg9d874c32018-06-08 13:21:18 +10002989 struct smb2_err_rsp *err_buf = NULL;
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04002990 struct smb2_symlink_err_rsp *symlink;
Aurelien Aptel352d96f2020-05-31 12:38:22 -05002991 struct TCP_Server_Info *server = cifs_pick_channel(tcon->ses);
Pavel Shilovsky78932422016-07-24 10:37:38 +03002992 unsigned int sub_len;
2993 unsigned int sub_offset;
2994 unsigned int print_len;
2995 unsigned int print_offset;
Paulo Alcantara04ad69c2021-03-08 12:00:50 -03002996 int flags = CIFS_CP_CREATE_CLOSE_OP;
Ronnie Sahlbergebaf5462019-04-10 08:44:46 +10002997 struct smb_rqst rqst[3];
2998 int resp_buftype[3];
2999 struct kvec rsp_iov[3];
3000 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
3001 struct kvec io_iov[SMB2_IOCTL_IOV_SIZE];
3002 struct kvec close_iov[1];
3003 struct smb2_create_rsp *create_rsp;
3004 struct smb2_ioctl_rsp *ioctl_rsp;
Ronnie Sahlberg5de254d2019-06-27 14:57:02 +10003005 struct reparse_data_buffer *reparse_buf;
Amir Goldstein0f060932020-02-03 21:46:43 +02003006 int create_options = is_reparse_point ? OPEN_REPARSE_POINT : 0;
Ronnie Sahlbergebaf5462019-04-10 08:44:46 +10003007 u32 plen;
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04003008
3009 cifs_dbg(FYI, "%s: path: %s\n", __func__, full_path);
3010
Ronnie Sahlberg5de254d2019-06-27 14:57:02 +10003011 *target_path = NULL;
3012
Ronnie Sahlbergebaf5462019-04-10 08:44:46 +10003013 if (smb3_encryption_required(tcon))
3014 flags |= CIFS_TRANSFORM_REQ;
3015
3016 memset(rqst, 0, sizeof(rqst));
3017 resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
3018 memset(rsp_iov, 0, sizeof(rsp_iov));
3019
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04003020 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
3021 if (!utf16_path)
3022 return -ENOMEM;
3023
Ronnie Sahlbergebaf5462019-04-10 08:44:46 +10003024 /* Open */
3025 memset(&open_iov, 0, sizeof(open_iov));
3026 rqst[0].rq_iov = open_iov;
3027 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
3028
3029 memset(&oparms, 0, sizeof(oparms));
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04003030 oparms.tcon = tcon;
3031 oparms.desired_access = FILE_READ_ATTRIBUTES;
3032 oparms.disposition = FILE_OPEN;
Amir Goldstein0f060932020-02-03 21:46:43 +02003033 oparms.create_options = cifs_create_options(cifs_sb, create_options);
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04003034 oparms.fid = &fid;
3035 oparms.reconnect = false;
3036
Aurelien Aptel352d96f2020-05-31 12:38:22 -05003037 rc = SMB2_open_init(tcon, server,
3038 &rqst[0], &oplock, &oparms, utf16_path);
Ronnie Sahlbergebaf5462019-04-10 08:44:46 +10003039 if (rc)
3040 goto querty_exit;
3041 smb2_set_next_command(tcon, &rqst[0]);
3042
3043
3044 /* IOCTL */
3045 memset(&io_iov, 0, sizeof(io_iov));
3046 rqst[1].rq_iov = io_iov;
3047 rqst[1].rq_nvec = SMB2_IOCTL_IOV_SIZE;
3048
Aurelien Aptel352d96f2020-05-31 12:38:22 -05003049 rc = SMB2_ioctl_init(tcon, server,
3050 &rqst[1], fid.persistent_fid,
Ronnie Sahlbergebaf5462019-04-10 08:44:46 +10003051 fid.volatile_fid, FSCTL_GET_REPARSE_POINT,
Ronnie Sahlberg731b82b2020-01-08 13:08:07 +10003052 true /* is_fctl */, NULL, 0,
3053 CIFSMaxBufSize -
3054 MAX_SMB2_CREATE_RESPONSE_SIZE -
3055 MAX_SMB2_CLOSE_RESPONSE_SIZE);
Ronnie Sahlbergebaf5462019-04-10 08:44:46 +10003056 if (rc)
3057 goto querty_exit;
3058
3059 smb2_set_next_command(tcon, &rqst[1]);
3060 smb2_set_related(&rqst[1]);
3061
3062
3063 /* Close */
3064 memset(&close_iov, 0, sizeof(close_iov));
3065 rqst[2].rq_iov = close_iov;
3066 rqst[2].rq_nvec = 1;
3067
Aurelien Aptel352d96f2020-05-31 12:38:22 -05003068 rc = SMB2_close_init(tcon, server,
3069 &rqst[2], COMPOUND_FID, COMPOUND_FID, false);
Ronnie Sahlbergebaf5462019-04-10 08:44:46 +10003070 if (rc)
3071 goto querty_exit;
3072
3073 smb2_set_related(&rqst[2]);
3074
Aurelien Aptel352d96f2020-05-31 12:38:22 -05003075 rc = compound_send_recv(xid, tcon->ses, server,
3076 flags, 3, rqst,
Ronnie Sahlbergebaf5462019-04-10 08:44:46 +10003077 resp_buftype, rsp_iov);
3078
3079 create_rsp = rsp_iov[0].iov_base;
3080 if (create_rsp && create_rsp->sync_hdr.Status)
3081 err_iov = rsp_iov[0];
3082 ioctl_rsp = rsp_iov[1].iov_base;
3083
3084 /*
3085 * Open was successful and we got an ioctl response.
3086 */
3087 if ((rc == 0) && (is_reparse_point)) {
3088 /* See MS-FSCC 2.3.23 */
3089
Ronnie Sahlberg5de254d2019-06-27 14:57:02 +10003090 reparse_buf = (struct reparse_data_buffer *)
3091 ((char *)ioctl_rsp +
3092 le32_to_cpu(ioctl_rsp->OutputOffset));
Ronnie Sahlbergebaf5462019-04-10 08:44:46 +10003093 plen = le32_to_cpu(ioctl_rsp->OutputCount);
3094
3095 if (plen + le32_to_cpu(ioctl_rsp->OutputOffset) >
3096 rsp_iov[1].iov_len) {
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +10003097 cifs_tcon_dbg(VFS, "srv returned invalid ioctl len: %d\n",
Ronnie Sahlberg5de254d2019-06-27 14:57:02 +10003098 plen);
Ronnie Sahlbergebaf5462019-04-10 08:44:46 +10003099 rc = -EIO;
3100 goto querty_exit;
3101 }
3102
Ronnie Sahlbergf5f111c2019-07-07 07:45:42 +10003103 rc = parse_reparse_point(reparse_buf, plen, target_path,
3104 cifs_sb);
Ronnie Sahlbergebaf5462019-04-10 08:44:46 +10003105 goto querty_exit;
3106 }
3107
Gustavo A. R. Silva0d568cd2018-04-13 10:13:29 -05003108 if (!rc || !err_iov.iov_base) {
Ronnie Sahlberg9d874c32018-06-08 13:21:18 +10003109 rc = -ENOENT;
Ronnie Sahlbergebaf5462019-04-10 08:44:46 +10003110 goto querty_exit;
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04003111 }
Pavel Shilovsky78932422016-07-24 10:37:38 +03003112
Ronnie Sahlberg91cb74f2018-04-13 09:03:19 +10003113 err_buf = err_iov.iov_base;
Pavel Shilovsky78932422016-07-24 10:37:38 +03003114 if (le32_to_cpu(err_buf->ByteCount) < sizeof(struct smb2_symlink_err_rsp) ||
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10003115 err_iov.iov_len < SMB2_SYMLINK_STRUCT_SIZE) {
Ronnie Sahlbergdf070af2019-07-09 18:41:11 +10003116 rc = -EINVAL;
3117 goto querty_exit;
3118 }
3119
3120 symlink = (struct smb2_symlink_err_rsp *)err_buf->ErrorData;
3121 if (le32_to_cpu(symlink->SymLinkErrorTag) != SYMLINK_ERROR_TAG ||
3122 le32_to_cpu(symlink->ReparseTag) != IO_REPARSE_TAG_SYMLINK) {
3123 rc = -EINVAL;
Ronnie Sahlberg9d874c32018-06-08 13:21:18 +10003124 goto querty_exit;
Pavel Shilovsky78932422016-07-24 10:37:38 +03003125 }
3126
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04003127 /* open must fail on symlink - reset rc */
3128 rc = 0;
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04003129 sub_len = le16_to_cpu(symlink->SubstituteNameLength);
3130 sub_offset = le16_to_cpu(symlink->SubstituteNameOffset);
Pavel Shilovsky78932422016-07-24 10:37:38 +03003131 print_len = le16_to_cpu(symlink->PrintNameLength);
3132 print_offset = le16_to_cpu(symlink->PrintNameOffset);
3133
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10003134 if (err_iov.iov_len < SMB2_SYMLINK_STRUCT_SIZE + sub_offset + sub_len) {
Ronnie Sahlbergdf070af2019-07-09 18:41:11 +10003135 rc = -EINVAL;
Ronnie Sahlberg9d874c32018-06-08 13:21:18 +10003136 goto querty_exit;
Pavel Shilovsky78932422016-07-24 10:37:38 +03003137 }
3138
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10003139 if (err_iov.iov_len <
3140 SMB2_SYMLINK_STRUCT_SIZE + print_offset + print_len) {
Ronnie Sahlbergdf070af2019-07-09 18:41:11 +10003141 rc = -EINVAL;
Ronnie Sahlberg9d874c32018-06-08 13:21:18 +10003142 goto querty_exit;
Pavel Shilovsky78932422016-07-24 10:37:38 +03003143 }
3144
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04003145 *target_path = cifs_strndup_from_utf16(
3146 (char *)symlink->PathBuffer + sub_offset,
3147 sub_len, true, cifs_sb->local_nls);
3148 if (!(*target_path)) {
Ronnie Sahlberg9d874c32018-06-08 13:21:18 +10003149 rc = -ENOMEM;
3150 goto querty_exit;
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04003151 }
3152 convert_delimiter(*target_path, '/');
3153 cifs_dbg(FYI, "%s: target path: %s\n", __func__, *target_path);
Ronnie Sahlberg9d874c32018-06-08 13:21:18 +10003154
3155 querty_exit:
Ronnie Sahlbergebaf5462019-04-10 08:44:46 +10003156 cifs_dbg(FYI, "query symlink rc %d\n", rc);
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04003157 kfree(utf16_path);
Ronnie Sahlbergebaf5462019-04-10 08:44:46 +10003158 SMB2_open_free(&rqst[0]);
3159 SMB2_ioctl_free(&rqst[1]);
3160 SMB2_close_free(&rqst[2]);
3161 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
3162 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
3163 free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04003164 return rc;
3165}
3166
Steve French2e4564b2020-10-22 22:03:14 -05003167int
3168smb2_query_reparse_tag(const unsigned int xid, struct cifs_tcon *tcon,
3169 struct cifs_sb_info *cifs_sb, const char *full_path,
3170 __u32 *tag)
3171{
3172 int rc;
3173 __le16 *utf16_path = NULL;
3174 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
3175 struct cifs_open_parms oparms;
3176 struct cifs_fid fid;
Steve French2e4564b2020-10-22 22:03:14 -05003177 struct TCP_Server_Info *server = cifs_pick_channel(tcon->ses);
Paulo Alcantara04ad69c2021-03-08 12:00:50 -03003178 int flags = CIFS_CP_CREATE_CLOSE_OP;
Steve French2e4564b2020-10-22 22:03:14 -05003179 struct smb_rqst rqst[3];
3180 int resp_buftype[3];
3181 struct kvec rsp_iov[3];
3182 struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
3183 struct kvec io_iov[SMB2_IOCTL_IOV_SIZE];
3184 struct kvec close_iov[1];
Steve French2e4564b2020-10-22 22:03:14 -05003185 struct smb2_ioctl_rsp *ioctl_rsp;
3186 struct reparse_data_buffer *reparse_buf;
3187 u32 plen;
3188
3189 cifs_dbg(FYI, "%s: path: %s\n", __func__, full_path);
3190
3191 if (smb3_encryption_required(tcon))
3192 flags |= CIFS_TRANSFORM_REQ;
3193
3194 memset(rqst, 0, sizeof(rqst));
3195 resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
3196 memset(rsp_iov, 0, sizeof(rsp_iov));
3197
3198 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
3199 if (!utf16_path)
3200 return -ENOMEM;
3201
3202 /*
3203 * setup smb2open - TODO add optimization to call cifs_get_readable_path
3204 * to see if there is a handle already open that we can use
3205 */
3206 memset(&open_iov, 0, sizeof(open_iov));
3207 rqst[0].rq_iov = open_iov;
3208 rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
3209
3210 memset(&oparms, 0, sizeof(oparms));
3211 oparms.tcon = tcon;
3212 oparms.desired_access = FILE_READ_ATTRIBUTES;
3213 oparms.disposition = FILE_OPEN;
3214 oparms.create_options = cifs_create_options(cifs_sb, OPEN_REPARSE_POINT);
3215 oparms.fid = &fid;
3216 oparms.reconnect = false;
3217
3218 rc = SMB2_open_init(tcon, server,
3219 &rqst[0], &oplock, &oparms, utf16_path);
3220 if (rc)
3221 goto query_rp_exit;
3222 smb2_set_next_command(tcon, &rqst[0]);
3223
3224
3225 /* IOCTL */
3226 memset(&io_iov, 0, sizeof(io_iov));
3227 rqst[1].rq_iov = io_iov;
3228 rqst[1].rq_nvec = SMB2_IOCTL_IOV_SIZE;
3229
3230 rc = SMB2_ioctl_init(tcon, server,
Namjae Jeon79631782020-12-03 12:31:36 +09003231 &rqst[1], COMPOUND_FID,
3232 COMPOUND_FID, FSCTL_GET_REPARSE_POINT,
Steve French2e4564b2020-10-22 22:03:14 -05003233 true /* is_fctl */, NULL, 0,
3234 CIFSMaxBufSize -
3235 MAX_SMB2_CREATE_RESPONSE_SIZE -
3236 MAX_SMB2_CLOSE_RESPONSE_SIZE);
3237 if (rc)
3238 goto query_rp_exit;
3239
3240 smb2_set_next_command(tcon, &rqst[1]);
3241 smb2_set_related(&rqst[1]);
3242
3243
3244 /* Close */
3245 memset(&close_iov, 0, sizeof(close_iov));
3246 rqst[2].rq_iov = close_iov;
3247 rqst[2].rq_nvec = 1;
3248
3249 rc = SMB2_close_init(tcon, server,
3250 &rqst[2], COMPOUND_FID, COMPOUND_FID, false);
3251 if (rc)
3252 goto query_rp_exit;
3253
3254 smb2_set_related(&rqst[2]);
3255
3256 rc = compound_send_recv(xid, tcon->ses, server,
3257 flags, 3, rqst,
3258 resp_buftype, rsp_iov);
3259
Steve French2e4564b2020-10-22 22:03:14 -05003260 ioctl_rsp = rsp_iov[1].iov_base;
3261
3262 /*
3263 * Open was successful and we got an ioctl response.
3264 */
3265 if (rc == 0) {
3266 /* See MS-FSCC 2.3.23 */
3267
3268 reparse_buf = (struct reparse_data_buffer *)
3269 ((char *)ioctl_rsp +
3270 le32_to_cpu(ioctl_rsp->OutputOffset));
3271 plen = le32_to_cpu(ioctl_rsp->OutputCount);
3272
3273 if (plen + le32_to_cpu(ioctl_rsp->OutputOffset) >
3274 rsp_iov[1].iov_len) {
3275 cifs_tcon_dbg(FYI, "srv returned invalid ioctl len: %d\n",
3276 plen);
3277 rc = -EIO;
3278 goto query_rp_exit;
3279 }
3280 *tag = le32_to_cpu(reparse_buf->ReparseTag);
3281 }
3282
3283 query_rp_exit:
3284 kfree(utf16_path);
3285 SMB2_open_free(&rqst[0]);
3286 SMB2_ioctl_free(&rqst[1]);
3287 SMB2_close_free(&rqst[2]);
3288 free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
3289 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
3290 free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
3291 return rc;
3292}
3293
Shirish Pargaonkar2f1afe22017-06-22 22:52:05 -05003294static struct cifs_ntsd *
3295get_smb2_acl_by_fid(struct cifs_sb_info *cifs_sb,
Boris Protopopov3970acf2020-12-18 11:30:12 -06003296 const struct cifs_fid *cifsfid, u32 *pacllen, u32 info)
Shirish Pargaonkar2f1afe22017-06-22 22:52:05 -05003297{
3298 struct cifs_ntsd *pntsd = NULL;
3299 unsigned int xid;
3300 int rc = -EOPNOTSUPP;
3301 struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
3302
3303 if (IS_ERR(tlink))
3304 return ERR_CAST(tlink);
3305
3306 xid = get_xid();
3307 cifs_dbg(FYI, "trying to get acl\n");
3308
3309 rc = SMB2_query_acl(xid, tlink_tcon(tlink), cifsfid->persistent_fid,
Boris Protopopov3970acf2020-12-18 11:30:12 -06003310 cifsfid->volatile_fid, (void **)&pntsd, pacllen,
3311 info);
Shirish Pargaonkar2f1afe22017-06-22 22:52:05 -05003312 free_xid(xid);
3313
3314 cifs_put_tlink(tlink);
3315
3316 cifs_dbg(FYI, "%s: rc = %d ACL len %d\n", __func__, rc, *pacllen);
3317 if (rc)
3318 return ERR_PTR(rc);
3319 return pntsd;
3320
3321}
3322
3323static struct cifs_ntsd *
3324get_smb2_acl_by_path(struct cifs_sb_info *cifs_sb,
Boris Protopopov3970acf2020-12-18 11:30:12 -06003325 const char *path, u32 *pacllen, u32 info)
Shirish Pargaonkar2f1afe22017-06-22 22:52:05 -05003326{
3327 struct cifs_ntsd *pntsd = NULL;
3328 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
3329 unsigned int xid;
3330 int rc;
3331 struct cifs_tcon *tcon;
3332 struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
3333 struct cifs_fid fid;
3334 struct cifs_open_parms oparms;
3335 __le16 *utf16_path;
3336
3337 cifs_dbg(FYI, "get smb3 acl for path %s\n", path);
3338 if (IS_ERR(tlink))
3339 return ERR_CAST(tlink);
3340
3341 tcon = tlink_tcon(tlink);
3342 xid = get_xid();
3343
Shirish Pargaonkar2f1afe22017-06-22 22:52:05 -05003344 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
Steve Frenchcfe89092018-05-19 02:04:55 -05003345 if (!utf16_path) {
3346 rc = -ENOMEM;
3347 free_xid(xid);
3348 return ERR_PTR(rc);
3349 }
Shirish Pargaonkar2f1afe22017-06-22 22:52:05 -05003350
3351 oparms.tcon = tcon;
3352 oparms.desired_access = READ_CONTROL;
3353 oparms.disposition = FILE_OPEN;
Steve French3c3317d2020-10-21 13:12:08 -05003354 /*
3355 * When querying an ACL, even if the file is a symlink we want to open
3356 * the source not the target, and so the protocol requires that the
3357 * client specify this flag when opening a reparse point
3358 */
3359 oparms.create_options = cifs_create_options(cifs_sb, 0) | OPEN_REPARSE_POINT;
Shirish Pargaonkar2f1afe22017-06-22 22:52:05 -05003360 oparms.fid = &fid;
3361 oparms.reconnect = false;
3362
Boris Protopopov3970acf2020-12-18 11:30:12 -06003363 if (info & SACL_SECINFO)
3364 oparms.desired_access |= SYSTEM_SECURITY;
3365
Aurelien Aptel69dda302020-03-02 17:53:22 +01003366 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, NULL,
3367 NULL);
Shirish Pargaonkar2f1afe22017-06-22 22:52:05 -05003368 kfree(utf16_path);
3369 if (!rc) {
3370 rc = SMB2_query_acl(xid, tlink_tcon(tlink), fid.persistent_fid,
Boris Protopopov3970acf2020-12-18 11:30:12 -06003371 fid.volatile_fid, (void **)&pntsd, pacllen,
3372 info);
Shirish Pargaonkar2f1afe22017-06-22 22:52:05 -05003373 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
3374 }
3375
3376 cifs_put_tlink(tlink);
3377 free_xid(xid);
3378
3379 cifs_dbg(FYI, "%s: rc = %d ACL len %d\n", __func__, rc, *pacllen);
3380 if (rc)
3381 return ERR_PTR(rc);
3382 return pntsd;
3383}
3384
Shirish Pargaonkar366ed842017-06-28 22:37:32 -05003385static int
3386set_smb2_acl(struct cifs_ntsd *pnntsd, __u32 acllen,
3387 struct inode *inode, const char *path, int aclflag)
3388{
3389 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
3390 unsigned int xid;
3391 int rc, access_flags = 0;
3392 struct cifs_tcon *tcon;
3393 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
3394 struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
3395 struct cifs_fid fid;
3396 struct cifs_open_parms oparms;
3397 __le16 *utf16_path;
3398
3399 cifs_dbg(FYI, "set smb3 acl for path %s\n", path);
3400 if (IS_ERR(tlink))
3401 return PTR_ERR(tlink);
3402
3403 tcon = tlink_tcon(tlink);
3404 xid = get_xid();
3405
Boris Protopopov3970acf2020-12-18 11:30:12 -06003406 if (aclflag & CIFS_ACL_OWNER || aclflag & CIFS_ACL_GROUP)
3407 access_flags |= WRITE_OWNER;
3408 if (aclflag & CIFS_ACL_SACL)
3409 access_flags |= SYSTEM_SECURITY;
3410 if (aclflag & CIFS_ACL_DACL)
3411 access_flags |= WRITE_DAC;
Shirish Pargaonkar366ed842017-06-28 22:37:32 -05003412
3413 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
Steve Frenchcfe89092018-05-19 02:04:55 -05003414 if (!utf16_path) {
3415 rc = -ENOMEM;
3416 free_xid(xid);
3417 return rc;
3418 }
Shirish Pargaonkar366ed842017-06-28 22:37:32 -05003419
3420 oparms.tcon = tcon;
3421 oparms.desired_access = access_flags;
Amir Goldstein0f060932020-02-03 21:46:43 +02003422 oparms.create_options = cifs_create_options(cifs_sb, 0);
Shirish Pargaonkar366ed842017-06-28 22:37:32 -05003423 oparms.disposition = FILE_OPEN;
3424 oparms.path = path;
3425 oparms.fid = &fid;
3426 oparms.reconnect = false;
3427
Aurelien Aptel69dda302020-03-02 17:53:22 +01003428 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL,
3429 NULL, NULL);
Shirish Pargaonkar366ed842017-06-28 22:37:32 -05003430 kfree(utf16_path);
3431 if (!rc) {
3432 rc = SMB2_set_acl(xid, tlink_tcon(tlink), fid.persistent_fid,
3433 fid.volatile_fid, pnntsd, acllen, aclflag);
3434 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
3435 }
3436
3437 cifs_put_tlink(tlink);
3438 free_xid(xid);
3439 return rc;
3440}
Shirish Pargaonkar366ed842017-06-28 22:37:32 -05003441
Shirish Pargaonkar2f1afe22017-06-22 22:52:05 -05003442/* Retrieve an ACL from the server */
3443static struct cifs_ntsd *
3444get_smb2_acl(struct cifs_sb_info *cifs_sb,
Boris Protopopov3970acf2020-12-18 11:30:12 -06003445 struct inode *inode, const char *path,
3446 u32 *pacllen, u32 info)
Shirish Pargaonkar2f1afe22017-06-22 22:52:05 -05003447{
3448 struct cifs_ntsd *pntsd = NULL;
3449 struct cifsFileInfo *open_file = NULL;
3450
Boris Protopopov9541b812020-12-17 20:58:08 +00003451 if (inode && !(info & SACL_SECINFO))
Shirish Pargaonkar2f1afe22017-06-22 22:52:05 -05003452 open_file = find_readable_file(CIFS_I(inode), true);
Boris Protopopov9541b812020-12-17 20:58:08 +00003453 if (!open_file || (info & SACL_SECINFO))
Boris Protopopov3970acf2020-12-18 11:30:12 -06003454 return get_smb2_acl_by_path(cifs_sb, path, pacllen, info);
Shirish Pargaonkar2f1afe22017-06-22 22:52:05 -05003455
Boris Protopopov3970acf2020-12-18 11:30:12 -06003456 pntsd = get_smb2_acl_by_fid(cifs_sb, &open_file->fid, pacllen, info);
Shirish Pargaonkar2f1afe22017-06-22 22:52:05 -05003457 cifsFileInfo_put(open_file);
3458 return pntsd;
3459}
Shirish Pargaonkar2f1afe22017-06-22 22:52:05 -05003460
Steve French30175622014-08-17 18:16:40 -05003461static long smb3_zero_range(struct file *file, struct cifs_tcon *tcon,
3462 loff_t offset, loff_t len, bool keep_size)
3463{
Ronnie Sahlberg72c419d2019-03-13 14:37:49 +10003464 struct cifs_ses *ses = tcon->ses;
Steve French30175622014-08-17 18:16:40 -05003465 struct inode *inode;
3466 struct cifsInodeInfo *cifsi;
3467 struct cifsFileInfo *cfile = file->private_data;
3468 struct file_zero_data_information fsctl_buf;
3469 long rc;
3470 unsigned int xid;
Ronnie Sahlberg72c419d2019-03-13 14:37:49 +10003471 __le64 eof;
Steve French30175622014-08-17 18:16:40 -05003472
3473 xid = get_xid();
3474
David Howells2b0143b2015-03-17 22:25:59 +00003475 inode = d_inode(cfile->dentry);
Steve French30175622014-08-17 18:16:40 -05003476 cifsi = CIFS_I(inode);
3477
Christoph Probsta205d502019-05-08 21:36:25 +02003478 trace_smb3_zero_enter(xid, cfile->fid.persistent_fid, tcon->tid,
Steve French779ede02019-03-13 01:41:49 -05003479 ses->Suid, offset, len);
3480
Zhang Xiaoxu6b690402020-06-23 07:31:54 -04003481 /*
3482 * We zero the range through ioctl, so we need remove the page caches
3483 * first, otherwise the data may be inconsistent with the server.
3484 */
3485 truncate_pagecache_range(inode, offset, offset + len - 1);
Steve French779ede02019-03-13 01:41:49 -05003486
Steve French30175622014-08-17 18:16:40 -05003487 /* if file not oplocked can't be sure whether asking to extend size */
3488 if (!CIFS_CACHE_READ(cifsi))
Steve Frenchcfe89092018-05-19 02:04:55 -05003489 if (keep_size == false) {
3490 rc = -EOPNOTSUPP;
Steve French779ede02019-03-13 01:41:49 -05003491 trace_smb3_zero_err(xid, cfile->fid.persistent_fid,
3492 tcon->tid, ses->Suid, offset, len, rc);
Steve Frenchcfe89092018-05-19 02:04:55 -05003493 free_xid(xid);
3494 return rc;
3495 }
Steve French30175622014-08-17 18:16:40 -05003496
Steve Frenchd1c35af2019-05-09 00:09:37 -05003497 cifs_dbg(FYI, "Offset %lld len %lld\n", offset, len);
Steve French30175622014-08-17 18:16:40 -05003498
3499 fsctl_buf.FileOffset = cpu_to_le64(offset);
3500 fsctl_buf.BeyondFinalZero = cpu_to_le64(offset + len);
3501
Ronnie Sahlbergc4250142019-05-02 15:52:57 +10003502 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
3503 cfile->fid.volatile_fid, FSCTL_SET_ZERO_DATA, true,
3504 (char *)&fsctl_buf,
3505 sizeof(struct file_zero_data_information),
3506 0, NULL, NULL);
Ronnie Sahlberg72c419d2019-03-13 14:37:49 +10003507 if (rc)
3508 goto zero_range_exit;
3509
3510 /*
3511 * do we also need to change the size of the file?
3512 */
3513 if (keep_size == false && i_size_read(inode) < offset + len) {
Ronnie Sahlberg72c419d2019-03-13 14:37:49 +10003514 eof = cpu_to_le64(offset + len);
Ronnie Sahlbergc4250142019-05-02 15:52:57 +10003515 rc = SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
3516 cfile->fid.volatile_fid, cfile->pid, &eof);
Ronnie Sahlberg72c419d2019-03-13 14:37:49 +10003517 }
3518
Ronnie Sahlberg72c419d2019-03-13 14:37:49 +10003519 zero_range_exit:
Steve French30175622014-08-17 18:16:40 -05003520 free_xid(xid);
Steve French779ede02019-03-13 01:41:49 -05003521 if (rc)
3522 trace_smb3_zero_err(xid, cfile->fid.persistent_fid, tcon->tid,
3523 ses->Suid, offset, len, rc);
3524 else
3525 trace_smb3_zero_done(xid, cfile->fid.persistent_fid, tcon->tid,
3526 ses->Suid, offset, len);
Steve French30175622014-08-17 18:16:40 -05003527 return rc;
3528}
3529
Steve French31742c52014-08-17 08:38:47 -05003530static long smb3_punch_hole(struct file *file, struct cifs_tcon *tcon,
3531 loff_t offset, loff_t len)
3532{
3533 struct inode *inode;
Steve French31742c52014-08-17 08:38:47 -05003534 struct cifsFileInfo *cfile = file->private_data;
3535 struct file_zero_data_information fsctl_buf;
3536 long rc;
3537 unsigned int xid;
3538 __u8 set_sparse = 1;
3539
3540 xid = get_xid();
3541
David Howells2b0143b2015-03-17 22:25:59 +00003542 inode = d_inode(cfile->dentry);
Steve French31742c52014-08-17 08:38:47 -05003543
3544 /* Need to make file sparse, if not already, before freeing range. */
3545 /* Consider adding equivalent for compressed since it could also work */
Steve Frenchcfe89092018-05-19 02:04:55 -05003546 if (!smb2_set_sparse(xid, tcon, cfile, inode, set_sparse)) {
3547 rc = -EOPNOTSUPP;
3548 free_xid(xid);
3549 return rc;
3550 }
Steve French31742c52014-08-17 08:38:47 -05003551
Zhang Xiaoxuacc91c22020-06-23 07:31:53 -04003552 /*
3553 * We implement the punch hole through ioctl, so we need remove the page
3554 * caches first, otherwise the data may be inconsistent with the server.
3555 */
3556 truncate_pagecache_range(inode, offset, offset + len - 1);
3557
Christoph Probsta205d502019-05-08 21:36:25 +02003558 cifs_dbg(FYI, "Offset %lld len %lld\n", offset, len);
Steve French31742c52014-08-17 08:38:47 -05003559
3560 fsctl_buf.FileOffset = cpu_to_le64(offset);
3561 fsctl_buf.BeyondFinalZero = cpu_to_le64(offset + len);
3562
3563 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
3564 cfile->fid.volatile_fid, FSCTL_SET_ZERO_DATA,
Aurelien Aptel63a83b82018-01-24 13:46:11 +01003565 true /* is_fctl */, (char *)&fsctl_buf,
Steve French153322f2019-03-28 22:32:49 -05003566 sizeof(struct file_zero_data_information),
3567 CIFSMaxBufSize, NULL, NULL);
Steve French31742c52014-08-17 08:38:47 -05003568 free_xid(xid);
3569 return rc;
3570}
3571
Steve French9ccf3212014-10-18 17:01:15 -05003572static long smb3_simple_falloc(struct file *file, struct cifs_tcon *tcon,
3573 loff_t off, loff_t len, bool keep_size)
3574{
3575 struct inode *inode;
3576 struct cifsInodeInfo *cifsi;
3577 struct cifsFileInfo *cfile = file->private_data;
3578 long rc = -EOPNOTSUPP;
3579 unsigned int xid;
Ronnie Sahlbergf1699472019-03-15 00:08:48 +10003580 __le64 eof;
Steve French9ccf3212014-10-18 17:01:15 -05003581
3582 xid = get_xid();
3583
David Howells2b0143b2015-03-17 22:25:59 +00003584 inode = d_inode(cfile->dentry);
Steve French9ccf3212014-10-18 17:01:15 -05003585 cifsi = CIFS_I(inode);
3586
Steve French779ede02019-03-13 01:41:49 -05003587 trace_smb3_falloc_enter(xid, cfile->fid.persistent_fid, tcon->tid,
3588 tcon->ses->Suid, off, len);
Steve French9ccf3212014-10-18 17:01:15 -05003589 /* if file not oplocked can't be sure whether asking to extend size */
3590 if (!CIFS_CACHE_READ(cifsi))
Steve Frenchcfe89092018-05-19 02:04:55 -05003591 if (keep_size == false) {
Steve French779ede02019-03-13 01:41:49 -05003592 trace_smb3_falloc_err(xid, cfile->fid.persistent_fid,
3593 tcon->tid, tcon->ses->Suid, off, len, rc);
Steve Frenchcfe89092018-05-19 02:04:55 -05003594 free_xid(xid);
3595 return rc;
3596 }
Steve French9ccf3212014-10-18 17:01:15 -05003597
3598 /*
Ronnie Sahlberg8bd0d702020-01-17 11:45:02 +10003599 * Extending the file
3600 */
3601 if ((keep_size == false) && i_size_read(inode) < off + len) {
Murphy Zhouef4a6322020-03-18 20:43:38 +08003602 rc = inode_newsize_ok(inode, off + len);
3603 if (rc)
3604 goto out;
3605
Ronnie Sahlberg8bd0d702020-01-17 11:45:02 +10003606 if ((cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) == 0)
3607 smb2_set_sparse(xid, tcon, cfile, inode, false);
3608
3609 eof = cpu_to_le64(off + len);
3610 rc = SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
3611 cfile->fid.volatile_fid, cfile->pid, &eof);
3612 if (rc == 0) {
3613 cifsi->server_eof = off + len;
3614 cifs_setsize(inode, off + len);
3615 cifs_truncate_page(inode->i_mapping, inode->i_size);
3616 truncate_setsize(inode, off + len);
3617 }
3618 goto out;
3619 }
3620
3621 /*
Steve French9ccf3212014-10-18 17:01:15 -05003622 * Files are non-sparse by default so falloc may be a no-op
Ronnie Sahlberg8bd0d702020-01-17 11:45:02 +10003623 * Must check if file sparse. If not sparse, and since we are not
3624 * extending then no need to do anything since file already allocated
Steve French9ccf3212014-10-18 17:01:15 -05003625 */
3626 if ((cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) == 0) {
Ronnie Sahlberg8bd0d702020-01-17 11:45:02 +10003627 rc = 0;
3628 goto out;
Steve French9ccf3212014-10-18 17:01:15 -05003629 }
3630
3631 if ((keep_size == true) || (i_size_read(inode) >= off + len)) {
3632 /*
3633 * Check if falloc starts within first few pages of file
3634 * and ends within a few pages of the end of file to
3635 * ensure that most of file is being forced to be
3636 * fallocated now. If so then setting whole file sparse
3637 * ie potentially making a few extra pages at the beginning
3638 * or end of the file non-sparse via set_sparse is harmless.
3639 */
Steve Frenchcfe89092018-05-19 02:04:55 -05003640 if ((off > 8192) || (off + len + 8192 < i_size_read(inode))) {
3641 rc = -EOPNOTSUPP;
Ronnie Sahlberg8bd0d702020-01-17 11:45:02 +10003642 goto out;
Ronnie Sahlbergf1699472019-03-15 00:08:48 +10003643 }
Steve French9ccf3212014-10-18 17:01:15 -05003644 }
Steve French9ccf3212014-10-18 17:01:15 -05003645
Ronnie Sahlberg8bd0d702020-01-17 11:45:02 +10003646 smb2_set_sparse(xid, tcon, cfile, inode, false);
3647 rc = 0;
3648
3649out:
Steve French779ede02019-03-13 01:41:49 -05003650 if (rc)
3651 trace_smb3_falloc_err(xid, cfile->fid.persistent_fid, tcon->tid,
3652 tcon->ses->Suid, off, len, rc);
3653 else
3654 trace_smb3_falloc_done(xid, cfile->fid.persistent_fid, tcon->tid,
3655 tcon->ses->Suid, off, len);
Steve French9ccf3212014-10-18 17:01:15 -05003656
3657 free_xid(xid);
3658 return rc;
3659}
3660
Ronnie Sahlbergdece44e2019-05-15 07:17:02 +10003661static loff_t smb3_llseek(struct file *file, struct cifs_tcon *tcon, loff_t offset, int whence)
3662{
3663 struct cifsFileInfo *wrcfile, *cfile = file->private_data;
3664 struct cifsInodeInfo *cifsi;
3665 struct inode *inode;
3666 int rc = 0;
3667 struct file_allocated_range_buffer in_data, *out_data = NULL;
3668 u32 out_data_len;
3669 unsigned int xid;
3670
3671 if (whence != SEEK_HOLE && whence != SEEK_DATA)
3672 return generic_file_llseek(file, offset, whence);
3673
3674 inode = d_inode(cfile->dentry);
3675 cifsi = CIFS_I(inode);
3676
3677 if (offset < 0 || offset >= i_size_read(inode))
3678 return -ENXIO;
3679
3680 xid = get_xid();
3681 /*
3682 * We need to be sure that all dirty pages are written as they
3683 * might fill holes on the server.
3684 * Note that we also MUST flush any written pages since at least
3685 * some servers (Windows2016) will not reflect recent writes in
3686 * QUERY_ALLOCATED_RANGES until SMB2_flush is called.
3687 */
Aurelien Aptel86f740f2020-02-21 11:19:06 +01003688 wrcfile = find_writable_file(cifsi, FIND_WR_ANY);
Ronnie Sahlbergdece44e2019-05-15 07:17:02 +10003689 if (wrcfile) {
3690 filemap_write_and_wait(inode->i_mapping);
3691 smb2_flush_file(xid, tcon, &wrcfile->fid);
3692 cifsFileInfo_put(wrcfile);
3693 }
3694
3695 if (!(cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE)) {
3696 if (whence == SEEK_HOLE)
3697 offset = i_size_read(inode);
3698 goto lseek_exit;
3699 }
3700
3701 in_data.file_offset = cpu_to_le64(offset);
3702 in_data.length = cpu_to_le64(i_size_read(inode));
3703
3704 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
3705 cfile->fid.volatile_fid,
3706 FSCTL_QUERY_ALLOCATED_RANGES, true,
3707 (char *)&in_data, sizeof(in_data),
3708 sizeof(struct file_allocated_range_buffer),
3709 (char **)&out_data, &out_data_len);
3710 if (rc == -E2BIG)
3711 rc = 0;
3712 if (rc)
3713 goto lseek_exit;
3714
3715 if (whence == SEEK_HOLE && out_data_len == 0)
3716 goto lseek_exit;
3717
3718 if (whence == SEEK_DATA && out_data_len == 0) {
3719 rc = -ENXIO;
3720 goto lseek_exit;
3721 }
3722
3723 if (out_data_len < sizeof(struct file_allocated_range_buffer)) {
3724 rc = -EINVAL;
3725 goto lseek_exit;
3726 }
3727 if (whence == SEEK_DATA) {
3728 offset = le64_to_cpu(out_data->file_offset);
3729 goto lseek_exit;
3730 }
3731 if (offset < le64_to_cpu(out_data->file_offset))
3732 goto lseek_exit;
3733
3734 offset = le64_to_cpu(out_data->file_offset) + le64_to_cpu(out_data->length);
3735
3736 lseek_exit:
3737 free_xid(xid);
3738 kfree(out_data);
3739 if (!rc)
3740 return vfs_setpos(file, offset, inode->i_sb->s_maxbytes);
3741 else
3742 return rc;
3743}
3744
Ronnie Sahlberg2f3ebab2019-04-25 16:45:29 +10003745static int smb3_fiemap(struct cifs_tcon *tcon,
3746 struct cifsFileInfo *cfile,
3747 struct fiemap_extent_info *fei, u64 start, u64 len)
3748{
3749 unsigned int xid;
3750 struct file_allocated_range_buffer in_data, *out_data;
3751 u32 out_data_len;
3752 int i, num, rc, flags, last_blob;
3753 u64 next;
3754
Christoph Hellwig45dd0522020-05-23 09:30:14 +02003755 rc = fiemap_prep(d_inode(cfile->dentry), fei, start, &len, 0);
Christoph Hellwigcddf8a22020-05-23 09:30:13 +02003756 if (rc)
3757 return rc;
Ronnie Sahlberg2f3ebab2019-04-25 16:45:29 +10003758
3759 xid = get_xid();
3760 again:
3761 in_data.file_offset = cpu_to_le64(start);
3762 in_data.length = cpu_to_le64(len);
3763
3764 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
3765 cfile->fid.volatile_fid,
3766 FSCTL_QUERY_ALLOCATED_RANGES, true,
3767 (char *)&in_data, sizeof(in_data),
3768 1024 * sizeof(struct file_allocated_range_buffer),
3769 (char **)&out_data, &out_data_len);
3770 if (rc == -E2BIG) {
3771 last_blob = 0;
3772 rc = 0;
3773 } else
3774 last_blob = 1;
3775 if (rc)
3776 goto out;
3777
Murphy Zhou979a2662020-03-14 11:38:31 +08003778 if (out_data_len && out_data_len < sizeof(struct file_allocated_range_buffer)) {
Ronnie Sahlberg2f3ebab2019-04-25 16:45:29 +10003779 rc = -EINVAL;
3780 goto out;
3781 }
3782 if (out_data_len % sizeof(struct file_allocated_range_buffer)) {
3783 rc = -EINVAL;
3784 goto out;
3785 }
3786
3787 num = out_data_len / sizeof(struct file_allocated_range_buffer);
3788 for (i = 0; i < num; i++) {
3789 flags = 0;
3790 if (i == num - 1 && last_blob)
3791 flags |= FIEMAP_EXTENT_LAST;
3792
3793 rc = fiemap_fill_next_extent(fei,
3794 le64_to_cpu(out_data[i].file_offset),
3795 le64_to_cpu(out_data[i].file_offset),
3796 le64_to_cpu(out_data[i].length),
3797 flags);
3798 if (rc < 0)
3799 goto out;
3800 if (rc == 1) {
3801 rc = 0;
3802 goto out;
3803 }
3804 }
3805
3806 if (!last_blob) {
3807 next = le64_to_cpu(out_data[num - 1].file_offset) +
3808 le64_to_cpu(out_data[num - 1].length);
3809 len = len - (next - start);
3810 start = next;
3811 goto again;
3812 }
3813
3814 out:
3815 free_xid(xid);
3816 kfree(out_data);
3817 return rc;
3818}
Steve French9ccf3212014-10-18 17:01:15 -05003819
Steve French31742c52014-08-17 08:38:47 -05003820static long smb3_fallocate(struct file *file, struct cifs_tcon *tcon, int mode,
3821 loff_t off, loff_t len)
3822{
3823 /* KEEP_SIZE already checked for by do_fallocate */
3824 if (mode & FALLOC_FL_PUNCH_HOLE)
3825 return smb3_punch_hole(file, tcon, off, len);
Steve French30175622014-08-17 18:16:40 -05003826 else if (mode & FALLOC_FL_ZERO_RANGE) {
3827 if (mode & FALLOC_FL_KEEP_SIZE)
3828 return smb3_zero_range(file, tcon, off, len, true);
3829 return smb3_zero_range(file, tcon, off, len, false);
Steve French9ccf3212014-10-18 17:01:15 -05003830 } else if (mode == FALLOC_FL_KEEP_SIZE)
3831 return smb3_simple_falloc(file, tcon, off, len, true);
3832 else if (mode == 0)
3833 return smb3_simple_falloc(file, tcon, off, len, false);
Steve French31742c52014-08-17 08:38:47 -05003834
3835 return -EOPNOTSUPP;
3836}
3837
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04003838static void
Sachin Prabhuc11f1df2014-03-11 16:11:47 +00003839smb2_downgrade_oplock(struct TCP_Server_Info *server,
Pavel Shilovsky9bd45402019-10-29 16:51:19 -07003840 struct cifsInodeInfo *cinode, __u32 oplock,
3841 unsigned int epoch, bool *purge_cache)
Sachin Prabhuc11f1df2014-03-11 16:11:47 +00003842{
Pavel Shilovsky9bd45402019-10-29 16:51:19 -07003843 server->ops->set_oplock_level(cinode, oplock, 0, NULL);
Sachin Prabhuc11f1df2014-03-11 16:11:47 +00003844}
3845
3846static void
Pavel Shilovsky9bd45402019-10-29 16:51:19 -07003847smb21_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
3848 unsigned int epoch, bool *purge_cache);
3849
3850static void
3851smb3_downgrade_oplock(struct TCP_Server_Info *server,
3852 struct cifsInodeInfo *cinode, __u32 oplock,
3853 unsigned int epoch, bool *purge_cache)
Pavel Shilovsky7b9b9ed2019-02-13 15:43:08 -08003854{
Pavel Shilovsky9bd45402019-10-29 16:51:19 -07003855 unsigned int old_state = cinode->oplock;
3856 unsigned int old_epoch = cinode->epoch;
3857 unsigned int new_state;
3858
3859 if (epoch > old_epoch) {
3860 smb21_set_oplock_level(cinode, oplock, 0, NULL);
3861 cinode->epoch = epoch;
3862 }
3863
3864 new_state = cinode->oplock;
3865 *purge_cache = false;
3866
3867 if ((old_state & CIFS_CACHE_READ_FLG) != 0 &&
3868 (new_state & CIFS_CACHE_READ_FLG) == 0)
3869 *purge_cache = true;
3870 else if (old_state == new_state && (epoch - old_epoch > 1))
3871 *purge_cache = true;
Pavel Shilovsky7b9b9ed2019-02-13 15:43:08 -08003872}
3873
3874static void
Pavel Shilovsky42873b02013-09-05 21:30:16 +04003875smb2_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
3876 unsigned int epoch, bool *purge_cache)
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04003877{
3878 oplock &= 0xFF;
3879 if (oplock == SMB2_OPLOCK_LEVEL_NOCHANGE)
3880 return;
3881 if (oplock == SMB2_OPLOCK_LEVEL_BATCH) {
Pavel Shilovsky42873b02013-09-05 21:30:16 +04003882 cinode->oplock = CIFS_CACHE_RHW_FLG;
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04003883 cifs_dbg(FYI, "Batch Oplock granted on inode %p\n",
3884 &cinode->vfs_inode);
3885 } else if (oplock == SMB2_OPLOCK_LEVEL_EXCLUSIVE) {
Pavel Shilovsky42873b02013-09-05 21:30:16 +04003886 cinode->oplock = CIFS_CACHE_RW_FLG;
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04003887 cifs_dbg(FYI, "Exclusive Oplock granted on inode %p\n",
3888 &cinode->vfs_inode);
3889 } else if (oplock == SMB2_OPLOCK_LEVEL_II) {
3890 cinode->oplock = CIFS_CACHE_READ_FLG;
3891 cifs_dbg(FYI, "Level II Oplock granted on inode %p\n",
3892 &cinode->vfs_inode);
3893 } else
3894 cinode->oplock = 0;
3895}
3896
3897static void
Pavel Shilovsky42873b02013-09-05 21:30:16 +04003898smb21_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
3899 unsigned int epoch, bool *purge_cache)
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04003900{
3901 char message[5] = {0};
Christoph Probst6a54b2e2019-05-07 17:16:40 +02003902 unsigned int new_oplock = 0;
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04003903
3904 oplock &= 0xFF;
3905 if (oplock == SMB2_OPLOCK_LEVEL_NOCHANGE)
3906 return;
3907
Pavel Shilovskya016e272019-09-26 12:31:20 -07003908 /* Check if the server granted an oplock rather than a lease */
3909 if (oplock & SMB2_OPLOCK_LEVEL_EXCLUSIVE)
3910 return smb2_set_oplock_level(cinode, oplock, epoch,
3911 purge_cache);
3912
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04003913 if (oplock & SMB2_LEASE_READ_CACHING_HE) {
Christoph Probst6a54b2e2019-05-07 17:16:40 +02003914 new_oplock |= CIFS_CACHE_READ_FLG;
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04003915 strcat(message, "R");
3916 }
3917 if (oplock & SMB2_LEASE_HANDLE_CACHING_HE) {
Christoph Probst6a54b2e2019-05-07 17:16:40 +02003918 new_oplock |= CIFS_CACHE_HANDLE_FLG;
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04003919 strcat(message, "H");
3920 }
3921 if (oplock & SMB2_LEASE_WRITE_CACHING_HE) {
Christoph Probst6a54b2e2019-05-07 17:16:40 +02003922 new_oplock |= CIFS_CACHE_WRITE_FLG;
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04003923 strcat(message, "W");
3924 }
Christoph Probst6a54b2e2019-05-07 17:16:40 +02003925 if (!new_oplock)
3926 strncpy(message, "None", sizeof(message));
3927
3928 cinode->oplock = new_oplock;
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04003929 cifs_dbg(FYI, "%s Lease granted on inode %p\n", message,
3930 &cinode->vfs_inode);
3931}
3932
Pavel Shilovsky42873b02013-09-05 21:30:16 +04003933static void
3934smb3_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
3935 unsigned int epoch, bool *purge_cache)
3936{
3937 unsigned int old_oplock = cinode->oplock;
3938
3939 smb21_set_oplock_level(cinode, oplock, epoch, purge_cache);
3940
3941 if (purge_cache) {
3942 *purge_cache = false;
3943 if (old_oplock == CIFS_CACHE_READ_FLG) {
3944 if (cinode->oplock == CIFS_CACHE_READ_FLG &&
3945 (epoch - cinode->epoch > 0))
3946 *purge_cache = true;
3947 else if (cinode->oplock == CIFS_CACHE_RH_FLG &&
3948 (epoch - cinode->epoch > 1))
3949 *purge_cache = true;
3950 else if (cinode->oplock == CIFS_CACHE_RHW_FLG &&
3951 (epoch - cinode->epoch > 1))
3952 *purge_cache = true;
3953 else if (cinode->oplock == 0 &&
3954 (epoch - cinode->epoch > 0))
3955 *purge_cache = true;
3956 } else if (old_oplock == CIFS_CACHE_RH_FLG) {
3957 if (cinode->oplock == CIFS_CACHE_RH_FLG &&
3958 (epoch - cinode->epoch > 0))
3959 *purge_cache = true;
3960 else if (cinode->oplock == CIFS_CACHE_RHW_FLG &&
3961 (epoch - cinode->epoch > 1))
3962 *purge_cache = true;
3963 }
3964 cinode->epoch = epoch;
3965 }
3966}
3967
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04003968static bool
3969smb2_is_read_op(__u32 oplock)
3970{
3971 return oplock == SMB2_OPLOCK_LEVEL_II;
3972}
3973
3974static bool
3975smb21_is_read_op(__u32 oplock)
3976{
3977 return (oplock & SMB2_LEASE_READ_CACHING_HE) &&
3978 !(oplock & SMB2_LEASE_WRITE_CACHING_HE);
3979}
3980
Pavel Shilovskyf0473902013-09-04 13:44:05 +04003981static __le32
3982map_oplock_to_lease(u8 oplock)
3983{
3984 if (oplock == SMB2_OPLOCK_LEVEL_EXCLUSIVE)
3985 return SMB2_LEASE_WRITE_CACHING | SMB2_LEASE_READ_CACHING;
3986 else if (oplock == SMB2_OPLOCK_LEVEL_II)
3987 return SMB2_LEASE_READ_CACHING;
3988 else if (oplock == SMB2_OPLOCK_LEVEL_BATCH)
3989 return SMB2_LEASE_HANDLE_CACHING | SMB2_LEASE_READ_CACHING |
3990 SMB2_LEASE_WRITE_CACHING;
3991 return 0;
3992}
3993
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04003994static char *
3995smb2_create_lease_buf(u8 *lease_key, u8 oplock)
3996{
3997 struct create_lease *buf;
3998
3999 buf = kzalloc(sizeof(struct create_lease), GFP_KERNEL);
4000 if (!buf)
4001 return NULL;
4002
Stefano Brivio729c0c92018-07-05 15:10:02 +02004003 memcpy(&buf->lcontext.LeaseKey, lease_key, SMB2_LEASE_KEY_SIZE);
Pavel Shilovskyf0473902013-09-04 13:44:05 +04004004 buf->lcontext.LeaseState = map_oplock_to_lease(oplock);
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04004005
4006 buf->ccontext.DataOffset = cpu_to_le16(offsetof
4007 (struct create_lease, lcontext));
4008 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context));
4009 buf->ccontext.NameOffset = cpu_to_le16(offsetof
4010 (struct create_lease, Name));
4011 buf->ccontext.NameLength = cpu_to_le16(4);
Steve French12197a72014-05-14 05:29:40 -07004012 /* SMB2_CREATE_REQUEST_LEASE is "RqLs" */
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04004013 buf->Name[0] = 'R';
4014 buf->Name[1] = 'q';
4015 buf->Name[2] = 'L';
4016 buf->Name[3] = 's';
4017 return (char *)buf;
4018}
4019
Pavel Shilovskyf0473902013-09-04 13:44:05 +04004020static char *
4021smb3_create_lease_buf(u8 *lease_key, u8 oplock)
4022{
4023 struct create_lease_v2 *buf;
4024
4025 buf = kzalloc(sizeof(struct create_lease_v2), GFP_KERNEL);
4026 if (!buf)
4027 return NULL;
4028
Stefano Brivio729c0c92018-07-05 15:10:02 +02004029 memcpy(&buf->lcontext.LeaseKey, lease_key, SMB2_LEASE_KEY_SIZE);
Pavel Shilovskyf0473902013-09-04 13:44:05 +04004030 buf->lcontext.LeaseState = map_oplock_to_lease(oplock);
4031
4032 buf->ccontext.DataOffset = cpu_to_le16(offsetof
4033 (struct create_lease_v2, lcontext));
4034 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context_v2));
4035 buf->ccontext.NameOffset = cpu_to_le16(offsetof
4036 (struct create_lease_v2, Name));
4037 buf->ccontext.NameLength = cpu_to_le16(4);
Steve French12197a72014-05-14 05:29:40 -07004038 /* SMB2_CREATE_REQUEST_LEASE is "RqLs" */
Pavel Shilovskyf0473902013-09-04 13:44:05 +04004039 buf->Name[0] = 'R';
4040 buf->Name[1] = 'q';
4041 buf->Name[2] = 'L';
4042 buf->Name[3] = 's';
4043 return (char *)buf;
4044}
4045
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04004046static __u8
Ronnie Sahlberg96164ab2018-04-26 08:10:18 -06004047smb2_parse_lease_buf(void *buf, unsigned int *epoch, char *lease_key)
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04004048{
4049 struct create_lease *lc = (struct create_lease *)buf;
4050
Pavel Shilovsky42873b02013-09-05 21:30:16 +04004051 *epoch = 0; /* not used */
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04004052 if (lc->lcontext.LeaseFlags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS)
4053 return SMB2_OPLOCK_LEVEL_NOCHANGE;
4054 return le32_to_cpu(lc->lcontext.LeaseState);
4055}
4056
Pavel Shilovskyf0473902013-09-04 13:44:05 +04004057static __u8
Ronnie Sahlberg96164ab2018-04-26 08:10:18 -06004058smb3_parse_lease_buf(void *buf, unsigned int *epoch, char *lease_key)
Pavel Shilovskyf0473902013-09-04 13:44:05 +04004059{
4060 struct create_lease_v2 *lc = (struct create_lease_v2 *)buf;
4061
Pavel Shilovsky42873b02013-09-05 21:30:16 +04004062 *epoch = le16_to_cpu(lc->lcontext.Epoch);
Pavel Shilovskyf0473902013-09-04 13:44:05 +04004063 if (lc->lcontext.LeaseFlags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS)
4064 return SMB2_OPLOCK_LEVEL_NOCHANGE;
Ronnie Sahlberg96164ab2018-04-26 08:10:18 -06004065 if (lease_key)
Stefano Brivio729c0c92018-07-05 15:10:02 +02004066 memcpy(lease_key, &lc->lcontext.LeaseKey, SMB2_LEASE_KEY_SIZE);
Pavel Shilovskyf0473902013-09-04 13:44:05 +04004067 return le32_to_cpu(lc->lcontext.LeaseState);
4068}
4069
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04004070static unsigned int
4071smb2_wp_retry_size(struct inode *inode)
4072{
Ronnie Sahlberg522aa3b2020-12-14 16:40:17 +10004073 return min_t(unsigned int, CIFS_SB(inode->i_sb)->ctx->wsize,
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04004074 SMB2_MAX_BUFFER_SIZE);
4075}
4076
Pavel Shilovsky52755802014-08-18 20:49:57 +04004077static bool
4078smb2_dir_needs_close(struct cifsFileInfo *cfile)
4079{
4080 return !cfile->invalidHandle;
4081}
4082
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07004083static void
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10004084fill_transform_hdr(struct smb2_transform_hdr *tr_hdr, unsigned int orig_len,
Steve French2b2f7542019-06-07 15:16:10 -05004085 struct smb_rqst *old_rq, __le16 cipher_type)
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07004086{
4087 struct smb2_sync_hdr *shdr =
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10004088 (struct smb2_sync_hdr *)old_rq->rq_iov[0].iov_base;
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07004089
4090 memset(tr_hdr, 0, sizeof(struct smb2_transform_hdr));
4091 tr_hdr->ProtocolId = SMB2_TRANSFORM_PROTO_NUM;
4092 tr_hdr->OriginalMessageSize = cpu_to_le32(orig_len);
4093 tr_hdr->Flags = cpu_to_le16(0x01);
Steve French63ca5652020-10-15 23:41:40 -05004094 if ((cipher_type == SMB2_ENCRYPTION_AES128_GCM) ||
4095 (cipher_type == SMB2_ENCRYPTION_AES256_GCM))
Steve Frenchfd08f2d2020-10-15 00:25:02 -05004096 get_random_bytes(&tr_hdr->Nonce, SMB3_AES_GCM_NONCE);
Steve French2b2f7542019-06-07 15:16:10 -05004097 else
Steve Frenchfd08f2d2020-10-15 00:25:02 -05004098 get_random_bytes(&tr_hdr->Nonce, SMB3_AES_CCM_NONCE);
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07004099 memcpy(&tr_hdr->SessionId, &shdr->SessionId, 8);
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07004100}
4101
Ronnie Sahlberg262916b2018-02-20 12:45:21 +11004102/* We can not use the normal sg_set_buf() as we will sometimes pass a
4103 * stack object as buf.
4104 */
4105static inline void smb2_sg_set_buf(struct scatterlist *sg, const void *buf,
4106 unsigned int buflen)
4107{
Sebastien Tisserantee9d6612019-08-01 12:06:08 -05004108 void *addr;
4109 /*
4110 * VMAP_STACK (at least) puts stack into the vmalloc address space
4111 */
4112 if (is_vmalloc_addr(buf))
4113 addr = vmalloc_to_page(buf);
4114 else
4115 addr = virt_to_page(buf);
4116 sg_set_page(sg, addr, buflen, offset_in_page(buf));
Ronnie Sahlberg262916b2018-02-20 12:45:21 +11004117}
4118
Ronnie Sahlbergb2c96de2018-08-01 09:26:11 +10004119/* Assumes the first rqst has a transform header as the first iov.
4120 * I.e.
4121 * rqst[0].rq_iov[0] is transform header
4122 * rqst[0].rq_iov[1+] data to be encrypted/decrypted
4123 * rqst[1+].rq_iov[0+] data to be encrypted/decrypted
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10004124 */
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07004125static struct scatterlist *
Ronnie Sahlbergb2c96de2018-08-01 09:26:11 +10004126init_sg(int num_rqst, struct smb_rqst *rqst, u8 *sign)
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07004127{
Ronnie Sahlbergb2c96de2018-08-01 09:26:11 +10004128 unsigned int sg_len;
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07004129 struct scatterlist *sg;
4130 unsigned int i;
4131 unsigned int j;
Ronnie Sahlbergb2c96de2018-08-01 09:26:11 +10004132 unsigned int idx = 0;
4133 int skip;
4134
4135 sg_len = 1;
4136 for (i = 0; i < num_rqst; i++)
4137 sg_len += rqst[i].rq_nvec + rqst[i].rq_npages;
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07004138
4139 sg = kmalloc_array(sg_len, sizeof(struct scatterlist), GFP_KERNEL);
4140 if (!sg)
4141 return NULL;
4142
4143 sg_init_table(sg, sg_len);
Ronnie Sahlbergb2c96de2018-08-01 09:26:11 +10004144 for (i = 0; i < num_rqst; i++) {
4145 for (j = 0; j < rqst[i].rq_nvec; j++) {
4146 /*
4147 * The first rqst has a transform header where the
4148 * first 20 bytes are not part of the encrypted blob
4149 */
4150 skip = (i == 0) && (j == 0) ? 20 : 0;
4151 smb2_sg_set_buf(&sg[idx++],
4152 rqst[i].rq_iov[j].iov_base + skip,
4153 rqst[i].rq_iov[j].iov_len - skip);
Ronnie Sahlberge77fe732018-12-31 13:43:40 +10004154 }
Steve Frenchd5f07fb2018-06-05 17:46:24 -05004155
Ronnie Sahlbergb2c96de2018-08-01 09:26:11 +10004156 for (j = 0; j < rqst[i].rq_npages; j++) {
4157 unsigned int len, offset;
4158
4159 rqst_page_get_length(&rqst[i], j, &len, &offset);
4160 sg_set_page(&sg[idx++], rqst[i].rq_pages[j], len, offset);
4161 }
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07004162 }
Ronnie Sahlbergb2c96de2018-08-01 09:26:11 +10004163 smb2_sg_set_buf(&sg[idx], sign, SMB2_SIGNATURE_SIZE);
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07004164 return sg;
4165}
4166
Pavel Shilovsky61cfac6f2017-02-28 16:05:19 -08004167static int
4168smb2_get_enc_key(struct TCP_Server_Info *server, __u64 ses_id, int enc, u8 *key)
4169{
4170 struct cifs_ses *ses;
4171 u8 *ses_enc_key;
4172
4173 spin_lock(&cifs_tcp_ses_lock);
Aurelien Apteld70e9fa2019-09-20 06:31:10 +02004174 list_for_each_entry(server, &cifs_tcp_ses_list, tcp_ses_list) {
4175 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
4176 if (ses->Suid == ses_id) {
4177 ses_enc_key = enc ? ses->smb3encryptionkey :
4178 ses->smb3decryptionkey;
Shyam Prasad N45a45462021-03-25 12:34:54 +00004179 memcpy(key, ses_enc_key, SMB3_ENC_DEC_KEY_SIZE);
Aurelien Apteld70e9fa2019-09-20 06:31:10 +02004180 spin_unlock(&cifs_tcp_ses_lock);
4181 return 0;
4182 }
4183 }
Pavel Shilovsky61cfac6f2017-02-28 16:05:19 -08004184 }
4185 spin_unlock(&cifs_tcp_ses_lock);
4186
4187 return 1;
4188}
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07004189/*
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10004190 * Encrypt or decrypt @rqst message. @rqst[0] has the following format:
4191 * iov[0] - transform header (associate data),
4192 * iov[1-N] - SMB2 header and pages - data to encrypt.
4193 * On success return encrypted data in iov[1-N] and pages, leave iov[0]
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07004194 * untouched.
4195 */
4196static int
Ronnie Sahlbergb2c96de2018-08-01 09:26:11 +10004197crypt_message(struct TCP_Server_Info *server, int num_rqst,
4198 struct smb_rqst *rqst, int enc)
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07004199{
4200 struct smb2_transform_hdr *tr_hdr =
Ronnie Sahlbergb2c96de2018-08-01 09:26:11 +10004201 (struct smb2_transform_hdr *)rqst[0].rq_iov[0].iov_base;
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10004202 unsigned int assoc_data_len = sizeof(struct smb2_transform_hdr) - 20;
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07004203 int rc = 0;
4204 struct scatterlist *sg;
4205 u8 sign[SMB2_SIGNATURE_SIZE] = {};
Shyam Prasad N45a45462021-03-25 12:34:54 +00004206 u8 key[SMB3_ENC_DEC_KEY_SIZE];
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07004207 struct aead_request *req;
4208 char *iv;
4209 unsigned int iv_len;
Gilad Ben-Yossefa5186b82017-10-18 08:00:46 +01004210 DECLARE_CRYPTO_WAIT(wait);
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07004211 struct crypto_aead *tfm;
4212 unsigned int crypt_len = le32_to_cpu(tr_hdr->OriginalMessageSize);
4213
Pavel Shilovsky61cfac6f2017-02-28 16:05:19 -08004214 rc = smb2_get_enc_key(server, tr_hdr->SessionId, enc, key);
4215 if (rc) {
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +10004216 cifs_server_dbg(VFS, "%s: Could not get %scryption key\n", __func__,
Pavel Shilovsky61cfac6f2017-02-28 16:05:19 -08004217 enc ? "en" : "de");
Shyam Prasad N0bd294b2020-10-15 10:41:31 -07004218 return rc;
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07004219 }
4220
4221 rc = smb3_crypto_aead_allocate(server);
4222 if (rc) {
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +10004223 cifs_server_dbg(VFS, "%s: crypto alloc failed\n", __func__);
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07004224 return rc;
4225 }
4226
4227 tfm = enc ? server->secmech.ccmaesencrypt :
4228 server->secmech.ccmaesdecrypt;
Steve French63ca5652020-10-15 23:41:40 -05004229
Shyam Prasad N45a45462021-03-25 12:34:54 +00004230 if ((server->cipher_type == SMB2_ENCRYPTION_AES256_CCM) ||
4231 (server->cipher_type == SMB2_ENCRYPTION_AES256_GCM))
Steve French63ca5652020-10-15 23:41:40 -05004232 rc = crypto_aead_setkey(tfm, key, SMB3_GCM256_CRYPTKEY_SIZE);
4233 else
Shyam Prasad N45a45462021-03-25 12:34:54 +00004234 rc = crypto_aead_setkey(tfm, key, SMB3_GCM128_CRYPTKEY_SIZE);
Steve French63ca5652020-10-15 23:41:40 -05004235
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07004236 if (rc) {
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +10004237 cifs_server_dbg(VFS, "%s: Failed to set aead key %d\n", __func__, rc);
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07004238 return rc;
4239 }
4240
4241 rc = crypto_aead_setauthsize(tfm, SMB2_SIGNATURE_SIZE);
4242 if (rc) {
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +10004243 cifs_server_dbg(VFS, "%s: Failed to set authsize %d\n", __func__, rc);
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07004244 return rc;
4245 }
4246
4247 req = aead_request_alloc(tfm, GFP_KERNEL);
4248 if (!req) {
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +10004249 cifs_server_dbg(VFS, "%s: Failed to alloc aead request\n", __func__);
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07004250 return -ENOMEM;
4251 }
4252
4253 if (!enc) {
4254 memcpy(sign, &tr_hdr->Signature, SMB2_SIGNATURE_SIZE);
4255 crypt_len += SMB2_SIGNATURE_SIZE;
4256 }
4257
Ronnie Sahlbergb2c96de2018-08-01 09:26:11 +10004258 sg = init_sg(num_rqst, rqst, sign);
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07004259 if (!sg) {
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +10004260 cifs_server_dbg(VFS, "%s: Failed to init sg\n", __func__);
Christophe Jaillet517a6e42017-06-11 09:12:47 +02004261 rc = -ENOMEM;
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07004262 goto free_req;
4263 }
4264
4265 iv_len = crypto_aead_ivsize(tfm);
4266 iv = kzalloc(iv_len, GFP_KERNEL);
4267 if (!iv) {
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +10004268 cifs_server_dbg(VFS, "%s: Failed to alloc iv\n", __func__);
Christophe Jaillet517a6e42017-06-11 09:12:47 +02004269 rc = -ENOMEM;
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07004270 goto free_sg;
4271 }
Steve French2b2f7542019-06-07 15:16:10 -05004272
Steve French63ca5652020-10-15 23:41:40 -05004273 if ((server->cipher_type == SMB2_ENCRYPTION_AES128_GCM) ||
4274 (server->cipher_type == SMB2_ENCRYPTION_AES256_GCM))
Steve Frenchfd08f2d2020-10-15 00:25:02 -05004275 memcpy(iv, (char *)tr_hdr->Nonce, SMB3_AES_GCM_NONCE);
Steve French2b2f7542019-06-07 15:16:10 -05004276 else {
4277 iv[0] = 3;
Steve Frenchfd08f2d2020-10-15 00:25:02 -05004278 memcpy(iv + 1, (char *)tr_hdr->Nonce, SMB3_AES_CCM_NONCE);
Steve French2b2f7542019-06-07 15:16:10 -05004279 }
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07004280
4281 aead_request_set_crypt(req, sg, sg, crypt_len, iv);
4282 aead_request_set_ad(req, assoc_data_len);
4283
4284 aead_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
Gilad Ben-Yossefa5186b82017-10-18 08:00:46 +01004285 crypto_req_done, &wait);
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07004286
Gilad Ben-Yossefa5186b82017-10-18 08:00:46 +01004287 rc = crypto_wait_req(enc ? crypto_aead_encrypt(req)
4288 : crypto_aead_decrypt(req), &wait);
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07004289
4290 if (!rc && enc)
4291 memcpy(&tr_hdr->Signature, sign, SMB2_SIGNATURE_SIZE);
4292
4293 kfree(iv);
4294free_sg:
4295 kfree(sg);
4296free_req:
4297 kfree(req);
4298 return rc;
4299}
4300
Ronnie Sahlbergb2c96de2018-08-01 09:26:11 +10004301void
4302smb3_free_compound_rqst(int num_rqst, struct smb_rqst *rqst)
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07004303{
Ronnie Sahlbergb2c96de2018-08-01 09:26:11 +10004304 int i, j;
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07004305
Ronnie Sahlbergb2c96de2018-08-01 09:26:11 +10004306 for (i = 0; i < num_rqst; i++) {
4307 if (rqst[i].rq_pages) {
4308 for (j = rqst[i].rq_npages - 1; j >= 0; j--)
4309 put_page(rqst[i].rq_pages[j]);
4310 kfree(rqst[i].rq_pages);
4311 }
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07004312 }
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07004313}
4314
Ronnie Sahlbergb2c96de2018-08-01 09:26:11 +10004315/*
4316 * This function will initialize new_rq and encrypt the content.
4317 * The first entry, new_rq[0], only contains a single iov which contains
4318 * a smb2_transform_hdr and is pre-allocated by the caller.
4319 * This function then populates new_rq[1+] with the content from olq_rq[0+].
4320 *
4321 * The end result is an array of smb_rqst structures where the first structure
4322 * only contains a single iov for the transform header which we then can pass
4323 * to crypt_message().
4324 *
4325 * new_rq[0].rq_iov[0] : smb2_transform_hdr pre-allocated by the caller
4326 * new_rq[1+].rq_iov[*] == old_rq[0+].rq_iov[*] : SMB2/3 requests
4327 */
4328static int
4329smb3_init_transform_rq(struct TCP_Server_Info *server, int num_rqst,
4330 struct smb_rqst *new_rq, struct smb_rqst *old_rq)
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07004331{
Ronnie Sahlbergb2c96de2018-08-01 09:26:11 +10004332 struct page **pages;
4333 struct smb2_transform_hdr *tr_hdr = new_rq[0].rq_iov[0].iov_base;
4334 unsigned int npages;
4335 unsigned int orig_len = 0;
4336 int i, j;
4337 int rc = -ENOMEM;
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07004338
Ronnie Sahlbergb2c96de2018-08-01 09:26:11 +10004339 for (i = 1; i < num_rqst; i++) {
4340 npages = old_rq[i - 1].rq_npages;
4341 pages = kmalloc_array(npages, sizeof(struct page *),
4342 GFP_KERNEL);
4343 if (!pages)
4344 goto err_free;
4345
4346 new_rq[i].rq_pages = pages;
4347 new_rq[i].rq_npages = npages;
4348 new_rq[i].rq_offset = old_rq[i - 1].rq_offset;
4349 new_rq[i].rq_pagesz = old_rq[i - 1].rq_pagesz;
4350 new_rq[i].rq_tailsz = old_rq[i - 1].rq_tailsz;
4351 new_rq[i].rq_iov = old_rq[i - 1].rq_iov;
4352 new_rq[i].rq_nvec = old_rq[i - 1].rq_nvec;
4353
4354 orig_len += smb_rqst_len(server, &old_rq[i - 1]);
4355
4356 for (j = 0; j < npages; j++) {
4357 pages[j] = alloc_page(GFP_KERNEL|__GFP_HIGHMEM);
4358 if (!pages[j])
4359 goto err_free;
4360 }
4361
4362 /* copy pages form the old */
4363 for (j = 0; j < npages; j++) {
4364 char *dst, *src;
4365 unsigned int offset, len;
4366
4367 rqst_page_get_length(&new_rq[i], j, &len, &offset);
4368
4369 dst = (char *) kmap(new_rq[i].rq_pages[j]) + offset;
4370 src = (char *) kmap(old_rq[i - 1].rq_pages[j]) + offset;
4371
4372 memcpy(dst, src, len);
4373 kunmap(new_rq[i].rq_pages[j]);
4374 kunmap(old_rq[i - 1].rq_pages[j]);
4375 }
4376 }
4377
4378 /* fill the 1st iov with a transform header */
Steve French2b2f7542019-06-07 15:16:10 -05004379 fill_transform_hdr(tr_hdr, orig_len, old_rq, server->cipher_type);
Ronnie Sahlbergb2c96de2018-08-01 09:26:11 +10004380
4381 rc = crypt_message(server, num_rqst, new_rq, 1);
Christoph Probsta205d502019-05-08 21:36:25 +02004382 cifs_dbg(FYI, "Encrypt message returned %d\n", rc);
Ronnie Sahlbergb2c96de2018-08-01 09:26:11 +10004383 if (rc)
4384 goto err_free;
4385
4386 return rc;
4387
4388err_free:
4389 smb3_free_compound_rqst(num_rqst - 1, &new_rq[1]);
4390 return rc;
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07004391}
4392
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004393static int
4394smb3_is_transform_hdr(void *buf)
4395{
4396 struct smb2_transform_hdr *trhdr = buf;
4397
4398 return trhdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM;
4399}
4400
4401static int
4402decrypt_raw_data(struct TCP_Server_Info *server, char *buf,
4403 unsigned int buf_data_size, struct page **pages,
Rohith Surabattula62593012020-10-08 09:58:41 +00004404 unsigned int npages, unsigned int page_data_size,
4405 bool is_offloaded)
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004406{
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10004407 struct kvec iov[2];
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004408 struct smb_rqst rqst = {NULL};
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004409 int rc;
4410
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10004411 iov[0].iov_base = buf;
4412 iov[0].iov_len = sizeof(struct smb2_transform_hdr);
4413 iov[1].iov_base = buf + sizeof(struct smb2_transform_hdr);
4414 iov[1].iov_len = buf_data_size;
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004415
4416 rqst.rq_iov = iov;
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10004417 rqst.rq_nvec = 2;
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004418 rqst.rq_pages = pages;
4419 rqst.rq_npages = npages;
4420 rqst.rq_pagesz = PAGE_SIZE;
4421 rqst.rq_tailsz = (page_data_size % PAGE_SIZE) ? : PAGE_SIZE;
4422
Ronnie Sahlbergb2c96de2018-08-01 09:26:11 +10004423 rc = crypt_message(server, 1, &rqst, 0);
Christoph Probsta205d502019-05-08 21:36:25 +02004424 cifs_dbg(FYI, "Decrypt message returned %d\n", rc);
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004425
4426 if (rc)
4427 return rc;
4428
Ronnie Sahlbergc713c872018-06-12 08:00:58 +10004429 memmove(buf, iov[1].iov_base, buf_data_size);
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10004430
Rohith Surabattula62593012020-10-08 09:58:41 +00004431 if (!is_offloaded)
4432 server->total_read = buf_data_size + page_data_size;
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004433
4434 return rc;
4435}
4436
4437static int
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08004438read_data_into_pages(struct TCP_Server_Info *server, struct page **pages,
4439 unsigned int npages, unsigned int len)
4440{
4441 int i;
4442 int length;
4443
4444 for (i = 0; i < npages; i++) {
4445 struct page *page = pages[i];
4446 size_t n;
4447
4448 n = len;
4449 if (len >= PAGE_SIZE) {
4450 /* enough data to fill the page */
4451 n = PAGE_SIZE;
4452 len -= n;
4453 } else {
4454 zero_user(page, len, PAGE_SIZE - len);
4455 len = 0;
4456 }
Long Li1dbe3462018-05-30 12:47:55 -07004457 length = cifs_read_page_from_socket(server, page, 0, n);
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08004458 if (length < 0)
4459 return length;
4460 server->total_read += length;
4461 }
4462
4463 return 0;
4464}
4465
4466static int
4467init_read_bvec(struct page **pages, unsigned int npages, unsigned int data_size,
4468 unsigned int cur_off, struct bio_vec **page_vec)
4469{
4470 struct bio_vec *bvec;
4471 int i;
4472
4473 bvec = kcalloc(npages, sizeof(struct bio_vec), GFP_KERNEL);
4474 if (!bvec)
4475 return -ENOMEM;
4476
4477 for (i = 0; i < npages; i++) {
4478 bvec[i].bv_page = pages[i];
4479 bvec[i].bv_offset = (i == 0) ? cur_off : 0;
4480 bvec[i].bv_len = min_t(unsigned int, PAGE_SIZE, data_size);
4481 data_size -= bvec[i].bv_len;
4482 }
4483
4484 if (data_size != 0) {
4485 cifs_dbg(VFS, "%s: something went wrong\n", __func__);
4486 kfree(bvec);
4487 return -EIO;
4488 }
4489
4490 *page_vec = bvec;
4491 return 0;
4492}
4493
4494static int
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004495handle_read_data(struct TCP_Server_Info *server, struct mid_q_entry *mid,
4496 char *buf, unsigned int buf_len, struct page **pages,
Rohith Surabattulade9ac0a2020-10-28 13:42:21 +00004497 unsigned int npages, unsigned int page_data_size,
4498 bool is_offloaded)
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004499{
4500 unsigned int data_offset;
4501 unsigned int data_len;
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08004502 unsigned int cur_off;
4503 unsigned int cur_page_idx;
4504 unsigned int pad_len;
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004505 struct cifs_readdata *rdata = mid->callback_data;
Ronnie Sahlberg49f466b2018-06-01 10:53:06 +10004506 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004507 struct bio_vec *bvec = NULL;
4508 struct iov_iter iter;
4509 struct kvec iov;
4510 int length;
Long Li74dcf412017-11-22 17:38:46 -07004511 bool use_rdma_mr = false;
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004512
4513 if (shdr->Command != SMB2_READ) {
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +10004514 cifs_server_dbg(VFS, "only big read responses are supported\n");
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004515 return -ENOTSUPP;
4516 }
4517
Pavel Shilovsky511c54a2017-07-08 14:32:00 -07004518 if (server->ops->is_session_expired &&
4519 server->ops->is_session_expired(buf)) {
Rohith Surabattulade9ac0a2020-10-28 13:42:21 +00004520 if (!is_offloaded)
4521 cifs_reconnect(server);
Pavel Shilovsky511c54a2017-07-08 14:32:00 -07004522 return -1;
4523 }
4524
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004525 if (server->ops->is_status_pending &&
Pavel Shilovsky66265f12019-01-23 17:11:16 -08004526 server->ops->is_status_pending(buf, server))
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004527 return -1;
4528
Pavel Shilovskyec678ea2019-01-18 15:38:11 -08004529 /* set up first two iov to get credits */
4530 rdata->iov[0].iov_base = buf;
Pavel Shilovskybb1bccb2019-01-17 16:18:38 -08004531 rdata->iov[0].iov_len = 0;
4532 rdata->iov[1].iov_base = buf;
Pavel Shilovskyec678ea2019-01-18 15:38:11 -08004533 rdata->iov[1].iov_len =
Pavel Shilovskybb1bccb2019-01-17 16:18:38 -08004534 min_t(unsigned int, buf_len, server->vals->read_rsp_size);
Pavel Shilovskyec678ea2019-01-18 15:38:11 -08004535 cifs_dbg(FYI, "0: iov_base=%p iov_len=%zu\n",
4536 rdata->iov[0].iov_base, rdata->iov[0].iov_len);
4537 cifs_dbg(FYI, "1: iov_base=%p iov_len=%zu\n",
4538 rdata->iov[1].iov_base, rdata->iov[1].iov_len);
4539
4540 rdata->result = server->ops->map_error(buf, true);
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004541 if (rdata->result != 0) {
4542 cifs_dbg(FYI, "%s: server returned error %d\n",
4543 __func__, rdata->result);
Pavel Shilovskyec678ea2019-01-18 15:38:11 -08004544 /* normal error on read response */
Rohith Surabattulaac873aa2020-10-29 05:03:10 +00004545 if (is_offloaded)
4546 mid->mid_state = MID_RESPONSE_RECEIVED;
4547 else
4548 dequeue_mid(mid, false);
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004549 return 0;
4550 }
4551
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10004552 data_offset = server->ops->read_data_offset(buf);
Long Li74dcf412017-11-22 17:38:46 -07004553#ifdef CONFIG_CIFS_SMB_DIRECT
4554 use_rdma_mr = rdata->mr;
4555#endif
4556 data_len = server->ops->read_data_length(buf, use_rdma_mr);
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004557
4558 if (data_offset < server->vals->read_rsp_size) {
4559 /*
4560 * win2k8 sometimes sends an offset of 0 when the read
4561 * is beyond the EOF. Treat it as if the data starts just after
4562 * the header.
4563 */
4564 cifs_dbg(FYI, "%s: data offset (%u) inside read response header\n",
4565 __func__, data_offset);
4566 data_offset = server->vals->read_rsp_size;
4567 } else if (data_offset > MAX_CIFS_SMALL_BUFFER_SIZE) {
4568 /* data_offset is beyond the end of smallbuf */
4569 cifs_dbg(FYI, "%s: data offset (%u) beyond end of smallbuf\n",
4570 __func__, data_offset);
4571 rdata->result = -EIO;
Rohith Surabattulaac873aa2020-10-29 05:03:10 +00004572 if (is_offloaded)
4573 mid->mid_state = MID_RESPONSE_MALFORMED;
4574 else
4575 dequeue_mid(mid, rdata->result);
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004576 return 0;
4577 }
4578
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08004579 pad_len = data_offset - server->vals->read_rsp_size;
4580
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004581 if (buf_len <= data_offset) {
4582 /* read response payload is in pages */
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08004583 cur_page_idx = pad_len / PAGE_SIZE;
4584 cur_off = pad_len % PAGE_SIZE;
4585
4586 if (cur_page_idx != 0) {
4587 /* data offset is beyond the 1st page of response */
4588 cifs_dbg(FYI, "%s: data offset (%u) beyond 1st page of response\n",
4589 __func__, data_offset);
4590 rdata->result = -EIO;
Rohith Surabattulaac873aa2020-10-29 05:03:10 +00004591 if (is_offloaded)
4592 mid->mid_state = MID_RESPONSE_MALFORMED;
4593 else
4594 dequeue_mid(mid, rdata->result);
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08004595 return 0;
4596 }
4597
4598 if (data_len > page_data_size - pad_len) {
4599 /* data_len is corrupt -- discard frame */
4600 rdata->result = -EIO;
Rohith Surabattulaac873aa2020-10-29 05:03:10 +00004601 if (is_offloaded)
4602 mid->mid_state = MID_RESPONSE_MALFORMED;
4603 else
4604 dequeue_mid(mid, rdata->result);
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08004605 return 0;
4606 }
4607
4608 rdata->result = init_read_bvec(pages, npages, page_data_size,
4609 cur_off, &bvec);
4610 if (rdata->result != 0) {
Rohith Surabattulaac873aa2020-10-29 05:03:10 +00004611 if (is_offloaded)
4612 mid->mid_state = MID_RESPONSE_MALFORMED;
4613 else
4614 dequeue_mid(mid, rdata->result);
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08004615 return 0;
4616 }
4617
David Howellsaa563d72018-10-20 00:57:56 +01004618 iov_iter_bvec(&iter, WRITE, bvec, npages, data_len);
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004619 } else if (buf_len >= data_offset + data_len) {
4620 /* read response payload is in buf */
4621 WARN_ONCE(npages > 0, "read data can be either in buf or in pages");
4622 iov.iov_base = buf + data_offset;
4623 iov.iov_len = data_len;
David Howellsaa563d72018-10-20 00:57:56 +01004624 iov_iter_kvec(&iter, WRITE, &iov, 1, data_len);
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004625 } else {
4626 /* read response payload cannot be in both buf and pages */
4627 WARN_ONCE(1, "buf can not contain only a part of read data");
4628 rdata->result = -EIO;
Rohith Surabattulaac873aa2020-10-29 05:03:10 +00004629 if (is_offloaded)
4630 mid->mid_state = MID_RESPONSE_MALFORMED;
4631 else
4632 dequeue_mid(mid, rdata->result);
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004633 return 0;
4634 }
4635
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004636 length = rdata->copy_into_pages(server, rdata, &iter);
4637
4638 kfree(bvec);
4639
4640 if (length < 0)
4641 return length;
4642
Rohith Surabattulaac873aa2020-10-29 05:03:10 +00004643 if (is_offloaded)
4644 mid->mid_state = MID_RESPONSE_RECEIVED;
4645 else
4646 dequeue_mid(mid, false);
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004647 return length;
4648}
4649
Steve French35cf94a2019-09-07 01:09:49 -05004650struct smb2_decrypt_work {
4651 struct work_struct decrypt;
4652 struct TCP_Server_Info *server;
4653 struct page **ppages;
4654 char *buf;
4655 unsigned int npages;
4656 unsigned int len;
4657};
4658
4659
4660static void smb2_decrypt_offload(struct work_struct *work)
4661{
4662 struct smb2_decrypt_work *dw = container_of(work,
4663 struct smb2_decrypt_work, decrypt);
4664 int i, rc;
4665 struct mid_q_entry *mid;
4666
4667 rc = decrypt_raw_data(dw->server, dw->buf, dw->server->vals->read_rsp_size,
Rohith Surabattula62593012020-10-08 09:58:41 +00004668 dw->ppages, dw->npages, dw->len, true);
Steve French35cf94a2019-09-07 01:09:49 -05004669 if (rc) {
4670 cifs_dbg(VFS, "error decrypting rc=%d\n", rc);
4671 goto free_pages;
4672 }
4673
Steve French22553972019-09-13 16:47:31 -05004674 dw->server->lstrp = jiffies;
Rohith Surabattulaac873aa2020-10-29 05:03:10 +00004675 mid = smb2_find_dequeue_mid(dw->server, dw->buf);
Steve French35cf94a2019-09-07 01:09:49 -05004676 if (mid == NULL)
4677 cifs_dbg(FYI, "mid not found\n");
4678 else {
4679 mid->decrypted = true;
4680 rc = handle_read_data(dw->server, mid, dw->buf,
4681 dw->server->vals->read_rsp_size,
Rohith Surabattulade9ac0a2020-10-28 13:42:21 +00004682 dw->ppages, dw->npages, dw->len,
4683 true);
Rohith Surabattula12541002020-10-29 06:07:56 +00004684 if (rc >= 0) {
4685#ifdef CONFIG_CIFS_STATS2
4686 mid->when_received = jiffies;
4687#endif
Rohith Surabattula9e550b02021-02-16 10:40:45 +00004688 if (dw->server->ops->is_network_name_deleted)
4689 dw->server->ops->is_network_name_deleted(dw->buf,
4690 dw->server);
4691
Rohith Surabattula12541002020-10-29 06:07:56 +00004692 mid->callback(mid);
4693 } else {
4694 spin_lock(&GlobalMid_Lock);
4695 if (dw->server->tcpStatus == CifsNeedReconnect) {
4696 mid->mid_state = MID_RETRY_NEEDED;
4697 spin_unlock(&GlobalMid_Lock);
4698 mid->callback(mid);
4699 } else {
4700 mid->mid_state = MID_REQUEST_SUBMITTED;
4701 mid->mid_flags &= ~(MID_DELETED);
4702 list_add_tail(&mid->qhead,
4703 &dw->server->pending_mid_q);
4704 spin_unlock(&GlobalMid_Lock);
4705 }
4706 }
Steve French22553972019-09-13 16:47:31 -05004707 cifs_mid_q_entry_release(mid);
Steve French35cf94a2019-09-07 01:09:49 -05004708 }
4709
Steve French35cf94a2019-09-07 01:09:49 -05004710free_pages:
4711 for (i = dw->npages-1; i >= 0; i--)
4712 put_page(dw->ppages[i]);
4713
4714 kfree(dw->ppages);
4715 cifs_small_buf_release(dw->buf);
Steve Frencha08d8972019-10-26 16:00:44 -05004716 kfree(dw);
Steve French35cf94a2019-09-07 01:09:49 -05004717}
4718
4719
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004720static int
Steve French35cf94a2019-09-07 01:09:49 -05004721receive_encrypted_read(struct TCP_Server_Info *server, struct mid_q_entry **mid,
4722 int *num_mids)
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08004723{
4724 char *buf = server->smallbuf;
4725 struct smb2_transform_hdr *tr_hdr = (struct smb2_transform_hdr *)buf;
4726 unsigned int npages;
4727 struct page **pages;
4728 unsigned int len;
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10004729 unsigned int buflen = server->pdu_size;
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08004730 int rc;
4731 int i = 0;
Steve French35cf94a2019-09-07 01:09:49 -05004732 struct smb2_decrypt_work *dw;
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08004733
Steve French35cf94a2019-09-07 01:09:49 -05004734 *num_mids = 1;
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10004735 len = min_t(unsigned int, buflen, server->vals->read_rsp_size +
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08004736 sizeof(struct smb2_transform_hdr)) - HEADER_SIZE(server) + 1;
4737
4738 rc = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1, len);
4739 if (rc < 0)
4740 return rc;
4741 server->total_read += rc;
4742
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10004743 len = le32_to_cpu(tr_hdr->OriginalMessageSize) -
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +11004744 server->vals->read_rsp_size;
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08004745 npages = DIV_ROUND_UP(len, PAGE_SIZE);
4746
4747 pages = kmalloc_array(npages, sizeof(struct page *), GFP_KERNEL);
4748 if (!pages) {
4749 rc = -ENOMEM;
4750 goto discard_data;
4751 }
4752
4753 for (; i < npages; i++) {
4754 pages[i] = alloc_page(GFP_KERNEL|__GFP_HIGHMEM);
4755 if (!pages[i]) {
4756 rc = -ENOMEM;
4757 goto discard_data;
4758 }
4759 }
4760
4761 /* read read data into pages */
4762 rc = read_data_into_pages(server, pages, npages, len);
4763 if (rc)
4764 goto free_pages;
4765
Pavel Shilovsky350be252017-04-10 10:31:33 -07004766 rc = cifs_discard_remaining_data(server);
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08004767 if (rc)
4768 goto free_pages;
4769
Steve French35cf94a2019-09-07 01:09:49 -05004770 /*
4771 * For large reads, offload to different thread for better performance,
4772 * use more cores decrypting which can be expensive
4773 */
4774
Steve French10328c42019-09-09 13:30:15 -05004775 if ((server->min_offload) && (server->in_flight > 1) &&
Steve French563317e2019-09-08 23:22:02 -05004776 (server->pdu_size >= server->min_offload)) {
Steve French35cf94a2019-09-07 01:09:49 -05004777 dw = kmalloc(sizeof(struct smb2_decrypt_work), GFP_KERNEL);
4778 if (dw == NULL)
4779 goto non_offloaded_decrypt;
4780
4781 dw->buf = server->smallbuf;
4782 server->smallbuf = (char *)cifs_small_buf_get();
4783
4784 INIT_WORK(&dw->decrypt, smb2_decrypt_offload);
4785
4786 dw->npages = npages;
4787 dw->server = server;
4788 dw->ppages = pages;
4789 dw->len = len;
Steve Frencha08d8972019-10-26 16:00:44 -05004790 queue_work(decrypt_wq, &dw->decrypt);
Steve French35cf94a2019-09-07 01:09:49 -05004791 *num_mids = 0; /* worker thread takes care of finding mid */
4792 return -1;
4793 }
4794
4795non_offloaded_decrypt:
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10004796 rc = decrypt_raw_data(server, buf, server->vals->read_rsp_size,
Rohith Surabattula62593012020-10-08 09:58:41 +00004797 pages, npages, len, false);
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08004798 if (rc)
4799 goto free_pages;
4800
4801 *mid = smb2_find_mid(server, buf);
4802 if (*mid == NULL)
4803 cifs_dbg(FYI, "mid not found\n");
4804 else {
4805 cifs_dbg(FYI, "mid found\n");
4806 (*mid)->decrypted = true;
4807 rc = handle_read_data(server, *mid, buf,
4808 server->vals->read_rsp_size,
Rohith Surabattulade9ac0a2020-10-28 13:42:21 +00004809 pages, npages, len, false);
Rohith Surabattula9e550b02021-02-16 10:40:45 +00004810 if (rc >= 0) {
4811 if (server->ops->is_network_name_deleted) {
4812 server->ops->is_network_name_deleted(buf,
4813 server);
4814 }
4815 }
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08004816 }
4817
4818free_pages:
4819 for (i = i - 1; i >= 0; i--)
4820 put_page(pages[i]);
4821 kfree(pages);
4822 return rc;
4823discard_data:
Pavel Shilovsky350be252017-04-10 10:31:33 -07004824 cifs_discard_remaining_data(server);
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08004825 goto free_pages;
4826}
4827
4828static int
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004829receive_encrypted_standard(struct TCP_Server_Info *server,
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10004830 struct mid_q_entry **mids, char **bufs,
4831 int *num_mids)
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004832{
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10004833 int ret, length;
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004834 char *buf = server->smallbuf;
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10004835 struct smb2_sync_hdr *shdr;
Ronnie Sahlberg2e964672018-04-09 18:06:26 +10004836 unsigned int pdu_length = server->pdu_size;
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004837 unsigned int buf_size;
4838 struct mid_q_entry *mid_entry;
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10004839 int next_is_large;
4840 char *next_buffer = NULL;
4841
4842 *num_mids = 0;
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004843
4844 /* switch to large buffer if too big for a small one */
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10004845 if (pdu_length > MAX_CIFS_SMALL_BUFFER_SIZE) {
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004846 server->large_buf = true;
4847 memcpy(server->bigbuf, buf, server->total_read);
4848 buf = server->bigbuf;
4849 }
4850
4851 /* now read the rest */
4852 length = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1,
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10004853 pdu_length - HEADER_SIZE(server) + 1);
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004854 if (length < 0)
4855 return length;
4856 server->total_read += length;
4857
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10004858 buf_size = pdu_length - sizeof(struct smb2_transform_hdr);
Rohith Surabattula62593012020-10-08 09:58:41 +00004859 length = decrypt_raw_data(server, buf, buf_size, NULL, 0, 0, false);
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004860 if (length)
4861 return length;
4862
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10004863 next_is_large = server->large_buf;
Pavel Shilovsky3edeb4a2019-07-22 11:38:22 -07004864one_more:
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10004865 shdr = (struct smb2_sync_hdr *)buf;
4866 if (shdr->NextCommand) {
Pavel Shilovsky3edeb4a2019-07-22 11:38:22 -07004867 if (next_is_large)
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10004868 next_buffer = (char *)cifs_buf_get();
Pavel Shilovsky3edeb4a2019-07-22 11:38:22 -07004869 else
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10004870 next_buffer = (char *)cifs_small_buf_get();
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10004871 memcpy(next_buffer,
Pavel Shilovsky3edeb4a2019-07-22 11:38:22 -07004872 buf + le32_to_cpu(shdr->NextCommand),
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10004873 pdu_length - le32_to_cpu(shdr->NextCommand));
4874 }
4875
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004876 mid_entry = smb2_find_mid(server, buf);
4877 if (mid_entry == NULL)
4878 cifs_dbg(FYI, "mid not found\n");
4879 else {
4880 cifs_dbg(FYI, "mid found\n");
4881 mid_entry->decrypted = true;
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10004882 mid_entry->resp_buf_size = server->pdu_size;
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004883 }
4884
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10004885 if (*num_mids >= MAX_COMPOUND) {
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +10004886 cifs_server_dbg(VFS, "too many PDUs in compound\n");
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10004887 return -1;
4888 }
4889 bufs[*num_mids] = buf;
4890 mids[(*num_mids)++] = mid_entry;
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004891
4892 if (mid_entry && mid_entry->handle)
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10004893 ret = mid_entry->handle(server, mid_entry);
4894 else
4895 ret = cifs_handle_standard(server, mid_entry);
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004896
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10004897 if (ret == 0 && shdr->NextCommand) {
4898 pdu_length -= le32_to_cpu(shdr->NextCommand);
4899 server->large_buf = next_is_large;
4900 if (next_is_large)
Pavel Shilovsky3edeb4a2019-07-22 11:38:22 -07004901 server->bigbuf = buf = next_buffer;
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10004902 else
Pavel Shilovsky3edeb4a2019-07-22 11:38:22 -07004903 server->smallbuf = buf = next_buffer;
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10004904 goto one_more;
Pavel Shilovsky3edeb4a2019-07-22 11:38:22 -07004905 } else if (ret != 0) {
4906 /*
4907 * ret != 0 here means that we didn't get to handle_mid() thus
4908 * server->smallbuf and server->bigbuf are still valid. We need
4909 * to free next_buffer because it is not going to be used
4910 * anywhere.
4911 */
4912 if (next_is_large)
4913 free_rsp_buf(CIFS_LARGE_BUFFER, next_buffer);
4914 else
4915 free_rsp_buf(CIFS_SMALL_BUFFER, next_buffer);
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10004916 }
4917
4918 return ret;
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004919}
4920
4921static int
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10004922smb3_receive_transform(struct TCP_Server_Info *server,
4923 struct mid_q_entry **mids, char **bufs, int *num_mids)
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004924{
4925 char *buf = server->smallbuf;
Ronnie Sahlberg2e964672018-04-09 18:06:26 +10004926 unsigned int pdu_length = server->pdu_size;
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004927 struct smb2_transform_hdr *tr_hdr = (struct smb2_transform_hdr *)buf;
4928 unsigned int orig_len = le32_to_cpu(tr_hdr->OriginalMessageSize);
4929
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10004930 if (pdu_length < sizeof(struct smb2_transform_hdr) +
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004931 sizeof(struct smb2_sync_hdr)) {
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +10004932 cifs_server_dbg(VFS, "Transform message is too small (%u)\n",
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004933 pdu_length);
4934 cifs_reconnect(server);
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004935 return -ECONNABORTED;
4936 }
4937
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10004938 if (pdu_length < orig_len + sizeof(struct smb2_transform_hdr)) {
Ronnie Sahlberg3175eb92019-09-04 12:32:41 +10004939 cifs_server_dbg(VFS, "Transform message is broken\n");
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004940 cifs_reconnect(server);
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004941 return -ECONNABORTED;
4942 }
4943
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10004944 /* TODO: add support for compounds containing READ. */
Paul Aurich6d2f84e2018-12-31 14:13:34 -08004945 if (pdu_length > CIFSMaxBufSize + MAX_HEADER_SIZE(server)) {
Steve French35cf94a2019-09-07 01:09:49 -05004946 return receive_encrypted_read(server, &mids[0], num_mids);
Paul Aurich6d2f84e2018-12-31 14:13:34 -08004947 }
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004948
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10004949 return receive_encrypted_standard(server, mids, bufs, num_mids);
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004950}
4951
4952int
4953smb3_handle_read_data(struct TCP_Server_Info *server, struct mid_q_entry *mid)
4954{
4955 char *buf = server->large_buf ? server->bigbuf : server->smallbuf;
4956
Ronnie Sahlberg1fc6ad22018-06-01 10:53:07 +10004957 return handle_read_data(server, mid, buf, server->pdu_size,
Rohith Surabattulade9ac0a2020-10-28 13:42:21 +00004958 NULL, 0, 0, false);
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08004959}
4960
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +10004961static int
4962smb2_next_header(char *buf)
4963{
4964 struct smb2_sync_hdr *hdr = (struct smb2_sync_hdr *)buf;
4965 struct smb2_transform_hdr *t_hdr = (struct smb2_transform_hdr *)buf;
4966
4967 if (hdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM)
4968 return sizeof(struct smb2_transform_hdr) +
4969 le32_to_cpu(t_hdr->OriginalMessageSize);
4970
4971 return le32_to_cpu(hdr->NextCommand);
4972}
4973
Aurelien Aptelc847dcc2019-03-14 00:29:17 -05004974static int
4975smb2_make_node(unsigned int xid, struct inode *inode,
4976 struct dentry *dentry, struct cifs_tcon *tcon,
Al Viro55869132021-03-18 01:38:53 -04004977 const char *full_path, umode_t mode, dev_t dev)
Aurelien Aptelc847dcc2019-03-14 00:29:17 -05004978{
4979 struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
4980 int rc = -EPERM;
Aurelien Aptelc847dcc2019-03-14 00:29:17 -05004981 FILE_ALL_INFO *buf = NULL;
Aurelien Aptel7c065142020-06-04 17:23:55 +02004982 struct cifs_io_parms io_parms = {0};
Aurelien Aptelc847dcc2019-03-14 00:29:17 -05004983 __u32 oplock = 0;
4984 struct cifs_fid fid;
4985 struct cifs_open_parms oparms;
4986 unsigned int bytes_written;
4987 struct win_dev *pdev;
4988 struct kvec iov[2];
4989
4990 /*
4991 * Check if mounted with mount parm 'sfu' mount parm.
4992 * SFU emulation should work with all servers, but only
4993 * supports block and char device (no socket & fifo),
4994 * and was used by default in earlier versions of Windows
4995 */
4996 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL))
4997 goto out;
4998
4999 /*
5000 * TODO: Add ability to create instead via reparse point. Windows (e.g.
5001 * their current NFS server) uses this approach to expose special files
5002 * over SMB2/SMB3 and Samba will do this with SMB3.1.1 POSIX Extensions
5003 */
5004
5005 if (!S_ISCHR(mode) && !S_ISBLK(mode))
5006 goto out;
5007
5008 cifs_dbg(FYI, "sfu compat create special file\n");
5009
5010 buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL);
5011 if (buf == NULL) {
5012 rc = -ENOMEM;
5013 goto out;
5014 }
5015
Aurelien Aptelc847dcc2019-03-14 00:29:17 -05005016 oparms.tcon = tcon;
5017 oparms.cifs_sb = cifs_sb;
5018 oparms.desired_access = GENERIC_WRITE;
Amir Goldstein0f060932020-02-03 21:46:43 +02005019 oparms.create_options = cifs_create_options(cifs_sb, CREATE_NOT_DIR |
5020 CREATE_OPTION_SPECIAL);
Aurelien Aptelc847dcc2019-03-14 00:29:17 -05005021 oparms.disposition = FILE_CREATE;
5022 oparms.path = full_path;
5023 oparms.fid = &fid;
5024 oparms.reconnect = false;
5025
5026 if (tcon->ses->server->oplocks)
5027 oplock = REQ_OPLOCK;
5028 else
5029 oplock = 0;
5030 rc = tcon->ses->server->ops->open(xid, &oparms, &oplock, buf);
5031 if (rc)
5032 goto out;
5033
5034 /*
5035 * BB Do not bother to decode buf since no local inode yet to put
5036 * timestamps in, but we can reuse it safely.
5037 */
5038
5039 pdev = (struct win_dev *)buf;
5040 io_parms.pid = current->tgid;
5041 io_parms.tcon = tcon;
5042 io_parms.offset = 0;
5043 io_parms.length = sizeof(struct win_dev);
5044 iov[1].iov_base = buf;
5045 iov[1].iov_len = sizeof(struct win_dev);
5046 if (S_ISCHR(mode)) {
5047 memcpy(pdev->type, "IntxCHR", 8);
5048 pdev->major = cpu_to_le64(MAJOR(dev));
5049 pdev->minor = cpu_to_le64(MINOR(dev));
5050 rc = tcon->ses->server->ops->sync_write(xid, &fid, &io_parms,
5051 &bytes_written, iov, 1);
5052 } else if (S_ISBLK(mode)) {
5053 memcpy(pdev->type, "IntxBLK", 8);
5054 pdev->major = cpu_to_le64(MAJOR(dev));
5055 pdev->minor = cpu_to_le64(MINOR(dev));
5056 rc = tcon->ses->server->ops->sync_write(xid, &fid, &io_parms,
5057 &bytes_written, iov, 1);
5058 }
5059 tcon->ses->server->ops->close(xid, tcon, &fid);
5060 d_drop(dentry);
5061
5062 /* FIXME: add code here to set EAs */
5063out:
5064 kfree(buf);
5065 return rc;
5066}
5067
5068
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04005069struct smb_version_operations smb20_operations = {
5070 .compare_fids = smb2_compare_fids,
5071 .setup_request = smb2_setup_request,
5072 .setup_async_request = smb2_setup_async_request,
5073 .check_receive = smb2_check_receive,
5074 .add_credits = smb2_add_credits,
5075 .set_credits = smb2_set_credits,
5076 .get_credits_field = smb2_get_credits_field,
5077 .get_credits = smb2_get_credits,
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04005078 .wait_mtu_credits = cifs_wait_mtu_credits,
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04005079 .get_next_mid = smb2_get_next_mid,
Pavel Shilovskyc781af72019-03-04 14:02:50 -08005080 .revert_current_mid = smb2_revert_current_mid,
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04005081 .read_data_offset = smb2_read_data_offset,
5082 .read_data_length = smb2_read_data_length,
5083 .map_error = map_smb2_to_linux_error,
5084 .find_mid = smb2_find_mid,
5085 .check_message = smb2_check_message,
5086 .dump_detail = smb2_dump_detail,
5087 .clear_stats = smb2_clear_stats,
5088 .print_stats = smb2_print_stats,
5089 .is_oplock_break = smb2_is_valid_oplock_break,
Sachin Prabhu38bd4902017-03-03 15:41:38 -08005090 .handle_cancelled_mid = smb2_handle_cancelled_mid,
Sachin Prabhuc11f1df2014-03-11 16:11:47 +00005091 .downgrade_oplock = smb2_downgrade_oplock,
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04005092 .need_neg = smb2_need_neg,
5093 .negotiate = smb2_negotiate,
5094 .negotiate_wsize = smb2_negotiate_wsize,
5095 .negotiate_rsize = smb2_negotiate_rsize,
5096 .sess_setup = SMB2_sess_setup,
5097 .logoff = SMB2_logoff,
5098 .tree_connect = SMB2_tcon,
5099 .tree_disconnect = SMB2_tdis,
Steve French34f62642013-10-09 02:07:00 -05005100 .qfs_tcon = smb2_qfs_tcon,
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04005101 .is_path_accessible = smb2_is_path_accessible,
5102 .can_echo = smb2_can_echo,
5103 .echo = SMB2_echo,
5104 .query_path_info = smb2_query_path_info,
5105 .get_srv_inum = smb2_get_srv_inum,
5106 .query_file_info = smb2_query_file_info,
5107 .set_path_size = smb2_set_path_size,
5108 .set_file_size = smb2_set_file_size,
5109 .set_file_info = smb2_set_file_info,
Steve French64a5cfa2013-10-14 15:31:32 -05005110 .set_compression = smb2_set_compression,
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04005111 .mkdir = smb2_mkdir,
5112 .mkdir_setinfo = smb2_mkdir_setinfo,
5113 .rmdir = smb2_rmdir,
5114 .unlink = smb2_unlink,
5115 .rename = smb2_rename_path,
5116 .create_hardlink = smb2_create_hardlink,
5117 .query_symlink = smb2_query_symlink,
Sachin Prabhu5b23c972016-07-11 16:53:20 +01005118 .query_mf_symlink = smb3_query_mf_symlink,
5119 .create_mf_symlink = smb3_create_mf_symlink,
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04005120 .open = smb2_open_file,
5121 .set_fid = smb2_set_fid,
5122 .close = smb2_close_file,
5123 .flush = smb2_flush_file,
5124 .async_readv = smb2_async_readv,
5125 .async_writev = smb2_async_writev,
5126 .sync_read = smb2_sync_read,
5127 .sync_write = smb2_sync_write,
5128 .query_dir_first = smb2_query_dir_first,
5129 .query_dir_next = smb2_query_dir_next,
5130 .close_dir = smb2_close_dir,
5131 .calc_smb_size = smb2_calc_size,
5132 .is_status_pending = smb2_is_status_pending,
Pavel Shilovsky511c54a2017-07-08 14:32:00 -07005133 .is_session_expired = smb2_is_session_expired,
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04005134 .oplock_response = smb2_oplock_response,
5135 .queryfs = smb2_queryfs,
5136 .mand_lock = smb2_mand_lock,
5137 .mand_unlock_range = smb2_unlock_range,
5138 .push_mand_locks = smb2_push_mandatory_locks,
5139 .get_lease_key = smb2_get_lease_key,
5140 .set_lease_key = smb2_set_lease_key,
5141 .new_lease_key = smb2_new_lease_key,
5142 .calc_signature = smb2_calc_signature,
5143 .is_read_op = smb2_is_read_op,
5144 .set_oplock_level = smb2_set_oplock_level,
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04005145 .create_lease_buf = smb2_create_lease_buf,
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04005146 .parse_lease_buf = smb2_parse_lease_buf,
Sachin Prabhu312bbc52017-04-04 02:12:04 -05005147 .copychunk_range = smb2_copychunk_range,
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04005148 .wp_retry_size = smb2_wp_retry_size,
Pavel Shilovsky52755802014-08-18 20:49:57 +04005149 .dir_needs_close = smb2_dir_needs_close,
Aurelien Aptel9d496402017-02-13 16:16:49 +01005150 .get_dfs_refer = smb2_get_dfs_refer,
Sachin Prabhuef65aae2017-01-18 15:35:57 +05305151 .select_sectype = smb2_select_sectype,
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10005152#ifdef CONFIG_CIFS_XATTR
5153 .query_all_EAs = smb2_query_eas,
Ronnie Sahlberg55175542017-08-24 11:24:56 +10005154 .set_EA = smb2_set_ea,
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10005155#endif /* CIFS_XATTR */
Shirish Pargaonkar2f1afe22017-06-22 22:52:05 -05005156 .get_acl = get_smb2_acl,
5157 .get_acl_by_fid = get_smb2_acl_by_fid,
Shirish Pargaonkar366ed842017-06-28 22:37:32 -05005158 .set_acl = set_smb2_acl,
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +10005159 .next_header = smb2_next_header,
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05005160 .ioctl_query_info = smb2_ioctl_query_info,
Aurelien Aptelc847dcc2019-03-14 00:29:17 -05005161 .make_node = smb2_make_node,
Ronnie Sahlberg2f3ebab2019-04-25 16:45:29 +10005162 .fiemap = smb3_fiemap,
Ronnie Sahlbergdece44e2019-05-15 07:17:02 +10005163 .llseek = smb3_llseek,
Rohith Surabattula8e670f72020-09-18 05:37:28 +00005164 .is_status_io_timeout = smb2_is_status_io_timeout,
Rohith Surabattula9e550b02021-02-16 10:40:45 +00005165 .is_network_name_deleted = smb2_is_network_name_deleted,
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04005166};
5167
Steve French1080ef72011-02-24 18:07:19 +00005168struct smb_version_operations smb21_operations = {
Pavel Shilovsky027e8ee2012-09-19 06:22:43 -07005169 .compare_fids = smb2_compare_fids,
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +04005170 .setup_request = smb2_setup_request,
Pavel Shilovskyc95b8ee2012-07-11 14:45:28 +04005171 .setup_async_request = smb2_setup_async_request,
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +04005172 .check_receive = smb2_check_receive,
Pavel Shilovsky28ea5292012-05-23 16:18:00 +04005173 .add_credits = smb2_add_credits,
5174 .set_credits = smb2_set_credits,
5175 .get_credits_field = smb2_get_credits_field,
5176 .get_credits = smb2_get_credits,
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04005177 .wait_mtu_credits = smb2_wait_mtu_credits,
Pavel Shilovsky9a1c67e2019-01-23 18:15:52 -08005178 .adjust_credits = smb2_adjust_credits,
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +04005179 .get_next_mid = smb2_get_next_mid,
Pavel Shilovskyc781af72019-03-04 14:02:50 -08005180 .revert_current_mid = smb2_revert_current_mid,
Pavel Shilovsky09a47072012-09-18 16:20:29 -07005181 .read_data_offset = smb2_read_data_offset,
5182 .read_data_length = smb2_read_data_length,
5183 .map_error = map_smb2_to_linux_error,
Pavel Shilovsky093b2bd2011-06-08 15:51:07 +04005184 .find_mid = smb2_find_mid,
5185 .check_message = smb2_check_message,
5186 .dump_detail = smb2_dump_detail,
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04005187 .clear_stats = smb2_clear_stats,
5188 .print_stats = smb2_print_stats,
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07005189 .is_oplock_break = smb2_is_valid_oplock_break,
Sachin Prabhu38bd4902017-03-03 15:41:38 -08005190 .handle_cancelled_mid = smb2_handle_cancelled_mid,
Pavel Shilovsky9bd45402019-10-29 16:51:19 -07005191 .downgrade_oplock = smb2_downgrade_oplock,
Pavel Shilovskyec2e4522011-12-27 16:12:43 +04005192 .need_neg = smb2_need_neg,
5193 .negotiate = smb2_negotiate,
Pavel Shilovsky3a3bab52012-09-18 16:20:28 -07005194 .negotiate_wsize = smb2_negotiate_wsize,
5195 .negotiate_rsize = smb2_negotiate_rsize,
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04005196 .sess_setup = SMB2_sess_setup,
5197 .logoff = SMB2_logoff,
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04005198 .tree_connect = SMB2_tcon,
5199 .tree_disconnect = SMB2_tdis,
Steve French34f62642013-10-09 02:07:00 -05005200 .qfs_tcon = smb2_qfs_tcon,
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04005201 .is_path_accessible = smb2_is_path_accessible,
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04005202 .can_echo = smb2_can_echo,
5203 .echo = SMB2_echo,
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04005204 .query_path_info = smb2_query_path_info,
5205 .get_srv_inum = smb2_get_srv_inum,
Pavel Shilovskyb7546bc2012-09-18 16:20:27 -07005206 .query_file_info = smb2_query_file_info,
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07005207 .set_path_size = smb2_set_path_size,
5208 .set_file_size = smb2_set_file_size,
Pavel Shilovsky1feeaac2012-09-18 16:20:32 -07005209 .set_file_info = smb2_set_file_info,
Steve French64a5cfa2013-10-14 15:31:32 -05005210 .set_compression = smb2_set_compression,
Pavel Shilovskya0e73182011-07-19 12:56:37 +04005211 .mkdir = smb2_mkdir,
5212 .mkdir_setinfo = smb2_mkdir_setinfo,
Pavel Shilovsky1a500f02012-07-10 16:14:38 +04005213 .rmdir = smb2_rmdir,
Pavel Shilovskycbe6f432012-09-18 16:20:25 -07005214 .unlink = smb2_unlink,
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07005215 .rename = smb2_rename_path,
Pavel Shilovsky568798c2012-09-18 16:20:31 -07005216 .create_hardlink = smb2_create_hardlink,
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04005217 .query_symlink = smb2_query_symlink,
Steve Frenchc22870e2014-09-16 07:18:19 -05005218 .query_mf_symlink = smb3_query_mf_symlink,
Steve French5ab97572014-09-15 04:49:28 -05005219 .create_mf_symlink = smb3_create_mf_symlink,
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07005220 .open = smb2_open_file,
5221 .set_fid = smb2_set_fid,
5222 .close = smb2_close_file,
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07005223 .flush = smb2_flush_file,
Pavel Shilovsky09a47072012-09-18 16:20:29 -07005224 .async_readv = smb2_async_readv,
Pavel Shilovsky33319142012-09-18 16:20:29 -07005225 .async_writev = smb2_async_writev,
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07005226 .sync_read = smb2_sync_read,
Pavel Shilovsky009d3442012-09-18 16:20:30 -07005227 .sync_write = smb2_sync_write,
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07005228 .query_dir_first = smb2_query_dir_first,
5229 .query_dir_next = smb2_query_dir_next,
5230 .close_dir = smb2_close_dir,
5231 .calc_smb_size = smb2_calc_size,
Pavel Shilovsky2e44b282012-09-18 16:20:33 -07005232 .is_status_pending = smb2_is_status_pending,
Pavel Shilovsky511c54a2017-07-08 14:32:00 -07005233 .is_session_expired = smb2_is_session_expired,
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07005234 .oplock_response = smb2_oplock_response,
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07005235 .queryfs = smb2_queryfs,
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07005236 .mand_lock = smb2_mand_lock,
5237 .mand_unlock_range = smb2_unlock_range,
Pavel Shilovskyb1407992012-09-19 06:22:44 -07005238 .push_mand_locks = smb2_push_mandatory_locks,
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07005239 .get_lease_key = smb2_get_lease_key,
5240 .set_lease_key = smb2_set_lease_key,
5241 .new_lease_key = smb2_new_lease_key,
Steve French38107d42012-12-08 22:08:06 -06005242 .calc_signature = smb2_calc_signature,
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04005243 .is_read_op = smb21_is_read_op,
5244 .set_oplock_level = smb21_set_oplock_level,
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04005245 .create_lease_buf = smb2_create_lease_buf,
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04005246 .parse_lease_buf = smb2_parse_lease_buf,
Sachin Prabhu312bbc52017-04-04 02:12:04 -05005247 .copychunk_range = smb2_copychunk_range,
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04005248 .wp_retry_size = smb2_wp_retry_size,
Pavel Shilovsky52755802014-08-18 20:49:57 +04005249 .dir_needs_close = smb2_dir_needs_close,
Steve French834170c2016-09-30 21:14:26 -05005250 .enum_snapshots = smb3_enum_snapshots,
Steve French2c6251a2020-02-12 22:37:08 -06005251 .notify = smb3_notify,
Aurelien Aptel9d496402017-02-13 16:16:49 +01005252 .get_dfs_refer = smb2_get_dfs_refer,
Sachin Prabhuef65aae2017-01-18 15:35:57 +05305253 .select_sectype = smb2_select_sectype,
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10005254#ifdef CONFIG_CIFS_XATTR
5255 .query_all_EAs = smb2_query_eas,
Ronnie Sahlberg55175542017-08-24 11:24:56 +10005256 .set_EA = smb2_set_ea,
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10005257#endif /* CIFS_XATTR */
Shirish Pargaonkar2f1afe22017-06-22 22:52:05 -05005258 .get_acl = get_smb2_acl,
5259 .get_acl_by_fid = get_smb2_acl_by_fid,
Shirish Pargaonkar366ed842017-06-28 22:37:32 -05005260 .set_acl = set_smb2_acl,
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +10005261 .next_header = smb2_next_header,
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05005262 .ioctl_query_info = smb2_ioctl_query_info,
Aurelien Aptelc847dcc2019-03-14 00:29:17 -05005263 .make_node = smb2_make_node,
Ronnie Sahlberg2f3ebab2019-04-25 16:45:29 +10005264 .fiemap = smb3_fiemap,
Ronnie Sahlbergdece44e2019-05-15 07:17:02 +10005265 .llseek = smb3_llseek,
Rohith Surabattula8e670f72020-09-18 05:37:28 +00005266 .is_status_io_timeout = smb2_is_status_io_timeout,
Rohith Surabattula9e550b02021-02-16 10:40:45 +00005267 .is_network_name_deleted = smb2_is_network_name_deleted,
Steve French38107d42012-12-08 22:08:06 -06005268};
5269
Steve French38107d42012-12-08 22:08:06 -06005270struct smb_version_operations smb30_operations = {
5271 .compare_fids = smb2_compare_fids,
5272 .setup_request = smb2_setup_request,
5273 .setup_async_request = smb2_setup_async_request,
5274 .check_receive = smb2_check_receive,
5275 .add_credits = smb2_add_credits,
5276 .set_credits = smb2_set_credits,
5277 .get_credits_field = smb2_get_credits_field,
5278 .get_credits = smb2_get_credits,
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04005279 .wait_mtu_credits = smb2_wait_mtu_credits,
Pavel Shilovsky9a1c67e2019-01-23 18:15:52 -08005280 .adjust_credits = smb2_adjust_credits,
Steve French38107d42012-12-08 22:08:06 -06005281 .get_next_mid = smb2_get_next_mid,
Pavel Shilovskyc781af72019-03-04 14:02:50 -08005282 .revert_current_mid = smb2_revert_current_mid,
Steve French38107d42012-12-08 22:08:06 -06005283 .read_data_offset = smb2_read_data_offset,
5284 .read_data_length = smb2_read_data_length,
5285 .map_error = map_smb2_to_linux_error,
5286 .find_mid = smb2_find_mid,
5287 .check_message = smb2_check_message,
5288 .dump_detail = smb2_dump_detail,
5289 .clear_stats = smb2_clear_stats,
5290 .print_stats = smb2_print_stats,
Steve French769ee6a2013-06-19 14:15:30 -05005291 .dump_share_caps = smb2_dump_share_caps,
Steve French38107d42012-12-08 22:08:06 -06005292 .is_oplock_break = smb2_is_valid_oplock_break,
Sachin Prabhu38bd4902017-03-03 15:41:38 -08005293 .handle_cancelled_mid = smb2_handle_cancelled_mid,
Pavel Shilovsky9bd45402019-10-29 16:51:19 -07005294 .downgrade_oplock = smb3_downgrade_oplock,
Steve French38107d42012-12-08 22:08:06 -06005295 .need_neg = smb2_need_neg,
5296 .negotiate = smb2_negotiate,
Steve French3d621232018-09-25 15:33:47 -05005297 .negotiate_wsize = smb3_negotiate_wsize,
5298 .negotiate_rsize = smb3_negotiate_rsize,
Steve French38107d42012-12-08 22:08:06 -06005299 .sess_setup = SMB2_sess_setup,
5300 .logoff = SMB2_logoff,
5301 .tree_connect = SMB2_tcon,
5302 .tree_disconnect = SMB2_tdis,
Steven Frenchaf6a12e2013-10-09 20:55:53 -05005303 .qfs_tcon = smb3_qfs_tcon,
Steve French38107d42012-12-08 22:08:06 -06005304 .is_path_accessible = smb2_is_path_accessible,
5305 .can_echo = smb2_can_echo,
5306 .echo = SMB2_echo,
5307 .query_path_info = smb2_query_path_info,
Steve French2e4564b2020-10-22 22:03:14 -05005308 /* WSL tags introduced long after smb2.1, enable for SMB3, 3.11 only */
5309 .query_reparse_tag = smb2_query_reparse_tag,
Steve French38107d42012-12-08 22:08:06 -06005310 .get_srv_inum = smb2_get_srv_inum,
5311 .query_file_info = smb2_query_file_info,
5312 .set_path_size = smb2_set_path_size,
5313 .set_file_size = smb2_set_file_size,
5314 .set_file_info = smb2_set_file_info,
Steve French64a5cfa2013-10-14 15:31:32 -05005315 .set_compression = smb2_set_compression,
Steve French38107d42012-12-08 22:08:06 -06005316 .mkdir = smb2_mkdir,
5317 .mkdir_setinfo = smb2_mkdir_setinfo,
5318 .rmdir = smb2_rmdir,
5319 .unlink = smb2_unlink,
5320 .rename = smb2_rename_path,
5321 .create_hardlink = smb2_create_hardlink,
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04005322 .query_symlink = smb2_query_symlink,
Steve Frenchc22870e2014-09-16 07:18:19 -05005323 .query_mf_symlink = smb3_query_mf_symlink,
Steve French5ab97572014-09-15 04:49:28 -05005324 .create_mf_symlink = smb3_create_mf_symlink,
Steve French38107d42012-12-08 22:08:06 -06005325 .open = smb2_open_file,
5326 .set_fid = smb2_set_fid,
5327 .close = smb2_close_file,
Steve French43f8a6a2019-12-02 21:46:54 -06005328 .close_getattr = smb2_close_getattr,
Steve French38107d42012-12-08 22:08:06 -06005329 .flush = smb2_flush_file,
5330 .async_readv = smb2_async_readv,
5331 .async_writev = smb2_async_writev,
5332 .sync_read = smb2_sync_read,
5333 .sync_write = smb2_sync_write,
5334 .query_dir_first = smb2_query_dir_first,
5335 .query_dir_next = smb2_query_dir_next,
5336 .close_dir = smb2_close_dir,
5337 .calc_smb_size = smb2_calc_size,
5338 .is_status_pending = smb2_is_status_pending,
Pavel Shilovsky511c54a2017-07-08 14:32:00 -07005339 .is_session_expired = smb2_is_session_expired,
Steve French38107d42012-12-08 22:08:06 -06005340 .oplock_response = smb2_oplock_response,
5341 .queryfs = smb2_queryfs,
5342 .mand_lock = smb2_mand_lock,
5343 .mand_unlock_range = smb2_unlock_range,
5344 .push_mand_locks = smb2_push_mandatory_locks,
5345 .get_lease_key = smb2_get_lease_key,
5346 .set_lease_key = smb2_set_lease_key,
5347 .new_lease_key = smb2_new_lease_key,
Steve French373512e2015-12-18 13:05:30 -06005348 .generate_signingkey = generate_smb30signingkey,
Steve French38107d42012-12-08 22:08:06 -06005349 .calc_signature = smb3_calc_signature,
Steve Frenchb3152e22015-06-24 03:17:02 -05005350 .set_integrity = smb3_set_integrity,
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04005351 .is_read_op = smb21_is_read_op,
Pavel Shilovsky42873b02013-09-05 21:30:16 +04005352 .set_oplock_level = smb3_set_oplock_level,
Pavel Shilovskyf0473902013-09-04 13:44:05 +04005353 .create_lease_buf = smb3_create_lease_buf,
5354 .parse_lease_buf = smb3_parse_lease_buf,
Sachin Prabhu312bbc52017-04-04 02:12:04 -05005355 .copychunk_range = smb2_copychunk_range,
Steve Frenchca9e7a12015-10-01 21:40:10 -05005356 .duplicate_extents = smb2_duplicate_extents,
Steve Frenchff1c0382013-11-19 23:44:46 -06005357 .validate_negotiate = smb3_validate_negotiate,
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04005358 .wp_retry_size = smb2_wp_retry_size,
Pavel Shilovsky52755802014-08-18 20:49:57 +04005359 .dir_needs_close = smb2_dir_needs_close,
Steve French31742c52014-08-17 08:38:47 -05005360 .fallocate = smb3_fallocate,
Steve French834170c2016-09-30 21:14:26 -05005361 .enum_snapshots = smb3_enum_snapshots,
Steve Frenchd26c2dd2020-02-06 06:00:14 -06005362 .notify = smb3_notify,
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07005363 .init_transform_rq = smb3_init_transform_rq,
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08005364 .is_transform_hdr = smb3_is_transform_hdr,
5365 .receive_transform = smb3_receive_transform,
Aurelien Aptel9d496402017-02-13 16:16:49 +01005366 .get_dfs_refer = smb2_get_dfs_refer,
Sachin Prabhuef65aae2017-01-18 15:35:57 +05305367 .select_sectype = smb2_select_sectype,
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10005368#ifdef CONFIG_CIFS_XATTR
5369 .query_all_EAs = smb2_query_eas,
Ronnie Sahlberg55175542017-08-24 11:24:56 +10005370 .set_EA = smb2_set_ea,
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10005371#endif /* CIFS_XATTR */
Shirish Pargaonkar2f1afe22017-06-22 22:52:05 -05005372 .get_acl = get_smb2_acl,
5373 .get_acl_by_fid = get_smb2_acl_by_fid,
Shirish Pargaonkar366ed842017-06-28 22:37:32 -05005374 .set_acl = set_smb2_acl,
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +10005375 .next_header = smb2_next_header,
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05005376 .ioctl_query_info = smb2_ioctl_query_info,
Aurelien Aptelc847dcc2019-03-14 00:29:17 -05005377 .make_node = smb2_make_node,
Ronnie Sahlberg2f3ebab2019-04-25 16:45:29 +10005378 .fiemap = smb3_fiemap,
Ronnie Sahlbergdece44e2019-05-15 07:17:02 +10005379 .llseek = smb3_llseek,
Rohith Surabattula8e670f72020-09-18 05:37:28 +00005380 .is_status_io_timeout = smb2_is_status_io_timeout,
Rohith Surabattula9e550b02021-02-16 10:40:45 +00005381 .is_network_name_deleted = smb2_is_network_name_deleted,
Steve French1080ef72011-02-24 18:07:19 +00005382};
5383
Steve Frenchaab18932015-06-23 23:37:11 -05005384struct smb_version_operations smb311_operations = {
5385 .compare_fids = smb2_compare_fids,
5386 .setup_request = smb2_setup_request,
5387 .setup_async_request = smb2_setup_async_request,
5388 .check_receive = smb2_check_receive,
5389 .add_credits = smb2_add_credits,
5390 .set_credits = smb2_set_credits,
5391 .get_credits_field = smb2_get_credits_field,
5392 .get_credits = smb2_get_credits,
5393 .wait_mtu_credits = smb2_wait_mtu_credits,
Pavel Shilovsky9a1c67e2019-01-23 18:15:52 -08005394 .adjust_credits = smb2_adjust_credits,
Steve Frenchaab18932015-06-23 23:37:11 -05005395 .get_next_mid = smb2_get_next_mid,
Pavel Shilovskyc781af72019-03-04 14:02:50 -08005396 .revert_current_mid = smb2_revert_current_mid,
Steve Frenchaab18932015-06-23 23:37:11 -05005397 .read_data_offset = smb2_read_data_offset,
5398 .read_data_length = smb2_read_data_length,
5399 .map_error = map_smb2_to_linux_error,
5400 .find_mid = smb2_find_mid,
5401 .check_message = smb2_check_message,
5402 .dump_detail = smb2_dump_detail,
5403 .clear_stats = smb2_clear_stats,
5404 .print_stats = smb2_print_stats,
5405 .dump_share_caps = smb2_dump_share_caps,
5406 .is_oplock_break = smb2_is_valid_oplock_break,
Sachin Prabhu38bd4902017-03-03 15:41:38 -08005407 .handle_cancelled_mid = smb2_handle_cancelled_mid,
Pavel Shilovsky9bd45402019-10-29 16:51:19 -07005408 .downgrade_oplock = smb3_downgrade_oplock,
Steve Frenchaab18932015-06-23 23:37:11 -05005409 .need_neg = smb2_need_neg,
5410 .negotiate = smb2_negotiate,
Steve French3d621232018-09-25 15:33:47 -05005411 .negotiate_wsize = smb3_negotiate_wsize,
5412 .negotiate_rsize = smb3_negotiate_rsize,
Steve Frenchaab18932015-06-23 23:37:11 -05005413 .sess_setup = SMB2_sess_setup,
5414 .logoff = SMB2_logoff,
5415 .tree_connect = SMB2_tcon,
5416 .tree_disconnect = SMB2_tdis,
5417 .qfs_tcon = smb3_qfs_tcon,
5418 .is_path_accessible = smb2_is_path_accessible,
5419 .can_echo = smb2_can_echo,
5420 .echo = SMB2_echo,
5421 .query_path_info = smb2_query_path_info,
Steve French2e4564b2020-10-22 22:03:14 -05005422 .query_reparse_tag = smb2_query_reparse_tag,
Steve Frenchaab18932015-06-23 23:37:11 -05005423 .get_srv_inum = smb2_get_srv_inum,
5424 .query_file_info = smb2_query_file_info,
5425 .set_path_size = smb2_set_path_size,
5426 .set_file_size = smb2_set_file_size,
5427 .set_file_info = smb2_set_file_info,
5428 .set_compression = smb2_set_compression,
5429 .mkdir = smb2_mkdir,
5430 .mkdir_setinfo = smb2_mkdir_setinfo,
Steve Frenchbea851b2018-06-14 21:56:32 -05005431 .posix_mkdir = smb311_posix_mkdir,
Steve Frenchaab18932015-06-23 23:37:11 -05005432 .rmdir = smb2_rmdir,
5433 .unlink = smb2_unlink,
5434 .rename = smb2_rename_path,
5435 .create_hardlink = smb2_create_hardlink,
5436 .query_symlink = smb2_query_symlink,
5437 .query_mf_symlink = smb3_query_mf_symlink,
5438 .create_mf_symlink = smb3_create_mf_symlink,
5439 .open = smb2_open_file,
5440 .set_fid = smb2_set_fid,
5441 .close = smb2_close_file,
Steve French43f8a6a2019-12-02 21:46:54 -06005442 .close_getattr = smb2_close_getattr,
Steve Frenchaab18932015-06-23 23:37:11 -05005443 .flush = smb2_flush_file,
5444 .async_readv = smb2_async_readv,
5445 .async_writev = smb2_async_writev,
5446 .sync_read = smb2_sync_read,
5447 .sync_write = smb2_sync_write,
5448 .query_dir_first = smb2_query_dir_first,
5449 .query_dir_next = smb2_query_dir_next,
5450 .close_dir = smb2_close_dir,
5451 .calc_smb_size = smb2_calc_size,
5452 .is_status_pending = smb2_is_status_pending,
Pavel Shilovsky511c54a2017-07-08 14:32:00 -07005453 .is_session_expired = smb2_is_session_expired,
Steve Frenchaab18932015-06-23 23:37:11 -05005454 .oplock_response = smb2_oplock_response,
Steve French2d304212018-06-24 23:28:12 -05005455 .queryfs = smb311_queryfs,
Steve Frenchaab18932015-06-23 23:37:11 -05005456 .mand_lock = smb2_mand_lock,
5457 .mand_unlock_range = smb2_unlock_range,
5458 .push_mand_locks = smb2_push_mandatory_locks,
5459 .get_lease_key = smb2_get_lease_key,
5460 .set_lease_key = smb2_set_lease_key,
5461 .new_lease_key = smb2_new_lease_key,
Steve French373512e2015-12-18 13:05:30 -06005462 .generate_signingkey = generate_smb311signingkey,
Steve Frenchaab18932015-06-23 23:37:11 -05005463 .calc_signature = smb3_calc_signature,
Steve Frenchb3152e22015-06-24 03:17:02 -05005464 .set_integrity = smb3_set_integrity,
Steve Frenchaab18932015-06-23 23:37:11 -05005465 .is_read_op = smb21_is_read_op,
5466 .set_oplock_level = smb3_set_oplock_level,
5467 .create_lease_buf = smb3_create_lease_buf,
5468 .parse_lease_buf = smb3_parse_lease_buf,
Sachin Prabhu312bbc52017-04-04 02:12:04 -05005469 .copychunk_range = smb2_copychunk_range,
Steve French02b16662015-06-27 21:18:36 -07005470 .duplicate_extents = smb2_duplicate_extents,
Steve Frenchaab18932015-06-23 23:37:11 -05005471/* .validate_negotiate = smb3_validate_negotiate, */ /* not used in 3.11 */
5472 .wp_retry_size = smb2_wp_retry_size,
5473 .dir_needs_close = smb2_dir_needs_close,
5474 .fallocate = smb3_fallocate,
Steve French834170c2016-09-30 21:14:26 -05005475 .enum_snapshots = smb3_enum_snapshots,
Steve Frenchd26c2dd2020-02-06 06:00:14 -06005476 .notify = smb3_notify,
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07005477 .init_transform_rq = smb3_init_transform_rq,
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08005478 .is_transform_hdr = smb3_is_transform_hdr,
5479 .receive_transform = smb3_receive_transform,
Aurelien Aptel9d496402017-02-13 16:16:49 +01005480 .get_dfs_refer = smb2_get_dfs_refer,
Sachin Prabhuef65aae2017-01-18 15:35:57 +05305481 .select_sectype = smb2_select_sectype,
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10005482#ifdef CONFIG_CIFS_XATTR
5483 .query_all_EAs = smb2_query_eas,
Ronnie Sahlberg55175542017-08-24 11:24:56 +10005484 .set_EA = smb2_set_ea,
Ronnie Sahlberg95907fe2017-08-24 11:24:55 +10005485#endif /* CIFS_XATTR */
Ronnie Sahlbergc1777df2018-08-10 11:03:55 +10005486 .get_acl = get_smb2_acl,
5487 .get_acl_by_fid = get_smb2_acl_by_fid,
5488 .set_acl = set_smb2_acl,
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +10005489 .next_header = smb2_next_header,
Ronnie Sahlbergf5b05d62018-10-07 19:19:58 -05005490 .ioctl_query_info = smb2_ioctl_query_info,
Aurelien Aptelc847dcc2019-03-14 00:29:17 -05005491 .make_node = smb2_make_node,
Ronnie Sahlberg2f3ebab2019-04-25 16:45:29 +10005492 .fiemap = smb3_fiemap,
Ronnie Sahlbergdece44e2019-05-15 07:17:02 +10005493 .llseek = smb3_llseek,
Rohith Surabattula8e670f72020-09-18 05:37:28 +00005494 .is_status_io_timeout = smb2_is_status_io_timeout,
Rohith Surabattula9e550b02021-02-16 10:40:45 +00005495 .is_network_name_deleted = smb2_is_network_name_deleted,
Steve Frenchaab18932015-06-23 23:37:11 -05005496};
Steve Frenchaab18932015-06-23 23:37:11 -05005497
Steve Frenchdd446b12012-11-28 23:21:06 -06005498struct smb_version_values smb20_values = {
5499 .version_string = SMB20_VERSION_STRING,
5500 .protocol_id = SMB20_PROT_ID,
5501 .req_capabilities = 0, /* MBZ */
5502 .large_lock_type = 0,
5503 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
5504 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
5505 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10005506 .header_size = sizeof(struct smb2_sync_hdr),
5507 .header_preamble_size = 0,
Steve Frenchdd446b12012-11-28 23:21:06 -06005508 .max_header_size = MAX_SMB2_HDR_SIZE,
5509 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
5510 .lock_cmd = SMB2_LOCK,
5511 .cap_unix = 0,
5512 .cap_nt_find = SMB2_NT_FIND,
5513 .cap_large_files = SMB2_LARGE_FILES,
Jeff Layton502858822013-06-27 12:45:00 -04005514 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5515 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04005516 .create_lease_size = sizeof(struct create_lease),
Steve Frenchdd446b12012-11-28 23:21:06 -06005517};
5518
Steve French1080ef72011-02-24 18:07:19 +00005519struct smb_version_values smb21_values = {
5520 .version_string = SMB21_VERSION_STRING,
Steve Frenche4aa25e2012-10-01 12:26:22 -05005521 .protocol_id = SMB21_PROT_ID,
5522 .req_capabilities = 0, /* MBZ on negotiate req until SMB3 dialect */
5523 .large_lock_type = 0,
5524 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
5525 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
5526 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10005527 .header_size = sizeof(struct smb2_sync_hdr),
5528 .header_preamble_size = 0,
Steve Frenche4aa25e2012-10-01 12:26:22 -05005529 .max_header_size = MAX_SMB2_HDR_SIZE,
5530 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
5531 .lock_cmd = SMB2_LOCK,
5532 .cap_unix = 0,
5533 .cap_nt_find = SMB2_NT_FIND,
5534 .cap_large_files = SMB2_LARGE_FILES,
Jeff Layton502858822013-06-27 12:45:00 -04005535 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5536 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04005537 .create_lease_size = sizeof(struct create_lease),
Steve Frenche4aa25e2012-10-01 12:26:22 -05005538};
5539
Steve French9764c022017-09-17 10:41:35 -05005540struct smb_version_values smb3any_values = {
5541 .version_string = SMB3ANY_VERSION_STRING,
5542 .protocol_id = SMB302_PROT_ID, /* doesn't matter, send protocol array */
Steve Frenchf8015682018-08-31 15:12:10 -05005543 .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 -05005544 .large_lock_type = 0,
5545 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
5546 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
5547 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10005548 .header_size = sizeof(struct smb2_sync_hdr),
5549 .header_preamble_size = 0,
Steve French9764c022017-09-17 10:41:35 -05005550 .max_header_size = MAX_SMB2_HDR_SIZE,
5551 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
5552 .lock_cmd = SMB2_LOCK,
5553 .cap_unix = 0,
5554 .cap_nt_find = SMB2_NT_FIND,
5555 .cap_large_files = SMB2_LARGE_FILES,
5556 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5557 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5558 .create_lease_size = sizeof(struct create_lease_v2),
5559};
5560
5561struct smb_version_values smbdefault_values = {
5562 .version_string = SMBDEFAULT_VERSION_STRING,
5563 .protocol_id = SMB302_PROT_ID, /* doesn't matter, send protocol array */
Steve Frenchf8015682018-08-31 15:12:10 -05005564 .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 -05005565 .large_lock_type = 0,
5566 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
5567 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
5568 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10005569 .header_size = sizeof(struct smb2_sync_hdr),
5570 .header_preamble_size = 0,
Steve French9764c022017-09-17 10:41:35 -05005571 .max_header_size = MAX_SMB2_HDR_SIZE,
5572 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
5573 .lock_cmd = SMB2_LOCK,
5574 .cap_unix = 0,
5575 .cap_nt_find = SMB2_NT_FIND,
5576 .cap_large_files = SMB2_LARGE_FILES,
5577 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5578 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5579 .create_lease_size = sizeof(struct create_lease_v2),
5580};
5581
Steve Frenche4aa25e2012-10-01 12:26:22 -05005582struct smb_version_values smb30_values = {
5583 .version_string = SMB30_VERSION_STRING,
5584 .protocol_id = SMB30_PROT_ID,
Steve Frenchf8015682018-08-31 15:12:10 -05005585 .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 -07005586 .large_lock_type = 0,
5587 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
5588 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
5589 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10005590 .header_size = sizeof(struct smb2_sync_hdr),
5591 .header_preamble_size = 0,
Pavel Shilovsky093b2bd2011-06-08 15:51:07 +04005592 .max_header_size = MAX_SMB2_HDR_SIZE,
Pavel Shilovsky09a47072012-09-18 16:20:29 -07005593 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +04005594 .lock_cmd = SMB2_LOCK,
Pavel Shilovsky29e20f92012-07-13 13:58:14 +04005595 .cap_unix = 0,
5596 .cap_nt_find = SMB2_NT_FIND,
5597 .cap_large_files = SMB2_LARGE_FILES,
Jeff Layton502858822013-06-27 12:45:00 -04005598 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5599 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
Pavel Shilovskyf0473902013-09-04 13:44:05 +04005600 .create_lease_size = sizeof(struct create_lease_v2),
Steve French1080ef72011-02-24 18:07:19 +00005601};
Steve French20b6d8b2013-06-12 22:48:41 -05005602
5603struct smb_version_values smb302_values = {
5604 .version_string = SMB302_VERSION_STRING,
5605 .protocol_id = SMB302_PROT_ID,
Steve Frenchf8015682018-08-31 15:12:10 -05005606 .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 -05005607 .large_lock_type = 0,
5608 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
5609 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
5610 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10005611 .header_size = sizeof(struct smb2_sync_hdr),
5612 .header_preamble_size = 0,
Steve French20b6d8b2013-06-12 22:48:41 -05005613 .max_header_size = MAX_SMB2_HDR_SIZE,
5614 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
5615 .lock_cmd = SMB2_LOCK,
5616 .cap_unix = 0,
5617 .cap_nt_find = SMB2_NT_FIND,
5618 .cap_large_files = SMB2_LARGE_FILES,
Jeff Layton502858822013-06-27 12:45:00 -04005619 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5620 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
Pavel Shilovskyf0473902013-09-04 13:44:05 +04005621 .create_lease_size = sizeof(struct create_lease_v2),
Steve French20b6d8b2013-06-12 22:48:41 -05005622};
Steve French5f7fbf72014-12-17 22:52:58 -06005623
Steve French5f7fbf72014-12-17 22:52:58 -06005624struct smb_version_values smb311_values = {
5625 .version_string = SMB311_VERSION_STRING,
5626 .protocol_id = SMB311_PROT_ID,
Steve Frenchf8015682018-08-31 15:12:10 -05005627 .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 -06005628 .large_lock_type = 0,
5629 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
5630 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
5631 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10005632 .header_size = sizeof(struct smb2_sync_hdr),
5633 .header_preamble_size = 0,
Steve French5f7fbf72014-12-17 22:52:58 -06005634 .max_header_size = MAX_SMB2_HDR_SIZE,
5635 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
5636 .lock_cmd = SMB2_LOCK,
5637 .cap_unix = 0,
5638 .cap_nt_find = SMB2_NT_FIND,
5639 .cap_large_files = SMB2_LARGE_FILES,
5640 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5641 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5642 .create_lease_size = sizeof(struct create_lease_v2),
5643};