blob: a44b4dbe4aaec9985d40e2c39121ae5834fe1723 [file] [log] [blame]
Steve French1080ef72011-02-24 18:07:19 +00001/*
2 * SMB2 version specific operations
3 *
4 * Copyright (c) 2012, Jeff Layton <jlayton@redhat.com>
5 *
6 * This library is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License v2 as published
8 * by the Free Software Foundation.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
13 * the GNU Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
Pavel Shilovsky3a3bab52012-09-18 16:20:28 -070020#include <linux/pagemap.h>
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -070021#include <linux/vfs.h>
Steve Frenchf29ebb42014-07-19 21:44:58 -050022#include <linux/falloc.h>
Pavel Shilovsky026e93d2016-11-03 16:47:37 -070023#include <linux/scatterlist.h>
24#include <crypto/aead.h>
Steve French1080ef72011-02-24 18:07:19 +000025#include "cifsglob.h"
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +040026#include "smb2pdu.h"
27#include "smb2proto.h"
Pavel Shilovsky28ea5292012-05-23 16:18:00 +040028#include "cifsproto.h"
29#include "cifs_debug.h"
Pavel Shilovskyb42bf882013-08-14 19:25:21 +040030#include "cifs_unicode.h"
Pavel Shilovsky2e44b282012-09-18 16:20:33 -070031#include "smb2status.h"
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -070032#include "smb2glob.h"
Steve French834170c2016-09-30 21:14:26 -050033#include "cifs_ioctl.h"
Pavel Shilovsky28ea5292012-05-23 16:18:00 +040034
35static int
36change_conf(struct TCP_Server_Info *server)
37{
38 server->credits += server->echo_credits + server->oplock_credits;
39 server->oplock_credits = server->echo_credits = 0;
40 switch (server->credits) {
41 case 0:
42 return -1;
43 case 1:
44 server->echoes = false;
45 server->oplocks = false;
Joe Perchesf96637b2013-05-04 22:12:25 -050046 cifs_dbg(VFS, "disabling echoes and oplocks\n");
Pavel Shilovsky28ea5292012-05-23 16:18:00 +040047 break;
48 case 2:
49 server->echoes = true;
50 server->oplocks = false;
51 server->echo_credits = 1;
Joe Perchesf96637b2013-05-04 22:12:25 -050052 cifs_dbg(FYI, "disabling oplocks\n");
Pavel Shilovsky28ea5292012-05-23 16:18:00 +040053 break;
54 default:
55 server->echoes = true;
Steve Frenche0ddde92015-09-22 09:29:38 -050056 if (enable_oplocks) {
57 server->oplocks = true;
58 server->oplock_credits = 1;
59 } else
60 server->oplocks = false;
61
Pavel Shilovsky28ea5292012-05-23 16:18:00 +040062 server->echo_credits = 1;
Pavel Shilovsky28ea5292012-05-23 16:18:00 +040063 }
64 server->credits -= server->echo_credits + server->oplock_credits;
65 return 0;
66}
67
68static void
69smb2_add_credits(struct TCP_Server_Info *server, const unsigned int add,
70 const int optype)
71{
72 int *val, rc = 0;
73 spin_lock(&server->req_lock);
74 val = server->ops->get_credits_field(server, optype);
75 *val += add;
Steve French141891f2016-09-23 00:44:16 -050076 if (*val > 65000) {
77 *val = 65000; /* Don't get near 64K credits, avoid srv bugs */
78 printk_once(KERN_WARNING "server overflowed SMB3 credits\n");
79 }
Pavel Shilovsky28ea5292012-05-23 16:18:00 +040080 server->in_flight--;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +040081 if (server->in_flight == 0 && (optype & CIFS_OP_MASK) != CIFS_NEG_OP)
Pavel Shilovsky28ea5292012-05-23 16:18:00 +040082 rc = change_conf(server);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -070083 /*
84 * Sometimes server returns 0 credits on oplock break ack - we need to
85 * rebalance credits in this case.
86 */
87 else if (server->in_flight > 0 && server->oplock_credits == 0 &&
88 server->oplocks) {
89 if (server->credits > 1) {
90 server->credits--;
91 server->oplock_credits++;
92 }
93 }
Pavel Shilovsky28ea5292012-05-23 16:18:00 +040094 spin_unlock(&server->req_lock);
95 wake_up(&server->request_q);
96 if (rc)
97 cifs_reconnect(server);
98}
99
100static void
101smb2_set_credits(struct TCP_Server_Info *server, const int val)
102{
103 spin_lock(&server->req_lock);
104 server->credits = val;
105 spin_unlock(&server->req_lock);
106}
107
108static int *
109smb2_get_credits_field(struct TCP_Server_Info *server, const int optype)
110{
111 switch (optype) {
112 case CIFS_ECHO_OP:
113 return &server->echo_credits;
114 case CIFS_OBREAK_OP:
115 return &server->oplock_credits;
116 default:
117 return &server->credits;
118 }
119}
120
121static unsigned int
122smb2_get_credits(struct mid_q_entry *mid)
123{
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700124 struct smb2_sync_hdr *shdr = get_sync_hdr(mid->resp_buf);
125
126 return le16_to_cpu(shdr->CreditRequest);
Pavel Shilovsky28ea5292012-05-23 16:18:00 +0400127}
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400128
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +0400129static int
130smb2_wait_mtu_credits(struct TCP_Server_Info *server, unsigned int size,
131 unsigned int *num, unsigned int *credits)
132{
133 int rc = 0;
134 unsigned int scredits;
135
136 spin_lock(&server->req_lock);
137 while (1) {
138 if (server->credits <= 0) {
139 spin_unlock(&server->req_lock);
140 cifs_num_waiters_inc(server);
141 rc = wait_event_killable(server->request_q,
142 has_credits(server, &server->credits));
143 cifs_num_waiters_dec(server);
144 if (rc)
145 return rc;
146 spin_lock(&server->req_lock);
147 } else {
148 if (server->tcpStatus == CifsExiting) {
149 spin_unlock(&server->req_lock);
150 return -ENOENT;
151 }
152
153 scredits = server->credits;
154 /* can deadlock with reopen */
155 if (scredits == 1) {
156 *num = SMB2_MAX_BUFFER_SIZE;
157 *credits = 0;
158 break;
159 }
160
161 /* leave one credit for a possible reopen */
162 scredits--;
163 *num = min_t(unsigned int, size,
164 scredits * SMB2_MAX_BUFFER_SIZE);
165
166 *credits = DIV_ROUND_UP(*num, SMB2_MAX_BUFFER_SIZE);
167 server->credits -= *credits;
168 server->in_flight++;
169 break;
170 }
171 }
172 spin_unlock(&server->req_lock);
173 return rc;
174}
175
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400176static __u64
177smb2_get_next_mid(struct TCP_Server_Info *server)
178{
179 __u64 mid;
180 /* for SMB2 we need the current value */
181 spin_lock(&GlobalMid_Lock);
182 mid = server->CurrentMid++;
183 spin_unlock(&GlobalMid_Lock);
184 return mid;
185}
Steve French1080ef72011-02-24 18:07:19 +0000186
Pavel Shilovsky093b2bd2011-06-08 15:51:07 +0400187static struct mid_q_entry *
188smb2_find_mid(struct TCP_Server_Info *server, char *buf)
189{
190 struct mid_q_entry *mid;
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700191 struct smb2_sync_hdr *shdr = get_sync_hdr(buf);
192 __u64 wire_mid = le64_to_cpu(shdr->MessageId);
Pavel Shilovsky093b2bd2011-06-08 15:51:07 +0400193
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700194 if (shdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM) {
Steve French373512e2015-12-18 13:05:30 -0600195 cifs_dbg(VFS, "encrypted frame parsing not supported yet");
196 return NULL;
197 }
198
Pavel Shilovsky093b2bd2011-06-08 15:51:07 +0400199 spin_lock(&GlobalMid_Lock);
200 list_for_each_entry(mid, &server->pending_mid_q, qhead) {
Sachin Prabhu9235d092014-12-09 17:37:00 +0000201 if ((mid->mid == wire_mid) &&
Pavel Shilovsky093b2bd2011-06-08 15:51:07 +0400202 (mid->mid_state == MID_REQUEST_SUBMITTED) &&
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700203 (mid->command == shdr->Command)) {
Pavel Shilovsky093b2bd2011-06-08 15:51:07 +0400204 spin_unlock(&GlobalMid_Lock);
205 return mid;
206 }
207 }
208 spin_unlock(&GlobalMid_Lock);
209 return NULL;
210}
211
212static void
213smb2_dump_detail(void *buf)
214{
215#ifdef CONFIG_CIFS_DEBUG2
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700216 struct smb2_sync_hdr *shdr = get_sync_hdr(buf);
Pavel Shilovsky093b2bd2011-06-08 15:51:07 +0400217
Joe Perchesf96637b2013-05-04 22:12:25 -0500218 cifs_dbg(VFS, "Cmd: %d Err: 0x%x Flags: 0x%x Mid: %llu Pid: %d\n",
Pavel Shilovsky31473fc2016-10-24 15:33:04 -0700219 shdr->Command, shdr->Status, shdr->Flags, shdr->MessageId,
220 shdr->ProcessId);
221 cifs_dbg(VFS, "smb buf %p len %u\n", buf, smb2_calc_size(buf));
Pavel Shilovsky093b2bd2011-06-08 15:51:07 +0400222#endif
223}
224
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400225static bool
226smb2_need_neg(struct TCP_Server_Info *server)
227{
228 return server->max_read == 0;
229}
230
231static int
232smb2_negotiate(const unsigned int xid, struct cifs_ses *ses)
233{
234 int rc;
235 ses->server->CurrentMid = 0;
236 rc = SMB2_negotiate(xid, ses);
237 /* BB we probably don't need to retry with modern servers */
238 if (rc == -EAGAIN)
239 rc = -EHOSTDOWN;
240 return rc;
241}
242
Pavel Shilovsky3a3bab52012-09-18 16:20:28 -0700243static unsigned int
244smb2_negotiate_wsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
245{
246 struct TCP_Server_Info *server = tcon->ses->server;
247 unsigned int wsize;
248
249 /* start with specified wsize, or default */
250 wsize = volume_info->wsize ? volume_info->wsize : CIFS_DEFAULT_IOSIZE;
251 wsize = min_t(unsigned int, wsize, server->max_write);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +0400252
253 if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
254 wsize = min_t(unsigned int, wsize, SMB2_MAX_BUFFER_SIZE);
Pavel Shilovsky3a3bab52012-09-18 16:20:28 -0700255
Pavel Shilovsky3a3bab52012-09-18 16:20:28 -0700256 return wsize;
257}
258
259static unsigned int
260smb2_negotiate_rsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
261{
262 struct TCP_Server_Info *server = tcon->ses->server;
263 unsigned int rsize;
264
265 /* start with specified rsize, or default */
266 rsize = volume_info->rsize ? volume_info->rsize : CIFS_DEFAULT_IOSIZE;
267 rsize = min_t(unsigned int, rsize, server->max_read);
Pavel Shilovskybed9da02014-06-25 11:28:57 +0400268
269 if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
270 rsize = min_t(unsigned int, rsize, SMB2_MAX_BUFFER_SIZE);
Pavel Shilovsky3a3bab52012-09-18 16:20:28 -0700271
Pavel Shilovsky3a3bab52012-09-18 16:20:28 -0700272 return rsize;
273}
274
Steve Frenchc481e9f2013-10-14 01:21:53 -0500275#ifdef CONFIG_CIFS_STATS2
276static int
277SMB3_request_interfaces(const unsigned int xid, struct cifs_tcon *tcon)
278{
279 int rc;
280 unsigned int ret_data_len = 0;
281 struct network_interface_info_ioctl_rsp *out_buf;
282
283 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
284 FSCTL_QUERY_NETWORK_INTERFACE_INFO, true /* is_fsctl */,
285 NULL /* no data input */, 0 /* no data input */,
286 (char **)&out_buf, &ret_data_len);
Steve French9ffc5412014-10-16 15:13:14 -0500287 if (rc != 0)
288 cifs_dbg(VFS, "error %d on ioctl to get interface list\n", rc);
289 else if (ret_data_len < sizeof(struct network_interface_info_ioctl_rsp)) {
290 cifs_dbg(VFS, "server returned bad net interface info buf\n");
291 rc = -EINVAL;
292 } else {
Steve Frenchc481e9f2013-10-14 01:21:53 -0500293 /* Dump info on first interface */
294 cifs_dbg(FYI, "Adapter Capability 0x%x\t",
295 le32_to_cpu(out_buf->Capability));
296 cifs_dbg(FYI, "Link Speed %lld\n",
297 le64_to_cpu(out_buf->LinkSpeed));
Steve French9ffc5412014-10-16 15:13:14 -0500298 }
Steve French24df1482016-09-29 04:20:23 -0500299 kfree(out_buf);
Steve Frenchc481e9f2013-10-14 01:21:53 -0500300 return rc;
301}
302#endif /* STATS2 */
303
Steve French34f62642013-10-09 02:07:00 -0500304static void
Steven Frenchaf6a12e2013-10-09 20:55:53 -0500305smb3_qfs_tcon(const unsigned int xid, struct cifs_tcon *tcon)
306{
307 int rc;
308 __le16 srch_path = 0; /* Null - open root of share */
309 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
310 struct cifs_open_parms oparms;
311 struct cifs_fid fid;
312
313 oparms.tcon = tcon;
314 oparms.desired_access = FILE_READ_ATTRIBUTES;
315 oparms.disposition = FILE_OPEN;
316 oparms.create_options = 0;
317 oparms.fid = &fid;
318 oparms.reconnect = false;
319
320 rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL);
321 if (rc)
322 return;
323
Steve Frenchc481e9f2013-10-14 01:21:53 -0500324#ifdef CONFIG_CIFS_STATS2
325 SMB3_request_interfaces(xid, tcon);
326#endif /* STATS2 */
327
Steven Frenchaf6a12e2013-10-09 20:55:53 -0500328 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
329 FS_ATTRIBUTE_INFORMATION);
330 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
331 FS_DEVICE_INFORMATION);
332 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
333 FS_SECTOR_SIZE_INFORMATION); /* SMB3 specific */
334 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
335 return;
336}
337
338static void
Steve French34f62642013-10-09 02:07:00 -0500339smb2_qfs_tcon(const unsigned int xid, struct cifs_tcon *tcon)
340{
341 int rc;
342 __le16 srch_path = 0; /* Null - open root of share */
343 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
344 struct cifs_open_parms oparms;
345 struct cifs_fid fid;
346
347 oparms.tcon = tcon;
348 oparms.desired_access = FILE_READ_ATTRIBUTES;
349 oparms.disposition = FILE_OPEN;
350 oparms.create_options = 0;
351 oparms.fid = &fid;
352 oparms.reconnect = false;
353
354 rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL);
355 if (rc)
356 return;
357
Steven French21671142013-10-09 13:36:35 -0500358 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
359 FS_ATTRIBUTE_INFORMATION);
360 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
361 FS_DEVICE_INFORMATION);
Steve French34f62642013-10-09 02:07:00 -0500362 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
363 return;
364}
365
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +0400366static int
367smb2_is_path_accessible(const unsigned int xid, struct cifs_tcon *tcon,
368 struct cifs_sb_info *cifs_sb, const char *full_path)
369{
370 int rc;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +0400371 __le16 *utf16_path;
Pavel Shilovsky2e44b282012-09-18 16:20:33 -0700372 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
Pavel Shilovsky064f6042013-07-09 18:20:30 +0400373 struct cifs_open_parms oparms;
374 struct cifs_fid fid;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +0400375
376 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
377 if (!utf16_path)
378 return -ENOMEM;
379
Pavel Shilovsky064f6042013-07-09 18:20:30 +0400380 oparms.tcon = tcon;
381 oparms.desired_access = FILE_READ_ATTRIBUTES;
382 oparms.disposition = FILE_OPEN;
383 oparms.create_options = 0;
384 oparms.fid = &fid;
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +0400385 oparms.reconnect = false;
Pavel Shilovsky064f6042013-07-09 18:20:30 +0400386
Pavel Shilovskyb42bf882013-08-14 19:25:21 +0400387 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +0400388 if (rc) {
389 kfree(utf16_path);
390 return rc;
391 }
392
Pavel Shilovsky064f6042013-07-09 18:20:30 +0400393 rc = SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +0400394 kfree(utf16_path);
395 return rc;
396}
397
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +0400398static int
399smb2_get_srv_inum(const unsigned int xid, struct cifs_tcon *tcon,
400 struct cifs_sb_info *cifs_sb, const char *full_path,
401 u64 *uniqueid, FILE_ALL_INFO *data)
402{
403 *uniqueid = le64_to_cpu(data->IndexNumber);
404 return 0;
405}
406
Pavel Shilovskyb7546bc2012-09-18 16:20:27 -0700407static int
408smb2_query_file_info(const unsigned int xid, struct cifs_tcon *tcon,
409 struct cifs_fid *fid, FILE_ALL_INFO *data)
410{
411 int rc;
412 struct smb2_file_all_info *smb2_data;
413
Pavel Shilovsky1bbe4992014-08-22 13:32:11 +0400414 smb2_data = kzalloc(sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
Pavel Shilovskyb7546bc2012-09-18 16:20:27 -0700415 GFP_KERNEL);
416 if (smb2_data == NULL)
417 return -ENOMEM;
418
419 rc = SMB2_query_info(xid, tcon, fid->persistent_fid, fid->volatile_fid,
420 smb2_data);
421 if (!rc)
422 move_smb2_info_to_cifs(data, smb2_data);
423 kfree(smb2_data);
424 return rc;
425}
426
Pavel Shilovsky9094fad2012-07-12 18:30:44 +0400427static bool
428smb2_can_echo(struct TCP_Server_Info *server)
429{
430 return server->echoes;
431}
432
Pavel Shilovskyd60622e2012-05-28 15:19:39 +0400433static void
434smb2_clear_stats(struct cifs_tcon *tcon)
435{
436#ifdef CONFIG_CIFS_STATS
437 int i;
438 for (i = 0; i < NUMBER_OF_SMB2_COMMANDS; i++) {
439 atomic_set(&tcon->stats.smb2_stats.smb2_com_sent[i], 0);
440 atomic_set(&tcon->stats.smb2_stats.smb2_com_failed[i], 0);
441 }
442#endif
443}
444
445static void
Steve French769ee6a2013-06-19 14:15:30 -0500446smb2_dump_share_caps(struct seq_file *m, struct cifs_tcon *tcon)
447{
448 seq_puts(m, "\n\tShare Capabilities:");
449 if (tcon->capabilities & SMB2_SHARE_CAP_DFS)
450 seq_puts(m, " DFS,");
451 if (tcon->capabilities & SMB2_SHARE_CAP_CONTINUOUS_AVAILABILITY)
452 seq_puts(m, " CONTINUOUS AVAILABILITY,");
453 if (tcon->capabilities & SMB2_SHARE_CAP_SCALEOUT)
454 seq_puts(m, " SCALEOUT,");
455 if (tcon->capabilities & SMB2_SHARE_CAP_CLUSTER)
456 seq_puts(m, " CLUSTER,");
457 if (tcon->capabilities & SMB2_SHARE_CAP_ASYMMETRIC)
458 seq_puts(m, " ASYMMETRIC,");
459 if (tcon->capabilities == 0)
460 seq_puts(m, " None");
Steven Frenchaf6a12e2013-10-09 20:55:53 -0500461 if (tcon->ss_flags & SSINFO_FLAGS_ALIGNED_DEVICE)
462 seq_puts(m, " Aligned,");
463 if (tcon->ss_flags & SSINFO_FLAGS_PARTITION_ALIGNED_ON_DEVICE)
464 seq_puts(m, " Partition Aligned,");
465 if (tcon->ss_flags & SSINFO_FLAGS_NO_SEEK_PENALTY)
466 seq_puts(m, " SSD,");
467 if (tcon->ss_flags & SSINFO_FLAGS_TRIM_ENABLED)
468 seq_puts(m, " TRIM-support,");
469
Steve French769ee6a2013-06-19 14:15:30 -0500470 seq_printf(m, "\tShare Flags: 0x%x", tcon->share_flags);
Steven Frenchaf6a12e2013-10-09 20:55:53 -0500471 if (tcon->perf_sector_size)
472 seq_printf(m, "\tOptimal sector size: 0x%x",
473 tcon->perf_sector_size);
Steve French769ee6a2013-06-19 14:15:30 -0500474}
475
476static void
Pavel Shilovskyd60622e2012-05-28 15:19:39 +0400477smb2_print_stats(struct seq_file *m, struct cifs_tcon *tcon)
478{
479#ifdef CONFIG_CIFS_STATS
480 atomic_t *sent = tcon->stats.smb2_stats.smb2_com_sent;
481 atomic_t *failed = tcon->stats.smb2_stats.smb2_com_failed;
482 seq_printf(m, "\nNegotiates: %d sent %d failed",
483 atomic_read(&sent[SMB2_NEGOTIATE_HE]),
484 atomic_read(&failed[SMB2_NEGOTIATE_HE]));
485 seq_printf(m, "\nSessionSetups: %d sent %d failed",
486 atomic_read(&sent[SMB2_SESSION_SETUP_HE]),
487 atomic_read(&failed[SMB2_SESSION_SETUP_HE]));
Pavel Shilovskyd60622e2012-05-28 15:19:39 +0400488 seq_printf(m, "\nLogoffs: %d sent %d failed",
489 atomic_read(&sent[SMB2_LOGOFF_HE]),
490 atomic_read(&failed[SMB2_LOGOFF_HE]));
491 seq_printf(m, "\nTreeConnects: %d sent %d failed",
492 atomic_read(&sent[SMB2_TREE_CONNECT_HE]),
493 atomic_read(&failed[SMB2_TREE_CONNECT_HE]));
494 seq_printf(m, "\nTreeDisconnects: %d sent %d failed",
495 atomic_read(&sent[SMB2_TREE_DISCONNECT_HE]),
496 atomic_read(&failed[SMB2_TREE_DISCONNECT_HE]));
497 seq_printf(m, "\nCreates: %d sent %d failed",
498 atomic_read(&sent[SMB2_CREATE_HE]),
499 atomic_read(&failed[SMB2_CREATE_HE]));
500 seq_printf(m, "\nCloses: %d sent %d failed",
501 atomic_read(&sent[SMB2_CLOSE_HE]),
502 atomic_read(&failed[SMB2_CLOSE_HE]));
503 seq_printf(m, "\nFlushes: %d sent %d failed",
504 atomic_read(&sent[SMB2_FLUSH_HE]),
505 atomic_read(&failed[SMB2_FLUSH_HE]));
506 seq_printf(m, "\nReads: %d sent %d failed",
507 atomic_read(&sent[SMB2_READ_HE]),
508 atomic_read(&failed[SMB2_READ_HE]));
509 seq_printf(m, "\nWrites: %d sent %d failed",
510 atomic_read(&sent[SMB2_WRITE_HE]),
511 atomic_read(&failed[SMB2_WRITE_HE]));
512 seq_printf(m, "\nLocks: %d sent %d failed",
513 atomic_read(&sent[SMB2_LOCK_HE]),
514 atomic_read(&failed[SMB2_LOCK_HE]));
515 seq_printf(m, "\nIOCTLs: %d sent %d failed",
516 atomic_read(&sent[SMB2_IOCTL_HE]),
517 atomic_read(&failed[SMB2_IOCTL_HE]));
518 seq_printf(m, "\nCancels: %d sent %d failed",
519 atomic_read(&sent[SMB2_CANCEL_HE]),
520 atomic_read(&failed[SMB2_CANCEL_HE]));
521 seq_printf(m, "\nEchos: %d sent %d failed",
522 atomic_read(&sent[SMB2_ECHO_HE]),
523 atomic_read(&failed[SMB2_ECHO_HE]));
524 seq_printf(m, "\nQueryDirectories: %d sent %d failed",
525 atomic_read(&sent[SMB2_QUERY_DIRECTORY_HE]),
526 atomic_read(&failed[SMB2_QUERY_DIRECTORY_HE]));
527 seq_printf(m, "\nChangeNotifies: %d sent %d failed",
528 atomic_read(&sent[SMB2_CHANGE_NOTIFY_HE]),
529 atomic_read(&failed[SMB2_CHANGE_NOTIFY_HE]));
530 seq_printf(m, "\nQueryInfos: %d sent %d failed",
531 atomic_read(&sent[SMB2_QUERY_INFO_HE]),
532 atomic_read(&failed[SMB2_QUERY_INFO_HE]));
533 seq_printf(m, "\nSetInfos: %d sent %d failed",
534 atomic_read(&sent[SMB2_SET_INFO_HE]),
535 atomic_read(&failed[SMB2_SET_INFO_HE]));
536 seq_printf(m, "\nOplockBreaks: %d sent %d failed",
537 atomic_read(&sent[SMB2_OPLOCK_BREAK_HE]),
538 atomic_read(&failed[SMB2_OPLOCK_BREAK_HE]));
539#endif
540}
541
Pavel Shilovskyf0df7372012-09-18 16:20:26 -0700542static void
543smb2_set_fid(struct cifsFileInfo *cfile, struct cifs_fid *fid, __u32 oplock)
544{
David Howells2b0143b2015-03-17 22:25:59 +0000545 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
Pavel Shilovsky53ef1012013-09-05 16:11:28 +0400546 struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server;
547
Pavel Shilovskyf0df7372012-09-18 16:20:26 -0700548 cfile->fid.persistent_fid = fid->persistent_fid;
549 cfile->fid.volatile_fid = fid->volatile_fid;
Pavel Shilovsky42873b02013-09-05 21:30:16 +0400550 server->ops->set_oplock_level(cinode, oplock, fid->epoch,
551 &fid->purge_cache);
Pavel Shilovsky18cceb62013-09-05 13:01:06 +0400552 cinode->can_cache_brlcks = CIFS_CACHE_WRITE(cinode);
Aurelien Aptel94f87372016-09-22 07:38:50 +0200553 memcpy(cfile->fid.create_guid, fid->create_guid, 16);
Pavel Shilovskyf0df7372012-09-18 16:20:26 -0700554}
555
Pavel Shilovsky760ad0c2012-09-25 11:00:07 +0400556static void
Pavel Shilovskyf0df7372012-09-18 16:20:26 -0700557smb2_close_file(const unsigned int xid, struct cifs_tcon *tcon,
558 struct cifs_fid *fid)
559{
Pavel Shilovsky760ad0c2012-09-25 11:00:07 +0400560 SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
Pavel Shilovskyf0df7372012-09-18 16:20:26 -0700561}
562
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -0700563static int
Steve French41c13582013-11-14 00:05:36 -0600564SMB2_request_res_key(const unsigned int xid, struct cifs_tcon *tcon,
565 u64 persistent_fid, u64 volatile_fid,
566 struct copychunk_ioctl *pcchunk)
567{
568 int rc;
569 unsigned int ret_data_len;
570 struct resume_key_req *res_key;
571
572 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
573 FSCTL_SRV_REQUEST_RESUME_KEY, true /* is_fsctl */,
574 NULL, 0 /* no input */,
575 (char **)&res_key, &ret_data_len);
576
577 if (rc) {
578 cifs_dbg(VFS, "refcpy ioctl error %d getting resume key\n", rc);
579 goto req_res_key_exit;
580 }
581 if (ret_data_len < sizeof(struct resume_key_req)) {
582 cifs_dbg(VFS, "Invalid refcopy resume key length\n");
583 rc = -EINVAL;
584 goto req_res_key_exit;
585 }
586 memcpy(pcchunk->SourceKey, res_key->ResumeKey, COPY_CHUNK_RES_KEY_SIZE);
587
588req_res_key_exit:
589 kfree(res_key);
590 return rc;
591}
592
593static int
594smb2_clone_range(const unsigned int xid,
595 struct cifsFileInfo *srcfile,
596 struct cifsFileInfo *trgtfile, u64 src_off,
597 u64 len, u64 dest_off)
598{
599 int rc;
600 unsigned int ret_data_len;
601 struct copychunk_ioctl *pcchunk;
Steve French9bf0c9c2013-11-16 18:05:28 -0600602 struct copychunk_ioctl_rsp *retbuf = NULL;
603 struct cifs_tcon *tcon;
604 int chunks_copied = 0;
605 bool chunk_sizes_updated = false;
Steve French41c13582013-11-14 00:05:36 -0600606
607 pcchunk = kmalloc(sizeof(struct copychunk_ioctl), GFP_KERNEL);
608
609 if (pcchunk == NULL)
610 return -ENOMEM;
611
612 cifs_dbg(FYI, "in smb2_clone_range - about to call request res key\n");
613 /* Request a key from the server to identify the source of the copy */
614 rc = SMB2_request_res_key(xid, tlink_tcon(srcfile->tlink),
615 srcfile->fid.persistent_fid,
616 srcfile->fid.volatile_fid, pcchunk);
617
618 /* Note: request_res_key sets res_key null only if rc !=0 */
619 if (rc)
Steve French9bf0c9c2013-11-16 18:05:28 -0600620 goto cchunk_out;
Steve French41c13582013-11-14 00:05:36 -0600621
622 /* For now array only one chunk long, will make more flexible later */
Fabian Frederickbc09d142014-12-10 15:41:15 -0800623 pcchunk->ChunkCount = cpu_to_le32(1);
Steve French41c13582013-11-14 00:05:36 -0600624 pcchunk->Reserved = 0;
Steve French41c13582013-11-14 00:05:36 -0600625 pcchunk->Reserved2 = 0;
626
Steve French9bf0c9c2013-11-16 18:05:28 -0600627 tcon = tlink_tcon(trgtfile->tlink);
628
629 while (len > 0) {
630 pcchunk->SourceOffset = cpu_to_le64(src_off);
631 pcchunk->TargetOffset = cpu_to_le64(dest_off);
632 pcchunk->Length =
633 cpu_to_le32(min_t(u32, len, tcon->max_bytes_chunk));
634
635 /* Request server copy to target from src identified by key */
636 rc = SMB2_ioctl(xid, tcon, trgtfile->fid.persistent_fid,
Steve French41c13582013-11-14 00:05:36 -0600637 trgtfile->fid.volatile_fid, FSCTL_SRV_COPYCHUNK_WRITE,
638 true /* is_fsctl */, (char *)pcchunk,
Steve French9bf0c9c2013-11-16 18:05:28 -0600639 sizeof(struct copychunk_ioctl), (char **)&retbuf,
640 &ret_data_len);
641 if (rc == 0) {
642 if (ret_data_len !=
643 sizeof(struct copychunk_ioctl_rsp)) {
644 cifs_dbg(VFS, "invalid cchunk response size\n");
645 rc = -EIO;
646 goto cchunk_out;
647 }
648 if (retbuf->TotalBytesWritten == 0) {
649 cifs_dbg(FYI, "no bytes copied\n");
650 rc = -EIO;
651 goto cchunk_out;
652 }
653 /*
654 * Check if server claimed to write more than we asked
655 */
656 if (le32_to_cpu(retbuf->TotalBytesWritten) >
657 le32_to_cpu(pcchunk->Length)) {
658 cifs_dbg(VFS, "invalid copy chunk response\n");
659 rc = -EIO;
660 goto cchunk_out;
661 }
662 if (le32_to_cpu(retbuf->ChunksWritten) != 1) {
663 cifs_dbg(VFS, "invalid num chunks written\n");
664 rc = -EIO;
665 goto cchunk_out;
666 }
667 chunks_copied++;
Steve French41c13582013-11-14 00:05:36 -0600668
Steve French9bf0c9c2013-11-16 18:05:28 -0600669 src_off += le32_to_cpu(retbuf->TotalBytesWritten);
670 dest_off += le32_to_cpu(retbuf->TotalBytesWritten);
671 len -= le32_to_cpu(retbuf->TotalBytesWritten);
Steve French41c13582013-11-14 00:05:36 -0600672
Steve French9bf0c9c2013-11-16 18:05:28 -0600673 cifs_dbg(FYI, "Chunks %d PartialChunk %d Total %d\n",
674 le32_to_cpu(retbuf->ChunksWritten),
675 le32_to_cpu(retbuf->ChunkBytesWritten),
676 le32_to_cpu(retbuf->TotalBytesWritten));
677 } else if (rc == -EINVAL) {
678 if (ret_data_len != sizeof(struct copychunk_ioctl_rsp))
679 goto cchunk_out;
Steve French41c13582013-11-14 00:05:36 -0600680
Steve French9bf0c9c2013-11-16 18:05:28 -0600681 cifs_dbg(FYI, "MaxChunks %d BytesChunk %d MaxCopy %d\n",
682 le32_to_cpu(retbuf->ChunksWritten),
683 le32_to_cpu(retbuf->ChunkBytesWritten),
684 le32_to_cpu(retbuf->TotalBytesWritten));
685
686 /*
687 * Check if this is the first request using these sizes,
688 * (ie check if copy succeed once with original sizes
689 * and check if the server gave us different sizes after
690 * we already updated max sizes on previous request).
691 * if not then why is the server returning an error now
692 */
693 if ((chunks_copied != 0) || chunk_sizes_updated)
694 goto cchunk_out;
695
696 /* Check that server is not asking us to grow size */
697 if (le32_to_cpu(retbuf->ChunkBytesWritten) <
698 tcon->max_bytes_chunk)
699 tcon->max_bytes_chunk =
700 le32_to_cpu(retbuf->ChunkBytesWritten);
701 else
702 goto cchunk_out; /* server gave us bogus size */
703
704 /* No need to change MaxChunks since already set to 1 */
705 chunk_sizes_updated = true;
Sachin Prabhu2477bc52015-02-04 13:10:26 +0000706 } else
707 goto cchunk_out;
Steve French9bf0c9c2013-11-16 18:05:28 -0600708 }
709
710cchunk_out:
Steve French41c13582013-11-14 00:05:36 -0600711 kfree(pcchunk);
Steve French24df1482016-09-29 04:20:23 -0500712 kfree(retbuf);
Steve French41c13582013-11-14 00:05:36 -0600713 return rc;
714}
715
716static int
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -0700717smb2_flush_file(const unsigned int xid, struct cifs_tcon *tcon,
718 struct cifs_fid *fid)
719{
720 return SMB2_flush(xid, tcon, fid->persistent_fid, fid->volatile_fid);
721}
722
Pavel Shilovsky09a47072012-09-18 16:20:29 -0700723static unsigned int
724smb2_read_data_offset(char *buf)
725{
726 struct smb2_read_rsp *rsp = (struct smb2_read_rsp *)buf;
727 return rsp->DataOffset;
728}
729
730static unsigned int
731smb2_read_data_length(char *buf)
732{
733 struct smb2_read_rsp *rsp = (struct smb2_read_rsp *)buf;
734 return le32_to_cpu(rsp->DataLength);
735}
736
Pavel Shilovskyd8e05032012-09-18 16:20:30 -0700737
738static int
Steve Frenchdb8b6312014-09-22 05:13:55 -0500739smb2_sync_read(const unsigned int xid, struct cifs_fid *pfid,
Pavel Shilovskyd8e05032012-09-18 16:20:30 -0700740 struct cifs_io_parms *parms, unsigned int *bytes_read,
741 char **buf, int *buf_type)
742{
Steve Frenchdb8b6312014-09-22 05:13:55 -0500743 parms->persistent_fid = pfid->persistent_fid;
744 parms->volatile_fid = pfid->volatile_fid;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -0700745 return SMB2_read(xid, parms, bytes_read, buf, buf_type);
746}
747
Pavel Shilovsky009d3442012-09-18 16:20:30 -0700748static int
Steve Frenchdb8b6312014-09-22 05:13:55 -0500749smb2_sync_write(const unsigned int xid, struct cifs_fid *pfid,
Pavel Shilovsky009d3442012-09-18 16:20:30 -0700750 struct cifs_io_parms *parms, unsigned int *written,
751 struct kvec *iov, unsigned long nr_segs)
752{
753
Steve Frenchdb8b6312014-09-22 05:13:55 -0500754 parms->persistent_fid = pfid->persistent_fid;
755 parms->volatile_fid = pfid->volatile_fid;
Pavel Shilovsky009d3442012-09-18 16:20:30 -0700756 return SMB2_write(xid, parms, written, iov, nr_segs);
757}
758
Steve Frenchd43cc792014-08-13 17:16:29 -0500759/* Set or clear the SPARSE_FILE attribute based on value passed in setsparse */
760static bool smb2_set_sparse(const unsigned int xid, struct cifs_tcon *tcon,
761 struct cifsFileInfo *cfile, struct inode *inode, __u8 setsparse)
762{
763 struct cifsInodeInfo *cifsi;
764 int rc;
765
766 cifsi = CIFS_I(inode);
767
768 /* if file already sparse don't bother setting sparse again */
769 if ((cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) && setsparse)
770 return true; /* already sparse */
771
772 if (!(cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) && !setsparse)
773 return true; /* already not sparse */
774
775 /*
776 * Can't check for sparse support on share the usual way via the
777 * FS attribute info (FILE_SUPPORTS_SPARSE_FILES) on the share
778 * since Samba server doesn't set the flag on the share, yet
779 * supports the set sparse FSCTL and returns sparse correctly
780 * in the file attributes. If we fail setting sparse though we
781 * mark that server does not support sparse files for this share
782 * to avoid repeatedly sending the unsupported fsctl to server
783 * if the file is repeatedly extended.
784 */
785 if (tcon->broken_sparse_sup)
786 return false;
787
788 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
789 cfile->fid.volatile_fid, FSCTL_SET_SPARSE,
790 true /* is_fctl */, &setsparse, 1, NULL, NULL);
791 if (rc) {
792 tcon->broken_sparse_sup = true;
793 cifs_dbg(FYI, "set sparse rc = %d\n", rc);
794 return false;
795 }
796
797 if (setsparse)
798 cifsi->cifsAttrs |= FILE_ATTRIBUTE_SPARSE_FILE;
799 else
800 cifsi->cifsAttrs &= (~FILE_ATTRIBUTE_SPARSE_FILE);
801
802 return true;
803}
804
Pavel Shilovskyc839ff22012-09-18 16:20:32 -0700805static int
806smb2_set_file_size(const unsigned int xid, struct cifs_tcon *tcon,
807 struct cifsFileInfo *cfile, __u64 size, bool set_alloc)
808{
809 __le64 eof = cpu_to_le64(size);
Steve French3d1a3742014-08-11 21:05:25 -0500810 struct inode *inode;
811
812 /*
813 * If extending file more than one page make sparse. Many Linux fs
814 * make files sparse by default when extending via ftruncate
815 */
David Howells2b0143b2015-03-17 22:25:59 +0000816 inode = d_inode(cfile->dentry);
Steve French3d1a3742014-08-11 21:05:25 -0500817
818 if (!set_alloc && (size > inode->i_size + 8192)) {
Steve French3d1a3742014-08-11 21:05:25 -0500819 __u8 set_sparse = 1;
Steve French3d1a3742014-08-11 21:05:25 -0500820
Steve Frenchd43cc792014-08-13 17:16:29 -0500821 /* whether set sparse succeeds or not, extend the file */
822 smb2_set_sparse(xid, tcon, cfile, inode, set_sparse);
Steve French3d1a3742014-08-11 21:05:25 -0500823 }
824
Pavel Shilovskyc839ff22012-09-18 16:20:32 -0700825 return SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
Steve Frenchf29ebb42014-07-19 21:44:58 -0500826 cfile->fid.volatile_fid, cfile->pid, &eof, false);
Pavel Shilovskyc839ff22012-09-18 16:20:32 -0700827}
828
Steve French02b16662015-06-27 21:18:36 -0700829static int
830smb2_duplicate_extents(const unsigned int xid,
831 struct cifsFileInfo *srcfile,
832 struct cifsFileInfo *trgtfile, u64 src_off,
833 u64 len, u64 dest_off)
834{
835 int rc;
836 unsigned int ret_data_len;
Steve French02b16662015-06-27 21:18:36 -0700837 struct duplicate_extents_to_file dup_ext_buf;
838 struct cifs_tcon *tcon = tlink_tcon(trgtfile->tlink);
839
840 /* server fileays advertise duplicate extent support with this flag */
841 if ((le32_to_cpu(tcon->fsAttrInfo.Attributes) &
842 FILE_SUPPORTS_BLOCK_REFCOUNTING) == 0)
843 return -EOPNOTSUPP;
844
845 dup_ext_buf.VolatileFileHandle = srcfile->fid.volatile_fid;
846 dup_ext_buf.PersistentFileHandle = srcfile->fid.persistent_fid;
847 dup_ext_buf.SourceFileOffset = cpu_to_le64(src_off);
848 dup_ext_buf.TargetFileOffset = cpu_to_le64(dest_off);
849 dup_ext_buf.ByteCount = cpu_to_le64(len);
850 cifs_dbg(FYI, "duplicate extents: src off %lld dst off %lld len %lld",
851 src_off, dest_off, len);
852
853 rc = smb2_set_file_size(xid, tcon, trgtfile, dest_off + len, false);
854 if (rc)
855 goto duplicate_extents_out;
856
857 rc = SMB2_ioctl(xid, tcon, trgtfile->fid.persistent_fid,
858 trgtfile->fid.volatile_fid,
859 FSCTL_DUPLICATE_EXTENTS_TO_FILE,
860 true /* is_fsctl */, (char *)&dup_ext_buf,
861 sizeof(struct duplicate_extents_to_file),
Steve French24df1482016-09-29 04:20:23 -0500862 NULL,
Steve French02b16662015-06-27 21:18:36 -0700863 &ret_data_len);
864
865 if (ret_data_len > 0)
866 cifs_dbg(FYI, "non-zero response length in duplicate extents");
867
868duplicate_extents_out:
869 return rc;
870}
Steve French02b16662015-06-27 21:18:36 -0700871
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -0700872static int
Steve French64a5cfa2013-10-14 15:31:32 -0500873smb2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
874 struct cifsFileInfo *cfile)
875{
876 return SMB2_set_compression(xid, tcon, cfile->fid.persistent_fid,
877 cfile->fid.volatile_fid);
878}
879
880static int
Steve Frenchb3152e22015-06-24 03:17:02 -0500881smb3_set_integrity(const unsigned int xid, struct cifs_tcon *tcon,
882 struct cifsFileInfo *cfile)
883{
884 struct fsctl_set_integrity_information_req integr_info;
Steve Frenchb3152e22015-06-24 03:17:02 -0500885 unsigned int ret_data_len;
886
887 integr_info.ChecksumAlgorithm = cpu_to_le16(CHECKSUM_TYPE_UNCHANGED);
888 integr_info.Flags = 0;
889 integr_info.Reserved = 0;
890
891 return SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
892 cfile->fid.volatile_fid,
893 FSCTL_SET_INTEGRITY_INFORMATION,
894 true /* is_fsctl */, (char *)&integr_info,
895 sizeof(struct fsctl_set_integrity_information_req),
Steve French24df1482016-09-29 04:20:23 -0500896 NULL,
Steve Frenchb3152e22015-06-24 03:17:02 -0500897 &ret_data_len);
898
899}
900
901static int
Steve French834170c2016-09-30 21:14:26 -0500902smb3_enum_snapshots(const unsigned int xid, struct cifs_tcon *tcon,
903 struct cifsFileInfo *cfile, void __user *ioc_buf)
904{
905 char *retbuf = NULL;
906 unsigned int ret_data_len = 0;
907 int rc;
908 struct smb_snapshot_array snapshot_in;
909
910 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
911 cfile->fid.volatile_fid,
912 FSCTL_SRV_ENUMERATE_SNAPSHOTS,
913 true /* is_fsctl */, NULL, 0 /* no input data */,
914 (char **)&retbuf,
915 &ret_data_len);
916 cifs_dbg(FYI, "enum snaphots ioctl returned %d and ret buflen is %d\n",
917 rc, ret_data_len);
918 if (rc)
919 return rc;
920
921 if (ret_data_len && (ioc_buf != NULL) && (retbuf != NULL)) {
922 /* Fixup buffer */
923 if (copy_from_user(&snapshot_in, ioc_buf,
924 sizeof(struct smb_snapshot_array))) {
925 rc = -EFAULT;
926 kfree(retbuf);
927 return rc;
928 }
929 if (snapshot_in.snapshot_array_size < sizeof(struct smb_snapshot_array)) {
930 rc = -ERANGE;
931 return rc;
932 }
933
934 if (ret_data_len > snapshot_in.snapshot_array_size)
935 ret_data_len = snapshot_in.snapshot_array_size;
936
937 if (copy_to_user(ioc_buf, retbuf, ret_data_len))
938 rc = -EFAULT;
939 }
940
941 kfree(retbuf);
942 return rc;
943}
944
945static int
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -0700946smb2_query_dir_first(const unsigned int xid, struct cifs_tcon *tcon,
947 const char *path, struct cifs_sb_info *cifs_sb,
948 struct cifs_fid *fid, __u16 search_flags,
949 struct cifs_search_info *srch_inf)
950{
951 __le16 *utf16_path;
952 int rc;
Pavel Shilovsky2e44b282012-09-18 16:20:33 -0700953 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
Pavel Shilovsky064f6042013-07-09 18:20:30 +0400954 struct cifs_open_parms oparms;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -0700955
956 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
957 if (!utf16_path)
958 return -ENOMEM;
959
Pavel Shilovsky064f6042013-07-09 18:20:30 +0400960 oparms.tcon = tcon;
961 oparms.desired_access = FILE_READ_ATTRIBUTES | FILE_READ_DATA;
962 oparms.disposition = FILE_OPEN;
963 oparms.create_options = 0;
964 oparms.fid = fid;
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +0400965 oparms.reconnect = false;
Pavel Shilovsky064f6042013-07-09 18:20:30 +0400966
Pavel Shilovskyb42bf882013-08-14 19:25:21 +0400967 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -0700968 kfree(utf16_path);
969 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500970 cifs_dbg(VFS, "open dir failed\n");
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -0700971 return rc;
972 }
973
974 srch_inf->entries_in_buffer = 0;
975 srch_inf->index_of_last_entry = 0;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -0700976
Pavel Shilovsky064f6042013-07-09 18:20:30 +0400977 rc = SMB2_query_directory(xid, tcon, fid->persistent_fid,
978 fid->volatile_fid, 0, srch_inf);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -0700979 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500980 cifs_dbg(VFS, "query directory failed\n");
Pavel Shilovsky064f6042013-07-09 18:20:30 +0400981 SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -0700982 }
983 return rc;
984}
985
986static int
987smb2_query_dir_next(const unsigned int xid, struct cifs_tcon *tcon,
988 struct cifs_fid *fid, __u16 search_flags,
989 struct cifs_search_info *srch_inf)
990{
991 return SMB2_query_directory(xid, tcon, fid->persistent_fid,
992 fid->volatile_fid, 0, srch_inf);
993}
994
995static int
996smb2_close_dir(const unsigned int xid, struct cifs_tcon *tcon,
997 struct cifs_fid *fid)
998{
999 return SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
1000}
1001
Pavel Shilovsky2e44b282012-09-18 16:20:33 -07001002/*
1003* If we negotiate SMB2 protocol and get STATUS_PENDING - update
1004* the number of credits and return true. Otherwise - return false.
1005*/
1006static bool
1007smb2_is_status_pending(char *buf, struct TCP_Server_Info *server, int length)
1008{
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001009 struct smb2_sync_hdr *shdr = get_sync_hdr(buf);
Pavel Shilovsky2e44b282012-09-18 16:20:33 -07001010
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001011 if (shdr->Status != STATUS_PENDING)
Pavel Shilovsky2e44b282012-09-18 16:20:33 -07001012 return false;
1013
1014 if (!length) {
1015 spin_lock(&server->req_lock);
Pavel Shilovsky31473fc2016-10-24 15:33:04 -07001016 server->credits += le16_to_cpu(shdr->CreditRequest);
Pavel Shilovsky2e44b282012-09-18 16:20:33 -07001017 spin_unlock(&server->req_lock);
1018 wake_up(&server->request_q);
1019 }
1020
1021 return true;
1022}
1023
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07001024static int
1025smb2_oplock_response(struct cifs_tcon *tcon, struct cifs_fid *fid,
1026 struct cifsInodeInfo *cinode)
1027{
Pavel Shilovsky0822f512012-09-19 06:22:45 -07001028 if (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_LEASING)
1029 return SMB2_lease_break(0, tcon, cinode->lease_key,
1030 smb2_get_lease_state(cinode));
1031
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07001032 return SMB2_oplock_break(0, tcon, fid->persistent_fid,
1033 fid->volatile_fid,
Pavel Shilovsky18cceb62013-09-05 13:01:06 +04001034 CIFS_CACHE_READ(cinode) ? 1 : 0);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07001035}
1036
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07001037static int
1038smb2_queryfs(const unsigned int xid, struct cifs_tcon *tcon,
1039 struct kstatfs *buf)
1040{
1041 int rc;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07001042 __le16 srch_path = 0; /* Null - open root of share */
1043 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001044 struct cifs_open_parms oparms;
1045 struct cifs_fid fid;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07001046
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001047 oparms.tcon = tcon;
1048 oparms.desired_access = FILE_READ_ATTRIBUTES;
1049 oparms.disposition = FILE_OPEN;
1050 oparms.create_options = 0;
1051 oparms.fid = &fid;
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001052 oparms.reconnect = false;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001053
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04001054 rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07001055 if (rc)
1056 return rc;
1057 buf->f_type = SMB2_MAGIC_NUMBER;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001058 rc = SMB2_QFS_info(xid, tcon, fid.persistent_fid, fid.volatile_fid,
1059 buf);
1060 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07001061 return rc;
1062}
1063
Pavel Shilovsky027e8ee2012-09-19 06:22:43 -07001064static bool
1065smb2_compare_fids(struct cifsFileInfo *ob1, struct cifsFileInfo *ob2)
1066{
1067 return ob1->fid.persistent_fid == ob2->fid.persistent_fid &&
1068 ob1->fid.volatile_fid == ob2->fid.volatile_fid;
1069}
1070
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07001071static int
1072smb2_mand_lock(const unsigned int xid, struct cifsFileInfo *cfile, __u64 offset,
1073 __u64 length, __u32 type, int lock, int unlock, bool wait)
1074{
1075 if (unlock && !lock)
1076 type = SMB2_LOCKFLAG_UNLOCK;
1077 return SMB2_lock(xid, tlink_tcon(cfile->tlink),
1078 cfile->fid.persistent_fid, cfile->fid.volatile_fid,
1079 current->tgid, length, offset, type, wait);
1080}
1081
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001082static void
1083smb2_get_lease_key(struct inode *inode, struct cifs_fid *fid)
1084{
1085 memcpy(fid->lease_key, CIFS_I(inode)->lease_key, SMB2_LEASE_KEY_SIZE);
1086}
1087
1088static void
1089smb2_set_lease_key(struct inode *inode, struct cifs_fid *fid)
1090{
1091 memcpy(CIFS_I(inode)->lease_key, fid->lease_key, SMB2_LEASE_KEY_SIZE);
1092}
1093
1094static void
1095smb2_new_lease_key(struct cifs_fid *fid)
1096{
Steve Frenchfa70b872016-09-22 00:39:34 -05001097 generate_random_uuid(fid->lease_key);
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001098}
1099
Pavel Shilovsky78932422016-07-24 10:37:38 +03001100#define SMB2_SYMLINK_STRUCT_SIZE \
1101 (sizeof(struct smb2_err_rsp) - 1 + sizeof(struct smb2_symlink_err_rsp))
1102
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04001103static int
1104smb2_query_symlink(const unsigned int xid, struct cifs_tcon *tcon,
1105 const char *full_path, char **target_path,
1106 struct cifs_sb_info *cifs_sb)
1107{
1108 int rc;
1109 __le16 *utf16_path;
1110 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
1111 struct cifs_open_parms oparms;
1112 struct cifs_fid fid;
1113 struct smb2_err_rsp *err_buf = NULL;
1114 struct smb2_symlink_err_rsp *symlink;
Pavel Shilovsky78932422016-07-24 10:37:38 +03001115 unsigned int sub_len;
1116 unsigned int sub_offset;
1117 unsigned int print_len;
1118 unsigned int print_offset;
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04001119
1120 cifs_dbg(FYI, "%s: path: %s\n", __func__, full_path);
1121
1122 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
1123 if (!utf16_path)
1124 return -ENOMEM;
1125
1126 oparms.tcon = tcon;
1127 oparms.desired_access = FILE_READ_ATTRIBUTES;
1128 oparms.disposition = FILE_OPEN;
1129 oparms.create_options = 0;
1130 oparms.fid = &fid;
1131 oparms.reconnect = false;
1132
1133 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, &err_buf);
1134
1135 if (!rc || !err_buf) {
1136 kfree(utf16_path);
1137 return -ENOENT;
1138 }
Pavel Shilovsky78932422016-07-24 10:37:38 +03001139
1140 if (le32_to_cpu(err_buf->ByteCount) < sizeof(struct smb2_symlink_err_rsp) ||
1141 get_rfc1002_length(err_buf) + 4 < SMB2_SYMLINK_STRUCT_SIZE) {
1142 kfree(utf16_path);
1143 return -ENOENT;
1144 }
1145
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04001146 /* open must fail on symlink - reset rc */
1147 rc = 0;
1148 symlink = (struct smb2_symlink_err_rsp *)err_buf->ErrorData;
1149 sub_len = le16_to_cpu(symlink->SubstituteNameLength);
1150 sub_offset = le16_to_cpu(symlink->SubstituteNameOffset);
Pavel Shilovsky78932422016-07-24 10:37:38 +03001151 print_len = le16_to_cpu(symlink->PrintNameLength);
1152 print_offset = le16_to_cpu(symlink->PrintNameOffset);
1153
1154 if (get_rfc1002_length(err_buf) + 4 <
1155 SMB2_SYMLINK_STRUCT_SIZE + sub_offset + sub_len) {
1156 kfree(utf16_path);
1157 return -ENOENT;
1158 }
1159
1160 if (get_rfc1002_length(err_buf) + 4 <
1161 SMB2_SYMLINK_STRUCT_SIZE + print_offset + print_len) {
1162 kfree(utf16_path);
1163 return -ENOENT;
1164 }
1165
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04001166 *target_path = cifs_strndup_from_utf16(
1167 (char *)symlink->PathBuffer + sub_offset,
1168 sub_len, true, cifs_sb->local_nls);
1169 if (!(*target_path)) {
1170 kfree(utf16_path);
1171 return -ENOMEM;
1172 }
1173 convert_delimiter(*target_path, '/');
1174 cifs_dbg(FYI, "%s: target path: %s\n", __func__, *target_path);
1175 kfree(utf16_path);
1176 return rc;
1177}
1178
Steve French30175622014-08-17 18:16:40 -05001179static long smb3_zero_range(struct file *file, struct cifs_tcon *tcon,
1180 loff_t offset, loff_t len, bool keep_size)
1181{
1182 struct inode *inode;
1183 struct cifsInodeInfo *cifsi;
1184 struct cifsFileInfo *cfile = file->private_data;
1185 struct file_zero_data_information fsctl_buf;
1186 long rc;
1187 unsigned int xid;
1188
1189 xid = get_xid();
1190
David Howells2b0143b2015-03-17 22:25:59 +00001191 inode = d_inode(cfile->dentry);
Steve French30175622014-08-17 18:16:40 -05001192 cifsi = CIFS_I(inode);
1193
1194 /* if file not oplocked can't be sure whether asking to extend size */
1195 if (!CIFS_CACHE_READ(cifsi))
1196 if (keep_size == false)
1197 return -EOPNOTSUPP;
1198
Steve French2bb93d22014-08-20 18:56:29 -05001199 /*
Steve French30175622014-08-17 18:16:40 -05001200 * Must check if file sparse since fallocate -z (zero range) assumes
1201 * non-sparse allocation
1202 */
1203 if (!(cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE))
1204 return -EOPNOTSUPP;
1205
1206 /*
1207 * need to make sure we are not asked to extend the file since the SMB3
1208 * fsctl does not change the file size. In the future we could change
1209 * this to zero the first part of the range then set the file size
1210 * which for a non sparse file would zero the newly extended range
1211 */
1212 if (keep_size == false)
1213 if (i_size_read(inode) < offset + len)
1214 return -EOPNOTSUPP;
1215
1216 cifs_dbg(FYI, "offset %lld len %lld", offset, len);
1217
1218 fsctl_buf.FileOffset = cpu_to_le64(offset);
1219 fsctl_buf.BeyondFinalZero = cpu_to_le64(offset + len);
1220
1221 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
1222 cfile->fid.volatile_fid, FSCTL_SET_ZERO_DATA,
1223 true /* is_fctl */, (char *)&fsctl_buf,
1224 sizeof(struct file_zero_data_information), NULL, NULL);
1225 free_xid(xid);
1226 return rc;
1227}
1228
Steve French31742c52014-08-17 08:38:47 -05001229static long smb3_punch_hole(struct file *file, struct cifs_tcon *tcon,
1230 loff_t offset, loff_t len)
1231{
1232 struct inode *inode;
1233 struct cifsInodeInfo *cifsi;
1234 struct cifsFileInfo *cfile = file->private_data;
1235 struct file_zero_data_information fsctl_buf;
1236 long rc;
1237 unsigned int xid;
1238 __u8 set_sparse = 1;
1239
1240 xid = get_xid();
1241
David Howells2b0143b2015-03-17 22:25:59 +00001242 inode = d_inode(cfile->dentry);
Steve French31742c52014-08-17 08:38:47 -05001243 cifsi = CIFS_I(inode);
1244
1245 /* Need to make file sparse, if not already, before freeing range. */
1246 /* Consider adding equivalent for compressed since it could also work */
1247 if (!smb2_set_sparse(xid, tcon, cfile, inode, set_sparse))
1248 return -EOPNOTSUPP;
1249
1250 cifs_dbg(FYI, "offset %lld len %lld", offset, len);
1251
1252 fsctl_buf.FileOffset = cpu_to_le64(offset);
1253 fsctl_buf.BeyondFinalZero = cpu_to_le64(offset + len);
1254
1255 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
1256 cfile->fid.volatile_fid, FSCTL_SET_ZERO_DATA,
1257 true /* is_fctl */, (char *)&fsctl_buf,
1258 sizeof(struct file_zero_data_information), NULL, NULL);
1259 free_xid(xid);
1260 return rc;
1261}
1262
Steve French9ccf3212014-10-18 17:01:15 -05001263static long smb3_simple_falloc(struct file *file, struct cifs_tcon *tcon,
1264 loff_t off, loff_t len, bool keep_size)
1265{
1266 struct inode *inode;
1267 struct cifsInodeInfo *cifsi;
1268 struct cifsFileInfo *cfile = file->private_data;
1269 long rc = -EOPNOTSUPP;
1270 unsigned int xid;
1271
1272 xid = get_xid();
1273
David Howells2b0143b2015-03-17 22:25:59 +00001274 inode = d_inode(cfile->dentry);
Steve French9ccf3212014-10-18 17:01:15 -05001275 cifsi = CIFS_I(inode);
1276
1277 /* if file not oplocked can't be sure whether asking to extend size */
1278 if (!CIFS_CACHE_READ(cifsi))
1279 if (keep_size == false)
1280 return -EOPNOTSUPP;
1281
1282 /*
1283 * Files are non-sparse by default so falloc may be a no-op
1284 * Must check if file sparse. If not sparse, and not extending
1285 * then no need to do anything since file already allocated
1286 */
1287 if ((cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) == 0) {
1288 if (keep_size == true)
1289 return 0;
1290 /* check if extending file */
1291 else if (i_size_read(inode) >= off + len)
1292 /* not extending file and already not sparse */
1293 return 0;
1294 /* BB: in future add else clause to extend file */
1295 else
1296 return -EOPNOTSUPP;
1297 }
1298
1299 if ((keep_size == true) || (i_size_read(inode) >= off + len)) {
1300 /*
1301 * Check if falloc starts within first few pages of file
1302 * and ends within a few pages of the end of file to
1303 * ensure that most of file is being forced to be
1304 * fallocated now. If so then setting whole file sparse
1305 * ie potentially making a few extra pages at the beginning
1306 * or end of the file non-sparse via set_sparse is harmless.
1307 */
1308 if ((off > 8192) || (off + len + 8192 < i_size_read(inode)))
1309 return -EOPNOTSUPP;
1310
1311 rc = smb2_set_sparse(xid, tcon, cfile, inode, false);
1312 }
1313 /* BB: else ... in future add code to extend file and set sparse */
1314
1315
1316 free_xid(xid);
1317 return rc;
1318}
1319
1320
Steve French31742c52014-08-17 08:38:47 -05001321static long smb3_fallocate(struct file *file, struct cifs_tcon *tcon, int mode,
1322 loff_t off, loff_t len)
1323{
1324 /* KEEP_SIZE already checked for by do_fallocate */
1325 if (mode & FALLOC_FL_PUNCH_HOLE)
1326 return smb3_punch_hole(file, tcon, off, len);
Steve French30175622014-08-17 18:16:40 -05001327 else if (mode & FALLOC_FL_ZERO_RANGE) {
1328 if (mode & FALLOC_FL_KEEP_SIZE)
1329 return smb3_zero_range(file, tcon, off, len, true);
1330 return smb3_zero_range(file, tcon, off, len, false);
Steve French9ccf3212014-10-18 17:01:15 -05001331 } else if (mode == FALLOC_FL_KEEP_SIZE)
1332 return smb3_simple_falloc(file, tcon, off, len, true);
1333 else if (mode == 0)
1334 return smb3_simple_falloc(file, tcon, off, len, false);
Steve French31742c52014-08-17 08:38:47 -05001335
1336 return -EOPNOTSUPP;
1337}
1338
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04001339static void
Sachin Prabhuc11f1df2014-03-11 16:11:47 +00001340smb2_downgrade_oplock(struct TCP_Server_Info *server,
1341 struct cifsInodeInfo *cinode, bool set_level2)
1342{
1343 if (set_level2)
1344 server->ops->set_oplock_level(cinode, SMB2_OPLOCK_LEVEL_II,
1345 0, NULL);
1346 else
1347 server->ops->set_oplock_level(cinode, 0, 0, NULL);
1348}
1349
1350static void
Pavel Shilovsky42873b02013-09-05 21:30:16 +04001351smb2_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
1352 unsigned int epoch, bool *purge_cache)
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04001353{
1354 oplock &= 0xFF;
1355 if (oplock == SMB2_OPLOCK_LEVEL_NOCHANGE)
1356 return;
1357 if (oplock == SMB2_OPLOCK_LEVEL_BATCH) {
Pavel Shilovsky42873b02013-09-05 21:30:16 +04001358 cinode->oplock = CIFS_CACHE_RHW_FLG;
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04001359 cifs_dbg(FYI, "Batch Oplock granted on inode %p\n",
1360 &cinode->vfs_inode);
1361 } else if (oplock == SMB2_OPLOCK_LEVEL_EXCLUSIVE) {
Pavel Shilovsky42873b02013-09-05 21:30:16 +04001362 cinode->oplock = CIFS_CACHE_RW_FLG;
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04001363 cifs_dbg(FYI, "Exclusive Oplock granted on inode %p\n",
1364 &cinode->vfs_inode);
1365 } else if (oplock == SMB2_OPLOCK_LEVEL_II) {
1366 cinode->oplock = CIFS_CACHE_READ_FLG;
1367 cifs_dbg(FYI, "Level II Oplock granted on inode %p\n",
1368 &cinode->vfs_inode);
1369 } else
1370 cinode->oplock = 0;
1371}
1372
1373static void
Pavel Shilovsky42873b02013-09-05 21:30:16 +04001374smb21_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
1375 unsigned int epoch, bool *purge_cache)
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04001376{
1377 char message[5] = {0};
1378
1379 oplock &= 0xFF;
1380 if (oplock == SMB2_OPLOCK_LEVEL_NOCHANGE)
1381 return;
1382
1383 cinode->oplock = 0;
1384 if (oplock & SMB2_LEASE_READ_CACHING_HE) {
1385 cinode->oplock |= CIFS_CACHE_READ_FLG;
1386 strcat(message, "R");
1387 }
1388 if (oplock & SMB2_LEASE_HANDLE_CACHING_HE) {
1389 cinode->oplock |= CIFS_CACHE_HANDLE_FLG;
1390 strcat(message, "H");
1391 }
1392 if (oplock & SMB2_LEASE_WRITE_CACHING_HE) {
1393 cinode->oplock |= CIFS_CACHE_WRITE_FLG;
1394 strcat(message, "W");
1395 }
1396 if (!cinode->oplock)
1397 strcat(message, "None");
1398 cifs_dbg(FYI, "%s Lease granted on inode %p\n", message,
1399 &cinode->vfs_inode);
1400}
1401
Pavel Shilovsky42873b02013-09-05 21:30:16 +04001402static void
1403smb3_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
1404 unsigned int epoch, bool *purge_cache)
1405{
1406 unsigned int old_oplock = cinode->oplock;
1407
1408 smb21_set_oplock_level(cinode, oplock, epoch, purge_cache);
1409
1410 if (purge_cache) {
1411 *purge_cache = false;
1412 if (old_oplock == CIFS_CACHE_READ_FLG) {
1413 if (cinode->oplock == CIFS_CACHE_READ_FLG &&
1414 (epoch - cinode->epoch > 0))
1415 *purge_cache = true;
1416 else if (cinode->oplock == CIFS_CACHE_RH_FLG &&
1417 (epoch - cinode->epoch > 1))
1418 *purge_cache = true;
1419 else if (cinode->oplock == CIFS_CACHE_RHW_FLG &&
1420 (epoch - cinode->epoch > 1))
1421 *purge_cache = true;
1422 else if (cinode->oplock == 0 &&
1423 (epoch - cinode->epoch > 0))
1424 *purge_cache = true;
1425 } else if (old_oplock == CIFS_CACHE_RH_FLG) {
1426 if (cinode->oplock == CIFS_CACHE_RH_FLG &&
1427 (epoch - cinode->epoch > 0))
1428 *purge_cache = true;
1429 else if (cinode->oplock == CIFS_CACHE_RHW_FLG &&
1430 (epoch - cinode->epoch > 1))
1431 *purge_cache = true;
1432 }
1433 cinode->epoch = epoch;
1434 }
1435}
1436
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04001437static bool
1438smb2_is_read_op(__u32 oplock)
1439{
1440 return oplock == SMB2_OPLOCK_LEVEL_II;
1441}
1442
1443static bool
1444smb21_is_read_op(__u32 oplock)
1445{
1446 return (oplock & SMB2_LEASE_READ_CACHING_HE) &&
1447 !(oplock & SMB2_LEASE_WRITE_CACHING_HE);
1448}
1449
Pavel Shilovskyf0473902013-09-04 13:44:05 +04001450static __le32
1451map_oplock_to_lease(u8 oplock)
1452{
1453 if (oplock == SMB2_OPLOCK_LEVEL_EXCLUSIVE)
1454 return SMB2_LEASE_WRITE_CACHING | SMB2_LEASE_READ_CACHING;
1455 else if (oplock == SMB2_OPLOCK_LEVEL_II)
1456 return SMB2_LEASE_READ_CACHING;
1457 else if (oplock == SMB2_OPLOCK_LEVEL_BATCH)
1458 return SMB2_LEASE_HANDLE_CACHING | SMB2_LEASE_READ_CACHING |
1459 SMB2_LEASE_WRITE_CACHING;
1460 return 0;
1461}
1462
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001463static char *
1464smb2_create_lease_buf(u8 *lease_key, u8 oplock)
1465{
1466 struct create_lease *buf;
1467
1468 buf = kzalloc(sizeof(struct create_lease), GFP_KERNEL);
1469 if (!buf)
1470 return NULL;
1471
1472 buf->lcontext.LeaseKeyLow = cpu_to_le64(*((u64 *)lease_key));
1473 buf->lcontext.LeaseKeyHigh = cpu_to_le64(*((u64 *)(lease_key + 8)));
Pavel Shilovskyf0473902013-09-04 13:44:05 +04001474 buf->lcontext.LeaseState = map_oplock_to_lease(oplock);
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001475
1476 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1477 (struct create_lease, lcontext));
1478 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context));
1479 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1480 (struct create_lease, Name));
1481 buf->ccontext.NameLength = cpu_to_le16(4);
Steve French12197a72014-05-14 05:29:40 -07001482 /* SMB2_CREATE_REQUEST_LEASE is "RqLs" */
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001483 buf->Name[0] = 'R';
1484 buf->Name[1] = 'q';
1485 buf->Name[2] = 'L';
1486 buf->Name[3] = 's';
1487 return (char *)buf;
1488}
1489
Pavel Shilovskyf0473902013-09-04 13:44:05 +04001490static char *
1491smb3_create_lease_buf(u8 *lease_key, u8 oplock)
1492{
1493 struct create_lease_v2 *buf;
1494
1495 buf = kzalloc(sizeof(struct create_lease_v2), GFP_KERNEL);
1496 if (!buf)
1497 return NULL;
1498
1499 buf->lcontext.LeaseKeyLow = cpu_to_le64(*((u64 *)lease_key));
1500 buf->lcontext.LeaseKeyHigh = cpu_to_le64(*((u64 *)(lease_key + 8)));
1501 buf->lcontext.LeaseState = map_oplock_to_lease(oplock);
1502
1503 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1504 (struct create_lease_v2, lcontext));
1505 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context_v2));
1506 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1507 (struct create_lease_v2, Name));
1508 buf->ccontext.NameLength = cpu_to_le16(4);
Steve French12197a72014-05-14 05:29:40 -07001509 /* SMB2_CREATE_REQUEST_LEASE is "RqLs" */
Pavel Shilovskyf0473902013-09-04 13:44:05 +04001510 buf->Name[0] = 'R';
1511 buf->Name[1] = 'q';
1512 buf->Name[2] = 'L';
1513 buf->Name[3] = 's';
1514 return (char *)buf;
1515}
1516
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001517static __u8
Pavel Shilovsky42873b02013-09-05 21:30:16 +04001518smb2_parse_lease_buf(void *buf, unsigned int *epoch)
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001519{
1520 struct create_lease *lc = (struct create_lease *)buf;
1521
Pavel Shilovsky42873b02013-09-05 21:30:16 +04001522 *epoch = 0; /* not used */
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001523 if (lc->lcontext.LeaseFlags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS)
1524 return SMB2_OPLOCK_LEVEL_NOCHANGE;
1525 return le32_to_cpu(lc->lcontext.LeaseState);
1526}
1527
Pavel Shilovskyf0473902013-09-04 13:44:05 +04001528static __u8
Pavel Shilovsky42873b02013-09-05 21:30:16 +04001529smb3_parse_lease_buf(void *buf, unsigned int *epoch)
Pavel Shilovskyf0473902013-09-04 13:44:05 +04001530{
1531 struct create_lease_v2 *lc = (struct create_lease_v2 *)buf;
1532
Pavel Shilovsky42873b02013-09-05 21:30:16 +04001533 *epoch = le16_to_cpu(lc->lcontext.Epoch);
Pavel Shilovskyf0473902013-09-04 13:44:05 +04001534 if (lc->lcontext.LeaseFlags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS)
1535 return SMB2_OPLOCK_LEVEL_NOCHANGE;
1536 return le32_to_cpu(lc->lcontext.LeaseState);
1537}
1538
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04001539static unsigned int
1540smb2_wp_retry_size(struct inode *inode)
1541{
1542 return min_t(unsigned int, CIFS_SB(inode->i_sb)->wsize,
1543 SMB2_MAX_BUFFER_SIZE);
1544}
1545
Pavel Shilovsky52755802014-08-18 20:49:57 +04001546static bool
1547smb2_dir_needs_close(struct cifsFileInfo *cfile)
1548{
1549 return !cfile->invalidHandle;
1550}
1551
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07001552static void
1553fill_transform_hdr(struct smb2_transform_hdr *tr_hdr, struct smb_rqst *old_rq)
1554{
1555 struct smb2_sync_hdr *shdr =
1556 (struct smb2_sync_hdr *)old_rq->rq_iov[1].iov_base;
1557 unsigned int orig_len = get_rfc1002_length(old_rq->rq_iov[0].iov_base);
1558
1559 memset(tr_hdr, 0, sizeof(struct smb2_transform_hdr));
1560 tr_hdr->ProtocolId = SMB2_TRANSFORM_PROTO_NUM;
1561 tr_hdr->OriginalMessageSize = cpu_to_le32(orig_len);
1562 tr_hdr->Flags = cpu_to_le16(0x01);
1563 get_random_bytes(&tr_hdr->Nonce, SMB3_AES128CMM_NONCE);
1564 memcpy(&tr_hdr->SessionId, &shdr->SessionId, 8);
1565 inc_rfc1001_len(tr_hdr, sizeof(struct smb2_transform_hdr) - 4);
1566 inc_rfc1001_len(tr_hdr, orig_len);
1567}
1568
1569static struct scatterlist *
1570init_sg(struct smb_rqst *rqst, u8 *sign)
1571{
1572 unsigned int sg_len = rqst->rq_nvec + rqst->rq_npages + 1;
1573 unsigned int assoc_data_len = sizeof(struct smb2_transform_hdr) - 24;
1574 struct scatterlist *sg;
1575 unsigned int i;
1576 unsigned int j;
1577
1578 sg = kmalloc_array(sg_len, sizeof(struct scatterlist), GFP_KERNEL);
1579 if (!sg)
1580 return NULL;
1581
1582 sg_init_table(sg, sg_len);
1583 sg_set_buf(&sg[0], rqst->rq_iov[0].iov_base + 24, assoc_data_len);
1584 for (i = 1; i < rqst->rq_nvec; i++)
1585 sg_set_buf(&sg[i], rqst->rq_iov[i].iov_base,
1586 rqst->rq_iov[i].iov_len);
1587 for (j = 0; i < sg_len - 1; i++, j++) {
1588 unsigned int len = (j < rqst->rq_npages - 1) ? rqst->rq_pagesz
1589 : rqst->rq_tailsz;
1590 sg_set_page(&sg[i], rqst->rq_pages[j], len, 0);
1591 }
1592 sg_set_buf(&sg[sg_len - 1], sign, SMB2_SIGNATURE_SIZE);
1593 return sg;
1594}
1595
1596struct cifs_crypt_result {
1597 int err;
1598 struct completion completion;
1599};
1600
1601static void cifs_crypt_complete(struct crypto_async_request *req, int err)
1602{
1603 struct cifs_crypt_result *res = req->data;
1604
1605 if (err == -EINPROGRESS)
1606 return;
1607
1608 res->err = err;
1609 complete(&res->completion);
1610}
1611
1612/*
1613 * Encrypt or decrypt @rqst message. @rqst has the following format:
1614 * iov[0] - transform header (associate data),
1615 * iov[1-N] and pages - data to encrypt.
1616 * On success return encrypted data in iov[1-N] and pages, leave iov[0]
1617 * untouched.
1618 */
1619static int
1620crypt_message(struct TCP_Server_Info *server, struct smb_rqst *rqst, int enc)
1621{
1622 struct smb2_transform_hdr *tr_hdr =
1623 (struct smb2_transform_hdr *)rqst->rq_iov[0].iov_base;
1624 unsigned int assoc_data_len = sizeof(struct smb2_transform_hdr) - 24;
1625 struct cifs_ses *ses;
1626 int rc = 0;
1627 struct scatterlist *sg;
1628 u8 sign[SMB2_SIGNATURE_SIZE] = {};
1629 struct aead_request *req;
1630 char *iv;
1631 unsigned int iv_len;
1632 struct cifs_crypt_result result = {0, };
1633 struct crypto_aead *tfm;
1634 unsigned int crypt_len = le32_to_cpu(tr_hdr->OriginalMessageSize);
1635
1636 init_completion(&result.completion);
1637
1638 ses = smb2_find_smb_ses(server, tr_hdr->SessionId);
1639 if (!ses) {
1640 cifs_dbg(VFS, "%s: Could not find session\n", __func__);
1641 return 0;
1642 }
1643
1644 rc = smb3_crypto_aead_allocate(server);
1645 if (rc) {
1646 cifs_dbg(VFS, "%s: crypto alloc failed\n", __func__);
1647 return rc;
1648 }
1649
1650 tfm = enc ? server->secmech.ccmaesencrypt :
1651 server->secmech.ccmaesdecrypt;
1652 rc = crypto_aead_setkey(tfm, enc ? ses->smb3encryptionkey :
1653 ses->smb3decryptionkey, SMB3_SIGN_KEY_SIZE);
1654 if (rc) {
1655 cifs_dbg(VFS, "%s: Failed to set aead key %d\n", __func__, rc);
1656 return rc;
1657 }
1658
1659 rc = crypto_aead_setauthsize(tfm, SMB2_SIGNATURE_SIZE);
1660 if (rc) {
1661 cifs_dbg(VFS, "%s: Failed to set authsize %d\n", __func__, rc);
1662 return rc;
1663 }
1664
1665 req = aead_request_alloc(tfm, GFP_KERNEL);
1666 if (!req) {
1667 cifs_dbg(VFS, "%s: Failed to alloc aead request", __func__);
1668 return -ENOMEM;
1669 }
1670
1671 if (!enc) {
1672 memcpy(sign, &tr_hdr->Signature, SMB2_SIGNATURE_SIZE);
1673 crypt_len += SMB2_SIGNATURE_SIZE;
1674 }
1675
1676 sg = init_sg(rqst, sign);
1677 if (!sg) {
1678 cifs_dbg(VFS, "%s: Failed to init sg %d", __func__, rc);
1679 goto free_req;
1680 }
1681
1682 iv_len = crypto_aead_ivsize(tfm);
1683 iv = kzalloc(iv_len, GFP_KERNEL);
1684 if (!iv) {
1685 cifs_dbg(VFS, "%s: Failed to alloc IV", __func__);
1686 goto free_sg;
1687 }
1688 iv[0] = 3;
1689 memcpy(iv + 1, (char *)tr_hdr->Nonce, SMB3_AES128CMM_NONCE);
1690
1691 aead_request_set_crypt(req, sg, sg, crypt_len, iv);
1692 aead_request_set_ad(req, assoc_data_len);
1693
1694 aead_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
1695 cifs_crypt_complete, &result);
1696
1697 rc = enc ? crypto_aead_encrypt(req) : crypto_aead_decrypt(req);
1698
1699 if (rc == -EINPROGRESS || rc == -EBUSY) {
1700 wait_for_completion(&result.completion);
1701 rc = result.err;
1702 }
1703
1704 if (!rc && enc)
1705 memcpy(&tr_hdr->Signature, sign, SMB2_SIGNATURE_SIZE);
1706
1707 kfree(iv);
1708free_sg:
1709 kfree(sg);
1710free_req:
1711 kfree(req);
1712 return rc;
1713}
1714
1715static int
1716smb3_init_transform_rq(struct TCP_Server_Info *server, struct smb_rqst *new_rq,
1717 struct smb_rqst *old_rq)
1718{
1719 struct kvec *iov;
1720 struct page **pages;
1721 struct smb2_transform_hdr *tr_hdr;
1722 unsigned int npages = old_rq->rq_npages;
1723 int i;
1724 int rc = -ENOMEM;
1725
1726 pages = kmalloc_array(npages, sizeof(struct page *), GFP_KERNEL);
1727 if (!pages)
1728 return rc;
1729
1730 new_rq->rq_pages = pages;
1731 new_rq->rq_npages = old_rq->rq_npages;
1732 new_rq->rq_pagesz = old_rq->rq_pagesz;
1733 new_rq->rq_tailsz = old_rq->rq_tailsz;
1734
1735 for (i = 0; i < npages; i++) {
1736 pages[i] = alloc_page(GFP_KERNEL|__GFP_HIGHMEM);
1737 if (!pages[i])
1738 goto err_free_pages;
1739 }
1740
1741 iov = kmalloc_array(old_rq->rq_nvec, sizeof(struct kvec), GFP_KERNEL);
1742 if (!iov)
1743 goto err_free_pages;
1744
1745 /* copy all iovs from the old except the 1st one (rfc1002 length) */
1746 memcpy(&iov[1], &old_rq->rq_iov[1],
1747 sizeof(struct kvec) * (old_rq->rq_nvec - 1));
1748 new_rq->rq_iov = iov;
1749 new_rq->rq_nvec = old_rq->rq_nvec;
1750
1751 tr_hdr = kmalloc(sizeof(struct smb2_transform_hdr), GFP_KERNEL);
1752 if (!tr_hdr)
1753 goto err_free_iov;
1754
1755 /* fill the 1st iov with a transform header */
1756 fill_transform_hdr(tr_hdr, old_rq);
1757 new_rq->rq_iov[0].iov_base = tr_hdr;
1758 new_rq->rq_iov[0].iov_len = sizeof(struct smb2_transform_hdr);
1759
1760 /* copy pages form the old */
1761 for (i = 0; i < npages; i++) {
1762 char *dst = kmap(new_rq->rq_pages[i]);
1763 char *src = kmap(old_rq->rq_pages[i]);
1764 unsigned int len = (i < npages - 1) ? new_rq->rq_pagesz :
1765 new_rq->rq_tailsz;
1766 memcpy(dst, src, len);
1767 kunmap(new_rq->rq_pages[i]);
1768 kunmap(old_rq->rq_pages[i]);
1769 }
1770
1771 rc = crypt_message(server, new_rq, 1);
1772 cifs_dbg(FYI, "encrypt message returned %d", rc);
1773 if (rc)
1774 goto err_free_tr_hdr;
1775
1776 return rc;
1777
1778err_free_tr_hdr:
1779 kfree(tr_hdr);
1780err_free_iov:
1781 kfree(iov);
1782err_free_pages:
1783 for (i = i - 1; i >= 0; i--)
1784 put_page(pages[i]);
1785 kfree(pages);
1786 return rc;
1787}
1788
1789static void
1790smb3_free_transform_rq(struct smb_rqst *rqst)
1791{
1792 int i = rqst->rq_npages - 1;
1793
1794 for (; i >= 0; i--)
1795 put_page(rqst->rq_pages[i]);
1796 kfree(rqst->rq_pages);
1797 /* free transform header */
1798 kfree(rqst->rq_iov[0].iov_base);
1799 kfree(rqst->rq_iov);
1800}
1801
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08001802static int
1803smb3_is_transform_hdr(void *buf)
1804{
1805 struct smb2_transform_hdr *trhdr = buf;
1806
1807 return trhdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM;
1808}
1809
1810static int
1811decrypt_raw_data(struct TCP_Server_Info *server, char *buf,
1812 unsigned int buf_data_size, struct page **pages,
1813 unsigned int npages, unsigned int page_data_size)
1814{
1815 struct kvec iov[2];
1816 struct smb_rqst rqst = {NULL};
1817 struct smb2_hdr *hdr;
1818 int rc;
1819
1820 iov[0].iov_base = buf;
1821 iov[0].iov_len = sizeof(struct smb2_transform_hdr);
1822 iov[1].iov_base = buf + sizeof(struct smb2_transform_hdr);
1823 iov[1].iov_len = buf_data_size;
1824
1825 rqst.rq_iov = iov;
1826 rqst.rq_nvec = 2;
1827 rqst.rq_pages = pages;
1828 rqst.rq_npages = npages;
1829 rqst.rq_pagesz = PAGE_SIZE;
1830 rqst.rq_tailsz = (page_data_size % PAGE_SIZE) ? : PAGE_SIZE;
1831
1832 rc = crypt_message(server, &rqst, 0);
1833 cifs_dbg(FYI, "decrypt message returned %d\n", rc);
1834
1835 if (rc)
1836 return rc;
1837
1838 memmove(buf + 4, iov[1].iov_base, buf_data_size);
1839 hdr = (struct smb2_hdr *)buf;
1840 hdr->smb2_buf_length = cpu_to_be32(buf_data_size + page_data_size);
1841 server->total_read = buf_data_size + page_data_size + 4;
1842
1843 return rc;
1844}
1845
1846static int
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08001847read_data_into_pages(struct TCP_Server_Info *server, struct page **pages,
1848 unsigned int npages, unsigned int len)
1849{
1850 int i;
1851 int length;
1852
1853 for (i = 0; i < npages; i++) {
1854 struct page *page = pages[i];
1855 size_t n;
1856
1857 n = len;
1858 if (len >= PAGE_SIZE) {
1859 /* enough data to fill the page */
1860 n = PAGE_SIZE;
1861 len -= n;
1862 } else {
1863 zero_user(page, len, PAGE_SIZE - len);
1864 len = 0;
1865 }
1866 length = cifs_read_page_from_socket(server, page, n);
1867 if (length < 0)
1868 return length;
1869 server->total_read += length;
1870 }
1871
1872 return 0;
1873}
1874
1875static int
1876init_read_bvec(struct page **pages, unsigned int npages, unsigned int data_size,
1877 unsigned int cur_off, struct bio_vec **page_vec)
1878{
1879 struct bio_vec *bvec;
1880 int i;
1881
1882 bvec = kcalloc(npages, sizeof(struct bio_vec), GFP_KERNEL);
1883 if (!bvec)
1884 return -ENOMEM;
1885
1886 for (i = 0; i < npages; i++) {
1887 bvec[i].bv_page = pages[i];
1888 bvec[i].bv_offset = (i == 0) ? cur_off : 0;
1889 bvec[i].bv_len = min_t(unsigned int, PAGE_SIZE, data_size);
1890 data_size -= bvec[i].bv_len;
1891 }
1892
1893 if (data_size != 0) {
1894 cifs_dbg(VFS, "%s: something went wrong\n", __func__);
1895 kfree(bvec);
1896 return -EIO;
1897 }
1898
1899 *page_vec = bvec;
1900 return 0;
1901}
1902
1903static int
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08001904handle_read_data(struct TCP_Server_Info *server, struct mid_q_entry *mid,
1905 char *buf, unsigned int buf_len, struct page **pages,
1906 unsigned int npages, unsigned int page_data_size)
1907{
1908 unsigned int data_offset;
1909 unsigned int data_len;
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08001910 unsigned int cur_off;
1911 unsigned int cur_page_idx;
1912 unsigned int pad_len;
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08001913 struct cifs_readdata *rdata = mid->callback_data;
1914 struct smb2_sync_hdr *shdr = get_sync_hdr(buf);
1915 struct bio_vec *bvec = NULL;
1916 struct iov_iter iter;
1917 struct kvec iov;
1918 int length;
1919
1920 if (shdr->Command != SMB2_READ) {
1921 cifs_dbg(VFS, "only big read responses are supported\n");
1922 return -ENOTSUPP;
1923 }
1924
1925 if (server->ops->is_status_pending &&
1926 server->ops->is_status_pending(buf, server, 0))
1927 return -1;
1928
1929 rdata->result = server->ops->map_error(buf, false);
1930 if (rdata->result != 0) {
1931 cifs_dbg(FYI, "%s: server returned error %d\n",
1932 __func__, rdata->result);
1933 dequeue_mid(mid, rdata->result);
1934 return 0;
1935 }
1936
1937 data_offset = server->ops->read_data_offset(buf) + 4;
1938 data_len = server->ops->read_data_length(buf);
1939
1940 if (data_offset < server->vals->read_rsp_size) {
1941 /*
1942 * win2k8 sometimes sends an offset of 0 when the read
1943 * is beyond the EOF. Treat it as if the data starts just after
1944 * the header.
1945 */
1946 cifs_dbg(FYI, "%s: data offset (%u) inside read response header\n",
1947 __func__, data_offset);
1948 data_offset = server->vals->read_rsp_size;
1949 } else if (data_offset > MAX_CIFS_SMALL_BUFFER_SIZE) {
1950 /* data_offset is beyond the end of smallbuf */
1951 cifs_dbg(FYI, "%s: data offset (%u) beyond end of smallbuf\n",
1952 __func__, data_offset);
1953 rdata->result = -EIO;
1954 dequeue_mid(mid, rdata->result);
1955 return 0;
1956 }
1957
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08001958 pad_len = data_offset - server->vals->read_rsp_size;
1959
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08001960 if (buf_len <= data_offset) {
1961 /* read response payload is in pages */
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08001962 cur_page_idx = pad_len / PAGE_SIZE;
1963 cur_off = pad_len % PAGE_SIZE;
1964
1965 if (cur_page_idx != 0) {
1966 /* data offset is beyond the 1st page of response */
1967 cifs_dbg(FYI, "%s: data offset (%u) beyond 1st page of response\n",
1968 __func__, data_offset);
1969 rdata->result = -EIO;
1970 dequeue_mid(mid, rdata->result);
1971 return 0;
1972 }
1973
1974 if (data_len > page_data_size - pad_len) {
1975 /* data_len is corrupt -- discard frame */
1976 rdata->result = -EIO;
1977 dequeue_mid(mid, rdata->result);
1978 return 0;
1979 }
1980
1981 rdata->result = init_read_bvec(pages, npages, page_data_size,
1982 cur_off, &bvec);
1983 if (rdata->result != 0) {
1984 dequeue_mid(mid, rdata->result);
1985 return 0;
1986 }
1987
1988 iov_iter_bvec(&iter, WRITE | ITER_BVEC, bvec, npages, data_len);
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08001989 } else if (buf_len >= data_offset + data_len) {
1990 /* read response payload is in buf */
1991 WARN_ONCE(npages > 0, "read data can be either in buf or in pages");
1992 iov.iov_base = buf + data_offset;
1993 iov.iov_len = data_len;
1994 iov_iter_kvec(&iter, WRITE | ITER_KVEC, &iov, 1, data_len);
1995 } else {
1996 /* read response payload cannot be in both buf and pages */
1997 WARN_ONCE(1, "buf can not contain only a part of read data");
1998 rdata->result = -EIO;
1999 dequeue_mid(mid, rdata->result);
2000 return 0;
2001 }
2002
2003 /* set up first iov for signature check */
2004 rdata->iov[0].iov_base = buf;
2005 rdata->iov[0].iov_len = 4;
2006 rdata->iov[1].iov_base = buf + 4;
2007 rdata->iov[1].iov_len = server->vals->read_rsp_size - 4;
2008 cifs_dbg(FYI, "0: iov_base=%p iov_len=%zu\n",
2009 rdata->iov[0].iov_base, server->vals->read_rsp_size);
2010
2011 length = rdata->copy_into_pages(server, rdata, &iter);
2012
2013 kfree(bvec);
2014
2015 if (length < 0)
2016 return length;
2017
2018 dequeue_mid(mid, false);
2019 return length;
2020}
2021
2022static int
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08002023receive_encrypted_read(struct TCP_Server_Info *server, struct mid_q_entry **mid)
2024{
2025 char *buf = server->smallbuf;
2026 struct smb2_transform_hdr *tr_hdr = (struct smb2_transform_hdr *)buf;
2027 unsigned int npages;
2028 struct page **pages;
2029 unsigned int len;
2030 unsigned int buflen = get_rfc1002_length(buf) + 4;
2031 int rc;
2032 int i = 0;
2033
2034 len = min_t(unsigned int, buflen, server->vals->read_rsp_size - 4 +
2035 sizeof(struct smb2_transform_hdr)) - HEADER_SIZE(server) + 1;
2036
2037 rc = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1, len);
2038 if (rc < 0)
2039 return rc;
2040 server->total_read += rc;
2041
2042 len = le32_to_cpu(tr_hdr->OriginalMessageSize) + 4 -
2043 server->vals->read_rsp_size;
2044 npages = DIV_ROUND_UP(len, PAGE_SIZE);
2045
2046 pages = kmalloc_array(npages, sizeof(struct page *), GFP_KERNEL);
2047 if (!pages) {
2048 rc = -ENOMEM;
2049 goto discard_data;
2050 }
2051
2052 for (; i < npages; i++) {
2053 pages[i] = alloc_page(GFP_KERNEL|__GFP_HIGHMEM);
2054 if (!pages[i]) {
2055 rc = -ENOMEM;
2056 goto discard_data;
2057 }
2058 }
2059
2060 /* read read data into pages */
2061 rc = read_data_into_pages(server, pages, npages, len);
2062 if (rc)
2063 goto free_pages;
2064
2065 rc = cifs_discard_remaining_data(server);
2066 if (rc)
2067 goto free_pages;
2068
2069 rc = decrypt_raw_data(server, buf, server->vals->read_rsp_size - 4,
2070 pages, npages, len);
2071 if (rc)
2072 goto free_pages;
2073
2074 *mid = smb2_find_mid(server, buf);
2075 if (*mid == NULL)
2076 cifs_dbg(FYI, "mid not found\n");
2077 else {
2078 cifs_dbg(FYI, "mid found\n");
2079 (*mid)->decrypted = true;
2080 rc = handle_read_data(server, *mid, buf,
2081 server->vals->read_rsp_size,
2082 pages, npages, len);
2083 }
2084
2085free_pages:
2086 for (i = i - 1; i >= 0; i--)
2087 put_page(pages[i]);
2088 kfree(pages);
2089 return rc;
2090discard_data:
2091 cifs_discard_remaining_data(server);
2092 goto free_pages;
2093}
2094
2095static int
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002096receive_encrypted_standard(struct TCP_Server_Info *server,
2097 struct mid_q_entry **mid)
2098{
2099 int length;
2100 char *buf = server->smallbuf;
2101 unsigned int pdu_length = get_rfc1002_length(buf);
2102 unsigned int buf_size;
2103 struct mid_q_entry *mid_entry;
2104
2105 /* switch to large buffer if too big for a small one */
2106 if (pdu_length + 4 > MAX_CIFS_SMALL_BUFFER_SIZE) {
2107 server->large_buf = true;
2108 memcpy(server->bigbuf, buf, server->total_read);
2109 buf = server->bigbuf;
2110 }
2111
2112 /* now read the rest */
2113 length = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1,
2114 pdu_length - HEADER_SIZE(server) + 1 + 4);
2115 if (length < 0)
2116 return length;
2117 server->total_read += length;
2118
2119 buf_size = pdu_length + 4 - sizeof(struct smb2_transform_hdr);
2120 length = decrypt_raw_data(server, buf, buf_size, NULL, 0, 0);
2121 if (length)
2122 return length;
2123
2124 mid_entry = smb2_find_mid(server, buf);
2125 if (mid_entry == NULL)
2126 cifs_dbg(FYI, "mid not found\n");
2127 else {
2128 cifs_dbg(FYI, "mid found\n");
2129 mid_entry->decrypted = true;
2130 }
2131
2132 *mid = mid_entry;
2133
2134 if (mid_entry && mid_entry->handle)
2135 return mid_entry->handle(server, mid_entry);
2136
2137 return cifs_handle_standard(server, mid_entry);
2138}
2139
2140static int
2141smb3_receive_transform(struct TCP_Server_Info *server, struct mid_q_entry **mid)
2142{
2143 char *buf = server->smallbuf;
2144 unsigned int pdu_length = get_rfc1002_length(buf);
2145 struct smb2_transform_hdr *tr_hdr = (struct smb2_transform_hdr *)buf;
2146 unsigned int orig_len = le32_to_cpu(tr_hdr->OriginalMessageSize);
2147
2148 if (pdu_length + 4 < sizeof(struct smb2_transform_hdr) +
2149 sizeof(struct smb2_sync_hdr)) {
2150 cifs_dbg(VFS, "Transform message is too small (%u)\n",
2151 pdu_length);
2152 cifs_reconnect(server);
2153 wake_up(&server->response_q);
2154 return -ECONNABORTED;
2155 }
2156
2157 if (pdu_length + 4 < orig_len + sizeof(struct smb2_transform_hdr)) {
2158 cifs_dbg(VFS, "Transform message is broken\n");
2159 cifs_reconnect(server);
2160 wake_up(&server->response_q);
2161 return -ECONNABORTED;
2162 }
2163
Pavel Shilovskyc42a6ab2016-11-17 16:20:23 -08002164 if (pdu_length + 4 > CIFSMaxBufSize + MAX_HEADER_SIZE(server))
2165 return receive_encrypted_read(server, mid);
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002166
2167 return receive_encrypted_standard(server, mid);
2168}
2169
2170int
2171smb3_handle_read_data(struct TCP_Server_Info *server, struct mid_q_entry *mid)
2172{
2173 char *buf = server->large_buf ? server->bigbuf : server->smallbuf;
2174
2175 return handle_read_data(server, mid, buf, get_rfc1002_length(buf) + 4,
2176 NULL, 0, 0);
2177}
2178
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04002179struct smb_version_operations smb20_operations = {
2180 .compare_fids = smb2_compare_fids,
2181 .setup_request = smb2_setup_request,
2182 .setup_async_request = smb2_setup_async_request,
2183 .check_receive = smb2_check_receive,
2184 .add_credits = smb2_add_credits,
2185 .set_credits = smb2_set_credits,
2186 .get_credits_field = smb2_get_credits_field,
2187 .get_credits = smb2_get_credits,
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002188 .wait_mtu_credits = cifs_wait_mtu_credits,
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04002189 .get_next_mid = smb2_get_next_mid,
2190 .read_data_offset = smb2_read_data_offset,
2191 .read_data_length = smb2_read_data_length,
2192 .map_error = map_smb2_to_linux_error,
2193 .find_mid = smb2_find_mid,
2194 .check_message = smb2_check_message,
2195 .dump_detail = smb2_dump_detail,
2196 .clear_stats = smb2_clear_stats,
2197 .print_stats = smb2_print_stats,
2198 .is_oplock_break = smb2_is_valid_oplock_break,
Sachin Prabhuc11f1df2014-03-11 16:11:47 +00002199 .downgrade_oplock = smb2_downgrade_oplock,
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04002200 .need_neg = smb2_need_neg,
2201 .negotiate = smb2_negotiate,
2202 .negotiate_wsize = smb2_negotiate_wsize,
2203 .negotiate_rsize = smb2_negotiate_rsize,
2204 .sess_setup = SMB2_sess_setup,
2205 .logoff = SMB2_logoff,
2206 .tree_connect = SMB2_tcon,
2207 .tree_disconnect = SMB2_tdis,
Steve French34f62642013-10-09 02:07:00 -05002208 .qfs_tcon = smb2_qfs_tcon,
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04002209 .is_path_accessible = smb2_is_path_accessible,
2210 .can_echo = smb2_can_echo,
2211 .echo = SMB2_echo,
2212 .query_path_info = smb2_query_path_info,
2213 .get_srv_inum = smb2_get_srv_inum,
2214 .query_file_info = smb2_query_file_info,
2215 .set_path_size = smb2_set_path_size,
2216 .set_file_size = smb2_set_file_size,
2217 .set_file_info = smb2_set_file_info,
Steve French64a5cfa2013-10-14 15:31:32 -05002218 .set_compression = smb2_set_compression,
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04002219 .mkdir = smb2_mkdir,
2220 .mkdir_setinfo = smb2_mkdir_setinfo,
2221 .rmdir = smb2_rmdir,
2222 .unlink = smb2_unlink,
2223 .rename = smb2_rename_path,
2224 .create_hardlink = smb2_create_hardlink,
2225 .query_symlink = smb2_query_symlink,
Sachin Prabhu5b23c972016-07-11 16:53:20 +01002226 .query_mf_symlink = smb3_query_mf_symlink,
2227 .create_mf_symlink = smb3_create_mf_symlink,
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04002228 .open = smb2_open_file,
2229 .set_fid = smb2_set_fid,
2230 .close = smb2_close_file,
2231 .flush = smb2_flush_file,
2232 .async_readv = smb2_async_readv,
2233 .async_writev = smb2_async_writev,
2234 .sync_read = smb2_sync_read,
2235 .sync_write = smb2_sync_write,
2236 .query_dir_first = smb2_query_dir_first,
2237 .query_dir_next = smb2_query_dir_next,
2238 .close_dir = smb2_close_dir,
2239 .calc_smb_size = smb2_calc_size,
2240 .is_status_pending = smb2_is_status_pending,
2241 .oplock_response = smb2_oplock_response,
2242 .queryfs = smb2_queryfs,
2243 .mand_lock = smb2_mand_lock,
2244 .mand_unlock_range = smb2_unlock_range,
2245 .push_mand_locks = smb2_push_mandatory_locks,
2246 .get_lease_key = smb2_get_lease_key,
2247 .set_lease_key = smb2_set_lease_key,
2248 .new_lease_key = smb2_new_lease_key,
2249 .calc_signature = smb2_calc_signature,
2250 .is_read_op = smb2_is_read_op,
2251 .set_oplock_level = smb2_set_oplock_level,
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04002252 .create_lease_buf = smb2_create_lease_buf,
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04002253 .parse_lease_buf = smb2_parse_lease_buf,
Steve French41c13582013-11-14 00:05:36 -06002254 .clone_range = smb2_clone_range,
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04002255 .wp_retry_size = smb2_wp_retry_size,
Pavel Shilovsky52755802014-08-18 20:49:57 +04002256 .dir_needs_close = smb2_dir_needs_close,
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04002257};
2258
Steve French1080ef72011-02-24 18:07:19 +00002259struct smb_version_operations smb21_operations = {
Pavel Shilovsky027e8ee2012-09-19 06:22:43 -07002260 .compare_fids = smb2_compare_fids,
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +04002261 .setup_request = smb2_setup_request,
Pavel Shilovskyc95b8ee2012-07-11 14:45:28 +04002262 .setup_async_request = smb2_setup_async_request,
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +04002263 .check_receive = smb2_check_receive,
Pavel Shilovsky28ea5292012-05-23 16:18:00 +04002264 .add_credits = smb2_add_credits,
2265 .set_credits = smb2_set_credits,
2266 .get_credits_field = smb2_get_credits_field,
2267 .get_credits = smb2_get_credits,
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002268 .wait_mtu_credits = smb2_wait_mtu_credits,
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +04002269 .get_next_mid = smb2_get_next_mid,
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002270 .read_data_offset = smb2_read_data_offset,
2271 .read_data_length = smb2_read_data_length,
2272 .map_error = map_smb2_to_linux_error,
Pavel Shilovsky093b2bd2011-06-08 15:51:07 +04002273 .find_mid = smb2_find_mid,
2274 .check_message = smb2_check_message,
2275 .dump_detail = smb2_dump_detail,
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04002276 .clear_stats = smb2_clear_stats,
2277 .print_stats = smb2_print_stats,
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07002278 .is_oplock_break = smb2_is_valid_oplock_break,
Sachin Prabhuc11f1df2014-03-11 16:11:47 +00002279 .downgrade_oplock = smb2_downgrade_oplock,
Pavel Shilovskyec2e4522011-12-27 16:12:43 +04002280 .need_neg = smb2_need_neg,
2281 .negotiate = smb2_negotiate,
Pavel Shilovsky3a3bab52012-09-18 16:20:28 -07002282 .negotiate_wsize = smb2_negotiate_wsize,
2283 .negotiate_rsize = smb2_negotiate_rsize,
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04002284 .sess_setup = SMB2_sess_setup,
2285 .logoff = SMB2_logoff,
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04002286 .tree_connect = SMB2_tcon,
2287 .tree_disconnect = SMB2_tdis,
Steve French34f62642013-10-09 02:07:00 -05002288 .qfs_tcon = smb2_qfs_tcon,
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04002289 .is_path_accessible = smb2_is_path_accessible,
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04002290 .can_echo = smb2_can_echo,
2291 .echo = SMB2_echo,
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04002292 .query_path_info = smb2_query_path_info,
2293 .get_srv_inum = smb2_get_srv_inum,
Pavel Shilovskyb7546bc2012-09-18 16:20:27 -07002294 .query_file_info = smb2_query_file_info,
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07002295 .set_path_size = smb2_set_path_size,
2296 .set_file_size = smb2_set_file_size,
Pavel Shilovsky1feeaac2012-09-18 16:20:32 -07002297 .set_file_info = smb2_set_file_info,
Steve French64a5cfa2013-10-14 15:31:32 -05002298 .set_compression = smb2_set_compression,
Pavel Shilovskya0e73182011-07-19 12:56:37 +04002299 .mkdir = smb2_mkdir,
2300 .mkdir_setinfo = smb2_mkdir_setinfo,
Pavel Shilovsky1a500f02012-07-10 16:14:38 +04002301 .rmdir = smb2_rmdir,
Pavel Shilovskycbe6f432012-09-18 16:20:25 -07002302 .unlink = smb2_unlink,
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07002303 .rename = smb2_rename_path,
Pavel Shilovsky568798c2012-09-18 16:20:31 -07002304 .create_hardlink = smb2_create_hardlink,
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04002305 .query_symlink = smb2_query_symlink,
Steve Frenchc22870e2014-09-16 07:18:19 -05002306 .query_mf_symlink = smb3_query_mf_symlink,
Steve French5ab97572014-09-15 04:49:28 -05002307 .create_mf_symlink = smb3_create_mf_symlink,
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07002308 .open = smb2_open_file,
2309 .set_fid = smb2_set_fid,
2310 .close = smb2_close_file,
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07002311 .flush = smb2_flush_file,
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002312 .async_readv = smb2_async_readv,
Pavel Shilovsky33319142012-09-18 16:20:29 -07002313 .async_writev = smb2_async_writev,
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07002314 .sync_read = smb2_sync_read,
Pavel Shilovsky009d3442012-09-18 16:20:30 -07002315 .sync_write = smb2_sync_write,
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07002316 .query_dir_first = smb2_query_dir_first,
2317 .query_dir_next = smb2_query_dir_next,
2318 .close_dir = smb2_close_dir,
2319 .calc_smb_size = smb2_calc_size,
Pavel Shilovsky2e44b282012-09-18 16:20:33 -07002320 .is_status_pending = smb2_is_status_pending,
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07002321 .oplock_response = smb2_oplock_response,
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07002322 .queryfs = smb2_queryfs,
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07002323 .mand_lock = smb2_mand_lock,
2324 .mand_unlock_range = smb2_unlock_range,
Pavel Shilovskyb1407992012-09-19 06:22:44 -07002325 .push_mand_locks = smb2_push_mandatory_locks,
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07002326 .get_lease_key = smb2_get_lease_key,
2327 .set_lease_key = smb2_set_lease_key,
2328 .new_lease_key = smb2_new_lease_key,
Steve French38107d42012-12-08 22:08:06 -06002329 .calc_signature = smb2_calc_signature,
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04002330 .is_read_op = smb21_is_read_op,
2331 .set_oplock_level = smb21_set_oplock_level,
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04002332 .create_lease_buf = smb2_create_lease_buf,
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04002333 .parse_lease_buf = smb2_parse_lease_buf,
Steve French41c13582013-11-14 00:05:36 -06002334 .clone_range = smb2_clone_range,
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04002335 .wp_retry_size = smb2_wp_retry_size,
Pavel Shilovsky52755802014-08-18 20:49:57 +04002336 .dir_needs_close = smb2_dir_needs_close,
Steve French834170c2016-09-30 21:14:26 -05002337 .enum_snapshots = smb3_enum_snapshots,
Steve French38107d42012-12-08 22:08:06 -06002338};
2339
Steve French38107d42012-12-08 22:08:06 -06002340struct smb_version_operations smb30_operations = {
2341 .compare_fids = smb2_compare_fids,
2342 .setup_request = smb2_setup_request,
2343 .setup_async_request = smb2_setup_async_request,
2344 .check_receive = smb2_check_receive,
2345 .add_credits = smb2_add_credits,
2346 .set_credits = smb2_set_credits,
2347 .get_credits_field = smb2_get_credits_field,
2348 .get_credits = smb2_get_credits,
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04002349 .wait_mtu_credits = smb2_wait_mtu_credits,
Steve French38107d42012-12-08 22:08:06 -06002350 .get_next_mid = smb2_get_next_mid,
2351 .read_data_offset = smb2_read_data_offset,
2352 .read_data_length = smb2_read_data_length,
2353 .map_error = map_smb2_to_linux_error,
2354 .find_mid = smb2_find_mid,
2355 .check_message = smb2_check_message,
2356 .dump_detail = smb2_dump_detail,
2357 .clear_stats = smb2_clear_stats,
2358 .print_stats = smb2_print_stats,
Steve French769ee6a2013-06-19 14:15:30 -05002359 .dump_share_caps = smb2_dump_share_caps,
Steve French38107d42012-12-08 22:08:06 -06002360 .is_oplock_break = smb2_is_valid_oplock_break,
Sachin Prabhuc11f1df2014-03-11 16:11:47 +00002361 .downgrade_oplock = smb2_downgrade_oplock,
Steve French38107d42012-12-08 22:08:06 -06002362 .need_neg = smb2_need_neg,
2363 .negotiate = smb2_negotiate,
2364 .negotiate_wsize = smb2_negotiate_wsize,
2365 .negotiate_rsize = smb2_negotiate_rsize,
2366 .sess_setup = SMB2_sess_setup,
2367 .logoff = SMB2_logoff,
2368 .tree_connect = SMB2_tcon,
2369 .tree_disconnect = SMB2_tdis,
Steven Frenchaf6a12e2013-10-09 20:55:53 -05002370 .qfs_tcon = smb3_qfs_tcon,
Steve French38107d42012-12-08 22:08:06 -06002371 .is_path_accessible = smb2_is_path_accessible,
2372 .can_echo = smb2_can_echo,
2373 .echo = SMB2_echo,
2374 .query_path_info = smb2_query_path_info,
2375 .get_srv_inum = smb2_get_srv_inum,
2376 .query_file_info = smb2_query_file_info,
2377 .set_path_size = smb2_set_path_size,
2378 .set_file_size = smb2_set_file_size,
2379 .set_file_info = smb2_set_file_info,
Steve French64a5cfa2013-10-14 15:31:32 -05002380 .set_compression = smb2_set_compression,
Steve French38107d42012-12-08 22:08:06 -06002381 .mkdir = smb2_mkdir,
2382 .mkdir_setinfo = smb2_mkdir_setinfo,
2383 .rmdir = smb2_rmdir,
2384 .unlink = smb2_unlink,
2385 .rename = smb2_rename_path,
2386 .create_hardlink = smb2_create_hardlink,
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04002387 .query_symlink = smb2_query_symlink,
Steve Frenchc22870e2014-09-16 07:18:19 -05002388 .query_mf_symlink = smb3_query_mf_symlink,
Steve French5ab97572014-09-15 04:49:28 -05002389 .create_mf_symlink = smb3_create_mf_symlink,
Steve French38107d42012-12-08 22:08:06 -06002390 .open = smb2_open_file,
2391 .set_fid = smb2_set_fid,
2392 .close = smb2_close_file,
2393 .flush = smb2_flush_file,
2394 .async_readv = smb2_async_readv,
2395 .async_writev = smb2_async_writev,
2396 .sync_read = smb2_sync_read,
2397 .sync_write = smb2_sync_write,
2398 .query_dir_first = smb2_query_dir_first,
2399 .query_dir_next = smb2_query_dir_next,
2400 .close_dir = smb2_close_dir,
2401 .calc_smb_size = smb2_calc_size,
2402 .is_status_pending = smb2_is_status_pending,
2403 .oplock_response = smb2_oplock_response,
2404 .queryfs = smb2_queryfs,
2405 .mand_lock = smb2_mand_lock,
2406 .mand_unlock_range = smb2_unlock_range,
2407 .push_mand_locks = smb2_push_mandatory_locks,
2408 .get_lease_key = smb2_get_lease_key,
2409 .set_lease_key = smb2_set_lease_key,
2410 .new_lease_key = smb2_new_lease_key,
Steve French373512e2015-12-18 13:05:30 -06002411 .generate_signingkey = generate_smb30signingkey,
Steve French38107d42012-12-08 22:08:06 -06002412 .calc_signature = smb3_calc_signature,
Steve Frenchb3152e22015-06-24 03:17:02 -05002413 .set_integrity = smb3_set_integrity,
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04002414 .is_read_op = smb21_is_read_op,
Pavel Shilovsky42873b02013-09-05 21:30:16 +04002415 .set_oplock_level = smb3_set_oplock_level,
Pavel Shilovskyf0473902013-09-04 13:44:05 +04002416 .create_lease_buf = smb3_create_lease_buf,
2417 .parse_lease_buf = smb3_parse_lease_buf,
Steve French41c13582013-11-14 00:05:36 -06002418 .clone_range = smb2_clone_range,
Steve Frenchca9e7a12015-10-01 21:40:10 -05002419 .duplicate_extents = smb2_duplicate_extents,
Steve Frenchff1c0382013-11-19 23:44:46 -06002420 .validate_negotiate = smb3_validate_negotiate,
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04002421 .wp_retry_size = smb2_wp_retry_size,
Pavel Shilovsky52755802014-08-18 20:49:57 +04002422 .dir_needs_close = smb2_dir_needs_close,
Steve French31742c52014-08-17 08:38:47 -05002423 .fallocate = smb3_fallocate,
Steve French834170c2016-09-30 21:14:26 -05002424 .enum_snapshots = smb3_enum_snapshots,
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002425 .init_transform_rq = smb3_init_transform_rq,
2426 .free_transform_rq = smb3_free_transform_rq,
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002427 .is_transform_hdr = smb3_is_transform_hdr,
2428 .receive_transform = smb3_receive_transform,
Steve French1080ef72011-02-24 18:07:19 +00002429};
2430
Steve Frenchaab18932015-06-23 23:37:11 -05002431#ifdef CONFIG_CIFS_SMB311
2432struct smb_version_operations smb311_operations = {
2433 .compare_fids = smb2_compare_fids,
2434 .setup_request = smb2_setup_request,
2435 .setup_async_request = smb2_setup_async_request,
2436 .check_receive = smb2_check_receive,
2437 .add_credits = smb2_add_credits,
2438 .set_credits = smb2_set_credits,
2439 .get_credits_field = smb2_get_credits_field,
2440 .get_credits = smb2_get_credits,
2441 .wait_mtu_credits = smb2_wait_mtu_credits,
2442 .get_next_mid = smb2_get_next_mid,
2443 .read_data_offset = smb2_read_data_offset,
2444 .read_data_length = smb2_read_data_length,
2445 .map_error = map_smb2_to_linux_error,
2446 .find_mid = smb2_find_mid,
2447 .check_message = smb2_check_message,
2448 .dump_detail = smb2_dump_detail,
2449 .clear_stats = smb2_clear_stats,
2450 .print_stats = smb2_print_stats,
2451 .dump_share_caps = smb2_dump_share_caps,
2452 .is_oplock_break = smb2_is_valid_oplock_break,
2453 .downgrade_oplock = smb2_downgrade_oplock,
2454 .need_neg = smb2_need_neg,
2455 .negotiate = smb2_negotiate,
2456 .negotiate_wsize = smb2_negotiate_wsize,
2457 .negotiate_rsize = smb2_negotiate_rsize,
2458 .sess_setup = SMB2_sess_setup,
2459 .logoff = SMB2_logoff,
2460 .tree_connect = SMB2_tcon,
2461 .tree_disconnect = SMB2_tdis,
2462 .qfs_tcon = smb3_qfs_tcon,
2463 .is_path_accessible = smb2_is_path_accessible,
2464 .can_echo = smb2_can_echo,
2465 .echo = SMB2_echo,
2466 .query_path_info = smb2_query_path_info,
2467 .get_srv_inum = smb2_get_srv_inum,
2468 .query_file_info = smb2_query_file_info,
2469 .set_path_size = smb2_set_path_size,
2470 .set_file_size = smb2_set_file_size,
2471 .set_file_info = smb2_set_file_info,
2472 .set_compression = smb2_set_compression,
2473 .mkdir = smb2_mkdir,
2474 .mkdir_setinfo = smb2_mkdir_setinfo,
2475 .rmdir = smb2_rmdir,
2476 .unlink = smb2_unlink,
2477 .rename = smb2_rename_path,
2478 .create_hardlink = smb2_create_hardlink,
2479 .query_symlink = smb2_query_symlink,
2480 .query_mf_symlink = smb3_query_mf_symlink,
2481 .create_mf_symlink = smb3_create_mf_symlink,
2482 .open = smb2_open_file,
2483 .set_fid = smb2_set_fid,
2484 .close = smb2_close_file,
2485 .flush = smb2_flush_file,
2486 .async_readv = smb2_async_readv,
2487 .async_writev = smb2_async_writev,
2488 .sync_read = smb2_sync_read,
2489 .sync_write = smb2_sync_write,
2490 .query_dir_first = smb2_query_dir_first,
2491 .query_dir_next = smb2_query_dir_next,
2492 .close_dir = smb2_close_dir,
2493 .calc_smb_size = smb2_calc_size,
2494 .is_status_pending = smb2_is_status_pending,
2495 .oplock_response = smb2_oplock_response,
2496 .queryfs = smb2_queryfs,
2497 .mand_lock = smb2_mand_lock,
2498 .mand_unlock_range = smb2_unlock_range,
2499 .push_mand_locks = smb2_push_mandatory_locks,
2500 .get_lease_key = smb2_get_lease_key,
2501 .set_lease_key = smb2_set_lease_key,
2502 .new_lease_key = smb2_new_lease_key,
Steve French373512e2015-12-18 13:05:30 -06002503 .generate_signingkey = generate_smb311signingkey,
Steve Frenchaab18932015-06-23 23:37:11 -05002504 .calc_signature = smb3_calc_signature,
Steve Frenchb3152e22015-06-24 03:17:02 -05002505 .set_integrity = smb3_set_integrity,
Steve Frenchaab18932015-06-23 23:37:11 -05002506 .is_read_op = smb21_is_read_op,
2507 .set_oplock_level = smb3_set_oplock_level,
2508 .create_lease_buf = smb3_create_lease_buf,
2509 .parse_lease_buf = smb3_parse_lease_buf,
2510 .clone_range = smb2_clone_range,
Steve French02b16662015-06-27 21:18:36 -07002511 .duplicate_extents = smb2_duplicate_extents,
Steve Frenchaab18932015-06-23 23:37:11 -05002512/* .validate_negotiate = smb3_validate_negotiate, */ /* not used in 3.11 */
2513 .wp_retry_size = smb2_wp_retry_size,
2514 .dir_needs_close = smb2_dir_needs_close,
2515 .fallocate = smb3_fallocate,
Steve French834170c2016-09-30 21:14:26 -05002516 .enum_snapshots = smb3_enum_snapshots,
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002517 .init_transform_rq = smb3_init_transform_rq,
2518 .free_transform_rq = smb3_free_transform_rq,
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08002519 .is_transform_hdr = smb3_is_transform_hdr,
2520 .receive_transform = smb3_receive_transform,
Steve Frenchaab18932015-06-23 23:37:11 -05002521};
2522#endif /* CIFS_SMB311 */
2523
Steve Frenchdd446b12012-11-28 23:21:06 -06002524struct smb_version_values smb20_values = {
2525 .version_string = SMB20_VERSION_STRING,
2526 .protocol_id = SMB20_PROT_ID,
2527 .req_capabilities = 0, /* MBZ */
2528 .large_lock_type = 0,
2529 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
2530 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
2531 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
2532 .header_size = sizeof(struct smb2_hdr),
2533 .max_header_size = MAX_SMB2_HDR_SIZE,
2534 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
2535 .lock_cmd = SMB2_LOCK,
2536 .cap_unix = 0,
2537 .cap_nt_find = SMB2_NT_FIND,
2538 .cap_large_files = SMB2_LARGE_FILES,
Jeff Layton50285882013-06-27 12:45:00 -04002539 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
2540 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04002541 .create_lease_size = sizeof(struct create_lease),
Steve Frenchdd446b12012-11-28 23:21:06 -06002542};
2543
Steve French1080ef72011-02-24 18:07:19 +00002544struct smb_version_values smb21_values = {
2545 .version_string = SMB21_VERSION_STRING,
Steve Frenche4aa25e2012-10-01 12:26:22 -05002546 .protocol_id = SMB21_PROT_ID,
2547 .req_capabilities = 0, /* MBZ on negotiate req until SMB3 dialect */
2548 .large_lock_type = 0,
2549 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
2550 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
2551 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
2552 .header_size = sizeof(struct smb2_hdr),
2553 .max_header_size = MAX_SMB2_HDR_SIZE,
2554 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
2555 .lock_cmd = SMB2_LOCK,
2556 .cap_unix = 0,
2557 .cap_nt_find = SMB2_NT_FIND,
2558 .cap_large_files = SMB2_LARGE_FILES,
Jeff Layton50285882013-06-27 12:45:00 -04002559 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
2560 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04002561 .create_lease_size = sizeof(struct create_lease),
Steve Frenche4aa25e2012-10-01 12:26:22 -05002562};
2563
2564struct smb_version_values smb30_values = {
2565 .version_string = SMB30_VERSION_STRING,
2566 .protocol_id = SMB30_PROT_ID,
Steve French373512e2015-12-18 13:05:30 -06002567 .req_capabilities = SMB2_GLOBAL_CAP_DFS | SMB2_GLOBAL_CAP_LEASING | SMB2_GLOBAL_CAP_LARGE_MTU | SMB2_GLOBAL_CAP_PERSISTENT_HANDLES | SMB2_GLOBAL_CAP_ENCRYPTION,
Pavel Shilovsky027e8ee2012-09-19 06:22:43 -07002568 .large_lock_type = 0,
2569 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
2570 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
2571 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
Pavel Shilovsky093b2bd2011-06-08 15:51:07 +04002572 .header_size = sizeof(struct smb2_hdr),
2573 .max_header_size = MAX_SMB2_HDR_SIZE,
Pavel Shilovsky09a47072012-09-18 16:20:29 -07002574 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +04002575 .lock_cmd = SMB2_LOCK,
Pavel Shilovsky29e20f92012-07-13 13:58:14 +04002576 .cap_unix = 0,
2577 .cap_nt_find = SMB2_NT_FIND,
2578 .cap_large_files = SMB2_LARGE_FILES,
Jeff Layton50285882013-06-27 12:45:00 -04002579 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
2580 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
Pavel Shilovskyf0473902013-09-04 13:44:05 +04002581 .create_lease_size = sizeof(struct create_lease_v2),
Steve French1080ef72011-02-24 18:07:19 +00002582};
Steve French20b6d8b2013-06-12 22:48:41 -05002583
2584struct smb_version_values smb302_values = {
2585 .version_string = SMB302_VERSION_STRING,
2586 .protocol_id = SMB302_PROT_ID,
Steve French373512e2015-12-18 13:05:30 -06002587 .req_capabilities = SMB2_GLOBAL_CAP_DFS | SMB2_GLOBAL_CAP_LEASING | SMB2_GLOBAL_CAP_LARGE_MTU | SMB2_GLOBAL_CAP_PERSISTENT_HANDLES | SMB2_GLOBAL_CAP_ENCRYPTION,
Steve French20b6d8b2013-06-12 22:48:41 -05002588 .large_lock_type = 0,
2589 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
2590 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
2591 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
2592 .header_size = sizeof(struct smb2_hdr),
2593 .max_header_size = MAX_SMB2_HDR_SIZE,
2594 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
2595 .lock_cmd = SMB2_LOCK,
2596 .cap_unix = 0,
2597 .cap_nt_find = SMB2_NT_FIND,
2598 .cap_large_files = SMB2_LARGE_FILES,
Jeff Layton50285882013-06-27 12:45:00 -04002599 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
2600 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
Pavel Shilovskyf0473902013-09-04 13:44:05 +04002601 .create_lease_size = sizeof(struct create_lease_v2),
Steve French20b6d8b2013-06-12 22:48:41 -05002602};
Steve French5f7fbf72014-12-17 22:52:58 -06002603
2604#ifdef CONFIG_CIFS_SMB311
2605struct smb_version_values smb311_values = {
2606 .version_string = SMB311_VERSION_STRING,
2607 .protocol_id = SMB311_PROT_ID,
Steve Frenchb618f002015-11-03 09:15:03 -06002608 .req_capabilities = SMB2_GLOBAL_CAP_DFS | SMB2_GLOBAL_CAP_LEASING | SMB2_GLOBAL_CAP_LARGE_MTU | SMB2_GLOBAL_CAP_PERSISTENT_HANDLES,
Steve French5f7fbf72014-12-17 22:52:58 -06002609 .large_lock_type = 0,
2610 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
2611 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
2612 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
2613 .header_size = sizeof(struct smb2_hdr),
2614 .max_header_size = MAX_SMB2_HDR_SIZE,
2615 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
2616 .lock_cmd = SMB2_LOCK,
2617 .cap_unix = 0,
2618 .cap_nt_find = SMB2_NT_FIND,
2619 .cap_large_files = SMB2_LARGE_FILES,
2620 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
2621 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
2622 .create_lease_size = sizeof(struct create_lease_v2),
2623};
2624#endif /* SMB311 */