~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/fs/cifs/smb2ops.c

Version: ~ [ linux-5.16-rc3 ] ~ [ linux-5.15.5 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.82 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.162 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.218 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.256 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.291 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.293 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.18.140 ] ~ [ linux-3.16.85 ] ~ [ linux-3.14.79 ] ~ [ linux-3.12.74 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp