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