]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - fs/cifs/smb2ops.c
Add Get/Set Integrity Information structure definitions
[mirror_ubuntu-bionic-kernel.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;
9235d098 179 __u64 wire_mid = le64_to_cpu(hdr->MessageId);
093b2bda
PS
180
181 spin_lock(&GlobalMid_Lock);
182 list_for_each_entry(mid, &server->pending_mid_q, qhead) {
9235d098 183 if ((mid->mid == wire_mid) &&
093b2bda
PS
184 (mid->mid_state == MID_REQUEST_SUBMITTED) &&
185 (mid->command == hdr->Command)) {
186 spin_unlock(&GlobalMid_Lock);
187 return mid;
188 }
189 }
190 spin_unlock(&GlobalMid_Lock);
191 return NULL;
192}
193
194static void
195smb2_dump_detail(void *buf)
196{
197#ifdef CONFIG_CIFS_DEBUG2
198 struct smb2_hdr *smb = (struct smb2_hdr *)buf;
199
f96637be
JP
200 cifs_dbg(VFS, "Cmd: %d Err: 0x%x Flags: 0x%x Mid: %llu Pid: %d\n",
201 smb->Command, smb->Status, smb->Flags, smb->MessageId,
202 smb->ProcessId);
203 cifs_dbg(VFS, "smb buf %p len %u\n", smb, smb2_calc_size(smb));
093b2bda
PS
204#endif
205}
206
ec2e4523
PS
207static bool
208smb2_need_neg(struct TCP_Server_Info *server)
209{
210 return server->max_read == 0;
211}
212
213static int
214smb2_negotiate(const unsigned int xid, struct cifs_ses *ses)
215{
216 int rc;
217 ses->server->CurrentMid = 0;
218 rc = SMB2_negotiate(xid, ses);
219 /* BB we probably don't need to retry with modern servers */
220 if (rc == -EAGAIN)
221 rc = -EHOSTDOWN;
222 return rc;
223}
224
3a3bab50
PS
225static unsigned int
226smb2_negotiate_wsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
227{
228 struct TCP_Server_Info *server = tcon->ses->server;
229 unsigned int wsize;
230
231 /* start with specified wsize, or default */
232 wsize = volume_info->wsize ? volume_info->wsize : CIFS_DEFAULT_IOSIZE;
233 wsize = min_t(unsigned int, wsize, server->max_write);
cb7e9eab
PS
234
235 if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
236 wsize = min_t(unsigned int, wsize, SMB2_MAX_BUFFER_SIZE);
3a3bab50 237
3a3bab50
PS
238 return wsize;
239}
240
241static unsigned int
242smb2_negotiate_rsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
243{
244 struct TCP_Server_Info *server = tcon->ses->server;
245 unsigned int rsize;
246
247 /* start with specified rsize, or default */
248 rsize = volume_info->rsize ? volume_info->rsize : CIFS_DEFAULT_IOSIZE;
249 rsize = min_t(unsigned int, rsize, server->max_read);
bed9da02
PS
250
251 if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
252 rsize = min_t(unsigned int, rsize, SMB2_MAX_BUFFER_SIZE);
3a3bab50 253
3a3bab50
PS
254 return rsize;
255}
256
c481e9fe
SF
257#ifdef CONFIG_CIFS_STATS2
258static int
259SMB3_request_interfaces(const unsigned int xid, struct cifs_tcon *tcon)
260{
261 int rc;
262 unsigned int ret_data_len = 0;
263 struct network_interface_info_ioctl_rsp *out_buf;
264
265 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
266 FSCTL_QUERY_NETWORK_INTERFACE_INFO, true /* is_fsctl */,
267 NULL /* no data input */, 0 /* no data input */,
268 (char **)&out_buf, &ret_data_len);
9ffc5412
SF
269 if (rc != 0)
270 cifs_dbg(VFS, "error %d on ioctl to get interface list\n", rc);
271 else if (ret_data_len < sizeof(struct network_interface_info_ioctl_rsp)) {
272 cifs_dbg(VFS, "server returned bad net interface info buf\n");
273 rc = -EINVAL;
274 } else {
c481e9fe
SF
275 /* Dump info on first interface */
276 cifs_dbg(FYI, "Adapter Capability 0x%x\t",
277 le32_to_cpu(out_buf->Capability));
278 cifs_dbg(FYI, "Link Speed %lld\n",
279 le64_to_cpu(out_buf->LinkSpeed));
9ffc5412 280 }
c481e9fe
SF
281
282 return rc;
283}
284#endif /* STATS2 */
285
af6a12ea
SF
286static void
287smb3_qfs_tcon(const unsigned int xid, struct cifs_tcon *tcon)
288{
289 int rc;
290 __le16 srch_path = 0; /* Null - open root of share */
291 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
292 struct cifs_open_parms oparms;
293 struct cifs_fid fid;
294
295 oparms.tcon = tcon;
296 oparms.desired_access = FILE_READ_ATTRIBUTES;
297 oparms.disposition = FILE_OPEN;
298 oparms.create_options = 0;
299 oparms.fid = &fid;
300 oparms.reconnect = false;
301
302 rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL);
303 if (rc)
304 return;
305
c481e9fe
SF
306#ifdef CONFIG_CIFS_STATS2
307 SMB3_request_interfaces(xid, tcon);
308#endif /* STATS2 */
309
af6a12ea
SF
310 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
311 FS_ATTRIBUTE_INFORMATION);
312 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
313 FS_DEVICE_INFORMATION);
314 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
315 FS_SECTOR_SIZE_INFORMATION); /* SMB3 specific */
316 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
317 return;
318}
319
34f62640
SF
320static void
321smb2_qfs_tcon(const unsigned int xid, struct cifs_tcon *tcon)
322{
323 int rc;
324 __le16 srch_path = 0; /* Null - open root of share */
325 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
326 struct cifs_open_parms oparms;
327 struct cifs_fid fid;
328
329 oparms.tcon = tcon;
330 oparms.desired_access = FILE_READ_ATTRIBUTES;
331 oparms.disposition = FILE_OPEN;
332 oparms.create_options = 0;
333 oparms.fid = &fid;
334 oparms.reconnect = false;
335
336 rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL);
337 if (rc)
338 return;
339
2167114c
SF
340 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
341 FS_ATTRIBUTE_INFORMATION);
342 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
343 FS_DEVICE_INFORMATION);
34f62640
SF
344 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
345 return;
346}
347
2503a0db
PS
348static int
349smb2_is_path_accessible(const unsigned int xid, struct cifs_tcon *tcon,
350 struct cifs_sb_info *cifs_sb, const char *full_path)
351{
352 int rc;
2503a0db 353 __le16 *utf16_path;
2e44b288 354 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
064f6047
PS
355 struct cifs_open_parms oparms;
356 struct cifs_fid fid;
2503a0db
PS
357
358 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
359 if (!utf16_path)
360 return -ENOMEM;
361
064f6047
PS
362 oparms.tcon = tcon;
363 oparms.desired_access = FILE_READ_ATTRIBUTES;
364 oparms.disposition = FILE_OPEN;
365 oparms.create_options = 0;
366 oparms.fid = &fid;
9cbc0b73 367 oparms.reconnect = false;
064f6047 368
b42bf888 369 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL);
2503a0db
PS
370 if (rc) {
371 kfree(utf16_path);
372 return rc;
373 }
374
064f6047 375 rc = SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
2503a0db
PS
376 kfree(utf16_path);
377 return rc;
378}
379
be4cb9e3
PS
380static int
381smb2_get_srv_inum(const unsigned int xid, struct cifs_tcon *tcon,
382 struct cifs_sb_info *cifs_sb, const char *full_path,
383 u64 *uniqueid, FILE_ALL_INFO *data)
384{
385 *uniqueid = le64_to_cpu(data->IndexNumber);
386 return 0;
387}
388
b7546bc5
PS
389static int
390smb2_query_file_info(const unsigned int xid, struct cifs_tcon *tcon,
391 struct cifs_fid *fid, FILE_ALL_INFO *data)
392{
393 int rc;
394 struct smb2_file_all_info *smb2_data;
395
1bbe4997 396 smb2_data = kzalloc(sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
b7546bc5
PS
397 GFP_KERNEL);
398 if (smb2_data == NULL)
399 return -ENOMEM;
400
401 rc = SMB2_query_info(xid, tcon, fid->persistent_fid, fid->volatile_fid,
402 smb2_data);
403 if (!rc)
404 move_smb2_info_to_cifs(data, smb2_data);
405 kfree(smb2_data);
406 return rc;
407}
408
9094fad1
PS
409static bool
410smb2_can_echo(struct TCP_Server_Info *server)
411{
412 return server->echoes;
413}
414
d60622eb
PS
415static void
416smb2_clear_stats(struct cifs_tcon *tcon)
417{
418#ifdef CONFIG_CIFS_STATS
419 int i;
420 for (i = 0; i < NUMBER_OF_SMB2_COMMANDS; i++) {
421 atomic_set(&tcon->stats.smb2_stats.smb2_com_sent[i], 0);
422 atomic_set(&tcon->stats.smb2_stats.smb2_com_failed[i], 0);
423 }
424#endif
425}
426
769ee6a4
SF
427static void
428smb2_dump_share_caps(struct seq_file *m, struct cifs_tcon *tcon)
429{
430 seq_puts(m, "\n\tShare Capabilities:");
431 if (tcon->capabilities & SMB2_SHARE_CAP_DFS)
432 seq_puts(m, " DFS,");
433 if (tcon->capabilities & SMB2_SHARE_CAP_CONTINUOUS_AVAILABILITY)
434 seq_puts(m, " CONTINUOUS AVAILABILITY,");
435 if (tcon->capabilities & SMB2_SHARE_CAP_SCALEOUT)
436 seq_puts(m, " SCALEOUT,");
437 if (tcon->capabilities & SMB2_SHARE_CAP_CLUSTER)
438 seq_puts(m, " CLUSTER,");
439 if (tcon->capabilities & SMB2_SHARE_CAP_ASYMMETRIC)
440 seq_puts(m, " ASYMMETRIC,");
441 if (tcon->capabilities == 0)
442 seq_puts(m, " None");
af6a12ea
SF
443 if (tcon->ss_flags & SSINFO_FLAGS_ALIGNED_DEVICE)
444 seq_puts(m, " Aligned,");
445 if (tcon->ss_flags & SSINFO_FLAGS_PARTITION_ALIGNED_ON_DEVICE)
446 seq_puts(m, " Partition Aligned,");
447 if (tcon->ss_flags & SSINFO_FLAGS_NO_SEEK_PENALTY)
448 seq_puts(m, " SSD,");
449 if (tcon->ss_flags & SSINFO_FLAGS_TRIM_ENABLED)
450 seq_puts(m, " TRIM-support,");
451
769ee6a4 452 seq_printf(m, "\tShare Flags: 0x%x", tcon->share_flags);
af6a12ea
SF
453 if (tcon->perf_sector_size)
454 seq_printf(m, "\tOptimal sector size: 0x%x",
455 tcon->perf_sector_size);
769ee6a4
SF
456}
457
d60622eb
PS
458static void
459smb2_print_stats(struct seq_file *m, struct cifs_tcon *tcon)
460{
461#ifdef CONFIG_CIFS_STATS
462 atomic_t *sent = tcon->stats.smb2_stats.smb2_com_sent;
463 atomic_t *failed = tcon->stats.smb2_stats.smb2_com_failed;
464 seq_printf(m, "\nNegotiates: %d sent %d failed",
465 atomic_read(&sent[SMB2_NEGOTIATE_HE]),
466 atomic_read(&failed[SMB2_NEGOTIATE_HE]));
467 seq_printf(m, "\nSessionSetups: %d sent %d failed",
468 atomic_read(&sent[SMB2_SESSION_SETUP_HE]),
469 atomic_read(&failed[SMB2_SESSION_SETUP_HE]));
d60622eb
PS
470 seq_printf(m, "\nLogoffs: %d sent %d failed",
471 atomic_read(&sent[SMB2_LOGOFF_HE]),
472 atomic_read(&failed[SMB2_LOGOFF_HE]));
473 seq_printf(m, "\nTreeConnects: %d sent %d failed",
474 atomic_read(&sent[SMB2_TREE_CONNECT_HE]),
475 atomic_read(&failed[SMB2_TREE_CONNECT_HE]));
476 seq_printf(m, "\nTreeDisconnects: %d sent %d failed",
477 atomic_read(&sent[SMB2_TREE_DISCONNECT_HE]),
478 atomic_read(&failed[SMB2_TREE_DISCONNECT_HE]));
479 seq_printf(m, "\nCreates: %d sent %d failed",
480 atomic_read(&sent[SMB2_CREATE_HE]),
481 atomic_read(&failed[SMB2_CREATE_HE]));
482 seq_printf(m, "\nCloses: %d sent %d failed",
483 atomic_read(&sent[SMB2_CLOSE_HE]),
484 atomic_read(&failed[SMB2_CLOSE_HE]));
485 seq_printf(m, "\nFlushes: %d sent %d failed",
486 atomic_read(&sent[SMB2_FLUSH_HE]),
487 atomic_read(&failed[SMB2_FLUSH_HE]));
488 seq_printf(m, "\nReads: %d sent %d failed",
489 atomic_read(&sent[SMB2_READ_HE]),
490 atomic_read(&failed[SMB2_READ_HE]));
491 seq_printf(m, "\nWrites: %d sent %d failed",
492 atomic_read(&sent[SMB2_WRITE_HE]),
493 atomic_read(&failed[SMB2_WRITE_HE]));
494 seq_printf(m, "\nLocks: %d sent %d failed",
495 atomic_read(&sent[SMB2_LOCK_HE]),
496 atomic_read(&failed[SMB2_LOCK_HE]));
497 seq_printf(m, "\nIOCTLs: %d sent %d failed",
498 atomic_read(&sent[SMB2_IOCTL_HE]),
499 atomic_read(&failed[SMB2_IOCTL_HE]));
500 seq_printf(m, "\nCancels: %d sent %d failed",
501 atomic_read(&sent[SMB2_CANCEL_HE]),
502 atomic_read(&failed[SMB2_CANCEL_HE]));
503 seq_printf(m, "\nEchos: %d sent %d failed",
504 atomic_read(&sent[SMB2_ECHO_HE]),
505 atomic_read(&failed[SMB2_ECHO_HE]));
506 seq_printf(m, "\nQueryDirectories: %d sent %d failed",
507 atomic_read(&sent[SMB2_QUERY_DIRECTORY_HE]),
508 atomic_read(&failed[SMB2_QUERY_DIRECTORY_HE]));
509 seq_printf(m, "\nChangeNotifies: %d sent %d failed",
510 atomic_read(&sent[SMB2_CHANGE_NOTIFY_HE]),
511 atomic_read(&failed[SMB2_CHANGE_NOTIFY_HE]));
512 seq_printf(m, "\nQueryInfos: %d sent %d failed",
513 atomic_read(&sent[SMB2_QUERY_INFO_HE]),
514 atomic_read(&failed[SMB2_QUERY_INFO_HE]));
515 seq_printf(m, "\nSetInfos: %d sent %d failed",
516 atomic_read(&sent[SMB2_SET_INFO_HE]),
517 atomic_read(&failed[SMB2_SET_INFO_HE]));
518 seq_printf(m, "\nOplockBreaks: %d sent %d failed",
519 atomic_read(&sent[SMB2_OPLOCK_BREAK_HE]),
520 atomic_read(&failed[SMB2_OPLOCK_BREAK_HE]));
521#endif
522}
523
f0df737e
PS
524static void
525smb2_set_fid(struct cifsFileInfo *cfile, struct cifs_fid *fid, __u32 oplock)
526{
2b0143b5 527 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
53ef1016
PS
528 struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server;
529
f0df737e
PS
530 cfile->fid.persistent_fid = fid->persistent_fid;
531 cfile->fid.volatile_fid = fid->volatile_fid;
42873b0a
PS
532 server->ops->set_oplock_level(cinode, oplock, fid->epoch,
533 &fid->purge_cache);
18cceb6a 534 cinode->can_cache_brlcks = CIFS_CACHE_WRITE(cinode);
f0df737e
PS
535}
536
760ad0ca 537static void
f0df737e
PS
538smb2_close_file(const unsigned int xid, struct cifs_tcon *tcon,
539 struct cifs_fid *fid)
540{
760ad0ca 541 SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
f0df737e
PS
542}
543
41c1358e
SF
544static int
545SMB2_request_res_key(const unsigned int xid, struct cifs_tcon *tcon,
546 u64 persistent_fid, u64 volatile_fid,
547 struct copychunk_ioctl *pcchunk)
548{
549 int rc;
550 unsigned int ret_data_len;
551 struct resume_key_req *res_key;
552
553 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
554 FSCTL_SRV_REQUEST_RESUME_KEY, true /* is_fsctl */,
555 NULL, 0 /* no input */,
556 (char **)&res_key, &ret_data_len);
557
558 if (rc) {
559 cifs_dbg(VFS, "refcpy ioctl error %d getting resume key\n", rc);
560 goto req_res_key_exit;
561 }
562 if (ret_data_len < sizeof(struct resume_key_req)) {
563 cifs_dbg(VFS, "Invalid refcopy resume key length\n");
564 rc = -EINVAL;
565 goto req_res_key_exit;
566 }
567 memcpy(pcchunk->SourceKey, res_key->ResumeKey, COPY_CHUNK_RES_KEY_SIZE);
568
569req_res_key_exit:
570 kfree(res_key);
571 return rc;
572}
573
574static int
575smb2_clone_range(const unsigned int xid,
576 struct cifsFileInfo *srcfile,
577 struct cifsFileInfo *trgtfile, u64 src_off,
578 u64 len, u64 dest_off)
579{
580 int rc;
581 unsigned int ret_data_len;
582 struct copychunk_ioctl *pcchunk;
9bf0c9cd
SF
583 struct copychunk_ioctl_rsp *retbuf = NULL;
584 struct cifs_tcon *tcon;
585 int chunks_copied = 0;
586 bool chunk_sizes_updated = false;
41c1358e
SF
587
588 pcchunk = kmalloc(sizeof(struct copychunk_ioctl), GFP_KERNEL);
589
590 if (pcchunk == NULL)
591 return -ENOMEM;
592
593 cifs_dbg(FYI, "in smb2_clone_range - about to call request res key\n");
594 /* Request a key from the server to identify the source of the copy */
595 rc = SMB2_request_res_key(xid, tlink_tcon(srcfile->tlink),
596 srcfile->fid.persistent_fid,
597 srcfile->fid.volatile_fid, pcchunk);
598
599 /* Note: request_res_key sets res_key null only if rc !=0 */
600 if (rc)
9bf0c9cd 601 goto cchunk_out;
41c1358e
SF
602
603 /* For now array only one chunk long, will make more flexible later */
bc09d141 604 pcchunk->ChunkCount = cpu_to_le32(1);
41c1358e 605 pcchunk->Reserved = 0;
41c1358e
SF
606 pcchunk->Reserved2 = 0;
607
9bf0c9cd 608 tcon = tlink_tcon(trgtfile->tlink);
41c1358e 609
9bf0c9cd
SF
610 while (len > 0) {
611 pcchunk->SourceOffset = cpu_to_le64(src_off);
612 pcchunk->TargetOffset = cpu_to_le64(dest_off);
613 pcchunk->Length =
614 cpu_to_le32(min_t(u32, len, tcon->max_bytes_chunk));
41c1358e 615
9bf0c9cd
SF
616 /* Request server copy to target from src identified by key */
617 rc = SMB2_ioctl(xid, tcon, trgtfile->fid.persistent_fid,
618 trgtfile->fid.volatile_fid, FSCTL_SRV_COPYCHUNK_WRITE,
619 true /* is_fsctl */, (char *)pcchunk,
620 sizeof(struct copychunk_ioctl), (char **)&retbuf,
621 &ret_data_len);
622 if (rc == 0) {
623 if (ret_data_len !=
624 sizeof(struct copychunk_ioctl_rsp)) {
625 cifs_dbg(VFS, "invalid cchunk response size\n");
626 rc = -EIO;
627 goto cchunk_out;
628 }
629 if (retbuf->TotalBytesWritten == 0) {
630 cifs_dbg(FYI, "no bytes copied\n");
631 rc = -EIO;
632 goto cchunk_out;
633 }
634 /*
635 * Check if server claimed to write more than we asked
636 */
637 if (le32_to_cpu(retbuf->TotalBytesWritten) >
638 le32_to_cpu(pcchunk->Length)) {
639 cifs_dbg(VFS, "invalid copy chunk response\n");
640 rc = -EIO;
641 goto cchunk_out;
642 }
643 if (le32_to_cpu(retbuf->ChunksWritten) != 1) {
644 cifs_dbg(VFS, "invalid num chunks written\n");
645 rc = -EIO;
646 goto cchunk_out;
647 }
648 chunks_copied++;
649
650 src_off += le32_to_cpu(retbuf->TotalBytesWritten);
651 dest_off += le32_to_cpu(retbuf->TotalBytesWritten);
652 len -= le32_to_cpu(retbuf->TotalBytesWritten);
653
654 cifs_dbg(FYI, "Chunks %d PartialChunk %d Total %d\n",
655 le32_to_cpu(retbuf->ChunksWritten),
656 le32_to_cpu(retbuf->ChunkBytesWritten),
657 le32_to_cpu(retbuf->TotalBytesWritten));
658 } else if (rc == -EINVAL) {
659 if (ret_data_len != sizeof(struct copychunk_ioctl_rsp))
660 goto cchunk_out;
661
662 cifs_dbg(FYI, "MaxChunks %d BytesChunk %d MaxCopy %d\n",
663 le32_to_cpu(retbuf->ChunksWritten),
664 le32_to_cpu(retbuf->ChunkBytesWritten),
665 le32_to_cpu(retbuf->TotalBytesWritten));
666
667 /*
668 * Check if this is the first request using these sizes,
669 * (ie check if copy succeed once with original sizes
670 * and check if the server gave us different sizes after
671 * we already updated max sizes on previous request).
672 * if not then why is the server returning an error now
673 */
674 if ((chunks_copied != 0) || chunk_sizes_updated)
675 goto cchunk_out;
676
677 /* Check that server is not asking us to grow size */
678 if (le32_to_cpu(retbuf->ChunkBytesWritten) <
679 tcon->max_bytes_chunk)
680 tcon->max_bytes_chunk =
681 le32_to_cpu(retbuf->ChunkBytesWritten);
682 else
683 goto cchunk_out; /* server gave us bogus size */
684
685 /* No need to change MaxChunks since already set to 1 */
686 chunk_sizes_updated = true;
2477bc58
SP
687 } else
688 goto cchunk_out;
9bf0c9cd 689 }
41c1358e 690
9bf0c9cd 691cchunk_out:
41c1358e
SF
692 kfree(pcchunk);
693 return rc;
694}
695
7a5cfb19
PS
696static int
697smb2_flush_file(const unsigned int xid, struct cifs_tcon *tcon,
698 struct cifs_fid *fid)
699{
700 return SMB2_flush(xid, tcon, fid->persistent_fid, fid->volatile_fid);
701}
702
09a4707e
PS
703static unsigned int
704smb2_read_data_offset(char *buf)
705{
706 struct smb2_read_rsp *rsp = (struct smb2_read_rsp *)buf;
707 return rsp->DataOffset;
708}
709
710static unsigned int
711smb2_read_data_length(char *buf)
712{
713 struct smb2_read_rsp *rsp = (struct smb2_read_rsp *)buf;
714 return le32_to_cpu(rsp->DataLength);
715}
716
d8e05039
PS
717
718static int
db8b631d 719smb2_sync_read(const unsigned int xid, struct cifs_fid *pfid,
d8e05039
PS
720 struct cifs_io_parms *parms, unsigned int *bytes_read,
721 char **buf, int *buf_type)
722{
db8b631d
SF
723 parms->persistent_fid = pfid->persistent_fid;
724 parms->volatile_fid = pfid->volatile_fid;
d8e05039
PS
725 return SMB2_read(xid, parms, bytes_read, buf, buf_type);
726}
727
009d3443 728static int
db8b631d 729smb2_sync_write(const unsigned int xid, struct cifs_fid *pfid,
009d3443
PS
730 struct cifs_io_parms *parms, unsigned int *written,
731 struct kvec *iov, unsigned long nr_segs)
732{
733
db8b631d
SF
734 parms->persistent_fid = pfid->persistent_fid;
735 parms->volatile_fid = pfid->volatile_fid;
009d3443
PS
736 return SMB2_write(xid, parms, written, iov, nr_segs);
737}
738
d43cc793
SF
739/* Set or clear the SPARSE_FILE attribute based on value passed in setsparse */
740static bool smb2_set_sparse(const unsigned int xid, struct cifs_tcon *tcon,
741 struct cifsFileInfo *cfile, struct inode *inode, __u8 setsparse)
742{
743 struct cifsInodeInfo *cifsi;
744 int rc;
745
746 cifsi = CIFS_I(inode);
747
748 /* if file already sparse don't bother setting sparse again */
749 if ((cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) && setsparse)
750 return true; /* already sparse */
751
752 if (!(cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) && !setsparse)
753 return true; /* already not sparse */
754
755 /*
756 * Can't check for sparse support on share the usual way via the
757 * FS attribute info (FILE_SUPPORTS_SPARSE_FILES) on the share
758 * since Samba server doesn't set the flag on the share, yet
759 * supports the set sparse FSCTL and returns sparse correctly
760 * in the file attributes. If we fail setting sparse though we
761 * mark that server does not support sparse files for this share
762 * to avoid repeatedly sending the unsupported fsctl to server
763 * if the file is repeatedly extended.
764 */
765 if (tcon->broken_sparse_sup)
766 return false;
767
768 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
769 cfile->fid.volatile_fid, FSCTL_SET_SPARSE,
770 true /* is_fctl */, &setsparse, 1, NULL, NULL);
771 if (rc) {
772 tcon->broken_sparse_sup = true;
773 cifs_dbg(FYI, "set sparse rc = %d\n", rc);
774 return false;
775 }
776
777 if (setsparse)
778 cifsi->cifsAttrs |= FILE_ATTRIBUTE_SPARSE_FILE;
779 else
780 cifsi->cifsAttrs &= (~FILE_ATTRIBUTE_SPARSE_FILE);
781
782 return true;
783}
784
c839ff24
PS
785static int
786smb2_set_file_size(const unsigned int xid, struct cifs_tcon *tcon,
787 struct cifsFileInfo *cfile, __u64 size, bool set_alloc)
788{
789 __le64 eof = cpu_to_le64(size);
3d1a3745
SF
790 struct inode *inode;
791
792 /*
793 * If extending file more than one page make sparse. Many Linux fs
794 * make files sparse by default when extending via ftruncate
795 */
2b0143b5 796 inode = d_inode(cfile->dentry);
3d1a3745
SF
797
798 if (!set_alloc && (size > inode->i_size + 8192)) {
3d1a3745 799 __u8 set_sparse = 1;
d43cc793
SF
800
801 /* whether set sparse succeeds or not, extend the file */
802 smb2_set_sparse(xid, tcon, cfile, inode, set_sparse);
3d1a3745
SF
803 }
804
c839ff24 805 return SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
f29ebb47 806 cfile->fid.volatile_fid, cfile->pid, &eof, false);
c839ff24
PS
807}
808
02b16665
SF
809#ifdef CONFIG_CIFS_SMB311
810static int
811smb2_duplicate_extents(const unsigned int xid,
812 struct cifsFileInfo *srcfile,
813 struct cifsFileInfo *trgtfile, u64 src_off,
814 u64 len, u64 dest_off)
815{
816 int rc;
817 unsigned int ret_data_len;
818 char *retbuf = NULL;
819 struct duplicate_extents_to_file dup_ext_buf;
820 struct cifs_tcon *tcon = tlink_tcon(trgtfile->tlink);
821
822 /* server fileays advertise duplicate extent support with this flag */
823 if ((le32_to_cpu(tcon->fsAttrInfo.Attributes) &
824 FILE_SUPPORTS_BLOCK_REFCOUNTING) == 0)
825 return -EOPNOTSUPP;
826
827 dup_ext_buf.VolatileFileHandle = srcfile->fid.volatile_fid;
828 dup_ext_buf.PersistentFileHandle = srcfile->fid.persistent_fid;
829 dup_ext_buf.SourceFileOffset = cpu_to_le64(src_off);
830 dup_ext_buf.TargetFileOffset = cpu_to_le64(dest_off);
831 dup_ext_buf.ByteCount = cpu_to_le64(len);
832 cifs_dbg(FYI, "duplicate extents: src off %lld dst off %lld len %lld",
833 src_off, dest_off, len);
834
835 rc = smb2_set_file_size(xid, tcon, trgtfile, dest_off + len, false);
836 if (rc)
837 goto duplicate_extents_out;
838
839 rc = SMB2_ioctl(xid, tcon, trgtfile->fid.persistent_fid,
840 trgtfile->fid.volatile_fid,
841 FSCTL_DUPLICATE_EXTENTS_TO_FILE,
842 true /* is_fsctl */, (char *)&dup_ext_buf,
843 sizeof(struct duplicate_extents_to_file),
844 (char **)&retbuf,
845 &ret_data_len);
846
847 if (ret_data_len > 0)
848 cifs_dbg(FYI, "non-zero response length in duplicate extents");
849
850duplicate_extents_out:
851 return rc;
852}
853#endif /* CONFIG_CIFS_SMB311 */
854
855
64a5cfa6
SF
856static int
857smb2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
858 struct cifsFileInfo *cfile)
859{
860 return SMB2_set_compression(xid, tcon, cfile->fid.persistent_fid,
861 cfile->fid.volatile_fid);
862}
863
d324f08d
PS
864static int
865smb2_query_dir_first(const unsigned int xid, struct cifs_tcon *tcon,
866 const char *path, struct cifs_sb_info *cifs_sb,
867 struct cifs_fid *fid, __u16 search_flags,
868 struct cifs_search_info *srch_inf)
869{
870 __le16 *utf16_path;
871 int rc;
2e44b288 872 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
064f6047 873 struct cifs_open_parms oparms;
d324f08d
PS
874
875 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
876 if (!utf16_path)
877 return -ENOMEM;
878
064f6047
PS
879 oparms.tcon = tcon;
880 oparms.desired_access = FILE_READ_ATTRIBUTES | FILE_READ_DATA;
881 oparms.disposition = FILE_OPEN;
882 oparms.create_options = 0;
883 oparms.fid = fid;
9cbc0b73 884 oparms.reconnect = false;
064f6047 885
b42bf888 886 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL);
d324f08d
PS
887 kfree(utf16_path);
888 if (rc) {
f96637be 889 cifs_dbg(VFS, "open dir failed\n");
d324f08d
PS
890 return rc;
891 }
892
893 srch_inf->entries_in_buffer = 0;
894 srch_inf->index_of_last_entry = 0;
d324f08d 895
064f6047
PS
896 rc = SMB2_query_directory(xid, tcon, fid->persistent_fid,
897 fid->volatile_fid, 0, srch_inf);
d324f08d 898 if (rc) {
f96637be 899 cifs_dbg(VFS, "query directory failed\n");
064f6047 900 SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
d324f08d
PS
901 }
902 return rc;
903}
904
905static int
906smb2_query_dir_next(const unsigned int xid, struct cifs_tcon *tcon,
907 struct cifs_fid *fid, __u16 search_flags,
908 struct cifs_search_info *srch_inf)
909{
910 return SMB2_query_directory(xid, tcon, fid->persistent_fid,
911 fid->volatile_fid, 0, srch_inf);
912}
913
914static int
915smb2_close_dir(const unsigned int xid, struct cifs_tcon *tcon,
916 struct cifs_fid *fid)
917{
918 return SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
919}
920
2e44b288
PS
921/*
922* If we negotiate SMB2 protocol and get STATUS_PENDING - update
923* the number of credits and return true. Otherwise - return false.
924*/
925static bool
926smb2_is_status_pending(char *buf, struct TCP_Server_Info *server, int length)
927{
928 struct smb2_hdr *hdr = (struct smb2_hdr *)buf;
929
12e8a208 930 if (hdr->Status != STATUS_PENDING)
2e44b288
PS
931 return false;
932
933 if (!length) {
934 spin_lock(&server->req_lock);
935 server->credits += le16_to_cpu(hdr->CreditRequest);
936 spin_unlock(&server->req_lock);
937 wake_up(&server->request_q);
938 }
939
940 return true;
941}
942
983c88a4
PS
943static int
944smb2_oplock_response(struct cifs_tcon *tcon, struct cifs_fid *fid,
945 struct cifsInodeInfo *cinode)
946{
0822f514
PS
947 if (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_LEASING)
948 return SMB2_lease_break(0, tcon, cinode->lease_key,
949 smb2_get_lease_state(cinode));
950
983c88a4
PS
951 return SMB2_oplock_break(0, tcon, fid->persistent_fid,
952 fid->volatile_fid,
18cceb6a 953 CIFS_CACHE_READ(cinode) ? 1 : 0);
983c88a4
PS
954}
955
6fc05c25
PS
956static int
957smb2_queryfs(const unsigned int xid, struct cifs_tcon *tcon,
958 struct kstatfs *buf)
959{
960 int rc;
6fc05c25
PS
961 __le16 srch_path = 0; /* Null - open root of share */
962 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
064f6047
PS
963 struct cifs_open_parms oparms;
964 struct cifs_fid fid;
965
966 oparms.tcon = tcon;
967 oparms.desired_access = FILE_READ_ATTRIBUTES;
968 oparms.disposition = FILE_OPEN;
969 oparms.create_options = 0;
970 oparms.fid = &fid;
9cbc0b73 971 oparms.reconnect = false;
6fc05c25 972
b42bf888 973 rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL);
6fc05c25
PS
974 if (rc)
975 return rc;
976 buf->f_type = SMB2_MAGIC_NUMBER;
064f6047
PS
977 rc = SMB2_QFS_info(xid, tcon, fid.persistent_fid, fid.volatile_fid,
978 buf);
979 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
6fc05c25
PS
980 return rc;
981}
982
027e8eec
PS
983static bool
984smb2_compare_fids(struct cifsFileInfo *ob1, struct cifsFileInfo *ob2)
985{
986 return ob1->fid.persistent_fid == ob2->fid.persistent_fid &&
987 ob1->fid.volatile_fid == ob2->fid.volatile_fid;
988}
989
f7ba7fe6
PS
990static int
991smb2_mand_lock(const unsigned int xid, struct cifsFileInfo *cfile, __u64 offset,
992 __u64 length, __u32 type, int lock, int unlock, bool wait)
993{
994 if (unlock && !lock)
995 type = SMB2_LOCKFLAG_UNLOCK;
996 return SMB2_lock(xid, tlink_tcon(cfile->tlink),
997 cfile->fid.persistent_fid, cfile->fid.volatile_fid,
998 current->tgid, length, offset, type, wait);
999}
1000
b8c32dbb
PS
1001static void
1002smb2_get_lease_key(struct inode *inode, struct cifs_fid *fid)
1003{
1004 memcpy(fid->lease_key, CIFS_I(inode)->lease_key, SMB2_LEASE_KEY_SIZE);
1005}
1006
1007static void
1008smb2_set_lease_key(struct inode *inode, struct cifs_fid *fid)
1009{
1010 memcpy(CIFS_I(inode)->lease_key, fid->lease_key, SMB2_LEASE_KEY_SIZE);
1011}
1012
1013static void
1014smb2_new_lease_key(struct cifs_fid *fid)
1015{
1016 get_random_bytes(fid->lease_key, SMB2_LEASE_KEY_SIZE);
1017}
1018
b42bf888
PS
1019static int
1020smb2_query_symlink(const unsigned int xid, struct cifs_tcon *tcon,
1021 const char *full_path, char **target_path,
1022 struct cifs_sb_info *cifs_sb)
1023{
1024 int rc;
1025 __le16 *utf16_path;
1026 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
1027 struct cifs_open_parms oparms;
1028 struct cifs_fid fid;
1029 struct smb2_err_rsp *err_buf = NULL;
1030 struct smb2_symlink_err_rsp *symlink;
1031 unsigned int sub_len, sub_offset;
1032
1033 cifs_dbg(FYI, "%s: path: %s\n", __func__, full_path);
1034
1035 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
1036 if (!utf16_path)
1037 return -ENOMEM;
1038
1039 oparms.tcon = tcon;
1040 oparms.desired_access = FILE_READ_ATTRIBUTES;
1041 oparms.disposition = FILE_OPEN;
1042 oparms.create_options = 0;
1043 oparms.fid = &fid;
1044 oparms.reconnect = false;
1045
1046 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, &err_buf);
1047
1048 if (!rc || !err_buf) {
1049 kfree(utf16_path);
1050 return -ENOENT;
1051 }
1052 /* open must fail on symlink - reset rc */
1053 rc = 0;
1054 symlink = (struct smb2_symlink_err_rsp *)err_buf->ErrorData;
1055 sub_len = le16_to_cpu(symlink->SubstituteNameLength);
1056 sub_offset = le16_to_cpu(symlink->SubstituteNameOffset);
1057 *target_path = cifs_strndup_from_utf16(
1058 (char *)symlink->PathBuffer + sub_offset,
1059 sub_len, true, cifs_sb->local_nls);
1060 if (!(*target_path)) {
1061 kfree(utf16_path);
1062 return -ENOMEM;
1063 }
1064 convert_delimiter(*target_path, '/');
1065 cifs_dbg(FYI, "%s: target path: %s\n", __func__, *target_path);
1066 kfree(utf16_path);
1067 return rc;
1068}
1069
30175628
SF
1070static long smb3_zero_range(struct file *file, struct cifs_tcon *tcon,
1071 loff_t offset, loff_t len, bool keep_size)
1072{
1073 struct inode *inode;
1074 struct cifsInodeInfo *cifsi;
1075 struct cifsFileInfo *cfile = file->private_data;
1076 struct file_zero_data_information fsctl_buf;
1077 long rc;
1078 unsigned int xid;
1079
1080 xid = get_xid();
1081
2b0143b5 1082 inode = d_inode(cfile->dentry);
30175628
SF
1083 cifsi = CIFS_I(inode);
1084
1085 /* if file not oplocked can't be sure whether asking to extend size */
1086 if (!CIFS_CACHE_READ(cifsi))
1087 if (keep_size == false)
1088 return -EOPNOTSUPP;
1089
2bb93d24 1090 /*
30175628
SF
1091 * Must check if file sparse since fallocate -z (zero range) assumes
1092 * non-sparse allocation
1093 */
1094 if (!(cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE))
1095 return -EOPNOTSUPP;
1096
1097 /*
1098 * need to make sure we are not asked to extend the file since the SMB3
1099 * fsctl does not change the file size. In the future we could change
1100 * this to zero the first part of the range then set the file size
1101 * which for a non sparse file would zero the newly extended range
1102 */
1103 if (keep_size == false)
1104 if (i_size_read(inode) < offset + len)
1105 return -EOPNOTSUPP;
1106
1107 cifs_dbg(FYI, "offset %lld len %lld", offset, len);
1108
1109 fsctl_buf.FileOffset = cpu_to_le64(offset);
1110 fsctl_buf.BeyondFinalZero = cpu_to_le64(offset + len);
1111
1112 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
1113 cfile->fid.volatile_fid, FSCTL_SET_ZERO_DATA,
1114 true /* is_fctl */, (char *)&fsctl_buf,
1115 sizeof(struct file_zero_data_information), NULL, NULL);
1116 free_xid(xid);
1117 return rc;
1118}
1119
31742c5a
SF
1120static long smb3_punch_hole(struct file *file, struct cifs_tcon *tcon,
1121 loff_t offset, loff_t len)
1122{
1123 struct inode *inode;
1124 struct cifsInodeInfo *cifsi;
1125 struct cifsFileInfo *cfile = file->private_data;
1126 struct file_zero_data_information fsctl_buf;
1127 long rc;
1128 unsigned int xid;
1129 __u8 set_sparse = 1;
1130
1131 xid = get_xid();
1132
2b0143b5 1133 inode = d_inode(cfile->dentry);
31742c5a
SF
1134 cifsi = CIFS_I(inode);
1135
1136 /* Need to make file sparse, if not already, before freeing range. */
1137 /* Consider adding equivalent for compressed since it could also work */
1138 if (!smb2_set_sparse(xid, tcon, cfile, inode, set_sparse))
1139 return -EOPNOTSUPP;
1140
1141 cifs_dbg(FYI, "offset %lld len %lld", offset, len);
1142
1143 fsctl_buf.FileOffset = cpu_to_le64(offset);
1144 fsctl_buf.BeyondFinalZero = cpu_to_le64(offset + len);
1145
1146 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
1147 cfile->fid.volatile_fid, FSCTL_SET_ZERO_DATA,
1148 true /* is_fctl */, (char *)&fsctl_buf,
1149 sizeof(struct file_zero_data_information), NULL, NULL);
1150 free_xid(xid);
1151 return rc;
1152}
1153
9ccf3216
SF
1154static long smb3_simple_falloc(struct file *file, struct cifs_tcon *tcon,
1155 loff_t off, loff_t len, bool keep_size)
1156{
1157 struct inode *inode;
1158 struct cifsInodeInfo *cifsi;
1159 struct cifsFileInfo *cfile = file->private_data;
1160 long rc = -EOPNOTSUPP;
1161 unsigned int xid;
1162
1163 xid = get_xid();
1164
2b0143b5 1165 inode = d_inode(cfile->dentry);
9ccf3216
SF
1166 cifsi = CIFS_I(inode);
1167
1168 /* if file not oplocked can't be sure whether asking to extend size */
1169 if (!CIFS_CACHE_READ(cifsi))
1170 if (keep_size == false)
1171 return -EOPNOTSUPP;
1172
1173 /*
1174 * Files are non-sparse by default so falloc may be a no-op
1175 * Must check if file sparse. If not sparse, and not extending
1176 * then no need to do anything since file already allocated
1177 */
1178 if ((cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) == 0) {
1179 if (keep_size == true)
1180 return 0;
1181 /* check if extending file */
1182 else if (i_size_read(inode) >= off + len)
1183 /* not extending file and already not sparse */
1184 return 0;
1185 /* BB: in future add else clause to extend file */
1186 else
1187 return -EOPNOTSUPP;
1188 }
1189
1190 if ((keep_size == true) || (i_size_read(inode) >= off + len)) {
1191 /*
1192 * Check if falloc starts within first few pages of file
1193 * and ends within a few pages of the end of file to
1194 * ensure that most of file is being forced to be
1195 * fallocated now. If so then setting whole file sparse
1196 * ie potentially making a few extra pages at the beginning
1197 * or end of the file non-sparse via set_sparse is harmless.
1198 */
1199 if ((off > 8192) || (off + len + 8192 < i_size_read(inode)))
1200 return -EOPNOTSUPP;
1201
1202 rc = smb2_set_sparse(xid, tcon, cfile, inode, false);
1203 }
1204 /* BB: else ... in future add code to extend file and set sparse */
1205
1206
1207 free_xid(xid);
1208 return rc;
1209}
1210
1211
31742c5a
SF
1212static long smb3_fallocate(struct file *file, struct cifs_tcon *tcon, int mode,
1213 loff_t off, loff_t len)
1214{
1215 /* KEEP_SIZE already checked for by do_fallocate */
1216 if (mode & FALLOC_FL_PUNCH_HOLE)
1217 return smb3_punch_hole(file, tcon, off, len);
30175628
SF
1218 else if (mode & FALLOC_FL_ZERO_RANGE) {
1219 if (mode & FALLOC_FL_KEEP_SIZE)
1220 return smb3_zero_range(file, tcon, off, len, true);
1221 return smb3_zero_range(file, tcon, off, len, false);
9ccf3216
SF
1222 } else if (mode == FALLOC_FL_KEEP_SIZE)
1223 return smb3_simple_falloc(file, tcon, off, len, true);
1224 else if (mode == 0)
1225 return smb3_simple_falloc(file, tcon, off, len, false);
31742c5a
SF
1226
1227 return -EOPNOTSUPP;
1228}
1229
c11f1df5
SP
1230static void
1231smb2_downgrade_oplock(struct TCP_Server_Info *server,
1232 struct cifsInodeInfo *cinode, bool set_level2)
1233{
1234 if (set_level2)
1235 server->ops->set_oplock_level(cinode, SMB2_OPLOCK_LEVEL_II,
1236 0, NULL);
1237 else
1238 server->ops->set_oplock_level(cinode, 0, 0, NULL);
1239}
1240
53ef1016 1241static void
42873b0a
PS
1242smb2_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
1243 unsigned int epoch, bool *purge_cache)
53ef1016
PS
1244{
1245 oplock &= 0xFF;
1246 if (oplock == SMB2_OPLOCK_LEVEL_NOCHANGE)
1247 return;
1248 if (oplock == SMB2_OPLOCK_LEVEL_BATCH) {
42873b0a 1249 cinode->oplock = CIFS_CACHE_RHW_FLG;
53ef1016
PS
1250 cifs_dbg(FYI, "Batch Oplock granted on inode %p\n",
1251 &cinode->vfs_inode);
1252 } else if (oplock == SMB2_OPLOCK_LEVEL_EXCLUSIVE) {
42873b0a 1253 cinode->oplock = CIFS_CACHE_RW_FLG;
53ef1016
PS
1254 cifs_dbg(FYI, "Exclusive Oplock granted on inode %p\n",
1255 &cinode->vfs_inode);
1256 } else if (oplock == SMB2_OPLOCK_LEVEL_II) {
1257 cinode->oplock = CIFS_CACHE_READ_FLG;
1258 cifs_dbg(FYI, "Level II Oplock granted on inode %p\n",
1259 &cinode->vfs_inode);
1260 } else
1261 cinode->oplock = 0;
1262}
1263
1264static void
42873b0a
PS
1265smb21_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
1266 unsigned int epoch, bool *purge_cache)
53ef1016
PS
1267{
1268 char message[5] = {0};
1269
1270 oplock &= 0xFF;
1271 if (oplock == SMB2_OPLOCK_LEVEL_NOCHANGE)
1272 return;
1273
1274 cinode->oplock = 0;
1275 if (oplock & SMB2_LEASE_READ_CACHING_HE) {
1276 cinode->oplock |= CIFS_CACHE_READ_FLG;
1277 strcat(message, "R");
1278 }
1279 if (oplock & SMB2_LEASE_HANDLE_CACHING_HE) {
1280 cinode->oplock |= CIFS_CACHE_HANDLE_FLG;
1281 strcat(message, "H");
1282 }
1283 if (oplock & SMB2_LEASE_WRITE_CACHING_HE) {
1284 cinode->oplock |= CIFS_CACHE_WRITE_FLG;
1285 strcat(message, "W");
1286 }
1287 if (!cinode->oplock)
1288 strcat(message, "None");
1289 cifs_dbg(FYI, "%s Lease granted on inode %p\n", message,
1290 &cinode->vfs_inode);
1291}
1292
42873b0a
PS
1293static void
1294smb3_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
1295 unsigned int epoch, bool *purge_cache)
1296{
1297 unsigned int old_oplock = cinode->oplock;
1298
1299 smb21_set_oplock_level(cinode, oplock, epoch, purge_cache);
1300
1301 if (purge_cache) {
1302 *purge_cache = false;
1303 if (old_oplock == CIFS_CACHE_READ_FLG) {
1304 if (cinode->oplock == CIFS_CACHE_READ_FLG &&
1305 (epoch - cinode->epoch > 0))
1306 *purge_cache = true;
1307 else if (cinode->oplock == CIFS_CACHE_RH_FLG &&
1308 (epoch - cinode->epoch > 1))
1309 *purge_cache = true;
1310 else if (cinode->oplock == CIFS_CACHE_RHW_FLG &&
1311 (epoch - cinode->epoch > 1))
1312 *purge_cache = true;
1313 else if (cinode->oplock == 0 &&
1314 (epoch - cinode->epoch > 0))
1315 *purge_cache = true;
1316 } else if (old_oplock == CIFS_CACHE_RH_FLG) {
1317 if (cinode->oplock == CIFS_CACHE_RH_FLG &&
1318 (epoch - cinode->epoch > 0))
1319 *purge_cache = true;
1320 else if (cinode->oplock == CIFS_CACHE_RHW_FLG &&
1321 (epoch - cinode->epoch > 1))
1322 *purge_cache = true;
1323 }
1324 cinode->epoch = epoch;
1325 }
1326}
1327
53ef1016
PS
1328static bool
1329smb2_is_read_op(__u32 oplock)
1330{
1331 return oplock == SMB2_OPLOCK_LEVEL_II;
1332}
1333
1334static bool
1335smb21_is_read_op(__u32 oplock)
1336{
1337 return (oplock & SMB2_LEASE_READ_CACHING_HE) &&
1338 !(oplock & SMB2_LEASE_WRITE_CACHING_HE);
1339}
1340
f047390a
PS
1341static __le32
1342map_oplock_to_lease(u8 oplock)
1343{
1344 if (oplock == SMB2_OPLOCK_LEVEL_EXCLUSIVE)
1345 return SMB2_LEASE_WRITE_CACHING | SMB2_LEASE_READ_CACHING;
1346 else if (oplock == SMB2_OPLOCK_LEVEL_II)
1347 return SMB2_LEASE_READ_CACHING;
1348 else if (oplock == SMB2_OPLOCK_LEVEL_BATCH)
1349 return SMB2_LEASE_HANDLE_CACHING | SMB2_LEASE_READ_CACHING |
1350 SMB2_LEASE_WRITE_CACHING;
1351 return 0;
1352}
1353
a41a28bd
PS
1354static char *
1355smb2_create_lease_buf(u8 *lease_key, u8 oplock)
1356{
1357 struct create_lease *buf;
1358
1359 buf = kzalloc(sizeof(struct create_lease), GFP_KERNEL);
1360 if (!buf)
1361 return NULL;
1362
1363 buf->lcontext.LeaseKeyLow = cpu_to_le64(*((u64 *)lease_key));
1364 buf->lcontext.LeaseKeyHigh = cpu_to_le64(*((u64 *)(lease_key + 8)));
f047390a 1365 buf->lcontext.LeaseState = map_oplock_to_lease(oplock);
a41a28bd
PS
1366
1367 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1368 (struct create_lease, lcontext));
1369 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context));
1370 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1371 (struct create_lease, Name));
1372 buf->ccontext.NameLength = cpu_to_le16(4);
12197a7f 1373 /* SMB2_CREATE_REQUEST_LEASE is "RqLs" */
a41a28bd
PS
1374 buf->Name[0] = 'R';
1375 buf->Name[1] = 'q';
1376 buf->Name[2] = 'L';
1377 buf->Name[3] = 's';
1378 return (char *)buf;
1379}
1380
f047390a
PS
1381static char *
1382smb3_create_lease_buf(u8 *lease_key, u8 oplock)
1383{
1384 struct create_lease_v2 *buf;
1385
1386 buf = kzalloc(sizeof(struct create_lease_v2), GFP_KERNEL);
1387 if (!buf)
1388 return NULL;
1389
1390 buf->lcontext.LeaseKeyLow = cpu_to_le64(*((u64 *)lease_key));
1391 buf->lcontext.LeaseKeyHigh = cpu_to_le64(*((u64 *)(lease_key + 8)));
1392 buf->lcontext.LeaseState = map_oplock_to_lease(oplock);
1393
1394 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1395 (struct create_lease_v2, lcontext));
1396 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context_v2));
1397 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1398 (struct create_lease_v2, Name));
1399 buf->ccontext.NameLength = cpu_to_le16(4);
12197a7f 1400 /* SMB2_CREATE_REQUEST_LEASE is "RqLs" */
f047390a
PS
1401 buf->Name[0] = 'R';
1402 buf->Name[1] = 'q';
1403 buf->Name[2] = 'L';
1404 buf->Name[3] = 's';
1405 return (char *)buf;
1406}
1407
b5c7cde3 1408static __u8
42873b0a 1409smb2_parse_lease_buf(void *buf, unsigned int *epoch)
b5c7cde3
PS
1410{
1411 struct create_lease *lc = (struct create_lease *)buf;
1412
42873b0a 1413 *epoch = 0; /* not used */
b5c7cde3
PS
1414 if (lc->lcontext.LeaseFlags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS)
1415 return SMB2_OPLOCK_LEVEL_NOCHANGE;
1416 return le32_to_cpu(lc->lcontext.LeaseState);
1417}
1418
f047390a 1419static __u8
42873b0a 1420smb3_parse_lease_buf(void *buf, unsigned int *epoch)
f047390a
PS
1421{
1422 struct create_lease_v2 *lc = (struct create_lease_v2 *)buf;
1423
42873b0a 1424 *epoch = le16_to_cpu(lc->lcontext.Epoch);
f047390a
PS
1425 if (lc->lcontext.LeaseFlags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS)
1426 return SMB2_OPLOCK_LEVEL_NOCHANGE;
1427 return le32_to_cpu(lc->lcontext.LeaseState);
1428}
1429
7f6c5008
PS
1430static unsigned int
1431smb2_wp_retry_size(struct inode *inode)
1432{
1433 return min_t(unsigned int, CIFS_SB(inode->i_sb)->wsize,
1434 SMB2_MAX_BUFFER_SIZE);
1435}
1436
52755808
PS
1437static bool
1438smb2_dir_needs_close(struct cifsFileInfo *cfile)
1439{
1440 return !cfile->invalidHandle;
1441}
1442
53ef1016 1443struct smb_version_operations smb20_operations = {
027e8eec 1444 .compare_fids = smb2_compare_fids,
2dc7e1c0 1445 .setup_request = smb2_setup_request,
c95b8eed 1446 .setup_async_request = smb2_setup_async_request,
2dc7e1c0 1447 .check_receive = smb2_check_receive,
28ea5290
PS
1448 .add_credits = smb2_add_credits,
1449 .set_credits = smb2_set_credits,
1450 .get_credits_field = smb2_get_credits_field,
1451 .get_credits = smb2_get_credits,
cb7e9eab 1452 .wait_mtu_credits = cifs_wait_mtu_credits,
2dc7e1c0 1453 .get_next_mid = smb2_get_next_mid,
09a4707e
PS
1454 .read_data_offset = smb2_read_data_offset,
1455 .read_data_length = smb2_read_data_length,
1456 .map_error = map_smb2_to_linux_error,
093b2bda
PS
1457 .find_mid = smb2_find_mid,
1458 .check_message = smb2_check_message,
1459 .dump_detail = smb2_dump_detail,
d60622eb
PS
1460 .clear_stats = smb2_clear_stats,
1461 .print_stats = smb2_print_stats,
983c88a4 1462 .is_oplock_break = smb2_is_valid_oplock_break,
c11f1df5 1463 .downgrade_oplock = smb2_downgrade_oplock,
ec2e4523
PS
1464 .need_neg = smb2_need_neg,
1465 .negotiate = smb2_negotiate,
3a3bab50
PS
1466 .negotiate_wsize = smb2_negotiate_wsize,
1467 .negotiate_rsize = smb2_negotiate_rsize,
5478f9ba
PS
1468 .sess_setup = SMB2_sess_setup,
1469 .logoff = SMB2_logoff,
faaf946a
PS
1470 .tree_connect = SMB2_tcon,
1471 .tree_disconnect = SMB2_tdis,
34f62640 1472 .qfs_tcon = smb2_qfs_tcon,
2503a0db 1473 .is_path_accessible = smb2_is_path_accessible,
9094fad1
PS
1474 .can_echo = smb2_can_echo,
1475 .echo = SMB2_echo,
be4cb9e3
PS
1476 .query_path_info = smb2_query_path_info,
1477 .get_srv_inum = smb2_get_srv_inum,
b7546bc5 1478 .query_file_info = smb2_query_file_info,
c839ff24
PS
1479 .set_path_size = smb2_set_path_size,
1480 .set_file_size = smb2_set_file_size,
1feeaac7 1481 .set_file_info = smb2_set_file_info,
64a5cfa6 1482 .set_compression = smb2_set_compression,
a0e73183
PS
1483 .mkdir = smb2_mkdir,
1484 .mkdir_setinfo = smb2_mkdir_setinfo,
1a500f01 1485 .rmdir = smb2_rmdir,
cbe6f439 1486 .unlink = smb2_unlink,
35143eb5 1487 .rename = smb2_rename_path,
568798cc 1488 .create_hardlink = smb2_create_hardlink,
b42bf888 1489 .query_symlink = smb2_query_symlink,
f0df737e
PS
1490 .open = smb2_open_file,
1491 .set_fid = smb2_set_fid,
1492 .close = smb2_close_file,
7a5cfb19 1493 .flush = smb2_flush_file,
09a4707e 1494 .async_readv = smb2_async_readv,
33319141 1495 .async_writev = smb2_async_writev,
d8e05039 1496 .sync_read = smb2_sync_read,
009d3443 1497 .sync_write = smb2_sync_write,
d324f08d
PS
1498 .query_dir_first = smb2_query_dir_first,
1499 .query_dir_next = smb2_query_dir_next,
1500 .close_dir = smb2_close_dir,
1501 .calc_smb_size = smb2_calc_size,
2e44b288 1502 .is_status_pending = smb2_is_status_pending,
983c88a4 1503 .oplock_response = smb2_oplock_response,
6fc05c25 1504 .queryfs = smb2_queryfs,
f7ba7fe6
PS
1505 .mand_lock = smb2_mand_lock,
1506 .mand_unlock_range = smb2_unlock_range,
b140799a 1507 .push_mand_locks = smb2_push_mandatory_locks,
b8c32dbb
PS
1508 .get_lease_key = smb2_get_lease_key,
1509 .set_lease_key = smb2_set_lease_key,
1510 .new_lease_key = smb2_new_lease_key,
38107d45 1511 .calc_signature = smb2_calc_signature,
53ef1016
PS
1512 .is_read_op = smb2_is_read_op,
1513 .set_oplock_level = smb2_set_oplock_level,
a41a28bd 1514 .create_lease_buf = smb2_create_lease_buf,
b5c7cde3 1515 .parse_lease_buf = smb2_parse_lease_buf,
41c1358e 1516 .clone_range = smb2_clone_range,
7f6c5008 1517 .wp_retry_size = smb2_wp_retry_size,
52755808 1518 .dir_needs_close = smb2_dir_needs_close,
38107d45
SF
1519};
1520
53ef1016
PS
1521struct smb_version_operations smb21_operations = {
1522 .compare_fids = smb2_compare_fids,
1523 .setup_request = smb2_setup_request,
1524 .setup_async_request = smb2_setup_async_request,
1525 .check_receive = smb2_check_receive,
1526 .add_credits = smb2_add_credits,
1527 .set_credits = smb2_set_credits,
1528 .get_credits_field = smb2_get_credits_field,
1529 .get_credits = smb2_get_credits,
cb7e9eab 1530 .wait_mtu_credits = smb2_wait_mtu_credits,
53ef1016
PS
1531 .get_next_mid = smb2_get_next_mid,
1532 .read_data_offset = smb2_read_data_offset,
1533 .read_data_length = smb2_read_data_length,
1534 .map_error = map_smb2_to_linux_error,
1535 .find_mid = smb2_find_mid,
1536 .check_message = smb2_check_message,
1537 .dump_detail = smb2_dump_detail,
1538 .clear_stats = smb2_clear_stats,
1539 .print_stats = smb2_print_stats,
1540 .is_oplock_break = smb2_is_valid_oplock_break,
c11f1df5 1541 .downgrade_oplock = smb2_downgrade_oplock,
53ef1016
PS
1542 .need_neg = smb2_need_neg,
1543 .negotiate = smb2_negotiate,
1544 .negotiate_wsize = smb2_negotiate_wsize,
1545 .negotiate_rsize = smb2_negotiate_rsize,
1546 .sess_setup = SMB2_sess_setup,
1547 .logoff = SMB2_logoff,
1548 .tree_connect = SMB2_tcon,
1549 .tree_disconnect = SMB2_tdis,
34f62640 1550 .qfs_tcon = smb2_qfs_tcon,
53ef1016
PS
1551 .is_path_accessible = smb2_is_path_accessible,
1552 .can_echo = smb2_can_echo,
1553 .echo = SMB2_echo,
1554 .query_path_info = smb2_query_path_info,
1555 .get_srv_inum = smb2_get_srv_inum,
1556 .query_file_info = smb2_query_file_info,
1557 .set_path_size = smb2_set_path_size,
1558 .set_file_size = smb2_set_file_size,
1559 .set_file_info = smb2_set_file_info,
64a5cfa6 1560 .set_compression = smb2_set_compression,
53ef1016
PS
1561 .mkdir = smb2_mkdir,
1562 .mkdir_setinfo = smb2_mkdir_setinfo,
1563 .rmdir = smb2_rmdir,
1564 .unlink = smb2_unlink,
1565 .rename = smb2_rename_path,
1566 .create_hardlink = smb2_create_hardlink,
1567 .query_symlink = smb2_query_symlink,
c22870ea 1568 .query_mf_symlink = smb3_query_mf_symlink,
5ab97578 1569 .create_mf_symlink = smb3_create_mf_symlink,
53ef1016
PS
1570 .open = smb2_open_file,
1571 .set_fid = smb2_set_fid,
1572 .close = smb2_close_file,
1573 .flush = smb2_flush_file,
1574 .async_readv = smb2_async_readv,
1575 .async_writev = smb2_async_writev,
1576 .sync_read = smb2_sync_read,
1577 .sync_write = smb2_sync_write,
1578 .query_dir_first = smb2_query_dir_first,
1579 .query_dir_next = smb2_query_dir_next,
1580 .close_dir = smb2_close_dir,
1581 .calc_smb_size = smb2_calc_size,
1582 .is_status_pending = smb2_is_status_pending,
1583 .oplock_response = smb2_oplock_response,
1584 .queryfs = smb2_queryfs,
1585 .mand_lock = smb2_mand_lock,
1586 .mand_unlock_range = smb2_unlock_range,
1587 .push_mand_locks = smb2_push_mandatory_locks,
1588 .get_lease_key = smb2_get_lease_key,
1589 .set_lease_key = smb2_set_lease_key,
1590 .new_lease_key = smb2_new_lease_key,
1591 .calc_signature = smb2_calc_signature,
1592 .is_read_op = smb21_is_read_op,
1593 .set_oplock_level = smb21_set_oplock_level,
a41a28bd 1594 .create_lease_buf = smb2_create_lease_buf,
b5c7cde3 1595 .parse_lease_buf = smb2_parse_lease_buf,
41c1358e 1596 .clone_range = smb2_clone_range,
7f6c5008 1597 .wp_retry_size = smb2_wp_retry_size,
52755808 1598 .dir_needs_close = smb2_dir_needs_close,
53ef1016 1599};
38107d45
SF
1600
1601struct smb_version_operations smb30_operations = {
1602 .compare_fids = smb2_compare_fids,
1603 .setup_request = smb2_setup_request,
1604 .setup_async_request = smb2_setup_async_request,
1605 .check_receive = smb2_check_receive,
1606 .add_credits = smb2_add_credits,
1607 .set_credits = smb2_set_credits,
1608 .get_credits_field = smb2_get_credits_field,
1609 .get_credits = smb2_get_credits,
cb7e9eab 1610 .wait_mtu_credits = smb2_wait_mtu_credits,
38107d45
SF
1611 .get_next_mid = smb2_get_next_mid,
1612 .read_data_offset = smb2_read_data_offset,
1613 .read_data_length = smb2_read_data_length,
1614 .map_error = map_smb2_to_linux_error,
1615 .find_mid = smb2_find_mid,
1616 .check_message = smb2_check_message,
1617 .dump_detail = smb2_dump_detail,
1618 .clear_stats = smb2_clear_stats,
1619 .print_stats = smb2_print_stats,
769ee6a4 1620 .dump_share_caps = smb2_dump_share_caps,
38107d45 1621 .is_oplock_break = smb2_is_valid_oplock_break,
c11f1df5 1622 .downgrade_oplock = smb2_downgrade_oplock,
38107d45
SF
1623 .need_neg = smb2_need_neg,
1624 .negotiate = smb2_negotiate,
1625 .negotiate_wsize = smb2_negotiate_wsize,
1626 .negotiate_rsize = smb2_negotiate_rsize,
1627 .sess_setup = SMB2_sess_setup,
1628 .logoff = SMB2_logoff,
1629 .tree_connect = SMB2_tcon,
1630 .tree_disconnect = SMB2_tdis,
af6a12ea 1631 .qfs_tcon = smb3_qfs_tcon,
38107d45
SF
1632 .is_path_accessible = smb2_is_path_accessible,
1633 .can_echo = smb2_can_echo,
1634 .echo = SMB2_echo,
1635 .query_path_info = smb2_query_path_info,
1636 .get_srv_inum = smb2_get_srv_inum,
1637 .query_file_info = smb2_query_file_info,
1638 .set_path_size = smb2_set_path_size,
1639 .set_file_size = smb2_set_file_size,
1640 .set_file_info = smb2_set_file_info,
64a5cfa6 1641 .set_compression = smb2_set_compression,
38107d45
SF
1642 .mkdir = smb2_mkdir,
1643 .mkdir_setinfo = smb2_mkdir_setinfo,
1644 .rmdir = smb2_rmdir,
1645 .unlink = smb2_unlink,
1646 .rename = smb2_rename_path,
1647 .create_hardlink = smb2_create_hardlink,
b42bf888 1648 .query_symlink = smb2_query_symlink,
c22870ea 1649 .query_mf_symlink = smb3_query_mf_symlink,
5ab97578 1650 .create_mf_symlink = smb3_create_mf_symlink,
38107d45
SF
1651 .open = smb2_open_file,
1652 .set_fid = smb2_set_fid,
1653 .close = smb2_close_file,
1654 .flush = smb2_flush_file,
1655 .async_readv = smb2_async_readv,
1656 .async_writev = smb2_async_writev,
1657 .sync_read = smb2_sync_read,
1658 .sync_write = smb2_sync_write,
1659 .query_dir_first = smb2_query_dir_first,
1660 .query_dir_next = smb2_query_dir_next,
1661 .close_dir = smb2_close_dir,
1662 .calc_smb_size = smb2_calc_size,
1663 .is_status_pending = smb2_is_status_pending,
1664 .oplock_response = smb2_oplock_response,
1665 .queryfs = smb2_queryfs,
1666 .mand_lock = smb2_mand_lock,
1667 .mand_unlock_range = smb2_unlock_range,
1668 .push_mand_locks = smb2_push_mandatory_locks,
1669 .get_lease_key = smb2_get_lease_key,
1670 .set_lease_key = smb2_set_lease_key,
1671 .new_lease_key = smb2_new_lease_key,
e65a5cb4 1672 .generate_signingkey = generate_smb3signingkey,
38107d45 1673 .calc_signature = smb3_calc_signature,
53ef1016 1674 .is_read_op = smb21_is_read_op,
42873b0a 1675 .set_oplock_level = smb3_set_oplock_level,
f047390a
PS
1676 .create_lease_buf = smb3_create_lease_buf,
1677 .parse_lease_buf = smb3_parse_lease_buf,
41c1358e 1678 .clone_range = smb2_clone_range,
ff1c038a 1679 .validate_negotiate = smb3_validate_negotiate,
7f6c5008 1680 .wp_retry_size = smb2_wp_retry_size,
52755808 1681 .dir_needs_close = smb2_dir_needs_close,
31742c5a 1682 .fallocate = smb3_fallocate,
1080ef75
SF
1683};
1684
aab1893d
SF
1685#ifdef CONFIG_CIFS_SMB311
1686struct smb_version_operations smb311_operations = {
1687 .compare_fids = smb2_compare_fids,
1688 .setup_request = smb2_setup_request,
1689 .setup_async_request = smb2_setup_async_request,
1690 .check_receive = smb2_check_receive,
1691 .add_credits = smb2_add_credits,
1692 .set_credits = smb2_set_credits,
1693 .get_credits_field = smb2_get_credits_field,
1694 .get_credits = smb2_get_credits,
1695 .wait_mtu_credits = smb2_wait_mtu_credits,
1696 .get_next_mid = smb2_get_next_mid,
1697 .read_data_offset = smb2_read_data_offset,
1698 .read_data_length = smb2_read_data_length,
1699 .map_error = map_smb2_to_linux_error,
1700 .find_mid = smb2_find_mid,
1701 .check_message = smb2_check_message,
1702 .dump_detail = smb2_dump_detail,
1703 .clear_stats = smb2_clear_stats,
1704 .print_stats = smb2_print_stats,
1705 .dump_share_caps = smb2_dump_share_caps,
1706 .is_oplock_break = smb2_is_valid_oplock_break,
1707 .downgrade_oplock = smb2_downgrade_oplock,
1708 .need_neg = smb2_need_neg,
1709 .negotiate = smb2_negotiate,
1710 .negotiate_wsize = smb2_negotiate_wsize,
1711 .negotiate_rsize = smb2_negotiate_rsize,
1712 .sess_setup = SMB2_sess_setup,
1713 .logoff = SMB2_logoff,
1714 .tree_connect = SMB2_tcon,
1715 .tree_disconnect = SMB2_tdis,
1716 .qfs_tcon = smb3_qfs_tcon,
1717 .is_path_accessible = smb2_is_path_accessible,
1718 .can_echo = smb2_can_echo,
1719 .echo = SMB2_echo,
1720 .query_path_info = smb2_query_path_info,
1721 .get_srv_inum = smb2_get_srv_inum,
1722 .query_file_info = smb2_query_file_info,
1723 .set_path_size = smb2_set_path_size,
1724 .set_file_size = smb2_set_file_size,
1725 .set_file_info = smb2_set_file_info,
1726 .set_compression = smb2_set_compression,
1727 .mkdir = smb2_mkdir,
1728 .mkdir_setinfo = smb2_mkdir_setinfo,
1729 .rmdir = smb2_rmdir,
1730 .unlink = smb2_unlink,
1731 .rename = smb2_rename_path,
1732 .create_hardlink = smb2_create_hardlink,
1733 .query_symlink = smb2_query_symlink,
1734 .query_mf_symlink = smb3_query_mf_symlink,
1735 .create_mf_symlink = smb3_create_mf_symlink,
1736 .open = smb2_open_file,
1737 .set_fid = smb2_set_fid,
1738 .close = smb2_close_file,
1739 .flush = smb2_flush_file,
1740 .async_readv = smb2_async_readv,
1741 .async_writev = smb2_async_writev,
1742 .sync_read = smb2_sync_read,
1743 .sync_write = smb2_sync_write,
1744 .query_dir_first = smb2_query_dir_first,
1745 .query_dir_next = smb2_query_dir_next,
1746 .close_dir = smb2_close_dir,
1747 .calc_smb_size = smb2_calc_size,
1748 .is_status_pending = smb2_is_status_pending,
1749 .oplock_response = smb2_oplock_response,
1750 .queryfs = smb2_queryfs,
1751 .mand_lock = smb2_mand_lock,
1752 .mand_unlock_range = smb2_unlock_range,
1753 .push_mand_locks = smb2_push_mandatory_locks,
1754 .get_lease_key = smb2_get_lease_key,
1755 .set_lease_key = smb2_set_lease_key,
1756 .new_lease_key = smb2_new_lease_key,
1757 .generate_signingkey = generate_smb3signingkey,
1758 .calc_signature = smb3_calc_signature,
1759 .is_read_op = smb21_is_read_op,
1760 .set_oplock_level = smb3_set_oplock_level,
1761 .create_lease_buf = smb3_create_lease_buf,
1762 .parse_lease_buf = smb3_parse_lease_buf,
1763 .clone_range = smb2_clone_range,
02b16665 1764 .duplicate_extents = smb2_duplicate_extents,
aab1893d
SF
1765/* .validate_negotiate = smb3_validate_negotiate, */ /* not used in 3.11 */
1766 .wp_retry_size = smb2_wp_retry_size,
1767 .dir_needs_close = smb2_dir_needs_close,
1768 .fallocate = smb3_fallocate,
1769};
1770#endif /* CIFS_SMB311 */
1771
dd446b16
SF
1772struct smb_version_values smb20_values = {
1773 .version_string = SMB20_VERSION_STRING,
1774 .protocol_id = SMB20_PROT_ID,
1775 .req_capabilities = 0, /* MBZ */
1776 .large_lock_type = 0,
1777 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
1778 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
1779 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
1780 .header_size = sizeof(struct smb2_hdr),
1781 .max_header_size = MAX_SMB2_HDR_SIZE,
1782 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
1783 .lock_cmd = SMB2_LOCK,
1784 .cap_unix = 0,
1785 .cap_nt_find = SMB2_NT_FIND,
1786 .cap_large_files = SMB2_LARGE_FILES,
50285882
JL
1787 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
1788 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
a41a28bd 1789 .create_lease_size = sizeof(struct create_lease),
dd446b16
SF
1790};
1791
1080ef75
SF
1792struct smb_version_values smb21_values = {
1793 .version_string = SMB21_VERSION_STRING,
e4aa25e7
SF
1794 .protocol_id = SMB21_PROT_ID,
1795 .req_capabilities = 0, /* MBZ on negotiate req until SMB3 dialect */
1796 .large_lock_type = 0,
1797 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
1798 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
1799 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
1800 .header_size = sizeof(struct smb2_hdr),
1801 .max_header_size = MAX_SMB2_HDR_SIZE,
1802 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
1803 .lock_cmd = SMB2_LOCK,
1804 .cap_unix = 0,
1805 .cap_nt_find = SMB2_NT_FIND,
1806 .cap_large_files = SMB2_LARGE_FILES,
50285882
JL
1807 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
1808 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
a41a28bd 1809 .create_lease_size = sizeof(struct create_lease),
e4aa25e7
SF
1810};
1811
1812struct smb_version_values smb30_values = {
1813 .version_string = SMB30_VERSION_STRING,
1814 .protocol_id = SMB30_PROT_ID,
1815 .req_capabilities = SMB2_GLOBAL_CAP_DFS | SMB2_GLOBAL_CAP_LEASING | SMB2_GLOBAL_CAP_LARGE_MTU,
027e8eec
PS
1816 .large_lock_type = 0,
1817 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
1818 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
1819 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
093b2bda
PS
1820 .header_size = sizeof(struct smb2_hdr),
1821 .max_header_size = MAX_SMB2_HDR_SIZE,
09a4707e 1822 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
2dc7e1c0 1823 .lock_cmd = SMB2_LOCK,
29e20f9c
PS
1824 .cap_unix = 0,
1825 .cap_nt_find = SMB2_NT_FIND,
1826 .cap_large_files = SMB2_LARGE_FILES,
50285882
JL
1827 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
1828 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
f047390a 1829 .create_lease_size = sizeof(struct create_lease_v2),
1080ef75 1830};
20b6d8b4
SF
1831
1832struct smb_version_values smb302_values = {
1833 .version_string = SMB302_VERSION_STRING,
1834 .protocol_id = SMB302_PROT_ID,
1835 .req_capabilities = SMB2_GLOBAL_CAP_DFS | SMB2_GLOBAL_CAP_LEASING | SMB2_GLOBAL_CAP_LARGE_MTU,
1836 .large_lock_type = 0,
1837 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
1838 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
1839 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
1840 .header_size = sizeof(struct smb2_hdr),
1841 .max_header_size = MAX_SMB2_HDR_SIZE,
1842 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
1843 .lock_cmd = SMB2_LOCK,
1844 .cap_unix = 0,
1845 .cap_nt_find = SMB2_NT_FIND,
1846 .cap_large_files = SMB2_LARGE_FILES,
50285882
JL
1847 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
1848 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
f047390a 1849 .create_lease_size = sizeof(struct create_lease_v2),
20b6d8b4 1850};
5f7fbf73
SF
1851
1852#ifdef CONFIG_CIFS_SMB311
1853struct smb_version_values smb311_values = {
1854 .version_string = SMB311_VERSION_STRING,
1855 .protocol_id = SMB311_PROT_ID,
1856 .req_capabilities = SMB2_GLOBAL_CAP_DFS | SMB2_GLOBAL_CAP_LEASING | SMB2_GLOBAL_CAP_LARGE_MTU,
1857 .large_lock_type = 0,
1858 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
1859 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
1860 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
1861 .header_size = sizeof(struct smb2_hdr),
1862 .max_header_size = MAX_SMB2_HDR_SIZE,
1863 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
1864 .lock_cmd = SMB2_LOCK,
1865 .cap_unix = 0,
1866 .cap_nt_find = SMB2_NT_FIND,
1867 .cap_large_files = SMB2_LARGE_FILES,
1868 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
1869 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
1870 .create_lease_size = sizeof(struct create_lease_v2),
1871};
1872#endif /* SMB311 */