blob: 5d456ebb381386e5299cf61123400f4985480e9a [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>
Steve French1080ef72011-02-24 18:07:19 +000023#include "cifsglob.h"
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +040024#include "smb2pdu.h"
25#include "smb2proto.h"
Pavel Shilovsky28ea5292012-05-23 16:18:00 +040026#include "cifsproto.h"
27#include "cifs_debug.h"
Pavel Shilovskyb42bf882013-08-14 19:25:21 +040028#include "cifs_unicode.h"
Pavel Shilovsky2e44b282012-09-18 16:20:33 -070029#include "smb2status.h"
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -070030#include "smb2glob.h"
Steve French834170c2016-09-30 21:14:26 -050031#include "cifs_ioctl.h"
Pavel Shilovsky28ea5292012-05-23 16:18:00 +040032
33static int
34change_conf(struct TCP_Server_Info *server)
35{
36 server->credits += server->echo_credits + server->oplock_credits;
37 server->oplock_credits = server->echo_credits = 0;
38 switch (server->credits) {
39 case 0:
40 return -1;
41 case 1:
42 server->echoes = false;
43 server->oplocks = false;
Joe Perchesf96637b2013-05-04 22:12:25 -050044 cifs_dbg(VFS, "disabling echoes and oplocks\n");
Pavel Shilovsky28ea5292012-05-23 16:18:00 +040045 break;
46 case 2:
47 server->echoes = true;
48 server->oplocks = false;
49 server->echo_credits = 1;
Joe Perchesf96637b2013-05-04 22:12:25 -050050 cifs_dbg(FYI, "disabling oplocks\n");
Pavel Shilovsky28ea5292012-05-23 16:18:00 +040051 break;
52 default:
53 server->echoes = true;
Steve Frenche0ddde92015-09-22 09:29:38 -050054 if (enable_oplocks) {
55 server->oplocks = true;
56 server->oplock_credits = 1;
57 } else
58 server->oplocks = false;
59
Pavel Shilovsky28ea5292012-05-23 16:18:00 +040060 server->echo_credits = 1;
Pavel Shilovsky28ea5292012-05-23 16:18:00 +040061 }
62 server->credits -= server->echo_credits + server->oplock_credits;
63 return 0;
64}
65
66static void
67smb2_add_credits(struct TCP_Server_Info *server, const unsigned int add,
68 const int optype)
69{
70 int *val, rc = 0;
71 spin_lock(&server->req_lock);
72 val = server->ops->get_credits_field(server, optype);
73 *val += add;
Steve French141891f2016-09-23 00:44:16 -050074 if (*val > 65000) {
75 *val = 65000; /* Don't get near 64K credits, avoid srv bugs */
76 printk_once(KERN_WARNING "server overflowed SMB3 credits\n");
77 }
Pavel Shilovsky28ea5292012-05-23 16:18:00 +040078 server->in_flight--;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +040079 if (server->in_flight == 0 && (optype & CIFS_OP_MASK) != CIFS_NEG_OP)
Pavel Shilovsky28ea5292012-05-23 16:18:00 +040080 rc = change_conf(server);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -070081 /*
82 * Sometimes server returns 0 credits on oplock break ack - we need to
83 * rebalance credits in this case.
84 */
85 else if (server->in_flight > 0 && server->oplock_credits == 0 &&
86 server->oplocks) {
87 if (server->credits > 1) {
88 server->credits--;
89 server->oplock_credits++;
90 }
91 }
Pavel Shilovsky28ea5292012-05-23 16:18:00 +040092 spin_unlock(&server->req_lock);
93 wake_up(&server->request_q);
94 if (rc)
95 cifs_reconnect(server);
96}
97
98static void
99smb2_set_credits(struct TCP_Server_Info *server, const int val)
100{
101 spin_lock(&server->req_lock);
102 server->credits = val;
103 spin_unlock(&server->req_lock);
104}
105
106static int *
107smb2_get_credits_field(struct TCP_Server_Info *server, const int optype)
108{
109 switch (optype) {
110 case CIFS_ECHO_OP:
111 return &server->echo_credits;
112 case CIFS_OBREAK_OP:
113 return &server->oplock_credits;
114 default:
115 return &server->credits;
116 }
117}
118
119static unsigned int
120smb2_get_credits(struct mid_q_entry *mid)
121{
122 return le16_to_cpu(((struct smb2_hdr *)mid->resp_buf)->CreditRequest);
123}
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400124
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +0400125static int
126smb2_wait_mtu_credits(struct TCP_Server_Info *server, unsigned int size,
127 unsigned int *num, unsigned int *credits)
128{
129 int rc = 0;
130 unsigned int scredits;
131
132 spin_lock(&server->req_lock);
133 while (1) {
134 if (server->credits <= 0) {
135 spin_unlock(&server->req_lock);
136 cifs_num_waiters_inc(server);
137 rc = wait_event_killable(server->request_q,
138 has_credits(server, &server->credits));
139 cifs_num_waiters_dec(server);
140 if (rc)
141 return rc;
142 spin_lock(&server->req_lock);
143 } else {
144 if (server->tcpStatus == CifsExiting) {
145 spin_unlock(&server->req_lock);
146 return -ENOENT;
147 }
148
149 scredits = server->credits;
150 /* can deadlock with reopen */
151 if (scredits == 1) {
152 *num = SMB2_MAX_BUFFER_SIZE;
153 *credits = 0;
154 break;
155 }
156
157 /* leave one credit for a possible reopen */
158 scredits--;
159 *num = min_t(unsigned int, size,
160 scredits * SMB2_MAX_BUFFER_SIZE);
161
162 *credits = DIV_ROUND_UP(*num, SMB2_MAX_BUFFER_SIZE);
163 server->credits -= *credits;
164 server->in_flight++;
165 break;
166 }
167 }
168 spin_unlock(&server->req_lock);
169 return rc;
170}
171
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400172static __u64
173smb2_get_next_mid(struct TCP_Server_Info *server)
174{
175 __u64 mid;
176 /* for SMB2 we need the current value */
177 spin_lock(&GlobalMid_Lock);
178 mid = server->CurrentMid++;
179 spin_unlock(&GlobalMid_Lock);
180 return mid;
181}
Steve French1080ef72011-02-24 18:07:19 +0000182
Pavel Shilovsky093b2bd2011-06-08 15:51:07 +0400183static struct mid_q_entry *
184smb2_find_mid(struct TCP_Server_Info *server, char *buf)
185{
186 struct mid_q_entry *mid;
187 struct smb2_hdr *hdr = (struct smb2_hdr *)buf;
Sachin Prabhu9235d092014-12-09 17:37:00 +0000188 __u64 wire_mid = le64_to_cpu(hdr->MessageId);
Pavel Shilovsky093b2bd2011-06-08 15:51:07 +0400189
Steve French373512e2015-12-18 13:05:30 -0600190 if (hdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM) {
191 cifs_dbg(VFS, "encrypted frame parsing not supported yet");
192 return NULL;
193 }
194
Pavel Shilovsky093b2bd2011-06-08 15:51:07 +0400195 spin_lock(&GlobalMid_Lock);
196 list_for_each_entry(mid, &server->pending_mid_q, qhead) {
Sachin Prabhu9235d092014-12-09 17:37:00 +0000197 if ((mid->mid == wire_mid) &&
Pavel Shilovsky093b2bd2011-06-08 15:51:07 +0400198 (mid->mid_state == MID_REQUEST_SUBMITTED) &&
199 (mid->command == hdr->Command)) {
200 spin_unlock(&GlobalMid_Lock);
201 return mid;
202 }
203 }
204 spin_unlock(&GlobalMid_Lock);
205 return NULL;
206}
207
208static void
209smb2_dump_detail(void *buf)
210{
211#ifdef CONFIG_CIFS_DEBUG2
212 struct smb2_hdr *smb = (struct smb2_hdr *)buf;
213
Joe Perchesf96637b2013-05-04 22:12:25 -0500214 cifs_dbg(VFS, "Cmd: %d Err: 0x%x Flags: 0x%x Mid: %llu Pid: %d\n",
215 smb->Command, smb->Status, smb->Flags, smb->MessageId,
216 smb->ProcessId);
217 cifs_dbg(VFS, "smb buf %p len %u\n", smb, smb2_calc_size(smb));
Pavel Shilovsky093b2bd2011-06-08 15:51:07 +0400218#endif
219}
220
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400221static bool
222smb2_need_neg(struct TCP_Server_Info *server)
223{
224 return server->max_read == 0;
225}
226
227static int
228smb2_negotiate(const unsigned int xid, struct cifs_ses *ses)
229{
230 int rc;
231 ses->server->CurrentMid = 0;
232 rc = SMB2_negotiate(xid, ses);
233 /* BB we probably don't need to retry with modern servers */
234 if (rc == -EAGAIN)
235 rc = -EHOSTDOWN;
236 return rc;
237}
238
Pavel Shilovsky3a3bab52012-09-18 16:20:28 -0700239static unsigned int
240smb2_negotiate_wsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
241{
242 struct TCP_Server_Info *server = tcon->ses->server;
243 unsigned int wsize;
244
245 /* start with specified wsize, or default */
246 wsize = volume_info->wsize ? volume_info->wsize : CIFS_DEFAULT_IOSIZE;
247 wsize = min_t(unsigned int, wsize, server->max_write);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +0400248
249 if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
250 wsize = min_t(unsigned int, wsize, SMB2_MAX_BUFFER_SIZE);
Pavel Shilovsky3a3bab52012-09-18 16:20:28 -0700251
Pavel Shilovsky3a3bab52012-09-18 16:20:28 -0700252 return wsize;
253}
254
255static unsigned int
256smb2_negotiate_rsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
257{
258 struct TCP_Server_Info *server = tcon->ses->server;
259 unsigned int rsize;
260
261 /* start with specified rsize, or default */
262 rsize = volume_info->rsize ? volume_info->rsize : CIFS_DEFAULT_IOSIZE;
263 rsize = min_t(unsigned int, rsize, server->max_read);
Pavel Shilovskybed9da02014-06-25 11:28:57 +0400264
265 if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
266 rsize = min_t(unsigned int, rsize, SMB2_MAX_BUFFER_SIZE);
Pavel Shilovsky3a3bab52012-09-18 16:20:28 -0700267
Pavel Shilovsky3a3bab52012-09-18 16:20:28 -0700268 return rsize;
269}
270
Steve Frenchc481e9f2013-10-14 01:21:53 -0500271#ifdef CONFIG_CIFS_STATS2
272static int
273SMB3_request_interfaces(const unsigned int xid, struct cifs_tcon *tcon)
274{
275 int rc;
276 unsigned int ret_data_len = 0;
277 struct network_interface_info_ioctl_rsp *out_buf;
278
279 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
280 FSCTL_QUERY_NETWORK_INTERFACE_INFO, true /* is_fsctl */,
281 NULL /* no data input */, 0 /* no data input */,
282 (char **)&out_buf, &ret_data_len);
Steve French9ffc5412014-10-16 15:13:14 -0500283 if (rc != 0)
284 cifs_dbg(VFS, "error %d on ioctl to get interface list\n", rc);
285 else if (ret_data_len < sizeof(struct network_interface_info_ioctl_rsp)) {
286 cifs_dbg(VFS, "server returned bad net interface info buf\n");
287 rc = -EINVAL;
288 } else {
Steve Frenchc481e9f2013-10-14 01:21:53 -0500289 /* Dump info on first interface */
290 cifs_dbg(FYI, "Adapter Capability 0x%x\t",
291 le32_to_cpu(out_buf->Capability));
292 cifs_dbg(FYI, "Link Speed %lld\n",
293 le64_to_cpu(out_buf->LinkSpeed));
Steve French9ffc5412014-10-16 15:13:14 -0500294 }
Steve French24df1482016-09-29 04:20:23 -0500295 kfree(out_buf);
Steve Frenchc481e9f2013-10-14 01:21:53 -0500296 return rc;
297}
298#endif /* STATS2 */
299
Steve French34f62642013-10-09 02:07:00 -0500300static void
Steven Frenchaf6a12e2013-10-09 20:55:53 -0500301smb3_qfs_tcon(const unsigned int xid, struct cifs_tcon *tcon)
302{
303 int rc;
304 __le16 srch_path = 0; /* Null - open root of share */
305 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
306 struct cifs_open_parms oparms;
307 struct cifs_fid fid;
308
309 oparms.tcon = tcon;
310 oparms.desired_access = FILE_READ_ATTRIBUTES;
311 oparms.disposition = FILE_OPEN;
312 oparms.create_options = 0;
313 oparms.fid = &fid;
314 oparms.reconnect = false;
315
316 rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL);
317 if (rc)
318 return;
319
Steve Frenchc481e9f2013-10-14 01:21:53 -0500320#ifdef CONFIG_CIFS_STATS2
321 SMB3_request_interfaces(xid, tcon);
322#endif /* STATS2 */
323
Steven Frenchaf6a12e2013-10-09 20:55:53 -0500324 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
325 FS_ATTRIBUTE_INFORMATION);
326 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
327 FS_DEVICE_INFORMATION);
328 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
329 FS_SECTOR_SIZE_INFORMATION); /* SMB3 specific */
330 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
331 return;
332}
333
334static void
Steve French34f62642013-10-09 02:07:00 -0500335smb2_qfs_tcon(const unsigned int xid, struct cifs_tcon *tcon)
336{
337 int rc;
338 __le16 srch_path = 0; /* Null - open root of share */
339 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
340 struct cifs_open_parms oparms;
341 struct cifs_fid fid;
342
343 oparms.tcon = tcon;
344 oparms.desired_access = FILE_READ_ATTRIBUTES;
345 oparms.disposition = FILE_OPEN;
346 oparms.create_options = 0;
347 oparms.fid = &fid;
348 oparms.reconnect = false;
349
350 rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL);
351 if (rc)
352 return;
353
Steven French21671142013-10-09 13:36:35 -0500354 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
355 FS_ATTRIBUTE_INFORMATION);
356 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
357 FS_DEVICE_INFORMATION);
Steve French34f62642013-10-09 02:07:00 -0500358 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
359 return;
360}
361
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +0400362static int
363smb2_is_path_accessible(const unsigned int xid, struct cifs_tcon *tcon,
364 struct cifs_sb_info *cifs_sb, const char *full_path)
365{
366 int rc;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +0400367 __le16 *utf16_path;
Pavel Shilovsky2e44b282012-09-18 16:20:33 -0700368 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
Pavel Shilovsky064f6042013-07-09 18:20:30 +0400369 struct cifs_open_parms oparms;
370 struct cifs_fid fid;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +0400371
372 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
373 if (!utf16_path)
374 return -ENOMEM;
375
Pavel Shilovsky064f6042013-07-09 18:20:30 +0400376 oparms.tcon = tcon;
377 oparms.desired_access = FILE_READ_ATTRIBUTES;
378 oparms.disposition = FILE_OPEN;
379 oparms.create_options = 0;
380 oparms.fid = &fid;
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +0400381 oparms.reconnect = false;
Pavel Shilovsky064f6042013-07-09 18:20:30 +0400382
Pavel Shilovskyb42bf882013-08-14 19:25:21 +0400383 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +0400384 if (rc) {
385 kfree(utf16_path);
386 return rc;
387 }
388
Pavel Shilovsky064f6042013-07-09 18:20:30 +0400389 rc = SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +0400390 kfree(utf16_path);
391 return rc;
392}
393
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +0400394static int
395smb2_get_srv_inum(const unsigned int xid, struct cifs_tcon *tcon,
396 struct cifs_sb_info *cifs_sb, const char *full_path,
397 u64 *uniqueid, FILE_ALL_INFO *data)
398{
399 *uniqueid = le64_to_cpu(data->IndexNumber);
400 return 0;
401}
402
Pavel Shilovskyb7546bc2012-09-18 16:20:27 -0700403static int
404smb2_query_file_info(const unsigned int xid, struct cifs_tcon *tcon,
405 struct cifs_fid *fid, FILE_ALL_INFO *data)
406{
407 int rc;
408 struct smb2_file_all_info *smb2_data;
409
Pavel Shilovsky1bbe4992014-08-22 13:32:11 +0400410 smb2_data = kzalloc(sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
Pavel Shilovskyb7546bc2012-09-18 16:20:27 -0700411 GFP_KERNEL);
412 if (smb2_data == NULL)
413 return -ENOMEM;
414
415 rc = SMB2_query_info(xid, tcon, fid->persistent_fid, fid->volatile_fid,
416 smb2_data);
417 if (!rc)
418 move_smb2_info_to_cifs(data, smb2_data);
419 kfree(smb2_data);
420 return rc;
421}
422
Pavel Shilovsky9094fad2012-07-12 18:30:44 +0400423static bool
424smb2_can_echo(struct TCP_Server_Info *server)
425{
426 return server->echoes;
427}
428
Pavel Shilovskyd60622e2012-05-28 15:19:39 +0400429static void
430smb2_clear_stats(struct cifs_tcon *tcon)
431{
432#ifdef CONFIG_CIFS_STATS
433 int i;
434 for (i = 0; i < NUMBER_OF_SMB2_COMMANDS; i++) {
435 atomic_set(&tcon->stats.smb2_stats.smb2_com_sent[i], 0);
436 atomic_set(&tcon->stats.smb2_stats.smb2_com_failed[i], 0);
437 }
438#endif
439}
440
441static void
Steve French769ee6a2013-06-19 14:15:30 -0500442smb2_dump_share_caps(struct seq_file *m, struct cifs_tcon *tcon)
443{
444 seq_puts(m, "\n\tShare Capabilities:");
445 if (tcon->capabilities & SMB2_SHARE_CAP_DFS)
446 seq_puts(m, " DFS,");
447 if (tcon->capabilities & SMB2_SHARE_CAP_CONTINUOUS_AVAILABILITY)
448 seq_puts(m, " CONTINUOUS AVAILABILITY,");
449 if (tcon->capabilities & SMB2_SHARE_CAP_SCALEOUT)
450 seq_puts(m, " SCALEOUT,");
451 if (tcon->capabilities & SMB2_SHARE_CAP_CLUSTER)
452 seq_puts(m, " CLUSTER,");
453 if (tcon->capabilities & SMB2_SHARE_CAP_ASYMMETRIC)
454 seq_puts(m, " ASYMMETRIC,");
455 if (tcon->capabilities == 0)
456 seq_puts(m, " None");
Steven Frenchaf6a12e2013-10-09 20:55:53 -0500457 if (tcon->ss_flags & SSINFO_FLAGS_ALIGNED_DEVICE)
458 seq_puts(m, " Aligned,");
459 if (tcon->ss_flags & SSINFO_FLAGS_PARTITION_ALIGNED_ON_DEVICE)
460 seq_puts(m, " Partition Aligned,");
461 if (tcon->ss_flags & SSINFO_FLAGS_NO_SEEK_PENALTY)
462 seq_puts(m, " SSD,");
463 if (tcon->ss_flags & SSINFO_FLAGS_TRIM_ENABLED)
464 seq_puts(m, " TRIM-support,");
465
Steve French769ee6a2013-06-19 14:15:30 -0500466 seq_printf(m, "\tShare Flags: 0x%x", tcon->share_flags);
Steven Frenchaf6a12e2013-10-09 20:55:53 -0500467 if (tcon->perf_sector_size)
468 seq_printf(m, "\tOptimal sector size: 0x%x",
469 tcon->perf_sector_size);
Steve French769ee6a2013-06-19 14:15:30 -0500470}
471
472static void
Pavel Shilovskyd60622e2012-05-28 15:19:39 +0400473smb2_print_stats(struct seq_file *m, struct cifs_tcon *tcon)
474{
475#ifdef CONFIG_CIFS_STATS
476 atomic_t *sent = tcon->stats.smb2_stats.smb2_com_sent;
477 atomic_t *failed = tcon->stats.smb2_stats.smb2_com_failed;
478 seq_printf(m, "\nNegotiates: %d sent %d failed",
479 atomic_read(&sent[SMB2_NEGOTIATE_HE]),
480 atomic_read(&failed[SMB2_NEGOTIATE_HE]));
481 seq_printf(m, "\nSessionSetups: %d sent %d failed",
482 atomic_read(&sent[SMB2_SESSION_SETUP_HE]),
483 atomic_read(&failed[SMB2_SESSION_SETUP_HE]));
Pavel Shilovskyd60622e2012-05-28 15:19:39 +0400484 seq_printf(m, "\nLogoffs: %d sent %d failed",
485 atomic_read(&sent[SMB2_LOGOFF_HE]),
486 atomic_read(&failed[SMB2_LOGOFF_HE]));
487 seq_printf(m, "\nTreeConnects: %d sent %d failed",
488 atomic_read(&sent[SMB2_TREE_CONNECT_HE]),
489 atomic_read(&failed[SMB2_TREE_CONNECT_HE]));
490 seq_printf(m, "\nTreeDisconnects: %d sent %d failed",
491 atomic_read(&sent[SMB2_TREE_DISCONNECT_HE]),
492 atomic_read(&failed[SMB2_TREE_DISCONNECT_HE]));
493 seq_printf(m, "\nCreates: %d sent %d failed",
494 atomic_read(&sent[SMB2_CREATE_HE]),
495 atomic_read(&failed[SMB2_CREATE_HE]));
496 seq_printf(m, "\nCloses: %d sent %d failed",
497 atomic_read(&sent[SMB2_CLOSE_HE]),
498 atomic_read(&failed[SMB2_CLOSE_HE]));
499 seq_printf(m, "\nFlushes: %d sent %d failed",
500 atomic_read(&sent[SMB2_FLUSH_HE]),
501 atomic_read(&failed[SMB2_FLUSH_HE]));
502 seq_printf(m, "\nReads: %d sent %d failed",
503 atomic_read(&sent[SMB2_READ_HE]),
504 atomic_read(&failed[SMB2_READ_HE]));
505 seq_printf(m, "\nWrites: %d sent %d failed",
506 atomic_read(&sent[SMB2_WRITE_HE]),
507 atomic_read(&failed[SMB2_WRITE_HE]));
508 seq_printf(m, "\nLocks: %d sent %d failed",
509 atomic_read(&sent[SMB2_LOCK_HE]),
510 atomic_read(&failed[SMB2_LOCK_HE]));
511 seq_printf(m, "\nIOCTLs: %d sent %d failed",
512 atomic_read(&sent[SMB2_IOCTL_HE]),
513 atomic_read(&failed[SMB2_IOCTL_HE]));
514 seq_printf(m, "\nCancels: %d sent %d failed",
515 atomic_read(&sent[SMB2_CANCEL_HE]),
516 atomic_read(&failed[SMB2_CANCEL_HE]));
517 seq_printf(m, "\nEchos: %d sent %d failed",
518 atomic_read(&sent[SMB2_ECHO_HE]),
519 atomic_read(&failed[SMB2_ECHO_HE]));
520 seq_printf(m, "\nQueryDirectories: %d sent %d failed",
521 atomic_read(&sent[SMB2_QUERY_DIRECTORY_HE]),
522 atomic_read(&failed[SMB2_QUERY_DIRECTORY_HE]));
523 seq_printf(m, "\nChangeNotifies: %d sent %d failed",
524 atomic_read(&sent[SMB2_CHANGE_NOTIFY_HE]),
525 atomic_read(&failed[SMB2_CHANGE_NOTIFY_HE]));
526 seq_printf(m, "\nQueryInfos: %d sent %d failed",
527 atomic_read(&sent[SMB2_QUERY_INFO_HE]),
528 atomic_read(&failed[SMB2_QUERY_INFO_HE]));
529 seq_printf(m, "\nSetInfos: %d sent %d failed",
530 atomic_read(&sent[SMB2_SET_INFO_HE]),
531 atomic_read(&failed[SMB2_SET_INFO_HE]));
532 seq_printf(m, "\nOplockBreaks: %d sent %d failed",
533 atomic_read(&sent[SMB2_OPLOCK_BREAK_HE]),
534 atomic_read(&failed[SMB2_OPLOCK_BREAK_HE]));
535#endif
536}
537
Pavel Shilovskyf0df7372012-09-18 16:20:26 -0700538static void
539smb2_set_fid(struct cifsFileInfo *cfile, struct cifs_fid *fid, __u32 oplock)
540{
David Howells2b0143b2015-03-17 22:25:59 +0000541 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
Pavel Shilovsky53ef1012013-09-05 16:11:28 +0400542 struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server;
543
Pavel Shilovskyf0df7372012-09-18 16:20:26 -0700544 cfile->fid.persistent_fid = fid->persistent_fid;
545 cfile->fid.volatile_fid = fid->volatile_fid;
Pavel Shilovsky42873b02013-09-05 21:30:16 +0400546 server->ops->set_oplock_level(cinode, oplock, fid->epoch,
547 &fid->purge_cache);
Pavel Shilovsky18cceb62013-09-05 13:01:06 +0400548 cinode->can_cache_brlcks = CIFS_CACHE_WRITE(cinode);
Aurelien Aptel94f87372016-09-22 07:38:50 +0200549 memcpy(cfile->fid.create_guid, fid->create_guid, 16);
Pavel Shilovskyf0df7372012-09-18 16:20:26 -0700550}
551
Pavel Shilovsky760ad0c2012-09-25 11:00:07 +0400552static void
Pavel Shilovskyf0df7372012-09-18 16:20:26 -0700553smb2_close_file(const unsigned int xid, struct cifs_tcon *tcon,
554 struct cifs_fid *fid)
555{
Pavel Shilovsky760ad0c2012-09-25 11:00:07 +0400556 SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
Pavel Shilovskyf0df7372012-09-18 16:20:26 -0700557}
558
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -0700559static int
Steve French41c13582013-11-14 00:05:36 -0600560SMB2_request_res_key(const unsigned int xid, struct cifs_tcon *tcon,
561 u64 persistent_fid, u64 volatile_fid,
562 struct copychunk_ioctl *pcchunk)
563{
564 int rc;
565 unsigned int ret_data_len;
566 struct resume_key_req *res_key;
567
568 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
569 FSCTL_SRV_REQUEST_RESUME_KEY, true /* is_fsctl */,
570 NULL, 0 /* no input */,
571 (char **)&res_key, &ret_data_len);
572
573 if (rc) {
574 cifs_dbg(VFS, "refcpy ioctl error %d getting resume key\n", rc);
575 goto req_res_key_exit;
576 }
577 if (ret_data_len < sizeof(struct resume_key_req)) {
578 cifs_dbg(VFS, "Invalid refcopy resume key length\n");
579 rc = -EINVAL;
580 goto req_res_key_exit;
581 }
582 memcpy(pcchunk->SourceKey, res_key->ResumeKey, COPY_CHUNK_RES_KEY_SIZE);
583
584req_res_key_exit:
585 kfree(res_key);
586 return rc;
587}
588
589static int
590smb2_clone_range(const unsigned int xid,
591 struct cifsFileInfo *srcfile,
592 struct cifsFileInfo *trgtfile, u64 src_off,
593 u64 len, u64 dest_off)
594{
595 int rc;
596 unsigned int ret_data_len;
597 struct copychunk_ioctl *pcchunk;
Steve French9bf0c9c2013-11-16 18:05:28 -0600598 struct copychunk_ioctl_rsp *retbuf = NULL;
599 struct cifs_tcon *tcon;
600 int chunks_copied = 0;
601 bool chunk_sizes_updated = false;
Steve French41c13582013-11-14 00:05:36 -0600602
603 pcchunk = kmalloc(sizeof(struct copychunk_ioctl), GFP_KERNEL);
604
605 if (pcchunk == NULL)
606 return -ENOMEM;
607
608 cifs_dbg(FYI, "in smb2_clone_range - about to call request res key\n");
609 /* Request a key from the server to identify the source of the copy */
610 rc = SMB2_request_res_key(xid, tlink_tcon(srcfile->tlink),
611 srcfile->fid.persistent_fid,
612 srcfile->fid.volatile_fid, pcchunk);
613
614 /* Note: request_res_key sets res_key null only if rc !=0 */
615 if (rc)
Steve French9bf0c9c2013-11-16 18:05:28 -0600616 goto cchunk_out;
Steve French41c13582013-11-14 00:05:36 -0600617
618 /* For now array only one chunk long, will make more flexible later */
Fabian Frederickbc09d142014-12-10 15:41:15 -0800619 pcchunk->ChunkCount = cpu_to_le32(1);
Steve French41c13582013-11-14 00:05:36 -0600620 pcchunk->Reserved = 0;
Steve French41c13582013-11-14 00:05:36 -0600621 pcchunk->Reserved2 = 0;
622
Steve French9bf0c9c2013-11-16 18:05:28 -0600623 tcon = tlink_tcon(trgtfile->tlink);
624
625 while (len > 0) {
626 pcchunk->SourceOffset = cpu_to_le64(src_off);
627 pcchunk->TargetOffset = cpu_to_le64(dest_off);
628 pcchunk->Length =
629 cpu_to_le32(min_t(u32, len, tcon->max_bytes_chunk));
630
631 /* Request server copy to target from src identified by key */
632 rc = SMB2_ioctl(xid, tcon, trgtfile->fid.persistent_fid,
Steve French41c13582013-11-14 00:05:36 -0600633 trgtfile->fid.volatile_fid, FSCTL_SRV_COPYCHUNK_WRITE,
634 true /* is_fsctl */, (char *)pcchunk,
Steve French9bf0c9c2013-11-16 18:05:28 -0600635 sizeof(struct copychunk_ioctl), (char **)&retbuf,
636 &ret_data_len);
637 if (rc == 0) {
638 if (ret_data_len !=
639 sizeof(struct copychunk_ioctl_rsp)) {
640 cifs_dbg(VFS, "invalid cchunk response size\n");
641 rc = -EIO;
642 goto cchunk_out;
643 }
644 if (retbuf->TotalBytesWritten == 0) {
645 cifs_dbg(FYI, "no bytes copied\n");
646 rc = -EIO;
647 goto cchunk_out;
648 }
649 /*
650 * Check if server claimed to write more than we asked
651 */
652 if (le32_to_cpu(retbuf->TotalBytesWritten) >
653 le32_to_cpu(pcchunk->Length)) {
654 cifs_dbg(VFS, "invalid copy chunk response\n");
655 rc = -EIO;
656 goto cchunk_out;
657 }
658 if (le32_to_cpu(retbuf->ChunksWritten) != 1) {
659 cifs_dbg(VFS, "invalid num chunks written\n");
660 rc = -EIO;
661 goto cchunk_out;
662 }
663 chunks_copied++;
Steve French41c13582013-11-14 00:05:36 -0600664
Steve French9bf0c9c2013-11-16 18:05:28 -0600665 src_off += le32_to_cpu(retbuf->TotalBytesWritten);
666 dest_off += le32_to_cpu(retbuf->TotalBytesWritten);
667 len -= le32_to_cpu(retbuf->TotalBytesWritten);
Steve French41c13582013-11-14 00:05:36 -0600668
Steve French9bf0c9c2013-11-16 18:05:28 -0600669 cifs_dbg(FYI, "Chunks %d PartialChunk %d Total %d\n",
670 le32_to_cpu(retbuf->ChunksWritten),
671 le32_to_cpu(retbuf->ChunkBytesWritten),
672 le32_to_cpu(retbuf->TotalBytesWritten));
673 } else if (rc == -EINVAL) {
674 if (ret_data_len != sizeof(struct copychunk_ioctl_rsp))
675 goto cchunk_out;
Steve French41c13582013-11-14 00:05:36 -0600676
Steve French9bf0c9c2013-11-16 18:05:28 -0600677 cifs_dbg(FYI, "MaxChunks %d BytesChunk %d MaxCopy %d\n",
678 le32_to_cpu(retbuf->ChunksWritten),
679 le32_to_cpu(retbuf->ChunkBytesWritten),
680 le32_to_cpu(retbuf->TotalBytesWritten));
681
682 /*
683 * Check if this is the first request using these sizes,
684 * (ie check if copy succeed once with original sizes
685 * and check if the server gave us different sizes after
686 * we already updated max sizes on previous request).
687 * if not then why is the server returning an error now
688 */
689 if ((chunks_copied != 0) || chunk_sizes_updated)
690 goto cchunk_out;
691
692 /* Check that server is not asking us to grow size */
693 if (le32_to_cpu(retbuf->ChunkBytesWritten) <
694 tcon->max_bytes_chunk)
695 tcon->max_bytes_chunk =
696 le32_to_cpu(retbuf->ChunkBytesWritten);
697 else
698 goto cchunk_out; /* server gave us bogus size */
699
700 /* No need to change MaxChunks since already set to 1 */
701 chunk_sizes_updated = true;
Sachin Prabhu2477bc52015-02-04 13:10:26 +0000702 } else
703 goto cchunk_out;
Steve French9bf0c9c2013-11-16 18:05:28 -0600704 }
705
706cchunk_out:
Steve French41c13582013-11-14 00:05:36 -0600707 kfree(pcchunk);
Steve French24df1482016-09-29 04:20:23 -0500708 kfree(retbuf);
Steve French41c13582013-11-14 00:05:36 -0600709 return rc;
710}
711
712static int
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -0700713smb2_flush_file(const unsigned int xid, struct cifs_tcon *tcon,
714 struct cifs_fid *fid)
715{
716 return SMB2_flush(xid, tcon, fid->persistent_fid, fid->volatile_fid);
717}
718
Pavel Shilovsky09a47072012-09-18 16:20:29 -0700719static unsigned int
720smb2_read_data_offset(char *buf)
721{
722 struct smb2_read_rsp *rsp = (struct smb2_read_rsp *)buf;
723 return rsp->DataOffset;
724}
725
726static unsigned int
727smb2_read_data_length(char *buf)
728{
729 struct smb2_read_rsp *rsp = (struct smb2_read_rsp *)buf;
730 return le32_to_cpu(rsp->DataLength);
731}
732
Pavel Shilovskyd8e05032012-09-18 16:20:30 -0700733
734static int
Steve Frenchdb8b6312014-09-22 05:13:55 -0500735smb2_sync_read(const unsigned int xid, struct cifs_fid *pfid,
Pavel Shilovskyd8e05032012-09-18 16:20:30 -0700736 struct cifs_io_parms *parms, unsigned int *bytes_read,
737 char **buf, int *buf_type)
738{
Steve Frenchdb8b6312014-09-22 05:13:55 -0500739 parms->persistent_fid = pfid->persistent_fid;
740 parms->volatile_fid = pfid->volatile_fid;
Pavel Shilovskyd8e05032012-09-18 16:20:30 -0700741 return SMB2_read(xid, parms, bytes_read, buf, buf_type);
742}
743
Pavel Shilovsky009d3442012-09-18 16:20:30 -0700744static int
Steve Frenchdb8b6312014-09-22 05:13:55 -0500745smb2_sync_write(const unsigned int xid, struct cifs_fid *pfid,
Pavel Shilovsky009d3442012-09-18 16:20:30 -0700746 struct cifs_io_parms *parms, unsigned int *written,
747 struct kvec *iov, unsigned long nr_segs)
748{
749
Steve Frenchdb8b6312014-09-22 05:13:55 -0500750 parms->persistent_fid = pfid->persistent_fid;
751 parms->volatile_fid = pfid->volatile_fid;
Pavel Shilovsky009d3442012-09-18 16:20:30 -0700752 return SMB2_write(xid, parms, written, iov, nr_segs);
753}
754
Steve Frenchd43cc792014-08-13 17:16:29 -0500755/* Set or clear the SPARSE_FILE attribute based on value passed in setsparse */
756static bool smb2_set_sparse(const unsigned int xid, struct cifs_tcon *tcon,
757 struct cifsFileInfo *cfile, struct inode *inode, __u8 setsparse)
758{
759 struct cifsInodeInfo *cifsi;
760 int rc;
761
762 cifsi = CIFS_I(inode);
763
764 /* if file already sparse don't bother setting sparse again */
765 if ((cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) && setsparse)
766 return true; /* already sparse */
767
768 if (!(cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) && !setsparse)
769 return true; /* already not sparse */
770
771 /*
772 * Can't check for sparse support on share the usual way via the
773 * FS attribute info (FILE_SUPPORTS_SPARSE_FILES) on the share
774 * since Samba server doesn't set the flag on the share, yet
775 * supports the set sparse FSCTL and returns sparse correctly
776 * in the file attributes. If we fail setting sparse though we
777 * mark that server does not support sparse files for this share
778 * to avoid repeatedly sending the unsupported fsctl to server
779 * if the file is repeatedly extended.
780 */
781 if (tcon->broken_sparse_sup)
782 return false;
783
784 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
785 cfile->fid.volatile_fid, FSCTL_SET_SPARSE,
786 true /* is_fctl */, &setsparse, 1, NULL, NULL);
787 if (rc) {
788 tcon->broken_sparse_sup = true;
789 cifs_dbg(FYI, "set sparse rc = %d\n", rc);
790 return false;
791 }
792
793 if (setsparse)
794 cifsi->cifsAttrs |= FILE_ATTRIBUTE_SPARSE_FILE;
795 else
796 cifsi->cifsAttrs &= (~FILE_ATTRIBUTE_SPARSE_FILE);
797
798 return true;
799}
800
Pavel Shilovskyc839ff22012-09-18 16:20:32 -0700801static int
802smb2_set_file_size(const unsigned int xid, struct cifs_tcon *tcon,
803 struct cifsFileInfo *cfile, __u64 size, bool set_alloc)
804{
805 __le64 eof = cpu_to_le64(size);
Steve French3d1a3742014-08-11 21:05:25 -0500806 struct inode *inode;
807
808 /*
809 * If extending file more than one page make sparse. Many Linux fs
810 * make files sparse by default when extending via ftruncate
811 */
David Howells2b0143b2015-03-17 22:25:59 +0000812 inode = d_inode(cfile->dentry);
Steve French3d1a3742014-08-11 21:05:25 -0500813
814 if (!set_alloc && (size > inode->i_size + 8192)) {
Steve French3d1a3742014-08-11 21:05:25 -0500815 __u8 set_sparse = 1;
Steve French3d1a3742014-08-11 21:05:25 -0500816
Steve Frenchd43cc792014-08-13 17:16:29 -0500817 /* whether set sparse succeeds or not, extend the file */
818 smb2_set_sparse(xid, tcon, cfile, inode, set_sparse);
Steve French3d1a3742014-08-11 21:05:25 -0500819 }
820
Pavel Shilovskyc839ff22012-09-18 16:20:32 -0700821 return SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
Steve Frenchf29ebb42014-07-19 21:44:58 -0500822 cfile->fid.volatile_fid, cfile->pid, &eof, false);
Pavel Shilovskyc839ff22012-09-18 16:20:32 -0700823}
824
Steve French02b16662015-06-27 21:18:36 -0700825static int
826smb2_duplicate_extents(const unsigned int xid,
827 struct cifsFileInfo *srcfile,
828 struct cifsFileInfo *trgtfile, u64 src_off,
829 u64 len, u64 dest_off)
830{
831 int rc;
832 unsigned int ret_data_len;
Steve French02b16662015-06-27 21:18:36 -0700833 struct duplicate_extents_to_file dup_ext_buf;
834 struct cifs_tcon *tcon = tlink_tcon(trgtfile->tlink);
835
836 /* server fileays advertise duplicate extent support with this flag */
837 if ((le32_to_cpu(tcon->fsAttrInfo.Attributes) &
838 FILE_SUPPORTS_BLOCK_REFCOUNTING) == 0)
839 return -EOPNOTSUPP;
840
841 dup_ext_buf.VolatileFileHandle = srcfile->fid.volatile_fid;
842 dup_ext_buf.PersistentFileHandle = srcfile->fid.persistent_fid;
843 dup_ext_buf.SourceFileOffset = cpu_to_le64(src_off);
844 dup_ext_buf.TargetFileOffset = cpu_to_le64(dest_off);
845 dup_ext_buf.ByteCount = cpu_to_le64(len);
846 cifs_dbg(FYI, "duplicate extents: src off %lld dst off %lld len %lld",
847 src_off, dest_off, len);
848
849 rc = smb2_set_file_size(xid, tcon, trgtfile, dest_off + len, false);
850 if (rc)
851 goto duplicate_extents_out;
852
853 rc = SMB2_ioctl(xid, tcon, trgtfile->fid.persistent_fid,
854 trgtfile->fid.volatile_fid,
855 FSCTL_DUPLICATE_EXTENTS_TO_FILE,
856 true /* is_fsctl */, (char *)&dup_ext_buf,
857 sizeof(struct duplicate_extents_to_file),
Steve French24df1482016-09-29 04:20:23 -0500858 NULL,
Steve French02b16662015-06-27 21:18:36 -0700859 &ret_data_len);
860
861 if (ret_data_len > 0)
862 cifs_dbg(FYI, "non-zero response length in duplicate extents");
863
864duplicate_extents_out:
865 return rc;
866}
Steve French02b16662015-06-27 21:18:36 -0700867
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -0700868static int
Steve French64a5cfa2013-10-14 15:31:32 -0500869smb2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
870 struct cifsFileInfo *cfile)
871{
872 return SMB2_set_compression(xid, tcon, cfile->fid.persistent_fid,
873 cfile->fid.volatile_fid);
874}
875
876static int
Steve Frenchb3152e22015-06-24 03:17:02 -0500877smb3_set_integrity(const unsigned int xid, struct cifs_tcon *tcon,
878 struct cifsFileInfo *cfile)
879{
880 struct fsctl_set_integrity_information_req integr_info;
Steve Frenchb3152e22015-06-24 03:17:02 -0500881 unsigned int ret_data_len;
882
883 integr_info.ChecksumAlgorithm = cpu_to_le16(CHECKSUM_TYPE_UNCHANGED);
884 integr_info.Flags = 0;
885 integr_info.Reserved = 0;
886
887 return SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
888 cfile->fid.volatile_fid,
889 FSCTL_SET_INTEGRITY_INFORMATION,
890 true /* is_fsctl */, (char *)&integr_info,
891 sizeof(struct fsctl_set_integrity_information_req),
Steve French24df1482016-09-29 04:20:23 -0500892 NULL,
Steve Frenchb3152e22015-06-24 03:17:02 -0500893 &ret_data_len);
894
895}
896
897static int
Steve French834170c2016-09-30 21:14:26 -0500898smb3_enum_snapshots(const unsigned int xid, struct cifs_tcon *tcon,
899 struct cifsFileInfo *cfile, void __user *ioc_buf)
900{
901 char *retbuf = NULL;
902 unsigned int ret_data_len = 0;
903 int rc;
904 struct smb_snapshot_array snapshot_in;
905
906 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
907 cfile->fid.volatile_fid,
908 FSCTL_SRV_ENUMERATE_SNAPSHOTS,
909 true /* is_fsctl */, NULL, 0 /* no input data */,
910 (char **)&retbuf,
911 &ret_data_len);
912 cifs_dbg(FYI, "enum snaphots ioctl returned %d and ret buflen is %d\n",
913 rc, ret_data_len);
914 if (rc)
915 return rc;
916
917 if (ret_data_len && (ioc_buf != NULL) && (retbuf != NULL)) {
918 /* Fixup buffer */
919 if (copy_from_user(&snapshot_in, ioc_buf,
920 sizeof(struct smb_snapshot_array))) {
921 rc = -EFAULT;
922 kfree(retbuf);
923 return rc;
924 }
925 if (snapshot_in.snapshot_array_size < sizeof(struct smb_snapshot_array)) {
926 rc = -ERANGE;
927 return rc;
928 }
929
930 if (ret_data_len > snapshot_in.snapshot_array_size)
931 ret_data_len = snapshot_in.snapshot_array_size;
932
933 if (copy_to_user(ioc_buf, retbuf, ret_data_len))
934 rc = -EFAULT;
935 }
936
937 kfree(retbuf);
938 return rc;
939}
940
941static int
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -0700942smb2_query_dir_first(const unsigned int xid, struct cifs_tcon *tcon,
943 const char *path, struct cifs_sb_info *cifs_sb,
944 struct cifs_fid *fid, __u16 search_flags,
945 struct cifs_search_info *srch_inf)
946{
947 __le16 *utf16_path;
948 int rc;
Pavel Shilovsky2e44b282012-09-18 16:20:33 -0700949 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
Pavel Shilovsky064f6042013-07-09 18:20:30 +0400950 struct cifs_open_parms oparms;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -0700951
952 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
953 if (!utf16_path)
954 return -ENOMEM;
955
Pavel Shilovsky064f6042013-07-09 18:20:30 +0400956 oparms.tcon = tcon;
957 oparms.desired_access = FILE_READ_ATTRIBUTES | FILE_READ_DATA;
958 oparms.disposition = FILE_OPEN;
959 oparms.create_options = 0;
960 oparms.fid = fid;
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +0400961 oparms.reconnect = false;
Pavel Shilovsky064f6042013-07-09 18:20:30 +0400962
Pavel Shilovskyb42bf882013-08-14 19:25:21 +0400963 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -0700964 kfree(utf16_path);
965 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500966 cifs_dbg(VFS, "open dir failed\n");
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -0700967 return rc;
968 }
969
970 srch_inf->entries_in_buffer = 0;
971 srch_inf->index_of_last_entry = 0;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -0700972
Pavel Shilovsky064f6042013-07-09 18:20:30 +0400973 rc = SMB2_query_directory(xid, tcon, fid->persistent_fid,
974 fid->volatile_fid, 0, srch_inf);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -0700975 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500976 cifs_dbg(VFS, "query directory failed\n");
Pavel Shilovsky064f6042013-07-09 18:20:30 +0400977 SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -0700978 }
979 return rc;
980}
981
982static int
983smb2_query_dir_next(const unsigned int xid, struct cifs_tcon *tcon,
984 struct cifs_fid *fid, __u16 search_flags,
985 struct cifs_search_info *srch_inf)
986{
987 return SMB2_query_directory(xid, tcon, fid->persistent_fid,
988 fid->volatile_fid, 0, srch_inf);
989}
990
991static int
992smb2_close_dir(const unsigned int xid, struct cifs_tcon *tcon,
993 struct cifs_fid *fid)
994{
995 return SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
996}
997
Pavel Shilovsky2e44b282012-09-18 16:20:33 -0700998/*
999* If we negotiate SMB2 protocol and get STATUS_PENDING - update
1000* the number of credits and return true. Otherwise - return false.
1001*/
1002static bool
1003smb2_is_status_pending(char *buf, struct TCP_Server_Info *server, int length)
1004{
1005 struct smb2_hdr *hdr = (struct smb2_hdr *)buf;
1006
Steve French12e8a202012-09-19 09:19:39 -07001007 if (hdr->Status != STATUS_PENDING)
Pavel Shilovsky2e44b282012-09-18 16:20:33 -07001008 return false;
1009
1010 if (!length) {
1011 spin_lock(&server->req_lock);
1012 server->credits += le16_to_cpu(hdr->CreditRequest);
1013 spin_unlock(&server->req_lock);
1014 wake_up(&server->request_q);
1015 }
1016
1017 return true;
1018}
1019
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07001020static int
1021smb2_oplock_response(struct cifs_tcon *tcon, struct cifs_fid *fid,
1022 struct cifsInodeInfo *cinode)
1023{
Pavel Shilovsky0822f512012-09-19 06:22:45 -07001024 if (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_LEASING)
1025 return SMB2_lease_break(0, tcon, cinode->lease_key,
1026 smb2_get_lease_state(cinode));
1027
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07001028 return SMB2_oplock_break(0, tcon, fid->persistent_fid,
1029 fid->volatile_fid,
Pavel Shilovsky18cceb62013-09-05 13:01:06 +04001030 CIFS_CACHE_READ(cinode) ? 1 : 0);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07001031}
1032
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07001033static int
1034smb2_queryfs(const unsigned int xid, struct cifs_tcon *tcon,
1035 struct kstatfs *buf)
1036{
1037 int rc;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07001038 __le16 srch_path = 0; /* Null - open root of share */
1039 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001040 struct cifs_open_parms oparms;
1041 struct cifs_fid fid;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07001042
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001043 oparms.tcon = tcon;
1044 oparms.desired_access = FILE_READ_ATTRIBUTES;
1045 oparms.disposition = FILE_OPEN;
1046 oparms.create_options = 0;
1047 oparms.fid = &fid;
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +04001048 oparms.reconnect = false;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001049
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04001050 rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07001051 if (rc)
1052 return rc;
1053 buf->f_type = SMB2_MAGIC_NUMBER;
Pavel Shilovsky064f6042013-07-09 18:20:30 +04001054 rc = SMB2_QFS_info(xid, tcon, fid.persistent_fid, fid.volatile_fid,
1055 buf);
1056 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07001057 return rc;
1058}
1059
Pavel Shilovsky027e8ee2012-09-19 06:22:43 -07001060static bool
1061smb2_compare_fids(struct cifsFileInfo *ob1, struct cifsFileInfo *ob2)
1062{
1063 return ob1->fid.persistent_fid == ob2->fid.persistent_fid &&
1064 ob1->fid.volatile_fid == ob2->fid.volatile_fid;
1065}
1066
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07001067static int
1068smb2_mand_lock(const unsigned int xid, struct cifsFileInfo *cfile, __u64 offset,
1069 __u64 length, __u32 type, int lock, int unlock, bool wait)
1070{
1071 if (unlock && !lock)
1072 type = SMB2_LOCKFLAG_UNLOCK;
1073 return SMB2_lock(xid, tlink_tcon(cfile->tlink),
1074 cfile->fid.persistent_fid, cfile->fid.volatile_fid,
1075 current->tgid, length, offset, type, wait);
1076}
1077
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001078static void
1079smb2_get_lease_key(struct inode *inode, struct cifs_fid *fid)
1080{
1081 memcpy(fid->lease_key, CIFS_I(inode)->lease_key, SMB2_LEASE_KEY_SIZE);
1082}
1083
1084static void
1085smb2_set_lease_key(struct inode *inode, struct cifs_fid *fid)
1086{
1087 memcpy(CIFS_I(inode)->lease_key, fid->lease_key, SMB2_LEASE_KEY_SIZE);
1088}
1089
1090static void
1091smb2_new_lease_key(struct cifs_fid *fid)
1092{
Steve Frenchfa70b872016-09-22 00:39:34 -05001093 generate_random_uuid(fid->lease_key);
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001094}
1095
Pavel Shilovsky78932422016-07-24 10:37:38 +03001096#define SMB2_SYMLINK_STRUCT_SIZE \
1097 (sizeof(struct smb2_err_rsp) - 1 + sizeof(struct smb2_symlink_err_rsp))
1098
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04001099static int
1100smb2_query_symlink(const unsigned int xid, struct cifs_tcon *tcon,
1101 const char *full_path, char **target_path,
1102 struct cifs_sb_info *cifs_sb)
1103{
1104 int rc;
1105 __le16 *utf16_path;
1106 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
1107 struct cifs_open_parms oparms;
1108 struct cifs_fid fid;
1109 struct smb2_err_rsp *err_buf = NULL;
1110 struct smb2_symlink_err_rsp *symlink;
Pavel Shilovsky78932422016-07-24 10:37:38 +03001111 unsigned int sub_len;
1112 unsigned int sub_offset;
1113 unsigned int print_len;
1114 unsigned int print_offset;
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04001115
1116 cifs_dbg(FYI, "%s: path: %s\n", __func__, full_path);
1117
1118 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
1119 if (!utf16_path)
1120 return -ENOMEM;
1121
1122 oparms.tcon = tcon;
1123 oparms.desired_access = FILE_READ_ATTRIBUTES;
1124 oparms.disposition = FILE_OPEN;
1125 oparms.create_options = 0;
1126 oparms.fid = &fid;
1127 oparms.reconnect = false;
1128
1129 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, &err_buf);
1130
1131 if (!rc || !err_buf) {
1132 kfree(utf16_path);
1133 return -ENOENT;
1134 }
Pavel Shilovsky78932422016-07-24 10:37:38 +03001135
1136 if (le32_to_cpu(err_buf->ByteCount) < sizeof(struct smb2_symlink_err_rsp) ||
1137 get_rfc1002_length(err_buf) + 4 < SMB2_SYMLINK_STRUCT_SIZE) {
1138 kfree(utf16_path);
1139 return -ENOENT;
1140 }
1141
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04001142 /* open must fail on symlink - reset rc */
1143 rc = 0;
1144 symlink = (struct smb2_symlink_err_rsp *)err_buf->ErrorData;
1145 sub_len = le16_to_cpu(symlink->SubstituteNameLength);
1146 sub_offset = le16_to_cpu(symlink->SubstituteNameOffset);
Pavel Shilovsky78932422016-07-24 10:37:38 +03001147 print_len = le16_to_cpu(symlink->PrintNameLength);
1148 print_offset = le16_to_cpu(symlink->PrintNameOffset);
1149
1150 if (get_rfc1002_length(err_buf) + 4 <
1151 SMB2_SYMLINK_STRUCT_SIZE + sub_offset + sub_len) {
1152 kfree(utf16_path);
1153 return -ENOENT;
1154 }
1155
1156 if (get_rfc1002_length(err_buf) + 4 <
1157 SMB2_SYMLINK_STRUCT_SIZE + print_offset + print_len) {
1158 kfree(utf16_path);
1159 return -ENOENT;
1160 }
1161
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04001162 *target_path = cifs_strndup_from_utf16(
1163 (char *)symlink->PathBuffer + sub_offset,
1164 sub_len, true, cifs_sb->local_nls);
1165 if (!(*target_path)) {
1166 kfree(utf16_path);
1167 return -ENOMEM;
1168 }
1169 convert_delimiter(*target_path, '/');
1170 cifs_dbg(FYI, "%s: target path: %s\n", __func__, *target_path);
1171 kfree(utf16_path);
1172 return rc;
1173}
1174
Steve French30175622014-08-17 18:16:40 -05001175static long smb3_zero_range(struct file *file, struct cifs_tcon *tcon,
1176 loff_t offset, loff_t len, bool keep_size)
1177{
1178 struct inode *inode;
1179 struct cifsInodeInfo *cifsi;
1180 struct cifsFileInfo *cfile = file->private_data;
1181 struct file_zero_data_information fsctl_buf;
1182 long rc;
1183 unsigned int xid;
1184
1185 xid = get_xid();
1186
David Howells2b0143b2015-03-17 22:25:59 +00001187 inode = d_inode(cfile->dentry);
Steve French30175622014-08-17 18:16:40 -05001188 cifsi = CIFS_I(inode);
1189
1190 /* if file not oplocked can't be sure whether asking to extend size */
1191 if (!CIFS_CACHE_READ(cifsi))
1192 if (keep_size == false)
1193 return -EOPNOTSUPP;
1194
Steve French2bb93d22014-08-20 18:56:29 -05001195 /*
Steve French30175622014-08-17 18:16:40 -05001196 * Must check if file sparse since fallocate -z (zero range) assumes
1197 * non-sparse allocation
1198 */
1199 if (!(cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE))
1200 return -EOPNOTSUPP;
1201
1202 /*
1203 * need to make sure we are not asked to extend the file since the SMB3
1204 * fsctl does not change the file size. In the future we could change
1205 * this to zero the first part of the range then set the file size
1206 * which for a non sparse file would zero the newly extended range
1207 */
1208 if (keep_size == false)
1209 if (i_size_read(inode) < offset + len)
1210 return -EOPNOTSUPP;
1211
1212 cifs_dbg(FYI, "offset %lld len %lld", offset, len);
1213
1214 fsctl_buf.FileOffset = cpu_to_le64(offset);
1215 fsctl_buf.BeyondFinalZero = cpu_to_le64(offset + len);
1216
1217 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
1218 cfile->fid.volatile_fid, FSCTL_SET_ZERO_DATA,
1219 true /* is_fctl */, (char *)&fsctl_buf,
1220 sizeof(struct file_zero_data_information), NULL, NULL);
1221 free_xid(xid);
1222 return rc;
1223}
1224
Steve French31742c52014-08-17 08:38:47 -05001225static long smb3_punch_hole(struct file *file, struct cifs_tcon *tcon,
1226 loff_t offset, loff_t len)
1227{
1228 struct inode *inode;
1229 struct cifsInodeInfo *cifsi;
1230 struct cifsFileInfo *cfile = file->private_data;
1231 struct file_zero_data_information fsctl_buf;
1232 long rc;
1233 unsigned int xid;
1234 __u8 set_sparse = 1;
1235
1236 xid = get_xid();
1237
David Howells2b0143b2015-03-17 22:25:59 +00001238 inode = d_inode(cfile->dentry);
Steve French31742c52014-08-17 08:38:47 -05001239 cifsi = CIFS_I(inode);
1240
1241 /* Need to make file sparse, if not already, before freeing range. */
1242 /* Consider adding equivalent for compressed since it could also work */
1243 if (!smb2_set_sparse(xid, tcon, cfile, inode, set_sparse))
1244 return -EOPNOTSUPP;
1245
1246 cifs_dbg(FYI, "offset %lld len %lld", offset, len);
1247
1248 fsctl_buf.FileOffset = cpu_to_le64(offset);
1249 fsctl_buf.BeyondFinalZero = cpu_to_le64(offset + len);
1250
1251 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
1252 cfile->fid.volatile_fid, FSCTL_SET_ZERO_DATA,
1253 true /* is_fctl */, (char *)&fsctl_buf,
1254 sizeof(struct file_zero_data_information), NULL, NULL);
1255 free_xid(xid);
1256 return rc;
1257}
1258
Steve French9ccf3212014-10-18 17:01:15 -05001259static long smb3_simple_falloc(struct file *file, struct cifs_tcon *tcon,
1260 loff_t off, loff_t len, bool keep_size)
1261{
1262 struct inode *inode;
1263 struct cifsInodeInfo *cifsi;
1264 struct cifsFileInfo *cfile = file->private_data;
1265 long rc = -EOPNOTSUPP;
1266 unsigned int xid;
1267
1268 xid = get_xid();
1269
David Howells2b0143b2015-03-17 22:25:59 +00001270 inode = d_inode(cfile->dentry);
Steve French9ccf3212014-10-18 17:01:15 -05001271 cifsi = CIFS_I(inode);
1272
1273 /* if file not oplocked can't be sure whether asking to extend size */
1274 if (!CIFS_CACHE_READ(cifsi))
1275 if (keep_size == false)
1276 return -EOPNOTSUPP;
1277
1278 /*
1279 * Files are non-sparse by default so falloc may be a no-op
1280 * Must check if file sparse. If not sparse, and not extending
1281 * then no need to do anything since file already allocated
1282 */
1283 if ((cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) == 0) {
1284 if (keep_size == true)
1285 return 0;
1286 /* check if extending file */
1287 else if (i_size_read(inode) >= off + len)
1288 /* not extending file and already not sparse */
1289 return 0;
1290 /* BB: in future add else clause to extend file */
1291 else
1292 return -EOPNOTSUPP;
1293 }
1294
1295 if ((keep_size == true) || (i_size_read(inode) >= off + len)) {
1296 /*
1297 * Check if falloc starts within first few pages of file
1298 * and ends within a few pages of the end of file to
1299 * ensure that most of file is being forced to be
1300 * fallocated now. If so then setting whole file sparse
1301 * ie potentially making a few extra pages at the beginning
1302 * or end of the file non-sparse via set_sparse is harmless.
1303 */
1304 if ((off > 8192) || (off + len + 8192 < i_size_read(inode)))
1305 return -EOPNOTSUPP;
1306
1307 rc = smb2_set_sparse(xid, tcon, cfile, inode, false);
1308 }
1309 /* BB: else ... in future add code to extend file and set sparse */
1310
1311
1312 free_xid(xid);
1313 return rc;
1314}
1315
1316
Steve French31742c52014-08-17 08:38:47 -05001317static long smb3_fallocate(struct file *file, struct cifs_tcon *tcon, int mode,
1318 loff_t off, loff_t len)
1319{
1320 /* KEEP_SIZE already checked for by do_fallocate */
1321 if (mode & FALLOC_FL_PUNCH_HOLE)
1322 return smb3_punch_hole(file, tcon, off, len);
Steve French30175622014-08-17 18:16:40 -05001323 else if (mode & FALLOC_FL_ZERO_RANGE) {
1324 if (mode & FALLOC_FL_KEEP_SIZE)
1325 return smb3_zero_range(file, tcon, off, len, true);
1326 return smb3_zero_range(file, tcon, off, len, false);
Steve French9ccf3212014-10-18 17:01:15 -05001327 } else if (mode == FALLOC_FL_KEEP_SIZE)
1328 return smb3_simple_falloc(file, tcon, off, len, true);
1329 else if (mode == 0)
1330 return smb3_simple_falloc(file, tcon, off, len, false);
Steve French31742c52014-08-17 08:38:47 -05001331
1332 return -EOPNOTSUPP;
1333}
1334
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04001335static void
Sachin Prabhuc11f1df2014-03-11 16:11:47 +00001336smb2_downgrade_oplock(struct TCP_Server_Info *server,
1337 struct cifsInodeInfo *cinode, bool set_level2)
1338{
1339 if (set_level2)
1340 server->ops->set_oplock_level(cinode, SMB2_OPLOCK_LEVEL_II,
1341 0, NULL);
1342 else
1343 server->ops->set_oplock_level(cinode, 0, 0, NULL);
1344}
1345
1346static void
Pavel Shilovsky42873b02013-09-05 21:30:16 +04001347smb2_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
1348 unsigned int epoch, bool *purge_cache)
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04001349{
1350 oplock &= 0xFF;
1351 if (oplock == SMB2_OPLOCK_LEVEL_NOCHANGE)
1352 return;
1353 if (oplock == SMB2_OPLOCK_LEVEL_BATCH) {
Pavel Shilovsky42873b02013-09-05 21:30:16 +04001354 cinode->oplock = CIFS_CACHE_RHW_FLG;
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04001355 cifs_dbg(FYI, "Batch Oplock granted on inode %p\n",
1356 &cinode->vfs_inode);
1357 } else if (oplock == SMB2_OPLOCK_LEVEL_EXCLUSIVE) {
Pavel Shilovsky42873b02013-09-05 21:30:16 +04001358 cinode->oplock = CIFS_CACHE_RW_FLG;
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04001359 cifs_dbg(FYI, "Exclusive Oplock granted on inode %p\n",
1360 &cinode->vfs_inode);
1361 } else if (oplock == SMB2_OPLOCK_LEVEL_II) {
1362 cinode->oplock = CIFS_CACHE_READ_FLG;
1363 cifs_dbg(FYI, "Level II Oplock granted on inode %p\n",
1364 &cinode->vfs_inode);
1365 } else
1366 cinode->oplock = 0;
1367}
1368
1369static void
Pavel Shilovsky42873b02013-09-05 21:30:16 +04001370smb21_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
1371 unsigned int epoch, bool *purge_cache)
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04001372{
1373 char message[5] = {0};
1374
1375 oplock &= 0xFF;
1376 if (oplock == SMB2_OPLOCK_LEVEL_NOCHANGE)
1377 return;
1378
1379 cinode->oplock = 0;
1380 if (oplock & SMB2_LEASE_READ_CACHING_HE) {
1381 cinode->oplock |= CIFS_CACHE_READ_FLG;
1382 strcat(message, "R");
1383 }
1384 if (oplock & SMB2_LEASE_HANDLE_CACHING_HE) {
1385 cinode->oplock |= CIFS_CACHE_HANDLE_FLG;
1386 strcat(message, "H");
1387 }
1388 if (oplock & SMB2_LEASE_WRITE_CACHING_HE) {
1389 cinode->oplock |= CIFS_CACHE_WRITE_FLG;
1390 strcat(message, "W");
1391 }
1392 if (!cinode->oplock)
1393 strcat(message, "None");
1394 cifs_dbg(FYI, "%s Lease granted on inode %p\n", message,
1395 &cinode->vfs_inode);
1396}
1397
Pavel Shilovsky42873b02013-09-05 21:30:16 +04001398static void
1399smb3_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
1400 unsigned int epoch, bool *purge_cache)
1401{
1402 unsigned int old_oplock = cinode->oplock;
1403
1404 smb21_set_oplock_level(cinode, oplock, epoch, purge_cache);
1405
1406 if (purge_cache) {
1407 *purge_cache = false;
1408 if (old_oplock == CIFS_CACHE_READ_FLG) {
1409 if (cinode->oplock == CIFS_CACHE_READ_FLG &&
1410 (epoch - cinode->epoch > 0))
1411 *purge_cache = true;
1412 else if (cinode->oplock == CIFS_CACHE_RH_FLG &&
1413 (epoch - cinode->epoch > 1))
1414 *purge_cache = true;
1415 else if (cinode->oplock == CIFS_CACHE_RHW_FLG &&
1416 (epoch - cinode->epoch > 1))
1417 *purge_cache = true;
1418 else if (cinode->oplock == 0 &&
1419 (epoch - cinode->epoch > 0))
1420 *purge_cache = true;
1421 } else if (old_oplock == CIFS_CACHE_RH_FLG) {
1422 if (cinode->oplock == CIFS_CACHE_RH_FLG &&
1423 (epoch - cinode->epoch > 0))
1424 *purge_cache = true;
1425 else if (cinode->oplock == CIFS_CACHE_RHW_FLG &&
1426 (epoch - cinode->epoch > 1))
1427 *purge_cache = true;
1428 }
1429 cinode->epoch = epoch;
1430 }
1431}
1432
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04001433static bool
1434smb2_is_read_op(__u32 oplock)
1435{
1436 return oplock == SMB2_OPLOCK_LEVEL_II;
1437}
1438
1439static bool
1440smb21_is_read_op(__u32 oplock)
1441{
1442 return (oplock & SMB2_LEASE_READ_CACHING_HE) &&
1443 !(oplock & SMB2_LEASE_WRITE_CACHING_HE);
1444}
1445
Pavel Shilovskyf0473902013-09-04 13:44:05 +04001446static __le32
1447map_oplock_to_lease(u8 oplock)
1448{
1449 if (oplock == SMB2_OPLOCK_LEVEL_EXCLUSIVE)
1450 return SMB2_LEASE_WRITE_CACHING | SMB2_LEASE_READ_CACHING;
1451 else if (oplock == SMB2_OPLOCK_LEVEL_II)
1452 return SMB2_LEASE_READ_CACHING;
1453 else if (oplock == SMB2_OPLOCK_LEVEL_BATCH)
1454 return SMB2_LEASE_HANDLE_CACHING | SMB2_LEASE_READ_CACHING |
1455 SMB2_LEASE_WRITE_CACHING;
1456 return 0;
1457}
1458
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001459static char *
1460smb2_create_lease_buf(u8 *lease_key, u8 oplock)
1461{
1462 struct create_lease *buf;
1463
1464 buf = kzalloc(sizeof(struct create_lease), GFP_KERNEL);
1465 if (!buf)
1466 return NULL;
1467
1468 buf->lcontext.LeaseKeyLow = cpu_to_le64(*((u64 *)lease_key));
1469 buf->lcontext.LeaseKeyHigh = cpu_to_le64(*((u64 *)(lease_key + 8)));
Pavel Shilovskyf0473902013-09-04 13:44:05 +04001470 buf->lcontext.LeaseState = map_oplock_to_lease(oplock);
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001471
1472 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1473 (struct create_lease, lcontext));
1474 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context));
1475 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1476 (struct create_lease, Name));
1477 buf->ccontext.NameLength = cpu_to_le16(4);
Steve French12197a72014-05-14 05:29:40 -07001478 /* SMB2_CREATE_REQUEST_LEASE is "RqLs" */
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001479 buf->Name[0] = 'R';
1480 buf->Name[1] = 'q';
1481 buf->Name[2] = 'L';
1482 buf->Name[3] = 's';
1483 return (char *)buf;
1484}
1485
Pavel Shilovskyf0473902013-09-04 13:44:05 +04001486static char *
1487smb3_create_lease_buf(u8 *lease_key, u8 oplock)
1488{
1489 struct create_lease_v2 *buf;
1490
1491 buf = kzalloc(sizeof(struct create_lease_v2), GFP_KERNEL);
1492 if (!buf)
1493 return NULL;
1494
1495 buf->lcontext.LeaseKeyLow = cpu_to_le64(*((u64 *)lease_key));
1496 buf->lcontext.LeaseKeyHigh = cpu_to_le64(*((u64 *)(lease_key + 8)));
1497 buf->lcontext.LeaseState = map_oplock_to_lease(oplock);
1498
1499 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1500 (struct create_lease_v2, lcontext));
1501 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context_v2));
1502 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1503 (struct create_lease_v2, Name));
1504 buf->ccontext.NameLength = cpu_to_le16(4);
Steve French12197a72014-05-14 05:29:40 -07001505 /* SMB2_CREATE_REQUEST_LEASE is "RqLs" */
Pavel Shilovskyf0473902013-09-04 13:44:05 +04001506 buf->Name[0] = 'R';
1507 buf->Name[1] = 'q';
1508 buf->Name[2] = 'L';
1509 buf->Name[3] = 's';
1510 return (char *)buf;
1511}
1512
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001513static __u8
Pavel Shilovsky42873b02013-09-05 21:30:16 +04001514smb2_parse_lease_buf(void *buf, unsigned int *epoch)
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001515{
1516 struct create_lease *lc = (struct create_lease *)buf;
1517
Pavel Shilovsky42873b02013-09-05 21:30:16 +04001518 *epoch = 0; /* not used */
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001519 if (lc->lcontext.LeaseFlags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS)
1520 return SMB2_OPLOCK_LEVEL_NOCHANGE;
1521 return le32_to_cpu(lc->lcontext.LeaseState);
1522}
1523
Pavel Shilovskyf0473902013-09-04 13:44:05 +04001524static __u8
Pavel Shilovsky42873b02013-09-05 21:30:16 +04001525smb3_parse_lease_buf(void *buf, unsigned int *epoch)
Pavel Shilovskyf0473902013-09-04 13:44:05 +04001526{
1527 struct create_lease_v2 *lc = (struct create_lease_v2 *)buf;
1528
Pavel Shilovsky42873b02013-09-05 21:30:16 +04001529 *epoch = le16_to_cpu(lc->lcontext.Epoch);
Pavel Shilovskyf0473902013-09-04 13:44:05 +04001530 if (lc->lcontext.LeaseFlags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS)
1531 return SMB2_OPLOCK_LEVEL_NOCHANGE;
1532 return le32_to_cpu(lc->lcontext.LeaseState);
1533}
1534
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04001535static unsigned int
1536smb2_wp_retry_size(struct inode *inode)
1537{
1538 return min_t(unsigned int, CIFS_SB(inode->i_sb)->wsize,
1539 SMB2_MAX_BUFFER_SIZE);
1540}
1541
Pavel Shilovsky52755802014-08-18 20:49:57 +04001542static bool
1543smb2_dir_needs_close(struct cifsFileInfo *cfile)
1544{
1545 return !cfile->invalidHandle;
1546}
1547
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04001548struct smb_version_operations smb20_operations = {
1549 .compare_fids = smb2_compare_fids,
1550 .setup_request = smb2_setup_request,
1551 .setup_async_request = smb2_setup_async_request,
1552 .check_receive = smb2_check_receive,
1553 .add_credits = smb2_add_credits,
1554 .set_credits = smb2_set_credits,
1555 .get_credits_field = smb2_get_credits_field,
1556 .get_credits = smb2_get_credits,
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04001557 .wait_mtu_credits = cifs_wait_mtu_credits,
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04001558 .get_next_mid = smb2_get_next_mid,
1559 .read_data_offset = smb2_read_data_offset,
1560 .read_data_length = smb2_read_data_length,
1561 .map_error = map_smb2_to_linux_error,
1562 .find_mid = smb2_find_mid,
1563 .check_message = smb2_check_message,
1564 .dump_detail = smb2_dump_detail,
1565 .clear_stats = smb2_clear_stats,
1566 .print_stats = smb2_print_stats,
1567 .is_oplock_break = smb2_is_valid_oplock_break,
Sachin Prabhuc11f1df2014-03-11 16:11:47 +00001568 .downgrade_oplock = smb2_downgrade_oplock,
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04001569 .need_neg = smb2_need_neg,
1570 .negotiate = smb2_negotiate,
1571 .negotiate_wsize = smb2_negotiate_wsize,
1572 .negotiate_rsize = smb2_negotiate_rsize,
1573 .sess_setup = SMB2_sess_setup,
1574 .logoff = SMB2_logoff,
1575 .tree_connect = SMB2_tcon,
1576 .tree_disconnect = SMB2_tdis,
Steve French34f62642013-10-09 02:07:00 -05001577 .qfs_tcon = smb2_qfs_tcon,
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04001578 .is_path_accessible = smb2_is_path_accessible,
1579 .can_echo = smb2_can_echo,
1580 .echo = SMB2_echo,
1581 .query_path_info = smb2_query_path_info,
1582 .get_srv_inum = smb2_get_srv_inum,
1583 .query_file_info = smb2_query_file_info,
1584 .set_path_size = smb2_set_path_size,
1585 .set_file_size = smb2_set_file_size,
1586 .set_file_info = smb2_set_file_info,
Steve French64a5cfa2013-10-14 15:31:32 -05001587 .set_compression = smb2_set_compression,
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04001588 .mkdir = smb2_mkdir,
1589 .mkdir_setinfo = smb2_mkdir_setinfo,
1590 .rmdir = smb2_rmdir,
1591 .unlink = smb2_unlink,
1592 .rename = smb2_rename_path,
1593 .create_hardlink = smb2_create_hardlink,
1594 .query_symlink = smb2_query_symlink,
Sachin Prabhu5b23c972016-07-11 16:53:20 +01001595 .query_mf_symlink = smb3_query_mf_symlink,
1596 .create_mf_symlink = smb3_create_mf_symlink,
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04001597 .open = smb2_open_file,
1598 .set_fid = smb2_set_fid,
1599 .close = smb2_close_file,
1600 .flush = smb2_flush_file,
1601 .async_readv = smb2_async_readv,
1602 .async_writev = smb2_async_writev,
1603 .sync_read = smb2_sync_read,
1604 .sync_write = smb2_sync_write,
1605 .query_dir_first = smb2_query_dir_first,
1606 .query_dir_next = smb2_query_dir_next,
1607 .close_dir = smb2_close_dir,
1608 .calc_smb_size = smb2_calc_size,
1609 .is_status_pending = smb2_is_status_pending,
1610 .oplock_response = smb2_oplock_response,
1611 .queryfs = smb2_queryfs,
1612 .mand_lock = smb2_mand_lock,
1613 .mand_unlock_range = smb2_unlock_range,
1614 .push_mand_locks = smb2_push_mandatory_locks,
1615 .get_lease_key = smb2_get_lease_key,
1616 .set_lease_key = smb2_set_lease_key,
1617 .new_lease_key = smb2_new_lease_key,
1618 .calc_signature = smb2_calc_signature,
1619 .is_read_op = smb2_is_read_op,
1620 .set_oplock_level = smb2_set_oplock_level,
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001621 .create_lease_buf = smb2_create_lease_buf,
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001622 .parse_lease_buf = smb2_parse_lease_buf,
Steve French41c13582013-11-14 00:05:36 -06001623 .clone_range = smb2_clone_range,
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04001624 .wp_retry_size = smb2_wp_retry_size,
Pavel Shilovsky52755802014-08-18 20:49:57 +04001625 .dir_needs_close = smb2_dir_needs_close,
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04001626};
1627
Steve French1080ef72011-02-24 18:07:19 +00001628struct smb_version_operations smb21_operations = {
Pavel Shilovsky027e8ee2012-09-19 06:22:43 -07001629 .compare_fids = smb2_compare_fids,
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +04001630 .setup_request = smb2_setup_request,
Pavel Shilovskyc95b8ee2012-07-11 14:45:28 +04001631 .setup_async_request = smb2_setup_async_request,
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +04001632 .check_receive = smb2_check_receive,
Pavel Shilovsky28ea5292012-05-23 16:18:00 +04001633 .add_credits = smb2_add_credits,
1634 .set_credits = smb2_set_credits,
1635 .get_credits_field = smb2_get_credits_field,
1636 .get_credits = smb2_get_credits,
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04001637 .wait_mtu_credits = smb2_wait_mtu_credits,
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +04001638 .get_next_mid = smb2_get_next_mid,
Pavel Shilovsky09a47072012-09-18 16:20:29 -07001639 .read_data_offset = smb2_read_data_offset,
1640 .read_data_length = smb2_read_data_length,
1641 .map_error = map_smb2_to_linux_error,
Pavel Shilovsky093b2bd2011-06-08 15:51:07 +04001642 .find_mid = smb2_find_mid,
1643 .check_message = smb2_check_message,
1644 .dump_detail = smb2_dump_detail,
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001645 .clear_stats = smb2_clear_stats,
1646 .print_stats = smb2_print_stats,
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07001647 .is_oplock_break = smb2_is_valid_oplock_break,
Sachin Prabhuc11f1df2014-03-11 16:11:47 +00001648 .downgrade_oplock = smb2_downgrade_oplock,
Pavel Shilovskyec2e4522011-12-27 16:12:43 +04001649 .need_neg = smb2_need_neg,
1650 .negotiate = smb2_negotiate,
Pavel Shilovsky3a3bab52012-09-18 16:20:28 -07001651 .negotiate_wsize = smb2_negotiate_wsize,
1652 .negotiate_rsize = smb2_negotiate_rsize,
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001653 .sess_setup = SMB2_sess_setup,
1654 .logoff = SMB2_logoff,
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001655 .tree_connect = SMB2_tcon,
1656 .tree_disconnect = SMB2_tdis,
Steve French34f62642013-10-09 02:07:00 -05001657 .qfs_tcon = smb2_qfs_tcon,
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001658 .is_path_accessible = smb2_is_path_accessible,
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04001659 .can_echo = smb2_can_echo,
1660 .echo = SMB2_echo,
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001661 .query_path_info = smb2_query_path_info,
1662 .get_srv_inum = smb2_get_srv_inum,
Pavel Shilovskyb7546bc2012-09-18 16:20:27 -07001663 .query_file_info = smb2_query_file_info,
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07001664 .set_path_size = smb2_set_path_size,
1665 .set_file_size = smb2_set_file_size,
Pavel Shilovsky1feeaac2012-09-18 16:20:32 -07001666 .set_file_info = smb2_set_file_info,
Steve French64a5cfa2013-10-14 15:31:32 -05001667 .set_compression = smb2_set_compression,
Pavel Shilovskya0e73182011-07-19 12:56:37 +04001668 .mkdir = smb2_mkdir,
1669 .mkdir_setinfo = smb2_mkdir_setinfo,
Pavel Shilovsky1a500f02012-07-10 16:14:38 +04001670 .rmdir = smb2_rmdir,
Pavel Shilovskycbe6f432012-09-18 16:20:25 -07001671 .unlink = smb2_unlink,
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07001672 .rename = smb2_rename_path,
Pavel Shilovsky568798c2012-09-18 16:20:31 -07001673 .create_hardlink = smb2_create_hardlink,
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04001674 .query_symlink = smb2_query_symlink,
Steve Frenchc22870e2014-09-16 07:18:19 -05001675 .query_mf_symlink = smb3_query_mf_symlink,
Steve French5ab97572014-09-15 04:49:28 -05001676 .create_mf_symlink = smb3_create_mf_symlink,
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001677 .open = smb2_open_file,
1678 .set_fid = smb2_set_fid,
1679 .close = smb2_close_file,
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07001680 .flush = smb2_flush_file,
Pavel Shilovsky09a47072012-09-18 16:20:29 -07001681 .async_readv = smb2_async_readv,
Pavel Shilovsky33319142012-09-18 16:20:29 -07001682 .async_writev = smb2_async_writev,
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07001683 .sync_read = smb2_sync_read,
Pavel Shilovsky009d3442012-09-18 16:20:30 -07001684 .sync_write = smb2_sync_write,
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07001685 .query_dir_first = smb2_query_dir_first,
1686 .query_dir_next = smb2_query_dir_next,
1687 .close_dir = smb2_close_dir,
1688 .calc_smb_size = smb2_calc_size,
Pavel Shilovsky2e44b282012-09-18 16:20:33 -07001689 .is_status_pending = smb2_is_status_pending,
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07001690 .oplock_response = smb2_oplock_response,
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07001691 .queryfs = smb2_queryfs,
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07001692 .mand_lock = smb2_mand_lock,
1693 .mand_unlock_range = smb2_unlock_range,
Pavel Shilovskyb1407992012-09-19 06:22:44 -07001694 .push_mand_locks = smb2_push_mandatory_locks,
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001695 .get_lease_key = smb2_get_lease_key,
1696 .set_lease_key = smb2_set_lease_key,
1697 .new_lease_key = smb2_new_lease_key,
Steve French38107d42012-12-08 22:08:06 -06001698 .calc_signature = smb2_calc_signature,
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04001699 .is_read_op = smb21_is_read_op,
1700 .set_oplock_level = smb21_set_oplock_level,
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001701 .create_lease_buf = smb2_create_lease_buf,
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001702 .parse_lease_buf = smb2_parse_lease_buf,
Steve French41c13582013-11-14 00:05:36 -06001703 .clone_range = smb2_clone_range,
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04001704 .wp_retry_size = smb2_wp_retry_size,
Pavel Shilovsky52755802014-08-18 20:49:57 +04001705 .dir_needs_close = smb2_dir_needs_close,
Steve French834170c2016-09-30 21:14:26 -05001706 .enum_snapshots = smb3_enum_snapshots,
Steve French38107d42012-12-08 22:08:06 -06001707};
1708
Steve French38107d42012-12-08 22:08:06 -06001709struct smb_version_operations smb30_operations = {
1710 .compare_fids = smb2_compare_fids,
1711 .setup_request = smb2_setup_request,
1712 .setup_async_request = smb2_setup_async_request,
1713 .check_receive = smb2_check_receive,
1714 .add_credits = smb2_add_credits,
1715 .set_credits = smb2_set_credits,
1716 .get_credits_field = smb2_get_credits_field,
1717 .get_credits = smb2_get_credits,
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04001718 .wait_mtu_credits = smb2_wait_mtu_credits,
Steve French38107d42012-12-08 22:08:06 -06001719 .get_next_mid = smb2_get_next_mid,
1720 .read_data_offset = smb2_read_data_offset,
1721 .read_data_length = smb2_read_data_length,
1722 .map_error = map_smb2_to_linux_error,
1723 .find_mid = smb2_find_mid,
1724 .check_message = smb2_check_message,
1725 .dump_detail = smb2_dump_detail,
1726 .clear_stats = smb2_clear_stats,
1727 .print_stats = smb2_print_stats,
Steve French769ee6a2013-06-19 14:15:30 -05001728 .dump_share_caps = smb2_dump_share_caps,
Steve French38107d42012-12-08 22:08:06 -06001729 .is_oplock_break = smb2_is_valid_oplock_break,
Sachin Prabhuc11f1df2014-03-11 16:11:47 +00001730 .downgrade_oplock = smb2_downgrade_oplock,
Steve French38107d42012-12-08 22:08:06 -06001731 .need_neg = smb2_need_neg,
1732 .negotiate = smb2_negotiate,
1733 .negotiate_wsize = smb2_negotiate_wsize,
1734 .negotiate_rsize = smb2_negotiate_rsize,
1735 .sess_setup = SMB2_sess_setup,
1736 .logoff = SMB2_logoff,
1737 .tree_connect = SMB2_tcon,
1738 .tree_disconnect = SMB2_tdis,
Steven Frenchaf6a12e2013-10-09 20:55:53 -05001739 .qfs_tcon = smb3_qfs_tcon,
Steve French38107d42012-12-08 22:08:06 -06001740 .is_path_accessible = smb2_is_path_accessible,
1741 .can_echo = smb2_can_echo,
1742 .echo = SMB2_echo,
1743 .query_path_info = smb2_query_path_info,
1744 .get_srv_inum = smb2_get_srv_inum,
1745 .query_file_info = smb2_query_file_info,
1746 .set_path_size = smb2_set_path_size,
1747 .set_file_size = smb2_set_file_size,
1748 .set_file_info = smb2_set_file_info,
Steve French64a5cfa2013-10-14 15:31:32 -05001749 .set_compression = smb2_set_compression,
Steve French38107d42012-12-08 22:08:06 -06001750 .mkdir = smb2_mkdir,
1751 .mkdir_setinfo = smb2_mkdir_setinfo,
1752 .rmdir = smb2_rmdir,
1753 .unlink = smb2_unlink,
1754 .rename = smb2_rename_path,
1755 .create_hardlink = smb2_create_hardlink,
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04001756 .query_symlink = smb2_query_symlink,
Steve Frenchc22870e2014-09-16 07:18:19 -05001757 .query_mf_symlink = smb3_query_mf_symlink,
Steve French5ab97572014-09-15 04:49:28 -05001758 .create_mf_symlink = smb3_create_mf_symlink,
Steve French38107d42012-12-08 22:08:06 -06001759 .open = smb2_open_file,
1760 .set_fid = smb2_set_fid,
1761 .close = smb2_close_file,
1762 .flush = smb2_flush_file,
1763 .async_readv = smb2_async_readv,
1764 .async_writev = smb2_async_writev,
1765 .sync_read = smb2_sync_read,
1766 .sync_write = smb2_sync_write,
1767 .query_dir_first = smb2_query_dir_first,
1768 .query_dir_next = smb2_query_dir_next,
1769 .close_dir = smb2_close_dir,
1770 .calc_smb_size = smb2_calc_size,
1771 .is_status_pending = smb2_is_status_pending,
1772 .oplock_response = smb2_oplock_response,
1773 .queryfs = smb2_queryfs,
1774 .mand_lock = smb2_mand_lock,
1775 .mand_unlock_range = smb2_unlock_range,
1776 .push_mand_locks = smb2_push_mandatory_locks,
1777 .get_lease_key = smb2_get_lease_key,
1778 .set_lease_key = smb2_set_lease_key,
1779 .new_lease_key = smb2_new_lease_key,
Steve French373512e2015-12-18 13:05:30 -06001780 .generate_signingkey = generate_smb30signingkey,
Steve French38107d42012-12-08 22:08:06 -06001781 .calc_signature = smb3_calc_signature,
Steve Frenchb3152e22015-06-24 03:17:02 -05001782 .set_integrity = smb3_set_integrity,
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04001783 .is_read_op = smb21_is_read_op,
Pavel Shilovsky42873b02013-09-05 21:30:16 +04001784 .set_oplock_level = smb3_set_oplock_level,
Pavel Shilovskyf0473902013-09-04 13:44:05 +04001785 .create_lease_buf = smb3_create_lease_buf,
1786 .parse_lease_buf = smb3_parse_lease_buf,
Steve French41c13582013-11-14 00:05:36 -06001787 .clone_range = smb2_clone_range,
Steve Frenchca9e7a12015-10-01 21:40:10 -05001788 .duplicate_extents = smb2_duplicate_extents,
Steve Frenchff1c0382013-11-19 23:44:46 -06001789 .validate_negotiate = smb3_validate_negotiate,
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04001790 .wp_retry_size = smb2_wp_retry_size,
Pavel Shilovsky52755802014-08-18 20:49:57 +04001791 .dir_needs_close = smb2_dir_needs_close,
Steve French31742c52014-08-17 08:38:47 -05001792 .fallocate = smb3_fallocate,
Steve French834170c2016-09-30 21:14:26 -05001793 .enum_snapshots = smb3_enum_snapshots,
Steve French1080ef72011-02-24 18:07:19 +00001794};
1795
Steve Frenchaab18932015-06-23 23:37:11 -05001796#ifdef CONFIG_CIFS_SMB311
1797struct smb_version_operations smb311_operations = {
1798 .compare_fids = smb2_compare_fids,
1799 .setup_request = smb2_setup_request,
1800 .setup_async_request = smb2_setup_async_request,
1801 .check_receive = smb2_check_receive,
1802 .add_credits = smb2_add_credits,
1803 .set_credits = smb2_set_credits,
1804 .get_credits_field = smb2_get_credits_field,
1805 .get_credits = smb2_get_credits,
1806 .wait_mtu_credits = smb2_wait_mtu_credits,
1807 .get_next_mid = smb2_get_next_mid,
1808 .read_data_offset = smb2_read_data_offset,
1809 .read_data_length = smb2_read_data_length,
1810 .map_error = map_smb2_to_linux_error,
1811 .find_mid = smb2_find_mid,
1812 .check_message = smb2_check_message,
1813 .dump_detail = smb2_dump_detail,
1814 .clear_stats = smb2_clear_stats,
1815 .print_stats = smb2_print_stats,
1816 .dump_share_caps = smb2_dump_share_caps,
1817 .is_oplock_break = smb2_is_valid_oplock_break,
1818 .downgrade_oplock = smb2_downgrade_oplock,
1819 .need_neg = smb2_need_neg,
1820 .negotiate = smb2_negotiate,
1821 .negotiate_wsize = smb2_negotiate_wsize,
1822 .negotiate_rsize = smb2_negotiate_rsize,
1823 .sess_setup = SMB2_sess_setup,
1824 .logoff = SMB2_logoff,
1825 .tree_connect = SMB2_tcon,
1826 .tree_disconnect = SMB2_tdis,
1827 .qfs_tcon = smb3_qfs_tcon,
1828 .is_path_accessible = smb2_is_path_accessible,
1829 .can_echo = smb2_can_echo,
1830 .echo = SMB2_echo,
1831 .query_path_info = smb2_query_path_info,
1832 .get_srv_inum = smb2_get_srv_inum,
1833 .query_file_info = smb2_query_file_info,
1834 .set_path_size = smb2_set_path_size,
1835 .set_file_size = smb2_set_file_size,
1836 .set_file_info = smb2_set_file_info,
1837 .set_compression = smb2_set_compression,
1838 .mkdir = smb2_mkdir,
1839 .mkdir_setinfo = smb2_mkdir_setinfo,
1840 .rmdir = smb2_rmdir,
1841 .unlink = smb2_unlink,
1842 .rename = smb2_rename_path,
1843 .create_hardlink = smb2_create_hardlink,
1844 .query_symlink = smb2_query_symlink,
1845 .query_mf_symlink = smb3_query_mf_symlink,
1846 .create_mf_symlink = smb3_create_mf_symlink,
1847 .open = smb2_open_file,
1848 .set_fid = smb2_set_fid,
1849 .close = smb2_close_file,
1850 .flush = smb2_flush_file,
1851 .async_readv = smb2_async_readv,
1852 .async_writev = smb2_async_writev,
1853 .sync_read = smb2_sync_read,
1854 .sync_write = smb2_sync_write,
1855 .query_dir_first = smb2_query_dir_first,
1856 .query_dir_next = smb2_query_dir_next,
1857 .close_dir = smb2_close_dir,
1858 .calc_smb_size = smb2_calc_size,
1859 .is_status_pending = smb2_is_status_pending,
1860 .oplock_response = smb2_oplock_response,
1861 .queryfs = smb2_queryfs,
1862 .mand_lock = smb2_mand_lock,
1863 .mand_unlock_range = smb2_unlock_range,
1864 .push_mand_locks = smb2_push_mandatory_locks,
1865 .get_lease_key = smb2_get_lease_key,
1866 .set_lease_key = smb2_set_lease_key,
1867 .new_lease_key = smb2_new_lease_key,
Steve French373512e2015-12-18 13:05:30 -06001868 .generate_signingkey = generate_smb311signingkey,
Steve Frenchaab18932015-06-23 23:37:11 -05001869 .calc_signature = smb3_calc_signature,
Steve Frenchb3152e22015-06-24 03:17:02 -05001870 .set_integrity = smb3_set_integrity,
Steve Frenchaab18932015-06-23 23:37:11 -05001871 .is_read_op = smb21_is_read_op,
1872 .set_oplock_level = smb3_set_oplock_level,
1873 .create_lease_buf = smb3_create_lease_buf,
1874 .parse_lease_buf = smb3_parse_lease_buf,
1875 .clone_range = smb2_clone_range,
Steve French02b16662015-06-27 21:18:36 -07001876 .duplicate_extents = smb2_duplicate_extents,
Steve Frenchaab18932015-06-23 23:37:11 -05001877/* .validate_negotiate = smb3_validate_negotiate, */ /* not used in 3.11 */
1878 .wp_retry_size = smb2_wp_retry_size,
1879 .dir_needs_close = smb2_dir_needs_close,
1880 .fallocate = smb3_fallocate,
Steve French834170c2016-09-30 21:14:26 -05001881 .enum_snapshots = smb3_enum_snapshots,
Steve Frenchaab18932015-06-23 23:37:11 -05001882};
1883#endif /* CIFS_SMB311 */
1884
Steve Frenchdd446b12012-11-28 23:21:06 -06001885struct smb_version_values smb20_values = {
1886 .version_string = SMB20_VERSION_STRING,
1887 .protocol_id = SMB20_PROT_ID,
1888 .req_capabilities = 0, /* MBZ */
1889 .large_lock_type = 0,
1890 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
1891 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
1892 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
1893 .header_size = sizeof(struct smb2_hdr),
1894 .max_header_size = MAX_SMB2_HDR_SIZE,
1895 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
1896 .lock_cmd = SMB2_LOCK,
1897 .cap_unix = 0,
1898 .cap_nt_find = SMB2_NT_FIND,
1899 .cap_large_files = SMB2_LARGE_FILES,
Jeff Layton50285882013-06-27 12:45:00 -04001900 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
1901 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001902 .create_lease_size = sizeof(struct create_lease),
Steve Frenchdd446b12012-11-28 23:21:06 -06001903};
1904
Steve French1080ef72011-02-24 18:07:19 +00001905struct smb_version_values smb21_values = {
1906 .version_string = SMB21_VERSION_STRING,
Steve Frenche4aa25e2012-10-01 12:26:22 -05001907 .protocol_id = SMB21_PROT_ID,
1908 .req_capabilities = 0, /* MBZ on negotiate req until SMB3 dialect */
1909 .large_lock_type = 0,
1910 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
1911 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
1912 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
1913 .header_size = sizeof(struct smb2_hdr),
1914 .max_header_size = MAX_SMB2_HDR_SIZE,
1915 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
1916 .lock_cmd = SMB2_LOCK,
1917 .cap_unix = 0,
1918 .cap_nt_find = SMB2_NT_FIND,
1919 .cap_large_files = SMB2_LARGE_FILES,
Jeff Layton50285882013-06-27 12:45:00 -04001920 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
1921 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001922 .create_lease_size = sizeof(struct create_lease),
Steve Frenche4aa25e2012-10-01 12:26:22 -05001923};
1924
1925struct smb_version_values smb30_values = {
1926 .version_string = SMB30_VERSION_STRING,
1927 .protocol_id = SMB30_PROT_ID,
Steve French373512e2015-12-18 13:05:30 -06001928 .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 -07001929 .large_lock_type = 0,
1930 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
1931 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
1932 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
Pavel Shilovsky093b2bd2011-06-08 15:51:07 +04001933 .header_size = sizeof(struct smb2_hdr),
1934 .max_header_size = MAX_SMB2_HDR_SIZE,
Pavel Shilovsky09a47072012-09-18 16:20:29 -07001935 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +04001936 .lock_cmd = SMB2_LOCK,
Pavel Shilovsky29e20f92012-07-13 13:58:14 +04001937 .cap_unix = 0,
1938 .cap_nt_find = SMB2_NT_FIND,
1939 .cap_large_files = SMB2_LARGE_FILES,
Jeff Layton50285882013-06-27 12:45:00 -04001940 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
1941 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
Pavel Shilovskyf0473902013-09-04 13:44:05 +04001942 .create_lease_size = sizeof(struct create_lease_v2),
Steve French1080ef72011-02-24 18:07:19 +00001943};
Steve French20b6d8b2013-06-12 22:48:41 -05001944
1945struct smb_version_values smb302_values = {
1946 .version_string = SMB302_VERSION_STRING,
1947 .protocol_id = SMB302_PROT_ID,
Steve French373512e2015-12-18 13:05:30 -06001948 .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 -05001949 .large_lock_type = 0,
1950 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
1951 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
1952 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
1953 .header_size = sizeof(struct smb2_hdr),
1954 .max_header_size = MAX_SMB2_HDR_SIZE,
1955 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
1956 .lock_cmd = SMB2_LOCK,
1957 .cap_unix = 0,
1958 .cap_nt_find = SMB2_NT_FIND,
1959 .cap_large_files = SMB2_LARGE_FILES,
Jeff Layton50285882013-06-27 12:45:00 -04001960 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
1961 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
Pavel Shilovskyf0473902013-09-04 13:44:05 +04001962 .create_lease_size = sizeof(struct create_lease_v2),
Steve French20b6d8b2013-06-12 22:48:41 -05001963};
Steve French5f7fbf72014-12-17 22:52:58 -06001964
1965#ifdef CONFIG_CIFS_SMB311
1966struct smb_version_values smb311_values = {
1967 .version_string = SMB311_VERSION_STRING,
1968 .protocol_id = SMB311_PROT_ID,
Steve Frenchb618f002015-11-03 09:15:03 -06001969 .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 -06001970 .large_lock_type = 0,
1971 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
1972 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
1973 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
1974 .header_size = sizeof(struct smb2_hdr),
1975 .max_header_size = MAX_SMB2_HDR_SIZE,
1976 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
1977 .lock_cmd = SMB2_LOCK,
1978 .cap_unix = 0,
1979 .cap_nt_find = SMB2_NT_FIND,
1980 .cap_large_files = SMB2_LARGE_FILES,
1981 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
1982 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
1983 .create_lease_size = sizeof(struct create_lease_v2),
1984};
1985#endif /* SMB311 */