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