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