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