91b9e5422e9af9a8c1adcc1c2382060c1d33008d
[cascardo/linux.git] / fs / cifs / smb2ops.c
1 /*
2  *  SMB2 version specific operations
3  *
4  *  Copyright (c) 2012, Jeff Layton <jlayton@redhat.com>
5  *
6  *  This library is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License v2 as published
8  *  by the Free Software Foundation.
9  *
10  *  This library is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
13  *  the GNU Lesser General Public License for more details.
14  *
15  *  You should have received a copy of the GNU Lesser General Public License
16  *  along with this library; if not, write to the Free Software
17  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */
19
20 #include <linux/pagemap.h>
21 #include <linux/vfs.h>
22 #include "cifsglob.h"
23 #include "smb2pdu.h"
24 #include "smb2proto.h"
25 #include "cifsproto.h"
26 #include "cifs_debug.h"
27 #include "cifs_unicode.h"
28 #include "smb2status.h"
29 #include "smb2glob.h"
30
31 static int
32 change_conf(struct TCP_Server_Info *server)
33 {
34         server->credits += server->echo_credits + server->oplock_credits;
35         server->oplock_credits = server->echo_credits = 0;
36         switch (server->credits) {
37         case 0:
38                 return -1;
39         case 1:
40                 server->echoes = false;
41                 server->oplocks = false;
42                 cifs_dbg(VFS, "disabling echoes and oplocks\n");
43                 break;
44         case 2:
45                 server->echoes = true;
46                 server->oplocks = false;
47                 server->echo_credits = 1;
48                 cifs_dbg(FYI, "disabling oplocks\n");
49                 break;
50         default:
51                 server->echoes = true;
52                 server->oplocks = true;
53                 server->echo_credits = 1;
54                 server->oplock_credits = 1;
55         }
56         server->credits -= server->echo_credits + server->oplock_credits;
57         return 0;
58 }
59
60 static void
61 smb2_add_credits(struct TCP_Server_Info *server, const unsigned int add,
62                  const int optype)
63 {
64         int *val, rc = 0;
65         spin_lock(&server->req_lock);
66         val = server->ops->get_credits_field(server, optype);
67         *val += add;
68         server->in_flight--;
69         if (server->in_flight == 0 && (optype & CIFS_OP_MASK) != CIFS_NEG_OP)
70                 rc = change_conf(server);
71         /*
72          * Sometimes server returns 0 credits on oplock break ack - we need to
73          * rebalance credits in this case.
74          */
75         else if (server->in_flight > 0 && server->oplock_credits == 0 &&
76                  server->oplocks) {
77                 if (server->credits > 1) {
78                         server->credits--;
79                         server->oplock_credits++;
80                 }
81         }
82         spin_unlock(&server->req_lock);
83         wake_up(&server->request_q);
84         if (rc)
85                 cifs_reconnect(server);
86 }
87
88 static void
89 smb2_set_credits(struct TCP_Server_Info *server, const int val)
90 {
91         spin_lock(&server->req_lock);
92         server->credits = val;
93         spin_unlock(&server->req_lock);
94 }
95
96 static int *
97 smb2_get_credits_field(struct TCP_Server_Info *server, const int optype)
98 {
99         switch (optype) {
100         case CIFS_ECHO_OP:
101                 return &server->echo_credits;
102         case CIFS_OBREAK_OP:
103                 return &server->oplock_credits;
104         default:
105                 return &server->credits;
106         }
107 }
108
109 static unsigned int
110 smb2_get_credits(struct mid_q_entry *mid)
111 {
112         return le16_to_cpu(((struct smb2_hdr *)mid->resp_buf)->CreditRequest);
113 }
114
115 static __u64
116 smb2_get_next_mid(struct TCP_Server_Info *server)
117 {
118         __u64 mid;
119         /* for SMB2 we need the current value */
120         spin_lock(&GlobalMid_Lock);
121         mid = server->CurrentMid++;
122         spin_unlock(&GlobalMid_Lock);
123         return mid;
124 }
125
126 static struct mid_q_entry *
127 smb2_find_mid(struct TCP_Server_Info *server, char *buf)
128 {
129         struct mid_q_entry *mid;
130         struct smb2_hdr *hdr = (struct smb2_hdr *)buf;
131
132         spin_lock(&GlobalMid_Lock);
133         list_for_each_entry(mid, &server->pending_mid_q, qhead) {
134                 if ((mid->mid == hdr->MessageId) &&
135                     (mid->mid_state == MID_REQUEST_SUBMITTED) &&
136                     (mid->command == hdr->Command)) {
137                         spin_unlock(&GlobalMid_Lock);
138                         return mid;
139                 }
140         }
141         spin_unlock(&GlobalMid_Lock);
142         return NULL;
143 }
144
145 static void
146 smb2_dump_detail(void *buf)
147 {
148 #ifdef CONFIG_CIFS_DEBUG2
149         struct smb2_hdr *smb = (struct smb2_hdr *)buf;
150
151         cifs_dbg(VFS, "Cmd: %d Err: 0x%x Flags: 0x%x Mid: %llu Pid: %d\n",
152                  smb->Command, smb->Status, smb->Flags, smb->MessageId,
153                  smb->ProcessId);
154         cifs_dbg(VFS, "smb buf %p len %u\n", smb, smb2_calc_size(smb));
155 #endif
156 }
157
158 static bool
159 smb2_need_neg(struct TCP_Server_Info *server)
160 {
161         return server->max_read == 0;
162 }
163
164 static int
165 smb2_negotiate(const unsigned int xid, struct cifs_ses *ses)
166 {
167         int rc;
168         ses->server->CurrentMid = 0;
169         rc = SMB2_negotiate(xid, ses);
170         /* BB we probably don't need to retry with modern servers */
171         if (rc == -EAGAIN)
172                 rc = -EHOSTDOWN;
173         return rc;
174 }
175
176 static unsigned int
177 smb2_negotiate_wsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
178 {
179         struct TCP_Server_Info *server = tcon->ses->server;
180         unsigned int wsize;
181
182         /* start with specified wsize, or default */
183         wsize = volume_info->wsize ? volume_info->wsize : CIFS_DEFAULT_IOSIZE;
184         wsize = min_t(unsigned int, wsize, server->max_write);
185         /*
186          * limit write size to 2 ** 16, because we don't support multicredit
187          * requests now.
188          */
189         wsize = min_t(unsigned int, wsize, 2 << 15);
190
191         return wsize;
192 }
193
194 static unsigned int
195 smb2_negotiate_rsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
196 {
197         struct TCP_Server_Info *server = tcon->ses->server;
198         unsigned int rsize;
199
200         /* start with specified rsize, or default */
201         rsize = volume_info->rsize ? volume_info->rsize : CIFS_DEFAULT_IOSIZE;
202         rsize = min_t(unsigned int, rsize, server->max_read);
203         /*
204          * limit write size to 2 ** 16, because we don't support multicredit
205          * requests now.
206          */
207         rsize = min_t(unsigned int, rsize, 2 << 15);
208
209         return rsize;
210 }
211
212 static int
213 smb2_is_path_accessible(const unsigned int xid, struct cifs_tcon *tcon,
214                         struct cifs_sb_info *cifs_sb, const char *full_path)
215 {
216         int rc;
217         __le16 *utf16_path;
218         __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
219         struct cifs_open_parms oparms;
220         struct cifs_fid fid;
221
222         utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
223         if (!utf16_path)
224                 return -ENOMEM;
225
226         oparms.tcon = tcon;
227         oparms.desired_access = FILE_READ_ATTRIBUTES;
228         oparms.disposition = FILE_OPEN;
229         oparms.create_options = 0;
230         oparms.fid = &fid;
231         oparms.reconnect = false;
232
233         rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL);
234         if (rc) {
235                 kfree(utf16_path);
236                 return rc;
237         }
238
239         rc = SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
240         kfree(utf16_path);
241         return rc;
242 }
243
244 static int
245 smb2_get_srv_inum(const unsigned int xid, struct cifs_tcon *tcon,
246                   struct cifs_sb_info *cifs_sb, const char *full_path,
247                   u64 *uniqueid, FILE_ALL_INFO *data)
248 {
249         *uniqueid = le64_to_cpu(data->IndexNumber);
250         return 0;
251 }
252
253 static int
254 smb2_query_file_info(const unsigned int xid, struct cifs_tcon *tcon,
255                      struct cifs_fid *fid, FILE_ALL_INFO *data)
256 {
257         int rc;
258         struct smb2_file_all_info *smb2_data;
259
260         smb2_data = kzalloc(sizeof(struct smb2_file_all_info) + MAX_NAME * 2,
261                             GFP_KERNEL);
262         if (smb2_data == NULL)
263                 return -ENOMEM;
264
265         rc = SMB2_query_info(xid, tcon, fid->persistent_fid, fid->volatile_fid,
266                              smb2_data);
267         if (!rc)
268                 move_smb2_info_to_cifs(data, smb2_data);
269         kfree(smb2_data);
270         return rc;
271 }
272
273 static bool
274 smb2_can_echo(struct TCP_Server_Info *server)
275 {
276         return server->echoes;
277 }
278
279 static void
280 smb2_clear_stats(struct cifs_tcon *tcon)
281 {
282 #ifdef CONFIG_CIFS_STATS
283         int i;
284         for (i = 0; i < NUMBER_OF_SMB2_COMMANDS; i++) {
285                 atomic_set(&tcon->stats.smb2_stats.smb2_com_sent[i], 0);
286                 atomic_set(&tcon->stats.smb2_stats.smb2_com_failed[i], 0);
287         }
288 #endif
289 }
290
291 static void
292 smb2_dump_share_caps(struct seq_file *m, struct cifs_tcon *tcon)
293 {
294         seq_puts(m, "\n\tShare Capabilities:");
295         if (tcon->capabilities & SMB2_SHARE_CAP_DFS)
296                 seq_puts(m, " DFS,");
297         if (tcon->capabilities & SMB2_SHARE_CAP_CONTINUOUS_AVAILABILITY)
298                 seq_puts(m, " CONTINUOUS AVAILABILITY,");
299         if (tcon->capabilities & SMB2_SHARE_CAP_SCALEOUT)
300                 seq_puts(m, " SCALEOUT,");
301         if (tcon->capabilities & SMB2_SHARE_CAP_CLUSTER)
302                 seq_puts(m, " CLUSTER,");
303         if (tcon->capabilities & SMB2_SHARE_CAP_ASYMMETRIC)
304                 seq_puts(m, " ASYMMETRIC,");
305         if (tcon->capabilities == 0)
306                 seq_puts(m, " None");
307         seq_printf(m, "\tShare Flags: 0x%x", tcon->share_flags);
308 }
309
310 static void
311 smb2_print_stats(struct seq_file *m, struct cifs_tcon *tcon)
312 {
313 #ifdef CONFIG_CIFS_STATS
314         atomic_t *sent = tcon->stats.smb2_stats.smb2_com_sent;
315         atomic_t *failed = tcon->stats.smb2_stats.smb2_com_failed;
316         seq_printf(m, "\nNegotiates: %d sent %d failed",
317                    atomic_read(&sent[SMB2_NEGOTIATE_HE]),
318                    atomic_read(&failed[SMB2_NEGOTIATE_HE]));
319         seq_printf(m, "\nSessionSetups: %d sent %d failed",
320                    atomic_read(&sent[SMB2_SESSION_SETUP_HE]),
321                    atomic_read(&failed[SMB2_SESSION_SETUP_HE]));
322         seq_printf(m, "\nLogoffs: %d sent %d failed",
323                    atomic_read(&sent[SMB2_LOGOFF_HE]),
324                    atomic_read(&failed[SMB2_LOGOFF_HE]));
325         seq_printf(m, "\nTreeConnects: %d sent %d failed",
326                    atomic_read(&sent[SMB2_TREE_CONNECT_HE]),
327                    atomic_read(&failed[SMB2_TREE_CONNECT_HE]));
328         seq_printf(m, "\nTreeDisconnects: %d sent %d failed",
329                    atomic_read(&sent[SMB2_TREE_DISCONNECT_HE]),
330                    atomic_read(&failed[SMB2_TREE_DISCONNECT_HE]));
331         seq_printf(m, "\nCreates: %d sent %d failed",
332                    atomic_read(&sent[SMB2_CREATE_HE]),
333                    atomic_read(&failed[SMB2_CREATE_HE]));
334         seq_printf(m, "\nCloses: %d sent %d failed",
335                    atomic_read(&sent[SMB2_CLOSE_HE]),
336                    atomic_read(&failed[SMB2_CLOSE_HE]));
337         seq_printf(m, "\nFlushes: %d sent %d failed",
338                    atomic_read(&sent[SMB2_FLUSH_HE]),
339                    atomic_read(&failed[SMB2_FLUSH_HE]));
340         seq_printf(m, "\nReads: %d sent %d failed",
341                    atomic_read(&sent[SMB2_READ_HE]),
342                    atomic_read(&failed[SMB2_READ_HE]));
343         seq_printf(m, "\nWrites: %d sent %d failed",
344                    atomic_read(&sent[SMB2_WRITE_HE]),
345                    atomic_read(&failed[SMB2_WRITE_HE]));
346         seq_printf(m, "\nLocks: %d sent %d failed",
347                    atomic_read(&sent[SMB2_LOCK_HE]),
348                    atomic_read(&failed[SMB2_LOCK_HE]));
349         seq_printf(m, "\nIOCTLs: %d sent %d failed",
350                    atomic_read(&sent[SMB2_IOCTL_HE]),
351                    atomic_read(&failed[SMB2_IOCTL_HE]));
352         seq_printf(m, "\nCancels: %d sent %d failed",
353                    atomic_read(&sent[SMB2_CANCEL_HE]),
354                    atomic_read(&failed[SMB2_CANCEL_HE]));
355         seq_printf(m, "\nEchos: %d sent %d failed",
356                    atomic_read(&sent[SMB2_ECHO_HE]),
357                    atomic_read(&failed[SMB2_ECHO_HE]));
358         seq_printf(m, "\nQueryDirectories: %d sent %d failed",
359                    atomic_read(&sent[SMB2_QUERY_DIRECTORY_HE]),
360                    atomic_read(&failed[SMB2_QUERY_DIRECTORY_HE]));
361         seq_printf(m, "\nChangeNotifies: %d sent %d failed",
362                    atomic_read(&sent[SMB2_CHANGE_NOTIFY_HE]),
363                    atomic_read(&failed[SMB2_CHANGE_NOTIFY_HE]));
364         seq_printf(m, "\nQueryInfos: %d sent %d failed",
365                    atomic_read(&sent[SMB2_QUERY_INFO_HE]),
366                    atomic_read(&failed[SMB2_QUERY_INFO_HE]));
367         seq_printf(m, "\nSetInfos: %d sent %d failed",
368                    atomic_read(&sent[SMB2_SET_INFO_HE]),
369                    atomic_read(&failed[SMB2_SET_INFO_HE]));
370         seq_printf(m, "\nOplockBreaks: %d sent %d failed",
371                    atomic_read(&sent[SMB2_OPLOCK_BREAK_HE]),
372                    atomic_read(&failed[SMB2_OPLOCK_BREAK_HE]));
373 #endif
374 }
375
376 static void
377 smb2_set_fid(struct cifsFileInfo *cfile, struct cifs_fid *fid, __u32 oplock)
378 {
379         struct cifsInodeInfo *cinode = CIFS_I(cfile->dentry->d_inode);
380         cfile->fid.persistent_fid = fid->persistent_fid;
381         cfile->fid.volatile_fid = fid->volatile_fid;
382         smb2_set_oplock_level(cinode, oplock);
383         cinode->can_cache_brlcks = cinode->clientCanCacheAll;
384 }
385
386 static void
387 smb2_close_file(const unsigned int xid, struct cifs_tcon *tcon,
388                 struct cifs_fid *fid)
389 {
390         SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
391 }
392
393 static int
394 smb2_flush_file(const unsigned int xid, struct cifs_tcon *tcon,
395                 struct cifs_fid *fid)
396 {
397         return SMB2_flush(xid, tcon, fid->persistent_fid, fid->volatile_fid);
398 }
399
400 static unsigned int
401 smb2_read_data_offset(char *buf)
402 {
403         struct smb2_read_rsp *rsp = (struct smb2_read_rsp *)buf;
404         return rsp->DataOffset;
405 }
406
407 static unsigned int
408 smb2_read_data_length(char *buf)
409 {
410         struct smb2_read_rsp *rsp = (struct smb2_read_rsp *)buf;
411         return le32_to_cpu(rsp->DataLength);
412 }
413
414
415 static int
416 smb2_sync_read(const unsigned int xid, struct cifsFileInfo *cfile,
417                struct cifs_io_parms *parms, unsigned int *bytes_read,
418                char **buf, int *buf_type)
419 {
420         parms->persistent_fid = cfile->fid.persistent_fid;
421         parms->volatile_fid = cfile->fid.volatile_fid;
422         return SMB2_read(xid, parms, bytes_read, buf, buf_type);
423 }
424
425 static int
426 smb2_sync_write(const unsigned int xid, struct cifsFileInfo *cfile,
427                 struct cifs_io_parms *parms, unsigned int *written,
428                 struct kvec *iov, unsigned long nr_segs)
429 {
430
431         parms->persistent_fid = cfile->fid.persistent_fid;
432         parms->volatile_fid = cfile->fid.volatile_fid;
433         return SMB2_write(xid, parms, written, iov, nr_segs);
434 }
435
436 static int
437 smb2_set_file_size(const unsigned int xid, struct cifs_tcon *tcon,
438                    struct cifsFileInfo *cfile, __u64 size, bool set_alloc)
439 {
440         __le64 eof = cpu_to_le64(size);
441         return SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
442                             cfile->fid.volatile_fid, cfile->pid, &eof);
443 }
444
445 static int
446 smb2_query_dir_first(const unsigned int xid, struct cifs_tcon *tcon,
447                      const char *path, struct cifs_sb_info *cifs_sb,
448                      struct cifs_fid *fid, __u16 search_flags,
449                      struct cifs_search_info *srch_inf)
450 {
451         __le16 *utf16_path;
452         int rc;
453         __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
454         struct cifs_open_parms oparms;
455
456         utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
457         if (!utf16_path)
458                 return -ENOMEM;
459
460         oparms.tcon = tcon;
461         oparms.desired_access = FILE_READ_ATTRIBUTES | FILE_READ_DATA;
462         oparms.disposition = FILE_OPEN;
463         oparms.create_options = 0;
464         oparms.fid = fid;
465         oparms.reconnect = false;
466
467         rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL);
468         kfree(utf16_path);
469         if (rc) {
470                 cifs_dbg(VFS, "open dir failed\n");
471                 return rc;
472         }
473
474         srch_inf->entries_in_buffer = 0;
475         srch_inf->index_of_last_entry = 0;
476
477         rc = SMB2_query_directory(xid, tcon, fid->persistent_fid,
478                                   fid->volatile_fid, 0, srch_inf);
479         if (rc) {
480                 cifs_dbg(VFS, "query directory failed\n");
481                 SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
482         }
483         return rc;
484 }
485
486 static int
487 smb2_query_dir_next(const unsigned int xid, struct cifs_tcon *tcon,
488                     struct cifs_fid *fid, __u16 search_flags,
489                     struct cifs_search_info *srch_inf)
490 {
491         return SMB2_query_directory(xid, tcon, fid->persistent_fid,
492                                     fid->volatile_fid, 0, srch_inf);
493 }
494
495 static int
496 smb2_close_dir(const unsigned int xid, struct cifs_tcon *tcon,
497                struct cifs_fid *fid)
498 {
499         return SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
500 }
501
502 /*
503 * If we negotiate SMB2 protocol and get STATUS_PENDING - update
504 * the number of credits and return true. Otherwise - return false.
505 */
506 static bool
507 smb2_is_status_pending(char *buf, struct TCP_Server_Info *server, int length)
508 {
509         struct smb2_hdr *hdr = (struct smb2_hdr *)buf;
510
511         if (hdr->Status != STATUS_PENDING)
512                 return false;
513
514         if (!length) {
515                 spin_lock(&server->req_lock);
516                 server->credits += le16_to_cpu(hdr->CreditRequest);
517                 spin_unlock(&server->req_lock);
518                 wake_up(&server->request_q);
519         }
520
521         return true;
522 }
523
524 static int
525 smb2_oplock_response(struct cifs_tcon *tcon, struct cifs_fid *fid,
526                      struct cifsInodeInfo *cinode)
527 {
528         if (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_LEASING)
529                 return SMB2_lease_break(0, tcon, cinode->lease_key,
530                                         smb2_get_lease_state(cinode));
531
532         return SMB2_oplock_break(0, tcon, fid->persistent_fid,
533                                  fid->volatile_fid,
534                                  cinode->clientCanCacheRead ? 1 : 0);
535 }
536
537 static int
538 smb2_queryfs(const unsigned int xid, struct cifs_tcon *tcon,
539              struct kstatfs *buf)
540 {
541         int rc;
542         __le16 srch_path = 0; /* Null - open root of share */
543         u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
544         struct cifs_open_parms oparms;
545         struct cifs_fid fid;
546
547         oparms.tcon = tcon;
548         oparms.desired_access = FILE_READ_ATTRIBUTES;
549         oparms.disposition = FILE_OPEN;
550         oparms.create_options = 0;
551         oparms.fid = &fid;
552         oparms.reconnect = false;
553
554         rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL);
555         if (rc)
556                 return rc;
557         buf->f_type = SMB2_MAGIC_NUMBER;
558         rc = SMB2_QFS_info(xid, tcon, fid.persistent_fid, fid.volatile_fid,
559                            buf);
560         SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
561         return rc;
562 }
563
564 static bool
565 smb2_compare_fids(struct cifsFileInfo *ob1, struct cifsFileInfo *ob2)
566 {
567         return ob1->fid.persistent_fid == ob2->fid.persistent_fid &&
568                ob1->fid.volatile_fid == ob2->fid.volatile_fid;
569 }
570
571 static int
572 smb2_mand_lock(const unsigned int xid, struct cifsFileInfo *cfile, __u64 offset,
573                __u64 length, __u32 type, int lock, int unlock, bool wait)
574 {
575         if (unlock && !lock)
576                 type = SMB2_LOCKFLAG_UNLOCK;
577         return SMB2_lock(xid, tlink_tcon(cfile->tlink),
578                          cfile->fid.persistent_fid, cfile->fid.volatile_fid,
579                          current->tgid, length, offset, type, wait);
580 }
581
582 static void
583 smb2_get_lease_key(struct inode *inode, struct cifs_fid *fid)
584 {
585         memcpy(fid->lease_key, CIFS_I(inode)->lease_key, SMB2_LEASE_KEY_SIZE);
586 }
587
588 static void
589 smb2_set_lease_key(struct inode *inode, struct cifs_fid *fid)
590 {
591         memcpy(CIFS_I(inode)->lease_key, fid->lease_key, SMB2_LEASE_KEY_SIZE);
592 }
593
594 static void
595 smb2_new_lease_key(struct cifs_fid *fid)
596 {
597         get_random_bytes(fid->lease_key, SMB2_LEASE_KEY_SIZE);
598 }
599
600 static int
601 smb2_query_symlink(const unsigned int xid, struct cifs_tcon *tcon,
602                    const char *full_path, char **target_path,
603                    struct cifs_sb_info *cifs_sb)
604 {
605         int rc;
606         __le16 *utf16_path;
607         __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
608         struct cifs_open_parms oparms;
609         struct cifs_fid fid;
610         struct smb2_err_rsp *err_buf = NULL;
611         struct smb2_symlink_err_rsp *symlink;
612         unsigned int sub_len, sub_offset;
613
614         cifs_dbg(FYI, "%s: path: %s\n", __func__, full_path);
615
616         utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
617         if (!utf16_path)
618                 return -ENOMEM;
619
620         oparms.tcon = tcon;
621         oparms.desired_access = FILE_READ_ATTRIBUTES;
622         oparms.disposition = FILE_OPEN;
623         oparms.create_options = 0;
624         oparms.fid = &fid;
625         oparms.reconnect = false;
626
627         rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, &err_buf);
628
629         if (!rc || !err_buf) {
630                 kfree(utf16_path);
631                 return -ENOENT;
632         }
633         /* open must fail on symlink - reset rc */
634         rc = 0;
635         symlink = (struct smb2_symlink_err_rsp *)err_buf->ErrorData;
636         sub_len = le16_to_cpu(symlink->SubstituteNameLength);
637         sub_offset = le16_to_cpu(symlink->SubstituteNameOffset);
638         *target_path = cifs_strndup_from_utf16(
639                                 (char *)symlink->PathBuffer + sub_offset,
640                                 sub_len, true, cifs_sb->local_nls);
641         if (!(*target_path)) {
642                 kfree(utf16_path);
643                 return -ENOMEM;
644         }
645         convert_delimiter(*target_path, '/');
646         cifs_dbg(FYI, "%s: target path: %s\n", __func__, *target_path);
647         kfree(utf16_path);
648         return rc;
649 }
650
651 struct smb_version_operations smb21_operations = {
652         .compare_fids = smb2_compare_fids,
653         .setup_request = smb2_setup_request,
654         .setup_async_request = smb2_setup_async_request,
655         .check_receive = smb2_check_receive,
656         .add_credits = smb2_add_credits,
657         .set_credits = smb2_set_credits,
658         .get_credits_field = smb2_get_credits_field,
659         .get_credits = smb2_get_credits,
660         .get_next_mid = smb2_get_next_mid,
661         .read_data_offset = smb2_read_data_offset,
662         .read_data_length = smb2_read_data_length,
663         .map_error = map_smb2_to_linux_error,
664         .find_mid = smb2_find_mid,
665         .check_message = smb2_check_message,
666         .dump_detail = smb2_dump_detail,
667         .clear_stats = smb2_clear_stats,
668         .print_stats = smb2_print_stats,
669         .is_oplock_break = smb2_is_valid_oplock_break,
670         .need_neg = smb2_need_neg,
671         .negotiate = smb2_negotiate,
672         .negotiate_wsize = smb2_negotiate_wsize,
673         .negotiate_rsize = smb2_negotiate_rsize,
674         .sess_setup = SMB2_sess_setup,
675         .logoff = SMB2_logoff,
676         .tree_connect = SMB2_tcon,
677         .tree_disconnect = SMB2_tdis,
678         .is_path_accessible = smb2_is_path_accessible,
679         .can_echo = smb2_can_echo,
680         .echo = SMB2_echo,
681         .query_path_info = smb2_query_path_info,
682         .get_srv_inum = smb2_get_srv_inum,
683         .query_file_info = smb2_query_file_info,
684         .set_path_size = smb2_set_path_size,
685         .set_file_size = smb2_set_file_size,
686         .set_file_info = smb2_set_file_info,
687         .mkdir = smb2_mkdir,
688         .mkdir_setinfo = smb2_mkdir_setinfo,
689         .rmdir = smb2_rmdir,
690         .unlink = smb2_unlink,
691         .rename = smb2_rename_path,
692         .create_hardlink = smb2_create_hardlink,
693         .query_symlink = smb2_query_symlink,
694         .open = smb2_open_file,
695         .set_fid = smb2_set_fid,
696         .close = smb2_close_file,
697         .flush = smb2_flush_file,
698         .async_readv = smb2_async_readv,
699         .async_writev = smb2_async_writev,
700         .sync_read = smb2_sync_read,
701         .sync_write = smb2_sync_write,
702         .query_dir_first = smb2_query_dir_first,
703         .query_dir_next = smb2_query_dir_next,
704         .close_dir = smb2_close_dir,
705         .calc_smb_size = smb2_calc_size,
706         .is_status_pending = smb2_is_status_pending,
707         .oplock_response = smb2_oplock_response,
708         .queryfs = smb2_queryfs,
709         .mand_lock = smb2_mand_lock,
710         .mand_unlock_range = smb2_unlock_range,
711         .push_mand_locks = smb2_push_mandatory_locks,
712         .get_lease_key = smb2_get_lease_key,
713         .set_lease_key = smb2_set_lease_key,
714         .new_lease_key = smb2_new_lease_key,
715         .calc_signature = smb2_calc_signature,
716 };
717
718
719 struct smb_version_operations smb30_operations = {
720         .compare_fids = smb2_compare_fids,
721         .setup_request = smb2_setup_request,
722         .setup_async_request = smb2_setup_async_request,
723         .check_receive = smb2_check_receive,
724         .add_credits = smb2_add_credits,
725         .set_credits = smb2_set_credits,
726         .get_credits_field = smb2_get_credits_field,
727         .get_credits = smb2_get_credits,
728         .get_next_mid = smb2_get_next_mid,
729         .read_data_offset = smb2_read_data_offset,
730         .read_data_length = smb2_read_data_length,
731         .map_error = map_smb2_to_linux_error,
732         .find_mid = smb2_find_mid,
733         .check_message = smb2_check_message,
734         .dump_detail = smb2_dump_detail,
735         .clear_stats = smb2_clear_stats,
736         .print_stats = smb2_print_stats,
737         .dump_share_caps = smb2_dump_share_caps,
738         .is_oplock_break = smb2_is_valid_oplock_break,
739         .need_neg = smb2_need_neg,
740         .negotiate = smb2_negotiate,
741         .negotiate_wsize = smb2_negotiate_wsize,
742         .negotiate_rsize = smb2_negotiate_rsize,
743         .sess_setup = SMB2_sess_setup,
744         .logoff = SMB2_logoff,
745         .tree_connect = SMB2_tcon,
746         .tree_disconnect = SMB2_tdis,
747         .is_path_accessible = smb2_is_path_accessible,
748         .can_echo = smb2_can_echo,
749         .echo = SMB2_echo,
750         .query_path_info = smb2_query_path_info,
751         .get_srv_inum = smb2_get_srv_inum,
752         .query_file_info = smb2_query_file_info,
753         .set_path_size = smb2_set_path_size,
754         .set_file_size = smb2_set_file_size,
755         .set_file_info = smb2_set_file_info,
756         .mkdir = smb2_mkdir,
757         .mkdir_setinfo = smb2_mkdir_setinfo,
758         .rmdir = smb2_rmdir,
759         .unlink = smb2_unlink,
760         .rename = smb2_rename_path,
761         .create_hardlink = smb2_create_hardlink,
762         .query_symlink = smb2_query_symlink,
763         .open = smb2_open_file,
764         .set_fid = smb2_set_fid,
765         .close = smb2_close_file,
766         .flush = smb2_flush_file,
767         .async_readv = smb2_async_readv,
768         .async_writev = smb2_async_writev,
769         .sync_read = smb2_sync_read,
770         .sync_write = smb2_sync_write,
771         .query_dir_first = smb2_query_dir_first,
772         .query_dir_next = smb2_query_dir_next,
773         .close_dir = smb2_close_dir,
774         .calc_smb_size = smb2_calc_size,
775         .is_status_pending = smb2_is_status_pending,
776         .oplock_response = smb2_oplock_response,
777         .queryfs = smb2_queryfs,
778         .mand_lock = smb2_mand_lock,
779         .mand_unlock_range = smb2_unlock_range,
780         .push_mand_locks = smb2_push_mandatory_locks,
781         .get_lease_key = smb2_get_lease_key,
782         .set_lease_key = smb2_set_lease_key,
783         .new_lease_key = smb2_new_lease_key,
784         .generate_signingkey = generate_smb3signingkey,
785         .calc_signature = smb3_calc_signature,
786 };
787
788 struct smb_version_values smb20_values = {
789         .version_string = SMB20_VERSION_STRING,
790         .protocol_id = SMB20_PROT_ID,
791         .req_capabilities = 0, /* MBZ */
792         .large_lock_type = 0,
793         .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
794         .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
795         .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
796         .header_size = sizeof(struct smb2_hdr),
797         .max_header_size = MAX_SMB2_HDR_SIZE,
798         .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
799         .lock_cmd = SMB2_LOCK,
800         .cap_unix = 0,
801         .cap_nt_find = SMB2_NT_FIND,
802         .cap_large_files = SMB2_LARGE_FILES,
803         .oplock_read = SMB2_OPLOCK_LEVEL_II,
804         .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
805         .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
806 };
807
808 struct smb_version_values smb21_values = {
809         .version_string = SMB21_VERSION_STRING,
810         .protocol_id = SMB21_PROT_ID,
811         .req_capabilities = 0, /* MBZ on negotiate req until SMB3 dialect */
812         .large_lock_type = 0,
813         .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
814         .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
815         .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
816         .header_size = sizeof(struct smb2_hdr),
817         .max_header_size = MAX_SMB2_HDR_SIZE,
818         .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
819         .lock_cmd = SMB2_LOCK,
820         .cap_unix = 0,
821         .cap_nt_find = SMB2_NT_FIND,
822         .cap_large_files = SMB2_LARGE_FILES,
823         .oplock_read = SMB2_OPLOCK_LEVEL_II,
824         .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
825         .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
826 };
827
828 struct smb_version_values smb30_values = {
829         .version_string = SMB30_VERSION_STRING,
830         .protocol_id = SMB30_PROT_ID,
831         .req_capabilities = SMB2_GLOBAL_CAP_DFS | SMB2_GLOBAL_CAP_LEASING | SMB2_GLOBAL_CAP_LARGE_MTU,
832         .large_lock_type = 0,
833         .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
834         .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
835         .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
836         .header_size = sizeof(struct smb2_hdr),
837         .max_header_size = MAX_SMB2_HDR_SIZE,
838         .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
839         .lock_cmd = SMB2_LOCK,
840         .cap_unix = 0,
841         .cap_nt_find = SMB2_NT_FIND,
842         .cap_large_files = SMB2_LARGE_FILES,
843         .oplock_read = SMB2_OPLOCK_LEVEL_II,
844         .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
845         .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
846 };
847
848 struct smb_version_values smb302_values = {
849         .version_string = SMB302_VERSION_STRING,
850         .protocol_id = SMB302_PROT_ID,
851         .req_capabilities = SMB2_GLOBAL_CAP_DFS | SMB2_GLOBAL_CAP_LEASING | SMB2_GLOBAL_CAP_LARGE_MTU,
852         .large_lock_type = 0,
853         .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
854         .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
855         .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
856         .header_size = sizeof(struct smb2_hdr),
857         .max_header_size = MAX_SMB2_HDR_SIZE,
858         .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
859         .lock_cmd = SMB2_LOCK,
860         .cap_unix = 0,
861         .cap_nt_find = SMB2_NT_FIND,
862         .cap_large_files = SMB2_LARGE_FILES,
863         .oplock_read = SMB2_OPLOCK_LEVEL_II,
864         .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
865         .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
866 };