CIFS: add use_ipc flag to SMB2_ioctl()
[linux-2.6-block.git] / fs / cifs / smb2ops.c
CommitLineData
1080ef75
SF
1/*
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
3a3bab50 20#include <linux/pagemap.h>
6fc05c25 21#include <linux/vfs.h>
f29ebb47 22#include <linux/falloc.h>
026e93dc
PS
23#include <linux/scatterlist.h>
24#include <crypto/aead.h>
1080ef75 25#include "cifsglob.h"
2dc7e1c0
PS
26#include "smb2pdu.h"
27#include "smb2proto.h"
28ea5290
PS
28#include "cifsproto.h"
29#include "cifs_debug.h"
b42bf888 30#include "cifs_unicode.h"
2e44b288 31#include "smb2status.h"
6fc05c25 32#include "smb2glob.h"
834170c8 33#include "cifs_ioctl.h"
28ea5290
PS
34
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;
f96637be 46 cifs_dbg(VFS, "disabling echoes and oplocks\n");
28ea5290
PS
47 break;
48 case 2:
49 server->echoes = true;
50 server->oplocks = false;
51 server->echo_credits = 1;
f96637be 52 cifs_dbg(FYI, "disabling oplocks\n");
28ea5290
PS
53 break;
54 default:
55 server->echoes = true;
e0ddde9d
SF
56 if (enable_oplocks) {
57 server->oplocks = true;
58 server->oplock_credits = 1;
59 } else
60 server->oplocks = false;
61
28ea5290 62 server->echo_credits = 1;
28ea5290
PS
63 }
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;
141891f4
SF
76 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 }
28ea5290 80 server->in_flight--;
ec2e4523 81 if (server->in_flight == 0 && (optype & CIFS_OP_MASK) != CIFS_NEG_OP)
28ea5290 82 rc = change_conf(server);
983c88a4
PS
83 /*
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 }
28ea5290
PS
94 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{
31473fc4
PS
124 struct smb2_sync_hdr *shdr = get_sync_hdr(mid->resp_buf);
125
126 return le16_to_cpu(shdr->CreditRequest);
28ea5290 127}
2dc7e1c0 128
cb7e9eab
PS
129static 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
2dc7e1c0
PS
176static __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}
1080ef75 186
093b2bda
PS
187static struct mid_q_entry *
188smb2_find_mid(struct TCP_Server_Info *server, char *buf)
189{
190 struct mid_q_entry *mid;
31473fc4
PS
191 struct smb2_sync_hdr *shdr = get_sync_hdr(buf);
192 __u64 wire_mid = le64_to_cpu(shdr->MessageId);
093b2bda 193
31473fc4 194 if (shdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM) {
373512ec
SF
195 cifs_dbg(VFS, "encrypted frame parsing not supported yet");
196 return NULL;
197 }
198
093b2bda
PS
199 spin_lock(&GlobalMid_Lock);
200 list_for_each_entry(mid, &server->pending_mid_q, qhead) {
9235d098 201 if ((mid->mid == wire_mid) &&
093b2bda 202 (mid->mid_state == MID_REQUEST_SUBMITTED) &&
31473fc4 203 (mid->command == shdr->Command)) {
093b2bda
PS
204 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
31473fc4 216 struct smb2_sync_hdr *shdr = get_sync_hdr(buf);
093b2bda 217
f96637be 218 cifs_dbg(VFS, "Cmd: %d Err: 0x%x Flags: 0x%x Mid: %llu Pid: %d\n",
31473fc4
PS
219 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));
093b2bda
PS
222#endif
223}
224
ec2e4523
PS
225static 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
3a3bab50
PS
243static 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);
cb7e9eab
PS
252
253 if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
254 wsize = min_t(unsigned int, wsize, SMB2_MAX_BUFFER_SIZE);
3a3bab50 255
3a3bab50
PS
256 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);
bed9da02
PS
268
269 if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
270 rsize = min_t(unsigned int, rsize, SMB2_MAX_BUFFER_SIZE);
3a3bab50 271
3a3bab50
PS
272 return rsize;
273}
274
c481e9fe
SF
275#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 */,
51146625 285 false /* use_ipc */,
c481e9fe
SF
286 NULL /* no data input */, 0 /* no data input */,
287 (char **)&out_buf, &ret_data_len);
9ffc5412
SF
288 if (rc != 0)
289 cifs_dbg(VFS, "error %d on ioctl to get interface list\n", rc);
290 else if (ret_data_len < sizeof(struct network_interface_info_ioctl_rsp)) {
291 cifs_dbg(VFS, "server returned bad net interface info buf\n");
292 rc = -EINVAL;
293 } else {
c481e9fe
SF
294 /* Dump info on first interface */
295 cifs_dbg(FYI, "Adapter Capability 0x%x\t",
296 le32_to_cpu(out_buf->Capability));
297 cifs_dbg(FYI, "Link Speed %lld\n",
298 le64_to_cpu(out_buf->LinkSpeed));
9ffc5412 299 }
24df1483 300 kfree(out_buf);
c481e9fe
SF
301 return rc;
302}
303#endif /* STATS2 */
304
af6a12ea
SF
305static void
306smb3_qfs_tcon(const unsigned int xid, struct cifs_tcon *tcon)
307{
308 int rc;
309 __le16 srch_path = 0; /* Null - open root of share */
310 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
311 struct cifs_open_parms oparms;
312 struct cifs_fid fid;
313
314 oparms.tcon = tcon;
315 oparms.desired_access = FILE_READ_ATTRIBUTES;
316 oparms.disposition = FILE_OPEN;
317 oparms.create_options = 0;
318 oparms.fid = &fid;
319 oparms.reconnect = false;
320
321 rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL);
322 if (rc)
323 return;
324
c481e9fe
SF
325#ifdef CONFIG_CIFS_STATS2
326 SMB3_request_interfaces(xid, tcon);
327#endif /* STATS2 */
328
af6a12ea
SF
329 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
330 FS_ATTRIBUTE_INFORMATION);
331 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
332 FS_DEVICE_INFORMATION);
333 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
334 FS_SECTOR_SIZE_INFORMATION); /* SMB3 specific */
335 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
336 return;
337}
338
34f62640
SF
339static void
340smb2_qfs_tcon(const unsigned int xid, struct cifs_tcon *tcon)
341{
342 int rc;
343 __le16 srch_path = 0; /* Null - open root of share */
344 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
345 struct cifs_open_parms oparms;
346 struct cifs_fid fid;
347
348 oparms.tcon = tcon;
349 oparms.desired_access = FILE_READ_ATTRIBUTES;
350 oparms.disposition = FILE_OPEN;
351 oparms.create_options = 0;
352 oparms.fid = &fid;
353 oparms.reconnect = false;
354
355 rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL);
356 if (rc)
357 return;
358
2167114c
SF
359 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
360 FS_ATTRIBUTE_INFORMATION);
361 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
362 FS_DEVICE_INFORMATION);
34f62640
SF
363 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
364 return;
365}
366
2503a0db
PS
367static int
368smb2_is_path_accessible(const unsigned int xid, struct cifs_tcon *tcon,
369 struct cifs_sb_info *cifs_sb, const char *full_path)
370{
371 int rc;
2503a0db 372 __le16 *utf16_path;
2e44b288 373 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
064f6047
PS
374 struct cifs_open_parms oparms;
375 struct cifs_fid fid;
2503a0db
PS
376
377 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
378 if (!utf16_path)
379 return -ENOMEM;
380
064f6047
PS
381 oparms.tcon = tcon;
382 oparms.desired_access = FILE_READ_ATTRIBUTES;
383 oparms.disposition = FILE_OPEN;
384 oparms.create_options = 0;
385 oparms.fid = &fid;
9cbc0b73 386 oparms.reconnect = false;
064f6047 387
b42bf888 388 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL);
2503a0db
PS
389 if (rc) {
390 kfree(utf16_path);
391 return rc;
392 }
393
064f6047 394 rc = SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
2503a0db
PS
395 kfree(utf16_path);
396 return rc;
397}
398
be4cb9e3
PS
399static int
400smb2_get_srv_inum(const unsigned int xid, struct cifs_tcon *tcon,
401 struct cifs_sb_info *cifs_sb, const char *full_path,
402 u64 *uniqueid, FILE_ALL_INFO *data)
403{
404 *uniqueid = le64_to_cpu(data->IndexNumber);
405 return 0;
406}
407
b7546bc5
PS
408static int
409smb2_query_file_info(const unsigned int xid, struct cifs_tcon *tcon,
410 struct cifs_fid *fid, FILE_ALL_INFO *data)
411{
412 int rc;
413 struct smb2_file_all_info *smb2_data;
414
1bbe4997 415 smb2_data = kzalloc(sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
b7546bc5
PS
416 GFP_KERNEL);
417 if (smb2_data == NULL)
418 return -ENOMEM;
419
420 rc = SMB2_query_info(xid, tcon, fid->persistent_fid, fid->volatile_fid,
421 smb2_data);
422 if (!rc)
423 move_smb2_info_to_cifs(data, smb2_data);
424 kfree(smb2_data);
425 return rc;
426}
427
9094fad1
PS
428static bool
429smb2_can_echo(struct TCP_Server_Info *server)
430{
431 return server->echoes;
432}
433
d60622eb
PS
434static void
435smb2_clear_stats(struct cifs_tcon *tcon)
436{
437#ifdef CONFIG_CIFS_STATS
438 int i;
439 for (i = 0; i < NUMBER_OF_SMB2_COMMANDS; i++) {
440 atomic_set(&tcon->stats.smb2_stats.smb2_com_sent[i], 0);
441 atomic_set(&tcon->stats.smb2_stats.smb2_com_failed[i], 0);
442 }
443#endif
444}
445
769ee6a4
SF
446static void
447smb2_dump_share_caps(struct seq_file *m, struct cifs_tcon *tcon)
448{
449 seq_puts(m, "\n\tShare Capabilities:");
450 if (tcon->capabilities & SMB2_SHARE_CAP_DFS)
451 seq_puts(m, " DFS,");
452 if (tcon->capabilities & SMB2_SHARE_CAP_CONTINUOUS_AVAILABILITY)
453 seq_puts(m, " CONTINUOUS AVAILABILITY,");
454 if (tcon->capabilities & SMB2_SHARE_CAP_SCALEOUT)
455 seq_puts(m, " SCALEOUT,");
456 if (tcon->capabilities & SMB2_SHARE_CAP_CLUSTER)
457 seq_puts(m, " CLUSTER,");
458 if (tcon->capabilities & SMB2_SHARE_CAP_ASYMMETRIC)
459 seq_puts(m, " ASYMMETRIC,");
460 if (tcon->capabilities == 0)
461 seq_puts(m, " None");
af6a12ea
SF
462 if (tcon->ss_flags & SSINFO_FLAGS_ALIGNED_DEVICE)
463 seq_puts(m, " Aligned,");
464 if (tcon->ss_flags & SSINFO_FLAGS_PARTITION_ALIGNED_ON_DEVICE)
465 seq_puts(m, " Partition Aligned,");
466 if (tcon->ss_flags & SSINFO_FLAGS_NO_SEEK_PENALTY)
467 seq_puts(m, " SSD,");
468 if (tcon->ss_flags & SSINFO_FLAGS_TRIM_ENABLED)
469 seq_puts(m, " TRIM-support,");
470
769ee6a4 471 seq_printf(m, "\tShare Flags: 0x%x", tcon->share_flags);
af6a12ea
SF
472 if (tcon->perf_sector_size)
473 seq_printf(m, "\tOptimal sector size: 0x%x",
474 tcon->perf_sector_size);
769ee6a4
SF
475}
476
d60622eb
PS
477static void
478smb2_print_stats(struct seq_file *m, struct cifs_tcon *tcon)
479{
480#ifdef CONFIG_CIFS_STATS
481 atomic_t *sent = tcon->stats.smb2_stats.smb2_com_sent;
482 atomic_t *failed = tcon->stats.smb2_stats.smb2_com_failed;
483 seq_printf(m, "\nNegotiates: %d sent %d failed",
484 atomic_read(&sent[SMB2_NEGOTIATE_HE]),
485 atomic_read(&failed[SMB2_NEGOTIATE_HE]));
486 seq_printf(m, "\nSessionSetups: %d sent %d failed",
487 atomic_read(&sent[SMB2_SESSION_SETUP_HE]),
488 atomic_read(&failed[SMB2_SESSION_SETUP_HE]));
d60622eb
PS
489 seq_printf(m, "\nLogoffs: %d sent %d failed",
490 atomic_read(&sent[SMB2_LOGOFF_HE]),
491 atomic_read(&failed[SMB2_LOGOFF_HE]));
492 seq_printf(m, "\nTreeConnects: %d sent %d failed",
493 atomic_read(&sent[SMB2_TREE_CONNECT_HE]),
494 atomic_read(&failed[SMB2_TREE_CONNECT_HE]));
495 seq_printf(m, "\nTreeDisconnects: %d sent %d failed",
496 atomic_read(&sent[SMB2_TREE_DISCONNECT_HE]),
497 atomic_read(&failed[SMB2_TREE_DISCONNECT_HE]));
498 seq_printf(m, "\nCreates: %d sent %d failed",
499 atomic_read(&sent[SMB2_CREATE_HE]),
500 atomic_read(&failed[SMB2_CREATE_HE]));
501 seq_printf(m, "\nCloses: %d sent %d failed",
502 atomic_read(&sent[SMB2_CLOSE_HE]),
503 atomic_read(&failed[SMB2_CLOSE_HE]));
504 seq_printf(m, "\nFlushes: %d sent %d failed",
505 atomic_read(&sent[SMB2_FLUSH_HE]),
506 atomic_read(&failed[SMB2_FLUSH_HE]));
507 seq_printf(m, "\nReads: %d sent %d failed",
508 atomic_read(&sent[SMB2_READ_HE]),
509 atomic_read(&failed[SMB2_READ_HE]));
510 seq_printf(m, "\nWrites: %d sent %d failed",
511 atomic_read(&sent[SMB2_WRITE_HE]),
512 atomic_read(&failed[SMB2_WRITE_HE]));
513 seq_printf(m, "\nLocks: %d sent %d failed",
514 atomic_read(&sent[SMB2_LOCK_HE]),
515 atomic_read(&failed[SMB2_LOCK_HE]));
516 seq_printf(m, "\nIOCTLs: %d sent %d failed",
517 atomic_read(&sent[SMB2_IOCTL_HE]),
518 atomic_read(&failed[SMB2_IOCTL_HE]));
519 seq_printf(m, "\nCancels: %d sent %d failed",
520 atomic_read(&sent[SMB2_CANCEL_HE]),
521 atomic_read(&failed[SMB2_CANCEL_HE]));
522 seq_printf(m, "\nEchos: %d sent %d failed",
523 atomic_read(&sent[SMB2_ECHO_HE]),
524 atomic_read(&failed[SMB2_ECHO_HE]));
525 seq_printf(m, "\nQueryDirectories: %d sent %d failed",
526 atomic_read(&sent[SMB2_QUERY_DIRECTORY_HE]),
527 atomic_read(&failed[SMB2_QUERY_DIRECTORY_HE]));
528 seq_printf(m, "\nChangeNotifies: %d sent %d failed",
529 atomic_read(&sent[SMB2_CHANGE_NOTIFY_HE]),
530 atomic_read(&failed[SMB2_CHANGE_NOTIFY_HE]));
531 seq_printf(m, "\nQueryInfos: %d sent %d failed",
532 atomic_read(&sent[SMB2_QUERY_INFO_HE]),
533 atomic_read(&failed[SMB2_QUERY_INFO_HE]));
534 seq_printf(m, "\nSetInfos: %d sent %d failed",
535 atomic_read(&sent[SMB2_SET_INFO_HE]),
536 atomic_read(&failed[SMB2_SET_INFO_HE]));
537 seq_printf(m, "\nOplockBreaks: %d sent %d failed",
538 atomic_read(&sent[SMB2_OPLOCK_BREAK_HE]),
539 atomic_read(&failed[SMB2_OPLOCK_BREAK_HE]));
540#endif
541}
542
f0df737e
PS
543static void
544smb2_set_fid(struct cifsFileInfo *cfile, struct cifs_fid *fid, __u32 oplock)
545{
2b0143b5 546 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
53ef1016
PS
547 struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server;
548
f0df737e
PS
549 cfile->fid.persistent_fid = fid->persistent_fid;
550 cfile->fid.volatile_fid = fid->volatile_fid;
42873b0a
PS
551 server->ops->set_oplock_level(cinode, oplock, fid->epoch,
552 &fid->purge_cache);
18cceb6a 553 cinode->can_cache_brlcks = CIFS_CACHE_WRITE(cinode);
94f87371 554 memcpy(cfile->fid.create_guid, fid->create_guid, 16);
f0df737e
PS
555}
556
760ad0ca 557static void
f0df737e
PS
558smb2_close_file(const unsigned int xid, struct cifs_tcon *tcon,
559 struct cifs_fid *fid)
560{
760ad0ca 561 SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
f0df737e
PS
562}
563
41c1358e
SF
564static int
565SMB2_request_res_key(const unsigned int xid, struct cifs_tcon *tcon,
566 u64 persistent_fid, u64 volatile_fid,
567 struct copychunk_ioctl *pcchunk)
568{
569 int rc;
570 unsigned int ret_data_len;
571 struct resume_key_req *res_key;
572
573 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
574 FSCTL_SRV_REQUEST_RESUME_KEY, true /* is_fsctl */,
51146625 575 false /* use_ipc */,
41c1358e
SF
576 NULL, 0 /* no input */,
577 (char **)&res_key, &ret_data_len);
578
579 if (rc) {
580 cifs_dbg(VFS, "refcpy ioctl error %d getting resume key\n", rc);
581 goto req_res_key_exit;
582 }
583 if (ret_data_len < sizeof(struct resume_key_req)) {
584 cifs_dbg(VFS, "Invalid refcopy resume key length\n");
585 rc = -EINVAL;
586 goto req_res_key_exit;
587 }
588 memcpy(pcchunk->SourceKey, res_key->ResumeKey, COPY_CHUNK_RES_KEY_SIZE);
589
590req_res_key_exit:
591 kfree(res_key);
592 return rc;
593}
594
595static int
596smb2_clone_range(const unsigned int xid,
597 struct cifsFileInfo *srcfile,
598 struct cifsFileInfo *trgtfile, u64 src_off,
599 u64 len, u64 dest_off)
600{
601 int rc;
602 unsigned int ret_data_len;
603 struct copychunk_ioctl *pcchunk;
9bf0c9cd
SF
604 struct copychunk_ioctl_rsp *retbuf = NULL;
605 struct cifs_tcon *tcon;
606 int chunks_copied = 0;
607 bool chunk_sizes_updated = false;
41c1358e
SF
608
609 pcchunk = kmalloc(sizeof(struct copychunk_ioctl), GFP_KERNEL);
610
611 if (pcchunk == NULL)
612 return -ENOMEM;
613
614 cifs_dbg(FYI, "in smb2_clone_range - about to call request res key\n");
615 /* Request a key from the server to identify the source of the copy */
616 rc = SMB2_request_res_key(xid, tlink_tcon(srcfile->tlink),
617 srcfile->fid.persistent_fid,
618 srcfile->fid.volatile_fid, pcchunk);
619
620 /* Note: request_res_key sets res_key null only if rc !=0 */
621 if (rc)
9bf0c9cd 622 goto cchunk_out;
41c1358e
SF
623
624 /* For now array only one chunk long, will make more flexible later */
bc09d141 625 pcchunk->ChunkCount = cpu_to_le32(1);
41c1358e 626 pcchunk->Reserved = 0;
41c1358e
SF
627 pcchunk->Reserved2 = 0;
628
9bf0c9cd 629 tcon = tlink_tcon(trgtfile->tlink);
41c1358e 630
9bf0c9cd
SF
631 while (len > 0) {
632 pcchunk->SourceOffset = cpu_to_le64(src_off);
633 pcchunk->TargetOffset = cpu_to_le64(dest_off);
634 pcchunk->Length =
635 cpu_to_le32(min_t(u32, len, tcon->max_bytes_chunk));
41c1358e 636
9bf0c9cd
SF
637 /* Request server copy to target from src identified by key */
638 rc = SMB2_ioctl(xid, tcon, trgtfile->fid.persistent_fid,
639 trgtfile->fid.volatile_fid, FSCTL_SRV_COPYCHUNK_WRITE,
51146625
AA
640 true /* is_fsctl */, false /* use_ipc */,
641 (char *)pcchunk,
9bf0c9cd
SF
642 sizeof(struct copychunk_ioctl), (char **)&retbuf,
643 &ret_data_len);
644 if (rc == 0) {
645 if (ret_data_len !=
646 sizeof(struct copychunk_ioctl_rsp)) {
647 cifs_dbg(VFS, "invalid cchunk response size\n");
648 rc = -EIO;
649 goto cchunk_out;
650 }
651 if (retbuf->TotalBytesWritten == 0) {
652 cifs_dbg(FYI, "no bytes copied\n");
653 rc = -EIO;
654 goto cchunk_out;
655 }
656 /*
657 * Check if server claimed to write more than we asked
658 */
659 if (le32_to_cpu(retbuf->TotalBytesWritten) >
660 le32_to_cpu(pcchunk->Length)) {
661 cifs_dbg(VFS, "invalid copy chunk response\n");
662 rc = -EIO;
663 goto cchunk_out;
664 }
665 if (le32_to_cpu(retbuf->ChunksWritten) != 1) {
666 cifs_dbg(VFS, "invalid num chunks written\n");
667 rc = -EIO;
668 goto cchunk_out;
669 }
670 chunks_copied++;
671
672 src_off += le32_to_cpu(retbuf->TotalBytesWritten);
673 dest_off += le32_to_cpu(retbuf->TotalBytesWritten);
674 len -= le32_to_cpu(retbuf->TotalBytesWritten);
675
676 cifs_dbg(FYI, "Chunks %d PartialChunk %d Total %d\n",
677 le32_to_cpu(retbuf->ChunksWritten),
678 le32_to_cpu(retbuf->ChunkBytesWritten),
679 le32_to_cpu(retbuf->TotalBytesWritten));
680 } else if (rc == -EINVAL) {
681 if (ret_data_len != sizeof(struct copychunk_ioctl_rsp))
682 goto cchunk_out;
683
684 cifs_dbg(FYI, "MaxChunks %d BytesChunk %d MaxCopy %d\n",
685 le32_to_cpu(retbuf->ChunksWritten),
686 le32_to_cpu(retbuf->ChunkBytesWritten),
687 le32_to_cpu(retbuf->TotalBytesWritten));
688
689 /*
690 * Check if this is the first request using these sizes,
691 * (ie check if copy succeed once with original sizes
692 * and check if the server gave us different sizes after
693 * we already updated max sizes on previous request).
694 * if not then why is the server returning an error now
695 */
696 if ((chunks_copied != 0) || chunk_sizes_updated)
697 goto cchunk_out;
698
699 /* Check that server is not asking us to grow size */
700 if (le32_to_cpu(retbuf->ChunkBytesWritten) <
701 tcon->max_bytes_chunk)
702 tcon->max_bytes_chunk =
703 le32_to_cpu(retbuf->ChunkBytesWritten);
704 else
705 goto cchunk_out; /* server gave us bogus size */
706
707 /* No need to change MaxChunks since already set to 1 */
708 chunk_sizes_updated = true;
2477bc58
SP
709 } else
710 goto cchunk_out;
9bf0c9cd 711 }
41c1358e 712
9bf0c9cd 713cchunk_out:
41c1358e 714 kfree(pcchunk);
24df1483 715 kfree(retbuf);
41c1358e
SF
716 return rc;
717}
718
7a5cfb19
PS
719static int
720smb2_flush_file(const unsigned int xid, struct cifs_tcon *tcon,
721 struct cifs_fid *fid)
722{
723 return SMB2_flush(xid, tcon, fid->persistent_fid, fid->volatile_fid);
724}
725
09a4707e
PS
726static unsigned int
727smb2_read_data_offset(char *buf)
728{
729 struct smb2_read_rsp *rsp = (struct smb2_read_rsp *)buf;
730 return rsp->DataOffset;
731}
732
733static unsigned int
734smb2_read_data_length(char *buf)
735{
736 struct smb2_read_rsp *rsp = (struct smb2_read_rsp *)buf;
737 return le32_to_cpu(rsp->DataLength);
738}
739
d8e05039
PS
740
741static int
db8b631d 742smb2_sync_read(const unsigned int xid, struct cifs_fid *pfid,
d8e05039
PS
743 struct cifs_io_parms *parms, unsigned int *bytes_read,
744 char **buf, int *buf_type)
745{
db8b631d
SF
746 parms->persistent_fid = pfid->persistent_fid;
747 parms->volatile_fid = pfid->volatile_fid;
d8e05039
PS
748 return SMB2_read(xid, parms, bytes_read, buf, buf_type);
749}
750
009d3443 751static int
db8b631d 752smb2_sync_write(const unsigned int xid, struct cifs_fid *pfid,
009d3443
PS
753 struct cifs_io_parms *parms, unsigned int *written,
754 struct kvec *iov, unsigned long nr_segs)
755{
756
db8b631d
SF
757 parms->persistent_fid = pfid->persistent_fid;
758 parms->volatile_fid = pfid->volatile_fid;
009d3443
PS
759 return SMB2_write(xid, parms, written, iov, nr_segs);
760}
761
d43cc793
SF
762/* Set or clear the SPARSE_FILE attribute based on value passed in setsparse */
763static bool smb2_set_sparse(const unsigned int xid, struct cifs_tcon *tcon,
764 struct cifsFileInfo *cfile, struct inode *inode, __u8 setsparse)
765{
766 struct cifsInodeInfo *cifsi;
767 int rc;
768
769 cifsi = CIFS_I(inode);
770
771 /* if file already sparse don't bother setting sparse again */
772 if ((cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) && setsparse)
773 return true; /* already sparse */
774
775 if (!(cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) && !setsparse)
776 return true; /* already not sparse */
777
778 /*
779 * Can't check for sparse support on share the usual way via the
780 * FS attribute info (FILE_SUPPORTS_SPARSE_FILES) on the share
781 * since Samba server doesn't set the flag on the share, yet
782 * supports the set sparse FSCTL and returns sparse correctly
783 * in the file attributes. If we fail setting sparse though we
784 * mark that server does not support sparse files for this share
785 * to avoid repeatedly sending the unsupported fsctl to server
786 * if the file is repeatedly extended.
787 */
788 if (tcon->broken_sparse_sup)
789 return false;
790
791 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
792 cfile->fid.volatile_fid, FSCTL_SET_SPARSE,
51146625
AA
793 true /* is_fctl */, false /* use_ipc */,
794 &setsparse, 1, NULL, NULL);
d43cc793
SF
795 if (rc) {
796 tcon->broken_sparse_sup = true;
797 cifs_dbg(FYI, "set sparse rc = %d\n", rc);
798 return false;
799 }
800
801 if (setsparse)
802 cifsi->cifsAttrs |= FILE_ATTRIBUTE_SPARSE_FILE;
803 else
804 cifsi->cifsAttrs &= (~FILE_ATTRIBUTE_SPARSE_FILE);
805
806 return true;
807}
808
c839ff24
PS
809static int
810smb2_set_file_size(const unsigned int xid, struct cifs_tcon *tcon,
811 struct cifsFileInfo *cfile, __u64 size, bool set_alloc)
812{
813 __le64 eof = cpu_to_le64(size);
3d1a3745
SF
814 struct inode *inode;
815
816 /*
817 * If extending file more than one page make sparse. Many Linux fs
818 * make files sparse by default when extending via ftruncate
819 */
2b0143b5 820 inode = d_inode(cfile->dentry);
3d1a3745
SF
821
822 if (!set_alloc && (size > inode->i_size + 8192)) {
3d1a3745 823 __u8 set_sparse = 1;
d43cc793
SF
824
825 /* whether set sparse succeeds or not, extend the file */
826 smb2_set_sparse(xid, tcon, cfile, inode, set_sparse);
3d1a3745
SF
827 }
828
c839ff24 829 return SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
f29ebb47 830 cfile->fid.volatile_fid, cfile->pid, &eof, false);
c839ff24
PS
831}
832
02b16665
SF
833static int
834smb2_duplicate_extents(const unsigned int xid,
835 struct cifsFileInfo *srcfile,
836 struct cifsFileInfo *trgtfile, u64 src_off,
837 u64 len, u64 dest_off)
838{
839 int rc;
840 unsigned int ret_data_len;
02b16665
SF
841 struct duplicate_extents_to_file dup_ext_buf;
842 struct cifs_tcon *tcon = tlink_tcon(trgtfile->tlink);
843
844 /* server fileays advertise duplicate extent support with this flag */
845 if ((le32_to_cpu(tcon->fsAttrInfo.Attributes) &
846 FILE_SUPPORTS_BLOCK_REFCOUNTING) == 0)
847 return -EOPNOTSUPP;
848
849 dup_ext_buf.VolatileFileHandle = srcfile->fid.volatile_fid;
850 dup_ext_buf.PersistentFileHandle = srcfile->fid.persistent_fid;
851 dup_ext_buf.SourceFileOffset = cpu_to_le64(src_off);
852 dup_ext_buf.TargetFileOffset = cpu_to_le64(dest_off);
853 dup_ext_buf.ByteCount = cpu_to_le64(len);
854 cifs_dbg(FYI, "duplicate extents: src off %lld dst off %lld len %lld",
855 src_off, dest_off, len);
856
857 rc = smb2_set_file_size(xid, tcon, trgtfile, dest_off + len, false);
858 if (rc)
859 goto duplicate_extents_out;
860
861 rc = SMB2_ioctl(xid, tcon, trgtfile->fid.persistent_fid,
862 trgtfile->fid.volatile_fid,
863 FSCTL_DUPLICATE_EXTENTS_TO_FILE,
51146625
AA
864 true /* is_fsctl */, false /* use_ipc */,
865 (char *)&dup_ext_buf,
02b16665 866 sizeof(struct duplicate_extents_to_file),
24df1483 867 NULL,
02b16665
SF
868 &ret_data_len);
869
870 if (ret_data_len > 0)
871 cifs_dbg(FYI, "non-zero response length in duplicate extents");
872
873duplicate_extents_out:
874 return rc;
875}
02b16665 876
64a5cfa6
SF
877static int
878smb2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
879 struct cifsFileInfo *cfile)
880{
881 return SMB2_set_compression(xid, tcon, cfile->fid.persistent_fid,
882 cfile->fid.volatile_fid);
883}
884
b3152e2c
SF
885static int
886smb3_set_integrity(const unsigned int xid, struct cifs_tcon *tcon,
887 struct cifsFileInfo *cfile)
888{
889 struct fsctl_set_integrity_information_req integr_info;
b3152e2c
SF
890 unsigned int ret_data_len;
891
892 integr_info.ChecksumAlgorithm = cpu_to_le16(CHECKSUM_TYPE_UNCHANGED);
893 integr_info.Flags = 0;
894 integr_info.Reserved = 0;
895
896 return SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
897 cfile->fid.volatile_fid,
898 FSCTL_SET_INTEGRITY_INFORMATION,
51146625
AA
899 true /* is_fsctl */, false /* use_ipc */,
900 (char *)&integr_info,
b3152e2c 901 sizeof(struct fsctl_set_integrity_information_req),
24df1483 902 NULL,
b3152e2c
SF
903 &ret_data_len);
904
905}
906
834170c8
SF
907static int
908smb3_enum_snapshots(const unsigned int xid, struct cifs_tcon *tcon,
909 struct cifsFileInfo *cfile, void __user *ioc_buf)
910{
911 char *retbuf = NULL;
912 unsigned int ret_data_len = 0;
913 int rc;
914 struct smb_snapshot_array snapshot_in;
915
916 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
917 cfile->fid.volatile_fid,
918 FSCTL_SRV_ENUMERATE_SNAPSHOTS,
51146625
AA
919 true /* is_fsctl */, false /* use_ipc */,
920 NULL, 0 /* no input data */,
834170c8
SF
921 (char **)&retbuf,
922 &ret_data_len);
923 cifs_dbg(FYI, "enum snaphots ioctl returned %d and ret buflen is %d\n",
924 rc, ret_data_len);
925 if (rc)
926 return rc;
927
928 if (ret_data_len && (ioc_buf != NULL) && (retbuf != NULL)) {
929 /* Fixup buffer */
930 if (copy_from_user(&snapshot_in, ioc_buf,
931 sizeof(struct smb_snapshot_array))) {
932 rc = -EFAULT;
933 kfree(retbuf);
934 return rc;
935 }
936 if (snapshot_in.snapshot_array_size < sizeof(struct smb_snapshot_array)) {
937 rc = -ERANGE;
938 return rc;
939 }
940
941 if (ret_data_len > snapshot_in.snapshot_array_size)
942 ret_data_len = snapshot_in.snapshot_array_size;
943
944 if (copy_to_user(ioc_buf, retbuf, ret_data_len))
945 rc = -EFAULT;
946 }
947
948 kfree(retbuf);
949 return rc;
950}
951
d324f08d
PS
952static int
953smb2_query_dir_first(const unsigned int xid, struct cifs_tcon *tcon,
954 const char *path, struct cifs_sb_info *cifs_sb,
955 struct cifs_fid *fid, __u16 search_flags,
956 struct cifs_search_info *srch_inf)
957{
958 __le16 *utf16_path;
959 int rc;
2e44b288 960 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
064f6047 961 struct cifs_open_parms oparms;
d324f08d
PS
962
963 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
964 if (!utf16_path)
965 return -ENOMEM;
966
064f6047
PS
967 oparms.tcon = tcon;
968 oparms.desired_access = FILE_READ_ATTRIBUTES | FILE_READ_DATA;
969 oparms.disposition = FILE_OPEN;
970 oparms.create_options = 0;
971 oparms.fid = fid;
9cbc0b73 972 oparms.reconnect = false;
064f6047 973
b42bf888 974 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL);
d324f08d
PS
975 kfree(utf16_path);
976 if (rc) {
f96637be 977 cifs_dbg(VFS, "open dir failed\n");
d324f08d
PS
978 return rc;
979 }
980
981 srch_inf->entries_in_buffer = 0;
982 srch_inf->index_of_last_entry = 0;
d324f08d 983
064f6047
PS
984 rc = SMB2_query_directory(xid, tcon, fid->persistent_fid,
985 fid->volatile_fid, 0, srch_inf);
d324f08d 986 if (rc) {
f96637be 987 cifs_dbg(VFS, "query directory failed\n");
064f6047 988 SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
d324f08d
PS
989 }
990 return rc;
991}
992
993static int
994smb2_query_dir_next(const unsigned int xid, struct cifs_tcon *tcon,
995 struct cifs_fid *fid, __u16 search_flags,
996 struct cifs_search_info *srch_inf)
997{
998 return SMB2_query_directory(xid, tcon, fid->persistent_fid,
999 fid->volatile_fid, 0, srch_inf);
1000}
1001
1002static int
1003smb2_close_dir(const unsigned int xid, struct cifs_tcon *tcon,
1004 struct cifs_fid *fid)
1005{
1006 return SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
1007}
1008
2e44b288
PS
1009/*
1010* If we negotiate SMB2 protocol and get STATUS_PENDING - update
1011* the number of credits and return true. Otherwise - return false.
1012*/
1013static bool
1014smb2_is_status_pending(char *buf, struct TCP_Server_Info *server, int length)
1015{
31473fc4 1016 struct smb2_sync_hdr *shdr = get_sync_hdr(buf);
2e44b288 1017
31473fc4 1018 if (shdr->Status != STATUS_PENDING)
2e44b288
PS
1019 return false;
1020
1021 if (!length) {
1022 spin_lock(&server->req_lock);
31473fc4 1023 server->credits += le16_to_cpu(shdr->CreditRequest);
2e44b288
PS
1024 spin_unlock(&server->req_lock);
1025 wake_up(&server->request_q);
1026 }
1027
1028 return true;
1029}
1030
983c88a4
PS
1031static int
1032smb2_oplock_response(struct cifs_tcon *tcon, struct cifs_fid *fid,
1033 struct cifsInodeInfo *cinode)
1034{
0822f514
PS
1035 if (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_LEASING)
1036 return SMB2_lease_break(0, tcon, cinode->lease_key,
1037 smb2_get_lease_state(cinode));
1038
983c88a4
PS
1039 return SMB2_oplock_break(0, tcon, fid->persistent_fid,
1040 fid->volatile_fid,
18cceb6a 1041 CIFS_CACHE_READ(cinode) ? 1 : 0);
983c88a4
PS
1042}
1043
6fc05c25
PS
1044static int
1045smb2_queryfs(const unsigned int xid, struct cifs_tcon *tcon,
1046 struct kstatfs *buf)
1047{
1048 int rc;
6fc05c25
PS
1049 __le16 srch_path = 0; /* Null - open root of share */
1050 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
064f6047
PS
1051 struct cifs_open_parms oparms;
1052 struct cifs_fid fid;
1053
1054 oparms.tcon = tcon;
1055 oparms.desired_access = FILE_READ_ATTRIBUTES;
1056 oparms.disposition = FILE_OPEN;
1057 oparms.create_options = 0;
1058 oparms.fid = &fid;
9cbc0b73 1059 oparms.reconnect = false;
6fc05c25 1060
b42bf888 1061 rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL);
6fc05c25
PS
1062 if (rc)
1063 return rc;
1064 buf->f_type = SMB2_MAGIC_NUMBER;
064f6047
PS
1065 rc = SMB2_QFS_info(xid, tcon, fid.persistent_fid, fid.volatile_fid,
1066 buf);
1067 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
6fc05c25
PS
1068 return rc;
1069}
1070
027e8eec
PS
1071static bool
1072smb2_compare_fids(struct cifsFileInfo *ob1, struct cifsFileInfo *ob2)
1073{
1074 return ob1->fid.persistent_fid == ob2->fid.persistent_fid &&
1075 ob1->fid.volatile_fid == ob2->fid.volatile_fid;
1076}
1077
f7ba7fe6
PS
1078static int
1079smb2_mand_lock(const unsigned int xid, struct cifsFileInfo *cfile, __u64 offset,
1080 __u64 length, __u32 type, int lock, int unlock, bool wait)
1081{
1082 if (unlock && !lock)
1083 type = SMB2_LOCKFLAG_UNLOCK;
1084 return SMB2_lock(xid, tlink_tcon(cfile->tlink),
1085 cfile->fid.persistent_fid, cfile->fid.volatile_fid,
1086 current->tgid, length, offset, type, wait);
1087}
1088
b8c32dbb
PS
1089static void
1090smb2_get_lease_key(struct inode *inode, struct cifs_fid *fid)
1091{
1092 memcpy(fid->lease_key, CIFS_I(inode)->lease_key, SMB2_LEASE_KEY_SIZE);
1093}
1094
1095static void
1096smb2_set_lease_key(struct inode *inode, struct cifs_fid *fid)
1097{
1098 memcpy(CIFS_I(inode)->lease_key, fid->lease_key, SMB2_LEASE_KEY_SIZE);
1099}
1100
1101static void
1102smb2_new_lease_key(struct cifs_fid *fid)
1103{
fa70b87c 1104 generate_random_uuid(fid->lease_key);
b8c32dbb
PS
1105}
1106
7893242e
PS
1107#define SMB2_SYMLINK_STRUCT_SIZE \
1108 (sizeof(struct smb2_err_rsp) - 1 + sizeof(struct smb2_symlink_err_rsp))
1109
b42bf888
PS
1110static int
1111smb2_query_symlink(const unsigned int xid, struct cifs_tcon *tcon,
1112 const char *full_path, char **target_path,
1113 struct cifs_sb_info *cifs_sb)
1114{
1115 int rc;
1116 __le16 *utf16_path;
1117 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
1118 struct cifs_open_parms oparms;
1119 struct cifs_fid fid;
1120 struct smb2_err_rsp *err_buf = NULL;
1121 struct smb2_symlink_err_rsp *symlink;
7893242e
PS
1122 unsigned int sub_len;
1123 unsigned int sub_offset;
1124 unsigned int print_len;
1125 unsigned int print_offset;
b42bf888
PS
1126
1127 cifs_dbg(FYI, "%s: path: %s\n", __func__, full_path);
1128
1129 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
1130 if (!utf16_path)
1131 return -ENOMEM;
1132
1133 oparms.tcon = tcon;
1134 oparms.desired_access = FILE_READ_ATTRIBUTES;
1135 oparms.disposition = FILE_OPEN;
1136 oparms.create_options = 0;
1137 oparms.fid = &fid;
1138 oparms.reconnect = false;
1139
1140 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, &err_buf);
1141
1142 if (!rc || !err_buf) {
1143 kfree(utf16_path);
1144 return -ENOENT;
1145 }
7893242e
PS
1146
1147 if (le32_to_cpu(err_buf->ByteCount) < sizeof(struct smb2_symlink_err_rsp) ||
1148 get_rfc1002_length(err_buf) + 4 < SMB2_SYMLINK_STRUCT_SIZE) {
1149 kfree(utf16_path);
1150 return -ENOENT;
1151 }
1152
b42bf888
PS
1153 /* open must fail on symlink - reset rc */
1154 rc = 0;
1155 symlink = (struct smb2_symlink_err_rsp *)err_buf->ErrorData;
1156 sub_len = le16_to_cpu(symlink->SubstituteNameLength);
1157 sub_offset = le16_to_cpu(symlink->SubstituteNameOffset);
7893242e
PS
1158 print_len = le16_to_cpu(symlink->PrintNameLength);
1159 print_offset = le16_to_cpu(symlink->PrintNameOffset);
1160
1161 if (get_rfc1002_length(err_buf) + 4 <
1162 SMB2_SYMLINK_STRUCT_SIZE + sub_offset + sub_len) {
1163 kfree(utf16_path);
1164 return -ENOENT;
1165 }
1166
1167 if (get_rfc1002_length(err_buf) + 4 <
1168 SMB2_SYMLINK_STRUCT_SIZE + print_offset + print_len) {
1169 kfree(utf16_path);
1170 return -ENOENT;
1171 }
1172
b42bf888
PS
1173 *target_path = cifs_strndup_from_utf16(
1174 (char *)symlink->PathBuffer + sub_offset,
1175 sub_len, true, cifs_sb->local_nls);
1176 if (!(*target_path)) {
1177 kfree(utf16_path);
1178 return -ENOMEM;
1179 }
1180 convert_delimiter(*target_path, '/');
1181 cifs_dbg(FYI, "%s: target path: %s\n", __func__, *target_path);
1182 kfree(utf16_path);
1183 return rc;
1184}
1185
30175628
SF
1186static long smb3_zero_range(struct file *file, struct cifs_tcon *tcon,
1187 loff_t offset, loff_t len, bool keep_size)
1188{
1189 struct inode *inode;
1190 struct cifsInodeInfo *cifsi;
1191 struct cifsFileInfo *cfile = file->private_data;
1192 struct file_zero_data_information fsctl_buf;
1193 long rc;
1194 unsigned int xid;
1195
1196 xid = get_xid();
1197
2b0143b5 1198 inode = d_inode(cfile->dentry);
30175628
SF
1199 cifsi = CIFS_I(inode);
1200
1201 /* if file not oplocked can't be sure whether asking to extend size */
1202 if (!CIFS_CACHE_READ(cifsi))
1203 if (keep_size == false)
1204 return -EOPNOTSUPP;
1205
2bb93d24 1206 /*
30175628
SF
1207 * Must check if file sparse since fallocate -z (zero range) assumes
1208 * non-sparse allocation
1209 */
1210 if (!(cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE))
1211 return -EOPNOTSUPP;
1212
1213 /*
1214 * need to make sure we are not asked to extend the file since the SMB3
1215 * fsctl does not change the file size. In the future we could change
1216 * this to zero the first part of the range then set the file size
1217 * which for a non sparse file would zero the newly extended range
1218 */
1219 if (keep_size == false)
1220 if (i_size_read(inode) < offset + len)
1221 return -EOPNOTSUPP;
1222
1223 cifs_dbg(FYI, "offset %lld len %lld", offset, len);
1224
1225 fsctl_buf.FileOffset = cpu_to_le64(offset);
1226 fsctl_buf.BeyondFinalZero = cpu_to_le64(offset + len);
1227
1228 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
1229 cfile->fid.volatile_fid, FSCTL_SET_ZERO_DATA,
51146625
AA
1230 true /* is_fctl */, false /* use_ipc */,
1231 (char *)&fsctl_buf,
30175628
SF
1232 sizeof(struct file_zero_data_information), NULL, NULL);
1233 free_xid(xid);
1234 return rc;
1235}
1236
31742c5a
SF
1237static long smb3_punch_hole(struct file *file, struct cifs_tcon *tcon,
1238 loff_t offset, loff_t len)
1239{
1240 struct inode *inode;
1241 struct cifsInodeInfo *cifsi;
1242 struct cifsFileInfo *cfile = file->private_data;
1243 struct file_zero_data_information fsctl_buf;
1244 long rc;
1245 unsigned int xid;
1246 __u8 set_sparse = 1;
1247
1248 xid = get_xid();
1249
2b0143b5 1250 inode = d_inode(cfile->dentry);
31742c5a
SF
1251 cifsi = CIFS_I(inode);
1252
1253 /* Need to make file sparse, if not already, before freeing range. */
1254 /* Consider adding equivalent for compressed since it could also work */
1255 if (!smb2_set_sparse(xid, tcon, cfile, inode, set_sparse))
1256 return -EOPNOTSUPP;
1257
1258 cifs_dbg(FYI, "offset %lld len %lld", offset, len);
1259
1260 fsctl_buf.FileOffset = cpu_to_le64(offset);
1261 fsctl_buf.BeyondFinalZero = cpu_to_le64(offset + len);
1262
1263 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
1264 cfile->fid.volatile_fid, FSCTL_SET_ZERO_DATA,
51146625
AA
1265 true /* is_fctl */, false /* use_ipc */,
1266 (char *)&fsctl_buf,
31742c5a
SF
1267 sizeof(struct file_zero_data_information), NULL, NULL);
1268 free_xid(xid);
1269 return rc;
1270}
1271
9ccf3216
SF
1272static long smb3_simple_falloc(struct file *file, struct cifs_tcon *tcon,
1273 loff_t off, loff_t len, bool keep_size)
1274{
1275 struct inode *inode;
1276 struct cifsInodeInfo *cifsi;
1277 struct cifsFileInfo *cfile = file->private_data;
1278 long rc = -EOPNOTSUPP;
1279 unsigned int xid;
1280
1281 xid = get_xid();
1282
2b0143b5 1283 inode = d_inode(cfile->dentry);
9ccf3216
SF
1284 cifsi = CIFS_I(inode);
1285
1286 /* if file not oplocked can't be sure whether asking to extend size */
1287 if (!CIFS_CACHE_READ(cifsi))
1288 if (keep_size == false)
1289 return -EOPNOTSUPP;
1290
1291 /*
1292 * Files are non-sparse by default so falloc may be a no-op
1293 * Must check if file sparse. If not sparse, and not extending
1294 * then no need to do anything since file already allocated
1295 */
1296 if ((cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) == 0) {
1297 if (keep_size == true)
1298 return 0;
1299 /* check if extending file */
1300 else if (i_size_read(inode) >= off + len)
1301 /* not extending file and already not sparse */
1302 return 0;
1303 /* BB: in future add else clause to extend file */
1304 else
1305 return -EOPNOTSUPP;
1306 }
1307
1308 if ((keep_size == true) || (i_size_read(inode) >= off + len)) {
1309 /*
1310 * Check if falloc starts within first few pages of file
1311 * and ends within a few pages of the end of file to
1312 * ensure that most of file is being forced to be
1313 * fallocated now. If so then setting whole file sparse
1314 * ie potentially making a few extra pages at the beginning
1315 * or end of the file non-sparse via set_sparse is harmless.
1316 */
1317 if ((off > 8192) || (off + len + 8192 < i_size_read(inode)))
1318 return -EOPNOTSUPP;
1319
1320 rc = smb2_set_sparse(xid, tcon, cfile, inode, false);
1321 }
1322 /* BB: else ... in future add code to extend file and set sparse */
1323
1324
1325 free_xid(xid);
1326 return rc;
1327}
1328
1329
31742c5a
SF
1330static long smb3_fallocate(struct file *file, struct cifs_tcon *tcon, int mode,
1331 loff_t off, loff_t len)
1332{
1333 /* KEEP_SIZE already checked for by do_fallocate */
1334 if (mode & FALLOC_FL_PUNCH_HOLE)
1335 return smb3_punch_hole(file, tcon, off, len);
30175628
SF
1336 else if (mode & FALLOC_FL_ZERO_RANGE) {
1337 if (mode & FALLOC_FL_KEEP_SIZE)
1338 return smb3_zero_range(file, tcon, off, len, true);
1339 return smb3_zero_range(file, tcon, off, len, false);
9ccf3216
SF
1340 } else if (mode == FALLOC_FL_KEEP_SIZE)
1341 return smb3_simple_falloc(file, tcon, off, len, true);
1342 else if (mode == 0)
1343 return smb3_simple_falloc(file, tcon, off, len, false);
31742c5a
SF
1344
1345 return -EOPNOTSUPP;
1346}
1347
c11f1df5
SP
1348static void
1349smb2_downgrade_oplock(struct TCP_Server_Info *server,
1350 struct cifsInodeInfo *cinode, bool set_level2)
1351{
1352 if (set_level2)
1353 server->ops->set_oplock_level(cinode, SMB2_OPLOCK_LEVEL_II,
1354 0, NULL);
1355 else
1356 server->ops->set_oplock_level(cinode, 0, 0, NULL);
1357}
1358
53ef1016 1359static void
42873b0a
PS
1360smb2_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
1361 unsigned int epoch, bool *purge_cache)
53ef1016
PS
1362{
1363 oplock &= 0xFF;
1364 if (oplock == SMB2_OPLOCK_LEVEL_NOCHANGE)
1365 return;
1366 if (oplock == SMB2_OPLOCK_LEVEL_BATCH) {
42873b0a 1367 cinode->oplock = CIFS_CACHE_RHW_FLG;
53ef1016
PS
1368 cifs_dbg(FYI, "Batch Oplock granted on inode %p\n",
1369 &cinode->vfs_inode);
1370 } else if (oplock == SMB2_OPLOCK_LEVEL_EXCLUSIVE) {
42873b0a 1371 cinode->oplock = CIFS_CACHE_RW_FLG;
53ef1016
PS
1372 cifs_dbg(FYI, "Exclusive Oplock granted on inode %p\n",
1373 &cinode->vfs_inode);
1374 } else if (oplock == SMB2_OPLOCK_LEVEL_II) {
1375 cinode->oplock = CIFS_CACHE_READ_FLG;
1376 cifs_dbg(FYI, "Level II Oplock granted on inode %p\n",
1377 &cinode->vfs_inode);
1378 } else
1379 cinode->oplock = 0;
1380}
1381
1382static void
42873b0a
PS
1383smb21_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
1384 unsigned int epoch, bool *purge_cache)
53ef1016
PS
1385{
1386 char message[5] = {0};
1387
1388 oplock &= 0xFF;
1389 if (oplock == SMB2_OPLOCK_LEVEL_NOCHANGE)
1390 return;
1391
1392 cinode->oplock = 0;
1393 if (oplock & SMB2_LEASE_READ_CACHING_HE) {
1394 cinode->oplock |= CIFS_CACHE_READ_FLG;
1395 strcat(message, "R");
1396 }
1397 if (oplock & SMB2_LEASE_HANDLE_CACHING_HE) {
1398 cinode->oplock |= CIFS_CACHE_HANDLE_FLG;
1399 strcat(message, "H");
1400 }
1401 if (oplock & SMB2_LEASE_WRITE_CACHING_HE) {
1402 cinode->oplock |= CIFS_CACHE_WRITE_FLG;
1403 strcat(message, "W");
1404 }
1405 if (!cinode->oplock)
1406 strcat(message, "None");
1407 cifs_dbg(FYI, "%s Lease granted on inode %p\n", message,
1408 &cinode->vfs_inode);
1409}
1410
42873b0a
PS
1411static void
1412smb3_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
1413 unsigned int epoch, bool *purge_cache)
1414{
1415 unsigned int old_oplock = cinode->oplock;
1416
1417 smb21_set_oplock_level(cinode, oplock, epoch, purge_cache);
1418
1419 if (purge_cache) {
1420 *purge_cache = false;
1421 if (old_oplock == CIFS_CACHE_READ_FLG) {
1422 if (cinode->oplock == CIFS_CACHE_READ_FLG &&
1423 (epoch - cinode->epoch > 0))
1424 *purge_cache = true;
1425 else if (cinode->oplock == CIFS_CACHE_RH_FLG &&
1426 (epoch - cinode->epoch > 1))
1427 *purge_cache = true;
1428 else if (cinode->oplock == CIFS_CACHE_RHW_FLG &&
1429 (epoch - cinode->epoch > 1))
1430 *purge_cache = true;
1431 else if (cinode->oplock == 0 &&
1432 (epoch - cinode->epoch > 0))
1433 *purge_cache = true;
1434 } else if (old_oplock == CIFS_CACHE_RH_FLG) {
1435 if (cinode->oplock == CIFS_CACHE_RH_FLG &&
1436 (epoch - cinode->epoch > 0))
1437 *purge_cache = true;
1438 else if (cinode->oplock == CIFS_CACHE_RHW_FLG &&
1439 (epoch - cinode->epoch > 1))
1440 *purge_cache = true;
1441 }
1442 cinode->epoch = epoch;
1443 }
1444}
1445
53ef1016
PS
1446static bool
1447smb2_is_read_op(__u32 oplock)
1448{
1449 return oplock == SMB2_OPLOCK_LEVEL_II;
1450}
1451
1452static bool
1453smb21_is_read_op(__u32 oplock)
1454{
1455 return (oplock & SMB2_LEASE_READ_CACHING_HE) &&
1456 !(oplock & SMB2_LEASE_WRITE_CACHING_HE);
1457}
1458
f047390a
PS
1459static __le32
1460map_oplock_to_lease(u8 oplock)
1461{
1462 if (oplock == SMB2_OPLOCK_LEVEL_EXCLUSIVE)
1463 return SMB2_LEASE_WRITE_CACHING | SMB2_LEASE_READ_CACHING;
1464 else if (oplock == SMB2_OPLOCK_LEVEL_II)
1465 return SMB2_LEASE_READ_CACHING;
1466 else if (oplock == SMB2_OPLOCK_LEVEL_BATCH)
1467 return SMB2_LEASE_HANDLE_CACHING | SMB2_LEASE_READ_CACHING |
1468 SMB2_LEASE_WRITE_CACHING;
1469 return 0;
1470}
1471
a41a28bd
PS
1472static char *
1473smb2_create_lease_buf(u8 *lease_key, u8 oplock)
1474{
1475 struct create_lease *buf;
1476
1477 buf = kzalloc(sizeof(struct create_lease), GFP_KERNEL);
1478 if (!buf)
1479 return NULL;
1480
1481 buf->lcontext.LeaseKeyLow = cpu_to_le64(*((u64 *)lease_key));
1482 buf->lcontext.LeaseKeyHigh = cpu_to_le64(*((u64 *)(lease_key + 8)));
f047390a 1483 buf->lcontext.LeaseState = map_oplock_to_lease(oplock);
a41a28bd
PS
1484
1485 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1486 (struct create_lease, lcontext));
1487 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context));
1488 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1489 (struct create_lease, Name));
1490 buf->ccontext.NameLength = cpu_to_le16(4);
12197a7f 1491 /* SMB2_CREATE_REQUEST_LEASE is "RqLs" */
a41a28bd
PS
1492 buf->Name[0] = 'R';
1493 buf->Name[1] = 'q';
1494 buf->Name[2] = 'L';
1495 buf->Name[3] = 's';
1496 return (char *)buf;
1497}
1498
f047390a
PS
1499static char *
1500smb3_create_lease_buf(u8 *lease_key, u8 oplock)
1501{
1502 struct create_lease_v2 *buf;
1503
1504 buf = kzalloc(sizeof(struct create_lease_v2), GFP_KERNEL);
1505 if (!buf)
1506 return NULL;
1507
1508 buf->lcontext.LeaseKeyLow = cpu_to_le64(*((u64 *)lease_key));
1509 buf->lcontext.LeaseKeyHigh = cpu_to_le64(*((u64 *)(lease_key + 8)));
1510 buf->lcontext.LeaseState = map_oplock_to_lease(oplock);
1511
1512 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1513 (struct create_lease_v2, lcontext));
1514 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context_v2));
1515 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1516 (struct create_lease_v2, Name));
1517 buf->ccontext.NameLength = cpu_to_le16(4);
12197a7f 1518 /* SMB2_CREATE_REQUEST_LEASE is "RqLs" */
f047390a
PS
1519 buf->Name[0] = 'R';
1520 buf->Name[1] = 'q';
1521 buf->Name[2] = 'L';
1522 buf->Name[3] = 's';
1523 return (char *)buf;
1524}
1525
b5c7cde3 1526static __u8
42873b0a 1527smb2_parse_lease_buf(void *buf, unsigned int *epoch)
b5c7cde3
PS
1528{
1529 struct create_lease *lc = (struct create_lease *)buf;
1530
42873b0a 1531 *epoch = 0; /* not used */
b5c7cde3
PS
1532 if (lc->lcontext.LeaseFlags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS)
1533 return SMB2_OPLOCK_LEVEL_NOCHANGE;
1534 return le32_to_cpu(lc->lcontext.LeaseState);
1535}
1536
f047390a 1537static __u8
42873b0a 1538smb3_parse_lease_buf(void *buf, unsigned int *epoch)
f047390a
PS
1539{
1540 struct create_lease_v2 *lc = (struct create_lease_v2 *)buf;
1541
42873b0a 1542 *epoch = le16_to_cpu(lc->lcontext.Epoch);
f047390a
PS
1543 if (lc->lcontext.LeaseFlags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS)
1544 return SMB2_OPLOCK_LEVEL_NOCHANGE;
1545 return le32_to_cpu(lc->lcontext.LeaseState);
1546}
1547
7f6c5008
PS
1548static unsigned int
1549smb2_wp_retry_size(struct inode *inode)
1550{
1551 return min_t(unsigned int, CIFS_SB(inode->i_sb)->wsize,
1552 SMB2_MAX_BUFFER_SIZE);
1553}
1554
52755808
PS
1555static bool
1556smb2_dir_needs_close(struct cifsFileInfo *cfile)
1557{
1558 return !cfile->invalidHandle;
1559}
1560
026e93dc
PS
1561static void
1562fill_transform_hdr(struct smb2_transform_hdr *tr_hdr, struct smb_rqst *old_rq)
1563{
1564 struct smb2_sync_hdr *shdr =
1565 (struct smb2_sync_hdr *)old_rq->rq_iov[1].iov_base;
1566 unsigned int orig_len = get_rfc1002_length(old_rq->rq_iov[0].iov_base);
1567
1568 memset(tr_hdr, 0, sizeof(struct smb2_transform_hdr));
1569 tr_hdr->ProtocolId = SMB2_TRANSFORM_PROTO_NUM;
1570 tr_hdr->OriginalMessageSize = cpu_to_le32(orig_len);
1571 tr_hdr->Flags = cpu_to_le16(0x01);
1572 get_random_bytes(&tr_hdr->Nonce, SMB3_AES128CMM_NONCE);
1573 memcpy(&tr_hdr->SessionId, &shdr->SessionId, 8);
1574 inc_rfc1001_len(tr_hdr, sizeof(struct smb2_transform_hdr) - 4);
1575 inc_rfc1001_len(tr_hdr, orig_len);
1576}
1577
1578static struct scatterlist *
1579init_sg(struct smb_rqst *rqst, u8 *sign)
1580{
1581 unsigned int sg_len = rqst->rq_nvec + rqst->rq_npages + 1;
1582 unsigned int assoc_data_len = sizeof(struct smb2_transform_hdr) - 24;
1583 struct scatterlist *sg;
1584 unsigned int i;
1585 unsigned int j;
1586
1587 sg = kmalloc_array(sg_len, sizeof(struct scatterlist), GFP_KERNEL);
1588 if (!sg)
1589 return NULL;
1590
1591 sg_init_table(sg, sg_len);
1592 sg_set_buf(&sg[0], rqst->rq_iov[0].iov_base + 24, assoc_data_len);
1593 for (i = 1; i < rqst->rq_nvec; i++)
1594 sg_set_buf(&sg[i], rqst->rq_iov[i].iov_base,
1595 rqst->rq_iov[i].iov_len);
1596 for (j = 0; i < sg_len - 1; i++, j++) {
1597 unsigned int len = (j < rqst->rq_npages - 1) ? rqst->rq_pagesz
1598 : rqst->rq_tailsz;
1599 sg_set_page(&sg[i], rqst->rq_pages[j], len, 0);
1600 }
1601 sg_set_buf(&sg[sg_len - 1], sign, SMB2_SIGNATURE_SIZE);
1602 return sg;
1603}
1604
1605struct cifs_crypt_result {
1606 int err;
1607 struct completion completion;
1608};
1609
1610static void cifs_crypt_complete(struct crypto_async_request *req, int err)
1611{
1612 struct cifs_crypt_result *res = req->data;
1613
1614 if (err == -EINPROGRESS)
1615 return;
1616
1617 res->err = err;
1618 complete(&res->completion);
1619}
1620
61cfac6f
PS
1621static int
1622smb2_get_enc_key(struct TCP_Server_Info *server, __u64 ses_id, int enc, u8 *key)
1623{
1624 struct cifs_ses *ses;
1625 u8 *ses_enc_key;
1626
1627 spin_lock(&cifs_tcp_ses_lock);
1628 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
1629 if (ses->Suid != ses_id)
1630 continue;
1631 ses_enc_key = enc ? ses->smb3encryptionkey :
1632 ses->smb3decryptionkey;
1633 memcpy(key, ses_enc_key, SMB3_SIGN_KEY_SIZE);
1634 spin_unlock(&cifs_tcp_ses_lock);
1635 return 0;
1636 }
1637 spin_unlock(&cifs_tcp_ses_lock);
1638
1639 return 1;
1640}
026e93dc
PS
1641/*
1642 * Encrypt or decrypt @rqst message. @rqst has the following format:
1643 * iov[0] - transform header (associate data),
1644 * iov[1-N] and pages - data to encrypt.
1645 * On success return encrypted data in iov[1-N] and pages, leave iov[0]
1646 * untouched.
1647 */
1648static int
1649crypt_message(struct TCP_Server_Info *server, struct smb_rqst *rqst, int enc)
1650{
1651 struct smb2_transform_hdr *tr_hdr =
1652 (struct smb2_transform_hdr *)rqst->rq_iov[0].iov_base;
1653 unsigned int assoc_data_len = sizeof(struct smb2_transform_hdr) - 24;
026e93dc
PS
1654 int rc = 0;
1655 struct scatterlist *sg;
1656 u8 sign[SMB2_SIGNATURE_SIZE] = {};
61cfac6f 1657 u8 key[SMB3_SIGN_KEY_SIZE];
026e93dc
PS
1658 struct aead_request *req;
1659 char *iv;
1660 unsigned int iv_len;
1661 struct cifs_crypt_result result = {0, };
1662 struct crypto_aead *tfm;
1663 unsigned int crypt_len = le32_to_cpu(tr_hdr->OriginalMessageSize);
1664
1665 init_completion(&result.completion);
1666
61cfac6f
PS
1667 rc = smb2_get_enc_key(server, tr_hdr->SessionId, enc, key);
1668 if (rc) {
1669 cifs_dbg(VFS, "%s: Could not get %scryption key\n", __func__,
1670 enc ? "en" : "de");
026e93dc
PS
1671 return 0;
1672 }
1673
1674 rc = smb3_crypto_aead_allocate(server);
1675 if (rc) {
1676 cifs_dbg(VFS, "%s: crypto alloc failed\n", __func__);
1677 return rc;
1678 }
1679
1680 tfm = enc ? server->secmech.ccmaesencrypt :
1681 server->secmech.ccmaesdecrypt;
61cfac6f 1682 rc = crypto_aead_setkey(tfm, key, SMB3_SIGN_KEY_SIZE);
026e93dc
PS
1683 if (rc) {
1684 cifs_dbg(VFS, "%s: Failed to set aead key %d\n", __func__, rc);
1685 return rc;
1686 }
1687
1688 rc = crypto_aead_setauthsize(tfm, SMB2_SIGNATURE_SIZE);
1689 if (rc) {
1690 cifs_dbg(VFS, "%s: Failed to set authsize %d\n", __func__, rc);
1691 return rc;
1692 }
1693
1694 req = aead_request_alloc(tfm, GFP_KERNEL);
1695 if (!req) {
1696 cifs_dbg(VFS, "%s: Failed to alloc aead request", __func__);
1697 return -ENOMEM;
1698 }
1699
1700 if (!enc) {
1701 memcpy(sign, &tr_hdr->Signature, SMB2_SIGNATURE_SIZE);
1702 crypt_len += SMB2_SIGNATURE_SIZE;
1703 }
1704
1705 sg = init_sg(rqst, sign);
1706 if (!sg) {
1707 cifs_dbg(VFS, "%s: Failed to init sg %d", __func__, rc);
1708 goto free_req;
1709 }
1710
1711 iv_len = crypto_aead_ivsize(tfm);
1712 iv = kzalloc(iv_len, GFP_KERNEL);
1713 if (!iv) {
1714 cifs_dbg(VFS, "%s: Failed to alloc IV", __func__);
1715 goto free_sg;
1716 }
1717 iv[0] = 3;
1718 memcpy(iv + 1, (char *)tr_hdr->Nonce, SMB3_AES128CMM_NONCE);
1719
1720 aead_request_set_crypt(req, sg, sg, crypt_len, iv);
1721 aead_request_set_ad(req, assoc_data_len);
1722
1723 aead_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
1724 cifs_crypt_complete, &result);
1725
1726 rc = enc ? crypto_aead_encrypt(req) : crypto_aead_decrypt(req);
1727
1728 if (rc == -EINPROGRESS || rc == -EBUSY) {
1729 wait_for_completion(&result.completion);
1730 rc = result.err;
1731 }
1732
1733 if (!rc && enc)
1734 memcpy(&tr_hdr->Signature, sign, SMB2_SIGNATURE_SIZE);
1735
1736 kfree(iv);
1737free_sg:
1738 kfree(sg);
1739free_req:
1740 kfree(req);
1741 return rc;
1742}
1743
1744static int
1745smb3_init_transform_rq(struct TCP_Server_Info *server, struct smb_rqst *new_rq,
1746 struct smb_rqst *old_rq)
1747{
1748 struct kvec *iov;
1749 struct page **pages;
1750 struct smb2_transform_hdr *tr_hdr;
1751 unsigned int npages = old_rq->rq_npages;
1752 int i;
1753 int rc = -ENOMEM;
1754
1755 pages = kmalloc_array(npages, sizeof(struct page *), GFP_KERNEL);
1756 if (!pages)
1757 return rc;
1758
1759 new_rq->rq_pages = pages;
1760 new_rq->rq_npages = old_rq->rq_npages;
1761 new_rq->rq_pagesz = old_rq->rq_pagesz;
1762 new_rq->rq_tailsz = old_rq->rq_tailsz;
1763
1764 for (i = 0; i < npages; i++) {
1765 pages[i] = alloc_page(GFP_KERNEL|__GFP_HIGHMEM);
1766 if (!pages[i])
1767 goto err_free_pages;
1768 }
1769
1770 iov = kmalloc_array(old_rq->rq_nvec, sizeof(struct kvec), GFP_KERNEL);
1771 if (!iov)
1772 goto err_free_pages;
1773
1774 /* copy all iovs from the old except the 1st one (rfc1002 length) */
1775 memcpy(&iov[1], &old_rq->rq_iov[1],
1776 sizeof(struct kvec) * (old_rq->rq_nvec - 1));
1777 new_rq->rq_iov = iov;
1778 new_rq->rq_nvec = old_rq->rq_nvec;
1779
1780 tr_hdr = kmalloc(sizeof(struct smb2_transform_hdr), GFP_KERNEL);
1781 if (!tr_hdr)
1782 goto err_free_iov;
1783
1784 /* fill the 1st iov with a transform header */
1785 fill_transform_hdr(tr_hdr, old_rq);
1786 new_rq->rq_iov[0].iov_base = tr_hdr;
1787 new_rq->rq_iov[0].iov_len = sizeof(struct smb2_transform_hdr);
1788
1789 /* copy pages form the old */
1790 for (i = 0; i < npages; i++) {
1791 char *dst = kmap(new_rq->rq_pages[i]);
1792 char *src = kmap(old_rq->rq_pages[i]);
1793 unsigned int len = (i < npages - 1) ? new_rq->rq_pagesz :
1794 new_rq->rq_tailsz;
1795 memcpy(dst, src, len);
1796 kunmap(new_rq->rq_pages[i]);
1797 kunmap(old_rq->rq_pages[i]);
1798 }
1799
1800 rc = crypt_message(server, new_rq, 1);
1801 cifs_dbg(FYI, "encrypt message returned %d", rc);
1802 if (rc)
1803 goto err_free_tr_hdr;
1804
1805 return rc;
1806
1807err_free_tr_hdr:
1808 kfree(tr_hdr);
1809err_free_iov:
1810 kfree(iov);
1811err_free_pages:
1812 for (i = i - 1; i >= 0; i--)
1813 put_page(pages[i]);
1814 kfree(pages);
1815 return rc;
1816}
1817
1818static void
1819smb3_free_transform_rq(struct smb_rqst *rqst)
1820{
1821 int i = rqst->rq_npages - 1;
1822
1823 for (; i >= 0; i--)
1824 put_page(rqst->rq_pages[i]);
1825 kfree(rqst->rq_pages);
1826 /* free transform header */
1827 kfree(rqst->rq_iov[0].iov_base);
1828 kfree(rqst->rq_iov);
1829}
1830
4326ed2f
PS
1831static int
1832smb3_is_transform_hdr(void *buf)
1833{
1834 struct smb2_transform_hdr *trhdr = buf;
1835
1836 return trhdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM;
1837}
1838
1839static int
1840decrypt_raw_data(struct TCP_Server_Info *server, char *buf,
1841 unsigned int buf_data_size, struct page **pages,
1842 unsigned int npages, unsigned int page_data_size)
1843{
1844 struct kvec iov[2];
1845 struct smb_rqst rqst = {NULL};
1846 struct smb2_hdr *hdr;
1847 int rc;
1848
1849 iov[0].iov_base = buf;
1850 iov[0].iov_len = sizeof(struct smb2_transform_hdr);
1851 iov[1].iov_base = buf + sizeof(struct smb2_transform_hdr);
1852 iov[1].iov_len = buf_data_size;
1853
1854 rqst.rq_iov = iov;
1855 rqst.rq_nvec = 2;
1856 rqst.rq_pages = pages;
1857 rqst.rq_npages = npages;
1858 rqst.rq_pagesz = PAGE_SIZE;
1859 rqst.rq_tailsz = (page_data_size % PAGE_SIZE) ? : PAGE_SIZE;
1860
1861 rc = crypt_message(server, &rqst, 0);
1862 cifs_dbg(FYI, "decrypt message returned %d\n", rc);
1863
1864 if (rc)
1865 return rc;
1866
1867 memmove(buf + 4, iov[1].iov_base, buf_data_size);
1868 hdr = (struct smb2_hdr *)buf;
1869 hdr->smb2_buf_length = cpu_to_be32(buf_data_size + page_data_size);
1870 server->total_read = buf_data_size + page_data_size + 4;
1871
1872 return rc;
1873}
1874
c42a6abe
PS
1875static int
1876read_data_into_pages(struct TCP_Server_Info *server, struct page **pages,
1877 unsigned int npages, unsigned int len)
1878{
1879 int i;
1880 int length;
1881
1882 for (i = 0; i < npages; i++) {
1883 struct page *page = pages[i];
1884 size_t n;
1885
1886 n = len;
1887 if (len >= PAGE_SIZE) {
1888 /* enough data to fill the page */
1889 n = PAGE_SIZE;
1890 len -= n;
1891 } else {
1892 zero_user(page, len, PAGE_SIZE - len);
1893 len = 0;
1894 }
1895 length = cifs_read_page_from_socket(server, page, n);
1896 if (length < 0)
1897 return length;
1898 server->total_read += length;
1899 }
1900
1901 return 0;
1902}
1903
1904static int
1905init_read_bvec(struct page **pages, unsigned int npages, unsigned int data_size,
1906 unsigned int cur_off, struct bio_vec **page_vec)
1907{
1908 struct bio_vec *bvec;
1909 int i;
1910
1911 bvec = kcalloc(npages, sizeof(struct bio_vec), GFP_KERNEL);
1912 if (!bvec)
1913 return -ENOMEM;
1914
1915 for (i = 0; i < npages; i++) {
1916 bvec[i].bv_page = pages[i];
1917 bvec[i].bv_offset = (i == 0) ? cur_off : 0;
1918 bvec[i].bv_len = min_t(unsigned int, PAGE_SIZE, data_size);
1919 data_size -= bvec[i].bv_len;
1920 }
1921
1922 if (data_size != 0) {
1923 cifs_dbg(VFS, "%s: something went wrong\n", __func__);
1924 kfree(bvec);
1925 return -EIO;
1926 }
1927
1928 *page_vec = bvec;
1929 return 0;
1930}
1931
4326ed2f
PS
1932static int
1933handle_read_data(struct TCP_Server_Info *server, struct mid_q_entry *mid,
1934 char *buf, unsigned int buf_len, struct page **pages,
1935 unsigned int npages, unsigned int page_data_size)
1936{
1937 unsigned int data_offset;
1938 unsigned int data_len;
c42a6abe
PS
1939 unsigned int cur_off;
1940 unsigned int cur_page_idx;
1941 unsigned int pad_len;
4326ed2f
PS
1942 struct cifs_readdata *rdata = mid->callback_data;
1943 struct smb2_sync_hdr *shdr = get_sync_hdr(buf);
1944 struct bio_vec *bvec = NULL;
1945 struct iov_iter iter;
1946 struct kvec iov;
1947 int length;
1948
1949 if (shdr->Command != SMB2_READ) {
1950 cifs_dbg(VFS, "only big read responses are supported\n");
1951 return -ENOTSUPP;
1952 }
1953
1954 if (server->ops->is_status_pending &&
1955 server->ops->is_status_pending(buf, server, 0))
1956 return -1;
1957
1958 rdata->result = server->ops->map_error(buf, false);
1959 if (rdata->result != 0) {
1960 cifs_dbg(FYI, "%s: server returned error %d\n",
1961 __func__, rdata->result);
1962 dequeue_mid(mid, rdata->result);
1963 return 0;
1964 }
1965
1966 data_offset = server->ops->read_data_offset(buf) + 4;
1967 data_len = server->ops->read_data_length(buf);
1968
1969 if (data_offset < server->vals->read_rsp_size) {
1970 /*
1971 * win2k8 sometimes sends an offset of 0 when the read
1972 * is beyond the EOF. Treat it as if the data starts just after
1973 * the header.
1974 */
1975 cifs_dbg(FYI, "%s: data offset (%u) inside read response header\n",
1976 __func__, data_offset);
1977 data_offset = server->vals->read_rsp_size;
1978 } else if (data_offset > MAX_CIFS_SMALL_BUFFER_SIZE) {
1979 /* data_offset is beyond the end of smallbuf */
1980 cifs_dbg(FYI, "%s: data offset (%u) beyond end of smallbuf\n",
1981 __func__, data_offset);
1982 rdata->result = -EIO;
1983 dequeue_mid(mid, rdata->result);
1984 return 0;
1985 }
1986
c42a6abe
PS
1987 pad_len = data_offset - server->vals->read_rsp_size;
1988
4326ed2f
PS
1989 if (buf_len <= data_offset) {
1990 /* read response payload is in pages */
c42a6abe
PS
1991 cur_page_idx = pad_len / PAGE_SIZE;
1992 cur_off = pad_len % PAGE_SIZE;
1993
1994 if (cur_page_idx != 0) {
1995 /* data offset is beyond the 1st page of response */
1996 cifs_dbg(FYI, "%s: data offset (%u) beyond 1st page of response\n",
1997 __func__, data_offset);
1998 rdata->result = -EIO;
1999 dequeue_mid(mid, rdata->result);
2000 return 0;
2001 }
2002
2003 if (data_len > page_data_size - pad_len) {
2004 /* data_len is corrupt -- discard frame */
2005 rdata->result = -EIO;
2006 dequeue_mid(mid, rdata->result);
2007 return 0;
2008 }
2009
2010 rdata->result = init_read_bvec(pages, npages, page_data_size,
2011 cur_off, &bvec);
2012 if (rdata->result != 0) {
2013 dequeue_mid(mid, rdata->result);
2014 return 0;
2015 }
2016
2017 iov_iter_bvec(&iter, WRITE | ITER_BVEC, bvec, npages, data_len);
4326ed2f
PS
2018 } else if (buf_len >= data_offset + data_len) {
2019 /* read response payload is in buf */
2020 WARN_ONCE(npages > 0, "read data can be either in buf or in pages");
2021 iov.iov_base = buf + data_offset;
2022 iov.iov_len = data_len;
2023 iov_iter_kvec(&iter, WRITE | ITER_KVEC, &iov, 1, data_len);
2024 } else {
2025 /* read response payload cannot be in both buf and pages */
2026 WARN_ONCE(1, "buf can not contain only a part of read data");
2027 rdata->result = -EIO;
2028 dequeue_mid(mid, rdata->result);
2029 return 0;
2030 }
2031
2032 /* set up first iov for signature check */
2033 rdata->iov[0].iov_base = buf;
2034 rdata->iov[0].iov_len = 4;
2035 rdata->iov[1].iov_base = buf + 4;
2036 rdata->iov[1].iov_len = server->vals->read_rsp_size - 4;
2037 cifs_dbg(FYI, "0: iov_base=%p iov_len=%zu\n",
2038 rdata->iov[0].iov_base, server->vals->read_rsp_size);
2039
2040 length = rdata->copy_into_pages(server, rdata, &iter);
2041
2042 kfree(bvec);
2043
2044 if (length < 0)
2045 return length;
2046
2047 dequeue_mid(mid, false);
2048 return length;
2049}
2050
c42a6abe
PS
2051static int
2052receive_encrypted_read(struct TCP_Server_Info *server, struct mid_q_entry **mid)
2053{
2054 char *buf = server->smallbuf;
2055 struct smb2_transform_hdr *tr_hdr = (struct smb2_transform_hdr *)buf;
2056 unsigned int npages;
2057 struct page **pages;
2058 unsigned int len;
2059 unsigned int buflen = get_rfc1002_length(buf) + 4;
2060 int rc;
2061 int i = 0;
2062
2063 len = min_t(unsigned int, buflen, server->vals->read_rsp_size - 4 +
2064 sizeof(struct smb2_transform_hdr)) - HEADER_SIZE(server) + 1;
2065
2066 rc = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1, len);
2067 if (rc < 0)
2068 return rc;
2069 server->total_read += rc;
2070
2071 len = le32_to_cpu(tr_hdr->OriginalMessageSize) + 4 -
2072 server->vals->read_rsp_size;
2073 npages = DIV_ROUND_UP(len, PAGE_SIZE);
2074
2075 pages = kmalloc_array(npages, sizeof(struct page *), GFP_KERNEL);
2076 if (!pages) {
2077 rc = -ENOMEM;
2078 goto discard_data;
2079 }
2080
2081 for (; i < npages; i++) {
2082 pages[i] = alloc_page(GFP_KERNEL|__GFP_HIGHMEM);
2083 if (!pages[i]) {
2084 rc = -ENOMEM;
2085 goto discard_data;
2086 }
2087 }
2088
2089 /* read read data into pages */
2090 rc = read_data_into_pages(server, pages, npages, len);
2091 if (rc)
2092 goto free_pages;
2093
2094 rc = cifs_discard_remaining_data(server);
2095 if (rc)
2096 goto free_pages;
2097
2098 rc = decrypt_raw_data(server, buf, server->vals->read_rsp_size - 4,
2099 pages, npages, len);
2100 if (rc)
2101 goto free_pages;
2102
2103 *mid = smb2_find_mid(server, buf);
2104 if (*mid == NULL)
2105 cifs_dbg(FYI, "mid not found\n");
2106 else {
2107 cifs_dbg(FYI, "mid found\n");
2108 (*mid)->decrypted = true;
2109 rc = handle_read_data(server, *mid, buf,
2110 server->vals->read_rsp_size,
2111 pages, npages, len);
2112 }
2113
2114free_pages:
2115 for (i = i - 1; i >= 0; i--)
2116 put_page(pages[i]);
2117 kfree(pages);
2118 return rc;
2119discard_data:
2120 cifs_discard_remaining_data(server);
2121 goto free_pages;
2122}
2123
4326ed2f
PS
2124static int
2125receive_encrypted_standard(struct TCP_Server_Info *server,
2126 struct mid_q_entry **mid)
2127{
2128 int length;
2129 char *buf = server->smallbuf;
2130 unsigned int pdu_length = get_rfc1002_length(buf);
2131 unsigned int buf_size;
2132 struct mid_q_entry *mid_entry;
2133
2134 /* switch to large buffer if too big for a small one */
2135 if (pdu_length + 4 > MAX_CIFS_SMALL_BUFFER_SIZE) {
2136 server->large_buf = true;
2137 memcpy(server->bigbuf, buf, server->total_read);
2138 buf = server->bigbuf;
2139 }
2140
2141 /* now read the rest */
2142 length = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1,
2143 pdu_length - HEADER_SIZE(server) + 1 + 4);
2144 if (length < 0)
2145 return length;
2146 server->total_read += length;
2147
2148 buf_size = pdu_length + 4 - sizeof(struct smb2_transform_hdr);
2149 length = decrypt_raw_data(server, buf, buf_size, NULL, 0, 0);
2150 if (length)
2151 return length;
2152
2153 mid_entry = smb2_find_mid(server, buf);
2154 if (mid_entry == NULL)
2155 cifs_dbg(FYI, "mid not found\n");
2156 else {
2157 cifs_dbg(FYI, "mid found\n");
2158 mid_entry->decrypted = true;
2159 }
2160
2161 *mid = mid_entry;
2162
2163 if (mid_entry && mid_entry->handle)
2164 return mid_entry->handle(server, mid_entry);
2165
2166 return cifs_handle_standard(server, mid_entry);
2167}
2168
2169static int
2170smb3_receive_transform(struct TCP_Server_Info *server, struct mid_q_entry **mid)
2171{
2172 char *buf = server->smallbuf;
2173 unsigned int pdu_length = get_rfc1002_length(buf);
2174 struct smb2_transform_hdr *tr_hdr = (struct smb2_transform_hdr *)buf;
2175 unsigned int orig_len = le32_to_cpu(tr_hdr->OriginalMessageSize);
2176
2177 if (pdu_length + 4 < sizeof(struct smb2_transform_hdr) +
2178 sizeof(struct smb2_sync_hdr)) {
2179 cifs_dbg(VFS, "Transform message is too small (%u)\n",
2180 pdu_length);
2181 cifs_reconnect(server);
2182 wake_up(&server->response_q);
2183 return -ECONNABORTED;
2184 }
2185
2186 if (pdu_length + 4 < orig_len + sizeof(struct smb2_transform_hdr)) {
2187 cifs_dbg(VFS, "Transform message is broken\n");
2188 cifs_reconnect(server);
2189 wake_up(&server->response_q);
2190 return -ECONNABORTED;
2191 }
2192
c42a6abe
PS
2193 if (pdu_length + 4 > CIFSMaxBufSize + MAX_HEADER_SIZE(server))
2194 return receive_encrypted_read(server, mid);
4326ed2f
PS
2195
2196 return receive_encrypted_standard(server, mid);
2197}
2198
2199int
2200smb3_handle_read_data(struct TCP_Server_Info *server, struct mid_q_entry *mid)
2201{
2202 char *buf = server->large_buf ? server->bigbuf : server->smallbuf;
2203
2204 return handle_read_data(server, mid, buf, get_rfc1002_length(buf) + 4,
2205 NULL, 0, 0);
2206}
2207
53ef1016 2208struct smb_version_operations smb20_operations = {
027e8eec 2209 .compare_fids = smb2_compare_fids,
2dc7e1c0 2210 .setup_request = smb2_setup_request,
c95b8eed 2211 .setup_async_request = smb2_setup_async_request,
2dc7e1c0 2212 .check_receive = smb2_check_receive,
28ea5290
PS
2213 .add_credits = smb2_add_credits,
2214 .set_credits = smb2_set_credits,
2215 .get_credits_field = smb2_get_credits_field,
2216 .get_credits = smb2_get_credits,
cb7e9eab 2217 .wait_mtu_credits = cifs_wait_mtu_credits,
2dc7e1c0 2218 .get_next_mid = smb2_get_next_mid,
09a4707e
PS
2219 .read_data_offset = smb2_read_data_offset,
2220 .read_data_length = smb2_read_data_length,
2221 .map_error = map_smb2_to_linux_error,
093b2bda
PS
2222 .find_mid = smb2_find_mid,
2223 .check_message = smb2_check_message,
2224 .dump_detail = smb2_dump_detail,
d60622eb
PS
2225 .clear_stats = smb2_clear_stats,
2226 .print_stats = smb2_print_stats,
983c88a4 2227 .is_oplock_break = smb2_is_valid_oplock_break,
c11f1df5 2228 .downgrade_oplock = smb2_downgrade_oplock,
ec2e4523
PS
2229 .need_neg = smb2_need_neg,
2230 .negotiate = smb2_negotiate,
3a3bab50
PS
2231 .negotiate_wsize = smb2_negotiate_wsize,
2232 .negotiate_rsize = smb2_negotiate_rsize,
5478f9ba
PS
2233 .sess_setup = SMB2_sess_setup,
2234 .logoff = SMB2_logoff,
faaf946a
PS
2235 .tree_connect = SMB2_tcon,
2236 .tree_disconnect = SMB2_tdis,
34f62640 2237 .qfs_tcon = smb2_qfs_tcon,
2503a0db 2238 .is_path_accessible = smb2_is_path_accessible,
9094fad1
PS
2239 .can_echo = smb2_can_echo,
2240 .echo = SMB2_echo,
be4cb9e3
PS
2241 .query_path_info = smb2_query_path_info,
2242 .get_srv_inum = smb2_get_srv_inum,
b7546bc5 2243 .query_file_info = smb2_query_file_info,
c839ff24
PS
2244 .set_path_size = smb2_set_path_size,
2245 .set_file_size = smb2_set_file_size,
1feeaac7 2246 .set_file_info = smb2_set_file_info,
64a5cfa6 2247 .set_compression = smb2_set_compression,
a0e73183
PS
2248 .mkdir = smb2_mkdir,
2249 .mkdir_setinfo = smb2_mkdir_setinfo,
1a500f01 2250 .rmdir = smb2_rmdir,
cbe6f439 2251 .unlink = smb2_unlink,
35143eb5 2252 .rename = smb2_rename_path,
568798cc 2253 .create_hardlink = smb2_create_hardlink,
b42bf888 2254 .query_symlink = smb2_query_symlink,
5b23c97d
SP
2255 .query_mf_symlink = smb3_query_mf_symlink,
2256 .create_mf_symlink = smb3_create_mf_symlink,
f0df737e
PS
2257 .open = smb2_open_file,
2258 .set_fid = smb2_set_fid,
2259 .close = smb2_close_file,
7a5cfb19 2260 .flush = smb2_flush_file,
09a4707e 2261 .async_readv = smb2_async_readv,
33319141 2262 .async_writev = smb2_async_writev,
d8e05039 2263 .sync_read = smb2_sync_read,
009d3443 2264 .sync_write = smb2_sync_write,
d324f08d
PS
2265 .query_dir_first = smb2_query_dir_first,
2266 .query_dir_next = smb2_query_dir_next,
2267 .close_dir = smb2_close_dir,
2268 .calc_smb_size = smb2_calc_size,
2e44b288 2269 .is_status_pending = smb2_is_status_pending,
983c88a4 2270 .oplock_response = smb2_oplock_response,
6fc05c25 2271 .queryfs = smb2_queryfs,
f7ba7fe6
PS
2272 .mand_lock = smb2_mand_lock,
2273 .mand_unlock_range = smb2_unlock_range,
b140799a 2274 .push_mand_locks = smb2_push_mandatory_locks,
b8c32dbb
PS
2275 .get_lease_key = smb2_get_lease_key,
2276 .set_lease_key = smb2_set_lease_key,
2277 .new_lease_key = smb2_new_lease_key,
38107d45 2278 .calc_signature = smb2_calc_signature,
53ef1016
PS
2279 .is_read_op = smb2_is_read_op,
2280 .set_oplock_level = smb2_set_oplock_level,
a41a28bd 2281 .create_lease_buf = smb2_create_lease_buf,
b5c7cde3 2282 .parse_lease_buf = smb2_parse_lease_buf,
41c1358e 2283 .clone_range = smb2_clone_range,
7f6c5008 2284 .wp_retry_size = smb2_wp_retry_size,
52755808 2285 .dir_needs_close = smb2_dir_needs_close,
38107d45
SF
2286};
2287
53ef1016
PS
2288struct smb_version_operations smb21_operations = {
2289 .compare_fids = smb2_compare_fids,
2290 .setup_request = smb2_setup_request,
2291 .setup_async_request = smb2_setup_async_request,
2292 .check_receive = smb2_check_receive,
2293 .add_credits = smb2_add_credits,
2294 .set_credits = smb2_set_credits,
2295 .get_credits_field = smb2_get_credits_field,
2296 .get_credits = smb2_get_credits,
cb7e9eab 2297 .wait_mtu_credits = smb2_wait_mtu_credits,
53ef1016
PS
2298 .get_next_mid = smb2_get_next_mid,
2299 .read_data_offset = smb2_read_data_offset,
2300 .read_data_length = smb2_read_data_length,
2301 .map_error = map_smb2_to_linux_error,
2302 .find_mid = smb2_find_mid,
2303 .check_message = smb2_check_message,
2304 .dump_detail = smb2_dump_detail,
2305 .clear_stats = smb2_clear_stats,
2306 .print_stats = smb2_print_stats,
2307 .is_oplock_break = smb2_is_valid_oplock_break,
c11f1df5 2308 .downgrade_oplock = smb2_downgrade_oplock,
53ef1016
PS
2309 .need_neg = smb2_need_neg,
2310 .negotiate = smb2_negotiate,
2311 .negotiate_wsize = smb2_negotiate_wsize,
2312 .negotiate_rsize = smb2_negotiate_rsize,
2313 .sess_setup = SMB2_sess_setup,
2314 .logoff = SMB2_logoff,
2315 .tree_connect = SMB2_tcon,
2316 .tree_disconnect = SMB2_tdis,
34f62640 2317 .qfs_tcon = smb2_qfs_tcon,
53ef1016
PS
2318 .is_path_accessible = smb2_is_path_accessible,
2319 .can_echo = smb2_can_echo,
2320 .echo = SMB2_echo,
2321 .query_path_info = smb2_query_path_info,
2322 .get_srv_inum = smb2_get_srv_inum,
2323 .query_file_info = smb2_query_file_info,
2324 .set_path_size = smb2_set_path_size,
2325 .set_file_size = smb2_set_file_size,
2326 .set_file_info = smb2_set_file_info,
64a5cfa6 2327 .set_compression = smb2_set_compression,
53ef1016
PS
2328 .mkdir = smb2_mkdir,
2329 .mkdir_setinfo = smb2_mkdir_setinfo,
2330 .rmdir = smb2_rmdir,
2331 .unlink = smb2_unlink,
2332 .rename = smb2_rename_path,
2333 .create_hardlink = smb2_create_hardlink,
2334 .query_symlink = smb2_query_symlink,
c22870ea 2335 .query_mf_symlink = smb3_query_mf_symlink,
5ab97578 2336 .create_mf_symlink = smb3_create_mf_symlink,
53ef1016
PS
2337 .open = smb2_open_file,
2338 .set_fid = smb2_set_fid,
2339 .close = smb2_close_file,
2340 .flush = smb2_flush_file,
2341 .async_readv = smb2_async_readv,
2342 .async_writev = smb2_async_writev,
2343 .sync_read = smb2_sync_read,
2344 .sync_write = smb2_sync_write,
2345 .query_dir_first = smb2_query_dir_first,
2346 .query_dir_next = smb2_query_dir_next,
2347 .close_dir = smb2_close_dir,
2348 .calc_smb_size = smb2_calc_size,
2349 .is_status_pending = smb2_is_status_pending,
2350 .oplock_response = smb2_oplock_response,
2351 .queryfs = smb2_queryfs,
2352 .mand_lock = smb2_mand_lock,
2353 .mand_unlock_range = smb2_unlock_range,
2354 .push_mand_locks = smb2_push_mandatory_locks,
2355 .get_lease_key = smb2_get_lease_key,
2356 .set_lease_key = smb2_set_lease_key,
2357 .new_lease_key = smb2_new_lease_key,
2358 .calc_signature = smb2_calc_signature,
2359 .is_read_op = smb21_is_read_op,
2360 .set_oplock_level = smb21_set_oplock_level,
a41a28bd 2361 .create_lease_buf = smb2_create_lease_buf,
b5c7cde3 2362 .parse_lease_buf = smb2_parse_lease_buf,
41c1358e 2363 .clone_range = smb2_clone_range,
7f6c5008 2364 .wp_retry_size = smb2_wp_retry_size,
52755808 2365 .dir_needs_close = smb2_dir_needs_close,
834170c8 2366 .enum_snapshots = smb3_enum_snapshots,
53ef1016 2367};
38107d45
SF
2368
2369struct smb_version_operations smb30_operations = {
2370 .compare_fids = smb2_compare_fids,
2371 .setup_request = smb2_setup_request,
2372 .setup_async_request = smb2_setup_async_request,
2373 .check_receive = smb2_check_receive,
2374 .add_credits = smb2_add_credits,
2375 .set_credits = smb2_set_credits,
2376 .get_credits_field = smb2_get_credits_field,
2377 .get_credits = smb2_get_credits,
cb7e9eab 2378 .wait_mtu_credits = smb2_wait_mtu_credits,
38107d45
SF
2379 .get_next_mid = smb2_get_next_mid,
2380 .read_data_offset = smb2_read_data_offset,
2381 .read_data_length = smb2_read_data_length,
2382 .map_error = map_smb2_to_linux_error,
2383 .find_mid = smb2_find_mid,
2384 .check_message = smb2_check_message,
2385 .dump_detail = smb2_dump_detail,
2386 .clear_stats = smb2_clear_stats,
2387 .print_stats = smb2_print_stats,
769ee6a4 2388 .dump_share_caps = smb2_dump_share_caps,
38107d45 2389 .is_oplock_break = smb2_is_valid_oplock_break,
c11f1df5 2390 .downgrade_oplock = smb2_downgrade_oplock,
38107d45
SF
2391 .need_neg = smb2_need_neg,
2392 .negotiate = smb2_negotiate,
2393 .negotiate_wsize = smb2_negotiate_wsize,
2394 .negotiate_rsize = smb2_negotiate_rsize,
2395 .sess_setup = SMB2_sess_setup,
2396 .logoff = SMB2_logoff,
2397 .tree_connect = SMB2_tcon,
2398 .tree_disconnect = SMB2_tdis,
af6a12ea 2399 .qfs_tcon = smb3_qfs_tcon,
38107d45
SF
2400 .is_path_accessible = smb2_is_path_accessible,
2401 .can_echo = smb2_can_echo,
2402 .echo = SMB2_echo,
2403 .query_path_info = smb2_query_path_info,
2404 .get_srv_inum = smb2_get_srv_inum,
2405 .query_file_info = smb2_query_file_info,
2406 .set_path_size = smb2_set_path_size,
2407 .set_file_size = smb2_set_file_size,
2408 .set_file_info = smb2_set_file_info,
64a5cfa6 2409 .set_compression = smb2_set_compression,
38107d45
SF
2410 .mkdir = smb2_mkdir,
2411 .mkdir_setinfo = smb2_mkdir_setinfo,
2412 .rmdir = smb2_rmdir,
2413 .unlink = smb2_unlink,
2414 .rename = smb2_rename_path,
2415 .create_hardlink = smb2_create_hardlink,
b42bf888 2416 .query_symlink = smb2_query_symlink,
c22870ea 2417 .query_mf_symlink = smb3_query_mf_symlink,
5ab97578 2418 .create_mf_symlink = smb3_create_mf_symlink,
38107d45
SF
2419 .open = smb2_open_file,
2420 .set_fid = smb2_set_fid,
2421 .close = smb2_close_file,
2422 .flush = smb2_flush_file,
2423 .async_readv = smb2_async_readv,
2424 .async_writev = smb2_async_writev,
2425 .sync_read = smb2_sync_read,
2426 .sync_write = smb2_sync_write,
2427 .query_dir_first = smb2_query_dir_first,
2428 .query_dir_next = smb2_query_dir_next,
2429 .close_dir = smb2_close_dir,
2430 .calc_smb_size = smb2_calc_size,
2431 .is_status_pending = smb2_is_status_pending,
2432 .oplock_response = smb2_oplock_response,
2433 .queryfs = smb2_queryfs,
2434 .mand_lock = smb2_mand_lock,
2435 .mand_unlock_range = smb2_unlock_range,
2436 .push_mand_locks = smb2_push_mandatory_locks,
2437 .get_lease_key = smb2_get_lease_key,
2438 .set_lease_key = smb2_set_lease_key,
2439 .new_lease_key = smb2_new_lease_key,
373512ec 2440 .generate_signingkey = generate_smb30signingkey,
38107d45 2441 .calc_signature = smb3_calc_signature,
b3152e2c 2442 .set_integrity = smb3_set_integrity,
53ef1016 2443 .is_read_op = smb21_is_read_op,
42873b0a 2444 .set_oplock_level = smb3_set_oplock_level,
f047390a
PS
2445 .create_lease_buf = smb3_create_lease_buf,
2446 .parse_lease_buf = smb3_parse_lease_buf,
41c1358e 2447 .clone_range = smb2_clone_range,
ca9e7a1c 2448 .duplicate_extents = smb2_duplicate_extents,
ff1c038a 2449 .validate_negotiate = smb3_validate_negotiate,
7f6c5008 2450 .wp_retry_size = smb2_wp_retry_size,
52755808 2451 .dir_needs_close = smb2_dir_needs_close,
31742c5a 2452 .fallocate = smb3_fallocate,
834170c8 2453 .enum_snapshots = smb3_enum_snapshots,
026e93dc
PS
2454 .init_transform_rq = smb3_init_transform_rq,
2455 .free_transform_rq = smb3_free_transform_rq,
4326ed2f
PS
2456 .is_transform_hdr = smb3_is_transform_hdr,
2457 .receive_transform = smb3_receive_transform,
1080ef75
SF
2458};
2459
aab1893d
SF
2460#ifdef CONFIG_CIFS_SMB311
2461struct smb_version_operations smb311_operations = {
2462 .compare_fids = smb2_compare_fids,
2463 .setup_request = smb2_setup_request,
2464 .setup_async_request = smb2_setup_async_request,
2465 .check_receive = smb2_check_receive,
2466 .add_credits = smb2_add_credits,
2467 .set_credits = smb2_set_credits,
2468 .get_credits_field = smb2_get_credits_field,
2469 .get_credits = smb2_get_credits,
2470 .wait_mtu_credits = smb2_wait_mtu_credits,
2471 .get_next_mid = smb2_get_next_mid,
2472 .read_data_offset = smb2_read_data_offset,
2473 .read_data_length = smb2_read_data_length,
2474 .map_error = map_smb2_to_linux_error,
2475 .find_mid = smb2_find_mid,
2476 .check_message = smb2_check_message,
2477 .dump_detail = smb2_dump_detail,
2478 .clear_stats = smb2_clear_stats,
2479 .print_stats = smb2_print_stats,
2480 .dump_share_caps = smb2_dump_share_caps,
2481 .is_oplock_break = smb2_is_valid_oplock_break,
2482 .downgrade_oplock = smb2_downgrade_oplock,
2483 .need_neg = smb2_need_neg,
2484 .negotiate = smb2_negotiate,
2485 .negotiate_wsize = smb2_negotiate_wsize,
2486 .negotiate_rsize = smb2_negotiate_rsize,
2487 .sess_setup = SMB2_sess_setup,
2488 .logoff = SMB2_logoff,
2489 .tree_connect = SMB2_tcon,
2490 .tree_disconnect = SMB2_tdis,
2491 .qfs_tcon = smb3_qfs_tcon,
2492 .is_path_accessible = smb2_is_path_accessible,
2493 .can_echo = smb2_can_echo,
2494 .echo = SMB2_echo,
2495 .query_path_info = smb2_query_path_info,
2496 .get_srv_inum = smb2_get_srv_inum,
2497 .query_file_info = smb2_query_file_info,
2498 .set_path_size = smb2_set_path_size,
2499 .set_file_size = smb2_set_file_size,
2500 .set_file_info = smb2_set_file_info,
2501 .set_compression = smb2_set_compression,
2502 .mkdir = smb2_mkdir,
2503 .mkdir_setinfo = smb2_mkdir_setinfo,
2504 .rmdir = smb2_rmdir,
2505 .unlink = smb2_unlink,
2506 .rename = smb2_rename_path,
2507 .create_hardlink = smb2_create_hardlink,
2508 .query_symlink = smb2_query_symlink,
2509 .query_mf_symlink = smb3_query_mf_symlink,
2510 .create_mf_symlink = smb3_create_mf_symlink,
2511 .open = smb2_open_file,
2512 .set_fid = smb2_set_fid,
2513 .close = smb2_close_file,
2514 .flush = smb2_flush_file,
2515 .async_readv = smb2_async_readv,
2516 .async_writev = smb2_async_writev,
2517 .sync_read = smb2_sync_read,
2518 .sync_write = smb2_sync_write,
2519 .query_dir_first = smb2_query_dir_first,
2520 .query_dir_next = smb2_query_dir_next,
2521 .close_dir = smb2_close_dir,
2522 .calc_smb_size = smb2_calc_size,
2523 .is_status_pending = smb2_is_status_pending,
2524 .oplock_response = smb2_oplock_response,
2525 .queryfs = smb2_queryfs,
2526 .mand_lock = smb2_mand_lock,
2527 .mand_unlock_range = smb2_unlock_range,
2528 .push_mand_locks = smb2_push_mandatory_locks,
2529 .get_lease_key = smb2_get_lease_key,
2530 .set_lease_key = smb2_set_lease_key,
2531 .new_lease_key = smb2_new_lease_key,
373512ec 2532 .generate_signingkey = generate_smb311signingkey,
aab1893d 2533 .calc_signature = smb3_calc_signature,
b3152e2c 2534 .set_integrity = smb3_set_integrity,
aab1893d
SF
2535 .is_read_op = smb21_is_read_op,
2536 .set_oplock_level = smb3_set_oplock_level,
2537 .create_lease_buf = smb3_create_lease_buf,
2538 .parse_lease_buf = smb3_parse_lease_buf,
2539 .clone_range = smb2_clone_range,
02b16665 2540 .duplicate_extents = smb2_duplicate_extents,
aab1893d
SF
2541/* .validate_negotiate = smb3_validate_negotiate, */ /* not used in 3.11 */
2542 .wp_retry_size = smb2_wp_retry_size,
2543 .dir_needs_close = smb2_dir_needs_close,
2544 .fallocate = smb3_fallocate,
834170c8 2545 .enum_snapshots = smb3_enum_snapshots,
026e93dc
PS
2546 .init_transform_rq = smb3_init_transform_rq,
2547 .free_transform_rq = smb3_free_transform_rq,
4326ed2f
PS
2548 .is_transform_hdr = smb3_is_transform_hdr,
2549 .receive_transform = smb3_receive_transform,
aab1893d
SF
2550};
2551#endif /* CIFS_SMB311 */
2552
dd446b16
SF
2553struct smb_version_values smb20_values = {
2554 .version_string = SMB20_VERSION_STRING,
2555 .protocol_id = SMB20_PROT_ID,
2556 .req_capabilities = 0, /* MBZ */
2557 .large_lock_type = 0,
2558 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
2559 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
2560 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
2561 .header_size = sizeof(struct smb2_hdr),
2562 .max_header_size = MAX_SMB2_HDR_SIZE,
2563 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
2564 .lock_cmd = SMB2_LOCK,
2565 .cap_unix = 0,
2566 .cap_nt_find = SMB2_NT_FIND,
2567 .cap_large_files = SMB2_LARGE_FILES,
50285882
JL
2568 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
2569 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
a41a28bd 2570 .create_lease_size = sizeof(struct create_lease),
dd446b16
SF
2571};
2572
1080ef75
SF
2573struct smb_version_values smb21_values = {
2574 .version_string = SMB21_VERSION_STRING,
e4aa25e7
SF
2575 .protocol_id = SMB21_PROT_ID,
2576 .req_capabilities = 0, /* MBZ on negotiate req until SMB3 dialect */
2577 .large_lock_type = 0,
2578 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
2579 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
2580 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
2581 .header_size = sizeof(struct smb2_hdr),
2582 .max_header_size = MAX_SMB2_HDR_SIZE,
2583 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
2584 .lock_cmd = SMB2_LOCK,
2585 .cap_unix = 0,
2586 .cap_nt_find = SMB2_NT_FIND,
2587 .cap_large_files = SMB2_LARGE_FILES,
50285882
JL
2588 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
2589 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
a41a28bd 2590 .create_lease_size = sizeof(struct create_lease),
e4aa25e7
SF
2591};
2592
2593struct smb_version_values smb30_values = {
2594 .version_string = SMB30_VERSION_STRING,
2595 .protocol_id = SMB30_PROT_ID,
373512ec 2596 .req_capabilities = SMB2_GLOBAL_CAP_DFS | SMB2_GLOBAL_CAP_LEASING | SMB2_GLOBAL_CAP_LARGE_MTU | SMB2_GLOBAL_CAP_PERSISTENT_HANDLES | SMB2_GLOBAL_CAP_ENCRYPTION,
027e8eec
PS
2597 .large_lock_type = 0,
2598 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
2599 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
2600 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
093b2bda
PS
2601 .header_size = sizeof(struct smb2_hdr),
2602 .max_header_size = MAX_SMB2_HDR_SIZE,
09a4707e 2603 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
2dc7e1c0 2604 .lock_cmd = SMB2_LOCK,
29e20f9c
PS
2605 .cap_unix = 0,
2606 .cap_nt_find = SMB2_NT_FIND,
2607 .cap_large_files = SMB2_LARGE_FILES,
50285882
JL
2608 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
2609 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
f047390a 2610 .create_lease_size = sizeof(struct create_lease_v2),
1080ef75 2611};
20b6d8b4
SF
2612
2613struct smb_version_values smb302_values = {
2614 .version_string = SMB302_VERSION_STRING,
2615 .protocol_id = SMB302_PROT_ID,
373512ec 2616 .req_capabilities = SMB2_GLOBAL_CAP_DFS | SMB2_GLOBAL_CAP_LEASING | SMB2_GLOBAL_CAP_LARGE_MTU | SMB2_GLOBAL_CAP_PERSISTENT_HANDLES | SMB2_GLOBAL_CAP_ENCRYPTION,
20b6d8b4
SF
2617 .large_lock_type = 0,
2618 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
2619 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
2620 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
2621 .header_size = sizeof(struct smb2_hdr),
2622 .max_header_size = MAX_SMB2_HDR_SIZE,
2623 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
2624 .lock_cmd = SMB2_LOCK,
2625 .cap_unix = 0,
2626 .cap_nt_find = SMB2_NT_FIND,
2627 .cap_large_files = SMB2_LARGE_FILES,
50285882
JL
2628 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
2629 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
f047390a 2630 .create_lease_size = sizeof(struct create_lease_v2),
20b6d8b4 2631};
5f7fbf73
SF
2632
2633#ifdef CONFIG_CIFS_SMB311
2634struct smb_version_values smb311_values = {
2635 .version_string = SMB311_VERSION_STRING,
2636 .protocol_id = SMB311_PROT_ID,
b618f001 2637 .req_capabilities = SMB2_GLOBAL_CAP_DFS | SMB2_GLOBAL_CAP_LEASING | SMB2_GLOBAL_CAP_LARGE_MTU | SMB2_GLOBAL_CAP_PERSISTENT_HANDLES,
5f7fbf73
SF
2638 .large_lock_type = 0,
2639 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
2640 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
2641 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
2642 .header_size = sizeof(struct smb2_hdr),
2643 .max_header_size = MAX_SMB2_HDR_SIZE,
2644 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
2645 .lock_cmd = SMB2_LOCK,
2646 .cap_unix = 0,
2647 .cap_nt_find = SMB2_NT_FIND,
2648 .cap_large_files = SMB2_LARGE_FILES,
2649 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
2650 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
2651 .create_lease_size = sizeof(struct create_lease_v2),
2652};
2653#endif /* SMB311 */