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

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

Version: ~ [ linux-5.10-rc5 ] ~ [ linux-5.9.10 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.79 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.159 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.208 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.245 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.245 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.85 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ 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 // SPDX-License-Identifier: GPL-2.0
  2 /*
  3  *  SMB2 version specific operations
  4  *
  5  *  Copyright (c) 2012, Jeff Layton <jlayton@redhat.com>
  6  */
  7 
  8 #include <linux/pagemap.h>
  9 #include <linux/vfs.h>
 10 #include <linux/falloc.h>
 11 #include <linux/scatterlist.h>
 12 #include <linux/uuid.h>
 13 #include <linux/sort.h>
 14 #include <crypto/aead.h>
 15 #include <linux/fiemap.h>
 16 #include "cifsfs.h"
 17 #include "cifsglob.h"
 18 #include "smb2pdu.h"
 19 #include "smb2proto.h"
 20 #include "cifsproto.h"
 21 #include "cifs_debug.h"
 22 #include "cifs_unicode.h"
 23 #include "smb2status.h"
 24 #include "smb2glob.h"
 25 #include "cifs_ioctl.h"
 26 #include "smbdirect.h"
 27 
 28 /* Change credits for different ops and return the total number of credits */
 29 static int
 30 change_conf(struct TCP_Server_Info *server)
 31 {
 32         server->credits += server->echo_credits + server->oplock_credits;
 33         server->oplock_credits = server->echo_credits = 0;
 34         switch (server->credits) {
 35         case 0:
 36                 return 0;
 37         case 1:
 38                 server->echoes = false;
 39                 server->oplocks = false;
 40                 break;
 41         case 2:
 42                 server->echoes = true;
 43                 server->oplocks = false;
 44                 server->echo_credits = 1;
 45                 break;
 46         default:
 47                 server->echoes = true;
 48                 if (enable_oplocks) {
 49                         server->oplocks = true;
 50                         server->oplock_credits = 1;
 51                 } else
 52                         server->oplocks = false;
 53 
 54                 server->echo_credits = 1;
 55         }
 56         server->credits -= server->echo_credits + server->oplock_credits;
 57         return server->credits + server->echo_credits + server->oplock_credits;
 58 }
 59 
 60 static void
 61 smb2_add_credits(struct TCP_Server_Info *server,
 62                  const struct cifs_credits *credits, const int optype)
 63 {
 64         int *val, rc = -1;
 65         unsigned int add = credits->value;
 66         unsigned int instance = credits->instance;
 67         bool reconnect_detected = false;
 68 
 69         spin_lock(&server->req_lock);
 70         val = server->ops->get_credits_field(server, optype);
 71 
 72         /* eg found case where write overlapping reconnect messed up credits */
 73         if (((optype & CIFS_OP_MASK) == CIFS_NEG_OP) && (*val != 0))
 74                 trace_smb3_reconnect_with_invalid_credits(server->CurrentMid,
 75                         server->hostname, *val, add);
 76         if ((instance == 0) || (instance == server->reconnect_instance))
 77                 *val += add;
 78         else
 79                 reconnect_detected = true;
 80 
 81         if (*val > 65000) {
 82                 *val = 65000; /* Don't get near 64K credits, avoid srv bugs */
 83                 pr_warn_once("server overflowed SMB3 credits\n");
 84         }
 85         server->in_flight--;
 86         if (server->in_flight == 0 && (optype & CIFS_OP_MASK) != CIFS_NEG_OP)
 87                 rc = change_conf(server);
 88         /*
 89          * Sometimes server returns 0 credits on oplock break ack - we need to
 90          * rebalance credits in this case.
 91          */
 92         else if (server->in_flight > 0 && server->oplock_credits == 0 &&
 93                  server->oplocks) {
 94                 if (server->credits > 1) {
 95                         server->credits--;
 96                         server->oplock_credits++;
 97                 }
 98         }
 99         spin_unlock(&server->req_lock);
100         wake_up(&server->request_q);
101 
102         if (reconnect_detected)
103                 cifs_dbg(FYI, "trying to put %d credits from the old server instance %d\n",
104                          add, instance);
105 
106         if (server->tcpStatus == CifsNeedReconnect
107             || server->tcpStatus == CifsExiting)
108                 return;
109 
110         switch (rc) {
111         case -1:
112                 /* change_conf hasn't been executed */
113                 break;
114         case 0:
115                 cifs_server_dbg(VFS, "Possible client or server bug - zero credits\n");
116                 break;
117         case 1:
118                 cifs_server_dbg(VFS, "disabling echoes and oplocks\n");
119                 break;
120         case 2:
121                 cifs_dbg(FYI, "disabling oplocks\n");
122                 break;
123         default:
124                 trace_smb3_add_credits(server->CurrentMid,
125                         server->hostname, rc, add);
126                 cifs_dbg(FYI, "add %u credits total=%d\n", add, rc);
127         }
128 }
129 
130 static void
131 smb2_set_credits(struct TCP_Server_Info *server, const int val)
132 {
133         spin_lock(&server->req_lock);
134         server->credits = val;
135         if (val == 1)
136                 server->reconnect_instance++;
137         spin_unlock(&server->req_lock);
138         /* don't log while holding the lock */
139         if (val == 1)
140                 cifs_dbg(FYI, "set credits to 1 due to smb2 reconnect\n");
141 }
142 
143 static int *
144 smb2_get_credits_field(struct TCP_Server_Info *server, const int optype)
145 {
146         switch (optype) {
147         case CIFS_ECHO_OP:
148                 return &server->echo_credits;
149         case CIFS_OBREAK_OP:
150                 return &server->oplock_credits;
151         default:
152                 return &server->credits;
153         }
154 }
155 
156 static unsigned int
157 smb2_get_credits(struct mid_q_entry *mid)
158 {
159         return mid->credits_received;
160 }
161 
162 static int
163 smb2_wait_mtu_credits(struct TCP_Server_Info *server, unsigned int size,
164                       unsigned int *num, struct cifs_credits *credits)
165 {
166         int rc = 0;
167         unsigned int scredits;
168 
169         spin_lock(&server->req_lock);
170         while (1) {
171                 if (server->credits <= 0) {
172                         spin_unlock(&server->req_lock);
173                         cifs_num_waiters_inc(server);
174                         rc = wait_event_killable(server->request_q,
175                                 has_credits(server, &server->credits, 1));
176                         cifs_num_waiters_dec(server);
177                         if (rc)
178                                 return rc;
179                         spin_lock(&server->req_lock);
180                 } else {
181                         if (server->tcpStatus == CifsExiting) {
182                                 spin_unlock(&server->req_lock);
183                                 return -ENOENT;
184                         }
185 
186                         scredits = server->credits;
187                         /* can deadlock with reopen */
188                         if (scredits <= 8) {
189                                 *num = SMB2_MAX_BUFFER_SIZE;
190                                 credits->value = 0;
191                                 credits->instance = 0;
192                                 break;
193                         }
194 
195                         /* leave some credits for reopen and other ops */
196                         scredits -= 8;
197                         *num = min_t(unsigned int, size,
198                                      scredits * SMB2_MAX_BUFFER_SIZE);
199 
200                         credits->value =
201                                 DIV_ROUND_UP(*num, SMB2_MAX_BUFFER_SIZE);
202                         credits->instance = server->reconnect_instance;
203                         server->credits -= credits->value;
204                         server->in_flight++;
205                         if (server->in_flight > server->max_in_flight)
206                                 server->max_in_flight = server->in_flight;
207                         break;
208                 }
209         }
210         spin_unlock(&server->req_lock);
211         return rc;
212 }
213 
214 static int
215 smb2_adjust_credits(struct TCP_Server_Info *server,
216                     struct cifs_credits *credits,
217                     const unsigned int payload_size)
218 {
219         int new_val = DIV_ROUND_UP(payload_size, SMB2_MAX_BUFFER_SIZE);
220 
221         if (!credits->value || credits->value == new_val)
222                 return 0;
223 
224         if (credits->value < new_val) {
225                 WARN_ONCE(1, "request has less credits (%d) than required (%d)",
226                           credits->value, new_val);
227                 return -ENOTSUPP;
228         }
229 
230         spin_lock(&server->req_lock);
231 
232         if (server->reconnect_instance != credits->instance) {
233                 spin_unlock(&server->req_lock);
234                 cifs_server_dbg(VFS, "trying to return %d credits to old session\n",
235                          credits->value - new_val);
236                 return -EAGAIN;
237         }
238 
239         server->credits += credits->value - new_val;
240         spin_unlock(&server->req_lock);
241         wake_up(&server->request_q);
242         credits->value = new_val;
243         return 0;
244 }
245 
246 static __u64
247 smb2_get_next_mid(struct TCP_Server_Info *server)
248 {
249         __u64 mid;
250         /* for SMB2 we need the current value */
251         spin_lock(&GlobalMid_Lock);
252         mid = server->CurrentMid++;
253         spin_unlock(&GlobalMid_Lock);
254         return mid;
255 }
256 
257 static void
258 smb2_revert_current_mid(struct TCP_Server_Info *server, const unsigned int val)
259 {
260         spin_lock(&GlobalMid_Lock);
261         if (server->CurrentMid >= val)
262                 server->CurrentMid -= val;
263         spin_unlock(&GlobalMid_Lock);
264 }
265 
266 static struct mid_q_entry *
267 smb2_find_mid(struct TCP_Server_Info *server, char *buf)
268 {
269         struct mid_q_entry *mid;
270         struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
271         __u64 wire_mid = le64_to_cpu(shdr->MessageId);
272 
273         if (shdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM) {
274                 cifs_server_dbg(VFS, "Encrypted frame parsing not supported yet\n");
275                 return NULL;
276         }
277 
278         spin_lock(&GlobalMid_Lock);
279         list_for_each_entry(mid, &server->pending_mid_q, qhead) {
280                 if ((mid->mid == wire_mid) &&
281                     (mid->mid_state == MID_REQUEST_SUBMITTED) &&
282                     (mid->command == shdr->Command)) {
283                         kref_get(&mid->refcount);
284                         spin_unlock(&GlobalMid_Lock);
285                         return mid;
286                 }
287         }
288         spin_unlock(&GlobalMid_Lock);
289         return NULL;
290 }
291 
292 static void
293 smb2_dump_detail(void *buf, struct TCP_Server_Info *server)
294 {
295 #ifdef CONFIG_CIFS_DEBUG2
296         struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
297 
298         cifs_server_dbg(VFS, "Cmd: %d Err: 0x%x Flags: 0x%x Mid: %llu Pid: %d\n",
299                  shdr->Command, shdr->Status, shdr->Flags, shdr->MessageId,
300                  shdr->ProcessId);
301         cifs_server_dbg(VFS, "smb buf %p len %u\n", buf,
302                  server->ops->calc_smb_size(buf, server));
303 #endif
304 }
305 
306 static bool
307 smb2_need_neg(struct TCP_Server_Info *server)
308 {
309         return server->max_read == 0;
310 }
311 
312 static int
313 smb2_negotiate(const unsigned int xid, struct cifs_ses *ses)
314 {
315         int rc;
316 
317         cifs_ses_server(ses)->CurrentMid = 0;
318         rc = SMB2_negotiate(xid, ses);
319         /* BB we probably don't need to retry with modern servers */
320         if (rc == -EAGAIN)
321                 rc = -EHOSTDOWN;
322         return rc;
323 }
324 
325 static unsigned int
326 smb2_negotiate_wsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
327 {
328         struct TCP_Server_Info *server = tcon->ses->server;
329         unsigned int wsize;
330 
331         /* start with specified wsize, or default */
332         wsize = volume_info->wsize ? volume_info->wsize : CIFS_DEFAULT_IOSIZE;
333         wsize = min_t(unsigned int, wsize, server->max_write);
334         if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
335                 wsize = min_t(unsigned int, wsize, SMB2_MAX_BUFFER_SIZE);
336 
337         return wsize;
338 }
339 
340 static unsigned int
341 smb3_negotiate_wsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
342 {
343         struct TCP_Server_Info *server = tcon->ses->server;
344         unsigned int wsize;
345 
346         /* start with specified wsize, or default */
347         wsize = volume_info->wsize ? volume_info->wsize : SMB3_DEFAULT_IOSIZE;
348         wsize = min_t(unsigned int, wsize, server->max_write);
349 #ifdef CONFIG_CIFS_SMB_DIRECT
350         if (server->rdma) {
351                 if (server->sign)
352                         /*
353                          * Account for SMB2 data transfer packet header and
354                          * possible encryption header
355                          */
356                         wsize = min_t(unsigned int,
357                                 wsize,
358                                 server->smbd_conn->max_fragmented_send_size -
359                                         SMB2_READWRITE_PDU_HEADER_SIZE -
360                                         sizeof(struct smb2_transform_hdr));
361                 else
362                         wsize = min_t(unsigned int,
363                                 wsize, server->smbd_conn->max_readwrite_size);
364         }
365 #endif
366         if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
367                 wsize = min_t(unsigned int, wsize, SMB2_MAX_BUFFER_SIZE);
368 
369         return wsize;
370 }
371 
372 static unsigned int
373 smb2_negotiate_rsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
374 {
375         struct TCP_Server_Info *server = tcon->ses->server;
376         unsigned int rsize;
377 
378         /* start with specified rsize, or default */
379         rsize = volume_info->rsize ? volume_info->rsize : CIFS_DEFAULT_IOSIZE;
380         rsize = min_t(unsigned int, rsize, server->max_read);
381 
382         if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
383                 rsize = min_t(unsigned int, rsize, SMB2_MAX_BUFFER_SIZE);
384 
385         return rsize;
386 }
387 
388 static unsigned int
389 smb3_negotiate_rsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
390 {
391         struct TCP_Server_Info *server = tcon->ses->server;
392         unsigned int rsize;
393 
394         /* start with specified rsize, or default */
395         rsize = volume_info->rsize ? volume_info->rsize : SMB3_DEFAULT_IOSIZE;
396         rsize = min_t(unsigned int, rsize, server->max_read);
397 #ifdef CONFIG_CIFS_SMB_DIRECT
398         if (server->rdma) {
399                 if (server->sign)
400                         /*
401                          * Account for SMB2 data transfer packet header and
402                          * possible encryption header
403                          */
404                         rsize = min_t(unsigned int,
405                                 rsize,
406                                 server->smbd_conn->max_fragmented_recv_size -
407                                         SMB2_READWRITE_PDU_HEADER_SIZE -
408                                         sizeof(struct smb2_transform_hdr));
409                 else
410                         rsize = min_t(unsigned int,
411                                 rsize, server->smbd_conn->max_readwrite_size);
412         }
413 #endif
414 
415         if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
416                 rsize = min_t(unsigned int, rsize, SMB2_MAX_BUFFER_SIZE);
417 
418         return rsize;
419 }
420 
421 static int
422 parse_server_interfaces(struct network_interface_info_ioctl_rsp *buf,
423                         size_t buf_len,
424                         struct cifs_server_iface **iface_list,
425                         size_t *iface_count)
426 {
427         struct network_interface_info_ioctl_rsp *p;
428         struct sockaddr_in *addr4;
429         struct sockaddr_in6 *addr6;
430         struct iface_info_ipv4 *p4;
431         struct iface_info_ipv6 *p6;
432         struct cifs_server_iface *info;
433         ssize_t bytes_left;
434         size_t next = 0;
435         int nb_iface = 0;
436         int rc = 0;
437 
438         *iface_list = NULL;
439         *iface_count = 0;
440 
441         /*
442          * Fist pass: count and sanity check
443          */
444 
445         bytes_left = buf_len;
446         p = buf;
447         while (bytes_left >= sizeof(*p)) {
448                 nb_iface++;
449                 next = le32_to_cpu(p->Next);
450                 if (!next) {
451                         bytes_left -= sizeof(*p);
452                         break;
453                 }
454                 p = (struct network_interface_info_ioctl_rsp *)((u8 *)p+next);
455                 bytes_left -= next;
456         }
457 
458         if (!nb_iface) {
459                 cifs_dbg(VFS, "%s: malformed interface info\n", __func__);
460                 rc = -EINVAL;
461                 goto out;
462         }
463 
464         if (bytes_left || p->Next)
465                 cifs_dbg(VFS, "%s: incomplete interface info\n", __func__);
466 
467 
468         /*
469          * Second pass: extract info to internal structure
470          */
471 
472         *iface_list = kcalloc(nb_iface, sizeof(**iface_list), GFP_KERNEL);
473         if (!*iface_list) {
474                 rc = -ENOMEM;
475                 goto out;
476         }
477 
478         info = *iface_list;
479         bytes_left = buf_len;
480         p = buf;
481         while (bytes_left >= sizeof(*p)) {
482                 info->speed = le64_to_cpu(p->LinkSpeed);
483                 info->rdma_capable = le32_to_cpu(p->Capability & RDMA_CAPABLE);
484                 info->rss_capable = le32_to_cpu(p->Capability & RSS_CAPABLE);
485 
486                 cifs_dbg(FYI, "%s: adding iface %zu\n", __func__, *iface_count);
487                 cifs_dbg(FYI, "%s: speed %zu bps\n", __func__, info->speed);
488                 cifs_dbg(FYI, "%s: capabilities 0x%08x\n", __func__,
489                          le32_to_cpu(p->Capability));
490 
491                 switch (p->Family) {
492                 /*
493                  * The kernel and wire socket structures have the same
494                  * layout and use network byte order but make the
495                  * conversion explicit in case either one changes.
496                  */
497                 case INTERNETWORK:
498                         addr4 = (struct sockaddr_in *)&info->sockaddr;
499                         p4 = (struct iface_info_ipv4 *)p->Buffer;
500                         addr4->sin_family = AF_INET;
501                         memcpy(&addr4->sin_addr, &p4->IPv4Address, 4);
502 
503                         /* [MS-SMB2] 2.2.32.5.1.1 Clients MUST ignore these */
504                         addr4->sin_port = cpu_to_be16(CIFS_PORT);
505 
506                         cifs_dbg(FYI, "%s: ipv4 %pI4\n", __func__,
507                                  &addr4->sin_addr);
508                         break;
509                 case INTERNETWORKV6:
510                         addr6 = (struct sockaddr_in6 *)&info->sockaddr;
511                         p6 = (struct iface_info_ipv6 *)p->Buffer;
512                         addr6->sin6_family = AF_INET6;
513                         memcpy(&addr6->sin6_addr, &p6->IPv6Address, 16);
514 
515                         /* [MS-SMB2] 2.2.32.5.1.2 Clients MUST ignore these */
516                         addr6->sin6_flowinfo = 0;
517                         addr6->sin6_scope_id = 0;
518                         addr6->sin6_port = cpu_to_be16(CIFS_PORT);
519 
520                         cifs_dbg(FYI, "%s: ipv6 %pI6\n", __func__,
521                                  &addr6->sin6_addr);
522                         break;
523                 default:
524                         cifs_dbg(VFS,
525                                  "%s: skipping unsupported socket family\n",
526                                  __func__);
527                         goto next_iface;
528                 }
529 
530                 (*iface_count)++;
531                 info++;
532 next_iface:
533                 next = le32_to_cpu(p->Next);
534                 if (!next)
535                         break;
536                 p = (struct network_interface_info_ioctl_rsp *)((u8 *)p+next);
537                 bytes_left -= next;
538         }
539 
540         if (!*iface_count) {
541                 rc = -EINVAL;
542                 goto out;
543         }
544 
545 out:
546         if (rc) {
547                 kfree(*iface_list);
548                 *iface_count = 0;
549                 *iface_list = NULL;
550         }
551         return rc;
552 }
553 
554 static int compare_iface(const void *ia, const void *ib)
555 {
556         const struct cifs_server_iface *a = (struct cifs_server_iface *)ia;
557         const struct cifs_server_iface *b = (struct cifs_server_iface *)ib;
558 
559         return a->speed == b->speed ? 0 : (a->speed > b->speed ? -1 : 1);
560 }
561 
562 static int
563 SMB3_request_interfaces(const unsigned int xid, struct cifs_tcon *tcon)
564 {
565         int rc;
566         unsigned int ret_data_len = 0;
567         struct network_interface_info_ioctl_rsp *out_buf = NULL;
568         struct cifs_server_iface *iface_list;
569         size_t iface_count;
570         struct cifs_ses *ses = tcon->ses;
571 
572         rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
573                         FSCTL_QUERY_NETWORK_INTERFACE_INFO, true /* is_fsctl */,
574                         NULL /* no data input */, 0 /* no data input */,
575                         CIFSMaxBufSize, (char **)&out_buf, &ret_data_len);
576         if (rc == -EOPNOTSUPP) {
577                 cifs_dbg(FYI,
578                          "server does not support query network interfaces\n");
579                 goto out;
580         } else if (rc != 0) {
581                 cifs_tcon_dbg(VFS, "error %d on ioctl to get interface list\n", rc);
582                 goto out;
583         }
584 
585         rc = parse_server_interfaces(out_buf, ret_data_len,
586                                      &iface_list, &iface_count);
587         if (rc)
588                 goto out;
589 
590         /* sort interfaces from fastest to slowest */
591         sort(iface_list, iface_count, sizeof(*iface_list), compare_iface, NULL);
592 
593         spin_lock(&ses->iface_lock);
594         kfree(ses->iface_list);
595         ses->iface_list = iface_list;
596         ses->iface_count = iface_count;
597         ses->iface_last_update = jiffies;
598         spin_unlock(&ses->iface_lock);
599 
600 out:
601         kfree(out_buf);
602         return rc;
603 }
604 
605 static void
606 smb2_close_cached_fid(struct kref *ref)
607 {
608         struct cached_fid *cfid = container_of(ref, struct cached_fid,
609                                                refcount);
610 
611         if (cfid->is_valid) {
612                 cifs_dbg(FYI, "clear cached root file handle\n");
613                 SMB2_close(0, cfid->tcon, cfid->fid->persistent_fid,
614                            cfid->fid->volatile_fid);
615                 cfid->is_valid = false;
616                 cfid->file_all_info_is_valid = false;
617                 cfid->has_lease = false;
618         }
619 }
620 
621 void close_shroot(struct cached_fid *cfid)
622 {
623         mutex_lock(&cfid->fid_mutex);
624         kref_put(&cfid->refcount, smb2_close_cached_fid);
625         mutex_unlock(&cfid->fid_mutex);
626 }
627 
628 void close_shroot_lease_locked(struct cached_fid *cfid)
629 {
630         if (cfid->has_lease) {
631                 cfid->has_lease = false;
632                 kref_put(&cfid->refcount, smb2_close_cached_fid);
633         }
634 }
635 
636 void close_shroot_lease(struct cached_fid *cfid)
637 {
638         mutex_lock(&cfid->fid_mutex);
639         close_shroot_lease_locked(cfid);
640         mutex_unlock(&cfid->fid_mutex);
641 }
642 
643 void
644 smb2_cached_lease_break(struct work_struct *work)
645 {
646         struct cached_fid *cfid = container_of(work,
647                                 struct cached_fid, lease_break);
648 
649         close_shroot_lease(cfid);
650 }
651 
652 /*
653  * Open the directory at the root of a share
654  */
655 int open_shroot(unsigned int xid, struct cifs_tcon *tcon,
656                 struct cifs_sb_info *cifs_sb,
657                 struct cached_fid **cfid)
658 {
659         struct cifs_ses *ses = tcon->ses;
660         struct TCP_Server_Info *server = ses->server;
661         struct cifs_open_parms oparms;
662         struct smb2_create_rsp *o_rsp = NULL;
663         struct smb2_query_info_rsp *qi_rsp = NULL;
664         int resp_buftype[2];
665         struct smb_rqst rqst[2];
666         struct kvec rsp_iov[2];
667         struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
668         struct kvec qi_iov[1];
669         int rc, flags = 0;
670         __le16 utf16_path = 0; /* Null - since an open of top of share */
671         u8 oplock = SMB2_OPLOCK_LEVEL_II;
672         struct cifs_fid *pfid;
673 
674         mutex_lock(&tcon->crfid.fid_mutex);
675         if (tcon->crfid.is_valid) {
676                 cifs_dbg(FYI, "found a cached root file handle\n");
677                 *cfid = &tcon->crfid;
678                 kref_get(&tcon->crfid.refcount);
679                 mutex_unlock(&tcon->crfid.fid_mutex);
680                 return 0;
681         }
682 
683         /*
684          * We do not hold the lock for the open because in case
685          * SMB2_open needs to reconnect, it will end up calling
686          * cifs_mark_open_files_invalid() which takes the lock again
687          * thus causing a deadlock
688          */
689 
690         mutex_unlock(&tcon->crfid.fid_mutex);
691 
692         if (smb3_encryption_required(tcon))
693                 flags |= CIFS_TRANSFORM_REQ;
694 
695         if (!server->ops->new_lease_key)
696                 return -EIO;
697 
698         pfid = tcon->crfid.fid;
699         server->ops->new_lease_key(pfid);
700 
701         memset(rqst, 0, sizeof(rqst));
702         resp_buftype[0] = resp_buftype[1] = CIFS_NO_BUFFER;
703         memset(rsp_iov, 0, sizeof(rsp_iov));
704 
705         /* Open */
706         memset(&open_iov, 0, sizeof(open_iov));
707         rqst[0].rq_iov = open_iov;
708         rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
709 
710         oparms.tcon = tcon;
711         oparms.create_options = cifs_create_options(cifs_sb, 0);
712         oparms.desired_access = FILE_READ_ATTRIBUTES;
713         oparms.disposition = FILE_OPEN;
714         oparms.fid = pfid;
715         oparms.reconnect = false;
716 
717         rc = SMB2_open_init(tcon, server,
718                             &rqst[0], &oplock, &oparms, &utf16_path);
719         if (rc)
720                 goto oshr_free;
721         smb2_set_next_command(tcon, &rqst[0]);
722 
723         memset(&qi_iov, 0, sizeof(qi_iov));
724         rqst[1].rq_iov = qi_iov;
725         rqst[1].rq_nvec = 1;
726 
727         rc = SMB2_query_info_init(tcon, server,
728                                   &rqst[1], COMPOUND_FID,
729                                   COMPOUND_FID, FILE_ALL_INFORMATION,
730                                   SMB2_O_INFO_FILE, 0,
731                                   sizeof(struct smb2_file_all_info) +
732                                   PATH_MAX * 2, 0, NULL);
733         if (rc)
734                 goto oshr_free;
735 
736         smb2_set_related(&rqst[1]);
737 
738         rc = compound_send_recv(xid, ses, server,
739                                 flags, 2, rqst,
740                                 resp_buftype, rsp_iov);
741         mutex_lock(&tcon->crfid.fid_mutex);
742 
743         /*
744          * Now we need to check again as the cached root might have
745          * been successfully re-opened from a concurrent process
746          */
747 
748         if (tcon->crfid.is_valid) {
749                 /* work was already done */
750 
751                 /* stash fids for close() later */
752                 struct cifs_fid fid = {
753                         .persistent_fid = pfid->persistent_fid,
754                         .volatile_fid = pfid->volatile_fid,
755                 };
756 
757                 /*
758                  * caller expects this func to set pfid to a valid
759                  * cached root, so we copy the existing one and get a
760                  * reference.
761                  */
762                 memcpy(pfid, tcon->crfid.fid, sizeof(*pfid));
763                 kref_get(&tcon->crfid.refcount);
764 
765                 mutex_unlock(&tcon->crfid.fid_mutex);
766 
767                 if (rc == 0) {
768                         /* close extra handle outside of crit sec */
769                         SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
770                 }
771                 rc = 0;
772                 goto oshr_free;
773         }
774 
775         /* Cached root is still invalid, continue normaly */
776 
777         if (rc) {
778                 if (rc == -EREMCHG) {
779                         tcon->need_reconnect = true;
780                         pr_warn_once("server share %s deleted\n",
781                                      tcon->treeName);
782                 }
783                 goto oshr_exit;
784         }
785 
786         atomic_inc(&tcon->num_remote_opens);
787 
788         o_rsp = (struct smb2_create_rsp *)rsp_iov[0].iov_base;
789         oparms.fid->persistent_fid = o_rsp->PersistentFileId;
790         oparms.fid->volatile_fid = o_rsp->VolatileFileId;
791 #ifdef CONFIG_CIFS_DEBUG2
792         oparms.fid->mid = le64_to_cpu(o_rsp->sync_hdr.MessageId);
793 #endif /* CIFS_DEBUG2 */
794 
795         memcpy(tcon->crfid.fid, pfid, sizeof(struct cifs_fid));
796         tcon->crfid.tcon = tcon;
797         tcon->crfid.is_valid = true;
798         kref_init(&tcon->crfid.refcount);
799 
800         /* BB TBD check to see if oplock level check can be removed below */
801         if (o_rsp->OplockLevel == SMB2_OPLOCK_LEVEL_LEASE) {
802                 kref_get(&tcon->crfid.refcount);
803                 tcon->crfid.has_lease = true;
804                 smb2_parse_contexts(server, o_rsp,
805                                 &oparms.fid->epoch,
806                                     oparms.fid->lease_key, &oplock,
807                                     NULL, NULL);
808         } else
809                 goto oshr_exit;
810 
811         qi_rsp = (struct smb2_query_info_rsp *)rsp_iov[1].iov_base;
812         if (le32_to_cpu(qi_rsp->OutputBufferLength) < sizeof(struct smb2_file_all_info))
813                 goto oshr_exit;
814         if (!smb2_validate_and_copy_iov(
815                                 le16_to_cpu(qi_rsp->OutputBufferOffset),
816                                 sizeof(struct smb2_file_all_info),
817                                 &rsp_iov[1], sizeof(struct smb2_file_all_info),
818                                 (char *)&tcon->crfid.file_all_info))
819                 tcon->crfid.file_all_info_is_valid = true;
820 
821 oshr_exit:
822         mutex_unlock(&tcon->crfid.fid_mutex);
823 oshr_free:
824         SMB2_open_free(&rqst[0]);
825         SMB2_query_info_free(&rqst[1]);
826         free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
827         free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
828         if (rc == 0)
829                 *cfid = &tcon->crfid;
830         return rc;
831 }
832 
833 static void
834 smb3_qfs_tcon(const unsigned int xid, struct cifs_tcon *tcon,
835               struct cifs_sb_info *cifs_sb)
836 {
837         int rc;
838         __le16 srch_path = 0; /* Null - open root of share */
839         u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
840         struct cifs_open_parms oparms;
841         struct cifs_fid fid;
842         bool no_cached_open = tcon->nohandlecache;
843         struct cached_fid *cfid = NULL;
844 
845         oparms.tcon = tcon;
846         oparms.desired_access = FILE_READ_ATTRIBUTES;
847         oparms.disposition = FILE_OPEN;
848         oparms.create_options = cifs_create_options(cifs_sb, 0);
849         oparms.fid = &fid;
850         oparms.reconnect = false;
851 
852         if (no_cached_open) {
853                 rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL,
854                                NULL, NULL);
855         } else {
856                 rc = open_shroot(xid, tcon, cifs_sb, &cfid);
857                 if (rc == 0)
858                         memcpy(&fid, cfid->fid, sizeof(struct cifs_fid));
859         }
860         if (rc)
861                 return;
862 
863         SMB3_request_interfaces(xid, tcon);
864 
865         SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
866                         FS_ATTRIBUTE_INFORMATION);
867         SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
868                         FS_DEVICE_INFORMATION);
869         SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
870                         FS_VOLUME_INFORMATION);
871         SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
872                         FS_SECTOR_SIZE_INFORMATION); /* SMB3 specific */
873         if (no_cached_open)
874                 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
875         else
876                 close_shroot(cfid);
877 }
878 
879 static void
880 smb2_qfs_tcon(const unsigned int xid, struct cifs_tcon *tcon,
881               struct cifs_sb_info *cifs_sb)
882 {
883         int rc;
884         __le16 srch_path = 0; /* Null - open root of share */
885         u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
886         struct cifs_open_parms oparms;
887         struct cifs_fid fid;
888 
889         oparms.tcon = tcon;
890         oparms.desired_access = FILE_READ_ATTRIBUTES;
891         oparms.disposition = FILE_OPEN;
892         oparms.create_options = cifs_create_options(cifs_sb, 0);
893         oparms.fid = &fid;
894         oparms.reconnect = false;
895 
896         rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL,
897                        NULL, NULL);
898         if (rc)
899                 return;
900 
901         SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
902                         FS_ATTRIBUTE_INFORMATION);
903         SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
904                         FS_DEVICE_INFORMATION);
905         SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
906 }
907 
908 static int
909 smb2_is_path_accessible(const unsigned int xid, struct cifs_tcon *tcon,
910                         struct cifs_sb_info *cifs_sb, const char *full_path)
911 {
912         int rc;
913         __le16 *utf16_path;
914         __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
915         struct cifs_open_parms oparms;
916         struct cifs_fid fid;
917 
918         if ((*full_path == 0) && tcon->crfid.is_valid)
919                 return 0;
920 
921         utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
922         if (!utf16_path)
923                 return -ENOMEM;
924 
925         oparms.tcon = tcon;
926         oparms.desired_access = FILE_READ_ATTRIBUTES;
927         oparms.disposition = FILE_OPEN;
928         oparms.create_options = cifs_create_options(cifs_sb, 0);
929         oparms.fid = &fid;
930         oparms.reconnect = false;
931 
932         rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, NULL,
933                        NULL);
934         if (rc) {
935                 kfree(utf16_path);
936                 return rc;
937         }
938 
939         rc = SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
940         kfree(utf16_path);
941         return rc;
942 }
943 
944 static int
945 smb2_get_srv_inum(const unsigned int xid, struct cifs_tcon *tcon,
946                   struct cifs_sb_info *cifs_sb, const char *full_path,
947                   u64 *uniqueid, FILE_ALL_INFO *data)
948 {
949         *uniqueid = le64_to_cpu(data->IndexNumber);
950         return 0;
951 }
952 
953 static int
954 smb2_query_file_info(const unsigned int xid, struct cifs_tcon *tcon,
955                      struct cifs_fid *fid, FILE_ALL_INFO *data)
956 {
957         int rc;
958         struct smb2_file_all_info *smb2_data;
959 
960         smb2_data = kzalloc(sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
961                             GFP_KERNEL);
962         if (smb2_data == NULL)
963                 return -ENOMEM;
964 
965         rc = SMB2_query_info(xid, tcon, fid->persistent_fid, fid->volatile_fid,
966                              smb2_data);
967         if (!rc)
968                 move_smb2_info_to_cifs(data, smb2_data);
969         kfree(smb2_data);
970         return rc;
971 }
972 
973 #ifdef CONFIG_CIFS_XATTR
974 static ssize_t
975 move_smb2_ea_to_cifs(char *dst, size_t dst_size,
976                      struct smb2_file_full_ea_info *src, size_t src_size,
977                      const unsigned char *ea_name)
978 {
979         int rc = 0;
980         unsigned int ea_name_len = ea_name ? strlen(ea_name) : 0;
981         char *name, *value;
982         size_t buf_size = dst_size;
983         size_t name_len, value_len, user_name_len;
984 
985         while (src_size > 0) {
986                 name = &src->ea_data[0];
987                 name_len = (size_t)src->ea_name_length;
988                 value = &src->ea_data[src->ea_name_length + 1];
989                 value_len = (size_t)le16_to_cpu(src->ea_value_length);
990 
991                 if (name_len == 0)
992                         break;
993 
994                 if (src_size < 8 + name_len + 1 + value_len) {
995                         cifs_dbg(FYI, "EA entry goes beyond length of list\n");
996                         rc = -EIO;
997                         goto out;
998                 }
999 
1000                 if (ea_name) {
1001                         if (ea_name_len == name_len &&
1002                             memcmp(ea_name, name, name_len) == 0) {
1003                                 rc = value_len;
1004                                 if (dst_size == 0)
1005                                         goto out;
1006                                 if (dst_size < value_len) {
1007                                         rc = -ERANGE;
1008                                         goto out;
1009                                 }
1010                                 memcpy(dst, value, value_len);
1011                                 goto out;
1012                         }
1013                 } else {
1014                         /* 'user.' plus a terminating null */
1015                         user_name_len = 5 + 1 + name_len;
1016 
1017                         if (buf_size == 0) {
1018                                 /* skip copy - calc size only */
1019                                 rc += user_name_len;
1020                         } else if (dst_size >= user_name_len) {
1021                                 dst_size -= user_name_len;
1022                                 memcpy(dst, "user.", 5);
1023                                 dst += 5;
1024                                 memcpy(dst, src->ea_data, name_len);
1025                                 dst += name_len;
1026                                 *dst = 0;
1027                                 ++dst;
1028                                 rc += user_name_len;
1029                         } else {
1030                                 /* stop before overrun buffer */
1031                                 rc = -ERANGE;
1032                                 break;
1033                         }
1034                 }
1035 
1036                 if (!src->next_entry_offset)
1037                         break;
1038 
1039                 if (src_size < le32_to_cpu(src->next_entry_offset)) {
1040                         /* stop before overrun buffer */
1041                         rc = -ERANGE;
1042                         break;
1043                 }
1044                 src_size -= le32_to_cpu(src->next_entry_offset);
1045                 src = (void *)((char *)src +
1046                                le32_to_cpu(src->next_entry_offset));
1047         }
1048 
1049         /* didn't find the named attribute */
1050         if (ea_name)
1051                 rc = -ENODATA;
1052 
1053 out:
1054         return (ssize_t)rc;
1055 }
1056 
1057 static ssize_t
1058 smb2_query_eas(const unsigned int xid, struct cifs_tcon *tcon,
1059                const unsigned char *path, const unsigned char *ea_name,
1060                char *ea_data, size_t buf_size,
1061                struct cifs_sb_info *cifs_sb)
1062 {
1063         int rc;
1064         __le16 *utf16_path;
1065         struct kvec rsp_iov = {NULL, 0};
1066         int buftype = CIFS_NO_BUFFER;
1067         struct smb2_query_info_rsp *rsp;
1068         struct smb2_file_full_ea_info *info = NULL;
1069 
1070         utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
1071         if (!utf16_path)
1072                 return -ENOMEM;
1073 
1074         rc = smb2_query_info_compound(xid, tcon, utf16_path,
1075                                       FILE_READ_EA,
1076                                       FILE_FULL_EA_INFORMATION,
1077                                       SMB2_O_INFO_FILE,
1078                                       CIFSMaxBufSize -
1079                                       MAX_SMB2_CREATE_RESPONSE_SIZE -
1080                                       MAX_SMB2_CLOSE_RESPONSE_SIZE,
1081                                       &rsp_iov, &buftype, cifs_sb);
1082         if (rc) {
1083                 /*
1084                  * If ea_name is NULL (listxattr) and there are no EAs,
1085                  * return 0 as it's not an error. Otherwise, the specified
1086                  * ea_name was not found.
1087                  */
1088                 if (!ea_name && rc == -ENODATA)
1089                         rc = 0;
1090                 goto qeas_exit;
1091         }
1092 
1093         rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
1094         rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
1095                                le32_to_cpu(rsp->OutputBufferLength),
1096                                &rsp_iov,
1097                                sizeof(struct smb2_file_full_ea_info));
1098         if (rc)
1099                 goto qeas_exit;
1100 
1101         info = (struct smb2_file_full_ea_info *)(
1102                         le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
1103         rc = move_smb2_ea_to_cifs(ea_data, buf_size, info,
1104                         le32_to_cpu(rsp->OutputBufferLength), ea_name);
1105 
1106  qeas_exit:
1107         kfree(utf16_path);
1108         free_rsp_buf(buftype, rsp_iov.iov_base);
1109         return rc;
1110 }
1111 
1112 
1113 static int
1114 smb2_set_ea(const unsigned int xid, struct cifs_tcon *tcon,
1115             const char *path, const char *ea_name, const void *ea_value,
1116             const __u16 ea_value_len, const struct nls_table *nls_codepage,
1117             struct cifs_sb_info *cifs_sb)
1118 {
1119         struct cifs_ses *ses = tcon->ses;
1120         struct TCP_Server_Info *server = cifs_pick_channel(ses);
1121         __le16 *utf16_path = NULL;
1122         int ea_name_len = strlen(ea_name);
1123         int flags = 0;
1124         int len;
1125         struct smb_rqst rqst[3];
1126         int resp_buftype[3];
1127         struct kvec rsp_iov[3];
1128         struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
1129         struct cifs_open_parms oparms;
1130         __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
1131         struct cifs_fid fid;
1132         struct kvec si_iov[SMB2_SET_INFO_IOV_SIZE];
1133         unsigned int size[1];
1134         void *data[1];
1135         struct smb2_file_full_ea_info *ea = NULL;
1136         struct kvec close_iov[1];
1137         struct smb2_query_info_rsp *rsp;
1138         int rc, used_len = 0;
1139 
1140         if (smb3_encryption_required(tcon))
1141                 flags |= CIFS_TRANSFORM_REQ;
1142 
1143         if (ea_name_len > 255)
1144                 return -EINVAL;
1145 
1146         utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
1147         if (!utf16_path)
1148                 return -ENOMEM;
1149 
1150         memset(rqst, 0, sizeof(rqst));
1151         resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
1152         memset(rsp_iov, 0, sizeof(rsp_iov));
1153 
1154         if (ses->server->ops->query_all_EAs) {
1155                 if (!ea_value) {
1156                         rc = ses->server->ops->query_all_EAs(xid, tcon, path,
1157                                                              ea_name, NULL, 0,
1158                                                              cifs_sb);
1159                         if (rc == -ENODATA)
1160                                 goto sea_exit;
1161                 } else {
1162                         /* If we are adding a attribute we should first check
1163                          * if there will be enough space available to store
1164                          * the new EA. If not we should not add it since we
1165                          * would not be able to even read the EAs back.
1166                          */
1167                         rc = smb2_query_info_compound(xid, tcon, utf16_path,
1168                                       FILE_READ_EA,
1169                                       FILE_FULL_EA_INFORMATION,
1170                                       SMB2_O_INFO_FILE,
1171                                       CIFSMaxBufSize -
1172                                       MAX_SMB2_CREATE_RESPONSE_SIZE -
1173                                       MAX_SMB2_CLOSE_RESPONSE_SIZE,
1174                                       &rsp_iov[1], &resp_buftype[1], cifs_sb);
1175                         if (rc == 0) {
1176                                 rsp = (struct smb2_query_info_rsp *)rsp_iov[1].iov_base;
1177                                 used_len = le32_to_cpu(rsp->OutputBufferLength);
1178                         }
1179                         free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
1180                         resp_buftype[1] = CIFS_NO_BUFFER;
1181                         memset(&rsp_iov[1], 0, sizeof(rsp_iov[1]));
1182                         rc = 0;
1183 
1184                         /* Use a fudge factor of 256 bytes in case we collide
1185                          * with a different set_EAs command.
1186                          */
1187                         if(CIFSMaxBufSize - MAX_SMB2_CREATE_RESPONSE_SIZE -
1188                            MAX_SMB2_CLOSE_RESPONSE_SIZE - 256 <
1189                            used_len + ea_name_len + ea_value_len + 1) {
1190                                 rc = -ENOSPC;
1191                                 goto sea_exit;
1192                         }
1193                 }
1194         }
1195 
1196         /* Open */
1197         memset(&open_iov, 0, sizeof(open_iov));
1198         rqst[0].rq_iov = open_iov;
1199         rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
1200 
1201         memset(&oparms, 0, sizeof(oparms));
1202         oparms.tcon = tcon;
1203         oparms.desired_access = FILE_WRITE_EA;
1204         oparms.disposition = FILE_OPEN;
1205         oparms.create_options = cifs_create_options(cifs_sb, 0);
1206         oparms.fid = &fid;
1207         oparms.reconnect = false;
1208 
1209         rc = SMB2_open_init(tcon, server,
1210                             &rqst[0], &oplock, &oparms, utf16_path);
1211         if (rc)
1212                 goto sea_exit;
1213         smb2_set_next_command(tcon, &rqst[0]);
1214 
1215 
1216         /* Set Info */
1217         memset(&si_iov, 0, sizeof(si_iov));
1218         rqst[1].rq_iov = si_iov;
1219         rqst[1].rq_nvec = 1;
1220 
1221         len = sizeof(*ea) + ea_name_len + ea_value_len + 1;
1222         ea = kzalloc(len, GFP_KERNEL);
1223         if (ea == NULL) {
1224                 rc = -ENOMEM;
1225                 goto sea_exit;
1226         }
1227 
1228         ea->ea_name_length = ea_name_len;
1229         ea->ea_value_length = cpu_to_le16(ea_value_len);
1230         memcpy(ea->ea_data, ea_name, ea_name_len + 1);
1231         memcpy(ea->ea_data + ea_name_len + 1, ea_value, ea_value_len);
1232 
1233         size[0] = len;
1234         data[0] = ea;
1235 
1236         rc = SMB2_set_info_init(tcon, server,
1237                                 &rqst[1], COMPOUND_FID,
1238                                 COMPOUND_FID, current->tgid,
1239                                 FILE_FULL_EA_INFORMATION,
1240                                 SMB2_O_INFO_FILE, 0, data, size);
1241         smb2_set_next_command(tcon, &rqst[1]);
1242         smb2_set_related(&rqst[1]);
1243 
1244 
1245         /* Close */
1246         memset(&close_iov, 0, sizeof(close_iov));
1247         rqst[2].rq_iov = close_iov;
1248         rqst[2].rq_nvec = 1;
1249         rc = SMB2_close_init(tcon, server,
1250                              &rqst[2], COMPOUND_FID, COMPOUND_FID, false);
1251         smb2_set_related(&rqst[2]);
1252 
1253         rc = compound_send_recv(xid, ses, server,
1254                                 flags, 3, rqst,
1255                                 resp_buftype, rsp_iov);
1256         /* no need to bump num_remote_opens because handle immediately closed */
1257 
1258  sea_exit:
1259         kfree(ea);
1260         kfree(utf16_path);
1261         SMB2_open_free(&rqst[0]);
1262         SMB2_set_info_free(&rqst[1]);
1263         SMB2_close_free(&rqst[2]);
1264         free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
1265         free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
1266         free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
1267         return rc;
1268 }
1269 #endif
1270 
1271 static bool
1272 smb2_can_echo(struct TCP_Server_Info *server)
1273 {
1274         return server->echoes;
1275 }
1276 
1277 static void
1278 smb2_clear_stats(struct cifs_tcon *tcon)
1279 {
1280         int i;
1281 
1282         for (i = 0; i < NUMBER_OF_SMB2_COMMANDS; i++) {
1283                 atomic_set(&tcon->stats.smb2_stats.smb2_com_sent[i], 0);
1284                 atomic_set(&tcon->stats.smb2_stats.smb2_com_failed[i], 0);
1285         }
1286 }
1287 
1288 static void
1289 smb2_dump_share_caps(struct seq_file *m, struct cifs_tcon *tcon)
1290 {
1291         seq_puts(m, "\n\tShare Capabilities:");
1292         if (tcon->capabilities & SMB2_SHARE_CAP_DFS)
1293                 seq_puts(m, " DFS,");
1294         if (tcon->capabilities & SMB2_SHARE_CAP_CONTINUOUS_AVAILABILITY)
1295                 seq_puts(m, " CONTINUOUS AVAILABILITY,");
1296         if (tcon->capabilities & SMB2_SHARE_CAP_SCALEOUT)
1297                 seq_puts(m, " SCALEOUT,");
1298         if (tcon->capabilities & SMB2_SHARE_CAP_CLUSTER)
1299                 seq_puts(m, " CLUSTER,");
1300         if (tcon->capabilities & SMB2_SHARE_CAP_ASYMMETRIC)
1301                 seq_puts(m, " ASYMMETRIC,");
1302         if (tcon->capabilities == 0)
1303                 seq_puts(m, " None");
1304         if (tcon->ss_flags & SSINFO_FLAGS_ALIGNED_DEVICE)
1305                 seq_puts(m, " Aligned,");
1306         if (tcon->ss_flags & SSINFO_FLAGS_PARTITION_ALIGNED_ON_DEVICE)
1307                 seq_puts(m, " Partition Aligned,");
1308         if (tcon->ss_flags & SSINFO_FLAGS_NO_SEEK_PENALTY)
1309                 seq_puts(m, " SSD,");
1310         if (tcon->ss_flags & SSINFO_FLAGS_TRIM_ENABLED)
1311                 seq_puts(m, " TRIM-support,");
1312 
1313         seq_printf(m, "\tShare Flags: 0x%x", tcon->share_flags);
1314         seq_printf(m, "\n\ttid: 0x%x", tcon->tid);
1315         if (tcon->perf_sector_size)
1316                 seq_printf(m, "\tOptimal sector size: 0x%x",
1317                            tcon->perf_sector_size);
1318         seq_printf(m, "\tMaximal Access: 0x%x", tcon->maximal_access);
1319 }
1320 
1321 static void
1322 smb2_print_stats(struct seq_file *m, struct cifs_tcon *tcon)
1323 {
1324         atomic_t *sent = tcon->stats.smb2_stats.smb2_com_sent;
1325         atomic_t *failed = tcon->stats.smb2_stats.smb2_com_failed;
1326 
1327         /*
1328          *  Can't display SMB2_NEGOTIATE, SESSION_SETUP, LOGOFF, CANCEL and ECHO
1329          *  totals (requests sent) since those SMBs are per-session not per tcon
1330          */
1331         seq_printf(m, "\nBytes read: %llu  Bytes written: %llu",
1332                    (long long)(tcon->bytes_read),
1333                    (long long)(tcon->bytes_written));
1334         seq_printf(m, "\nOpen files: %d total (local), %d open on server",
1335                    atomic_read(&tcon->num_local_opens),
1336                    atomic_read(&tcon->num_remote_opens));
1337         seq_printf(m, "\nTreeConnects: %d total %d failed",
1338                    atomic_read(&sent[SMB2_TREE_CONNECT_HE]),
1339                    atomic_read(&failed[SMB2_TREE_CONNECT_HE]));
1340         seq_printf(m, "\nTreeDisconnects: %d total %d failed",
1341                    atomic_read(&sent[SMB2_TREE_DISCONNECT_HE]),
1342                    atomic_read(&failed[SMB2_TREE_DISCONNECT_HE]));
1343         seq_printf(m, "\nCreates: %d total %d failed",
1344                    atomic_read(&sent[SMB2_CREATE_HE]),
1345                    atomic_read(&failed[SMB2_CREATE_HE]));
1346         seq_printf(m, "\nCloses: %d total %d failed",
1347                    atomic_read(&sent[SMB2_CLOSE_HE]),
1348                    atomic_read(&failed[SMB2_CLOSE_HE]));
1349         seq_printf(m, "\nFlushes: %d total %d failed",
1350                    atomic_read(&sent[SMB2_FLUSH_HE]),
1351                    atomic_read(&failed[SMB2_FLUSH_HE]));
1352         seq_printf(m, "\nReads: %d total %d failed",
1353                    atomic_read(&sent[SMB2_READ_HE]),
1354                    atomic_read(&failed[SMB2_READ_HE]));
1355         seq_printf(m, "\nWrites: %d total %d failed",
1356                    atomic_read(&sent[SMB2_WRITE_HE]),
1357                    atomic_read(&failed[SMB2_WRITE_HE]));
1358         seq_printf(m, "\nLocks: %d total %d failed",
1359                    atomic_read(&sent[SMB2_LOCK_HE]),
1360                    atomic_read(&failed[SMB2_LOCK_HE]));
1361         seq_printf(m, "\nIOCTLs: %d total %d failed",
1362                    atomic_read(&sent[SMB2_IOCTL_HE]),
1363                    atomic_read(&failed[SMB2_IOCTL_HE]));
1364         seq_printf(m, "\nQueryDirectories: %d total %d failed",
1365                    atomic_read(&sent[SMB2_QUERY_DIRECTORY_HE]),
1366                    atomic_read(&failed[SMB2_QUERY_DIRECTORY_HE]));
1367         seq_printf(m, "\nChangeNotifies: %d total %d failed",
1368                    atomic_read(&sent[SMB2_CHANGE_NOTIFY_HE]),
1369                    atomic_read(&failed[SMB2_CHANGE_NOTIFY_HE]));
1370         seq_printf(m, "\nQueryInfos: %d total %d failed",
1371                    atomic_read(&sent[SMB2_QUERY_INFO_HE]),
1372                    atomic_read(&failed[SMB2_QUERY_INFO_HE]));
1373         seq_printf(m, "\nSetInfos: %d total %d failed",
1374                    atomic_read(&sent[SMB2_SET_INFO_HE]),
1375                    atomic_read(&failed[SMB2_SET_INFO_HE]));
1376         seq_printf(m, "\nOplockBreaks: %d sent %d failed",
1377                    atomic_read(&sent[SMB2_OPLOCK_BREAK_HE]),
1378                    atomic_read(&failed[SMB2_OPLOCK_BREAK_HE]));
1379 }
1380 
1381 static void
1382 smb2_set_fid(struct cifsFileInfo *cfile, struct cifs_fid *fid, __u32 oplock)
1383 {
1384         struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
1385         struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server;
1386 
1387         cfile->fid.persistent_fid = fid->persistent_fid;
1388         cfile->fid.volatile_fid = fid->volatile_fid;
1389         cfile->fid.access = fid->access;
1390 #ifdef CONFIG_CIFS_DEBUG2
1391         cfile->fid.mid = fid->mid;
1392 #endif /* CIFS_DEBUG2 */
1393         server->ops->set_oplock_level(cinode, oplock, fid->epoch,
1394                                       &fid->purge_cache);
1395         cinode->can_cache_brlcks = CIFS_CACHE_WRITE(cinode);
1396         memcpy(cfile->fid.create_guid, fid->create_guid, 16);
1397 }
1398 
1399 static void
1400 smb2_close_file(const unsigned int xid, struct cifs_tcon *tcon,
1401                 struct cifs_fid *fid)
1402 {
1403         SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
1404 }
1405 
1406 static void
1407 smb2_close_getattr(const unsigned int xid, struct cifs_tcon *tcon,
1408                    struct cifsFileInfo *cfile)
1409 {
1410         struct smb2_file_network_open_info file_inf;
1411         struct inode *inode;
1412         int rc;
1413 
1414         rc = __SMB2_close(xid, tcon, cfile->fid.persistent_fid,
1415                    cfile->fid.volatile_fid, &file_inf);
1416         if (rc)
1417                 return;
1418 
1419         inode = d_inode(cfile->dentry);
1420 
1421         spin_lock(&inode->i_lock);
1422         CIFS_I(inode)->time = jiffies;
1423 
1424         /* Creation time should not need to be updated on close */
1425         if (file_inf.LastWriteTime)
1426                 inode->i_mtime = cifs_NTtimeToUnix(file_inf.LastWriteTime);
1427         if (file_inf.ChangeTime)
1428                 inode->i_ctime = cifs_NTtimeToUnix(file_inf.ChangeTime);
1429         if (file_inf.LastAccessTime)
1430                 inode->i_atime = cifs_NTtimeToUnix(file_inf.LastAccessTime);
1431 
1432         /*
1433          * i_blocks is not related to (i_size / i_blksize),
1434          * but instead 512 byte (2**9) size is required for
1435          * calculating num blocks.
1436          */
1437         if (le64_to_cpu(file_inf.AllocationSize) > 4096)
1438                 inode->i_blocks =
1439                         (512 - 1 + le64_to_cpu(file_inf.AllocationSize)) >> 9;
1440 
1441         /* End of file and Attributes should not have to be updated on close */
1442         spin_unlock(&inode->i_lock);
1443 }
1444 
1445 static int
1446 SMB2_request_res_key(const unsigned int xid, struct cifs_tcon *tcon,
1447                      u64 persistent_fid, u64 volatile_fid,
1448                      struct copychunk_ioctl *pcchunk)
1449 {
1450         int rc;
1451         unsigned int ret_data_len;
1452         struct resume_key_req *res_key;
1453 
1454         rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
1455                         FSCTL_SRV_REQUEST_RESUME_KEY, true /* is_fsctl */,
1456                         NULL, 0 /* no input */, CIFSMaxBufSize,
1457                         (char **)&res_key, &ret_data_len);
1458 
1459         if (rc) {
1460                 cifs_tcon_dbg(VFS, "refcpy ioctl error %d getting resume key\n", rc);
1461                 goto req_res_key_exit;
1462         }
1463         if (ret_data_len < sizeof(struct resume_key_req)) {
1464                 cifs_tcon_dbg(VFS, "Invalid refcopy resume key length\n");
1465                 rc = -EINVAL;
1466                 goto req_res_key_exit;
1467         }
1468         memcpy(pcchunk->SourceKey, res_key->ResumeKey, COPY_CHUNK_RES_KEY_SIZE);
1469 
1470 req_res_key_exit:
1471         kfree(res_key);
1472         return rc;
1473 }
1474 
1475 struct iqi_vars {
1476         struct smb_rqst rqst[3];
1477         struct kvec rsp_iov[3];
1478         struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
1479         struct kvec qi_iov[1];
1480         struct kvec io_iov[SMB2_IOCTL_IOV_SIZE];
1481         struct kvec si_iov[SMB2_SET_INFO_IOV_SIZE];
1482         struct kvec close_iov[1];
1483 };
1484 
1485 static int
1486 smb2_ioctl_query_info(const unsigned int xid,
1487                       struct cifs_tcon *tcon,
1488                       struct cifs_sb_info *cifs_sb,
1489                       __le16 *path, int is_dir,
1490                       unsigned long p)
1491 {
1492         struct iqi_vars *vars;
1493         struct smb_rqst *rqst;
1494         struct kvec *rsp_iov;
1495         struct cifs_ses *ses = tcon->ses;
1496         struct TCP_Server_Info *server = cifs_pick_channel(ses);
1497         char __user *arg = (char __user *)p;
1498         struct smb_query_info qi;
1499         struct smb_query_info __user *pqi;
1500         int rc = 0;
1501         int flags = 0;
1502         struct smb2_query_info_rsp *qi_rsp = NULL;
1503         struct smb2_ioctl_rsp *io_rsp = NULL;
1504         void *buffer = NULL;
1505         int resp_buftype[3];
1506         struct cifs_open_parms oparms;
1507         u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
1508         struct cifs_fid fid;
1509         unsigned int size[2];
1510         void *data[2];
1511         int create_options = is_dir ? CREATE_NOT_FILE : CREATE_NOT_DIR;
1512 
1513         vars = kzalloc(sizeof(*vars), GFP_ATOMIC);
1514         if (vars == NULL)
1515                 return -ENOMEM;
1516         rqst = &vars->rqst[0];
1517         rsp_iov = &vars->rsp_iov[0];
1518 
1519         resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
1520 
1521         if (copy_from_user(&qi, arg, sizeof(struct smb_query_info)))
1522                 goto e_fault;
1523 
1524         if (qi.output_buffer_length > 1024) {
1525                 kfree(vars);
1526                 return -EINVAL;
1527         }
1528 
1529         if (!ses || !server) {
1530                 kfree(vars);
1531                 return -EIO;
1532         }
1533 
1534         if (smb3_encryption_required(tcon))
1535                 flags |= CIFS_TRANSFORM_REQ;
1536 
1537         buffer = memdup_user(arg + sizeof(struct smb_query_info),
1538                              qi.output_buffer_length);
1539         if (IS_ERR(buffer)) {
1540                 kfree(vars);
1541                 return PTR_ERR(buffer);
1542         }
1543 
1544         /* Open */
1545         rqst[0].rq_iov = &vars->open_iov[0];
1546         rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
1547 
1548         memset(&oparms, 0, sizeof(oparms));
1549         oparms.tcon = tcon;
1550         oparms.disposition = FILE_OPEN;
1551         oparms.create_options = cifs_create_options(cifs_sb, create_options);
1552         oparms.fid = &fid;
1553         oparms.reconnect = false;
1554 
1555         if (qi.flags & PASSTHRU_FSCTL) {
1556                 switch (qi.info_type & FSCTL_DEVICE_ACCESS_MASK) {
1557                 case FSCTL_DEVICE_ACCESS_FILE_READ_WRITE_ACCESS:
1558                         oparms.desired_access = FILE_READ_DATA | FILE_WRITE_DATA | FILE_READ_ATTRIBUTES | SYNCHRONIZE;
1559                         break;
1560                 case FSCTL_DEVICE_ACCESS_FILE_ANY_ACCESS:
1561                         oparms.desired_access = GENERIC_ALL;
1562                         break;
1563                 case FSCTL_DEVICE_ACCESS_FILE_READ_ACCESS:
1564                         oparms.desired_access = GENERIC_READ;
1565                         break;
1566                 case FSCTL_DEVICE_ACCESS_FILE_WRITE_ACCESS:
1567                         oparms.desired_access = GENERIC_WRITE;
1568                         break;
1569                 }
1570         } else if (qi.flags & PASSTHRU_SET_INFO) {
1571                 oparms.desired_access = GENERIC_WRITE;
1572         } else {
1573                 oparms.desired_access = FILE_READ_ATTRIBUTES | READ_CONTROL;
1574         }
1575 
1576         rc = SMB2_open_init(tcon, server,
1577                             &rqst[0], &oplock, &oparms, path);
1578         if (rc)
1579                 goto iqinf_exit;
1580         smb2_set_next_command(tcon, &rqst[0]);
1581 
1582         /* Query */
1583         if (qi.flags & PASSTHRU_FSCTL) {
1584                 /* Can eventually relax perm check since server enforces too */
1585                 if (!capable(CAP_SYS_ADMIN))
1586                         rc = -EPERM;
1587                 else  {
1588                         rqst[1].rq_iov = &vars->io_iov[0];
1589                         rqst[1].rq_nvec = SMB2_IOCTL_IOV_SIZE;
1590 
1591                         rc = SMB2_ioctl_init(tcon, server,
1592                                              &rqst[1],
1593                                              COMPOUND_FID, COMPOUND_FID,
1594                                              qi.info_type, true, buffer,
1595                                              qi.output_buffer_length,
1596                                              CIFSMaxBufSize -
1597                                              MAX_SMB2_CREATE_RESPONSE_SIZE -
1598                                              MAX_SMB2_CLOSE_RESPONSE_SIZE);
1599                 }
1600         } else if (qi.flags == PASSTHRU_SET_INFO) {
1601                 /* Can eventually relax perm check since server enforces too */
1602                 if (!capable(CAP_SYS_ADMIN))
1603                         rc = -EPERM;
1604                 else  {
1605                         rqst[1].rq_iov = &vars->si_iov[0];
1606                         rqst[1].rq_nvec = 1;
1607 
1608                         size[0] = 8;
1609                         data[0] = buffer;
1610 
1611                         rc = SMB2_set_info_init(tcon, server,
1612                                         &rqst[1],
1613                                         COMPOUND_FID, COMPOUND_FID,
1614                                         current->tgid,
1615                                         FILE_END_OF_FILE_INFORMATION,
1616                                         SMB2_O_INFO_FILE, 0, data, size);
1617                 }
1618         } else if (qi.flags == PASSTHRU_QUERY_INFO) {
1619                 rqst[1].rq_iov = &vars->qi_iov[0];
1620                 rqst[1].rq_nvec = 1;
1621 
1622                 rc = SMB2_query_info_init(tcon, server,
1623                                   &rqst[1], COMPOUND_FID,
1624                                   COMPOUND_FID, qi.file_info_class,
1625                                   qi.info_type, qi.additional_information,
1626                                   qi.input_buffer_length,
1627                                   qi.output_buffer_length, buffer);
1628         } else { /* unknown flags */
1629                 cifs_tcon_dbg(VFS, "Invalid passthru query flags: 0x%x\n",
1630                               qi.flags);
1631                 rc = -EINVAL;
1632         }
1633 
1634         if (rc)
1635                 goto iqinf_exit;
1636         smb2_set_next_command(tcon, &rqst[1]);
1637         smb2_set_related(&rqst[1]);
1638 
1639         /* Close */
1640         rqst[2].rq_iov = &vars->close_iov[0];
1641         rqst[2].rq_nvec = 1;
1642 
1643         rc = SMB2_close_init(tcon, server,
1644                              &rqst[2], COMPOUND_FID, COMPOUND_FID, false);
1645         if (rc)
1646                 goto iqinf_exit;
1647         smb2_set_related(&rqst[2]);
1648 
1649         rc = compound_send_recv(xid, ses, server,
1650                                 flags, 3, rqst,
1651                                 resp_buftype, rsp_iov);
1652         if (rc)
1653                 goto iqinf_exit;
1654 
1655         /* No need to bump num_remote_opens since handle immediately closed */
1656         if (qi.flags & PASSTHRU_FSCTL) {
1657                 pqi = (struct smb_query_info __user *)arg;
1658                 io_rsp = (struct smb2_ioctl_rsp *)rsp_iov[1].iov_base;
1659                 if (le32_to_cpu(io_rsp->OutputCount) < qi.input_buffer_length)
1660                         qi.input_buffer_length = le32_to_cpu(io_rsp->OutputCount);
1661                 if (qi.input_buffer_length > 0 &&
1662                     le32_to_cpu(io_rsp->OutputOffset) + qi.input_buffer_length
1663                     > rsp_iov[1].iov_len)
1664                         goto e_fault;
1665 
1666                 if (copy_to_user(&pqi->input_buffer_length,
1667                                  &qi.input_buffer_length,
1668                                  sizeof(qi.input_buffer_length)))
1669                         goto e_fault;
1670 
1671                 if (copy_to_user((void __user *)pqi + sizeof(struct smb_query_info),
1672                                  (const void *)io_rsp + le32_to_cpu(io_rsp->OutputOffset),
1673                                  qi.input_buffer_length))
1674                         goto e_fault;
1675         } else {
1676                 pqi = (struct smb_query_info __user *)arg;
1677                 qi_rsp = (struct smb2_query_info_rsp *)rsp_iov[1].iov_base;
1678                 if (le32_to_cpu(qi_rsp->OutputBufferLength) < qi.input_buffer_length)
1679                         qi.input_buffer_length = le32_to_cpu(qi_rsp->OutputBufferLength);
1680                 if (copy_to_user(&pqi->input_buffer_length,
1681                                  &qi.input_buffer_length,
1682                                  sizeof(qi.input_buffer_length)))
1683                         goto e_fault;
1684 
1685                 if (copy_to_user(pqi + 1, qi_rsp->Buffer,
1686                                  qi.input_buffer_length))
1687                         goto e_fault;
1688         }
1689 
1690  iqinf_exit:
1691         kfree(vars);
1692         kfree(buffer);
1693         SMB2_open_free(&rqst[0]);
1694         if (qi.flags & PASSTHRU_FSCTL)
1695                 SMB2_ioctl_free(&rqst[1]);
1696         else
1697                 SMB2_query_info_free(&rqst[1]);
1698 
1699         SMB2_close_free(&rqst[2]);
1700         free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
1701         free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
1702         free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
1703         return rc;
1704 
1705 e_fault:
1706         rc = -EFAULT;
1707         goto iqinf_exit;
1708 }
1709 
1710 static ssize_t
1711 smb2_copychunk_range(const unsigned int xid,
1712                         struct cifsFileInfo *srcfile,
1713                         struct cifsFileInfo *trgtfile, u64 src_off,
1714                         u64 len, u64 dest_off)
1715 {
1716         int rc;
1717         unsigned int ret_data_len;
1718         struct copychunk_ioctl *pcchunk;
1719         struct copychunk_ioctl_rsp *retbuf = NULL;
1720         struct cifs_tcon *tcon;
1721         int chunks_copied = 0;
1722         bool chunk_sizes_updated = false;
1723         ssize_t bytes_written, total_bytes_written = 0;
1724 
1725         pcchunk = kmalloc(sizeof(struct copychunk_ioctl), GFP_KERNEL);
1726 
1727         if (pcchunk == NULL)
1728                 return -ENOMEM;
1729 
1730         cifs_dbg(FYI, "%s: about to call request res key\n", __func__);
1731         /* Request a key from the server to identify the source of the copy */
1732         rc = SMB2_request_res_key(xid, tlink_tcon(srcfile->tlink),
1733                                 srcfile->fid.persistent_fid,
1734                                 srcfile->fid.volatile_fid, pcchunk);
1735 
1736         /* Note: request_res_key sets res_key null only if rc !=0 */
1737         if (rc)
1738                 goto cchunk_out;
1739 
1740         /* For now array only one chunk long, will make more flexible later */
1741         pcchunk->ChunkCount = cpu_to_le32(1);
1742         pcchunk->Reserved = 0;
1743         pcchunk->Reserved2 = 0;
1744 
1745         tcon = tlink_tcon(trgtfile->tlink);
1746 
1747         while (len > 0) {
1748                 pcchunk->SourceOffset = cpu_to_le64(src_off);
1749                 pcchunk->TargetOffset = cpu_to_le64(dest_off);
1750                 pcchunk->Length =
1751                         cpu_to_le32(min_t(u32, len, tcon->max_bytes_chunk));
1752 
1753                 /* Request server copy to target from src identified by key */
1754                 rc = SMB2_ioctl(xid, tcon, trgtfile->fid.persistent_fid,
1755                         trgtfile->fid.volatile_fid, FSCTL_SRV_COPYCHUNK_WRITE,
1756                         true /* is_fsctl */, (char *)pcchunk,
1757                         sizeof(struct copychunk_ioctl), CIFSMaxBufSize,
1758                         (char **)&retbuf, &ret_data_len);
1759                 if (rc == 0) {
1760                         if (ret_data_len !=
1761                                         sizeof(struct copychunk_ioctl_rsp)) {
1762                                 cifs_tcon_dbg(VFS, "Invalid cchunk response size\n");
1763                                 rc = -EIO;
1764                                 goto cchunk_out;
1765                         }
1766                         if (retbuf->TotalBytesWritten == 0) {
1767                                 cifs_dbg(FYI, "no bytes copied\n");
1768                                 rc = -EIO;
1769                                 goto cchunk_out;
1770                         }
1771                         /*
1772                          * Check if server claimed to write more than we asked
1773                          */
1774                         if (le32_to_cpu(retbuf->TotalBytesWritten) >
1775                             le32_to_cpu(pcchunk->Length)) {
1776                                 cifs_tcon_dbg(VFS, "Invalid copy chunk response\n");
1777                                 rc = -EIO;
1778                                 goto cchunk_out;
1779                         }
1780                         if (le32_to_cpu(retbuf->ChunksWritten) != 1) {
1781                                 cifs_tcon_dbg(VFS, "Invalid num chunks written\n");
1782                                 rc = -EIO;
1783                                 goto cchunk_out;
1784                         }
1785                         chunks_copied++;
1786 
1787                         bytes_written = le32_to_cpu(retbuf->TotalBytesWritten);
1788                         src_off += bytes_written;
1789                         dest_off += bytes_written;
1790                         len -= bytes_written;
1791                         total_bytes_written += bytes_written;
1792 
1793                         cifs_dbg(FYI, "Chunks %d PartialChunk %d Total %zu\n",
1794                                 le32_to_cpu(retbuf->ChunksWritten),
1795                                 le32_to_cpu(retbuf->ChunkBytesWritten),
1796                                 bytes_written);
1797                 } else if (rc == -EINVAL) {
1798                         if (ret_data_len != sizeof(struct copychunk_ioctl_rsp))
1799                                 goto cchunk_out;
1800 
1801                         cifs_dbg(FYI, "MaxChunks %d BytesChunk %d MaxCopy %d\n",
1802                                 le32_to_cpu(retbuf->ChunksWritten),
1803                                 le32_to_cpu(retbuf->ChunkBytesWritten),
1804                                 le32_to_cpu(retbuf->TotalBytesWritten));
1805 
1806                         /*
1807                          * Check if this is the first request using these sizes,
1808                          * (ie check if copy succeed once with original sizes
1809                          * and check if the server gave us different sizes after
1810                          * we already updated max sizes on previous request).
1811                          * if not then why is the server returning an error now
1812                          */
1813                         if ((chunks_copied != 0) || chunk_sizes_updated)
1814                                 goto cchunk_out;
1815 
1816                         /* Check that server is not asking us to grow size */
1817                         if (le32_to_cpu(retbuf->ChunkBytesWritten) <
1818                                         tcon->max_bytes_chunk)
1819                                 tcon->max_bytes_chunk =
1820                                         le32_to_cpu(retbuf->ChunkBytesWritten);
1821                         else
1822                                 goto cchunk_out; /* server gave us bogus size */
1823 
1824                         /* No need to change MaxChunks since already set to 1 */
1825                         chunk_sizes_updated = true;
1826                 } else
1827                         goto cchunk_out;
1828         }
1829 
1830 cchunk_out:
1831         kfree(pcchunk);
1832         kfree(retbuf);
1833         if (rc)
1834                 return rc;
1835         else
1836                 return total_bytes_written;
1837 }
1838 
1839 static int
1840 smb2_flush_file(const unsigned int xid, struct cifs_tcon *tcon,
1841                 struct cifs_fid *fid)
1842 {
1843         return SMB2_flush(xid, tcon, fid->persistent_fid, fid->volatile_fid);
1844 }
1845 
1846 static unsigned int
1847 smb2_read_data_offset(char *buf)
1848 {
1849         struct smb2_read_rsp *rsp = (struct smb2_read_rsp *)buf;
1850 
1851         return rsp->DataOffset;
1852 }
1853 
1854 static unsigned int
1855 smb2_read_data_length(char *buf, bool in_remaining)
1856 {
1857         struct smb2_read_rsp *rsp = (struct smb2_read_rsp *)buf;
1858 
1859         if (in_remaining)
1860                 return le32_to_cpu(rsp->DataRemaining);
1861 
1862         return le32_to_cpu(rsp->DataLength);
1863 }
1864 
1865 
1866 static int
1867 smb2_sync_read(const unsigned int xid, struct cifs_fid *pfid,
1868                struct cifs_io_parms *parms, unsigned int *bytes_read,
1869                char **buf, int *buf_type)
1870 {
1871         parms->persistent_fid = pfid->persistent_fid;
1872         parms->volatile_fid = pfid->volatile_fid;
1873         return SMB2_read(xid, parms, bytes_read, buf, buf_type);
1874 }
1875 
1876 static int
1877 smb2_sync_write(const unsigned int xid, struct cifs_fid *pfid,
1878                 struct cifs_io_parms *parms, unsigned int *written,
1879                 struct kvec *iov, unsigned long nr_segs)
1880 {
1881 
1882         parms->persistent_fid = pfid->persistent_fid;
1883         parms->volatile_fid = pfid->volatile_fid;
1884         return SMB2_write(xid, parms, written, iov, nr_segs);
1885 }
1886 
1887 /* Set or clear the SPARSE_FILE attribute based on value passed in setsparse */
1888 static bool smb2_set_sparse(const unsigned int xid, struct cifs_tcon *tcon,
1889                 struct cifsFileInfo *cfile, struct inode *inode, __u8 setsparse)
1890 {
1891         struct cifsInodeInfo *cifsi;
1892         int rc;
1893 
1894         cifsi = CIFS_I(inode);
1895 
1896         /* if file already sparse don't bother setting sparse again */
1897         if ((cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) && setsparse)
1898                 return true; /* already sparse */
1899 
1900         if (!(cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) && !setsparse)
1901                 return true; /* already not sparse */
1902 
1903         /*
1904          * Can't check for sparse support on share the usual way via the
1905          * FS attribute info (FILE_SUPPORTS_SPARSE_FILES) on the share
1906          * since Samba server doesn't set the flag on the share, yet
1907          * supports the set sparse FSCTL and returns sparse correctly
1908          * in the file attributes. If we fail setting sparse though we
1909          * mark that server does not support sparse files for this share
1910          * to avoid repeatedly sending the unsupported fsctl to server
1911          * if the file is repeatedly extended.
1912          */
1913         if (tcon->broken_sparse_sup)
1914                 return false;
1915 
1916         rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
1917                         cfile->fid.volatile_fid, FSCTL_SET_SPARSE,
1918                         true /* is_fctl */,
1919                         &setsparse, 1, CIFSMaxBufSize, NULL, NULL);
1920         if (rc) {
1921                 tcon->broken_sparse_sup = true;
1922                 cifs_dbg(FYI, "set sparse rc = %d\n", rc);
1923                 return false;
1924         }
1925 
1926         if (setsparse)
1927                 cifsi->cifsAttrs |= FILE_ATTRIBUTE_SPARSE_FILE;
1928         else
1929                 cifsi->cifsAttrs &= (~FILE_ATTRIBUTE_SPARSE_FILE);
1930 
1931         return true;
1932 }
1933 
1934 static int
1935 smb2_set_file_size(const unsigned int xid, struct cifs_tcon *tcon,
1936                    struct cifsFileInfo *cfile, __u64 size, bool set_alloc)
1937 {
1938         __le64 eof = cpu_to_le64(size);
1939         struct inode *inode;
1940 
1941         /*
1942          * If extending file more than one page make sparse. Many Linux fs
1943          * make files sparse by default when extending via ftruncate
1944          */
1945         inode = d_inode(cfile->dentry);
1946 
1947         if (!set_alloc && (size > inode->i_size + 8192)) {
1948                 __u8 set_sparse = 1;
1949 
1950                 /* whether set sparse succeeds or not, extend the file */
1951                 smb2_set_sparse(xid, tcon, cfile, inode, set_sparse);
1952         }
1953 
1954         return SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
1955                             cfile->fid.volatile_fid, cfile->pid, &eof);
1956 }
1957 
1958 static int
1959 smb2_duplicate_extents(const unsigned int xid,
1960                         struct cifsFileInfo *srcfile,
1961                         struct cifsFileInfo *trgtfile, u64 src_off,
1962                         u64 len, u64 dest_off)
1963 {
1964         int rc;
1965         unsigned int ret_data_len;
1966         struct duplicate_extents_to_file dup_ext_buf;
1967         struct cifs_tcon *tcon = tlink_tcon(trgtfile->tlink);
1968 
1969         /* server fileays advertise duplicate extent support with this flag */
1970         if ((le32_to_cpu(tcon->fsAttrInfo.Attributes) &
1971              FILE_SUPPORTS_BLOCK_REFCOUNTING) == 0)
1972                 return -EOPNOTSUPP;
1973 
1974         dup_ext_buf.VolatileFileHandle = srcfile->fid.volatile_fid;
1975         dup_ext_buf.PersistentFileHandle = srcfile->fid.persistent_fid;
1976         dup_ext_buf.SourceFileOffset = cpu_to_le64(src_off);
1977         dup_ext_buf.TargetFileOffset = cpu_to_le64(dest_off);
1978         dup_ext_buf.ByteCount = cpu_to_le64(len);
1979         cifs_dbg(FYI, "Duplicate extents: src off %lld dst off %lld len %lld\n",
1980                 src_off, dest_off, len);
1981 
1982         rc = smb2_set_file_size(xid, tcon, trgtfile, dest_off + len, false);
1983         if (rc)
1984                 goto duplicate_extents_out;
1985 
1986         rc = SMB2_ioctl(xid, tcon, trgtfile->fid.persistent_fid,
1987                         trgtfile->fid.volatile_fid,
1988                         FSCTL_DUPLICATE_EXTENTS_TO_FILE,
1989                         true /* is_fsctl */,
1990                         (char *)&dup_ext_buf,
1991                         sizeof(struct duplicate_extents_to_file),
1992                         CIFSMaxBufSize, NULL,
1993                         &ret_data_len);
1994 
1995         if (ret_data_len > 0)
1996                 cifs_dbg(FYI, "Non-zero response length in duplicate extents\n");
1997 
1998 duplicate_extents_out:
1999         return rc;
2000 }
2001 
2002 static int
2003 smb2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
2004                    struct cifsFileInfo *cfile)
2005 {
2006         return SMB2_set_compression(xid, tcon, cfile->fid.persistent_fid,
2007                             cfile->fid.volatile_fid);
2008 }
2009 
2010 static int
2011 smb3_set_integrity(const unsigned int xid, struct cifs_tcon *tcon,
2012                    struct cifsFileInfo *cfile)
2013 {
2014         struct fsctl_set_integrity_information_req integr_info;
2015         unsigned int ret_data_len;
2016 
2017         integr_info.ChecksumAlgorithm = cpu_to_le16(CHECKSUM_TYPE_UNCHANGED);
2018         integr_info.Flags = 0;
2019         integr_info.Reserved = 0;
2020 
2021         return SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
2022                         cfile->fid.volatile_fid,
2023                         FSCTL_SET_INTEGRITY_INFORMATION,
2024                         true /* is_fsctl */,
2025                         (char *)&integr_info,
2026                         sizeof(struct fsctl_set_integrity_information_req),
2027                         CIFSMaxBufSize, NULL,
2028                         &ret_data_len);
2029 
2030 }
2031 
2032 /* GMT Token is @GMT-YYYY.MM.DD-HH.MM.SS Unicode which is 48 bytes + null */
2033 #define GMT_TOKEN_SIZE 50
2034 
2035 #define MIN_SNAPSHOT_ARRAY_SIZE 16 /* See MS-SMB2 section 3.3.5.15.1 */
2036 
2037 /*
2038  * Input buffer contains (empty) struct smb_snapshot array with size filled in
2039  * For output see struct SRV_SNAPSHOT_ARRAY in MS-SMB2 section 2.2.32.2
2040  */
2041 static int
2042 smb3_enum_snapshots(const unsigned int xid, struct cifs_tcon *tcon,
2043                    struct cifsFileInfo *cfile, void __user *ioc_buf)
2044 {
2045         char *retbuf = NULL;
2046         unsigned int ret_data_len = 0;
2047         int rc;
2048         u32 max_response_size;
2049         struct smb_snapshot_array snapshot_in;
2050 
2051         /*
2052          * On the first query to enumerate the list of snapshots available
2053          * for this volume the buffer begins with 0 (number of snapshots
2054          * which can be returned is zero since at that point we do not know
2055          * how big the buffer needs to be). On the second query,
2056          * it (ret_data_len) is set to number of snapshots so we can
2057          * know to set the maximum response size larger (see below).
2058          */
2059         if (get_user(ret_data_len, (unsigned int __user *)ioc_buf))
2060                 return -EFAULT;
2061 
2062         /*
2063          * Note that for snapshot queries that servers like Azure expect that
2064          * the first query be minimal size (and just used to get the number/size
2065          * of previous versions) so response size must be specified as EXACTLY
2066          * sizeof(struct snapshot_array) which is 16 when rounded up to multiple
2067          * of eight bytes.
2068          */
2069         if (ret_data_len == 0)
2070                 max_response_size = MIN_SNAPSHOT_ARRAY_SIZE;
2071         else
2072                 max_response_size = CIFSMaxBufSize;
2073 
2074         rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
2075                         cfile->fid.volatile_fid,
2076                         FSCTL_SRV_ENUMERATE_SNAPSHOTS,
2077                         true /* is_fsctl */,
2078                         NULL, 0 /* no input data */, max_response_size,
2079                         (char **)&retbuf,
2080                         &ret_data_len);
2081         cifs_dbg(FYI, "enum snaphots ioctl returned %d and ret buflen is %d\n",
2082                         rc, ret_data_len);
2083         if (rc)
2084                 return rc;
2085 
2086         if (ret_data_len && (ioc_buf != NULL) && (retbuf != NULL)) {
2087                 /* Fixup buffer */
2088                 if (copy_from_user(&snapshot_in, ioc_buf,
2089                     sizeof(struct smb_snapshot_array))) {
2090                         rc = -EFAULT;
2091                         kfree(retbuf);
2092                         return rc;
2093                 }
2094 
2095                 /*
2096                  * Check for min size, ie not large enough to fit even one GMT
2097                  * token (snapshot).  On the first ioctl some users may pass in
2098                  * smaller size (or zero) to simply get the size of the array
2099                  * so the user space caller can allocate sufficient memory
2100                  * and retry the ioctl again with larger array size sufficient
2101                  * to hold all of the snapshot GMT tokens on the second try.
2102                  */
2103                 if (snapshot_in.snapshot_array_size < GMT_TOKEN_SIZE)
2104                         ret_data_len = sizeof(struct smb_snapshot_array);
2105 
2106                 /*
2107                  * We return struct SRV_SNAPSHOT_ARRAY, followed by
2108                  * the snapshot array (of 50 byte GMT tokens) each
2109                  * representing an available previous version of the data
2110                  */
2111                 if (ret_data_len > (snapshot_in.snapshot_array_size +
2112                                         sizeof(struct smb_snapshot_array)))
2113                         ret_data_len = snapshot_in.snapshot_array_size +
2114                                         sizeof(struct smb_snapshot_array);
2115 
2116                 if (copy_to_user(ioc_buf, retbuf, ret_data_len))
2117                         rc = -EFAULT;
2118         }
2119 
2120         kfree(retbuf);
2121         return rc;
2122 }
2123 
2124 
2125 
2126 static int
2127 smb3_notify(const unsigned int xid, struct file *pfile,
2128             void __user *ioc_buf)
2129 {
2130         struct smb3_notify notify;
2131         struct dentry *dentry = pfile->f_path.dentry;
2132         struct inode *inode = file_inode(pfile);
2133         struct cifs_sb_info *cifs_sb;
2134         struct cifs_open_parms oparms;
2135         struct cifs_fid fid;
2136         struct cifs_tcon *tcon;
2137         unsigned char *path = NULL;
2138         __le16 *utf16_path = NULL;
2139         u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2140         int rc = 0;
2141 
2142         path = build_path_from_dentry(dentry);
2143         if (path == NULL)
2144                 return -ENOMEM;
2145 
2146         cifs_sb = CIFS_SB(inode->i_sb);
2147 
2148         utf16_path = cifs_convert_path_to_utf16(path + 1, cifs_sb);
2149         if (utf16_path == NULL) {
2150                 rc = -ENOMEM;
2151                 goto notify_exit;
2152         }
2153 
2154         if (copy_from_user(&notify, ioc_buf, sizeof(struct smb3_notify))) {
2155                 rc = -EFAULT;
2156                 goto notify_exit;
2157         }
2158 
2159         tcon = cifs_sb_master_tcon(cifs_sb);
2160         oparms.tcon = tcon;
2161         oparms.desired_access = FILE_READ_ATTRIBUTES | FILE_READ_DATA;
2162         oparms.disposition = FILE_OPEN;
2163         oparms.create_options = cifs_create_options(cifs_sb, 0);
2164         oparms.fid = &fid;
2165         oparms.reconnect = false;
2166 
2167         rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, NULL,
2168                        NULL);
2169         if (rc)
2170                 goto notify_exit;
2171 
2172         rc = SMB2_change_notify(xid, tcon, fid.persistent_fid, fid.volatile_fid,
2173                                 notify.watch_tree, notify.completion_filter);
2174 
2175         SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
2176 
2177         cifs_dbg(FYI, "change notify for path %s rc %d\n", path, rc);
2178 
2179 notify_exit:
2180         kfree(path);
2181         kfree(utf16_path);
2182         return rc;
2183 }
2184 
2185 static int
2186 smb2_query_dir_first(const unsigned int xid, struct cifs_tcon *tcon,
2187                      const char *path, struct cifs_sb_info *cifs_sb,
2188                      struct cifs_fid *fid, __u16 search_flags,
2189                      struct cifs_search_info *srch_inf)
2190 {
2191         __le16 *utf16_path;
2192         struct smb_rqst rqst[2];
2193         struct kvec rsp_iov[2];
2194         int resp_buftype[2];
2195         struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
2196         struct kvec qd_iov[SMB2_QUERY_DIRECTORY_IOV_SIZE];
2197         int rc, flags = 0;
2198         u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2199         struct cifs_open_parms oparms;
2200         struct smb2_query_directory_rsp *qd_rsp = NULL;
2201         struct smb2_create_rsp *op_rsp = NULL;
2202         struct TCP_Server_Info *server = cifs_pick_channel(tcon->ses);
2203 
2204         utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
2205         if (!utf16_path)
2206                 return -ENOMEM;
2207 
2208         if (smb3_encryption_required(tcon))
2209                 flags |= CIFS_TRANSFORM_REQ;
2210 
2211         memset(rqst, 0, sizeof(rqst));
2212         resp_buftype[0] = resp_buftype[1] = CIFS_NO_BUFFER;
2213         memset(rsp_iov, 0, sizeof(rsp_iov));
2214 
2215         /* Open */
2216         memset(&open_iov, 0, sizeof(open_iov));
2217         rqst[0].rq_iov = open_iov;
2218         rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
2219 
2220         oparms.tcon = tcon;
2221         oparms.desired_access = FILE_READ_ATTRIBUTES | FILE_READ_DATA;
2222         oparms.disposition = FILE_OPEN;
2223         oparms.create_options = cifs_create_options(cifs_sb, 0);
2224         oparms.fid = fid;
2225         oparms.reconnect = false;
2226 
2227         rc = SMB2_open_init(tcon, server,
2228                             &rqst[0], &oplock, &oparms, utf16_path);
2229         if (rc)
2230                 goto qdf_free;
2231         smb2_set_next_command(tcon, &rqst[0]);
2232 
2233         /* Query directory */
2234         srch_inf->entries_in_buffer = 0;
2235         srch_inf->index_of_last_entry = 2;
2236 
2237         memset(&qd_iov, 0, sizeof(qd_iov));
2238         rqst[1].rq_iov = qd_iov;
2239         rqst[1].rq_nvec = SMB2_QUERY_DIRECTORY_IOV_SIZE;
2240 
2241         rc = SMB2_query_directory_init(xid, tcon, server,
2242                                        &rqst[1],
2243                                        COMPOUND_FID, COMPOUND_FID,
2244                                        0, srch_inf->info_level);
2245         if (rc)
2246                 goto qdf_free;
2247 
2248         smb2_set_related(&rqst[1]);
2249 
2250         rc = compound_send_recv(xid, tcon->ses, server,
2251                                 flags, 2, rqst,
2252                                 resp_buftype, rsp_iov);
2253 
2254         /* If the open failed there is nothing to do */
2255         op_rsp = (struct smb2_create_rsp *)rsp_iov[0].iov_base;
2256         if (op_rsp == NULL || op_rsp->sync_hdr.Status != STATUS_SUCCESS) {
2257                 cifs_dbg(FYI, "query_dir_first: open failed rc=%d\n", rc);
2258                 goto qdf_free;
2259         }
2260         fid->persistent_fid = op_rsp->PersistentFileId;
2261         fid->volatile_fid = op_rsp->VolatileFileId;
2262 
2263         /* Anything else than ENODATA means a genuine error */
2264         if (rc && rc != -ENODATA) {
2265                 SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
2266                 cifs_dbg(FYI, "query_dir_first: query directory failed rc=%d\n", rc);
2267                 trace_smb3_query_dir_err(xid, fid->persistent_fid,
2268                                          tcon->tid, tcon->ses->Suid, 0, 0, rc);
2269                 goto qdf_free;
2270         }
2271 
2272         atomic_inc(&tcon->num_remote_opens);
2273 
2274         qd_rsp = (struct smb2_query_directory_rsp *)rsp_iov[1].iov_base;
2275         if (qd_rsp->sync_hdr.Status == STATUS_NO_MORE_FILES) {
2276                 trace_smb3_query_dir_done(xid, fid->persistent_fid,
2277                                           tcon->tid, tcon->ses->Suid, 0, 0);
2278                 srch_inf->endOfSearch = true;
2279                 rc = 0;
2280                 goto qdf_free;
2281         }
2282 
2283         rc = smb2_parse_query_directory(tcon, &rsp_iov[1], resp_buftype[1],
2284                                         srch_inf);
2285         if (rc) {
2286                 trace_smb3_query_dir_err(xid, fid->persistent_fid, tcon->tid,
2287                         tcon->ses->Suid, 0, 0, rc);
2288                 goto qdf_free;
2289         }
2290         resp_buftype[1] = CIFS_NO_BUFFER;
2291 
2292         trace_smb3_query_dir_done(xid, fid->persistent_fid, tcon->tid,
2293                         tcon->ses->Suid, 0, srch_inf->entries_in_buffer);
2294 
2295  qdf_free:
2296         kfree(utf16_path);
2297         SMB2_open_free(&rqst[0]);
2298         SMB2_query_directory_free(&rqst[1]);
2299         free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
2300         free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
2301         return rc;
2302 }
2303 
2304 static int
2305 smb2_query_dir_next(const unsigned int xid, struct cifs_tcon *tcon,
2306                     struct cifs_fid *fid, __u16 search_flags,
2307                     struct cifs_search_info *srch_inf)
2308 {
2309         return SMB2_query_directory(xid, tcon, fid->persistent_fid,
2310                                     fid->volatile_fid, 0, srch_inf);
2311 }
2312 
2313 static int
2314 smb2_close_dir(const unsigned int xid, struct cifs_tcon *tcon,
2315                struct cifs_fid *fid)
2316 {
2317         return SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
2318 }
2319 
2320 /*
2321  * If we negotiate SMB2 protocol and get STATUS_PENDING - update
2322  * the number of credits and return true. Otherwise - return false.
2323  */
2324 static bool
2325 smb2_is_status_pending(char *buf, struct TCP_Server_Info *server)
2326 {
2327         struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
2328 
2329         if (shdr->Status != STATUS_PENDING)
2330                 return false;
2331 
2332         if (shdr->CreditRequest) {
2333                 spin_lock(&server->req_lock);
2334                 server->credits += le16_to_cpu(shdr->CreditRequest);
2335                 spin_unlock(&server->req_lock);
2336                 wake_up(&server->request_q);
2337         }
2338 
2339         return true;
2340 }
2341 
2342 static bool
2343 smb2_is_session_expired(char *buf)
2344 {
2345         struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
2346 
2347         if (shdr->Status != STATUS_NETWORK_SESSION_EXPIRED &&
2348             shdr->Status != STATUS_USER_SESSION_DELETED)
2349                 return false;
2350 
2351         trace_smb3_ses_expired(shdr->TreeId, shdr->SessionId,
2352                                le16_to_cpu(shdr->Command),
2353                                le64_to_cpu(shdr->MessageId));
2354         cifs_dbg(FYI, "Session expired or deleted\n");
2355 
2356         return true;
2357 }
2358 
2359 static bool
2360 smb2_is_status_io_timeout(char *buf)
2361 {
2362         struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
2363 
2364         if (shdr->Status == STATUS_IO_TIMEOUT)
2365                 return true;
2366         else
2367                 return false;
2368 }
2369 
2370 static int
2371 smb2_oplock_response(struct cifs_tcon *tcon, struct cifs_fid *fid,
2372                      struct cifsInodeInfo *cinode)
2373 {
2374         if (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_LEASING)
2375                 return SMB2_lease_break(0, tcon, cinode->lease_key,
2376                                         smb2_get_lease_state(cinode));
2377 
2378         return SMB2_oplock_break(0, tcon, fid->persistent_fid,
2379                                  fid->volatile_fid,
2380                                  CIFS_CACHE_READ(cinode) ? 1 : 0);
2381 }
2382 
2383 void
2384 smb2_set_related(struct smb_rqst *rqst)
2385 {
2386         struct smb2_sync_hdr *shdr;
2387 
2388         shdr = (struct smb2_sync_hdr *)(rqst->rq_iov[0].iov_base);
2389         if (shdr == NULL) {
2390                 cifs_dbg(FYI, "shdr NULL in smb2_set_related\n");
2391                 return;
2392         }
2393         shdr->Flags |= SMB2_FLAGS_RELATED_OPERATIONS;
2394 }
2395 
2396 char smb2_padding[7] = {0, 0, 0, 0, 0, 0, 0};
2397 
2398 void
2399 smb2_set_next_command(struct cifs_tcon *tcon, struct smb_rqst *rqst)
2400 {
2401         struct smb2_sync_hdr *shdr;
2402         struct cifs_ses *ses = tcon->ses;
2403         struct TCP_Server_Info *server = ses->server;
2404         unsigned long len = smb_rqst_len(server, rqst);
2405         int i, num_padding;
2406 
2407         shdr = (struct smb2_sync_hdr *)(rqst->rq_iov[0].iov_base);
2408         if (shdr == NULL) {
2409                 cifs_dbg(FYI, "shdr NULL in smb2_set_next_command\n");
2410                 return;
2411         }
2412 
2413         /* SMB headers in a compound are 8 byte aligned. */
2414 
2415         /* No padding needed */
2416         if (!(len & 7))
2417                 goto finished;
2418 
2419         num_padding = 8 - (len & 7);
2420         if (!smb3_encryption_required(tcon)) {
2421                 /*
2422                  * If we do not have encryption then we can just add an extra
2423                  * iov for the padding.
2424                  */
2425                 rqst->rq_iov[rqst->rq_nvec].iov_base = smb2_padding;
2426                 rqst->rq_iov[rqst->rq_nvec].iov_len = num_padding;
2427                 rqst->rq_nvec++;
2428                 len += num_padding;
2429         } else {
2430                 /*
2431                  * We can not add a small padding iov for the encryption case
2432                  * because the encryption framework can not handle the padding
2433                  * iovs.
2434                  * We have to flatten this into a single buffer and add
2435                  * the padding to it.
2436                  */
2437                 for (i = 1; i < rqst->rq_nvec; i++) {
2438                         memcpy(rqst->rq_iov[0].iov_base +
2439                                rqst->rq_iov[0].iov_len,
2440                                rqst->rq_iov[i].iov_base,
2441                                rqst->rq_iov[i].iov_len);
2442                         rqst->rq_iov[0].iov_len += rqst->rq_iov[i].iov_len;
2443                 }
2444                 memset(rqst->rq_iov[0].iov_base + rqst->rq_iov[0].iov_len,
2445                        0, num_padding);
2446                 rqst->rq_iov[0].iov_len += num_padding;
2447                 len += num_padding;
2448                 rqst->rq_nvec = 1;
2449         }
2450 
2451  finished:
2452         shdr->NextCommand = cpu_to_le32(len);
2453 }
2454 
2455 /*
2456  * Passes the query info response back to the caller on success.
2457  * Caller need to free this with free_rsp_buf().
2458  */
2459 int
2460 smb2_query_info_compound(const unsigned int xid, struct cifs_tcon *tcon,
2461                          __le16 *utf16_path, u32 desired_access,
2462                          u32 class, u32 type, u32 output_len,
2463                          struct kvec *rsp, int *buftype,
2464                          struct cifs_sb_info *cifs_sb)
2465 {
2466         struct cifs_ses *ses = tcon->ses;
2467         struct TCP_Server_Info *server = cifs_pick_channel(ses);
2468         int flags = 0;
2469         struct smb_rqst rqst[3];
2470         int resp_buftype[3];
2471         struct kvec rsp_iov[3];
2472         struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
2473         struct kvec qi_iov[1];
2474         struct kvec close_iov[1];
2475         u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2476         struct cifs_open_parms oparms;
2477         struct cifs_fid fid;
2478         int rc;
2479 
2480         if (smb3_encryption_required(tcon))
2481                 flags |= CIFS_TRANSFORM_REQ;
2482 
2483         memset(rqst, 0, sizeof(rqst));
2484         resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
2485         memset(rsp_iov, 0, sizeof(rsp_iov));
2486 
2487         memset(&open_iov, 0, sizeof(open_iov));
2488         rqst[0].rq_iov = open_iov;
2489         rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
2490 
2491         oparms.tcon = tcon;
2492         oparms.desired_access = desired_access;
2493         oparms.disposition = FILE_OPEN;
2494         oparms.create_options = cifs_create_options(cifs_sb, 0);
2495         oparms.fid = &fid;
2496         oparms.reconnect = false;
2497 
2498         rc = SMB2_open_init(tcon, server,
2499                             &rqst[0], &oplock, &oparms, utf16_path);
2500         if (rc)
2501                 goto qic_exit;
2502         smb2_set_next_command(tcon, &rqst[0]);
2503 
2504         memset(&qi_iov, 0, sizeof(qi_iov));
2505         rqst[1].rq_iov = qi_iov;
2506         rqst[1].rq_nvec = 1;
2507 
2508         rc = SMB2_query_info_init(tcon, server,
2509                                   &rqst[1], COMPOUND_FID, COMPOUND_FID,
2510                                   class, type, 0,
2511                                   output_len, 0,
2512                                   NULL);
2513         if (rc)
2514                 goto qic_exit;
2515         smb2_set_next_command(tcon, &rqst[1]);
2516         smb2_set_related(&rqst[1]);
2517 
2518         memset(&close_iov, 0, sizeof(close_iov));
2519         rqst[2].rq_iov = close_iov;
2520         rqst[2].rq_nvec = 1;
2521 
2522         rc = SMB2_close_init(tcon, server,
2523                              &rqst[2], COMPOUND_FID, COMPOUND_FID, false);
2524         if (rc)
2525                 goto qic_exit;
2526         smb2_set_related(&rqst[2]);
2527 
2528         rc = compound_send_recv(xid, ses, server,
2529                                 flags, 3, rqst,
2530                                 resp_buftype, rsp_iov);
2531         if (rc) {
2532                 free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
2533                 if (rc == -EREMCHG) {
2534                         tcon->need_reconnect = true;
2535                         pr_warn_once("server share %s deleted\n",
2536                                      tcon->treeName);
2537                 }
2538                 goto qic_exit;
2539         }
2540         *rsp = rsp_iov[1];
2541         *buftype = resp_buftype[1];
2542 
2543  qic_exit:
2544         SMB2_open_free(&rqst[0]);
2545         SMB2_query_info_free(&rqst[1]);
2546         SMB2_close_free(&rqst[2]);
2547         free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
2548         free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
2549         return rc;
2550 }
2551 
2552 static int
2553 smb2_queryfs(const unsigned int xid, struct cifs_tcon *tcon,
2554              struct cifs_sb_info *cifs_sb, struct kstatfs *buf)
2555 {
2556         struct smb2_query_info_rsp *rsp;
2557         struct smb2_fs_full_size_info *info = NULL;
2558         __le16 utf16_path = 0; /* Null - open root of share */
2559         struct kvec rsp_iov = {NULL, 0};
2560         int buftype = CIFS_NO_BUFFER;
2561         int rc;
2562 
2563 
2564         rc = smb2_query_info_compound(xid, tcon, &utf16_path,
2565                                       FILE_READ_ATTRIBUTES,
2566                                       FS_FULL_SIZE_INFORMATION,
2567                                       SMB2_O_INFO_FILESYSTEM,
2568                                       sizeof(struct smb2_fs_full_size_info),
2569                                       &rsp_iov, &buftype, cifs_sb);
2570         if (rc)
2571                 goto qfs_exit;
2572 
2573         rsp = (struct smb2_query_info_rsp *)rsp_iov.iov_base;
2574         buf->f_type = SMB2_MAGIC_NUMBER;
2575         info = (struct smb2_fs_full_size_info *)(
2576                 le16_to_cpu(rsp->OutputBufferOffset) + (char *)rsp);
2577         rc = smb2_validate_iov(le16_to_cpu(rsp->OutputBufferOffset),
2578                                le32_to_cpu(rsp->OutputBufferLength),
2579                                &rsp_iov,
2580                                sizeof(struct smb2_fs_full_size_info));
2581         if (!rc)
2582                 smb2_copy_fs_info_to_kstatfs(info, buf);
2583 
2584 qfs_exit:
2585         free_rsp_buf(buftype, rsp_iov.iov_base);
2586         return rc;
2587 }
2588 
2589 static int
2590 smb311_queryfs(const unsigned int xid, struct cifs_tcon *tcon,
2591                struct cifs_sb_info *cifs_sb, struct kstatfs *buf)
2592 {
2593         int rc;
2594         __le16 srch_path = 0; /* Null - open root of share */
2595         u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2596         struct cifs_open_parms oparms;
2597         struct cifs_fid fid;
2598 
2599         if (!tcon->posix_extensions)
2600                 return smb2_queryfs(xid, tcon, cifs_sb, buf);
2601 
2602         oparms.tcon = tcon;
2603         oparms.desired_access = FILE_READ_ATTRIBUTES;
2604         oparms.disposition = FILE_OPEN;
2605         oparms.create_options = cifs_create_options(cifs_sb, 0);
2606         oparms.fid = &fid;
2607         oparms.reconnect = false;
2608 
2609         rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL,
2610                        NULL, NULL);
2611         if (rc)
2612                 return rc;
2613 
2614         rc = SMB311_posix_qfs_info(xid, tcon, fid.persistent_fid,
2615                                    fid.volatile_fid, buf);
2616         buf->f_type = SMB2_MAGIC_NUMBER;
2617         SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
2618         return rc;
2619 }
2620 
2621 static bool
2622 smb2_compare_fids(struct cifsFileInfo *ob1, struct cifsFileInfo *ob2)
2623 {
2624         return ob1->fid.persistent_fid == ob2->fid.persistent_fid &&
2625                ob1->fid.volatile_fid == ob2->fid.volatile_fid;
2626 }
2627 
2628 static int
2629 smb2_mand_lock(const unsigned int xid, struct cifsFileInfo *cfile, __u64 offset,
2630                __u64 length, __u32 type, int lock, int unlock, bool wait)
2631 {
2632         if (unlock && !lock)
2633                 type = SMB2_LOCKFLAG_UNLOCK;
2634         return SMB2_lock(xid, tlink_tcon(cfile->tlink),
2635                          cfile->fid.persistent_fid, cfile->fid.volatile_fid,
2636                          current->tgid, length, offset, type, wait);
2637 }
2638 
2639 static void
2640 smb2_get_lease_key(struct inode *inode, struct cifs_fid *fid)
2641 {
2642         memcpy(fid->lease_key, CIFS_I(inode)->lease_key, SMB2_LEASE_KEY_SIZE);
2643 }
2644 
2645 static void
2646 smb2_set_lease_key(struct inode *inode, struct cifs_fid *fid)
2647 {
2648         memcpy(CIFS_I(inode)->lease_key, fid->lease_key, SMB2_LEASE_KEY_SIZE);
2649 }
2650 
2651 static void
2652 smb2_new_lease_key(struct cifs_fid *fid)
2653 {
2654         generate_random_uuid(fid->lease_key);
2655 }
2656 
2657 static int
2658 smb2_get_dfs_refer(const unsigned int xid, struct cifs_ses *ses,
2659                    const char *search_name,
2660                    struct dfs_info3_param **target_nodes,
2661                    unsigned int *num_of_nodes,
2662                    const struct nls_table *nls_codepage, int remap)
2663 {
2664         int rc;
2665         __le16 *utf16_path = NULL;
2666         int utf16_path_len = 0;
2667         struct cifs_tcon *tcon;
2668         struct fsctl_get_dfs_referral_req *dfs_req = NULL;
2669         struct get_dfs_referral_rsp *dfs_rsp = NULL;
2670         u32 dfs_req_size = 0, dfs_rsp_size = 0;
2671 
2672         cifs_dbg(FYI, "%s: path: %s\n", __func__, search_name);
2673 
2674         /*
2675          * Try to use the IPC tcon, otherwise just use any
2676          */
2677         tcon = ses->tcon_ipc;
2678         if (tcon == NULL) {
2679                 spin_lock(&cifs_tcp_ses_lock);
2680                 tcon = list_first_entry_or_null(&ses->tcon_list,
2681                                                 struct cifs_tcon,
2682                                                 tcon_list);
2683                 if (tcon)
2684                         tcon->tc_count++;
2685                 spin_unlock(&cifs_tcp_ses_lock);
2686         }
2687 
2688         if (tcon == NULL) {
2689                 cifs_dbg(VFS, "session %p has no tcon available for a dfs referral request\n",
2690                          ses);
2691                 rc = -ENOTCONN;
2692                 goto out;
2693         }
2694 
2695         utf16_path = cifs_strndup_to_utf16(search_name, PATH_MAX,
2696                                            &utf16_path_len,
2697                                            nls_codepage, remap);
2698         if (!utf16_path) {
2699                 rc = -ENOMEM;
2700                 goto out;
2701         }
2702 
2703         dfs_req_size = sizeof(*dfs_req) + utf16_path_len;
2704         dfs_req = kzalloc(dfs_req_size, GFP_KERNEL);
2705         if (!dfs_req) {
2706                 rc = -ENOMEM;
2707                 goto out;
2708         }
2709 
2710         /* Highest DFS referral version understood */
2711         dfs_req->MaxReferralLevel = DFS_VERSION;
2712 
2713         /* Path to resolve in an UTF-16 null-terminated string */
2714         memcpy(dfs_req->RequestFileName, utf16_path, utf16_path_len);
2715 
2716         do {
2717                 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
2718                                 FSCTL_DFS_GET_REFERRALS,
2719                                 true /* is_fsctl */,
2720                                 (char *)dfs_req, dfs_req_size, CIFSMaxBufSize,
2721                                 (char **)&dfs_rsp, &dfs_rsp_size);
2722         } while (rc == -EAGAIN);
2723 
2724         if (rc) {
2725                 if ((rc != -ENOENT) && (rc != -EOPNOTSUPP))
2726                         cifs_tcon_dbg(VFS, "ioctl error in %s rc=%d\n", __func__, rc);
2727                 goto out;
2728         }
2729 
2730         rc = parse_dfs_referrals(dfs_rsp, dfs_rsp_size,
2731                                  num_of_nodes, target_nodes,
2732                                  nls_codepage, remap, search_name,
2733                                  true /* is_unicode */);
2734         if (rc) {
2735                 cifs_tcon_dbg(VFS, "parse error in %s rc=%d\n", __func__, rc);
2736                 goto out;
2737         }
2738 
2739  out:
2740         if (tcon && !tcon->ipc) {
2741                 /* ipc tcons are not refcounted */
2742                 spin_lock(&cifs_tcp_ses_lock);
2743                 tcon->tc_count--;
2744                 spin_unlock(&cifs_tcp_ses_lock);
2745         }
2746         kfree(utf16_path);
2747         kfree(dfs_req);
2748         kfree(dfs_rsp);
2749         return rc;
2750 }
2751 
2752 static int
2753 parse_reparse_posix(struct reparse_posix_data *symlink_buf,
2754                       u32 plen, char **target_path,
2755                       struct cifs_sb_info *cifs_sb)
2756 {
2757         unsigned int len;
2758 
2759         /* See MS-FSCC 2.1.2.6 for the 'NFS' style reparse tags */
2760         len = le16_to_cpu(symlink_buf->ReparseDataLength);
2761 
2762         if (le64_to_cpu(symlink_buf->InodeType) != NFS_SPECFILE_LNK) {
2763                 cifs_dbg(VFS, "%lld not a supported symlink type\n",
2764                         le64_to_cpu(symlink_buf->InodeType));
2765                 return -EOPNOTSUPP;
2766         }
2767 
2768         *target_path = cifs_strndup_from_utf16(
2769                                 symlink_buf->PathBuffer,
2770                                 len, true, cifs_sb->local_nls);
2771         if (!(*target_path))
2772                 return -ENOMEM;
2773 
2774         convert_delimiter(*target_path, '/');
2775         cifs_dbg(FYI, "%s: target path: %s\n", __func__, *target_path);
2776 
2777         return 0;
2778 }
2779 
2780 static int
2781 parse_reparse_symlink(struct reparse_symlink_data_buffer *symlink_buf,
2782                       u32 plen, char **target_path,
2783                       struct cifs_sb_info *cifs_sb)
2784 {
2785         unsigned int sub_len;
2786         unsigned int sub_offset;
2787 
2788         /* We handle Symbolic Link reparse tag here. See: MS-FSCC 2.1.2.4 */
2789 
2790         sub_offset = le16_to_cpu(symlink_buf->SubstituteNameOffset);
2791         sub_len = le16_to_cpu(symlink_buf->SubstituteNameLength);
2792         if (sub_offset + 20 > plen ||
2793             sub_offset + sub_len + 20 > plen) {
2794                 cifs_dbg(VFS, "srv returned malformed symlink buffer\n");
2795                 return -EIO;
2796         }
2797 
2798         *target_path = cifs_strndup_from_utf16(
2799                                 symlink_buf->PathBuffer + sub_offset,
2800                                 sub_len, true, cifs_sb->local_nls);
2801         if (!(*target_path))
2802                 return -ENOMEM;
2803 
2804         convert_delimiter(*target_path, '/');
2805         cifs_dbg(FYI, "%s: target path: %s\n", __func__, *target_path);
2806 
2807         return 0;
2808 }
2809 
2810 static int
2811 parse_reparse_point(struct reparse_data_buffer *buf,
2812                     u32 plen, char **target_path,
2813                     struct cifs_sb_info *cifs_sb)
2814 {
2815         if (plen < sizeof(struct reparse_data_buffer)) {
2816                 cifs_dbg(VFS, "reparse buffer is too small. Must be at least 8 bytes but was %d\n",
2817                          plen);
2818                 return -EIO;
2819         }
2820 
2821         if (plen < le16_to_cpu(buf->ReparseDataLength) +
2822             sizeof(struct reparse_data_buffer)) {
2823                 cifs_dbg(VFS, "srv returned invalid reparse buf length: %d\n",
2824                          plen);
2825                 return -EIO;
2826         }
2827 
2828         /* See MS-FSCC 2.1.2 */
2829         switch (le32_to_cpu(buf->ReparseTag)) {
2830         case IO_REPARSE_TAG_NFS:
2831                 return parse_reparse_posix(
2832                         (struct reparse_posix_data *)buf,
2833                         plen, target_path, cifs_sb);
2834         case IO_REPARSE_TAG_SYMLINK:
2835                 return parse_reparse_symlink(
2836                         (struct reparse_symlink_data_buffer *)buf,
2837                         plen, target_path, cifs_sb);
2838         default:
2839                 cifs_dbg(VFS, "srv returned unknown symlink buffer tag:0x%08x\n",
2840                          le32_to_cpu(buf->ReparseTag));
2841                 return -EOPNOTSUPP;
2842         }
2843 }
2844 
2845 #define SMB2_SYMLINK_STRUCT_SIZE \
2846         (sizeof(struct smb2_err_rsp) - 1 + sizeof(struct smb2_symlink_err_rsp))
2847 
2848 static int
2849 smb2_query_symlink(const unsigned int xid, struct cifs_tcon *tcon,
2850                    struct cifs_sb_info *cifs_sb, const char *full_path,
2851                    char **target_path, bool is_reparse_point)
2852 {
2853         int rc;
2854         __le16 *utf16_path = NULL;
2855         __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
2856         struct cifs_open_parms oparms;
2857         struct cifs_fid fid;
2858         struct kvec err_iov = {NULL, 0};
2859         struct smb2_err_rsp *err_buf = NULL;
2860         struct smb2_symlink_err_rsp *symlink;
2861         struct TCP_Server_Info *server = cifs_pick_channel(tcon->ses);
2862         unsigned int sub_len;
2863         unsigned int sub_offset;
2864         unsigned int print_len;
2865         unsigned int print_offset;
2866         int flags = 0;
2867         struct smb_rqst rqst[3];
2868         int resp_buftype[3];
2869         struct kvec rsp_iov[3];
2870         struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
2871         struct kvec io_iov[SMB2_IOCTL_IOV_SIZE];
2872         struct kvec close_iov[1];
2873         struct smb2_create_rsp *create_rsp;
2874         struct smb2_ioctl_rsp *ioctl_rsp;
2875         struct reparse_data_buffer *reparse_buf;
2876         int create_options = is_reparse_point ? OPEN_REPARSE_POINT : 0;
2877         u32 plen;
2878 
2879         cifs_dbg(FYI, "%s: path: %s\n", __func__, full_path);
2880 
2881         *target_path = NULL;
2882 
2883         if (smb3_encryption_required(tcon))
2884                 flags |= CIFS_TRANSFORM_REQ;
2885 
2886         memset(rqst, 0, sizeof(rqst));
2887         resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
2888         memset(rsp_iov, 0, sizeof(rsp_iov));
2889 
2890         utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
2891         if (!utf16_path)
2892                 return -ENOMEM;
2893 
2894         /* Open */
2895         memset(&open_iov, 0, sizeof(open_iov));
2896         rqst[0].rq_iov = open_iov;
2897         rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
2898 
2899         memset(&oparms, 0, sizeof(oparms));
2900         oparms.tcon = tcon;
2901         oparms.desired_access = FILE_READ_ATTRIBUTES;
2902         oparms.disposition = FILE_OPEN;
2903         oparms.create_options = cifs_create_options(cifs_sb, create_options);
2904         oparms.fid = &fid;
2905         oparms.reconnect = false;
2906 
2907         rc = SMB2_open_init(tcon, server,
2908                             &rqst[0], &oplock, &oparms, utf16_path);
2909         if (rc)
2910                 goto querty_exit;
2911         smb2_set_next_command(tcon, &rqst[0]);
2912 
2913 
2914         /* IOCTL */
2915         memset(&io_iov, 0, sizeof(io_iov));
2916         rqst[1].rq_iov = io_iov;
2917         rqst[1].rq_nvec = SMB2_IOCTL_IOV_SIZE;
2918 
2919         rc = SMB2_ioctl_init(tcon, server,
2920                              &rqst[1], fid.persistent_fid,
2921                              fid.volatile_fid, FSCTL_GET_REPARSE_POINT,
2922                              true /* is_fctl */, NULL, 0,
2923                              CIFSMaxBufSize -
2924                              MAX_SMB2_CREATE_RESPONSE_SIZE -
2925                              MAX_SMB2_CLOSE_RESPONSE_SIZE);
2926         if (rc)
2927                 goto querty_exit;
2928 
2929         smb2_set_next_command(tcon, &rqst[1]);
2930         smb2_set_related(&rqst[1]);
2931 
2932 
2933         /* Close */
2934         memset(&close_iov, 0, sizeof(close_iov));
2935         rqst[2].rq_iov = close_iov;
2936         rqst[2].rq_nvec = 1;
2937 
2938         rc = SMB2_close_init(tcon, server,
2939                              &rqst[2], COMPOUND_FID, COMPOUND_FID, false);
2940         if (rc)
2941                 goto querty_exit;
2942 
2943         smb2_set_related(&rqst[2]);
2944 
2945         rc = compound_send_recv(xid, tcon->ses, server,
2946                                 flags, 3, rqst,
2947                                 resp_buftype, rsp_iov);
2948 
2949         create_rsp = rsp_iov[0].iov_base;
2950         if (create_rsp && create_rsp->sync_hdr.Status)
2951                 err_iov = rsp_iov[0];
2952         ioctl_rsp = rsp_iov[1].iov_base;
2953 
2954         /*
2955          * Open was successful and we got an ioctl response.
2956          */
2957         if ((rc == 0) && (is_reparse_point)) {
2958                 /* See MS-FSCC 2.3.23 */
2959 
2960                 reparse_buf = (struct reparse_data_buffer *)
2961                         ((char *)ioctl_rsp +
2962                          le32_to_cpu(ioctl_rsp->OutputOffset));
2963                 plen = le32_to_cpu(ioctl_rsp->OutputCount);
2964 
2965                 if (plen + le32_to_cpu(ioctl_rsp->OutputOffset) >
2966                     rsp_iov[1].iov_len) {
2967                         cifs_tcon_dbg(VFS, "srv returned invalid ioctl len: %d\n",
2968                                  plen);
2969                         rc = -EIO;
2970                         goto querty_exit;
2971                 }
2972 
2973                 rc = parse_reparse_point(reparse_buf, plen, target_path,
2974                                          cifs_sb);
2975                 goto querty_exit;
2976         }
2977 
2978         if (!rc || !err_iov.iov_base) {
2979                 rc = -ENOENT;
2980                 goto querty_exit;
2981         }
2982 
2983         err_buf = err_iov.iov_base;
2984         if (le32_to_cpu(err_buf->ByteCount) < sizeof(struct smb2_symlink_err_rsp) ||
2985             err_iov.iov_len < SMB2_SYMLINK_STRUCT_SIZE) {
2986                 rc = -EINVAL;
2987                 goto querty_exit;
2988         }
2989 
2990         symlink = (struct smb2_symlink_err_rsp *)err_buf->ErrorData;
2991         if (le32_to_cpu(symlink->SymLinkErrorTag) != SYMLINK_ERROR_TAG ||
2992             le32_to_cpu(symlink->ReparseTag) != IO_REPARSE_TAG_SYMLINK) {
2993                 rc = -EINVAL;
2994                 goto querty_exit;
2995         }
2996 
2997         /* open must fail on symlink - reset rc */
2998         rc = 0;
2999         sub_len = le16_to_cpu(symlink->SubstituteNameLength);
3000         sub_offset = le16_to_cpu(symlink->SubstituteNameOffset);
3001         print_len = le16_to_cpu(symlink->PrintNameLength);
3002         print_offset = le16_to_cpu(symlink->PrintNameOffset);
3003 
3004         if (err_iov.iov_len < SMB2_SYMLINK_STRUCT_SIZE + sub_offset + sub_len) {
3005                 rc = -EINVAL;
3006                 goto querty_exit;
3007         }
3008 
3009         if (err_iov.iov_len <
3010             SMB2_SYMLINK_STRUCT_SIZE + print_offset + print_len) {
3011                 rc = -EINVAL;
3012                 goto querty_exit;
3013         }
3014 
3015         *target_path = cifs_strndup_from_utf16(
3016                                 (char *)symlink->PathBuffer + sub_offset,
3017                                 sub_len, true, cifs_sb->local_nls);
3018         if (!(*target_path)) {
3019                 rc = -ENOMEM;
3020                 goto querty_exit;
3021         }
3022         convert_delimiter(*target_path, '/');
3023         cifs_dbg(FYI, "%s: target path: %s\n", __func__, *target_path);
3024 
3025  querty_exit:
3026         cifs_dbg(FYI, "query symlink rc %d\n", rc);
3027         kfree(utf16_path);
3028         SMB2_open_free(&rqst[0]);
3029         SMB2_ioctl_free(&rqst[1]);
3030         SMB2_close_free(&rqst[2]);
3031         free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
3032         free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
3033         free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
3034         return rc;
3035 }
3036 
3037 int
3038 smb2_query_reparse_tag(const unsigned int xid, struct cifs_tcon *tcon,
3039                    struct cifs_sb_info *cifs_sb, const char *full_path,
3040                    __u32 *tag)
3041 {
3042         int rc;
3043         __le16 *utf16_path = NULL;
3044         __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
3045         struct cifs_open_parms oparms;
3046         struct cifs_fid fid;
3047         struct TCP_Server_Info *server = cifs_pick_channel(tcon->ses);
3048         int flags = 0;
3049         struct smb_rqst rqst[3];
3050         int resp_buftype[3];
3051         struct kvec rsp_iov[3];
3052         struct kvec open_iov[SMB2_CREATE_IOV_SIZE];
3053         struct kvec io_iov[SMB2_IOCTL_IOV_SIZE];
3054         struct kvec close_iov[1];
3055         struct smb2_ioctl_rsp *ioctl_rsp;
3056         struct reparse_data_buffer *reparse_buf;
3057         u32 plen;
3058 
3059         cifs_dbg(FYI, "%s: path: %s\n", __func__, full_path);
3060 
3061         if (smb3_encryption_required(tcon))
3062                 flags |= CIFS_TRANSFORM_REQ;
3063 
3064         memset(rqst, 0, sizeof(rqst));
3065         resp_buftype[0] = resp_buftype[1] = resp_buftype[2] = CIFS_NO_BUFFER;
3066         memset(rsp_iov, 0, sizeof(rsp_iov));
3067 
3068         utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
3069         if (!utf16_path)
3070                 return -ENOMEM;
3071 
3072         /*
3073          * setup smb2open - TODO add optimization to call cifs_get_readable_path
3074          * to see if there is a handle already open that we can use
3075          */
3076         memset(&open_iov, 0, sizeof(open_iov));
3077         rqst[0].rq_iov = open_iov;
3078         rqst[0].rq_nvec = SMB2_CREATE_IOV_SIZE;
3079 
3080         memset(&oparms, 0, sizeof(oparms));
3081         oparms.tcon = tcon;
3082         oparms.desired_access = FILE_READ_ATTRIBUTES;
3083         oparms.disposition = FILE_OPEN;
3084         oparms.create_options = cifs_create_options(cifs_sb, OPEN_REPARSE_POINT);
3085         oparms.fid = &fid;
3086         oparms.reconnect = false;
3087 
3088         rc = SMB2_open_init(tcon, server,
3089                             &rqst[0], &oplock, &oparms, utf16_path);
3090         if (rc)
3091                 goto query_rp_exit;
3092         smb2_set_next_command(tcon, &rqst[0]);
3093 
3094 
3095         /* IOCTL */
3096         memset(&io_iov, 0, sizeof(io_iov));
3097         rqst[1].rq_iov = io_iov;
3098         rqst[1].rq_nvec = SMB2_IOCTL_IOV_SIZE;
3099 
3100         rc = SMB2_ioctl_init(tcon, server,
3101                              &rqst[1], fid.persistent_fid,
3102                              fid.volatile_fid, FSCTL_GET_REPARSE_POINT,
3103                              true /* is_fctl */, NULL, 0,
3104                              CIFSMaxBufSize -
3105                              MAX_SMB2_CREATE_RESPONSE_SIZE -
3106                              MAX_SMB2_CLOSE_RESPONSE_SIZE);
3107         if (rc)
3108                 goto query_rp_exit;
3109 
3110         smb2_set_next_command(tcon, &rqst[1]);
3111         smb2_set_related(&rqst[1]);
3112 
3113 
3114         /* Close */
3115         memset(&close_iov, 0, sizeof(close_iov));
3116         rqst[2].rq_iov = close_iov;
3117         rqst[2].rq_nvec = 1;
3118 
3119         rc = SMB2_close_init(tcon, server,
3120                              &rqst[2], COMPOUND_FID, COMPOUND_FID, false);
3121         if (rc)
3122                 goto query_rp_exit;
3123 
3124         smb2_set_related(&rqst[2]);
3125 
3126         rc = compound_send_recv(xid, tcon->ses, server,
3127                                 flags, 3, rqst,
3128                                 resp_buftype, rsp_iov);
3129 
3130         ioctl_rsp = rsp_iov[1].iov_base;
3131 
3132         /*
3133          * Open was successful and we got an ioctl response.
3134          */
3135         if (rc == 0) {
3136                 /* See MS-FSCC 2.3.23 */
3137 
3138                 reparse_buf = (struct reparse_data_buffer *)
3139                         ((char *)ioctl_rsp +
3140                          le32_to_cpu(ioctl_rsp->OutputOffset));
3141                 plen = le32_to_cpu(ioctl_rsp->OutputCount);
3142 
3143                 if (plen + le32_to_cpu(ioctl_rsp->OutputOffset) >
3144                     rsp_iov[1].iov_len) {
3145                         cifs_tcon_dbg(FYI, "srv returned invalid ioctl len: %d\n",
3146                                  plen);
3147                         rc = -EIO;
3148                         goto query_rp_exit;
3149                 }
3150                 *tag = le32_to_cpu(reparse_buf->ReparseTag);
3151         }
3152 
3153  query_rp_exit:
3154         kfree(utf16_path);
3155         SMB2_open_free(&rqst[0]);
3156         SMB2_ioctl_free(&rqst[1]);
3157         SMB2_close_free(&rqst[2]);
3158         free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
3159         free_rsp_buf(resp_buftype[1], rsp_iov[1].iov_base);
3160         free_rsp_buf(resp_buftype[2], rsp_iov[2].iov_base);
3161         return rc;
3162 }
3163 
3164 static struct cifs_ntsd *
3165 get_smb2_acl_by_fid(struct cifs_sb_info *cifs_sb,
3166                 const struct cifs_fid *cifsfid, u32 *pacllen)
3167 {
3168         struct cifs_ntsd *pntsd = NULL;
3169         unsigned int xid;
3170         int rc = -EOPNOTSUPP;
3171         struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
3172 
3173         if (IS_ERR(tlink))
3174                 return ERR_CAST(tlink);
3175 
3176         xid = get_xid();
3177         cifs_dbg(FYI, "trying to get acl\n");
3178 
3179         rc = SMB2_query_acl(xid, tlink_tcon(tlink), cifsfid->persistent_fid,
3180                             cifsfid->volatile_fid, (void **)&pntsd, pacllen);
3181         free_xid(xid);
3182 
3183         cifs_put_tlink(tlink);
3184 
3185         cifs_dbg(FYI, "%s: rc = %d ACL len %d\n", __func__, rc, *pacllen);
3186         if (rc)
3187                 return ERR_PTR(rc);
3188         return pntsd;
3189 
3190 }
3191 
3192 static struct cifs_ntsd *
3193 get_smb2_acl_by_path(struct cifs_sb_info *cifs_sb,
3194                 const char *path, u32 *pacllen)
3195 {
3196         struct cifs_ntsd *pntsd = NULL;
3197         u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
3198         unsigned int xid;
3199         int rc;
3200         struct cifs_tcon *tcon;
3201         struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
3202         struct cifs_fid fid;
3203         struct cifs_open_parms oparms;
3204         __le16 *utf16_path;
3205 
3206         cifs_dbg(FYI, "get smb3 acl for path %s\n", path);
3207         if (IS_ERR(tlink))
3208                 return ERR_CAST(tlink);
3209 
3210         tcon = tlink_tcon(tlink);
3211         xid = get_xid();
3212 
3213         utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
3214         if (!utf16_path) {
3215                 rc = -ENOMEM;
3216                 free_xid(xid);
3217                 return ERR_PTR(rc);
3218         }
3219 
3220         oparms.tcon = tcon;
3221         oparms.desired_access = READ_CONTROL;
3222         oparms.disposition = FILE_OPEN;
3223         /*
3224          * When querying an ACL, even if the file is a symlink we want to open
3225          * the source not the target, and so the protocol requires that the
3226          * client specify this flag when opening a reparse point
3227          */
3228         oparms.create_options = cifs_create_options(cifs_sb, 0) | OPEN_REPARSE_POINT;
3229         oparms.fid = &fid;
3230         oparms.reconnect = false;
3231 
3232         rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL, NULL,
3233                        NULL);
3234         kfree(utf16_path);
3235         if (!rc) {
3236                 rc = SMB2_query_acl(xid, tlink_tcon(tlink), fid.persistent_fid,
3237                             fid.volatile_fid, (void **)&pntsd, pacllen);
3238                 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
3239         }
3240 
3241         cifs_put_tlink(tlink);
3242         free_xid(xid);
3243 
3244         cifs_dbg(FYI, "%s: rc = %d ACL len %d\n", __func__, rc, *pacllen);
3245         if (rc)
3246                 return ERR_PTR(rc);
3247         return pntsd;
3248 }
3249 
3250 static int
3251 set_smb2_acl(struct cifs_ntsd *pnntsd, __u32 acllen,
3252                 struct inode *inode, const char *path, int aclflag)
3253 {
3254         u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
3255         unsigned int xid;
3256         int rc, access_flags = 0;
3257         struct cifs_tcon *tcon;
3258         struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
3259         struct tcon_link *tlink = cifs_sb_tlink(cifs_sb);
3260         struct cifs_fid fid;
3261         struct cifs_open_parms oparms;
3262         __le16 *utf16_path;
3263 
3264         cifs_dbg(FYI, "set smb3 acl for path %s\n", path);
3265         if (IS_ERR(tlink))
3266                 return PTR_ERR(tlink);
3267 
3268         tcon = tlink_tcon(tlink);
3269         xid = get_xid();
3270 
3271         if (aclflag == CIFS_ACL_OWNER || aclflag == CIFS_ACL_GROUP)
3272                 access_flags = WRITE_OWNER;
3273         else
3274                 access_flags = WRITE_DAC;
3275 
3276         utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
3277         if (!utf16_path) {
3278                 rc = -ENOMEM;
3279                 free_xid(xid);
3280                 return rc;
3281         }
3282 
3283         oparms.tcon = tcon;
3284         oparms.desired_access = access_flags;
3285         oparms.create_options = cifs_create_options(cifs_sb, 0);
3286         oparms.disposition = FILE_OPEN;
3287         oparms.path = path;
3288         oparms.fid = &fid;
3289         oparms.reconnect = false;
3290 
3291         rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL,
3292                        NULL, NULL);
3293         kfree(utf16_path);
3294         if (!rc) {
3295                 rc = SMB2_set_acl(xid, tlink_tcon(tlink), fid.persistent_fid,
3296                             fid.volatile_fid, pnntsd, acllen, aclflag);
3297                 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
3298         }
3299 
3300         cifs_put_tlink(tlink);
3301         free_xid(xid);
3302         return rc;
3303 }
3304 
3305 /* Retrieve an ACL from the server */
3306 static struct cifs_ntsd *
3307 get_smb2_acl(struct cifs_sb_info *cifs_sb,
3308                                       struct inode *inode, const char *path,
3309                                       u32 *pacllen)
3310 {
3311         struct cifs_ntsd *pntsd = NULL;
3312         struct cifsFileInfo *open_file = NULL;
3313 
3314         if (inode)
3315                 open_file = find_readable_file(CIFS_I(inode), true);
3316         if (!open_file)
3317                 return get_smb2_acl_by_path(cifs_sb, path, pacllen);
3318 
3319         pntsd = get_smb2_acl_by_fid(cifs_sb, &open_file->fid, pacllen);
3320         cifsFileInfo_put(open_file);
3321         return pntsd;
3322 }
3323 
3324 static long smb3_zero_range(struct file *file, struct cifs_tcon *tcon,
3325                             loff_t offset, loff_t len, bool keep_size)
3326 {
3327         struct cifs_ses *ses = tcon->ses;
3328         struct inode *inode;
3329         struct cifsInodeInfo *cifsi;
3330         struct cifsFileInfo *cfile = file->private_data;
3331         struct file_zero_data_information fsctl_buf;
3332         long rc;
3333         unsigned int xid;
3334         __le64 eof;
3335 
3336         xid = get_xid();
3337 
3338         inode = d_inode(cfile->dentry);
3339         cifsi = CIFS_I(inode);
3340 
3341         trace_smb3_zero_enter(xid, cfile->fid.persistent_fid, tcon->tid,
3342                               ses->Suid, offset, len);
3343 
3344         /*
3345          * We zero the range through ioctl, so we need remove the page caches
3346          * first, otherwise the data may be inconsistent with the server.
3347          */
3348         truncate_pagecache_range(inode, offset, offset + len - 1);
3349 
3350         /* if file not oplocked can't be sure whether asking to extend size */
3351         if (!CIFS_CACHE_READ(cifsi))
3352                 if (keep_size == false) {
3353                         rc = -EOPNOTSUPP;
3354                         trace_smb3_zero_err(xid, cfile->fid.persistent_fid,
3355                                 tcon->tid, ses->Suid, offset, len, rc);
3356                         free_xid(xid);
3357                         return rc;
3358                 }
3359 
3360         cifs_dbg(FYI, "Offset %lld len %lld\n", offset, len);
3361 
3362         fsctl_buf.FileOffset = cpu_to_le64(offset);
3363         fsctl_buf.BeyondFinalZero = cpu_to_le64(offset + len);
3364 
3365         rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
3366                         cfile->fid.volatile_fid, FSCTL_SET_ZERO_DATA, true,
3367                         (char *)&fsctl_buf,
3368                         sizeof(struct file_zero_data_information),
3369                         0, NULL, NULL);
3370         if (rc)
3371                 goto zero_range_exit;
3372 
3373         /*
3374          * do we also need to change the size of the file?
3375          */
3376         if (keep_size == false && i_size_read(inode) < offset + len) {
3377                 eof = cpu_to_le64(offset + len);
3378                 rc = SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
3379                                   cfile->fid.volatile_fid, cfile->pid, &eof);
3380         }
3381 
3382  zero_range_exit:
3383         free_xid(xid);
3384         if (rc)
3385                 trace_smb3_zero_err(xid, cfile->fid.persistent_fid, tcon->tid,
3386                               ses->Suid, offset, len, rc);
3387         else
3388                 trace_smb3_zero_done(xid, cfile->fid.persistent_fid, tcon->tid,
3389                               ses->Suid, offset, len);
3390         return rc;
3391 }
3392 
3393 static long smb3_punch_hole(struct file *file, struct cifs_tcon *tcon,
3394                             loff_t offset, loff_t len)
3395 {
3396         struct inode *inode;
3397         struct cifsFileInfo *cfile = file->private_data;
3398         struct file_zero_data_information fsctl_buf;
3399         long rc;
3400         unsigned int xid;
3401         __u8 set_sparse = 1;
3402 
3403         xid = get_xid();
3404 
3405         inode = d_inode(cfile->dentry);
3406 
3407         /* Need to make file sparse, if not already, before freeing range. */
3408         /* Consider adding equivalent for compressed since it could also work */
3409         if (!smb2_set_sparse(xid, tcon, cfile, inode, set_sparse)) {
3410                 rc = -EOPNOTSUPP;
3411                 free_xid(xid);
3412                 return rc;
3413         }
3414 
3415         /*
3416          * We implement the punch hole through ioctl, so we need remove the page
3417          * caches first, otherwise the data may be inconsistent with the server.
3418          */
3419         truncate_pagecache_range(inode, offset, offset + len - 1);
3420 
3421         cifs_dbg(FYI, "Offset %lld len %lld\n", offset, len);
3422 
3423         fsctl_buf.FileOffset = cpu_to_le64(offset);
3424         fsctl_buf.BeyondFinalZero = cpu_to_le64(offset + len);
3425 
3426         rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
3427                         cfile->fid.volatile_fid, FSCTL_SET_ZERO_DATA,
3428                         true /* is_fctl */, (char *)&fsctl_buf,
3429                         sizeof(struct file_zero_data_information),
3430                         CIFSMaxBufSize, NULL, NULL);
3431         free_xid(xid);
3432         return rc;
3433 }
3434 
3435 static long smb3_simple_falloc(struct file *file, struct cifs_tcon *tcon,
3436                             loff_t off, loff_t len, bool keep_size)
3437 {
3438         struct inode *inode;
3439         struct cifsInodeInfo *cifsi;
3440         struct cifsFileInfo *cfile = file->private_data;
3441         long rc = -EOPNOTSUPP;
3442         unsigned int xid;
3443         __le64 eof;
3444 
3445         xid = get_xid();
3446 
3447         inode = d_inode(cfile->dentry);
3448         cifsi = CIFS_I(inode);
3449 
3450         trace_smb3_falloc_enter(xid, cfile->fid.persistent_fid, tcon->tid,
3451                                 tcon->ses->Suid, off, len);
3452         /* if file not oplocked can't be sure whether asking to extend size */
3453         if (!CIFS_CACHE_READ(cifsi))
3454                 if (keep_size == false) {
3455                         trace_smb3_falloc_err(xid, cfile->fid.persistent_fid,
3456                                 tcon->tid, tcon->ses->Suid, off, len, rc);
3457                         free_xid(xid);
3458                         return rc;
3459                 }
3460 
3461         /*
3462          * Extending the file
3463          */
3464         if ((keep_size == false) && i_size_read(inode) < off + len) {
3465                 rc = inode_newsize_ok(inode, off + len);
3466                 if (rc)
3467                         goto out;
3468 
3469                 if ((cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) == 0)
3470                         smb2_set_sparse(xid, tcon, cfile, inode, false);
3471 
3472                 eof = cpu_to_le64(off + len);
3473                 rc = SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
3474                                   cfile->fid.volatile_fid, cfile->pid, &eof);
3475                 if (rc == 0) {
3476                         cifsi->server_eof = off + len;
3477                         cifs_setsize(inode, off + len);
3478                         cifs_truncate_page(inode->i_mapping, inode->i_size);
3479                         truncate_setsize(inode, off + len);
3480                 }
3481                 goto out;
3482         }
3483 
3484         /*
3485          * Files are non-sparse by default so falloc may be a no-op
3486          * Must check if file sparse. If not sparse, and since we are not
3487          * extending then no need to do anything since file already allocated
3488          */
3489         if ((cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) == 0) {
3490                 rc = 0;
3491                 goto out;
3492         }
3493 
3494         if ((keep_size == true) || (i_size_read(inode) >= off + len)) {
3495                 /*
3496                  * Check if falloc starts within first few pages of file
3497                  * and ends within a few pages of the end of file to
3498                  * ensure that most of file is being forced to be
3499                  * fallocated now. If so then setting whole file sparse
3500                  * ie potentially making a few extra pages at the beginning
3501                  * or end of the file non-sparse via set_sparse is harmless.
3502                  */
3503                 if ((off > 8192) || (off + len + 8192 < i_size_read(inode))) {
3504                         rc = -EOPNOTSUPP;
3505                         goto out;
3506                 }
3507         }
3508 
3509         smb2_set_sparse(xid, tcon, cfile, inode, false);
3510         rc = 0;
3511 
3512 out:
3513         if (rc)
3514                 trace_smb3_falloc_err(xid, cfile->fid.persistent_fid, tcon->tid,
3515                                 tcon->ses->Suid, off, len, rc);
3516         else
3517                 trace_smb3_falloc_done(xid, cfile->fid.persistent_fid, tcon->tid,
3518                                 tcon->ses->Suid, off, len);
3519 
3520         free_xid(xid);
3521         return rc;
3522 }
3523 
3524 static loff_t smb3_llseek(struct file *file, struct cifs_tcon *tcon, loff_t offset, int whence)
3525 {
3526         struct cifsFileInfo *wrcfile, *cfile = file->private_data;
3527         struct cifsInodeInfo *cifsi;
3528         struct inode *inode;
3529         int rc = 0;
3530         struct file_allocated_range_buffer in_data, *out_data = NULL;
3531         u32 out_data_len;
3532         unsigned int xid;
3533 
3534         if (whence != SEEK_HOLE && whence != SEEK_DATA)
3535                 return generic_file_llseek(file, offset, whence);
3536 
3537         inode = d_inode(cfile->dentry);
3538         cifsi = CIFS_I(inode);
3539 
3540         if (offset < 0 || offset >= i_size_read(inode))
3541                 return -ENXIO;
3542 
3543         xid = get_xid();
3544         /*
3545          * We need to be sure that all dirty pages are written as they
3546          * might fill holes on the server.
3547          * Note that we also MUST flush any written pages since at least
3548          * some servers (Windows2016) will not reflect recent writes in
3549          * QUERY_ALLOCATED_RANGES until SMB2_flush is called.
3550          */
3551         wrcfile = find_writable_file(cifsi, FIND_WR_ANY);
3552         if (wrcfile) {
3553                 filemap_write_and_wait(inode->i_mapping);
3554                 smb2_flush_file(xid, tcon, &wrcfile->fid);
3555                 cifsFileInfo_put(wrcfile);
3556         }
3557 
3558         if (!(cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE)) {
3559                 if (whence == SEEK_HOLE)
3560                         offset = i_size_read(inode);
3561                 goto lseek_exit;
3562         }
3563 
3564         in_data.file_offset = cpu_to_le64(offset);
3565         in_data.length = cpu_to_le64(i_size_read(inode));
3566 
3567         rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
3568                         cfile->fid.volatile_fid,
3569                         FSCTL_QUERY_ALLOCATED_RANGES, true,
3570                         (char *)&in_data, sizeof(in_data),
3571                         sizeof(struct file_allocated_range_buffer),
3572                         (char **)&out_data, &out_data_len);
3573         if (rc == -E2BIG)
3574                 rc = 0;
3575         if (rc)
3576                 goto lseek_exit;
3577 
3578         if (whence == SEEK_HOLE && out_data_len == 0)
3579                 goto lseek_exit;
3580 
3581         if (whence == SEEK_DATA && out_data_len == 0) {
3582                 rc = -ENXIO;
3583                 goto lseek_exit;
3584         }
3585 
3586         if (out_data_len < sizeof(struct file_allocated_range_buffer)) {
3587                 rc = -EINVAL;
3588                 goto lseek_exit;
3589         }
3590         if (whence == SEEK_DATA) {
3591                 offset = le64_to_cpu(out_data->file_offset);
3592                 goto lseek_exit;
3593         }
3594         if (offset < le64_to_cpu(out_data->file_offset))
3595                 goto lseek_exit;
3596 
3597         offset = le64_to_cpu(out_data->file_offset) + le64_to_cpu(out_data->length);
3598 
3599  lseek_exit:
3600         free_xid(xid);
3601         kfree(out_data);
3602         if (!rc)
3603                 return vfs_setpos(file, offset, inode->i_sb->s_maxbytes);
3604         else
3605                 return rc;
3606 }
3607 
3608 static int smb3_fiemap(struct cifs_tcon *tcon,
3609                        struct cifsFileInfo *cfile,
3610                        struct fiemap_extent_info *fei, u64 start, u64 len)
3611 {
3612         unsigned int xid;
3613         struct file_allocated_range_buffer in_data, *out_data;
3614         u32 out_data_len;
3615         int i, num, rc, flags, last_blob;
3616         u64 next;
3617 
3618         rc = fiemap_prep(d_inode(cfile->dentry), fei, start, &len, 0);
3619         if (rc)
3620                 return rc;
3621 
3622         xid = get_xid();
3623  again:
3624         in_data.file_offset = cpu_to_le64(start);
3625         in_data.length = cpu_to_le64(len);
3626 
3627         rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
3628                         cfile->fid.volatile_fid,
3629                         FSCTL_QUERY_ALLOCATED_RANGES, true,
3630                         (char *)&in_data, sizeof(in_data),
3631                         1024 * sizeof(struct file_allocated_range_buffer),
3632                         (char **)&out_data, &out_data_len);
3633         if (rc == -E2BIG) {
3634                 last_blob = 0;
3635                 rc = 0;
3636         } else
3637                 last_blob = 1;
3638         if (rc)
3639                 goto out;
3640 
3641         if (out_data_len && out_data_len < sizeof(struct file_allocated_range_buffer)) {
3642                 rc = -EINVAL;
3643                 goto out;
3644         }
3645         if (out_data_len % sizeof(struct file_allocated_range_buffer)) {
3646                 rc = -EINVAL;
3647                 goto out;
3648         }
3649 
3650         num = out_data_len / sizeof(struct file_allocated_range_buffer);
3651         for (i = 0; i < num; i++) {
3652                 flags = 0;
3653                 if (i == num - 1 && last_blob)
3654                         flags |= FIEMAP_EXTENT_LAST;
3655 
3656                 rc = fiemap_fill_next_extent(fei,
3657                                 le64_to_cpu(out_data[i].file_offset),
3658                                 le64_to_cpu(out_data[i].file_offset),
3659                                 le64_to_cpu(out_data[i].length),
3660                                 flags);
3661                 if (rc < 0)
3662                         goto out;
3663                 if (rc == 1) {
3664                         rc = 0;
3665                         goto out;
3666                 }
3667         }
3668 
3669         if (!last_blob) {
3670                 next = le64_to_cpu(out_data[num - 1].file_offset) +
3671                   le64_to_cpu(out_data[num - 1].length);
3672                 len = len - (next - start);
3673                 start = next;
3674                 goto again;
3675         }
3676 
3677  out:
3678         free_xid(xid);
3679         kfree(out_data);
3680         return rc;
3681 }
3682 
3683 static long smb3_fallocate(struct file *file, struct cifs_tcon *tcon, int mode,
3684                            loff_t off, loff_t len)
3685 {
3686         /* KEEP_SIZE already checked for by do_fallocate */
3687         if (mode & FALLOC_FL_PUNCH_HOLE)
3688                 return smb3_punch_hole(file, tcon, off, len);
3689         else if (mode & FALLOC_FL_ZERO_RANGE) {
3690                 if (mode & FALLOC_FL_KEEP_SIZE)
3691                         return smb3_zero_range(file, tcon, off, len, true);
3692                 return smb3_zero_range(file, tcon, off, len, false);
3693         } else if (mode == FALLOC_FL_KEEP_SIZE)
3694                 return smb3_simple_falloc(file, tcon, off, len, true);
3695         else if (mode == 0)
3696                 return smb3_simple_falloc(file, tcon, off, len, false);
3697 
3698         return -EOPNOTSUPP;
3699 }
3700 
3701 static void
3702 smb2_downgrade_oplock(struct TCP_Server_Info *server,
3703                       struct cifsInodeInfo *cinode, __u32 oplock,
3704                       unsigned int epoch, bool *purge_cache)
3705 {
3706         server->ops->set_oplock_level(cinode, oplock, 0, NULL);
3707 }
3708 
3709 static void
3710 smb21_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
3711                        unsigned int epoch, bool *purge_cache);
3712 
3713 static void
3714 smb3_downgrade_oplock(struct TCP_Server_Info *server,
3715                        struct cifsInodeInfo *cinode, __u32 oplock,
3716                        unsigned int epoch, bool *purge_cache)
3717 {
3718         unsigned int old_state = cinode->oplock;
3719         unsigned int old_epoch = cinode->epoch;
3720         unsigned int new_state;
3721 
3722         if (epoch > old_epoch) {
3723                 smb21_set_oplock_level(cinode, oplock, 0, NULL);
3724                 cinode->epoch = epoch;
3725         }
3726 
3727         new_state = cinode->oplock;
3728         *purge_cache = false;
3729 
3730         if ((old_state & CIFS_CACHE_READ_FLG) != 0 &&
3731             (new_state & CIFS_CACHE_READ_FLG) == 0)
3732                 *purge_cache = true;
3733         else if (old_state == new_state && (epoch - old_epoch > 1))
3734                 *purge_cache = true;
3735 }
3736 
3737 static void
3738 smb2_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
3739                       unsigned int epoch, bool *purge_cache)
3740 {
3741         oplock &= 0xFF;
3742         if (oplock == SMB2_OPLOCK_LEVEL_NOCHANGE)
3743                 return;
3744         if (oplock == SMB2_OPLOCK_LEVEL_BATCH) {
3745                 cinode->oplock = CIFS_CACHE_RHW_FLG;
3746                 cifs_dbg(FYI, "Batch Oplock granted on inode %p\n",
3747                          &cinode->vfs_inode);
3748         } else if (oplock == SMB2_OPLOCK_LEVEL_EXCLUSIVE) {
3749                 cinode->oplock = CIFS_CACHE_RW_FLG;
3750                 cifs_dbg(FYI, "Exclusive Oplock granted on inode %p\n",
3751                          &cinode->vfs_inode);
3752         } else if (oplock == SMB2_OPLOCK_LEVEL_II) {
3753                 cinode->oplock = CIFS_CACHE_READ_FLG;
3754                 cifs_dbg(FYI, "Level II Oplock granted on inode %p\n",
3755                          &cinode->vfs_inode);
3756         } else
3757                 cinode->oplock = 0;
3758 }
3759 
3760 static void
3761 smb21_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
3762                        unsigned int epoch, bool *purge_cache)
3763 {
3764         char message[5] = {0};
3765         unsigned int new_oplock = 0;
3766 
3767         oplock &= 0xFF;
3768         if (oplock == SMB2_OPLOCK_LEVEL_NOCHANGE)
3769                 return;
3770 
3771         /* Check if the server granted an oplock rather than a lease */
3772         if (oplock & SMB2_OPLOCK_LEVEL_EXCLUSIVE)
3773                 return smb2_set_oplock_level(cinode, oplock, epoch,
3774                                              purge_cache);
3775 
3776         if (oplock & SMB2_LEASE_READ_CACHING_HE) {
3777                 new_oplock |= CIFS_CACHE_READ_FLG;
3778                 strcat(message, "R");
3779         }
3780         if (oplock & SMB2_LEASE_HANDLE_CACHING_HE) {
3781                 new_oplock |= CIFS_CACHE_HANDLE_FLG;
3782                 strcat(message, "H");
3783         }
3784         if (oplock & SMB2_LEASE_WRITE_CACHING_HE) {
3785                 new_oplock |= CIFS_CACHE_WRITE_FLG;
3786                 strcat(message, "W");
3787         }
3788         if (!new_oplock)
3789                 strncpy(message, "None", sizeof(message));
3790 
3791         cinode->oplock = new_oplock;
3792         cifs_dbg(FYI, "%s Lease granted on inode %p\n", message,
3793                  &cinode->vfs_inode);
3794 }
3795 
3796 static void
3797 smb3_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
3798                       unsigned int epoch, bool *purge_cache)
3799 {
3800         unsigned int old_oplock = cinode->oplock;
3801 
3802         smb21_set_oplock_level(cinode, oplock, epoch, purge_cache);
3803 
3804         if (purge_cache) {
3805                 *purge_cache = false;
3806                 if (old_oplock == CIFS_CACHE_READ_FLG) {
3807                         if (cinode->oplock == CIFS_CACHE_READ_FLG &&
3808                             (epoch - cinode->epoch > 0))
3809                                 *purge_cache = true;
3810                         else if (cinode->oplock == CIFS_CACHE_RH_FLG &&
3811                                  (epoch - cinode->epoch > 1))
3812                                 *purge_cache = true;
3813                         else if (cinode->oplock == CIFS_CACHE_RHW_FLG &&
3814                                  (epoch - cinode->epoch > 1))
3815                                 *purge_cache = true;
3816                         else if (cinode->oplock == 0 &&
3817                                  (epoch - cinode->epoch > 0))
3818                                 *purge_cache = true;
3819                 } else if (old_oplock == CIFS_CACHE_RH_FLG) {
3820                         if (cinode->oplock == CIFS_CACHE_RH_FLG &&
3821                             (epoch - cinode->epoch > 0))
3822                                 *purge_cache = true;
3823                         else if (cinode->oplock == CIFS_CACHE_RHW_FLG &&
3824                                  (epoch - cinode->epoch > 1))
3825                                 *purge_cache = true;
3826                 }
3827                 cinode->epoch = epoch;
3828         }
3829 }
3830 
3831 static bool
3832 smb2_is_read_op(__u32 oplock)
3833 {
3834         return oplock == SMB2_OPLOCK_LEVEL_II;
3835 }
3836 
3837 static bool
3838 smb21_is_read_op(__u32 oplock)
3839 {
3840         return (oplock & SMB2_LEASE_READ_CACHING_HE) &&
3841                !(oplock & SMB2_LEASE_WRITE_CACHING_HE);
3842 }
3843 
3844 static __le32
3845 map_oplock_to_lease(u8 oplock)
3846 {
3847         if (oplock == SMB2_OPLOCK_LEVEL_EXCLUSIVE)
3848                 return SMB2_LEASE_WRITE_CACHING | SMB2_LEASE_READ_CACHING;
3849         else if (oplock == SMB2_OPLOCK_LEVEL_II)
3850                 return SMB2_LEASE_READ_CACHING;
3851         else if (oplock == SMB2_OPLOCK_LEVEL_BATCH)
3852                 return SMB2_LEASE_HANDLE_CACHING | SMB2_LEASE_READ_CACHING |
3853                        SMB2_LEASE_WRITE_CACHING;
3854         return 0;
3855 }
3856 
3857 static char *
3858 smb2_create_lease_buf(u8 *lease_key, u8 oplock)
3859 {
3860         struct create_lease *buf;
3861 
3862         buf = kzalloc(sizeof(struct create_lease), GFP_KERNEL);
3863         if (!buf)
3864                 return NULL;
3865 
3866         memcpy(&buf->lcontext.LeaseKey, lease_key, SMB2_LEASE_KEY_SIZE);
3867         buf->lcontext.LeaseState = map_oplock_to_lease(oplock);
3868 
3869         buf->ccontext.DataOffset = cpu_to_le16(offsetof
3870                                         (struct create_lease, lcontext));
3871         buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context));
3872         buf->ccontext.NameOffset = cpu_to_le16(offsetof
3873                                 (struct create_lease, Name));
3874         buf->ccontext.NameLength = cpu_to_le16(4);
3875         /* SMB2_CREATE_REQUEST_LEASE is "RqLs" */
3876         buf->Name[0] = 'R';
3877         buf->Name[1] = 'q';
3878         buf->Name[2] = 'L';
3879         buf->Name[3] = 's';
3880         return (char *)buf;
3881 }
3882 
3883 static char *
3884 smb3_create_lease_buf(u8 *lease_key, u8 oplock)
3885 {
3886         struct create_lease_v2 *buf;
3887 
3888         buf = kzalloc(sizeof(struct create_lease_v2), GFP_KERNEL);
3889         if (!buf)
3890                 return NULL;
3891 
3892         memcpy(&buf->lcontext.LeaseKey, lease_key, SMB2_LEASE_KEY_SIZE);
3893         buf->lcontext.LeaseState = map_oplock_to_lease(oplock);
3894 
3895         buf->ccontext.DataOffset = cpu_to_le16(offsetof
3896                                         (struct create_lease_v2, lcontext));
3897         buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context_v2));
3898         buf->ccontext.NameOffset = cpu_to_le16(offsetof
3899                                 (struct create_lease_v2, Name));
3900         buf->ccontext.NameLength = cpu_to_le16(4);
3901         /* SMB2_CREATE_REQUEST_LEASE is "RqLs" */
3902         buf->Name[0] = 'R';
3903         buf->Name[1] = 'q';
3904         buf->Name[2] = 'L';
3905         buf->Name[3] = 's';
3906         return (char *)buf;
3907 }
3908 
3909 static __u8
3910 smb2_parse_lease_buf(void *buf, unsigned int *epoch, char *lease_key)
3911 {
3912         struct create_lease *lc = (struct create_lease *)buf;
3913 
3914         *epoch = 0; /* not used */
3915         if (lc->lcontext.LeaseFlags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS)
3916                 return SMB2_OPLOCK_LEVEL_NOCHANGE;
3917         return le32_to_cpu(lc->lcontext.LeaseState);
3918 }
3919 
3920 static __u8
3921 smb3_parse_lease_buf(void *buf, unsigned int *epoch, char *lease_key)
3922 {
3923         struct create_lease_v2 *lc = (struct create_lease_v2 *)buf;
3924 
3925         *epoch = le16_to_cpu(lc->lcontext.Epoch);
3926         if (lc->lcontext.LeaseFlags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS)
3927                 return SMB2_OPLOCK_LEVEL_NOCHANGE;
3928         if (lease_key)
3929                 memcpy(lease_key, &lc->lcontext.LeaseKey, SMB2_LEASE_KEY_SIZE);
3930         return le32_to_cpu(lc->lcontext.LeaseState);
3931 }
3932 
3933 static unsigned int
3934 smb2_wp_retry_size(struct inode *inode)
3935 {
3936         return min_t(unsigned int, CIFS_SB(inode->i_sb)->wsize,
3937                      SMB2_MAX_BUFFER_SIZE);
3938 }
3939 
3940 static bool
3941 smb2_dir_needs_close(struct cifsFileInfo *cfile)
3942 {
3943         return !cfile->invalidHandle;
3944 }
3945 
3946 static void
3947 fill_transform_hdr(struct smb2_transform_hdr *tr_hdr, unsigned int orig_len,
3948                    struct smb_rqst *old_rq, __le16 cipher_type)
3949 {
3950         struct smb2_sync_hdr *shdr =
3951                         (struct smb2_sync_hdr *)old_rq->rq_iov[0].iov_base;
3952 
3953         memset(tr_hdr, 0, sizeof(struct smb2_transform_hdr));
3954         tr_hdr->ProtocolId = SMB2_TRANSFORM_PROTO_NUM;
3955         tr_hdr->OriginalMessageSize = cpu_to_le32(orig_len);
3956         tr_hdr->Flags = cpu_to_le16(0x01);
3957         if ((cipher_type == SMB2_ENCRYPTION_AES128_GCM) ||
3958             (cipher_type == SMB2_ENCRYPTION_AES256_GCM))
3959                 get_random_bytes(&tr_hdr->Nonce, SMB3_AES_GCM_NONCE);
3960         else
3961                 get_random_bytes(&tr_hdr->Nonce, SMB3_AES_CCM_NONCE);
3962         memcpy(&tr_hdr->SessionId, &shdr->SessionId, 8);
3963 }
3964 
3965 /* We can not use the normal sg_set_buf() as we will sometimes pass a
3966  * stack object as buf.
3967  */
3968 static inline void smb2_sg_set_buf(struct scatterlist *sg, const void *buf,
3969                                    unsigned int buflen)
3970 {
3971         void *addr;
3972         /*
3973          * VMAP_STACK (at least) puts stack into the vmalloc address space
3974          */
3975         if (is_vmalloc_addr(buf))
3976                 addr = vmalloc_to_page(buf);
3977         else
3978                 addr = virt_to_page(buf);
3979         sg_set_page(sg, addr, buflen, offset_in_page(buf));
3980 }
3981 
3982 /* Assumes the first rqst has a transform header as the first iov.
3983  * I.e.
3984  * rqst[0].rq_iov[0]  is transform header
3985  * rqst[0].rq_iov[1+] data to be encrypted/decrypted
3986  * rqst[1+].rq_iov[0+] data to be encrypted/decrypted
3987  */
3988 static struct scatterlist *
3989 init_sg(int num_rqst, struct smb_rqst *rqst, u8 *sign)
3990 {
3991         unsigned int sg_len;
3992         struct scatterlist *sg;
3993         unsigned int i;
3994         unsigned int j;
3995         unsigned int idx = 0;
3996         int skip;
3997 
3998         sg_len = 1;
3999         for (i = 0; i < num_rqst; i++)
4000                 sg_len += rqst[i].rq_nvec + rqst[i].rq_npages;
4001 
4002         sg = kmalloc_array(sg_len, sizeof(struct scatterlist), GFP_KERNEL);
4003         if (!sg)
4004                 return NULL;
4005 
4006         sg_init_table(sg, sg_len);
4007         for (i = 0; i < num_rqst; i++) {
4008                 for (j = 0; j < rqst[i].rq_nvec; j++) {
4009                         /*
4010                          * The first rqst has a transform header where the
4011                          * first 20 bytes are not part of the encrypted blob
4012                          */
4013                         skip = (i == 0) && (j == 0) ? 20 : 0;
4014                         smb2_sg_set_buf(&sg[idx++],
4015                                         rqst[i].rq_iov[j].iov_base + skip,
4016                                         rqst[i].rq_iov[j].iov_len - skip);
4017                         }
4018 
4019                 for (j = 0; j < rqst[i].rq_npages; j++) {
4020                         unsigned int len, offset;
4021 
4022                         rqst_page_get_length(&rqst[i], j, &len, &offset);
4023                         sg_set_page(&sg[idx++], rqst[i].rq_pages[j], len, offset);
4024                 }
4025         }
4026         smb2_sg_set_buf(&sg[idx], sign, SMB2_SIGNATURE_SIZE);
4027         return sg;
4028 }
4029 
4030 static int
4031 smb2_get_enc_key(struct TCP_Server_Info *server, __u64 ses_id, int enc, u8 *key)
4032 {
4033         struct cifs_ses *ses;
4034         u8 *ses_enc_key;
4035 
4036         spin_lock(&cifs_tcp_ses_lock);
4037         list_for_each_entry(server, &cifs_tcp_ses_list, tcp_ses_list) {
4038                 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
4039                         if (ses->Suid == ses_id) {
4040                                 ses_enc_key = enc ? ses->smb3encryptionkey :
4041                                         ses->smb3decryptionkey;
4042                                 memcpy(key, ses_enc_key, SMB3_SIGN_KEY_SIZE);
4043                                 spin_unlock(&cifs_tcp_ses_lock);
4044                                 return 0;
4045                         }
4046                 }
4047         }
4048         spin_unlock(&cifs_tcp_ses_lock);
4049 
4050         return 1;
4051 }
4052 /*
4053  * Encrypt or decrypt @rqst message. @rqst[0] has the following format:
4054  * iov[0]   - transform header (associate data),
4055  * iov[1-N] - SMB2 header and pages - data to encrypt.
4056  * On success return encrypted data in iov[1-N] and pages, leave iov[0]
4057  * untouched.
4058  */
4059 static int
4060 crypt_message(struct TCP_Server_Info *server, int num_rqst,
4061               struct smb_rqst *rqst, int enc)
4062 {
4063         struct smb2_transform_hdr *tr_hdr =
4064                 (struct smb2_transform_hdr *)rqst[0].rq_iov[0].iov_base;
4065         unsigned int assoc_data_len = sizeof(struct smb2_transform_hdr) - 20;
4066         int rc = 0;
4067         struct scatterlist *sg;
4068         u8 sign[SMB2_SIGNATURE_SIZE] = {};
4069         u8 key[SMB3_SIGN_KEY_SIZE];
4070         struct aead_request *req;
4071         char *iv;
4072         unsigned int iv_len;
4073         DECLARE_CRYPTO_WAIT(wait);
4074         struct crypto_aead *tfm;
4075         unsigned int crypt_len = le32_to_cpu(tr_hdr->OriginalMessageSize);
4076 
4077         rc = smb2_get_enc_key(server, tr_hdr->SessionId, enc, key);
4078         if (rc) {
4079                 cifs_server_dbg(VFS, "%s: Could not get %scryption key\n", __func__,
4080                          enc ? "en" : "de");
4081                 return rc;
4082         }
4083 
4084         rc = smb3_crypto_aead_allocate(server);
4085         if (rc) {
4086                 cifs_server_dbg(VFS, "%s: crypto alloc failed\n", __func__);
4087                 return rc;
4088         }
4089 
4090         tfm = enc ? server->secmech.ccmaesencrypt :
4091                                                 server->secmech.ccmaesdecrypt;
4092 
4093         if (server->cipher_type == SMB2_ENCRYPTION_AES256_GCM)
4094                 rc = crypto_aead_setkey(tfm, key, SMB3_GCM256_CRYPTKEY_SIZE);
4095         else
4096                 rc = crypto_aead_setkey(tfm, key, SMB3_SIGN_KEY_SIZE);
4097 
4098         if (rc) {
4099                 cifs_server_dbg(VFS, "%s: Failed to set aead key %d\n", __func__, rc);
4100                 return rc;
4101         }
4102 
4103         rc = crypto_aead_setauthsize(tfm, SMB2_SIGNATURE_SIZE);
4104         if (rc) {
4105                 cifs_server_dbg(VFS, "%s: Failed to set authsize %d\n", __func__, rc);
4106                 return rc;
4107         }
4108 
4109         req = aead_request_alloc(tfm, GFP_KERNEL);
4110         if (!req) {
4111                 cifs_server_dbg(VFS, "%s: Failed to alloc aead request\n", __func__);
4112                 return -ENOMEM;
4113         }
4114 
4115         if (!enc) {
4116                 memcpy(sign, &tr_hdr->Signature, SMB2_SIGNATURE_SIZE);
4117                 crypt_len += SMB2_SIGNATURE_SIZE;
4118         }
4119 
4120         sg = init_sg(num_rqst, rqst, sign);
4121         if (!sg) {
4122                 cifs_server_dbg(VFS, "%s: Failed to init sg\n", __func__);
4123                 rc = -ENOMEM;
4124                 goto free_req;
4125         }
4126 
4127         iv_len = crypto_aead_ivsize(tfm);
4128         iv = kzalloc(iv_len, GFP_KERNEL);
4129         if (!iv) {
4130                 cifs_server_dbg(VFS, "%s: Failed to alloc iv\n", __func__);
4131                 rc = -ENOMEM;
4132                 goto free_sg;
4133         }
4134 
4135         if ((server->cipher_type == SMB2_ENCRYPTION_AES128_GCM) ||
4136             (server->cipher_type == SMB2_ENCRYPTION_AES256_GCM))
4137                 memcpy(iv, (char *)tr_hdr->Nonce, SMB3_AES_GCM_NONCE);
4138         else {
4139                 iv[0] = 3;
4140                 memcpy(iv + 1, (char *)tr_hdr->Nonce, SMB3_AES_CCM_NONCE);
4141         }
4142 
4143         aead_request_set_crypt(req, sg, sg, crypt_len, iv);
4144         aead_request_set_ad(req, assoc_data_len);
4145 
4146         aead_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
4147                                   crypto_req_done, &wait);
4148 
4149         rc = crypto_wait_req(enc ? crypto_aead_encrypt(req)
4150                                 : crypto_aead_decrypt(req), &wait);
4151 
4152         if (!rc && enc)
4153                 memcpy(&tr_hdr->Signature, sign, SMB2_SIGNATURE_SIZE);
4154 
4155         kfree(iv);
4156 free_sg:
4157         kfree(sg);
4158 free_req:
4159         kfree(req);
4160         return rc;
4161 }
4162 
4163 void
4164 smb3_free_compound_rqst(int num_rqst, struct smb_rqst *rqst)
4165 {
4166         int i, j;
4167 
4168         for (i = 0; i < num_rqst; i++) {
4169                 if (rqst[i].rq_pages) {
4170                         for (j = rqst[i].rq_npages - 1; j >= 0; j--)
4171                                 put_page(rqst[i].rq_pages[j]);
4172                         kfree(rqst[i].rq_pages);
4173                 }
4174         }
4175 }
4176 
4177 /*
4178  * This function will initialize new_rq and encrypt the content.
4179  * The first entry, new_rq[0], only contains a single iov which contains
4180  * a smb2_transform_hdr and is pre-allocated by the caller.
4181  * This function then populates new_rq[1+] with the content from olq_rq[0+].
4182  *
4183  * The end result is an array of smb_rqst structures where the first structure
4184  * only contains a single iov for the transform header which we then can pass
4185  * to crypt_message().
4186  *
4187  * new_rq[0].rq_iov[0] :  smb2_transform_hdr pre-allocated by the caller
4188  * new_rq[1+].rq_iov[*] == old_rq[0+].rq_iov[*] : SMB2/3 requests
4189  */
4190 static int
4191 smb3_init_transform_rq(struct TCP_Server_Info *server, int num_rqst,
4192                        struct smb_rqst *new_rq, struct smb_rqst *old_rq)
4193 {
4194         struct page **pages;
4195         struct smb2_transform_hdr *tr_hdr = new_rq[0].rq_iov[0].iov_base;
4196         unsigned int npages;
4197         unsigned int orig_len = 0;
4198         int i, j;
4199         int rc = -ENOMEM;
4200 
4201         for (i = 1; i < num_rqst; i++) {
4202                 npages = old_rq[i - 1].rq_npages;
4203                 pages = kmalloc_array(npages, sizeof(struct page *),
4204                                       GFP_KERNEL);
4205                 if (!pages)
4206                         goto err_free;
4207 
4208                 new_rq[i].rq_pages = pages;
4209                 new_rq[i].rq_npages = npages;
4210                 new_rq[i].rq_offset = old_rq[i - 1].rq_offset;
4211                 new_rq[i].rq_pagesz = old_rq[i - 1].rq_pagesz;
4212                 new_rq[i].rq_tailsz = old_rq[i - 1].rq_tailsz;
4213                 new_rq[i].rq_iov = old_rq[i - 1].rq_iov;
4214                 new_rq[i].rq_nvec = old_rq[i - 1].rq_nvec;
4215 
4216                 orig_len += smb_rqst_len(server, &old_rq[i - 1]);
4217 
4218                 for (j = 0; j < npages; j++) {
4219                         pages[j] = alloc_page(GFP_KERNEL|__GFP_HIGHMEM);
4220                         if (!pages[j])
4221                                 goto err_free;
4222                 }
4223 
4224                 /* copy pages form the old */
4225                 for (j = 0; j < npages; j++) {
4226                         char *dst, *src;
4227                         unsigned int offset, len;
4228 
4229                         rqst_page_get_length(&new_rq[i], j, &len, &offset);
4230 
4231                         dst = (char *) kmap(new_rq[i].rq_pages[j]) + offset;
4232                         src = (char *) kmap(old_rq[i - 1].rq_pages[j]) + offset;
4233 
4234                         memcpy(dst, src, len);
4235                         kunmap(new_rq[i].rq_pages[j]);
4236                         kunmap(old_rq[i - 1].rq_pages[j]);
4237                 }
4238         }
4239 
4240         /* fill the 1st iov with a transform header */
4241         fill_transform_hdr(tr_hdr, orig_len, old_rq, server->cipher_type);
4242 
4243         rc = crypt_message(server, num_rqst, new_rq, 1);
4244         cifs_dbg(FYI, "Encrypt message returned %d\n", rc);
4245         if (rc)
4246                 goto err_free;
4247 
4248         return rc;
4249 
4250 err_free:
4251         smb3_free_compound_rqst(num_rqst - 1, &new_rq[1]);
4252         return rc;
4253 }
4254 
4255 static int
4256 smb3_is_transform_hdr(void *buf)
4257 {
4258         struct smb2_transform_hdr *trhdr = buf;
4259 
4260         return trhdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM;
4261 }
4262 
4263 static int
4264 decrypt_raw_data(struct TCP_Server_Info *server, char *buf,
4265                  unsigned int buf_data_size, struct page **pages,
4266                  unsigned int npages, unsigned int page_data_size,
4267                  bool is_offloaded)
4268 {
4269         struct kvec iov[2];
4270         struct smb_rqst rqst = {NULL};
4271         int rc;
4272 
4273         iov[0].iov_base = buf;
4274         iov[0].iov_len = sizeof(struct smb2_transform_hdr);
4275         iov[1].iov_base = buf + sizeof(struct smb2_transform_hdr);
4276         iov[1].iov_len = buf_data_size;
4277 
4278         rqst.rq_iov = iov;
4279         rqst.rq_nvec = 2;
4280         rqst.rq_pages = pages;
4281         rqst.rq_npages = npages;
4282         rqst.rq_pagesz = PAGE_SIZE;
4283         rqst.rq_tailsz = (page_data_size % PAGE_SIZE) ? : PAGE_SIZE;
4284 
4285         rc = crypt_message(server, 1, &rqst, 0);
4286         cifs_dbg(FYI, "Decrypt message returned %d\n", rc);
4287 
4288         if (rc)
4289                 return rc;
4290 
4291         memmove(buf, iov[1].iov_base, buf_data_size);
4292 
4293         if (!is_offloaded)
4294                 server->total_read = buf_data_size + page_data_size;
4295 
4296         return rc;
4297 }
4298 
4299 static int
4300 read_data_into_pages(struct TCP_Server_Info *server, struct page **pages,
4301                      unsigned int npages, unsigned int len)
4302 {
4303         int i;
4304         int length;
4305 
4306         for (i = 0; i < npages; i++) {
4307                 struct page *page = pages[i];
4308                 size_t n;
4309 
4310                 n = len;
4311                 if (len >= PAGE_SIZE) {
4312                         /* enough data to fill the page */
4313                         n = PAGE_SIZE;
4314                         len -= n;
4315                 } else {
4316                         zero_user(page, len, PAGE_SIZE - len);
4317                         len = 0;
4318                 }
4319                 length = cifs_read_page_from_socket(server, page, 0, n);
4320                 if (length < 0)
4321                         return length;
4322                 server->total_read += length;
4323         }
4324 
4325         return 0;
4326 }
4327 
4328 static int
4329 init_read_bvec(struct page **pages, unsigned int npages, unsigned int data_size,
4330                unsigned int cur_off, struct bio_vec **page_vec)
4331 {
4332         struct bio_vec *bvec;
4333         int i;
4334 
4335         bvec = kcalloc(npages, sizeof(struct bio_vec), GFP_KERNEL);
4336         if (!bvec)
4337                 return -ENOMEM;
4338 
4339         for (i = 0; i < npages; i++) {
4340                 bvec[i].bv_page = pages[i];
4341                 bvec[i].bv_offset = (i == 0) ? cur_off : 0;
4342                 bvec[i].bv_len = min_t(unsigned int, PAGE_SIZE, data_size);
4343                 data_size -= bvec[i].bv_len;
4344         }
4345 
4346         if (data_size != 0) {
4347                 cifs_dbg(VFS, "%s: something went wrong\n", __func__);
4348                 kfree(bvec);
4349                 return -EIO;
4350         }
4351 
4352         *page_vec = bvec;
4353         return 0;
4354 }
4355 
4356 static int
4357 handle_read_data(struct TCP_Server_Info *server, struct mid_q_entry *mid,
4358                  char *buf, unsigned int buf_len, struct page **pages,
4359                  unsigned int npages, unsigned int page_data_size)
4360 {
4361         unsigned int data_offset;
4362         unsigned int data_len;
4363         unsigned int cur_off;
4364         unsigned int cur_page_idx;
4365         unsigned int pad_len;
4366         struct cifs_readdata *rdata = mid->callback_data;
4367         struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buf;
4368         struct bio_vec *bvec = NULL;
4369         struct iov_iter iter;
4370         struct kvec iov;
4371         int length;
4372         bool use_rdma_mr = false;
4373 
4374         if (shdr->Command != SMB2_READ) {
4375                 cifs_server_dbg(VFS, "only big read responses are supported\n");
4376                 return -ENOTSUPP;
4377         }
4378 
4379         if (server->ops->is_session_expired &&
4380             server->ops->is_session_expired(buf)) {
4381                 cifs_reconnect(server);
4382                 return -1;
4383         }
4384 
4385         if (server->ops->is_status_pending &&
4386                         server->ops->is_status_pending(buf, server))
4387                 return -1;
4388 
4389         /* set up first two iov to get credits */
4390         rdata->iov[0].iov_base = buf;
4391         rdata->iov[0].iov_len = 0;
4392         rdata->iov[1].iov_base = buf;
4393         rdata->iov[1].iov_len =
4394                 min_t(unsigned int, buf_len, server->vals->read_rsp_size);
4395         cifs_dbg(FYI, "0: iov_base=%p iov_len=%zu\n",
4396                  rdata->iov[0].iov_base, rdata->iov[0].iov_len);
4397         cifs_dbg(FYI, "1: iov_base=%p iov_len=%zu\n",
4398                  rdata->iov[1].iov_base, rdata->iov[1].iov_len);
4399 
4400         rdata->result = server->ops->map_error(buf, true);
4401         if (rdata->result != 0) {
4402                 cifs_dbg(FYI, "%s: server returned error %d\n",
4403                          __func__, rdata->result);
4404                 /* normal error on read response */
4405                 dequeue_mid(mid, false);
4406                 return 0;
4407         }
4408 
4409         data_offset = server->ops->read_data_offset(buf);
4410 #ifdef CONFIG_CIFS_SMB_DIRECT
4411         use_rdma_mr = rdata->mr;
4412 #endif
4413         data_len = server->ops->read_data_length(buf, use_rdma_mr);
4414 
4415         if (data_offset < server->vals->read_rsp_size) {
4416                 /*
4417                  * win2k8 sometimes sends an offset of 0 when the read
4418                  * is beyond the EOF. Treat it as if the data starts just after
4419                  * the header.
4420                  */
4421                 cifs_dbg(FYI, "%s: data offset (%u) inside read response header\n",
4422                          __func__, data_offset);
4423                 data_offset = server->vals->read_rsp_size;
4424         } else if (data_offset > MAX_CIFS_SMALL_BUFFER_SIZE) {
4425                 /* data_offset is beyond the end of smallbuf */
4426                 cifs_dbg(FYI, "%s: data offset (%u) beyond end of smallbuf\n",
4427                          __func__, data_offset);
4428                 rdata->result = -EIO;
4429                 dequeue_mid(mid, rdata->result);
4430                 return 0;
4431         }
4432 
4433         pad_len = data_offset - server->vals->read_rsp_size;
4434 
4435         if (buf_len <= data_offset) {
4436                 /* read response payload is in pages */
4437                 cur_page_idx = pad_len / PAGE_SIZE;
4438                 cur_off = pad_len % PAGE_SIZE;
4439 
4440                 if (cur_page_idx != 0) {
4441                         /* data offset is beyond the 1st page of response */
4442                         cifs_dbg(FYI, "%s: data offset (%u) beyond 1st page of response\n",
4443                                  __func__, data_offset);
4444                         rdata->result = -EIO;
4445                         dequeue_mid(mid, rdata->result);
4446                         return 0;
4447                 }
4448 
4449                 if (data_len > page_data_size - pad_len) {
4450                         /* data_len is corrupt -- discard frame */
4451                         rdata->result = -EIO;
4452                         dequeue_mid(mid, rdata->result);
4453                         return 0;
4454                 }
4455 
4456                 rdata->result = init_read_bvec(pages, npages, page_data_size,
4457                                                cur_off, &bvec);
4458                 if (rdata->result != 0) {
4459                         dequeue_mid(mid, rdata->result);
4460                         return 0;
4461                 }
4462 
4463                 iov_iter_bvec(&iter, WRITE, bvec, npages, data_len);
4464         } else if (buf_len >= data_offset + data_len) {
4465                 /* read response payload is in buf */
4466                 WARN_ONCE(npages > 0, "read data can be either in buf or in pages");
4467                 iov.iov_base = buf + data_offset;
4468                 iov.iov_len = data_len;
4469                 iov_iter_kvec(&iter, WRITE, &iov, 1, data_len);
4470         } else {
4471                 /* read response payload cannot be in both buf and pages */
4472                 WARN_ONCE(1, "buf can not contain only a part of read data");
4473                 rdata->result = -EIO;
4474                 dequeue_mid(mid, rdata->result);
4475                 return 0;
4476         }
4477 
4478         length = rdata->copy_into_pages(server, rdata, &iter);
4479 
4480         kfree(bvec);
4481 
4482         if (length < 0)
4483                 return length;
4484 
4485         dequeue_mid(mid, false);
4486         return length;
4487 }
4488 
4489 struct smb2_decrypt_work {
4490         struct work_struct decrypt;
4491         struct TCP_Server_Info *server;
4492         struct page **ppages;
4493         char *buf;
4494         unsigned int npages;
4495         unsigned int len;
4496 };
4497 
4498 
4499 static void smb2_decrypt_offload(struct work_struct *work)
4500 {
4501         struct smb2_decrypt_work *dw = container_of(work,
4502                                 struct smb2_decrypt_work, decrypt);
4503         int i, rc;
4504         struct mid_q_entry *mid;
4505 
4506         rc = decrypt_raw_data(dw->server, dw->buf, dw->server->vals->read_rsp_size,
4507                               dw->ppages, dw->npages, dw->len, true);
4508         if (rc) {
4509                 cifs_dbg(VFS, "error decrypting rc=%d\n", rc);
4510                 goto free_pages;
4511         }
4512 
4513         dw->server->lstrp = jiffies;
4514         mid = smb2_find_mid(dw->server, dw->buf);
4515         if (mid == NULL)
4516                 cifs_dbg(FYI, "mid not found\n");
4517         else {
4518                 mid->decrypted = true;
4519                 rc = handle_read_data(dw->server, mid, dw->buf,
4520                                       dw->server->vals->read_rsp_size,
4521                                       dw->ppages, dw->npages, dw->len);
4522                 mid->callback(mid);
4523                 cifs_mid_q_entry_release(mid);
4524         }
4525 
4526 free_pages:
4527         for (i = dw->npages-1; i >= 0; i--)
4528                 put_page(dw->ppages[i]);
4529 
4530         kfree(dw->ppages);
4531         cifs_small_buf_release(dw->buf);
4532         kfree(dw);
4533 }
4534 
4535 
4536 static int
4537 receive_encrypted_read(struct TCP_Server_Info *server, struct mid_q_entry **mid,
4538                        int *num_mids)
4539 {
4540         char *buf = server->smallbuf;
4541         struct smb2_transform_hdr *tr_hdr = (struct smb2_transform_hdr *)buf;
4542         unsigned int npages;
4543         struct page **pages;
4544         unsigned int len;
4545         unsigned int buflen = server->pdu_size;
4546         int rc;
4547         int i = 0;
4548         struct smb2_decrypt_work *dw;
4549 
4550         *num_mids = 1;
4551         len = min_t(unsigned int, buflen, server->vals->read_rsp_size +
4552                 sizeof(struct smb2_transform_hdr)) - HEADER_SIZE(server) + 1;
4553 
4554         rc = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1, len);
4555         if (rc < 0)
4556                 return rc;
4557         server->total_read += rc;
4558 
4559         len = le32_to_cpu(tr_hdr->OriginalMessageSize) -
4560                 server->vals->read_rsp_size;
4561         npages = DIV_ROUND_UP(len, PAGE_SIZE);
4562 
4563         pages = kmalloc_array(npages, sizeof(struct page *), GFP_KERNEL);
4564         if (!pages) {
4565                 rc = -ENOMEM;
4566                 goto discard_data;
4567         }
4568 
4569         for (; i < npages; i++) {
4570                 pages[i] = alloc_page(GFP_KERNEL|__GFP_HIGHMEM);
4571                 if (!pages[i]) {
4572                         rc = -ENOMEM;
4573                         goto discard_data;
4574                 }
4575         }
4576 
4577         /* read read data into pages */
4578         rc = read_data_into_pages(server, pages, npages, len);
4579         if (rc)
4580                 goto free_pages;
4581 
4582         rc = cifs_discard_remaining_data(server);
4583         if (rc)
4584                 goto free_pages;
4585 
4586         /*
4587          * For large reads, offload to different thread for better performance,
4588          * use more cores decrypting which can be expensive
4589          */
4590 
4591         if ((server->min_offload) && (server->in_flight > 1) &&
4592             (server->pdu_size >= server->min_offload)) {
4593                 dw = kmalloc(sizeof(struct smb2_decrypt_work), GFP_KERNEL);
4594                 if (dw == NULL)
4595                         goto non_offloaded_decrypt;
4596 
4597                 dw->buf = server->smallbuf;
4598                 server->smallbuf = (char *)cifs_small_buf_get();
4599 
4600                 INIT_WORK(&dw->decrypt, smb2_decrypt_offload);
4601 
4602                 dw->npages = npages;
4603                 dw->server = server;
4604                 dw->ppages = pages;
4605                 dw->len = len;
4606                 queue_work(decrypt_wq, &dw->decrypt);
4607                 *num_mids = 0; /* worker thread takes care of finding mid */
4608                 return -1;
4609         }
4610 
4611 non_offloaded_decrypt:
4612         rc = decrypt_raw_data(server, buf, server->vals->read_rsp_size,
4613                               pages, npages, len, false);
4614         if (rc)
4615                 goto free_pages;
4616 
4617         *mid = smb2_find_mid(server, buf);
4618         if (*mid == NULL)
4619                 cifs_dbg(FYI, "mid not found\n");
4620         else {
4621                 cifs_dbg(FYI, "mid found\n");
4622                 (*mid)->decrypted = true;
4623                 rc = handle_read_data(server, *mid, buf,
4624                                       server->vals->read_rsp_size,
4625                                       pages, npages, len);
4626         }
4627 
4628 free_pages:
4629         for (i = i - 1; i >= 0; i--)
4630                 put_page(pages[i]);
4631         kfree(pages);
4632         return rc;
4633 discard_data:
4634         cifs_discard_remaining_data(server);
4635         goto free_pages;
4636 }
4637 
4638 static int
4639 receive_encrypted_standard(struct TCP_Server_Info *server,
4640                            struct mid_q_entry **mids, char **bufs,
4641                            int *num_mids)
4642 {
4643         int ret, length;
4644         char *buf = server->smallbuf;
4645         struct smb2_sync_hdr *shdr;
4646         unsigned int pdu_length = server->pdu_size;
4647         unsigned int buf_size;
4648         struct mid_q_entry *mid_entry;
4649         int next_is_large;
4650         char *next_buffer = NULL;
4651 
4652         *num_mids = 0;
4653 
4654         /* switch to large buffer if too big for a small one */
4655         if (pdu_length > MAX_CIFS_SMALL_BUFFER_SIZE) {
4656                 server->large_buf = true;
4657                 memcpy(server->bigbuf, buf, server->total_read);
4658                 buf = server->bigbuf;
4659         }
4660 
4661         /* now read the rest */
4662         length = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1,
4663                                 pdu_length - HEADER_SIZE(server) + 1);
4664         if (length < 0)
4665                 return length;
4666         server->total_read += length;
4667 
4668         buf_size = pdu_length - sizeof(struct smb2_transform_hdr);
4669         length = decrypt_raw_data(server, buf, buf_size, NULL, 0, 0, false);
4670         if (length)
4671                 return length;
4672 
4673         next_is_large = server->large_buf;
4674 one_more:
4675         shdr = (struct smb2_sync_hdr *)buf;
4676         if (shdr->NextCommand) {
4677                 if (next_is_large)
4678                         next_buffer = (char *)cifs_buf_get();
4679                 else
4680                         next_buffer = (char *)cifs_small_buf_get();
4681                 memcpy(next_buffer,
4682                        buf + le32_to_cpu(shdr->NextCommand),
4683                        pdu_length - le32_to_cpu(shdr->NextCommand));
4684         }
4685 
4686         mid_entry = smb2_find_mid(server, buf);
4687         if (mid_entry == NULL)
4688                 cifs_dbg(FYI, "mid not found\n");
4689         else {
4690                 cifs_dbg(FYI, "mid found\n");
4691                 mid_entry->decrypted = true;
4692                 mid_entry->resp_buf_size = server->pdu_size;
4693         }
4694 
4695         if (*num_mids >= MAX_COMPOUND) {
4696                 cifs_server_dbg(VFS, "too many PDUs in compound\n");
4697                 return -1;
4698         }
4699         bufs[*num_mids] = buf;
4700         mids[(*num_mids)++] = mid_entry;
4701 
4702         if (mid_entry && mid_entry->handle)
4703                 ret = mid_entry->handle(server, mid_entry);
4704         else
4705                 ret = cifs_handle_standard(server, mid_entry);
4706 
4707         if (ret == 0 && shdr->NextCommand) {
4708                 pdu_length -= le32_to_cpu(shdr->NextCommand);
4709                 server->large_buf = next_is_large;
4710                 if (next_is_large)
4711                         server->bigbuf = buf = next_buffer;
4712                 else
4713                         server->smallbuf = buf = next_buffer;
4714                 goto one_more;
4715         } else if (ret != 0) {
4716                 /*
4717                  * ret != 0 here means that we didn't get to handle_mid() thus
4718                  * server->smallbuf and server->bigbuf are still valid. We need
4719                  * to free next_buffer because it is not going to be used
4720                  * anywhere.
4721                  */
4722                 if (next_is_large)
4723                         free_rsp_buf(CIFS_LARGE_BUFFER, next_buffer);
4724                 else
4725                         free_rsp_buf(CIFS_SMALL_BUFFER, next_buffer);
4726         }
4727 
4728         return ret;
4729 }
4730 
4731 static int
4732 smb3_receive_transform(struct TCP_Server_Info *server,
4733                        struct mid_q_entry **mids, char **bufs, int *num_mids)
4734 {
4735         char *buf = server->smallbuf;
4736         unsigned int pdu_length = server->pdu_size;
4737         struct smb2_transform_hdr *tr_hdr = (struct smb2_transform_hdr *)buf;
4738         unsigned int orig_len = le32_to_cpu(tr_hdr->OriginalMessageSize);
4739 
4740         if (pdu_length < sizeof(struct smb2_transform_hdr) +
4741                                                 sizeof(struct smb2_sync_hdr)) {
4742                 cifs_server_dbg(VFS, "Transform message is too small (%u)\n",
4743                          pdu_length);
4744                 cifs_reconnect(server);
4745                 return -ECONNABORTED;
4746         }
4747 
4748         if (pdu_length < orig_len + sizeof(struct smb2_transform_hdr)) {
4749                 cifs_server_dbg(VFS, "Transform message is broken\n");
4750                 cifs_reconnect(server);
4751                 return -ECONNABORTED;
4752         }
4753 
4754         /* TODO: add support for compounds containing READ. */
4755         if (pdu_length > CIFSMaxBufSize + MAX_HEADER_SIZE(server)) {
4756                 return receive_encrypted_read(server, &mids[0], num_mids);
4757         }
4758 
4759         return receive_encrypted_standard(server, mids, bufs, num_mids);
4760 }
4761 
4762 int
4763 smb3_handle_read_data(struct TCP_Server_Info *server, struct mid_q_entry *mid)
4764 {
4765         char *buf = server->large_buf ? server->bigbuf : server->smallbuf;
4766 
4767         return handle_read_data(server, mid, buf, server->pdu_size,
4768                                 NULL, 0, 0);
4769 }
4770 
4771 static int
4772 smb2_next_header(char *buf)
4773 {
4774         struct smb2_sync_hdr *hdr = (struct smb2_sync_hdr *)buf;
4775         struct smb2_transform_hdr *t_hdr = (struct smb2_transform_hdr *)buf;
4776 
4777         if (hdr->ProtocolId == SMB2_TRANSFORM_PROTO_NUM)
4778                 return sizeof(struct smb2_transform_hdr) +
4779                   le32_to_cpu(t_hdr->OriginalMessageSize);
4780 
4781         return le32_to_cpu(hdr->NextCommand);
4782 }
4783 
4784 static int
4785 smb2_make_node(unsigned int xid, struct inode *inode,
4786                struct dentry *dentry, struct cifs_tcon *tcon,
4787                char *full_path, umode_t mode, dev_t dev)
4788 {
4789         struct cifs_sb_info *cifs_sb = CIFS_SB(inode->i_sb);
4790         int rc = -EPERM;
4791         FILE_ALL_INFO *buf = NULL;
4792         struct cifs_io_parms io_parms = {0};
4793         __u32 oplock = 0;
4794         struct cifs_fid fid;
4795         struct cifs_open_parms oparms;
4796         unsigned int bytes_written;
4797         struct win_dev *pdev;
4798         struct kvec iov[2];
4799 
4800         /*
4801          * Check if mounted with mount parm 'sfu' mount parm.
4802          * SFU emulation should work with all servers, but only
4803          * supports block and char device (no socket & fifo),
4804          * and was used by default in earlier versions of Windows
4805          */
4806         if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_UNX_EMUL))
4807                 goto out;
4808 
4809         /*
4810          * TODO: Add ability to create instead via reparse point. Windows (e.g.
4811          * their current NFS server) uses this approach to expose special files
4812          * over SMB2/SMB3 and Samba will do this with SMB3.1.1 POSIX Extensions
4813          */
4814 
4815         if (!S_ISCHR(mode) && !S_ISBLK(mode))
4816                 goto out;
4817 
4818         cifs_dbg(FYI, "sfu compat create special file\n");
4819 
4820         buf = kmalloc(sizeof(FILE_ALL_INFO), GFP_KERNEL);
4821         if (buf == NULL) {
4822                 rc = -ENOMEM;
4823                 goto out;
4824         }
4825 
4826         oparms.tcon = tcon;
4827         oparms.cifs_sb = cifs_sb;
4828         oparms.desired_access = GENERIC_WRITE;
4829         oparms.create_options = cifs_create_options(cifs_sb, CREATE_NOT_DIR |
4830                                                     CREATE_OPTION_SPECIAL);
4831         oparms.disposition = FILE_CREATE;
4832         oparms.path = full_path;
4833         oparms.fid = &fid;
4834         oparms.reconnect = false;
4835 
4836         if (tcon->ses->server->oplocks)
4837                 oplock = REQ_OPLOCK;
4838         else
4839                 oplock = 0;
4840         rc = tcon->ses->server->ops->open(xid, &oparms, &oplock, buf);
4841         if (rc)
4842                 goto out;
4843 
4844         /*
4845          * BB Do not bother to decode buf since no local inode yet to put
4846          * timestamps in, but we can reuse it safely.
4847          */
4848 
4849         pdev = (struct win_dev *)buf;
4850         io_parms.pid = current->tgid;
4851         io_parms.tcon = tcon;
4852         io_parms.offset = 0;
4853         io_parms.length = sizeof(struct win_dev);
4854         iov[1].iov_base = buf;
4855         iov[1].iov_len = sizeof(struct win_dev);
4856         if (S_ISCHR(mode)) {
4857                 memcpy(pdev->type, "IntxCHR", 8);
4858                 pdev->major = cpu_to_le64(MAJOR(dev));
4859                 pdev->minor = cpu_to_le64(MINOR(dev));
4860                 rc = tcon->ses->server->ops->sync_write(xid, &fid, &io_parms,
4861                                                         &bytes_written, iov, 1);
4862         } else if (S_ISBLK(mode)) {
4863                 memcpy(pdev->type, "IntxBLK", 8);
4864                 pdev->major = cpu_to_le64(MAJOR(dev));
4865                 pdev->minor = cpu_to_le64(MINOR(dev));
4866                 rc = tcon->ses->server->ops->sync_write(xid, &fid, &io_parms,
4867                                                         &bytes_written, iov, 1);
4868         }
4869         tcon->ses->server->ops->close(xid, tcon, &fid);
4870         d_drop(dentry);
4871 
4872         /* FIXME: add code here to set EAs */
4873 out:
4874         kfree(buf);
4875         return rc;
4876 }
4877 
4878 
4879 struct smb_version_operations smb20_operations = {
4880         .compare_fids = smb2_compare_fids,
4881         .setup_request = smb2_setup_request,
4882         .setup_async_request = smb2_setup_async_request,
4883         .check_receive = smb2_check_receive,
4884         .add_credits = smb2_add_credits,
4885         .set_credits = smb2_set_credits,
4886         .get_credits_field = smb2_get_credits_field,
4887         .get_credits = smb2_get_credits,
4888         .wait_mtu_credits = cifs_wait_mtu_credits,
4889         .get_next_mid = smb2_get_next_mid,
4890         .revert_current_mid = smb2_revert_current_mid,
4891         .read_data_offset = smb2_read_data_offset,
4892         .read_data_length = smb2_read_data_length,
4893         .map_error = map_smb2_to_linux_error,
4894         .find_mid = smb2_find_mid,
4895         .check_message = smb2_check_message,
4896         .dump_detail = smb2_dump_detail,
4897         .clear_stats = smb2_clear_stats,
4898         .print_stats = smb2_print_stats,
4899         .is_oplock_break = smb2_is_valid_oplock_break,
4900         .handle_cancelled_mid = smb2_handle_cancelled_mid,
4901         .downgrade_oplock = smb2_downgrade_oplock,
4902         .need_neg = smb2_need_neg,
4903         .negotiate = smb2_negotiate,
4904         .negotiate_wsize = smb2_negotiate_wsize,
4905         .negotiate_rsize = smb2_negotiate_rsize,
4906         .sess_setup = SMB2_sess_setup,
4907         .logoff = SMB2_logoff,
4908         .tree_connect = SMB2_tcon,
4909         .tree_disconnect = SMB2_tdis,
4910         .qfs_tcon = smb2_qfs_tcon,
4911         .is_path_accessible = smb2_is_path_accessible,
4912         .can_echo = smb2_can_echo,
4913         .echo = SMB2_echo,
4914         .query_path_info = smb2_query_path_info,
4915         .get_srv_inum = smb2_get_srv_inum,
4916         .query_file_info = smb2_query_file_info,
4917         .set_path_size = smb2_set_path_size,
4918         .set_file_size = smb2_set_file_size,
4919         .set_file_info = smb2_set_file_info,
4920         .set_compression = smb2_set_compression,
4921         .mkdir = smb2_mkdir,
4922         .mkdir_setinfo = smb2_mkdir_setinfo,
4923         .rmdir = smb2_rmdir,
4924         .unlink = smb2_unlink,
4925         .rename = smb2_rename_path,
4926         .create_hardlink = smb2_create_hardlink,
4927         .query_symlink = smb2_query_symlink,
4928         .query_mf_symlink = smb3_query_mf_symlink,
4929         .create_mf_symlink = smb3_create_mf_symlink,
4930         .open = smb2_open_file,
4931         .set_fid = smb2_set_fid,
4932         .close = smb2_close_file,
4933         .flush = smb2_flush_file,
4934         .async_readv = smb2_async_readv,
4935         .async_writev = smb2_async_writev,
4936         .sync_read = smb2_sync_read,
4937         .sync_write = smb2_sync_write,
4938         .query_dir_first = smb2_query_dir_first,
4939         .query_dir_next = smb2_query_dir_next,
4940         .close_dir = smb2_close_dir,
4941         .calc_smb_size = smb2_calc_size,
4942         .is_status_pending = smb2_is_status_pending,
4943         .is_session_expired = smb2_is_session_expired,
4944         .oplock_response = smb2_oplock_response,
4945         .queryfs = smb2_queryfs,
4946         .mand_lock = smb2_mand_lock,
4947         .mand_unlock_range = smb2_unlock_range,
4948         .push_mand_locks = smb2_push_mandatory_locks,
4949         .get_lease_key = smb2_get_lease_key,
4950         .set_lease_key = smb2_set_lease_key,
4951         .new_lease_key = smb2_new_lease_key,
4952         .calc_signature = smb2_calc_signature,
4953         .is_read_op = smb2_is_read_op,
4954         .set_oplock_level = smb2_set_oplock_level,
4955         .create_lease_buf = smb2_create_lease_buf,
4956         .parse_lease_buf = smb2_parse_lease_buf,
4957         .copychunk_range = smb2_copychunk_range,
4958         .wp_retry_size = smb2_wp_retry_size,
4959         .dir_needs_close = smb2_dir_needs_close,
4960         .get_dfs_refer = smb2_get_dfs_refer,
4961         .select_sectype = smb2_select_sectype,
4962 #ifdef CONFIG_CIFS_XATTR
4963         .query_all_EAs = smb2_query_eas,
4964         .set_EA = smb2_set_ea,
4965 #endif /* CIFS_XATTR */
4966         .get_acl = get_smb2_acl,
4967         .get_acl_by_fid = get_smb2_acl_by_fid,
4968         .set_acl = set_smb2_acl,
4969         .next_header = smb2_next_header,
4970         .ioctl_query_info = smb2_ioctl_query_info,
4971         .make_node = smb2_make_node,
4972         .fiemap = smb3_fiemap,
4973         .llseek = smb3_llseek,
4974         .is_status_io_timeout = smb2_is_status_io_timeout,
4975 };
4976 
4977 struct smb_version_operations smb21_operations = {
4978         .compare_fids = smb2_compare_fids,
4979         .setup_request = smb2_setup_request,
4980         .setup_async_request = smb2_setup_async_request,
4981         .check_receive = smb2_check_receive,
4982         .add_credits = smb2_add_credits,
4983         .set_credits = smb2_set_credits,
4984         .get_credits_field = smb2_get_credits_field,
4985         .get_credits = smb2_get_credits,
4986         .wait_mtu_credits = smb2_wait_mtu_credits,
4987         .adjust_credits = smb2_adjust_credits,
4988         .get_next_mid = smb2_get_next_mid,
4989         .revert_current_mid = smb2_revert_current_mid,
4990         .read_data_offset = smb2_read_data_offset,
4991         .read_data_length = smb2_read_data_length,
4992         .map_error = map_smb2_to_linux_error,
4993         .find_mid = smb2_find_mid,
4994         .check_message = smb2_check_message,
4995         .dump_detail = smb2_dump_detail,
4996         .clear_stats = smb2_clear_stats,
4997         .print_stats = smb2_print_stats,
4998         .is_oplock_break = smb2_is_valid_oplock_break,
4999         .handle_cancelled_mid = smb2_handle_cancelled_mid,
5000         .downgrade_oplock = smb2_downgrade_oplock,
5001         .need_neg = smb2_need_neg,
5002         .negotiate = smb2_negotiate,
5003         .negotiate_wsize = smb2_negotiate_wsize,
5004         .negotiate_rsize = smb2_negotiate_rsize,
5005         .sess_setup = SMB2_sess_setup,
5006         .logoff = SMB2_logoff,
5007         .tree_connect = SMB2_tcon,
5008         .tree_disconnect = SMB2_tdis,
5009         .qfs_tcon = smb2_qfs_tcon,
5010         .is_path_accessible = smb2_is_path_accessible,
5011         .can_echo = smb2_can_echo,
5012         .echo = SMB2_echo,
5013         .query_path_info = smb2_query_path_info,
5014         .get_srv_inum = smb2_get_srv_inum,
5015         .query_file_info = smb2_query_file_info,
5016         .set_path_size = smb2_set_path_size,
5017         .set_file_size = smb2_set_file_size,
5018         .set_file_info = smb2_set_file_info,
5019         .set_compression = smb2_set_compression,
5020         .mkdir = smb2_mkdir,
5021         .mkdir_setinfo = smb2_mkdir_setinfo,
5022         .rmdir = smb2_rmdir,
5023         .unlink = smb2_unlink,
5024         .rename = smb2_rename_path,
5025         .create_hardlink = smb2_create_hardlink,
5026         .query_symlink = smb2_query_symlink,
5027         .query_mf_symlink = smb3_query_mf_symlink,
5028         .create_mf_symlink = smb3_create_mf_symlink,
5029         .open = smb2_open_file,
5030         .set_fid = smb2_set_fid,
5031         .close = smb2_close_file,
5032         .flush = smb2_flush_file,
5033         .async_readv = smb2_async_readv,
5034         .async_writev = smb2_async_writev,
5035         .sync_read = smb2_sync_read,
5036         .sync_write = smb2_sync_write,
5037         .query_dir_first = smb2_query_dir_first,
5038         .query_dir_next = smb2_query_dir_next,
5039         .close_dir = smb2_close_dir,
5040         .calc_smb_size = smb2_calc_size,
5041         .is_status_pending = smb2_is_status_pending,
5042         .is_session_expired = smb2_is_session_expired,
5043         .oplock_response = smb2_oplock_response,
5044         .queryfs = smb2_queryfs,
5045         .mand_lock = smb2_mand_lock,
5046         .mand_unlock_range = smb2_unlock_range,
5047         .push_mand_locks = smb2_push_mandatory_locks,
5048         .get_lease_key = smb2_get_lease_key,
5049         .set_lease_key = smb2_set_lease_key,
5050         .new_lease_key = smb2_new_lease_key,
5051         .calc_signature = smb2_calc_signature,
5052         .is_read_op = smb21_is_read_op,
5053         .set_oplock_level = smb21_set_oplock_level,
5054         .create_lease_buf = smb2_create_lease_buf,
5055         .parse_lease_buf = smb2_parse_lease_buf,
5056         .copychunk_range = smb2_copychunk_range,
5057         .wp_retry_size = smb2_wp_retry_size,
5058         .dir_needs_close = smb2_dir_needs_close,
5059         .enum_snapshots = smb3_enum_snapshots,
5060         .notify = smb3_notify,
5061         .get_dfs_refer = smb2_get_dfs_refer,
5062         .select_sectype = smb2_select_sectype,
5063 #ifdef CONFIG_CIFS_XATTR
5064         .query_all_EAs = smb2_query_eas,
5065         .set_EA = smb2_set_ea,
5066 #endif /* CIFS_XATTR */
5067         .get_acl = get_smb2_acl,
5068         .get_acl_by_fid = get_smb2_acl_by_fid,
5069         .set_acl = set_smb2_acl,
5070         .next_header = smb2_next_header,
5071         .ioctl_query_info = smb2_ioctl_query_info,
5072         .make_node = smb2_make_node,
5073         .fiemap = smb3_fiemap,
5074         .llseek = smb3_llseek,
5075         .is_status_io_timeout = smb2_is_status_io_timeout,
5076 };
5077 
5078 struct smb_version_operations smb30_operations = {
5079         .compare_fids = smb2_compare_fids,
5080         .setup_request = smb2_setup_request,
5081         .setup_async_request = smb2_setup_async_request,
5082         .check_receive = smb2_check_receive,
5083         .add_credits = smb2_add_credits,
5084         .set_credits = smb2_set_credits,
5085         .get_credits_field = smb2_get_credits_field,
5086         .get_credits = smb2_get_credits,
5087         .wait_mtu_credits = smb2_wait_mtu_credits,
5088         .adjust_credits = smb2_adjust_credits,
5089         .get_next_mid = smb2_get_next_mid,
5090         .revert_current_mid = smb2_revert_current_mid,
5091         .read_data_offset = smb2_read_data_offset,
5092         .read_data_length = smb2_read_data_length,
5093         .map_error = map_smb2_to_linux_error,
5094         .find_mid = smb2_find_mid,
5095         .check_message = smb2_check_message,
5096         .dump_detail = smb2_dump_detail,
5097         .clear_stats = smb2_clear_stats,
5098         .print_stats = smb2_print_stats,
5099         .dump_share_caps = smb2_dump_share_caps,
5100         .is_oplock_break = smb2_is_valid_oplock_break,
5101         .handle_cancelled_mid = smb2_handle_cancelled_mid,
5102         .downgrade_oplock = smb3_downgrade_oplock,
5103         .need_neg = smb2_need_neg,
5104         .negotiate = smb2_negotiate,
5105         .negotiate_wsize = smb3_negotiate_wsize,
5106         .negotiate_rsize = smb3_negotiate_rsize,
5107         .sess_setup = SMB2_sess_setup,
5108         .logoff = SMB2_logoff,
5109         .tree_connect = SMB2_tcon,
5110         .tree_disconnect = SMB2_tdis,
5111         .qfs_tcon = smb3_qfs_tcon,
5112         .is_path_accessible = smb2_is_path_accessible,
5113         .can_echo = smb2_can_echo,
5114         .echo = SMB2_echo,
5115         .query_path_info = smb2_query_path_info,
5116         /* WSL tags introduced long after smb2.1, enable for SMB3, 3.11 only */
5117         .query_reparse_tag = smb2_query_reparse_tag,
5118         .get_srv_inum = smb2_get_srv_inum,
5119         .query_file_info = smb2_query_file_info,
5120         .set_path_size = smb2_set_path_size,
5121         .set_file_size = smb2_set_file_size,
5122         .set_file_info = smb2_set_file_info,
5123         .set_compression = smb2_set_compression,
5124         .mkdir = smb2_mkdir,
5125         .mkdir_setinfo = smb2_mkdir_setinfo,
5126         .rmdir = smb2_rmdir,
5127         .unlink = smb2_unlink,
5128         .rename = smb2_rename_path,
5129         .create_hardlink = smb2_create_hardlink,
5130         .query_symlink = smb2_query_symlink,
5131         .query_mf_symlink = smb3_query_mf_symlink,
5132         .create_mf_symlink = smb3_create_mf_symlink,
5133         .open = smb2_open_file,
5134         .set_fid = smb2_set_fid,
5135         .close = smb2_close_file,
5136         .close_getattr = smb2_close_getattr,
5137         .flush = smb2_flush_file,
5138         .async_readv = smb2_async_readv,
5139         .async_writev = smb2_async_writev,
5140         .sync_read = smb2_sync_read,
5141         .sync_write = smb2_sync_write,
5142         .query_dir_first = smb2_query_dir_first,
5143         .query_dir_next = smb2_query_dir_next,
5144         .close_dir = smb2_close_dir,
5145         .calc_smb_size = smb2_calc_size,
5146         .is_status_pending = smb2_is_status_pending,
5147         .is_session_expired = smb2_is_session_expired,
5148         .oplock_response = smb2_oplock_response,
5149         .queryfs = smb2_queryfs,
5150         .mand_lock = smb2_mand_lock,
5151         .mand_unlock_range = smb2_unlock_range,
5152         .push_mand_locks = smb2_push_mandatory_locks,
5153         .get_lease_key = smb2_get_lease_key,
5154         .set_lease_key = smb2_set_lease_key,
5155         .new_lease_key = smb2_new_lease_key,
5156         .generate_signingkey = generate_smb30signingkey,
5157         .calc_signature = smb3_calc_signature,
5158         .set_integrity  = smb3_set_integrity,
5159         .is_read_op = smb21_is_read_op,
5160         .set_oplock_level = smb3_set_oplock_level,
5161         .create_lease_buf = smb3_create_lease_buf,
5162         .parse_lease_buf = smb3_parse_lease_buf,
5163         .copychunk_range = smb2_copychunk_range,
5164         .duplicate_extents = smb2_duplicate_extents,
5165         .validate_negotiate = smb3_validate_negotiate,
5166         .wp_retry_size = smb2_wp_retry_size,
5167         .dir_needs_close = smb2_dir_needs_close,
5168         .fallocate = smb3_fallocate,
5169         .enum_snapshots = smb3_enum_snapshots,
5170         .notify = smb3_notify,
5171         .init_transform_rq = smb3_init_transform_rq,
5172         .is_transform_hdr = smb3_is_transform_hdr,
5173         .receive_transform = smb3_receive_transform,
5174         .get_dfs_refer = smb2_get_dfs_refer,
5175         .select_sectype = smb2_select_sectype,
5176 #ifdef CONFIG_CIFS_XATTR
5177         .query_all_EAs = smb2_query_eas,
5178         .set_EA = smb2_set_ea,
5179 #endif /* CIFS_XATTR */
5180         .get_acl = get_smb2_acl,
5181         .get_acl_by_fid = get_smb2_acl_by_fid,
5182         .set_acl = set_smb2_acl,
5183         .next_header = smb2_next_header,
5184         .ioctl_query_info = smb2_ioctl_query_info,
5185         .make_node = smb2_make_node,
5186         .fiemap = smb3_fiemap,
5187         .llseek = smb3_llseek,
5188         .is_status_io_timeout = smb2_is_status_io_timeout,
5189 };
5190 
5191 struct smb_version_operations smb311_operations = {
5192         .compare_fids = smb2_compare_fids,
5193         .setup_request = smb2_setup_request,
5194         .setup_async_request = smb2_setup_async_request,
5195         .check_receive = smb2_check_receive,
5196         .add_credits = smb2_add_credits,
5197         .set_credits = smb2_set_credits,
5198         .get_credits_field = smb2_get_credits_field,
5199         .get_credits = smb2_get_credits,
5200         .wait_mtu_credits = smb2_wait_mtu_credits,
5201         .adjust_credits = smb2_adjust_credits,
5202         .get_next_mid = smb2_get_next_mid,
5203         .revert_current_mid = smb2_revert_current_mid,
5204         .read_data_offset = smb2_read_data_offset,
5205         .read_data_length = smb2_read_data_length,
5206         .map_error = map_smb2_to_linux_error,
5207         .find_mid = smb2_find_mid,
5208         .check_message = smb2_check_message,
5209         .dump_detail = smb2_dump_detail,
5210         .clear_stats = smb2_clear_stats,
5211         .print_stats = smb2_print_stats,
5212         .dump_share_caps = smb2_dump_share_caps,
5213         .is_oplock_break = smb2_is_valid_oplock_break,
5214         .handle_cancelled_mid = smb2_handle_cancelled_mid,
5215         .downgrade_oplock = smb3_downgrade_oplock,
5216         .need_neg = smb2_need_neg,
5217         .negotiate = smb2_negotiate,
5218         .negotiate_wsize = smb3_negotiate_wsize,
5219         .negotiate_rsize = smb3_negotiate_rsize,
5220         .sess_setup = SMB2_sess_setup,
5221         .logoff = SMB2_logoff,
5222         .tree_connect = SMB2_tcon,
5223         .tree_disconnect = SMB2_tdis,
5224         .qfs_tcon = smb3_qfs_tcon,
5225         .is_path_accessible = smb2_is_path_accessible,
5226         .can_echo = smb2_can_echo,
5227         .echo = SMB2_echo,
5228         .query_path_info = smb2_query_path_info,
5229         .query_reparse_tag = smb2_query_reparse_tag,
5230         .get_srv_inum = smb2_get_srv_inum,
5231         .query_file_info = smb2_query_file_info,
5232         .set_path_size = smb2_set_path_size,
5233         .set_file_size = smb2_set_file_size,
5234         .set_file_info = smb2_set_file_info,
5235         .set_compression = smb2_set_compression,
5236         .mkdir = smb2_mkdir,
5237         .mkdir_setinfo = smb2_mkdir_setinfo,
5238         .posix_mkdir = smb311_posix_mkdir,
5239         .rmdir = smb2_rmdir,
5240         .unlink = smb2_unlink,
5241         .rename = smb2_rename_path,
5242         .create_hardlink = smb2_create_hardlink,
5243         .query_symlink = smb2_query_symlink,
5244         .query_mf_symlink = smb3_query_mf_symlink,
5245         .create_mf_symlink = smb3_create_mf_symlink,
5246         .open = smb2_open_file,
5247         .set_fid = smb2_set_fid,
5248         .close = smb2_close_file,
5249         .close_getattr = smb2_close_getattr,
5250         .flush = smb2_flush_file,
5251         .async_readv = smb2_async_readv,
5252         .async_writev = smb2_async_writev,
5253         .sync_read = smb2_sync_read,
5254         .sync_write = smb2_sync_write,
5255         .query_dir_first = smb2_query_dir_first,
5256         .query_dir_next = smb2_query_dir_next,
5257         .close_dir = smb2_close_dir,
5258         .calc_smb_size = smb2_calc_size,
5259         .is_status_pending = smb2_is_status_pending,
5260         .is_session_expired = smb2_is_session_expired,
5261         .oplock_response = smb2_oplock_response,
5262         .queryfs = smb311_queryfs,
5263         .mand_lock = smb2_mand_lock,
5264         .mand_unlock_range = smb2_unlock_range,
5265         .push_mand_locks = smb2_push_mandatory_locks,
5266         .get_lease_key = smb2_get_lease_key,
5267         .set_lease_key = smb2_set_lease_key,
5268         .new_lease_key = smb2_new_lease_key,
5269         .generate_signingkey = generate_smb311signingkey,
5270         .calc_signature = smb3_calc_signature,
5271         .set_integrity  = smb3_set_integrity,
5272         .is_read_op = smb21_is_read_op,
5273         .set_oplock_level = smb3_set_oplock_level,
5274         .create_lease_buf = smb3_create_lease_buf,
5275         .parse_lease_buf = smb3_parse_lease_buf,
5276         .copychunk_range = smb2_copychunk_range,
5277         .duplicate_extents = smb2_duplicate_extents,
5278 /*      .validate_negotiate = smb3_validate_negotiate, */ /* not used in 3.11 */
5279         .wp_retry_size = smb2_wp_retry_size,
5280         .dir_needs_close = smb2_dir_needs_close,
5281         .fallocate = smb3_fallocate,
5282         .enum_snapshots = smb3_enum_snapshots,
5283         .notify = smb3_notify,
5284         .init_transform_rq = smb3_init_transform_rq,
5285         .is_transform_hdr = smb3_is_transform_hdr,
5286         .receive_transform = smb3_receive_transform,
5287         .get_dfs_refer = smb2_get_dfs_refer,
5288         .select_sectype = smb2_select_sectype,
5289 #ifdef CONFIG_CIFS_XATTR
5290         .query_all_EAs = smb2_query_eas,
5291         .set_EA = smb2_set_ea,
5292 #endif /* CIFS_XATTR */
5293         .get_acl = get_smb2_acl,
5294         .get_acl_by_fid = get_smb2_acl_by_fid,
5295         .set_acl = set_smb2_acl,
5296         .next_header = smb2_next_header,
5297         .ioctl_query_info = smb2_ioctl_query_info,
5298         .make_node = smb2_make_node,
5299         .fiemap = smb3_fiemap,
5300         .llseek = smb3_llseek,
5301         .is_status_io_timeout = smb2_is_status_io_timeout,
5302 };
5303 
5304 struct smb_version_values smb20_values = {
5305         .version_string = SMB20_VERSION_STRING,
5306         .protocol_id = SMB20_PROT_ID,
5307         .req_capabilities = 0, /* MBZ */
5308         .large_lock_type = 0,
5309         .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
5310         .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
5311         .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
5312         .header_size = sizeof(struct smb2_sync_hdr),
5313         .header_preamble_size = 0,
5314         .max_header_size = MAX_SMB2_HDR_SIZE,
5315         .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
5316         .lock_cmd = SMB2_LOCK,
5317         .cap_unix = 0,
5318         .cap_nt_find = SMB2_NT_FIND,
5319         .cap_large_files = SMB2_LARGE_FILES,
5320         .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5321         .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5322         .create_lease_size = sizeof(struct create_lease),
5323 };
5324 
5325 struct smb_version_values smb21_values = {
5326         .version_string = SMB21_VERSION_STRING,
5327         .protocol_id = SMB21_PROT_ID,
5328         .req_capabilities = 0, /* MBZ on negotiate req until SMB3 dialect */
5329         .large_lock_type = 0,
5330         .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
5331         .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
5332         .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
5333         .header_size = sizeof(struct smb2_sync_hdr),
5334         .header_preamble_size = 0,
5335         .max_header_size = MAX_SMB2_HDR_SIZE,
5336         .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
5337         .lock_cmd = SMB2_LOCK,
5338         .cap_unix = 0,
5339         .cap_nt_find = SMB2_NT_FIND,
5340         .cap_large_files = SMB2_LARGE_FILES,
5341         .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5342         .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5343         .create_lease_size = sizeof(struct create_lease),
5344 };
5345 
5346 struct smb_version_values smb3any_values = {
5347         .version_string = SMB3ANY_VERSION_STRING,
5348         .protocol_id = SMB302_PROT_ID, /* doesn't matter, send protocol array */
5349         .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,
5350         .large_lock_type = 0,
5351         .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
5352         .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
5353         .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
5354         .header_size = sizeof(struct smb2_sync_hdr),
5355         .header_preamble_size = 0,
5356         .max_header_size = MAX_SMB2_HDR_SIZE,
5357         .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
5358         .lock_cmd = SMB2_LOCK,
5359         .cap_unix = 0,
5360         .cap_nt_find = SMB2_NT_FIND,
5361         .cap_large_files = SMB2_LARGE_FILES,
5362         .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5363         .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5364         .create_lease_size = sizeof(struct create_lease_v2),
5365 };
5366 
5367 struct smb_version_values smbdefault_values = {
5368         .version_string = SMBDEFAULT_VERSION_STRING,
5369         .protocol_id = SMB302_PROT_ID, /* doesn't matter, send protocol array */
5370         .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,
5371         .large_lock_type = 0,
5372         .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
5373         .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
5374         .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
5375         .header_size = sizeof(struct smb2_sync_hdr),
5376         .header_preamble_size = 0,
5377         .max_header_size = MAX_SMB2_HDR_SIZE,
5378         .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
5379         .lock_cmd = SMB2_LOCK,
5380         .cap_unix = 0,
5381         .cap_nt_find = SMB2_NT_FIND,
5382         .cap_large_files = SMB2_LARGE_FILES,
5383         .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5384         .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5385         .create_lease_size = sizeof(struct create_lease_v2),
5386 };
5387 
5388 struct smb_version_values smb30_values = {
5389         .version_string = SMB30_VERSION_STRING,
5390         .protocol_id = SMB30_PROT_ID,
5391         .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,
5392         .large_lock_type = 0,
5393         .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
5394         .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
5395         .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
5396         .header_size = sizeof(struct smb2_sync_hdr),
5397         .header_preamble_size = 0,
5398         .max_header_size = MAX_SMB2_HDR_SIZE,
5399         .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
5400         .lock_cmd = SMB2_LOCK,
5401         .cap_unix = 0,
5402         .cap_nt_find = SMB2_NT_FIND,
5403         .cap_large_files = SMB2_LARGE_FILES,
5404         .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5405         .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5406         .create_lease_size = sizeof(struct create_lease_v2),
5407 };
5408 
5409 struct smb_version_values smb302_values = {
5410         .version_string = SMB302_VERSION_STRING,
5411         .protocol_id = SMB302_PROT_ID,
5412         .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,
5413         .large_lock_type = 0,
5414         .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
5415         .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
5416         .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
5417         .header_size = sizeof(struct smb2_sync_hdr),
5418         .header_preamble_size = 0,
5419         .max_header_size = MAX_SMB2_HDR_SIZE,
5420         .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
5421         .lock_cmd = SMB2_LOCK,
5422         .cap_unix = 0,
5423         .cap_nt_find = SMB2_NT_FIND,
5424         .cap_large_files = SMB2_LARGE_FILES,
5425         .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5426         .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5427         .create_lease_size = sizeof(struct create_lease_v2),
5428 };
5429 
5430 struct smb_version_values smb311_values = {
5431         .version_string = SMB311_VERSION_STRING,
5432         .protocol_id = SMB311_PROT_ID,
5433         .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,
5434         .large_lock_type = 0,
5435         .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
5436         .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
5437         .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
5438         .header_size = sizeof(struct smb2_sync_hdr),
5439         .header_preamble_size = 0,
5440         .max_header_size = MAX_SMB2_HDR_SIZE,
5441         .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
5442         .lock_cmd = SMB2_LOCK,
5443         .cap_unix = 0,
5444         .cap_nt_find = SMB2_NT_FIND,
5445         .cap_large_files = SMB2_LARGE_FILES,
5446         .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
5447         .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
5448         .create_lease_size = sizeof(struct create_lease_v2),
5449 };
5450 

~ [ 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