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