Add sparse file support to SMB2/SMB3 mounts
[deliverable/linux.git] / fs / cifs / smb2ops.c
CommitLineData
1080ef75
SF
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
3a3bab50 20#include <linux/pagemap.h>
6fc05c25 21#include <linux/vfs.h>
f29ebb47 22#include <linux/falloc.h>
1080ef75 23#include "cifsglob.h"
2dc7e1c0
PS
24#include "smb2pdu.h"
25#include "smb2proto.h"
28ea5290
PS
26#include "cifsproto.h"
27#include "cifs_debug.h"
b42bf888 28#include "cifs_unicode.h"
2e44b288 29#include "smb2status.h"
6fc05c25 30#include "smb2glob.h"
28ea5290
PS
31
32static int
33change_conf(struct TCP_Server_Info *server)
34{
35 server->credits += server->echo_credits + server->oplock_credits;
36 server->oplock_credits = server->echo_credits = 0;
37 switch (server->credits) {
38 case 0:
39 return -1;
40 case 1:
41 server->echoes = false;
42 server->oplocks = false;
f96637be 43 cifs_dbg(VFS, "disabling echoes and oplocks\n");
28ea5290
PS
44 break;
45 case 2:
46 server->echoes = true;
47 server->oplocks = false;
48 server->echo_credits = 1;
f96637be 49 cifs_dbg(FYI, "disabling oplocks\n");
28ea5290
PS
50 break;
51 default:
52 server->echoes = true;
53 server->oplocks = true;
54 server->echo_credits = 1;
55 server->oplock_credits = 1;
56 }
57 server->credits -= server->echo_credits + server->oplock_credits;
58 return 0;
59}
60
61static void
62smb2_add_credits(struct TCP_Server_Info *server, const unsigned int add,
63 const int optype)
64{
65 int *val, rc = 0;
66 spin_lock(&server->req_lock);
67 val = server->ops->get_credits_field(server, optype);
68 *val += add;
69 server->in_flight--;
ec2e4523 70 if (server->in_flight == 0 && (optype & CIFS_OP_MASK) != CIFS_NEG_OP)
28ea5290 71 rc = change_conf(server);
983c88a4
PS
72 /*
73 * Sometimes server returns 0 credits on oplock break ack - we need to
74 * rebalance credits in this case.
75 */
76 else if (server->in_flight > 0 && server->oplock_credits == 0 &&
77 server->oplocks) {
78 if (server->credits > 1) {
79 server->credits--;
80 server->oplock_credits++;
81 }
82 }
28ea5290
PS
83 spin_unlock(&server->req_lock);
84 wake_up(&server->request_q);
85 if (rc)
86 cifs_reconnect(server);
87}
88
89static void
90smb2_set_credits(struct TCP_Server_Info *server, const int val)
91{
92 spin_lock(&server->req_lock);
93 server->credits = val;
94 spin_unlock(&server->req_lock);
95}
96
97static int *
98smb2_get_credits_field(struct TCP_Server_Info *server, const int optype)
99{
100 switch (optype) {
101 case CIFS_ECHO_OP:
102 return &server->echo_credits;
103 case CIFS_OBREAK_OP:
104 return &server->oplock_credits;
105 default:
106 return &server->credits;
107 }
108}
109
110static unsigned int
111smb2_get_credits(struct mid_q_entry *mid)
112{
113 return le16_to_cpu(((struct smb2_hdr *)mid->resp_buf)->CreditRequest);
114}
2dc7e1c0 115
cb7e9eab
PS
116static int
117smb2_wait_mtu_credits(struct TCP_Server_Info *server, unsigned int size,
118 unsigned int *num, unsigned int *credits)
119{
120 int rc = 0;
121 unsigned int scredits;
122
123 spin_lock(&server->req_lock);
124 while (1) {
125 if (server->credits <= 0) {
126 spin_unlock(&server->req_lock);
127 cifs_num_waiters_inc(server);
128 rc = wait_event_killable(server->request_q,
129 has_credits(server, &server->credits));
130 cifs_num_waiters_dec(server);
131 if (rc)
132 return rc;
133 spin_lock(&server->req_lock);
134 } else {
135 if (server->tcpStatus == CifsExiting) {
136 spin_unlock(&server->req_lock);
137 return -ENOENT;
138 }
139
140 scredits = server->credits;
141 /* can deadlock with reopen */
142 if (scredits == 1) {
143 *num = SMB2_MAX_BUFFER_SIZE;
144 *credits = 0;
145 break;
146 }
147
148 /* leave one credit for a possible reopen */
149 scredits--;
150 *num = min_t(unsigned int, size,
151 scredits * SMB2_MAX_BUFFER_SIZE);
152
153 *credits = DIV_ROUND_UP(*num, SMB2_MAX_BUFFER_SIZE);
154 server->credits -= *credits;
155 server->in_flight++;
156 break;
157 }
158 }
159 spin_unlock(&server->req_lock);
160 return rc;
161}
162
2dc7e1c0
PS
163static __u64
164smb2_get_next_mid(struct TCP_Server_Info *server)
165{
166 __u64 mid;
167 /* for SMB2 we need the current value */
168 spin_lock(&GlobalMid_Lock);
169 mid = server->CurrentMid++;
170 spin_unlock(&GlobalMid_Lock);
171 return mid;
172}
1080ef75 173
093b2bda
PS
174static struct mid_q_entry *
175smb2_find_mid(struct TCP_Server_Info *server, char *buf)
176{
177 struct mid_q_entry *mid;
178 struct smb2_hdr *hdr = (struct smb2_hdr *)buf;
179
180 spin_lock(&GlobalMid_Lock);
181 list_for_each_entry(mid, &server->pending_mid_q, qhead) {
182 if ((mid->mid == hdr->MessageId) &&
183 (mid->mid_state == MID_REQUEST_SUBMITTED) &&
184 (mid->command == hdr->Command)) {
185 spin_unlock(&GlobalMid_Lock);
186 return mid;
187 }
188 }
189 spin_unlock(&GlobalMid_Lock);
190 return NULL;
191}
192
193static void
194smb2_dump_detail(void *buf)
195{
196#ifdef CONFIG_CIFS_DEBUG2
197 struct smb2_hdr *smb = (struct smb2_hdr *)buf;
198
f96637be
JP
199 cifs_dbg(VFS, "Cmd: %d Err: 0x%x Flags: 0x%x Mid: %llu Pid: %d\n",
200 smb->Command, smb->Status, smb->Flags, smb->MessageId,
201 smb->ProcessId);
202 cifs_dbg(VFS, "smb buf %p len %u\n", smb, smb2_calc_size(smb));
093b2bda
PS
203#endif
204}
205
ec2e4523
PS
206static bool
207smb2_need_neg(struct TCP_Server_Info *server)
208{
209 return server->max_read == 0;
210}
211
212static int
213smb2_negotiate(const unsigned int xid, struct cifs_ses *ses)
214{
215 int rc;
216 ses->server->CurrentMid = 0;
217 rc = SMB2_negotiate(xid, ses);
218 /* BB we probably don't need to retry with modern servers */
219 if (rc == -EAGAIN)
220 rc = -EHOSTDOWN;
221 return rc;
222}
223
3a3bab50
PS
224static unsigned int
225smb2_negotiate_wsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
226{
227 struct TCP_Server_Info *server = tcon->ses->server;
228 unsigned int wsize;
229
230 /* start with specified wsize, or default */
231 wsize = volume_info->wsize ? volume_info->wsize : CIFS_DEFAULT_IOSIZE;
232 wsize = min_t(unsigned int, wsize, server->max_write);
cb7e9eab
PS
233
234 if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
235 wsize = min_t(unsigned int, wsize, SMB2_MAX_BUFFER_SIZE);
3a3bab50 236
3a3bab50
PS
237 return wsize;
238}
239
240static unsigned int
241smb2_negotiate_rsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
242{
243 struct TCP_Server_Info *server = tcon->ses->server;
244 unsigned int rsize;
245
246 /* start with specified rsize, or default */
247 rsize = volume_info->rsize ? volume_info->rsize : CIFS_DEFAULT_IOSIZE;
248 rsize = min_t(unsigned int, rsize, server->max_read);
bed9da02
PS
249
250 if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
251 rsize = min_t(unsigned int, rsize, SMB2_MAX_BUFFER_SIZE);
3a3bab50 252
3a3bab50
PS
253 return rsize;
254}
255
c481e9fe
SF
256#ifdef CONFIG_CIFS_STATS2
257static int
258SMB3_request_interfaces(const unsigned int xid, struct cifs_tcon *tcon)
259{
260 int rc;
261 unsigned int ret_data_len = 0;
262 struct network_interface_info_ioctl_rsp *out_buf;
263
264 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
265 FSCTL_QUERY_NETWORK_INTERFACE_INFO, true /* is_fsctl */,
266 NULL /* no data input */, 0 /* no data input */,
267 (char **)&out_buf, &ret_data_len);
268
269 if ((rc == 0) && (ret_data_len > 0)) {
270 /* Dump info on first interface */
271 cifs_dbg(FYI, "Adapter Capability 0x%x\t",
272 le32_to_cpu(out_buf->Capability));
273 cifs_dbg(FYI, "Link Speed %lld\n",
274 le64_to_cpu(out_buf->LinkSpeed));
275 } else
276 cifs_dbg(VFS, "error %d on ioctl to get interface list\n", rc);
277
278 return rc;
279}
280#endif /* STATS2 */
281
af6a12ea
SF
282static void
283smb3_qfs_tcon(const unsigned int xid, struct cifs_tcon *tcon)
284{
285 int rc;
286 __le16 srch_path = 0; /* Null - open root of share */
287 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
288 struct cifs_open_parms oparms;
289 struct cifs_fid fid;
290
291 oparms.tcon = tcon;
292 oparms.desired_access = FILE_READ_ATTRIBUTES;
293 oparms.disposition = FILE_OPEN;
294 oparms.create_options = 0;
295 oparms.fid = &fid;
296 oparms.reconnect = false;
297
298 rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL);
299 if (rc)
300 return;
301
c481e9fe
SF
302#ifdef CONFIG_CIFS_STATS2
303 SMB3_request_interfaces(xid, tcon);
304#endif /* STATS2 */
305
af6a12ea
SF
306 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
307 FS_ATTRIBUTE_INFORMATION);
308 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
309 FS_DEVICE_INFORMATION);
310 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
311 FS_SECTOR_SIZE_INFORMATION); /* SMB3 specific */
312 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
313 return;
314}
315
34f62640
SF
316static void
317smb2_qfs_tcon(const unsigned int xid, struct cifs_tcon *tcon)
318{
319 int rc;
320 __le16 srch_path = 0; /* Null - open root of share */
321 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
322 struct cifs_open_parms oparms;
323 struct cifs_fid fid;
324
325 oparms.tcon = tcon;
326 oparms.desired_access = FILE_READ_ATTRIBUTES;
327 oparms.disposition = FILE_OPEN;
328 oparms.create_options = 0;
329 oparms.fid = &fid;
330 oparms.reconnect = false;
331
332 rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL);
333 if (rc)
334 return;
335
2167114c
SF
336 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
337 FS_ATTRIBUTE_INFORMATION);
338 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
339 FS_DEVICE_INFORMATION);
34f62640
SF
340 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
341 return;
342}
343
2503a0db
PS
344static int
345smb2_is_path_accessible(const unsigned int xid, struct cifs_tcon *tcon,
346 struct cifs_sb_info *cifs_sb, const char *full_path)
347{
348 int rc;
2503a0db 349 __le16 *utf16_path;
2e44b288 350 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
064f6047
PS
351 struct cifs_open_parms oparms;
352 struct cifs_fid fid;
2503a0db
PS
353
354 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
355 if (!utf16_path)
356 return -ENOMEM;
357
064f6047
PS
358 oparms.tcon = tcon;
359 oparms.desired_access = FILE_READ_ATTRIBUTES;
360 oparms.disposition = FILE_OPEN;
361 oparms.create_options = 0;
362 oparms.fid = &fid;
9cbc0b73 363 oparms.reconnect = false;
064f6047 364
b42bf888 365 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL);
2503a0db
PS
366 if (rc) {
367 kfree(utf16_path);
368 return rc;
369 }
370
064f6047 371 rc = SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
2503a0db
PS
372 kfree(utf16_path);
373 return rc;
374}
375
be4cb9e3
PS
376static int
377smb2_get_srv_inum(const unsigned int xid, struct cifs_tcon *tcon,
378 struct cifs_sb_info *cifs_sb, const char *full_path,
379 u64 *uniqueid, FILE_ALL_INFO *data)
380{
381 *uniqueid = le64_to_cpu(data->IndexNumber);
382 return 0;
383}
384
b7546bc5
PS
385static int
386smb2_query_file_info(const unsigned int xid, struct cifs_tcon *tcon,
387 struct cifs_fid *fid, FILE_ALL_INFO *data)
388{
389 int rc;
390 struct smb2_file_all_info *smb2_data;
391
392 smb2_data = kzalloc(sizeof(struct smb2_file_all_info) + MAX_NAME * 2,
393 GFP_KERNEL);
394 if (smb2_data == NULL)
395 return -ENOMEM;
396
397 rc = SMB2_query_info(xid, tcon, fid->persistent_fid, fid->volatile_fid,
398 smb2_data);
399 if (!rc)
400 move_smb2_info_to_cifs(data, smb2_data);
401 kfree(smb2_data);
402 return rc;
403}
404
9094fad1
PS
405static bool
406smb2_can_echo(struct TCP_Server_Info *server)
407{
408 return server->echoes;
409}
410
d60622eb
PS
411static void
412smb2_clear_stats(struct cifs_tcon *tcon)
413{
414#ifdef CONFIG_CIFS_STATS
415 int i;
416 for (i = 0; i < NUMBER_OF_SMB2_COMMANDS; i++) {
417 atomic_set(&tcon->stats.smb2_stats.smb2_com_sent[i], 0);
418 atomic_set(&tcon->stats.smb2_stats.smb2_com_failed[i], 0);
419 }
420#endif
421}
422
769ee6a4
SF
423static void
424smb2_dump_share_caps(struct seq_file *m, struct cifs_tcon *tcon)
425{
426 seq_puts(m, "\n\tShare Capabilities:");
427 if (tcon->capabilities & SMB2_SHARE_CAP_DFS)
428 seq_puts(m, " DFS,");
429 if (tcon->capabilities & SMB2_SHARE_CAP_CONTINUOUS_AVAILABILITY)
430 seq_puts(m, " CONTINUOUS AVAILABILITY,");
431 if (tcon->capabilities & SMB2_SHARE_CAP_SCALEOUT)
432 seq_puts(m, " SCALEOUT,");
433 if (tcon->capabilities & SMB2_SHARE_CAP_CLUSTER)
434 seq_puts(m, " CLUSTER,");
435 if (tcon->capabilities & SMB2_SHARE_CAP_ASYMMETRIC)
436 seq_puts(m, " ASYMMETRIC,");
437 if (tcon->capabilities == 0)
438 seq_puts(m, " None");
af6a12ea
SF
439 if (tcon->ss_flags & SSINFO_FLAGS_ALIGNED_DEVICE)
440 seq_puts(m, " Aligned,");
441 if (tcon->ss_flags & SSINFO_FLAGS_PARTITION_ALIGNED_ON_DEVICE)
442 seq_puts(m, " Partition Aligned,");
443 if (tcon->ss_flags & SSINFO_FLAGS_NO_SEEK_PENALTY)
444 seq_puts(m, " SSD,");
445 if (tcon->ss_flags & SSINFO_FLAGS_TRIM_ENABLED)
446 seq_puts(m, " TRIM-support,");
447
769ee6a4 448 seq_printf(m, "\tShare Flags: 0x%x", tcon->share_flags);
af6a12ea
SF
449 if (tcon->perf_sector_size)
450 seq_printf(m, "\tOptimal sector size: 0x%x",
451 tcon->perf_sector_size);
769ee6a4
SF
452}
453
d60622eb
PS
454static void
455smb2_print_stats(struct seq_file *m, struct cifs_tcon *tcon)
456{
457#ifdef CONFIG_CIFS_STATS
458 atomic_t *sent = tcon->stats.smb2_stats.smb2_com_sent;
459 atomic_t *failed = tcon->stats.smb2_stats.smb2_com_failed;
460 seq_printf(m, "\nNegotiates: %d sent %d failed",
461 atomic_read(&sent[SMB2_NEGOTIATE_HE]),
462 atomic_read(&failed[SMB2_NEGOTIATE_HE]));
463 seq_printf(m, "\nSessionSetups: %d sent %d failed",
464 atomic_read(&sent[SMB2_SESSION_SETUP_HE]),
465 atomic_read(&failed[SMB2_SESSION_SETUP_HE]));
d60622eb
PS
466 seq_printf(m, "\nLogoffs: %d sent %d failed",
467 atomic_read(&sent[SMB2_LOGOFF_HE]),
468 atomic_read(&failed[SMB2_LOGOFF_HE]));
469 seq_printf(m, "\nTreeConnects: %d sent %d failed",
470 atomic_read(&sent[SMB2_TREE_CONNECT_HE]),
471 atomic_read(&failed[SMB2_TREE_CONNECT_HE]));
472 seq_printf(m, "\nTreeDisconnects: %d sent %d failed",
473 atomic_read(&sent[SMB2_TREE_DISCONNECT_HE]),
474 atomic_read(&failed[SMB2_TREE_DISCONNECT_HE]));
475 seq_printf(m, "\nCreates: %d sent %d failed",
476 atomic_read(&sent[SMB2_CREATE_HE]),
477 atomic_read(&failed[SMB2_CREATE_HE]));
478 seq_printf(m, "\nCloses: %d sent %d failed",
479 atomic_read(&sent[SMB2_CLOSE_HE]),
480 atomic_read(&failed[SMB2_CLOSE_HE]));
481 seq_printf(m, "\nFlushes: %d sent %d failed",
482 atomic_read(&sent[SMB2_FLUSH_HE]),
483 atomic_read(&failed[SMB2_FLUSH_HE]));
484 seq_printf(m, "\nReads: %d sent %d failed",
485 atomic_read(&sent[SMB2_READ_HE]),
486 atomic_read(&failed[SMB2_READ_HE]));
487 seq_printf(m, "\nWrites: %d sent %d failed",
488 atomic_read(&sent[SMB2_WRITE_HE]),
489 atomic_read(&failed[SMB2_WRITE_HE]));
490 seq_printf(m, "\nLocks: %d sent %d failed",
491 atomic_read(&sent[SMB2_LOCK_HE]),
492 atomic_read(&failed[SMB2_LOCK_HE]));
493 seq_printf(m, "\nIOCTLs: %d sent %d failed",
494 atomic_read(&sent[SMB2_IOCTL_HE]),
495 atomic_read(&failed[SMB2_IOCTL_HE]));
496 seq_printf(m, "\nCancels: %d sent %d failed",
497 atomic_read(&sent[SMB2_CANCEL_HE]),
498 atomic_read(&failed[SMB2_CANCEL_HE]));
499 seq_printf(m, "\nEchos: %d sent %d failed",
500 atomic_read(&sent[SMB2_ECHO_HE]),
501 atomic_read(&failed[SMB2_ECHO_HE]));
502 seq_printf(m, "\nQueryDirectories: %d sent %d failed",
503 atomic_read(&sent[SMB2_QUERY_DIRECTORY_HE]),
504 atomic_read(&failed[SMB2_QUERY_DIRECTORY_HE]));
505 seq_printf(m, "\nChangeNotifies: %d sent %d failed",
506 atomic_read(&sent[SMB2_CHANGE_NOTIFY_HE]),
507 atomic_read(&failed[SMB2_CHANGE_NOTIFY_HE]));
508 seq_printf(m, "\nQueryInfos: %d sent %d failed",
509 atomic_read(&sent[SMB2_QUERY_INFO_HE]),
510 atomic_read(&failed[SMB2_QUERY_INFO_HE]));
511 seq_printf(m, "\nSetInfos: %d sent %d failed",
512 atomic_read(&sent[SMB2_SET_INFO_HE]),
513 atomic_read(&failed[SMB2_SET_INFO_HE]));
514 seq_printf(m, "\nOplockBreaks: %d sent %d failed",
515 atomic_read(&sent[SMB2_OPLOCK_BREAK_HE]),
516 atomic_read(&failed[SMB2_OPLOCK_BREAK_HE]));
517#endif
518}
519
f0df737e
PS
520static void
521smb2_set_fid(struct cifsFileInfo *cfile, struct cifs_fid *fid, __u32 oplock)
522{
2e44b288 523 struct cifsInodeInfo *cinode = CIFS_I(cfile->dentry->d_inode);
53ef1016
PS
524 struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server;
525
f0df737e
PS
526 cfile->fid.persistent_fid = fid->persistent_fid;
527 cfile->fid.volatile_fid = fid->volatile_fid;
42873b0a
PS
528 server->ops->set_oplock_level(cinode, oplock, fid->epoch,
529 &fid->purge_cache);
18cceb6a 530 cinode->can_cache_brlcks = CIFS_CACHE_WRITE(cinode);
f0df737e
PS
531}
532
760ad0ca 533static void
f0df737e
PS
534smb2_close_file(const unsigned int xid, struct cifs_tcon *tcon,
535 struct cifs_fid *fid)
536{
760ad0ca 537 SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
f0df737e
PS
538}
539
41c1358e
SF
540static int
541SMB2_request_res_key(const unsigned int xid, struct cifs_tcon *tcon,
542 u64 persistent_fid, u64 volatile_fid,
543 struct copychunk_ioctl *pcchunk)
544{
545 int rc;
546 unsigned int ret_data_len;
547 struct resume_key_req *res_key;
548
549 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
550 FSCTL_SRV_REQUEST_RESUME_KEY, true /* is_fsctl */,
551 NULL, 0 /* no input */,
552 (char **)&res_key, &ret_data_len);
553
554 if (rc) {
555 cifs_dbg(VFS, "refcpy ioctl error %d getting resume key\n", rc);
556 goto req_res_key_exit;
557 }
558 if (ret_data_len < sizeof(struct resume_key_req)) {
559 cifs_dbg(VFS, "Invalid refcopy resume key length\n");
560 rc = -EINVAL;
561 goto req_res_key_exit;
562 }
563 memcpy(pcchunk->SourceKey, res_key->ResumeKey, COPY_CHUNK_RES_KEY_SIZE);
564
565req_res_key_exit:
566 kfree(res_key);
567 return rc;
568}
569
570static int
571smb2_clone_range(const unsigned int xid,
572 struct cifsFileInfo *srcfile,
573 struct cifsFileInfo *trgtfile, u64 src_off,
574 u64 len, u64 dest_off)
575{
576 int rc;
577 unsigned int ret_data_len;
578 struct copychunk_ioctl *pcchunk;
9bf0c9cd
SF
579 struct copychunk_ioctl_rsp *retbuf = NULL;
580 struct cifs_tcon *tcon;
581 int chunks_copied = 0;
582 bool chunk_sizes_updated = false;
41c1358e
SF
583
584 pcchunk = kmalloc(sizeof(struct copychunk_ioctl), GFP_KERNEL);
585
586 if (pcchunk == NULL)
587 return -ENOMEM;
588
589 cifs_dbg(FYI, "in smb2_clone_range - about to call request res key\n");
590 /* Request a key from the server to identify the source of the copy */
591 rc = SMB2_request_res_key(xid, tlink_tcon(srcfile->tlink),
592 srcfile->fid.persistent_fid,
593 srcfile->fid.volatile_fid, pcchunk);
594
595 /* Note: request_res_key sets res_key null only if rc !=0 */
596 if (rc)
9bf0c9cd 597 goto cchunk_out;
41c1358e
SF
598
599 /* For now array only one chunk long, will make more flexible later */
600 pcchunk->ChunkCount = __constant_cpu_to_le32(1);
601 pcchunk->Reserved = 0;
41c1358e
SF
602 pcchunk->Reserved2 = 0;
603
9bf0c9cd 604 tcon = tlink_tcon(trgtfile->tlink);
41c1358e 605
9bf0c9cd
SF
606 while (len > 0) {
607 pcchunk->SourceOffset = cpu_to_le64(src_off);
608 pcchunk->TargetOffset = cpu_to_le64(dest_off);
609 pcchunk->Length =
610 cpu_to_le32(min_t(u32, len, tcon->max_bytes_chunk));
41c1358e 611
9bf0c9cd
SF
612 /* Request server copy to target from src identified by key */
613 rc = SMB2_ioctl(xid, tcon, trgtfile->fid.persistent_fid,
614 trgtfile->fid.volatile_fid, FSCTL_SRV_COPYCHUNK_WRITE,
615 true /* is_fsctl */, (char *)pcchunk,
616 sizeof(struct copychunk_ioctl), (char **)&retbuf,
617 &ret_data_len);
618 if (rc == 0) {
619 if (ret_data_len !=
620 sizeof(struct copychunk_ioctl_rsp)) {
621 cifs_dbg(VFS, "invalid cchunk response size\n");
622 rc = -EIO;
623 goto cchunk_out;
624 }
625 if (retbuf->TotalBytesWritten == 0) {
626 cifs_dbg(FYI, "no bytes copied\n");
627 rc = -EIO;
628 goto cchunk_out;
629 }
630 /*
631 * Check if server claimed to write more than we asked
632 */
633 if (le32_to_cpu(retbuf->TotalBytesWritten) >
634 le32_to_cpu(pcchunk->Length)) {
635 cifs_dbg(VFS, "invalid copy chunk response\n");
636 rc = -EIO;
637 goto cchunk_out;
638 }
639 if (le32_to_cpu(retbuf->ChunksWritten) != 1) {
640 cifs_dbg(VFS, "invalid num chunks written\n");
641 rc = -EIO;
642 goto cchunk_out;
643 }
644 chunks_copied++;
645
646 src_off += le32_to_cpu(retbuf->TotalBytesWritten);
647 dest_off += le32_to_cpu(retbuf->TotalBytesWritten);
648 len -= le32_to_cpu(retbuf->TotalBytesWritten);
649
650 cifs_dbg(FYI, "Chunks %d PartialChunk %d Total %d\n",
651 le32_to_cpu(retbuf->ChunksWritten),
652 le32_to_cpu(retbuf->ChunkBytesWritten),
653 le32_to_cpu(retbuf->TotalBytesWritten));
654 } else if (rc == -EINVAL) {
655 if (ret_data_len != sizeof(struct copychunk_ioctl_rsp))
656 goto cchunk_out;
657
658 cifs_dbg(FYI, "MaxChunks %d BytesChunk %d MaxCopy %d\n",
659 le32_to_cpu(retbuf->ChunksWritten),
660 le32_to_cpu(retbuf->ChunkBytesWritten),
661 le32_to_cpu(retbuf->TotalBytesWritten));
662
663 /*
664 * Check if this is the first request using these sizes,
665 * (ie check if copy succeed once with original sizes
666 * and check if the server gave us different sizes after
667 * we already updated max sizes on previous request).
668 * if not then why is the server returning an error now
669 */
670 if ((chunks_copied != 0) || chunk_sizes_updated)
671 goto cchunk_out;
672
673 /* Check that server is not asking us to grow size */
674 if (le32_to_cpu(retbuf->ChunkBytesWritten) <
675 tcon->max_bytes_chunk)
676 tcon->max_bytes_chunk =
677 le32_to_cpu(retbuf->ChunkBytesWritten);
678 else
679 goto cchunk_out; /* server gave us bogus size */
680
681 /* No need to change MaxChunks since already set to 1 */
682 chunk_sizes_updated = true;
683 }
684 }
41c1358e 685
9bf0c9cd 686cchunk_out:
41c1358e
SF
687 kfree(pcchunk);
688 return rc;
689}
690
7a5cfb19
PS
691static int
692smb2_flush_file(const unsigned int xid, struct cifs_tcon *tcon,
693 struct cifs_fid *fid)
694{
695 return SMB2_flush(xid, tcon, fid->persistent_fid, fid->volatile_fid);
696}
697
09a4707e
PS
698static unsigned int
699smb2_read_data_offset(char *buf)
700{
701 struct smb2_read_rsp *rsp = (struct smb2_read_rsp *)buf;
702 return rsp->DataOffset;
703}
704
705static unsigned int
706smb2_read_data_length(char *buf)
707{
708 struct smb2_read_rsp *rsp = (struct smb2_read_rsp *)buf;
709 return le32_to_cpu(rsp->DataLength);
710}
711
d8e05039
PS
712
713static int
714smb2_sync_read(const unsigned int xid, struct cifsFileInfo *cfile,
715 struct cifs_io_parms *parms, unsigned int *bytes_read,
716 char **buf, int *buf_type)
717{
718 parms->persistent_fid = cfile->fid.persistent_fid;
719 parms->volatile_fid = cfile->fid.volatile_fid;
720 return SMB2_read(xid, parms, bytes_read, buf, buf_type);
721}
722
009d3443
PS
723static int
724smb2_sync_write(const unsigned int xid, struct cifsFileInfo *cfile,
725 struct cifs_io_parms *parms, unsigned int *written,
726 struct kvec *iov, unsigned long nr_segs)
727{
728
729 parms->persistent_fid = cfile->fid.persistent_fid;
730 parms->volatile_fid = cfile->fid.volatile_fid;
731 return SMB2_write(xid, parms, written, iov, nr_segs);
732}
733
c839ff24
PS
734static int
735smb2_set_file_size(const unsigned int xid, struct cifs_tcon *tcon,
736 struct cifsFileInfo *cfile, __u64 size, bool set_alloc)
737{
738 __le64 eof = cpu_to_le64(size);
3d1a3745
SF
739 struct inode *inode;
740
741 /*
742 * If extending file more than one page make sparse. Many Linux fs
743 * make files sparse by default when extending via ftruncate
744 */
745 inode = cfile->dentry->d_inode;
746
747 if (!set_alloc && (size > inode->i_size + 8192)) {
748 struct cifsInodeInfo *cifsi;
749 __u8 set_sparse = 1;
750 int rc;
751
752 cifsi = CIFS_I(inode);
753
754 /* if file already sparse or no server support don't bother */
755 if (cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE)
756 goto smb2_set_eof;
757
758 /*
759 * Can't check for sparse support on share the usual way via the
760 * FS attribute info (FILE_SUPPORTS_SPARSE_FILES) on the share
761 * since Samba server doesn't set the flag on the share, yet
762 * supports the set sparse FSCTL and returns sparse correctly
763 * in the file attributes. If we fail setting sparse though we
764 * mark that server does not support sparse files for this share
765 * to avoid repeatedly sending the unsupported fsctl to server
766 * if the file is repeatedly extended.
767 */
768 if (tcon->broken_sparse_sup)
769 goto smb2_set_eof;
770
771 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
772 cfile->fid.volatile_fid, FSCTL_SET_SPARSE,
773 true /* is_fctl */, &set_sparse, 1, NULL, NULL);
774 if (rc) {
775 tcon->broken_sparse_sup = true;
776 cifs_dbg(FYI, "set sparse rc = %d\n", rc);
777 } else
778 cifsi->cifsAttrs |= FILE_ATTRIBUTE_SPARSE_FILE;
779 }
780
781smb2_set_eof:
c839ff24 782 return SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
f29ebb47 783 cfile->fid.volatile_fid, cfile->pid, &eof, false);
c839ff24
PS
784}
785
64a5cfa6
SF
786static int
787smb2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
788 struct cifsFileInfo *cfile)
789{
790 return SMB2_set_compression(xid, tcon, cfile->fid.persistent_fid,
791 cfile->fid.volatile_fid);
792}
793
d324f08d
PS
794static int
795smb2_query_dir_first(const unsigned int xid, struct cifs_tcon *tcon,
796 const char *path, struct cifs_sb_info *cifs_sb,
797 struct cifs_fid *fid, __u16 search_flags,
798 struct cifs_search_info *srch_inf)
799{
800 __le16 *utf16_path;
801 int rc;
2e44b288 802 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
064f6047 803 struct cifs_open_parms oparms;
d324f08d
PS
804
805 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
806 if (!utf16_path)
807 return -ENOMEM;
808
064f6047
PS
809 oparms.tcon = tcon;
810 oparms.desired_access = FILE_READ_ATTRIBUTES | FILE_READ_DATA;
811 oparms.disposition = FILE_OPEN;
812 oparms.create_options = 0;
813 oparms.fid = fid;
9cbc0b73 814 oparms.reconnect = false;
064f6047 815
b42bf888 816 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL);
d324f08d
PS
817 kfree(utf16_path);
818 if (rc) {
f96637be 819 cifs_dbg(VFS, "open dir failed\n");
d324f08d
PS
820 return rc;
821 }
822
823 srch_inf->entries_in_buffer = 0;
824 srch_inf->index_of_last_entry = 0;
d324f08d 825
064f6047
PS
826 rc = SMB2_query_directory(xid, tcon, fid->persistent_fid,
827 fid->volatile_fid, 0, srch_inf);
d324f08d 828 if (rc) {
f96637be 829 cifs_dbg(VFS, "query directory failed\n");
064f6047 830 SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
d324f08d
PS
831 }
832 return rc;
833}
834
835static int
836smb2_query_dir_next(const unsigned int xid, struct cifs_tcon *tcon,
837 struct cifs_fid *fid, __u16 search_flags,
838 struct cifs_search_info *srch_inf)
839{
840 return SMB2_query_directory(xid, tcon, fid->persistent_fid,
841 fid->volatile_fid, 0, srch_inf);
842}
843
844static int
845smb2_close_dir(const unsigned int xid, struct cifs_tcon *tcon,
846 struct cifs_fid *fid)
847{
848 return SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
849}
850
2e44b288
PS
851/*
852* If we negotiate SMB2 protocol and get STATUS_PENDING - update
853* the number of credits and return true. Otherwise - return false.
854*/
855static bool
856smb2_is_status_pending(char *buf, struct TCP_Server_Info *server, int length)
857{
858 struct smb2_hdr *hdr = (struct smb2_hdr *)buf;
859
12e8a208 860 if (hdr->Status != STATUS_PENDING)
2e44b288
PS
861 return false;
862
863 if (!length) {
864 spin_lock(&server->req_lock);
865 server->credits += le16_to_cpu(hdr->CreditRequest);
866 spin_unlock(&server->req_lock);
867 wake_up(&server->request_q);
868 }
869
870 return true;
871}
872
983c88a4
PS
873static int
874smb2_oplock_response(struct cifs_tcon *tcon, struct cifs_fid *fid,
875 struct cifsInodeInfo *cinode)
876{
0822f514
PS
877 if (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_LEASING)
878 return SMB2_lease_break(0, tcon, cinode->lease_key,
879 smb2_get_lease_state(cinode));
880
983c88a4
PS
881 return SMB2_oplock_break(0, tcon, fid->persistent_fid,
882 fid->volatile_fid,
18cceb6a 883 CIFS_CACHE_READ(cinode) ? 1 : 0);
983c88a4
PS
884}
885
6fc05c25
PS
886static int
887smb2_queryfs(const unsigned int xid, struct cifs_tcon *tcon,
888 struct kstatfs *buf)
889{
890 int rc;
6fc05c25
PS
891 __le16 srch_path = 0; /* Null - open root of share */
892 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
064f6047
PS
893 struct cifs_open_parms oparms;
894 struct cifs_fid fid;
895
896 oparms.tcon = tcon;
897 oparms.desired_access = FILE_READ_ATTRIBUTES;
898 oparms.disposition = FILE_OPEN;
899 oparms.create_options = 0;
900 oparms.fid = &fid;
9cbc0b73 901 oparms.reconnect = false;
6fc05c25 902
b42bf888 903 rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL);
6fc05c25
PS
904 if (rc)
905 return rc;
906 buf->f_type = SMB2_MAGIC_NUMBER;
064f6047
PS
907 rc = SMB2_QFS_info(xid, tcon, fid.persistent_fid, fid.volatile_fid,
908 buf);
909 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
6fc05c25
PS
910 return rc;
911}
912
027e8eec
PS
913static bool
914smb2_compare_fids(struct cifsFileInfo *ob1, struct cifsFileInfo *ob2)
915{
916 return ob1->fid.persistent_fid == ob2->fid.persistent_fid &&
917 ob1->fid.volatile_fid == ob2->fid.volatile_fid;
918}
919
f7ba7fe6
PS
920static int
921smb2_mand_lock(const unsigned int xid, struct cifsFileInfo *cfile, __u64 offset,
922 __u64 length, __u32 type, int lock, int unlock, bool wait)
923{
924 if (unlock && !lock)
925 type = SMB2_LOCKFLAG_UNLOCK;
926 return SMB2_lock(xid, tlink_tcon(cfile->tlink),
927 cfile->fid.persistent_fid, cfile->fid.volatile_fid,
928 current->tgid, length, offset, type, wait);
929}
930
b8c32dbb
PS
931static void
932smb2_get_lease_key(struct inode *inode, struct cifs_fid *fid)
933{
934 memcpy(fid->lease_key, CIFS_I(inode)->lease_key, SMB2_LEASE_KEY_SIZE);
935}
936
937static void
938smb2_set_lease_key(struct inode *inode, struct cifs_fid *fid)
939{
940 memcpy(CIFS_I(inode)->lease_key, fid->lease_key, SMB2_LEASE_KEY_SIZE);
941}
942
943static void
944smb2_new_lease_key(struct cifs_fid *fid)
945{
946 get_random_bytes(fid->lease_key, SMB2_LEASE_KEY_SIZE);
947}
948
b42bf888
PS
949static int
950smb2_query_symlink(const unsigned int xid, struct cifs_tcon *tcon,
951 const char *full_path, char **target_path,
952 struct cifs_sb_info *cifs_sb)
953{
954 int rc;
955 __le16 *utf16_path;
956 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
957 struct cifs_open_parms oparms;
958 struct cifs_fid fid;
959 struct smb2_err_rsp *err_buf = NULL;
960 struct smb2_symlink_err_rsp *symlink;
961 unsigned int sub_len, sub_offset;
962
963 cifs_dbg(FYI, "%s: path: %s\n", __func__, full_path);
964
965 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
966 if (!utf16_path)
967 return -ENOMEM;
968
969 oparms.tcon = tcon;
970 oparms.desired_access = FILE_READ_ATTRIBUTES;
971 oparms.disposition = FILE_OPEN;
972 oparms.create_options = 0;
973 oparms.fid = &fid;
974 oparms.reconnect = false;
975
976 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, &err_buf);
977
978 if (!rc || !err_buf) {
979 kfree(utf16_path);
980 return -ENOENT;
981 }
982 /* open must fail on symlink - reset rc */
983 rc = 0;
984 symlink = (struct smb2_symlink_err_rsp *)err_buf->ErrorData;
985 sub_len = le16_to_cpu(symlink->SubstituteNameLength);
986 sub_offset = le16_to_cpu(symlink->SubstituteNameOffset);
987 *target_path = cifs_strndup_from_utf16(
988 (char *)symlink->PathBuffer + sub_offset,
989 sub_len, true, cifs_sb->local_nls);
990 if (!(*target_path)) {
991 kfree(utf16_path);
992 return -ENOMEM;
993 }
994 convert_delimiter(*target_path, '/');
995 cifs_dbg(FYI, "%s: target path: %s\n", __func__, *target_path);
996 kfree(utf16_path);
997 return rc;
998}
999
c11f1df5
SP
1000static void
1001smb2_downgrade_oplock(struct TCP_Server_Info *server,
1002 struct cifsInodeInfo *cinode, bool set_level2)
1003{
1004 if (set_level2)
1005 server->ops->set_oplock_level(cinode, SMB2_OPLOCK_LEVEL_II,
1006 0, NULL);
1007 else
1008 server->ops->set_oplock_level(cinode, 0, 0, NULL);
1009}
1010
53ef1016 1011static void
42873b0a
PS
1012smb2_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
1013 unsigned int epoch, bool *purge_cache)
53ef1016
PS
1014{
1015 oplock &= 0xFF;
1016 if (oplock == SMB2_OPLOCK_LEVEL_NOCHANGE)
1017 return;
1018 if (oplock == SMB2_OPLOCK_LEVEL_BATCH) {
42873b0a 1019 cinode->oplock = CIFS_CACHE_RHW_FLG;
53ef1016
PS
1020 cifs_dbg(FYI, "Batch Oplock granted on inode %p\n",
1021 &cinode->vfs_inode);
1022 } else if (oplock == SMB2_OPLOCK_LEVEL_EXCLUSIVE) {
42873b0a 1023 cinode->oplock = CIFS_CACHE_RW_FLG;
53ef1016
PS
1024 cifs_dbg(FYI, "Exclusive Oplock granted on inode %p\n",
1025 &cinode->vfs_inode);
1026 } else if (oplock == SMB2_OPLOCK_LEVEL_II) {
1027 cinode->oplock = CIFS_CACHE_READ_FLG;
1028 cifs_dbg(FYI, "Level II Oplock granted on inode %p\n",
1029 &cinode->vfs_inode);
1030 } else
1031 cinode->oplock = 0;
1032}
1033
1034static void
42873b0a
PS
1035smb21_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
1036 unsigned int epoch, bool *purge_cache)
53ef1016
PS
1037{
1038 char message[5] = {0};
1039
1040 oplock &= 0xFF;
1041 if (oplock == SMB2_OPLOCK_LEVEL_NOCHANGE)
1042 return;
1043
1044 cinode->oplock = 0;
1045 if (oplock & SMB2_LEASE_READ_CACHING_HE) {
1046 cinode->oplock |= CIFS_CACHE_READ_FLG;
1047 strcat(message, "R");
1048 }
1049 if (oplock & SMB2_LEASE_HANDLE_CACHING_HE) {
1050 cinode->oplock |= CIFS_CACHE_HANDLE_FLG;
1051 strcat(message, "H");
1052 }
1053 if (oplock & SMB2_LEASE_WRITE_CACHING_HE) {
1054 cinode->oplock |= CIFS_CACHE_WRITE_FLG;
1055 strcat(message, "W");
1056 }
1057 if (!cinode->oplock)
1058 strcat(message, "None");
1059 cifs_dbg(FYI, "%s Lease granted on inode %p\n", message,
1060 &cinode->vfs_inode);
1061}
1062
42873b0a
PS
1063static void
1064smb3_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
1065 unsigned int epoch, bool *purge_cache)
1066{
1067 unsigned int old_oplock = cinode->oplock;
1068
1069 smb21_set_oplock_level(cinode, oplock, epoch, purge_cache);
1070
1071 if (purge_cache) {
1072 *purge_cache = false;
1073 if (old_oplock == CIFS_CACHE_READ_FLG) {
1074 if (cinode->oplock == CIFS_CACHE_READ_FLG &&
1075 (epoch - cinode->epoch > 0))
1076 *purge_cache = true;
1077 else if (cinode->oplock == CIFS_CACHE_RH_FLG &&
1078 (epoch - cinode->epoch > 1))
1079 *purge_cache = true;
1080 else if (cinode->oplock == CIFS_CACHE_RHW_FLG &&
1081 (epoch - cinode->epoch > 1))
1082 *purge_cache = true;
1083 else if (cinode->oplock == 0 &&
1084 (epoch - cinode->epoch > 0))
1085 *purge_cache = true;
1086 } else if (old_oplock == CIFS_CACHE_RH_FLG) {
1087 if (cinode->oplock == CIFS_CACHE_RH_FLG &&
1088 (epoch - cinode->epoch > 0))
1089 *purge_cache = true;
1090 else if (cinode->oplock == CIFS_CACHE_RHW_FLG &&
1091 (epoch - cinode->epoch > 1))
1092 *purge_cache = true;
1093 }
1094 cinode->epoch = epoch;
1095 }
1096}
1097
53ef1016
PS
1098static bool
1099smb2_is_read_op(__u32 oplock)
1100{
1101 return oplock == SMB2_OPLOCK_LEVEL_II;
1102}
1103
1104static bool
1105smb21_is_read_op(__u32 oplock)
1106{
1107 return (oplock & SMB2_LEASE_READ_CACHING_HE) &&
1108 !(oplock & SMB2_LEASE_WRITE_CACHING_HE);
1109}
1110
f047390a
PS
1111static __le32
1112map_oplock_to_lease(u8 oplock)
1113{
1114 if (oplock == SMB2_OPLOCK_LEVEL_EXCLUSIVE)
1115 return SMB2_LEASE_WRITE_CACHING | SMB2_LEASE_READ_CACHING;
1116 else if (oplock == SMB2_OPLOCK_LEVEL_II)
1117 return SMB2_LEASE_READ_CACHING;
1118 else if (oplock == SMB2_OPLOCK_LEVEL_BATCH)
1119 return SMB2_LEASE_HANDLE_CACHING | SMB2_LEASE_READ_CACHING |
1120 SMB2_LEASE_WRITE_CACHING;
1121 return 0;
1122}
1123
a41a28bd
PS
1124static char *
1125smb2_create_lease_buf(u8 *lease_key, u8 oplock)
1126{
1127 struct create_lease *buf;
1128
1129 buf = kzalloc(sizeof(struct create_lease), GFP_KERNEL);
1130 if (!buf)
1131 return NULL;
1132
1133 buf->lcontext.LeaseKeyLow = cpu_to_le64(*((u64 *)lease_key));
1134 buf->lcontext.LeaseKeyHigh = cpu_to_le64(*((u64 *)(lease_key + 8)));
f047390a 1135 buf->lcontext.LeaseState = map_oplock_to_lease(oplock);
a41a28bd
PS
1136
1137 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1138 (struct create_lease, lcontext));
1139 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context));
1140 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1141 (struct create_lease, Name));
1142 buf->ccontext.NameLength = cpu_to_le16(4);
12197a7f 1143 /* SMB2_CREATE_REQUEST_LEASE is "RqLs" */
a41a28bd
PS
1144 buf->Name[0] = 'R';
1145 buf->Name[1] = 'q';
1146 buf->Name[2] = 'L';
1147 buf->Name[3] = 's';
1148 return (char *)buf;
1149}
1150
f047390a
PS
1151static char *
1152smb3_create_lease_buf(u8 *lease_key, u8 oplock)
1153{
1154 struct create_lease_v2 *buf;
1155
1156 buf = kzalloc(sizeof(struct create_lease_v2), GFP_KERNEL);
1157 if (!buf)
1158 return NULL;
1159
1160 buf->lcontext.LeaseKeyLow = cpu_to_le64(*((u64 *)lease_key));
1161 buf->lcontext.LeaseKeyHigh = cpu_to_le64(*((u64 *)(lease_key + 8)));
1162 buf->lcontext.LeaseState = map_oplock_to_lease(oplock);
1163
1164 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1165 (struct create_lease_v2, lcontext));
1166 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context_v2));
1167 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1168 (struct create_lease_v2, Name));
1169 buf->ccontext.NameLength = cpu_to_le16(4);
12197a7f 1170 /* SMB2_CREATE_REQUEST_LEASE is "RqLs" */
f047390a
PS
1171 buf->Name[0] = 'R';
1172 buf->Name[1] = 'q';
1173 buf->Name[2] = 'L';
1174 buf->Name[3] = 's';
1175 return (char *)buf;
1176}
1177
b5c7cde3 1178static __u8
42873b0a 1179smb2_parse_lease_buf(void *buf, unsigned int *epoch)
b5c7cde3
PS
1180{
1181 struct create_lease *lc = (struct create_lease *)buf;
1182
42873b0a 1183 *epoch = 0; /* not used */
b5c7cde3
PS
1184 if (lc->lcontext.LeaseFlags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS)
1185 return SMB2_OPLOCK_LEVEL_NOCHANGE;
1186 return le32_to_cpu(lc->lcontext.LeaseState);
1187}
1188
f047390a 1189static __u8
42873b0a 1190smb3_parse_lease_buf(void *buf, unsigned int *epoch)
f047390a
PS
1191{
1192 struct create_lease_v2 *lc = (struct create_lease_v2 *)buf;
1193
42873b0a 1194 *epoch = le16_to_cpu(lc->lcontext.Epoch);
f047390a
PS
1195 if (lc->lcontext.LeaseFlags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS)
1196 return SMB2_OPLOCK_LEVEL_NOCHANGE;
1197 return le32_to_cpu(lc->lcontext.LeaseState);
1198}
1199
7f6c5008
PS
1200static unsigned int
1201smb2_wp_retry_size(struct inode *inode)
1202{
1203 return min_t(unsigned int, CIFS_SB(inode->i_sb)->wsize,
1204 SMB2_MAX_BUFFER_SIZE);
1205}
1206
53ef1016 1207struct smb_version_operations smb20_operations = {
027e8eec 1208 .compare_fids = smb2_compare_fids,
2dc7e1c0 1209 .setup_request = smb2_setup_request,
c95b8eed 1210 .setup_async_request = smb2_setup_async_request,
2dc7e1c0 1211 .check_receive = smb2_check_receive,
28ea5290
PS
1212 .add_credits = smb2_add_credits,
1213 .set_credits = smb2_set_credits,
1214 .get_credits_field = smb2_get_credits_field,
1215 .get_credits = smb2_get_credits,
cb7e9eab 1216 .wait_mtu_credits = cifs_wait_mtu_credits,
2dc7e1c0 1217 .get_next_mid = smb2_get_next_mid,
09a4707e
PS
1218 .read_data_offset = smb2_read_data_offset,
1219 .read_data_length = smb2_read_data_length,
1220 .map_error = map_smb2_to_linux_error,
093b2bda
PS
1221 .find_mid = smb2_find_mid,
1222 .check_message = smb2_check_message,
1223 .dump_detail = smb2_dump_detail,
d60622eb
PS
1224 .clear_stats = smb2_clear_stats,
1225 .print_stats = smb2_print_stats,
983c88a4 1226 .is_oplock_break = smb2_is_valid_oplock_break,
c11f1df5 1227 .downgrade_oplock = smb2_downgrade_oplock,
ec2e4523
PS
1228 .need_neg = smb2_need_neg,
1229 .negotiate = smb2_negotiate,
3a3bab50
PS
1230 .negotiate_wsize = smb2_negotiate_wsize,
1231 .negotiate_rsize = smb2_negotiate_rsize,
5478f9ba
PS
1232 .sess_setup = SMB2_sess_setup,
1233 .logoff = SMB2_logoff,
faaf946a
PS
1234 .tree_connect = SMB2_tcon,
1235 .tree_disconnect = SMB2_tdis,
34f62640 1236 .qfs_tcon = smb2_qfs_tcon,
2503a0db 1237 .is_path_accessible = smb2_is_path_accessible,
9094fad1
PS
1238 .can_echo = smb2_can_echo,
1239 .echo = SMB2_echo,
be4cb9e3
PS
1240 .query_path_info = smb2_query_path_info,
1241 .get_srv_inum = smb2_get_srv_inum,
b7546bc5 1242 .query_file_info = smb2_query_file_info,
c839ff24
PS
1243 .set_path_size = smb2_set_path_size,
1244 .set_file_size = smb2_set_file_size,
1feeaac7 1245 .set_file_info = smb2_set_file_info,
64a5cfa6 1246 .set_compression = smb2_set_compression,
a0e73183
PS
1247 .mkdir = smb2_mkdir,
1248 .mkdir_setinfo = smb2_mkdir_setinfo,
1a500f01 1249 .rmdir = smb2_rmdir,
cbe6f439 1250 .unlink = smb2_unlink,
35143eb5 1251 .rename = smb2_rename_path,
568798cc 1252 .create_hardlink = smb2_create_hardlink,
b42bf888 1253 .query_symlink = smb2_query_symlink,
f0df737e
PS
1254 .open = smb2_open_file,
1255 .set_fid = smb2_set_fid,
1256 .close = smb2_close_file,
7a5cfb19 1257 .flush = smb2_flush_file,
09a4707e 1258 .async_readv = smb2_async_readv,
33319141 1259 .async_writev = smb2_async_writev,
d8e05039 1260 .sync_read = smb2_sync_read,
009d3443 1261 .sync_write = smb2_sync_write,
d324f08d
PS
1262 .query_dir_first = smb2_query_dir_first,
1263 .query_dir_next = smb2_query_dir_next,
1264 .close_dir = smb2_close_dir,
1265 .calc_smb_size = smb2_calc_size,
2e44b288 1266 .is_status_pending = smb2_is_status_pending,
983c88a4 1267 .oplock_response = smb2_oplock_response,
6fc05c25 1268 .queryfs = smb2_queryfs,
f7ba7fe6
PS
1269 .mand_lock = smb2_mand_lock,
1270 .mand_unlock_range = smb2_unlock_range,
b140799a 1271 .push_mand_locks = smb2_push_mandatory_locks,
b8c32dbb
PS
1272 .get_lease_key = smb2_get_lease_key,
1273 .set_lease_key = smb2_set_lease_key,
1274 .new_lease_key = smb2_new_lease_key,
38107d45 1275 .calc_signature = smb2_calc_signature,
53ef1016
PS
1276 .is_read_op = smb2_is_read_op,
1277 .set_oplock_level = smb2_set_oplock_level,
a41a28bd 1278 .create_lease_buf = smb2_create_lease_buf,
b5c7cde3 1279 .parse_lease_buf = smb2_parse_lease_buf,
41c1358e 1280 .clone_range = smb2_clone_range,
7f6c5008 1281 .wp_retry_size = smb2_wp_retry_size,
38107d45
SF
1282};
1283
53ef1016
PS
1284struct smb_version_operations smb21_operations = {
1285 .compare_fids = smb2_compare_fids,
1286 .setup_request = smb2_setup_request,
1287 .setup_async_request = smb2_setup_async_request,
1288 .check_receive = smb2_check_receive,
1289 .add_credits = smb2_add_credits,
1290 .set_credits = smb2_set_credits,
1291 .get_credits_field = smb2_get_credits_field,
1292 .get_credits = smb2_get_credits,
cb7e9eab 1293 .wait_mtu_credits = smb2_wait_mtu_credits,
53ef1016
PS
1294 .get_next_mid = smb2_get_next_mid,
1295 .read_data_offset = smb2_read_data_offset,
1296 .read_data_length = smb2_read_data_length,
1297 .map_error = map_smb2_to_linux_error,
1298 .find_mid = smb2_find_mid,
1299 .check_message = smb2_check_message,
1300 .dump_detail = smb2_dump_detail,
1301 .clear_stats = smb2_clear_stats,
1302 .print_stats = smb2_print_stats,
1303 .is_oplock_break = smb2_is_valid_oplock_break,
c11f1df5 1304 .downgrade_oplock = smb2_downgrade_oplock,
53ef1016
PS
1305 .need_neg = smb2_need_neg,
1306 .negotiate = smb2_negotiate,
1307 .negotiate_wsize = smb2_negotiate_wsize,
1308 .negotiate_rsize = smb2_negotiate_rsize,
1309 .sess_setup = SMB2_sess_setup,
1310 .logoff = SMB2_logoff,
1311 .tree_connect = SMB2_tcon,
1312 .tree_disconnect = SMB2_tdis,
34f62640 1313 .qfs_tcon = smb2_qfs_tcon,
53ef1016
PS
1314 .is_path_accessible = smb2_is_path_accessible,
1315 .can_echo = smb2_can_echo,
1316 .echo = SMB2_echo,
1317 .query_path_info = smb2_query_path_info,
1318 .get_srv_inum = smb2_get_srv_inum,
1319 .query_file_info = smb2_query_file_info,
1320 .set_path_size = smb2_set_path_size,
1321 .set_file_size = smb2_set_file_size,
1322 .set_file_info = smb2_set_file_info,
64a5cfa6 1323 .set_compression = smb2_set_compression,
53ef1016
PS
1324 .mkdir = smb2_mkdir,
1325 .mkdir_setinfo = smb2_mkdir_setinfo,
1326 .rmdir = smb2_rmdir,
1327 .unlink = smb2_unlink,
1328 .rename = smb2_rename_path,
1329 .create_hardlink = smb2_create_hardlink,
1330 .query_symlink = smb2_query_symlink,
1331 .open = smb2_open_file,
1332 .set_fid = smb2_set_fid,
1333 .close = smb2_close_file,
1334 .flush = smb2_flush_file,
1335 .async_readv = smb2_async_readv,
1336 .async_writev = smb2_async_writev,
1337 .sync_read = smb2_sync_read,
1338 .sync_write = smb2_sync_write,
1339 .query_dir_first = smb2_query_dir_first,
1340 .query_dir_next = smb2_query_dir_next,
1341 .close_dir = smb2_close_dir,
1342 .calc_smb_size = smb2_calc_size,
1343 .is_status_pending = smb2_is_status_pending,
1344 .oplock_response = smb2_oplock_response,
1345 .queryfs = smb2_queryfs,
1346 .mand_lock = smb2_mand_lock,
1347 .mand_unlock_range = smb2_unlock_range,
1348 .push_mand_locks = smb2_push_mandatory_locks,
1349 .get_lease_key = smb2_get_lease_key,
1350 .set_lease_key = smb2_set_lease_key,
1351 .new_lease_key = smb2_new_lease_key,
1352 .calc_signature = smb2_calc_signature,
1353 .is_read_op = smb21_is_read_op,
1354 .set_oplock_level = smb21_set_oplock_level,
a41a28bd 1355 .create_lease_buf = smb2_create_lease_buf,
b5c7cde3 1356 .parse_lease_buf = smb2_parse_lease_buf,
41c1358e 1357 .clone_range = smb2_clone_range,
7f6c5008 1358 .wp_retry_size = smb2_wp_retry_size,
53ef1016 1359};
38107d45
SF
1360
1361struct smb_version_operations smb30_operations = {
1362 .compare_fids = smb2_compare_fids,
1363 .setup_request = smb2_setup_request,
1364 .setup_async_request = smb2_setup_async_request,
1365 .check_receive = smb2_check_receive,
1366 .add_credits = smb2_add_credits,
1367 .set_credits = smb2_set_credits,
1368 .get_credits_field = smb2_get_credits_field,
1369 .get_credits = smb2_get_credits,
cb7e9eab 1370 .wait_mtu_credits = smb2_wait_mtu_credits,
38107d45
SF
1371 .get_next_mid = smb2_get_next_mid,
1372 .read_data_offset = smb2_read_data_offset,
1373 .read_data_length = smb2_read_data_length,
1374 .map_error = map_smb2_to_linux_error,
1375 .find_mid = smb2_find_mid,
1376 .check_message = smb2_check_message,
1377 .dump_detail = smb2_dump_detail,
1378 .clear_stats = smb2_clear_stats,
1379 .print_stats = smb2_print_stats,
769ee6a4 1380 .dump_share_caps = smb2_dump_share_caps,
38107d45 1381 .is_oplock_break = smb2_is_valid_oplock_break,
c11f1df5 1382 .downgrade_oplock = smb2_downgrade_oplock,
38107d45
SF
1383 .need_neg = smb2_need_neg,
1384 .negotiate = smb2_negotiate,
1385 .negotiate_wsize = smb2_negotiate_wsize,
1386 .negotiate_rsize = smb2_negotiate_rsize,
1387 .sess_setup = SMB2_sess_setup,
1388 .logoff = SMB2_logoff,
1389 .tree_connect = SMB2_tcon,
1390 .tree_disconnect = SMB2_tdis,
af6a12ea 1391 .qfs_tcon = smb3_qfs_tcon,
38107d45
SF
1392 .is_path_accessible = smb2_is_path_accessible,
1393 .can_echo = smb2_can_echo,
1394 .echo = SMB2_echo,
1395 .query_path_info = smb2_query_path_info,
1396 .get_srv_inum = smb2_get_srv_inum,
1397 .query_file_info = smb2_query_file_info,
1398 .set_path_size = smb2_set_path_size,
1399 .set_file_size = smb2_set_file_size,
1400 .set_file_info = smb2_set_file_info,
64a5cfa6 1401 .set_compression = smb2_set_compression,
38107d45
SF
1402 .mkdir = smb2_mkdir,
1403 .mkdir_setinfo = smb2_mkdir_setinfo,
1404 .rmdir = smb2_rmdir,
1405 .unlink = smb2_unlink,
1406 .rename = smb2_rename_path,
1407 .create_hardlink = smb2_create_hardlink,
b42bf888 1408 .query_symlink = smb2_query_symlink,
38107d45
SF
1409 .open = smb2_open_file,
1410 .set_fid = smb2_set_fid,
1411 .close = smb2_close_file,
1412 .flush = smb2_flush_file,
1413 .async_readv = smb2_async_readv,
1414 .async_writev = smb2_async_writev,
1415 .sync_read = smb2_sync_read,
1416 .sync_write = smb2_sync_write,
1417 .query_dir_first = smb2_query_dir_first,
1418 .query_dir_next = smb2_query_dir_next,
1419 .close_dir = smb2_close_dir,
1420 .calc_smb_size = smb2_calc_size,
1421 .is_status_pending = smb2_is_status_pending,
1422 .oplock_response = smb2_oplock_response,
1423 .queryfs = smb2_queryfs,
1424 .mand_lock = smb2_mand_lock,
1425 .mand_unlock_range = smb2_unlock_range,
1426 .push_mand_locks = smb2_push_mandatory_locks,
1427 .get_lease_key = smb2_get_lease_key,
1428 .set_lease_key = smb2_set_lease_key,
1429 .new_lease_key = smb2_new_lease_key,
e65a5cb4 1430 .generate_signingkey = generate_smb3signingkey,
38107d45 1431 .calc_signature = smb3_calc_signature,
53ef1016 1432 .is_read_op = smb21_is_read_op,
42873b0a 1433 .set_oplock_level = smb3_set_oplock_level,
f047390a
PS
1434 .create_lease_buf = smb3_create_lease_buf,
1435 .parse_lease_buf = smb3_parse_lease_buf,
41c1358e 1436 .clone_range = smb2_clone_range,
ff1c038a 1437 .validate_negotiate = smb3_validate_negotiate,
7f6c5008 1438 .wp_retry_size = smb2_wp_retry_size,
1080ef75
SF
1439};
1440
dd446b16
SF
1441struct smb_version_values smb20_values = {
1442 .version_string = SMB20_VERSION_STRING,
1443 .protocol_id = SMB20_PROT_ID,
1444 .req_capabilities = 0, /* MBZ */
1445 .large_lock_type = 0,
1446 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
1447 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
1448 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
1449 .header_size = sizeof(struct smb2_hdr),
1450 .max_header_size = MAX_SMB2_HDR_SIZE,
1451 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
1452 .lock_cmd = SMB2_LOCK,
1453 .cap_unix = 0,
1454 .cap_nt_find = SMB2_NT_FIND,
1455 .cap_large_files = SMB2_LARGE_FILES,
50285882
JL
1456 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
1457 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
a41a28bd 1458 .create_lease_size = sizeof(struct create_lease),
dd446b16
SF
1459};
1460
1080ef75
SF
1461struct smb_version_values smb21_values = {
1462 .version_string = SMB21_VERSION_STRING,
e4aa25e7
SF
1463 .protocol_id = SMB21_PROT_ID,
1464 .req_capabilities = 0, /* MBZ on negotiate req until SMB3 dialect */
1465 .large_lock_type = 0,
1466 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
1467 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
1468 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
1469 .header_size = sizeof(struct smb2_hdr),
1470 .max_header_size = MAX_SMB2_HDR_SIZE,
1471 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
1472 .lock_cmd = SMB2_LOCK,
1473 .cap_unix = 0,
1474 .cap_nt_find = SMB2_NT_FIND,
1475 .cap_large_files = SMB2_LARGE_FILES,
50285882
JL
1476 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
1477 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
a41a28bd 1478 .create_lease_size = sizeof(struct create_lease),
e4aa25e7
SF
1479};
1480
1481struct smb_version_values smb30_values = {
1482 .version_string = SMB30_VERSION_STRING,
1483 .protocol_id = SMB30_PROT_ID,
1484 .req_capabilities = SMB2_GLOBAL_CAP_DFS | SMB2_GLOBAL_CAP_LEASING | SMB2_GLOBAL_CAP_LARGE_MTU,
027e8eec
PS
1485 .large_lock_type = 0,
1486 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
1487 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
1488 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
093b2bda
PS
1489 .header_size = sizeof(struct smb2_hdr),
1490 .max_header_size = MAX_SMB2_HDR_SIZE,
09a4707e 1491 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
2dc7e1c0 1492 .lock_cmd = SMB2_LOCK,
29e20f9c
PS
1493 .cap_unix = 0,
1494 .cap_nt_find = SMB2_NT_FIND,
1495 .cap_large_files = SMB2_LARGE_FILES,
50285882
JL
1496 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
1497 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
f047390a 1498 .create_lease_size = sizeof(struct create_lease_v2),
1080ef75 1499};
20b6d8b4
SF
1500
1501struct smb_version_values smb302_values = {
1502 .version_string = SMB302_VERSION_STRING,
1503 .protocol_id = SMB302_PROT_ID,
1504 .req_capabilities = SMB2_GLOBAL_CAP_DFS | SMB2_GLOBAL_CAP_LEASING | SMB2_GLOBAL_CAP_LARGE_MTU,
1505 .large_lock_type = 0,
1506 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
1507 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
1508 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
1509 .header_size = sizeof(struct smb2_hdr),
1510 .max_header_size = MAX_SMB2_HDR_SIZE,
1511 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
1512 .lock_cmd = SMB2_LOCK,
1513 .cap_unix = 0,
1514 .cap_nt_find = SMB2_NT_FIND,
1515 .cap_large_files = SMB2_LARGE_FILES,
50285882
JL
1516 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
1517 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
f047390a 1518 .create_lease_size = sizeof(struct create_lease_v2),
20b6d8b4 1519};
This page took 0.187874 seconds and 5 git commands to generate.