]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - fs/cifs/smb2ops.c
Merge branches 'for-4.11/upstream-fixes', 'for-4.12/accutouch', 'for-4.12/cp2112...
[mirror_ubuntu-artful-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 return rc;
932 }
933
934 if (ret_data_len > snapshot_in.snapshot_array_size)
935 ret_data_len = snapshot_in.snapshot_array_size;
936
937 if (copy_to_user(ioc_buf, retbuf, ret_data_len))
938 rc = -EFAULT;
939 }
940
941 kfree(retbuf);
942 return rc;
943 }
944
945 static int
946 smb2_query_dir_first(const unsigned int xid, struct cifs_tcon *tcon,
947 const char *path, struct cifs_sb_info *cifs_sb,
948 struct cifs_fid *fid, __u16 search_flags,
949 struct cifs_search_info *srch_inf)
950 {
951 __le16 *utf16_path;
952 int rc;
953 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
954 struct cifs_open_parms oparms;
955
956 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
957 if (!utf16_path)
958 return -ENOMEM;
959
960 oparms.tcon = tcon;
961 oparms.desired_access = FILE_READ_ATTRIBUTES | FILE_READ_DATA;
962 oparms.disposition = FILE_OPEN;
963 oparms.create_options = 0;
964 oparms.fid = fid;
965 oparms.reconnect = false;
966
967 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL);
968 kfree(utf16_path);
969 if (rc) {
970 cifs_dbg(VFS, "open dir failed\n");
971 return rc;
972 }
973
974 srch_inf->entries_in_buffer = 0;
975 srch_inf->index_of_last_entry = 0;
976
977 rc = SMB2_query_directory(xid, tcon, fid->persistent_fid,
978 fid->volatile_fid, 0, srch_inf);
979 if (rc) {
980 cifs_dbg(VFS, "query directory failed\n");
981 SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
982 }
983 return rc;
984 }
985
986 static int
987 smb2_query_dir_next(const unsigned int xid, struct cifs_tcon *tcon,
988 struct cifs_fid *fid, __u16 search_flags,
989 struct cifs_search_info *srch_inf)
990 {
991 return SMB2_query_directory(xid, tcon, fid->persistent_fid,
992 fid->volatile_fid, 0, srch_inf);
993 }
994
995 static int
996 smb2_close_dir(const unsigned int xid, struct cifs_tcon *tcon,
997 struct cifs_fid *fid)
998 {
999 return SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
1000 }
1001
1002 /*
1003 * If we negotiate SMB2 protocol and get STATUS_PENDING - update
1004 * the number of credits and return true. Otherwise - return false.
1005 */
1006 static bool
1007 smb2_is_status_pending(char *buf, struct TCP_Server_Info *server, int length)
1008 {
1009 struct smb2_sync_hdr *shdr = get_sync_hdr(buf);
1010
1011 if (shdr->Status != STATUS_PENDING)
1012 return false;
1013
1014 if (!length) {
1015 spin_lock(&server->req_lock);
1016 server->credits += le16_to_cpu(shdr->CreditRequest);
1017 spin_unlock(&server->req_lock);
1018 wake_up(&server->request_q);
1019 }
1020
1021 return true;
1022 }
1023
1024 static int
1025 smb2_oplock_response(struct cifs_tcon *tcon, struct cifs_fid *fid,
1026 struct cifsInodeInfo *cinode)
1027 {
1028 if (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_LEASING)
1029 return SMB2_lease_break(0, tcon, cinode->lease_key,
1030 smb2_get_lease_state(cinode));
1031
1032 return SMB2_oplock_break(0, tcon, fid->persistent_fid,
1033 fid->volatile_fid,
1034 CIFS_CACHE_READ(cinode) ? 1 : 0);
1035 }
1036
1037 static int
1038 smb2_queryfs(const unsigned int xid, struct cifs_tcon *tcon,
1039 struct kstatfs *buf)
1040 {
1041 int rc;
1042 __le16 srch_path = 0; /* Null - open root of share */
1043 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
1044 struct cifs_open_parms oparms;
1045 struct cifs_fid fid;
1046
1047 oparms.tcon = tcon;
1048 oparms.desired_access = FILE_READ_ATTRIBUTES;
1049 oparms.disposition = FILE_OPEN;
1050 oparms.create_options = 0;
1051 oparms.fid = &fid;
1052 oparms.reconnect = false;
1053
1054 rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL);
1055 if (rc)
1056 return rc;
1057 buf->f_type = SMB2_MAGIC_NUMBER;
1058 rc = SMB2_QFS_info(xid, tcon, fid.persistent_fid, fid.volatile_fid,
1059 buf);
1060 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
1061 return rc;
1062 }
1063
1064 static bool
1065 smb2_compare_fids(struct cifsFileInfo *ob1, struct cifsFileInfo *ob2)
1066 {
1067 return ob1->fid.persistent_fid == ob2->fid.persistent_fid &&
1068 ob1->fid.volatile_fid == ob2->fid.volatile_fid;
1069 }
1070
1071 static int
1072 smb2_mand_lock(const unsigned int xid, struct cifsFileInfo *cfile, __u64 offset,
1073 __u64 length, __u32 type, int lock, int unlock, bool wait)
1074 {
1075 if (unlock && !lock)
1076 type = SMB2_LOCKFLAG_UNLOCK;
1077 return SMB2_lock(xid, tlink_tcon(cfile->tlink),
1078 cfile->fid.persistent_fid, cfile->fid.volatile_fid,
1079 current->tgid, length, offset, type, wait);
1080 }
1081
1082 static void
1083 smb2_get_lease_key(struct inode *inode, struct cifs_fid *fid)
1084 {
1085 memcpy(fid->lease_key, CIFS_I(inode)->lease_key, SMB2_LEASE_KEY_SIZE);
1086 }
1087
1088 static void
1089 smb2_set_lease_key(struct inode *inode, struct cifs_fid *fid)
1090 {
1091 memcpy(CIFS_I(inode)->lease_key, fid->lease_key, SMB2_LEASE_KEY_SIZE);
1092 }
1093
1094 static void
1095 smb2_new_lease_key(struct cifs_fid *fid)
1096 {
1097 generate_random_uuid(fid->lease_key);
1098 }
1099
1100 #define SMB2_SYMLINK_STRUCT_SIZE \
1101 (sizeof(struct smb2_err_rsp) - 1 + sizeof(struct smb2_symlink_err_rsp))
1102
1103 static int
1104 smb2_query_symlink(const unsigned int xid, struct cifs_tcon *tcon,
1105 const char *full_path, char **target_path,
1106 struct cifs_sb_info *cifs_sb)
1107 {
1108 int rc;
1109 __le16 *utf16_path;
1110 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
1111 struct cifs_open_parms oparms;
1112 struct cifs_fid fid;
1113 struct smb2_err_rsp *err_buf = NULL;
1114 struct smb2_symlink_err_rsp *symlink;
1115 unsigned int sub_len;
1116 unsigned int sub_offset;
1117 unsigned int print_len;
1118 unsigned int print_offset;
1119
1120 cifs_dbg(FYI, "%s: path: %s\n", __func__, full_path);
1121
1122 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
1123 if (!utf16_path)
1124 return -ENOMEM;
1125
1126 oparms.tcon = tcon;
1127 oparms.desired_access = FILE_READ_ATTRIBUTES;
1128 oparms.disposition = FILE_OPEN;
1129 oparms.create_options = 0;
1130 oparms.fid = &fid;
1131 oparms.reconnect = false;
1132
1133 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, &err_buf);
1134
1135 if (!rc || !err_buf) {
1136 kfree(utf16_path);
1137 return -ENOENT;
1138 }
1139
1140 if (le32_to_cpu(err_buf->ByteCount) < sizeof(struct smb2_symlink_err_rsp) ||
1141 get_rfc1002_length(err_buf) + 4 < SMB2_SYMLINK_STRUCT_SIZE) {
1142 kfree(utf16_path);
1143 return -ENOENT;
1144 }
1145
1146 /* open must fail on symlink - reset rc */
1147 rc = 0;
1148 symlink = (struct smb2_symlink_err_rsp *)err_buf->ErrorData;
1149 sub_len = le16_to_cpu(symlink->SubstituteNameLength);
1150 sub_offset = le16_to_cpu(symlink->SubstituteNameOffset);
1151 print_len = le16_to_cpu(symlink->PrintNameLength);
1152 print_offset = le16_to_cpu(symlink->PrintNameOffset);
1153
1154 if (get_rfc1002_length(err_buf) + 4 <
1155 SMB2_SYMLINK_STRUCT_SIZE + sub_offset + sub_len) {
1156 kfree(utf16_path);
1157 return -ENOENT;
1158 }
1159
1160 if (get_rfc1002_length(err_buf) + 4 <
1161 SMB2_SYMLINK_STRUCT_SIZE + print_offset + print_len) {
1162 kfree(utf16_path);
1163 return -ENOENT;
1164 }
1165
1166 *target_path = cifs_strndup_from_utf16(
1167 (char *)symlink->PathBuffer + sub_offset,
1168 sub_len, true, cifs_sb->local_nls);
1169 if (!(*target_path)) {
1170 kfree(utf16_path);
1171 return -ENOMEM;
1172 }
1173 convert_delimiter(*target_path, '/');
1174 cifs_dbg(FYI, "%s: target path: %s\n", __func__, *target_path);
1175 kfree(utf16_path);
1176 return rc;
1177 }
1178
1179 static long smb3_zero_range(struct file *file, struct cifs_tcon *tcon,
1180 loff_t offset, loff_t len, bool keep_size)
1181 {
1182 struct inode *inode;
1183 struct cifsInodeInfo *cifsi;
1184 struct cifsFileInfo *cfile = file->private_data;
1185 struct file_zero_data_information fsctl_buf;
1186 long rc;
1187 unsigned int xid;
1188
1189 xid = get_xid();
1190
1191 inode = d_inode(cfile->dentry);
1192 cifsi = CIFS_I(inode);
1193
1194 /* if file not oplocked can't be sure whether asking to extend size */
1195 if (!CIFS_CACHE_READ(cifsi))
1196 if (keep_size == false)
1197 return -EOPNOTSUPP;
1198
1199 /*
1200 * Must check if file sparse since fallocate -z (zero range) assumes
1201 * non-sparse allocation
1202 */
1203 if (!(cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE))
1204 return -EOPNOTSUPP;
1205
1206 /*
1207 * need to make sure we are not asked to extend the file since the SMB3
1208 * fsctl does not change the file size. In the future we could change
1209 * this to zero the first part of the range then set the file size
1210 * which for a non sparse file would zero the newly extended range
1211 */
1212 if (keep_size == false)
1213 if (i_size_read(inode) < offset + len)
1214 return -EOPNOTSUPP;
1215
1216 cifs_dbg(FYI, "offset %lld len %lld", offset, len);
1217
1218 fsctl_buf.FileOffset = cpu_to_le64(offset);
1219 fsctl_buf.BeyondFinalZero = cpu_to_le64(offset + len);
1220
1221 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
1222 cfile->fid.volatile_fid, FSCTL_SET_ZERO_DATA,
1223 true /* is_fctl */, (char *)&fsctl_buf,
1224 sizeof(struct file_zero_data_information), NULL, NULL);
1225 free_xid(xid);
1226 return rc;
1227 }
1228
1229 static long smb3_punch_hole(struct file *file, struct cifs_tcon *tcon,
1230 loff_t offset, loff_t len)
1231 {
1232 struct inode *inode;
1233 struct cifsInodeInfo *cifsi;
1234 struct cifsFileInfo *cfile = file->private_data;
1235 struct file_zero_data_information fsctl_buf;
1236 long rc;
1237 unsigned int xid;
1238 __u8 set_sparse = 1;
1239
1240 xid = get_xid();
1241
1242 inode = d_inode(cfile->dentry);
1243 cifsi = CIFS_I(inode);
1244
1245 /* Need to make file sparse, if not already, before freeing range. */
1246 /* Consider adding equivalent for compressed since it could also work */
1247 if (!smb2_set_sparse(xid, tcon, cfile, inode, set_sparse))
1248 return -EOPNOTSUPP;
1249
1250 cifs_dbg(FYI, "offset %lld len %lld", offset, len);
1251
1252 fsctl_buf.FileOffset = cpu_to_le64(offset);
1253 fsctl_buf.BeyondFinalZero = cpu_to_le64(offset + len);
1254
1255 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
1256 cfile->fid.volatile_fid, FSCTL_SET_ZERO_DATA,
1257 true /* is_fctl */, (char *)&fsctl_buf,
1258 sizeof(struct file_zero_data_information), NULL, NULL);
1259 free_xid(xid);
1260 return rc;
1261 }
1262
1263 static long smb3_simple_falloc(struct file *file, struct cifs_tcon *tcon,
1264 loff_t off, loff_t len, bool keep_size)
1265 {
1266 struct inode *inode;
1267 struct cifsInodeInfo *cifsi;
1268 struct cifsFileInfo *cfile = file->private_data;
1269 long rc = -EOPNOTSUPP;
1270 unsigned int xid;
1271
1272 xid = get_xid();
1273
1274 inode = d_inode(cfile->dentry);
1275 cifsi = CIFS_I(inode);
1276
1277 /* if file not oplocked can't be sure whether asking to extend size */
1278 if (!CIFS_CACHE_READ(cifsi))
1279 if (keep_size == false)
1280 return -EOPNOTSUPP;
1281
1282 /*
1283 * Files are non-sparse by default so falloc may be a no-op
1284 * Must check if file sparse. If not sparse, and not extending
1285 * then no need to do anything since file already allocated
1286 */
1287 if ((cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) == 0) {
1288 if (keep_size == true)
1289 return 0;
1290 /* check if extending file */
1291 else if (i_size_read(inode) >= off + len)
1292 /* not extending file and already not sparse */
1293 return 0;
1294 /* BB: in future add else clause to extend file */
1295 else
1296 return -EOPNOTSUPP;
1297 }
1298
1299 if ((keep_size == true) || (i_size_read(inode) >= off + len)) {
1300 /*
1301 * Check if falloc starts within first few pages of file
1302 * and ends within a few pages of the end of file to
1303 * ensure that most of file is being forced to be
1304 * fallocated now. If so then setting whole file sparse
1305 * ie potentially making a few extra pages at the beginning
1306 * or end of the file non-sparse via set_sparse is harmless.
1307 */
1308 if ((off > 8192) || (off + len + 8192 < i_size_read(inode)))
1309 return -EOPNOTSUPP;
1310
1311 rc = smb2_set_sparse(xid, tcon, cfile, inode, false);
1312 }
1313 /* BB: else ... in future add code to extend file and set sparse */
1314
1315
1316 free_xid(xid);
1317 return rc;
1318 }
1319
1320
1321 static long smb3_fallocate(struct file *file, struct cifs_tcon *tcon, int mode,
1322 loff_t off, loff_t len)
1323 {
1324 /* KEEP_SIZE already checked for by do_fallocate */
1325 if (mode & FALLOC_FL_PUNCH_HOLE)
1326 return smb3_punch_hole(file, tcon, off, len);
1327 else if (mode & FALLOC_FL_ZERO_RANGE) {
1328 if (mode & FALLOC_FL_KEEP_SIZE)
1329 return smb3_zero_range(file, tcon, off, len, true);
1330 return smb3_zero_range(file, tcon, off, len, false);
1331 } else if (mode == FALLOC_FL_KEEP_SIZE)
1332 return smb3_simple_falloc(file, tcon, off, len, true);
1333 else if (mode == 0)
1334 return smb3_simple_falloc(file, tcon, off, len, false);
1335
1336 return -EOPNOTSUPP;
1337 }
1338
1339 static void
1340 smb2_downgrade_oplock(struct TCP_Server_Info *server,
1341 struct cifsInodeInfo *cinode, bool set_level2)
1342 {
1343 if (set_level2)
1344 server->ops->set_oplock_level(cinode, SMB2_OPLOCK_LEVEL_II,
1345 0, NULL);
1346 else
1347 server->ops->set_oplock_level(cinode, 0, 0, NULL);
1348 }
1349
1350 static void
1351 smb2_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
1352 unsigned int epoch, bool *purge_cache)
1353 {
1354 oplock &= 0xFF;
1355 if (oplock == SMB2_OPLOCK_LEVEL_NOCHANGE)
1356 return;
1357 if (oplock == SMB2_OPLOCK_LEVEL_BATCH) {
1358 cinode->oplock = CIFS_CACHE_RHW_FLG;
1359 cifs_dbg(FYI, "Batch Oplock granted on inode %p\n",
1360 &cinode->vfs_inode);
1361 } else if (oplock == SMB2_OPLOCK_LEVEL_EXCLUSIVE) {
1362 cinode->oplock = CIFS_CACHE_RW_FLG;
1363 cifs_dbg(FYI, "Exclusive Oplock granted on inode %p\n",
1364 &cinode->vfs_inode);
1365 } else if (oplock == SMB2_OPLOCK_LEVEL_II) {
1366 cinode->oplock = CIFS_CACHE_READ_FLG;
1367 cifs_dbg(FYI, "Level II Oplock granted on inode %p\n",
1368 &cinode->vfs_inode);
1369 } else
1370 cinode->oplock = 0;
1371 }
1372
1373 static void
1374 smb21_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
1375 unsigned int epoch, bool *purge_cache)
1376 {
1377 char message[5] = {0};
1378
1379 oplock &= 0xFF;
1380 if (oplock == SMB2_OPLOCK_LEVEL_NOCHANGE)
1381 return;
1382
1383 cinode->oplock = 0;
1384 if (oplock & SMB2_LEASE_READ_CACHING_HE) {
1385 cinode->oplock |= CIFS_CACHE_READ_FLG;
1386 strcat(message, "R");
1387 }
1388 if (oplock & SMB2_LEASE_HANDLE_CACHING_HE) {
1389 cinode->oplock |= CIFS_CACHE_HANDLE_FLG;
1390 strcat(message, "H");
1391 }
1392 if (oplock & SMB2_LEASE_WRITE_CACHING_HE) {
1393 cinode->oplock |= CIFS_CACHE_WRITE_FLG;
1394 strcat(message, "W");
1395 }
1396 if (!cinode->oplock)
1397 strcat(message, "None");
1398 cifs_dbg(FYI, "%s Lease granted on inode %p\n", message,
1399 &cinode->vfs_inode);
1400 }
1401
1402 static void
1403 smb3_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
1404 unsigned int epoch, bool *purge_cache)
1405 {
1406 unsigned int old_oplock = cinode->oplock;
1407
1408 smb21_set_oplock_level(cinode, oplock, epoch, purge_cache);
1409
1410 if (purge_cache) {
1411 *purge_cache = false;
1412 if (old_oplock == CIFS_CACHE_READ_FLG) {
1413 if (cinode->oplock == CIFS_CACHE_READ_FLG &&
1414 (epoch - cinode->epoch > 0))
1415 *purge_cache = true;
1416 else if (cinode->oplock == CIFS_CACHE_RH_FLG &&
1417 (epoch - cinode->epoch > 1))
1418 *purge_cache = true;
1419 else if (cinode->oplock == CIFS_CACHE_RHW_FLG &&
1420 (epoch - cinode->epoch > 1))
1421 *purge_cache = true;
1422 else if (cinode->oplock == 0 &&
1423 (epoch - cinode->epoch > 0))
1424 *purge_cache = true;
1425 } else if (old_oplock == CIFS_CACHE_RH_FLG) {
1426 if (cinode->oplock == CIFS_CACHE_RH_FLG &&
1427 (epoch - cinode->epoch > 0))
1428 *purge_cache = true;
1429 else if (cinode->oplock == CIFS_CACHE_RHW_FLG &&
1430 (epoch - cinode->epoch > 1))
1431 *purge_cache = true;
1432 }
1433 cinode->epoch = epoch;
1434 }
1435 }
1436
1437 static bool
1438 smb2_is_read_op(__u32 oplock)
1439 {
1440 return oplock == SMB2_OPLOCK_LEVEL_II;
1441 }
1442
1443 static bool
1444 smb21_is_read_op(__u32 oplock)
1445 {
1446 return (oplock & SMB2_LEASE_READ_CACHING_HE) &&
1447 !(oplock & SMB2_LEASE_WRITE_CACHING_HE);
1448 }
1449
1450 static __le32
1451 map_oplock_to_lease(u8 oplock)
1452 {
1453 if (oplock == SMB2_OPLOCK_LEVEL_EXCLUSIVE)
1454 return SMB2_LEASE_WRITE_CACHING | SMB2_LEASE_READ_CACHING;
1455 else if (oplock == SMB2_OPLOCK_LEVEL_II)
1456 return SMB2_LEASE_READ_CACHING;
1457 else if (oplock == SMB2_OPLOCK_LEVEL_BATCH)
1458 return SMB2_LEASE_HANDLE_CACHING | SMB2_LEASE_READ_CACHING |
1459 SMB2_LEASE_WRITE_CACHING;
1460 return 0;
1461 }
1462
1463 static char *
1464 smb2_create_lease_buf(u8 *lease_key, u8 oplock)
1465 {
1466 struct create_lease *buf;
1467
1468 buf = kzalloc(sizeof(struct create_lease), GFP_KERNEL);
1469 if (!buf)
1470 return NULL;
1471
1472 buf->lcontext.LeaseKeyLow = cpu_to_le64(*((u64 *)lease_key));
1473 buf->lcontext.LeaseKeyHigh = cpu_to_le64(*((u64 *)(lease_key + 8)));
1474 buf->lcontext.LeaseState = map_oplock_to_lease(oplock);
1475
1476 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1477 (struct create_lease, lcontext));
1478 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context));
1479 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1480 (struct create_lease, Name));
1481 buf->ccontext.NameLength = cpu_to_le16(4);
1482 /* SMB2_CREATE_REQUEST_LEASE is "RqLs" */
1483 buf->Name[0] = 'R';
1484 buf->Name[1] = 'q';
1485 buf->Name[2] = 'L';
1486 buf->Name[3] = 's';
1487 return (char *)buf;
1488 }
1489
1490 static char *
1491 smb3_create_lease_buf(u8 *lease_key, u8 oplock)
1492 {
1493 struct create_lease_v2 *buf;
1494
1495 buf = kzalloc(sizeof(struct create_lease_v2), GFP_KERNEL);
1496 if (!buf)
1497 return NULL;
1498
1499 buf->lcontext.LeaseKeyLow = cpu_to_le64(*((u64 *)lease_key));
1500 buf->lcontext.LeaseKeyHigh = cpu_to_le64(*((u64 *)(lease_key + 8)));
1501 buf->lcontext.LeaseState = map_oplock_to_lease(oplock);
1502
1503 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1504 (struct create_lease_v2, lcontext));
1505 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context_v2));
1506 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1507 (struct create_lease_v2, Name));
1508 buf->ccontext.NameLength = cpu_to_le16(4);
1509 /* SMB2_CREATE_REQUEST_LEASE is "RqLs" */
1510 buf->Name[0] = 'R';
1511 buf->Name[1] = 'q';
1512 buf->Name[2] = 'L';
1513 buf->Name[3] = 's';
1514 return (char *)buf;
1515 }
1516
1517 static __u8
1518 smb2_parse_lease_buf(void *buf, unsigned int *epoch)
1519 {
1520 struct create_lease *lc = (struct create_lease *)buf;
1521
1522 *epoch = 0; /* not used */
1523 if (lc->lcontext.LeaseFlags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS)
1524 return SMB2_OPLOCK_LEVEL_NOCHANGE;
1525 return le32_to_cpu(lc->lcontext.LeaseState);
1526 }
1527
1528 static __u8
1529 smb3_parse_lease_buf(void *buf, unsigned int *epoch)
1530 {
1531 struct create_lease_v2 *lc = (struct create_lease_v2 *)buf;
1532
1533 *epoch = le16_to_cpu(lc->lcontext.Epoch);
1534 if (lc->lcontext.LeaseFlags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS)
1535 return SMB2_OPLOCK_LEVEL_NOCHANGE;
1536 return le32_to_cpu(lc->lcontext.LeaseState);
1537 }
1538
1539 static unsigned int
1540 smb2_wp_retry_size(struct inode *inode)
1541 {
1542 return min_t(unsigned int, CIFS_SB(inode->i_sb)->wsize,
1543 SMB2_MAX_BUFFER_SIZE);
1544 }
1545
1546 static bool
1547 smb2_dir_needs_close(struct cifsFileInfo *cfile)
1548 {
1549 return !cfile->invalidHandle;
1550 }
1551
1552 static void
1553 fill_transform_hdr(struct smb2_transform_hdr *tr_hdr, struct smb_rqst *old_rq)
1554 {
1555 struct smb2_sync_hdr *shdr =
1556 (struct smb2_sync_hdr *)old_rq->rq_iov[1].iov_base;
1557 unsigned int orig_len = get_rfc1002_length(old_rq->rq_iov[0].iov_base);
1558
1559 memset(tr_hdr, 0, sizeof(struct smb2_transform_hdr));
1560 tr_hdr->ProtocolId = SMB2_TRANSFORM_PROTO_NUM;
1561 tr_hdr->OriginalMessageSize = cpu_to_le32(orig_len);
1562 tr_hdr->Flags = cpu_to_le16(0x01);
1563 get_random_bytes(&tr_hdr->Nonce, SMB3_AES128CMM_NONCE);
1564 memcpy(&tr_hdr->SessionId, &shdr->SessionId, 8);
1565 inc_rfc1001_len(tr_hdr, sizeof(struct smb2_transform_hdr) - 4);
1566 inc_rfc1001_len(tr_hdr, orig_len);
1567 }
1568
1569 static struct scatterlist *
1570 init_sg(struct smb_rqst *rqst, u8 *sign)
1571 {
1572 unsigned int sg_len = rqst->rq_nvec + rqst->rq_npages + 1;
1573 unsigned int assoc_data_len = sizeof(struct smb2_transform_hdr) - 24;
1574 struct scatterlist *sg;
1575 unsigned int i;
1576 unsigned int j;
1577
1578 sg = kmalloc_array(sg_len, sizeof(struct scatterlist), GFP_KERNEL);
1579 if (!sg)
1580 return NULL;
1581
1582 sg_init_table(sg, sg_len);
1583 sg_set_buf(&sg[0], rqst->rq_iov[0].iov_base + 24, assoc_data_len);
1584 for (i = 1; i < rqst->rq_nvec; i++)
1585 sg_set_buf(&sg[i], rqst->rq_iov[i].iov_base,
1586 rqst->rq_iov[i].iov_len);
1587 for (j = 0; i < sg_len - 1; i++, j++) {
1588 unsigned int len = (j < rqst->rq_npages - 1) ? rqst->rq_pagesz
1589 : rqst->rq_tailsz;
1590 sg_set_page(&sg[i], rqst->rq_pages[j], len, 0);
1591 }
1592 sg_set_buf(&sg[sg_len - 1], sign, SMB2_SIGNATURE_SIZE);
1593 return sg;
1594 }
1595
1596 struct cifs_crypt_result {
1597 int err;
1598 struct completion completion;
1599 };
1600
1601 static void cifs_crypt_complete(struct crypto_async_request *req, int err)
1602 {
1603 struct cifs_crypt_result *res = req->data;
1604
1605 if (err == -EINPROGRESS)
1606 return;
1607
1608 res->err = err;
1609 complete(&res->completion);
1610 }
1611
1612 /*
1613 * Encrypt or decrypt @rqst message. @rqst has the following format:
1614 * iov[0] - transform header (associate data),
1615 * iov[1-N] and pages - data to encrypt.
1616 * On success return encrypted data in iov[1-N] and pages, leave iov[0]
1617 * untouched.
1618 */
1619 static int
1620 crypt_message(struct TCP_Server_Info *server, struct smb_rqst *rqst, int enc)
1621 {
1622 struct smb2_transform_hdr *tr_hdr =
1623 (struct smb2_transform_hdr *)rqst->rq_iov[0].iov_base;
1624 unsigned int assoc_data_len = sizeof(struct smb2_transform_hdr) - 24;
1625 struct cifs_ses *ses;
1626 int rc = 0;
1627 struct scatterlist *sg;
1628 u8 sign[SMB2_SIGNATURE_SIZE] = {};
1629 struct aead_request *req;
1630 char *iv;
1631 unsigned int iv_len;
1632 struct cifs_crypt_result result = {0, };
1633 struct crypto_aead *tfm;
1634 unsigned int crypt_len = le32_to_cpu(tr_hdr->OriginalMessageSize);
1635
1636 init_completion(&result.completion);
1637
1638 ses = smb2_find_smb_ses(server, tr_hdr->SessionId);
1639 if (!ses) {
1640 cifs_dbg(VFS, "%s: Could not find session\n", __func__);
1641 return 0;
1642 }
1643
1644 rc = smb3_crypto_aead_allocate(server);
1645 if (rc) {
1646 cifs_dbg(VFS, "%s: crypto alloc failed\n", __func__);
1647 return rc;
1648 }
1649
1650 tfm = enc ? server->secmech.ccmaesencrypt :
1651 server->secmech.ccmaesdecrypt;
1652 rc = crypto_aead_setkey(tfm, enc ? ses->smb3encryptionkey :
1653 ses->smb3decryptionkey, SMB3_SIGN_KEY_SIZE);
1654 if (rc) {
1655 cifs_dbg(VFS, "%s: Failed to set aead key %d\n", __func__, rc);
1656 return rc;
1657 }
1658
1659 rc = crypto_aead_setauthsize(tfm, SMB2_SIGNATURE_SIZE);
1660 if (rc) {
1661 cifs_dbg(VFS, "%s: Failed to set authsize %d\n", __func__, rc);
1662 return rc;
1663 }
1664
1665 req = aead_request_alloc(tfm, GFP_KERNEL);
1666 if (!req) {
1667 cifs_dbg(VFS, "%s: Failed to alloc aead request", __func__);
1668 return -ENOMEM;
1669 }
1670
1671 if (!enc) {
1672 memcpy(sign, &tr_hdr->Signature, SMB2_SIGNATURE_SIZE);
1673 crypt_len += SMB2_SIGNATURE_SIZE;
1674 }
1675
1676 sg = init_sg(rqst, sign);
1677 if (!sg) {
1678 cifs_dbg(VFS, "%s: Failed to init sg %d", __func__, rc);
1679 goto free_req;
1680 }
1681
1682 iv_len = crypto_aead_ivsize(tfm);
1683 iv = kzalloc(iv_len, GFP_KERNEL);
1684 if (!iv) {
1685 cifs_dbg(VFS, "%s: Failed to alloc IV", __func__);
1686 goto free_sg;
1687 }
1688 iv[0] = 3;
1689 memcpy(iv + 1, (char *)tr_hdr->Nonce, SMB3_AES128CMM_NONCE);
1690
1691 aead_request_set_crypt(req, sg, sg, crypt_len, iv);
1692 aead_request_set_ad(req, assoc_data_len);
1693
1694 aead_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
1695 cifs_crypt_complete, &result);
1696
1697 rc = enc ? crypto_aead_encrypt(req) : crypto_aead_decrypt(req);
1698
1699 if (rc == -EINPROGRESS || rc == -EBUSY) {
1700 wait_for_completion(&result.completion);
1701 rc = result.err;
1702 }
1703
1704 if (!rc && enc)
1705 memcpy(&tr_hdr->Signature, sign, SMB2_SIGNATURE_SIZE);
1706
1707 kfree(iv);
1708 free_sg:
1709 kfree(sg);
1710 free_req:
1711 kfree(req);
1712 return rc;
1713 }
1714
1715 static int
1716 smb3_init_transform_rq(struct TCP_Server_Info *server, struct smb_rqst *new_rq,
1717 struct smb_rqst *old_rq)
1718 {
1719 struct kvec *iov;
1720 struct page **pages;
1721 struct smb2_transform_hdr *tr_hdr;
1722 unsigned int npages = old_rq->rq_npages;
1723 int i;
1724 int rc = -ENOMEM;
1725
1726 pages = kmalloc_array(npages, sizeof(struct page *), GFP_KERNEL);
1727 if (!pages)
1728 return rc;
1729
1730 new_rq->rq_pages = pages;
1731 new_rq->rq_npages = old_rq->rq_npages;
1732 new_rq->rq_pagesz = old_rq->rq_pagesz;
1733 new_rq->rq_tailsz = old_rq->rq_tailsz;
1734
1735 for (i = 0; i < npages; i++) {
1736 pages[i] = alloc_page(GFP_KERNEL|__GFP_HIGHMEM);
1737 if (!pages[i])
1738 goto err_free_pages;
1739 }
1740
1741 iov = kmalloc_array(old_rq->rq_nvec, sizeof(struct kvec), GFP_KERNEL);
1742 if (!iov)
1743 goto err_free_pages;
1744
1745 /* copy all iovs from the old except the 1st one (rfc1002 length) */
1746 memcpy(&iov[1], &old_rq->rq_iov[1],
1747 sizeof(struct kvec) * (old_rq->rq_nvec - 1));
1748 new_rq->rq_iov = iov;
1749 new_rq->rq_nvec = old_rq->rq_nvec;
1750
1751 tr_hdr = kmalloc(sizeof(struct smb2_transform_hdr), GFP_KERNEL);
1752 if (!tr_hdr)
1753 goto err_free_iov;
1754
1755 /* fill the 1st iov with a transform header */
1756 fill_transform_hdr(tr_hdr, old_rq);
1757 new_rq->rq_iov[0].iov_base = tr_hdr;
1758 new_rq->rq_iov[0].iov_len = sizeof(struct smb2_transform_hdr);
1759
1760 /* copy pages form the old */
1761 for (i = 0; i < npages; i++) {
1762 char *dst = kmap(new_rq->rq_pages[i]);
1763 char *src = kmap(old_rq->rq_pages[i]);
1764 unsigned int len = (i < npages - 1) ? new_rq->rq_pagesz :
1765 new_rq->rq_tailsz;
1766 memcpy(dst, src, len);
1767 kunmap(new_rq->rq_pages[i]);
1768 kunmap(old_rq->rq_pages[i]);
1769 }
1770
1771 rc = crypt_message(server, new_rq, 1);
1772 cifs_dbg(FYI, "encrypt message returned %d", rc);
1773 if (rc)
1774 goto err_free_tr_hdr;
1775
1776 return rc;
1777
1778 err_free_tr_hdr:
1779 kfree(tr_hdr);
1780 err_free_iov:
1781 kfree(iov);
1782 err_free_pages:
1783 for (i = i - 1; i >= 0; i--)
1784 put_page(pages[i]);
1785 kfree(pages);
1786 return rc;
1787 }
1788
1789 static void
1790 smb3_free_transform_rq(struct smb_rqst *rqst)
1791 {
1792 int i = rqst->rq_npages - 1;
1793
1794 for (; i >= 0; i--)
1795 put_page(rqst->rq_pages[i]);
1796 kfree(rqst->rq_pages);
1797 /* free transform header */
1798 kfree(rqst->rq_iov[0].iov_base);
1799 kfree(rqst->rq_iov);
1800 }
1801
1802 static int
1803 smb3_is_transform_hdr(void *buf)
1804 {
1805 struct smb2_transform_hdr *trhdr = buf;
1806
1807 return trhdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM;
1808 }
1809
1810 static int
1811 decrypt_raw_data(struct TCP_Server_Info *server, char *buf,
1812 unsigned int buf_data_size, struct page **pages,
1813 unsigned int npages, unsigned int page_data_size)
1814 {
1815 struct kvec iov[2];
1816 struct smb_rqst rqst = {NULL};
1817 struct smb2_hdr *hdr;
1818 int rc;
1819
1820 iov[0].iov_base = buf;
1821 iov[0].iov_len = sizeof(struct smb2_transform_hdr);
1822 iov[1].iov_base = buf + sizeof(struct smb2_transform_hdr);
1823 iov[1].iov_len = buf_data_size;
1824
1825 rqst.rq_iov = iov;
1826 rqst.rq_nvec = 2;
1827 rqst.rq_pages = pages;
1828 rqst.rq_npages = npages;
1829 rqst.rq_pagesz = PAGE_SIZE;
1830 rqst.rq_tailsz = (page_data_size % PAGE_SIZE) ? : PAGE_SIZE;
1831
1832 rc = crypt_message(server, &rqst, 0);
1833 cifs_dbg(FYI, "decrypt message returned %d\n", rc);
1834
1835 if (rc)
1836 return rc;
1837
1838 memmove(buf + 4, iov[1].iov_base, buf_data_size);
1839 hdr = (struct smb2_hdr *)buf;
1840 hdr->smb2_buf_length = cpu_to_be32(buf_data_size + page_data_size);
1841 server->total_read = buf_data_size + page_data_size + 4;
1842
1843 return rc;
1844 }
1845
1846 static int
1847 read_data_into_pages(struct TCP_Server_Info *server, struct page **pages,
1848 unsigned int npages, unsigned int len)
1849 {
1850 int i;
1851 int length;
1852
1853 for (i = 0; i < npages; i++) {
1854 struct page *page = pages[i];
1855 size_t n;
1856
1857 n = len;
1858 if (len >= PAGE_SIZE) {
1859 /* enough data to fill the page */
1860 n = PAGE_SIZE;
1861 len -= n;
1862 } else {
1863 zero_user(page, len, PAGE_SIZE - len);
1864 len = 0;
1865 }
1866 length = cifs_read_page_from_socket(server, page, n);
1867 if (length < 0)
1868 return length;
1869 server->total_read += length;
1870 }
1871
1872 return 0;
1873 }
1874
1875 static int
1876 init_read_bvec(struct page **pages, unsigned int npages, unsigned int data_size,
1877 unsigned int cur_off, struct bio_vec **page_vec)
1878 {
1879 struct bio_vec *bvec;
1880 int i;
1881
1882 bvec = kcalloc(npages, sizeof(struct bio_vec), GFP_KERNEL);
1883 if (!bvec)
1884 return -ENOMEM;
1885
1886 for (i = 0; i < npages; i++) {
1887 bvec[i].bv_page = pages[i];
1888 bvec[i].bv_offset = (i == 0) ? cur_off : 0;
1889 bvec[i].bv_len = min_t(unsigned int, PAGE_SIZE, data_size);
1890 data_size -= bvec[i].bv_len;
1891 }
1892
1893 if (data_size != 0) {
1894 cifs_dbg(VFS, "%s: something went wrong\n", __func__);
1895 kfree(bvec);
1896 return -EIO;
1897 }
1898
1899 *page_vec = bvec;
1900 return 0;
1901 }
1902
1903 static int
1904 handle_read_data(struct TCP_Server_Info *server, struct mid_q_entry *mid,
1905 char *buf, unsigned int buf_len, struct page **pages,
1906 unsigned int npages, unsigned int page_data_size)
1907 {
1908 unsigned int data_offset;
1909 unsigned int data_len;
1910 unsigned int cur_off;
1911 unsigned int cur_page_idx;
1912 unsigned int pad_len;
1913 struct cifs_readdata *rdata = mid->callback_data;
1914 struct smb2_sync_hdr *shdr = get_sync_hdr(buf);
1915 struct bio_vec *bvec = NULL;
1916 struct iov_iter iter;
1917 struct kvec iov;
1918 int length;
1919
1920 if (shdr->Command != SMB2_READ) {
1921 cifs_dbg(VFS, "only big read responses are supported\n");
1922 return -ENOTSUPP;
1923 }
1924
1925 if (server->ops->is_status_pending &&
1926 server->ops->is_status_pending(buf, server, 0))
1927 return -1;
1928
1929 rdata->result = server->ops->map_error(buf, false);
1930 if (rdata->result != 0) {
1931 cifs_dbg(FYI, "%s: server returned error %d\n",
1932 __func__, rdata->result);
1933 dequeue_mid(mid, rdata->result);
1934 return 0;
1935 }
1936
1937 data_offset = server->ops->read_data_offset(buf) + 4;
1938 data_len = server->ops->read_data_length(buf);
1939
1940 if (data_offset < server->vals->read_rsp_size) {
1941 /*
1942 * win2k8 sometimes sends an offset of 0 when the read
1943 * is beyond the EOF. Treat it as if the data starts just after
1944 * the header.
1945 */
1946 cifs_dbg(FYI, "%s: data offset (%u) inside read response header\n",
1947 __func__, data_offset);
1948 data_offset = server->vals->read_rsp_size;
1949 } else if (data_offset > MAX_CIFS_SMALL_BUFFER_SIZE) {
1950 /* data_offset is beyond the end of smallbuf */
1951 cifs_dbg(FYI, "%s: data offset (%u) beyond end of smallbuf\n",
1952 __func__, data_offset);
1953 rdata->result = -EIO;
1954 dequeue_mid(mid, rdata->result);
1955 return 0;
1956 }
1957
1958 pad_len = data_offset - server->vals->read_rsp_size;
1959
1960 if (buf_len <= data_offset) {
1961 /* read response payload is in pages */
1962 cur_page_idx = pad_len / PAGE_SIZE;
1963 cur_off = pad_len % PAGE_SIZE;
1964
1965 if (cur_page_idx != 0) {
1966 /* data offset is beyond the 1st page of response */
1967 cifs_dbg(FYI, "%s: data offset (%u) beyond 1st page of response\n",
1968 __func__, data_offset);
1969 rdata->result = -EIO;
1970 dequeue_mid(mid, rdata->result);
1971 return 0;
1972 }
1973
1974 if (data_len > page_data_size - pad_len) {
1975 /* data_len is corrupt -- discard frame */
1976 rdata->result = -EIO;
1977 dequeue_mid(mid, rdata->result);
1978 return 0;
1979 }
1980
1981 rdata->result = init_read_bvec(pages, npages, page_data_size,
1982 cur_off, &bvec);
1983 if (rdata->result != 0) {
1984 dequeue_mid(mid, rdata->result);
1985 return 0;
1986 }
1987
1988 iov_iter_bvec(&iter, WRITE | ITER_BVEC, bvec, npages, data_len);
1989 } else if (buf_len >= data_offset + data_len) {
1990 /* read response payload is in buf */
1991 WARN_ONCE(npages > 0, "read data can be either in buf or in pages");
1992 iov.iov_base = buf + data_offset;
1993 iov.iov_len = data_len;
1994 iov_iter_kvec(&iter, WRITE | ITER_KVEC, &iov, 1, data_len);
1995 } else {
1996 /* read response payload cannot be in both buf and pages */
1997 WARN_ONCE(1, "buf can not contain only a part of read data");
1998 rdata->result = -EIO;
1999 dequeue_mid(mid, rdata->result);
2000 return 0;
2001 }
2002
2003 /* set up first iov for signature check */
2004 rdata->iov[0].iov_base = buf;
2005 rdata->iov[0].iov_len = 4;
2006 rdata->iov[1].iov_base = buf + 4;
2007 rdata->iov[1].iov_len = server->vals->read_rsp_size - 4;
2008 cifs_dbg(FYI, "0: iov_base=%p iov_len=%zu\n",
2009 rdata->iov[0].iov_base, server->vals->read_rsp_size);
2010
2011 length = rdata->copy_into_pages(server, rdata, &iter);
2012
2013 kfree(bvec);
2014
2015 if (length < 0)
2016 return length;
2017
2018 dequeue_mid(mid, false);
2019 return length;
2020 }
2021
2022 static int
2023 receive_encrypted_read(struct TCP_Server_Info *server, struct mid_q_entry **mid)
2024 {
2025 char *buf = server->smallbuf;
2026 struct smb2_transform_hdr *tr_hdr = (struct smb2_transform_hdr *)buf;
2027 unsigned int npages;
2028 struct page **pages;
2029 unsigned int len;
2030 unsigned int buflen = get_rfc1002_length(buf) + 4;
2031 int rc;
2032 int i = 0;
2033
2034 len = min_t(unsigned int, buflen, server->vals->read_rsp_size - 4 +
2035 sizeof(struct smb2_transform_hdr)) - HEADER_SIZE(server) + 1;
2036
2037 rc = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1, len);
2038 if (rc < 0)
2039 return rc;
2040 server->total_read += rc;
2041
2042 len = le32_to_cpu(tr_hdr->OriginalMessageSize) + 4 -
2043 server->vals->read_rsp_size;
2044 npages = DIV_ROUND_UP(len, PAGE_SIZE);
2045
2046 pages = kmalloc_array(npages, sizeof(struct page *), GFP_KERNEL);
2047 if (!pages) {
2048 rc = -ENOMEM;
2049 goto discard_data;
2050 }
2051
2052 for (; i < npages; i++) {
2053 pages[i] = alloc_page(GFP_KERNEL|__GFP_HIGHMEM);
2054 if (!pages[i]) {
2055 rc = -ENOMEM;
2056 goto discard_data;
2057 }
2058 }
2059
2060 /* read read data into pages */
2061 rc = read_data_into_pages(server, pages, npages, len);
2062 if (rc)
2063 goto free_pages;
2064
2065 rc = cifs_discard_remaining_data(server);
2066 if (rc)
2067 goto free_pages;
2068
2069 rc = decrypt_raw_data(server, buf, server->vals->read_rsp_size - 4,
2070 pages, npages, len);
2071 if (rc)
2072 goto free_pages;
2073
2074 *mid = smb2_find_mid(server, buf);
2075 if (*mid == NULL)
2076 cifs_dbg(FYI, "mid not found\n");
2077 else {
2078 cifs_dbg(FYI, "mid found\n");
2079 (*mid)->decrypted = true;
2080 rc = handle_read_data(server, *mid, buf,
2081 server->vals->read_rsp_size,
2082 pages, npages, len);
2083 }
2084
2085 free_pages:
2086 for (i = i - 1; i >= 0; i--)
2087 put_page(pages[i]);
2088 kfree(pages);
2089 return rc;
2090 discard_data:
2091 cifs_discard_remaining_data(server);
2092 goto free_pages;
2093 }
2094
2095 static int
2096 receive_encrypted_standard(struct TCP_Server_Info *server,
2097 struct mid_q_entry **mid)
2098 {
2099 int length;
2100 char *buf = server->smallbuf;
2101 unsigned int pdu_length = get_rfc1002_length(buf);
2102 unsigned int buf_size;
2103 struct mid_q_entry *mid_entry;
2104
2105 /* switch to large buffer if too big for a small one */
2106 if (pdu_length + 4 > MAX_CIFS_SMALL_BUFFER_SIZE) {
2107 server->large_buf = true;
2108 memcpy(server->bigbuf, buf, server->total_read);
2109 buf = server->bigbuf;
2110 }
2111
2112 /* now read the rest */
2113 length = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1,
2114 pdu_length - HEADER_SIZE(server) + 1 + 4);
2115 if (length < 0)
2116 return length;
2117 server->total_read += length;
2118
2119 buf_size = pdu_length + 4 - sizeof(struct smb2_transform_hdr);
2120 length = decrypt_raw_data(server, buf, buf_size, NULL, 0, 0);
2121 if (length)
2122 return length;
2123
2124 mid_entry = smb2_find_mid(server, buf);
2125 if (mid_entry == NULL)
2126 cifs_dbg(FYI, "mid not found\n");
2127 else {
2128 cifs_dbg(FYI, "mid found\n");
2129 mid_entry->decrypted = true;
2130 }
2131
2132 *mid = mid_entry;
2133
2134 if (mid_entry && mid_entry->handle)
2135 return mid_entry->handle(server, mid_entry);
2136
2137 return cifs_handle_standard(server, mid_entry);
2138 }
2139
2140 static int
2141 smb3_receive_transform(struct TCP_Server_Info *server, struct mid_q_entry **mid)
2142 {
2143 char *buf = server->smallbuf;
2144 unsigned int pdu_length = get_rfc1002_length(buf);
2145 struct smb2_transform_hdr *tr_hdr = (struct smb2_transform_hdr *)buf;
2146 unsigned int orig_len = le32_to_cpu(tr_hdr->OriginalMessageSize);
2147
2148 if (pdu_length + 4 < sizeof(struct smb2_transform_hdr) +
2149 sizeof(struct smb2_sync_hdr)) {
2150 cifs_dbg(VFS, "Transform message is too small (%u)\n",
2151 pdu_length);
2152 cifs_reconnect(server);
2153 wake_up(&server->response_q);
2154 return -ECONNABORTED;
2155 }
2156
2157 if (pdu_length + 4 < orig_len + sizeof(struct smb2_transform_hdr)) {
2158 cifs_dbg(VFS, "Transform message is broken\n");
2159 cifs_reconnect(server);
2160 wake_up(&server->response_q);
2161 return -ECONNABORTED;
2162 }
2163
2164 if (pdu_length + 4 > CIFSMaxBufSize + MAX_HEADER_SIZE(server))
2165 return receive_encrypted_read(server, mid);
2166
2167 return receive_encrypted_standard(server, mid);
2168 }
2169
2170 int
2171 smb3_handle_read_data(struct TCP_Server_Info *server, struct mid_q_entry *mid)
2172 {
2173 char *buf = server->large_buf ? server->bigbuf : server->smallbuf;
2174
2175 return handle_read_data(server, mid, buf, get_rfc1002_length(buf) + 4,
2176 NULL, 0, 0);
2177 }
2178
2179 struct smb_version_operations smb20_operations = {
2180 .compare_fids = smb2_compare_fids,
2181 .setup_request = smb2_setup_request,
2182 .setup_async_request = smb2_setup_async_request,
2183 .check_receive = smb2_check_receive,
2184 .add_credits = smb2_add_credits,
2185 .set_credits = smb2_set_credits,
2186 .get_credits_field = smb2_get_credits_field,
2187 .get_credits = smb2_get_credits,
2188 .wait_mtu_credits = cifs_wait_mtu_credits,
2189 .get_next_mid = smb2_get_next_mid,
2190 .read_data_offset = smb2_read_data_offset,
2191 .read_data_length = smb2_read_data_length,
2192 .map_error = map_smb2_to_linux_error,
2193 .find_mid = smb2_find_mid,
2194 .check_message = smb2_check_message,
2195 .dump_detail = smb2_dump_detail,
2196 .clear_stats = smb2_clear_stats,
2197 .print_stats = smb2_print_stats,
2198 .is_oplock_break = smb2_is_valid_oplock_break,
2199 .downgrade_oplock = smb2_downgrade_oplock,
2200 .need_neg = smb2_need_neg,
2201 .negotiate = smb2_negotiate,
2202 .negotiate_wsize = smb2_negotiate_wsize,
2203 .negotiate_rsize = smb2_negotiate_rsize,
2204 .sess_setup = SMB2_sess_setup,
2205 .logoff = SMB2_logoff,
2206 .tree_connect = SMB2_tcon,
2207 .tree_disconnect = SMB2_tdis,
2208 .qfs_tcon = smb2_qfs_tcon,
2209 .is_path_accessible = smb2_is_path_accessible,
2210 .can_echo = smb2_can_echo,
2211 .echo = SMB2_echo,
2212 .query_path_info = smb2_query_path_info,
2213 .get_srv_inum = smb2_get_srv_inum,
2214 .query_file_info = smb2_query_file_info,
2215 .set_path_size = smb2_set_path_size,
2216 .set_file_size = smb2_set_file_size,
2217 .set_file_info = smb2_set_file_info,
2218 .set_compression = smb2_set_compression,
2219 .mkdir = smb2_mkdir,
2220 .mkdir_setinfo = smb2_mkdir_setinfo,
2221 .rmdir = smb2_rmdir,
2222 .unlink = smb2_unlink,
2223 .rename = smb2_rename_path,
2224 .create_hardlink = smb2_create_hardlink,
2225 .query_symlink = smb2_query_symlink,
2226 .query_mf_symlink = smb3_query_mf_symlink,
2227 .create_mf_symlink = smb3_create_mf_symlink,
2228 .open = smb2_open_file,
2229 .set_fid = smb2_set_fid,
2230 .close = smb2_close_file,
2231 .flush = smb2_flush_file,
2232 .async_readv = smb2_async_readv,
2233 .async_writev = smb2_async_writev,
2234 .sync_read = smb2_sync_read,
2235 .sync_write = smb2_sync_write,
2236 .query_dir_first = smb2_query_dir_first,
2237 .query_dir_next = smb2_query_dir_next,
2238 .close_dir = smb2_close_dir,
2239 .calc_smb_size = smb2_calc_size,
2240 .is_status_pending = smb2_is_status_pending,
2241 .oplock_response = smb2_oplock_response,
2242 .queryfs = smb2_queryfs,
2243 .mand_lock = smb2_mand_lock,
2244 .mand_unlock_range = smb2_unlock_range,
2245 .push_mand_locks = smb2_push_mandatory_locks,
2246 .get_lease_key = smb2_get_lease_key,
2247 .set_lease_key = smb2_set_lease_key,
2248 .new_lease_key = smb2_new_lease_key,
2249 .calc_signature = smb2_calc_signature,
2250 .is_read_op = smb2_is_read_op,
2251 .set_oplock_level = smb2_set_oplock_level,
2252 .create_lease_buf = smb2_create_lease_buf,
2253 .parse_lease_buf = smb2_parse_lease_buf,
2254 .clone_range = smb2_clone_range,
2255 .wp_retry_size = smb2_wp_retry_size,
2256 .dir_needs_close = smb2_dir_needs_close,
2257 };
2258
2259 struct smb_version_operations smb21_operations = {
2260 .compare_fids = smb2_compare_fids,
2261 .setup_request = smb2_setup_request,
2262 .setup_async_request = smb2_setup_async_request,
2263 .check_receive = smb2_check_receive,
2264 .add_credits = smb2_add_credits,
2265 .set_credits = smb2_set_credits,
2266 .get_credits_field = smb2_get_credits_field,
2267 .get_credits = smb2_get_credits,
2268 .wait_mtu_credits = smb2_wait_mtu_credits,
2269 .get_next_mid = smb2_get_next_mid,
2270 .read_data_offset = smb2_read_data_offset,
2271 .read_data_length = smb2_read_data_length,
2272 .map_error = map_smb2_to_linux_error,
2273 .find_mid = smb2_find_mid,
2274 .check_message = smb2_check_message,
2275 .dump_detail = smb2_dump_detail,
2276 .clear_stats = smb2_clear_stats,
2277 .print_stats = smb2_print_stats,
2278 .is_oplock_break = smb2_is_valid_oplock_break,
2279 .downgrade_oplock = smb2_downgrade_oplock,
2280 .need_neg = smb2_need_neg,
2281 .negotiate = smb2_negotiate,
2282 .negotiate_wsize = smb2_negotiate_wsize,
2283 .negotiate_rsize = smb2_negotiate_rsize,
2284 .sess_setup = SMB2_sess_setup,
2285 .logoff = SMB2_logoff,
2286 .tree_connect = SMB2_tcon,
2287 .tree_disconnect = SMB2_tdis,
2288 .qfs_tcon = smb2_qfs_tcon,
2289 .is_path_accessible = smb2_is_path_accessible,
2290 .can_echo = smb2_can_echo,
2291 .echo = SMB2_echo,
2292 .query_path_info = smb2_query_path_info,
2293 .get_srv_inum = smb2_get_srv_inum,
2294 .query_file_info = smb2_query_file_info,
2295 .set_path_size = smb2_set_path_size,
2296 .set_file_size = smb2_set_file_size,
2297 .set_file_info = smb2_set_file_info,
2298 .set_compression = smb2_set_compression,
2299 .mkdir = smb2_mkdir,
2300 .mkdir_setinfo = smb2_mkdir_setinfo,
2301 .rmdir = smb2_rmdir,
2302 .unlink = smb2_unlink,
2303 .rename = smb2_rename_path,
2304 .create_hardlink = smb2_create_hardlink,
2305 .query_symlink = smb2_query_symlink,
2306 .query_mf_symlink = smb3_query_mf_symlink,
2307 .create_mf_symlink = smb3_create_mf_symlink,
2308 .open = smb2_open_file,
2309 .set_fid = smb2_set_fid,
2310 .close = smb2_close_file,
2311 .flush = smb2_flush_file,
2312 .async_readv = smb2_async_readv,
2313 .async_writev = smb2_async_writev,
2314 .sync_read = smb2_sync_read,
2315 .sync_write = smb2_sync_write,
2316 .query_dir_first = smb2_query_dir_first,
2317 .query_dir_next = smb2_query_dir_next,
2318 .close_dir = smb2_close_dir,
2319 .calc_smb_size = smb2_calc_size,
2320 .is_status_pending = smb2_is_status_pending,
2321 .oplock_response = smb2_oplock_response,
2322 .queryfs = smb2_queryfs,
2323 .mand_lock = smb2_mand_lock,
2324 .mand_unlock_range = smb2_unlock_range,
2325 .push_mand_locks = smb2_push_mandatory_locks,
2326 .get_lease_key = smb2_get_lease_key,
2327 .set_lease_key = smb2_set_lease_key,
2328 .new_lease_key = smb2_new_lease_key,
2329 .calc_signature = smb2_calc_signature,
2330 .is_read_op = smb21_is_read_op,
2331 .set_oplock_level = smb21_set_oplock_level,
2332 .create_lease_buf = smb2_create_lease_buf,
2333 .parse_lease_buf = smb2_parse_lease_buf,
2334 .clone_range = smb2_clone_range,
2335 .wp_retry_size = smb2_wp_retry_size,
2336 .dir_needs_close = smb2_dir_needs_close,
2337 .enum_snapshots = smb3_enum_snapshots,
2338 };
2339
2340 struct smb_version_operations smb30_operations = {
2341 .compare_fids = smb2_compare_fids,
2342 .setup_request = smb2_setup_request,
2343 .setup_async_request = smb2_setup_async_request,
2344 .check_receive = smb2_check_receive,
2345 .add_credits = smb2_add_credits,
2346 .set_credits = smb2_set_credits,
2347 .get_credits_field = smb2_get_credits_field,
2348 .get_credits = smb2_get_credits,
2349 .wait_mtu_credits = smb2_wait_mtu_credits,
2350 .get_next_mid = smb2_get_next_mid,
2351 .read_data_offset = smb2_read_data_offset,
2352 .read_data_length = smb2_read_data_length,
2353 .map_error = map_smb2_to_linux_error,
2354 .find_mid = smb2_find_mid,
2355 .check_message = smb2_check_message,
2356 .dump_detail = smb2_dump_detail,
2357 .clear_stats = smb2_clear_stats,
2358 .print_stats = smb2_print_stats,
2359 .dump_share_caps = smb2_dump_share_caps,
2360 .is_oplock_break = smb2_is_valid_oplock_break,
2361 .downgrade_oplock = smb2_downgrade_oplock,
2362 .need_neg = smb2_need_neg,
2363 .negotiate = smb2_negotiate,
2364 .negotiate_wsize = smb2_negotiate_wsize,
2365 .negotiate_rsize = smb2_negotiate_rsize,
2366 .sess_setup = SMB2_sess_setup,
2367 .logoff = SMB2_logoff,
2368 .tree_connect = SMB2_tcon,
2369 .tree_disconnect = SMB2_tdis,
2370 .qfs_tcon = smb3_qfs_tcon,
2371 .is_path_accessible = smb2_is_path_accessible,
2372 .can_echo = smb2_can_echo,
2373 .echo = SMB2_echo,
2374 .query_path_info = smb2_query_path_info,
2375 .get_srv_inum = smb2_get_srv_inum,
2376 .query_file_info = smb2_query_file_info,
2377 .set_path_size = smb2_set_path_size,
2378 .set_file_size = smb2_set_file_size,
2379 .set_file_info = smb2_set_file_info,
2380 .set_compression = smb2_set_compression,
2381 .mkdir = smb2_mkdir,
2382 .mkdir_setinfo = smb2_mkdir_setinfo,
2383 .rmdir = smb2_rmdir,
2384 .unlink = smb2_unlink,
2385 .rename = smb2_rename_path,
2386 .create_hardlink = smb2_create_hardlink,
2387 .query_symlink = smb2_query_symlink,
2388 .query_mf_symlink = smb3_query_mf_symlink,
2389 .create_mf_symlink = smb3_create_mf_symlink,
2390 .open = smb2_open_file,
2391 .set_fid = smb2_set_fid,
2392 .close = smb2_close_file,
2393 .flush = smb2_flush_file,
2394 .async_readv = smb2_async_readv,
2395 .async_writev = smb2_async_writev,
2396 .sync_read = smb2_sync_read,
2397 .sync_write = smb2_sync_write,
2398 .query_dir_first = smb2_query_dir_first,
2399 .query_dir_next = smb2_query_dir_next,
2400 .close_dir = smb2_close_dir,
2401 .calc_smb_size = smb2_calc_size,
2402 .is_status_pending = smb2_is_status_pending,
2403 .oplock_response = smb2_oplock_response,
2404 .queryfs = smb2_queryfs,
2405 .mand_lock = smb2_mand_lock,
2406 .mand_unlock_range = smb2_unlock_range,
2407 .push_mand_locks = smb2_push_mandatory_locks,
2408 .get_lease_key = smb2_get_lease_key,
2409 .set_lease_key = smb2_set_lease_key,
2410 .new_lease_key = smb2_new_lease_key,
2411 .generate_signingkey = generate_smb30signingkey,
2412 .calc_signature = smb3_calc_signature,
2413 .set_integrity = smb3_set_integrity,
2414 .is_read_op = smb21_is_read_op,
2415 .set_oplock_level = smb3_set_oplock_level,
2416 .create_lease_buf = smb3_create_lease_buf,
2417 .parse_lease_buf = smb3_parse_lease_buf,
2418 .clone_range = smb2_clone_range,
2419 .duplicate_extents = smb2_duplicate_extents,
2420 .validate_negotiate = smb3_validate_negotiate,
2421 .wp_retry_size = smb2_wp_retry_size,
2422 .dir_needs_close = smb2_dir_needs_close,
2423 .fallocate = smb3_fallocate,
2424 .enum_snapshots = smb3_enum_snapshots,
2425 .init_transform_rq = smb3_init_transform_rq,
2426 .free_transform_rq = smb3_free_transform_rq,
2427 .is_transform_hdr = smb3_is_transform_hdr,
2428 .receive_transform = smb3_receive_transform,
2429 };
2430
2431 #ifdef CONFIG_CIFS_SMB311
2432 struct smb_version_operations smb311_operations = {
2433 .compare_fids = smb2_compare_fids,
2434 .setup_request = smb2_setup_request,
2435 .setup_async_request = smb2_setup_async_request,
2436 .check_receive = smb2_check_receive,
2437 .add_credits = smb2_add_credits,
2438 .set_credits = smb2_set_credits,
2439 .get_credits_field = smb2_get_credits_field,
2440 .get_credits = smb2_get_credits,
2441 .wait_mtu_credits = smb2_wait_mtu_credits,
2442 .get_next_mid = smb2_get_next_mid,
2443 .read_data_offset = smb2_read_data_offset,
2444 .read_data_length = smb2_read_data_length,
2445 .map_error = map_smb2_to_linux_error,
2446 .find_mid = smb2_find_mid,
2447 .check_message = smb2_check_message,
2448 .dump_detail = smb2_dump_detail,
2449 .clear_stats = smb2_clear_stats,
2450 .print_stats = smb2_print_stats,
2451 .dump_share_caps = smb2_dump_share_caps,
2452 .is_oplock_break = smb2_is_valid_oplock_break,
2453 .downgrade_oplock = smb2_downgrade_oplock,
2454 .need_neg = smb2_need_neg,
2455 .negotiate = smb2_negotiate,
2456 .negotiate_wsize = smb2_negotiate_wsize,
2457 .negotiate_rsize = smb2_negotiate_rsize,
2458 .sess_setup = SMB2_sess_setup,
2459 .logoff = SMB2_logoff,
2460 .tree_connect = SMB2_tcon,
2461 .tree_disconnect = SMB2_tdis,
2462 .qfs_tcon = smb3_qfs_tcon,
2463 .is_path_accessible = smb2_is_path_accessible,
2464 .can_echo = smb2_can_echo,
2465 .echo = SMB2_echo,
2466 .query_path_info = smb2_query_path_info,
2467 .get_srv_inum = smb2_get_srv_inum,
2468 .query_file_info = smb2_query_file_info,
2469 .set_path_size = smb2_set_path_size,
2470 .set_file_size = smb2_set_file_size,
2471 .set_file_info = smb2_set_file_info,
2472 .set_compression = smb2_set_compression,
2473 .mkdir = smb2_mkdir,
2474 .mkdir_setinfo = smb2_mkdir_setinfo,
2475 .rmdir = smb2_rmdir,
2476 .unlink = smb2_unlink,
2477 .rename = smb2_rename_path,
2478 .create_hardlink = smb2_create_hardlink,
2479 .query_symlink = smb2_query_symlink,
2480 .query_mf_symlink = smb3_query_mf_symlink,
2481 .create_mf_symlink = smb3_create_mf_symlink,
2482 .open = smb2_open_file,
2483 .set_fid = smb2_set_fid,
2484 .close = smb2_close_file,
2485 .flush = smb2_flush_file,
2486 .async_readv = smb2_async_readv,
2487 .async_writev = smb2_async_writev,
2488 .sync_read = smb2_sync_read,
2489 .sync_write = smb2_sync_write,
2490 .query_dir_first = smb2_query_dir_first,
2491 .query_dir_next = smb2_query_dir_next,
2492 .close_dir = smb2_close_dir,
2493 .calc_smb_size = smb2_calc_size,
2494 .is_status_pending = smb2_is_status_pending,
2495 .oplock_response = smb2_oplock_response,
2496 .queryfs = smb2_queryfs,
2497 .mand_lock = smb2_mand_lock,
2498 .mand_unlock_range = smb2_unlock_range,
2499 .push_mand_locks = smb2_push_mandatory_locks,
2500 .get_lease_key = smb2_get_lease_key,
2501 .set_lease_key = smb2_set_lease_key,
2502 .new_lease_key = smb2_new_lease_key,
2503 .generate_signingkey = generate_smb311signingkey,
2504 .calc_signature = smb3_calc_signature,
2505 .set_integrity = smb3_set_integrity,
2506 .is_read_op = smb21_is_read_op,
2507 .set_oplock_level = smb3_set_oplock_level,
2508 .create_lease_buf = smb3_create_lease_buf,
2509 .parse_lease_buf = smb3_parse_lease_buf,
2510 .clone_range = smb2_clone_range,
2511 .duplicate_extents = smb2_duplicate_extents,
2512 /* .validate_negotiate = smb3_validate_negotiate, */ /* not used in 3.11 */
2513 .wp_retry_size = smb2_wp_retry_size,
2514 .dir_needs_close = smb2_dir_needs_close,
2515 .fallocate = smb3_fallocate,
2516 .enum_snapshots = smb3_enum_snapshots,
2517 .init_transform_rq = smb3_init_transform_rq,
2518 .free_transform_rq = smb3_free_transform_rq,
2519 .is_transform_hdr = smb3_is_transform_hdr,
2520 .receive_transform = smb3_receive_transform,
2521 };
2522 #endif /* CIFS_SMB311 */
2523
2524 struct smb_version_values smb20_values = {
2525 .version_string = SMB20_VERSION_STRING,
2526 .protocol_id = SMB20_PROT_ID,
2527 .req_capabilities = 0, /* MBZ */
2528 .large_lock_type = 0,
2529 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
2530 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
2531 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
2532 .header_size = sizeof(struct smb2_hdr),
2533 .max_header_size = MAX_SMB2_HDR_SIZE,
2534 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
2535 .lock_cmd = SMB2_LOCK,
2536 .cap_unix = 0,
2537 .cap_nt_find = SMB2_NT_FIND,
2538 .cap_large_files = SMB2_LARGE_FILES,
2539 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
2540 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
2541 .create_lease_size = sizeof(struct create_lease),
2542 };
2543
2544 struct smb_version_values smb21_values = {
2545 .version_string = SMB21_VERSION_STRING,
2546 .protocol_id = SMB21_PROT_ID,
2547 .req_capabilities = 0, /* MBZ on negotiate req until SMB3 dialect */
2548 .large_lock_type = 0,
2549 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
2550 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
2551 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
2552 .header_size = sizeof(struct smb2_hdr),
2553 .max_header_size = MAX_SMB2_HDR_SIZE,
2554 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
2555 .lock_cmd = SMB2_LOCK,
2556 .cap_unix = 0,
2557 .cap_nt_find = SMB2_NT_FIND,
2558 .cap_large_files = SMB2_LARGE_FILES,
2559 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
2560 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
2561 .create_lease_size = sizeof(struct create_lease),
2562 };
2563
2564 struct smb_version_values smb30_values = {
2565 .version_string = SMB30_VERSION_STRING,
2566 .protocol_id = SMB30_PROT_ID,
2567 .req_capabilities = SMB2_GLOBAL_CAP_DFS | SMB2_GLOBAL_CAP_LEASING | SMB2_GLOBAL_CAP_LARGE_MTU | SMB2_GLOBAL_CAP_PERSISTENT_HANDLES | SMB2_GLOBAL_CAP_ENCRYPTION,
2568 .large_lock_type = 0,
2569 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
2570 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
2571 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
2572 .header_size = sizeof(struct smb2_hdr),
2573 .max_header_size = MAX_SMB2_HDR_SIZE,
2574 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
2575 .lock_cmd = SMB2_LOCK,
2576 .cap_unix = 0,
2577 .cap_nt_find = SMB2_NT_FIND,
2578 .cap_large_files = SMB2_LARGE_FILES,
2579 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
2580 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
2581 .create_lease_size = sizeof(struct create_lease_v2),
2582 };
2583
2584 struct smb_version_values smb302_values = {
2585 .version_string = SMB302_VERSION_STRING,
2586 .protocol_id = SMB302_PROT_ID,
2587 .req_capabilities = SMB2_GLOBAL_CAP_DFS | SMB2_GLOBAL_CAP_LEASING | SMB2_GLOBAL_CAP_LARGE_MTU | SMB2_GLOBAL_CAP_PERSISTENT_HANDLES | SMB2_GLOBAL_CAP_ENCRYPTION,
2588 .large_lock_type = 0,
2589 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
2590 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
2591 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
2592 .header_size = sizeof(struct smb2_hdr),
2593 .max_header_size = MAX_SMB2_HDR_SIZE,
2594 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
2595 .lock_cmd = SMB2_LOCK,
2596 .cap_unix = 0,
2597 .cap_nt_find = SMB2_NT_FIND,
2598 .cap_large_files = SMB2_LARGE_FILES,
2599 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
2600 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
2601 .create_lease_size = sizeof(struct create_lease_v2),
2602 };
2603
2604 #ifdef CONFIG_CIFS_SMB311
2605 struct smb_version_values smb311_values = {
2606 .version_string = SMB311_VERSION_STRING,
2607 .protocol_id = SMB311_PROT_ID,
2608 .req_capabilities = SMB2_GLOBAL_CAP_DFS | SMB2_GLOBAL_CAP_LEASING | SMB2_GLOBAL_CAP_LARGE_MTU | SMB2_GLOBAL_CAP_PERSISTENT_HANDLES,
2609 .large_lock_type = 0,
2610 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
2611 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
2612 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
2613 .header_size = sizeof(struct smb2_hdr),
2614 .max_header_size = MAX_SMB2_HDR_SIZE,
2615 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
2616 .lock_cmd = SMB2_LOCK,
2617 .cap_unix = 0,
2618 .cap_nt_find = SMB2_NT_FIND,
2619 .cap_large_files = SMB2_LARGE_FILES,
2620 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
2621 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
2622 .create_lease_size = sizeof(struct create_lease_v2),
2623 };
2624 #endif /* SMB311 */