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