HID: hiddev: Fix slab-out-of-bounds write in hiddev_ioctl_usage()
[linux/fpc-iii.git] / fs / cifs / smb2ops.c
blob087261ca6d4639358f9fb9c791bd1e50f9c551c6
1 /*
2 * SMB2 version specific operations
4 * Copyright (c) 2012, Jeff Layton <jlayton@redhat.com>
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.
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.
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
20 #include <linux/pagemap.h>
21 #include <linux/vfs.h>
22 #include <linux/falloc.h>
23 #include "cifsglob.h"
24 #include "smb2pdu.h"
25 #include "smb2proto.h"
26 #include "cifsproto.h"
27 #include "cifs_debug.h"
28 #include "cifs_unicode.h"
29 #include "smb2status.h"
30 #include "smb2glob.h"
32 static int
33 change_conf(struct TCP_Server_Info *server)
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;
43 cifs_dbg(VFS, "disabling echoes and oplocks\n");
44 break;
45 case 2:
46 server->echoes = true;
47 server->oplocks = false;
48 server->echo_credits = 1;
49 cifs_dbg(FYI, "disabling oplocks\n");
50 break;
51 default:
52 server->echoes = true;
53 if (enable_oplocks) {
54 server->oplocks = true;
55 server->oplock_credits = 1;
56 } else
57 server->oplocks = false;
59 server->echo_credits = 1;
61 server->credits -= server->echo_credits + server->oplock_credits;
62 return 0;
65 static void
66 smb2_add_credits(struct TCP_Server_Info *server, const unsigned int add,
67 const int optype)
69 int *val, rc = 0;
70 spin_lock(&server->req_lock);
71 val = server->ops->get_credits_field(server, optype);
72 *val += add;
73 server->in_flight--;
74 if (server->in_flight == 0 && (optype & CIFS_OP_MASK) != CIFS_NEG_OP)
75 rc = change_conf(server);
77 * Sometimes server returns 0 credits on oplock break ack - we need to
78 * rebalance credits in this case.
80 else if (server->in_flight > 0 && server->oplock_credits == 0 &&
81 server->oplocks) {
82 if (server->credits > 1) {
83 server->credits--;
84 server->oplock_credits++;
87 spin_unlock(&server->req_lock);
88 wake_up(&server->request_q);
89 if (rc)
90 cifs_reconnect(server);
93 static void
94 smb2_set_credits(struct TCP_Server_Info *server, const int val)
96 spin_lock(&server->req_lock);
97 server->credits = val;
98 spin_unlock(&server->req_lock);
101 static int *
102 smb2_get_credits_field(struct TCP_Server_Info *server, const int optype)
104 switch (optype) {
105 case CIFS_ECHO_OP:
106 return &server->echo_credits;
107 case CIFS_OBREAK_OP:
108 return &server->oplock_credits;
109 default:
110 return &server->credits;
114 static unsigned int
115 smb2_get_credits(struct mid_q_entry *mid)
117 return le16_to_cpu(((struct smb2_hdr *)mid->resp_buf)->CreditRequest);
120 static int
121 smb2_wait_mtu_credits(struct TCP_Server_Info *server, unsigned int size,
122 unsigned int *num, unsigned int *credits)
124 int rc = 0;
125 unsigned int scredits;
127 spin_lock(&server->req_lock);
128 while (1) {
129 if (server->credits <= 0) {
130 spin_unlock(&server->req_lock);
131 cifs_num_waiters_inc(server);
132 rc = wait_event_killable(server->request_q,
133 has_credits(server, &server->credits));
134 cifs_num_waiters_dec(server);
135 if (rc)
136 return rc;
137 spin_lock(&server->req_lock);
138 } else {
139 if (server->tcpStatus == CifsExiting) {
140 spin_unlock(&server->req_lock);
141 return -ENOENT;
144 scredits = server->credits;
145 /* can deadlock with reopen */
146 if (scredits <= 8) {
147 *num = SMB2_MAX_BUFFER_SIZE;
148 *credits = 0;
149 break;
152 /* leave some credits for reopen and other ops */
153 scredits -= 8;
154 *num = min_t(unsigned int, size,
155 scredits * SMB2_MAX_BUFFER_SIZE);
157 *credits = DIV_ROUND_UP(*num, SMB2_MAX_BUFFER_SIZE);
158 server->credits -= *credits;
159 server->in_flight++;
160 break;
163 spin_unlock(&server->req_lock);
164 return rc;
167 static __u64
168 smb2_get_next_mid(struct TCP_Server_Info *server)
170 __u64 mid;
171 /* for SMB2 we need the current value */
172 spin_lock(&GlobalMid_Lock);
173 mid = server->CurrentMid++;
174 spin_unlock(&GlobalMid_Lock);
175 return mid;
178 static struct mid_q_entry *
179 smb2_find_mid(struct TCP_Server_Info *server, char *buf)
181 struct mid_q_entry *mid;
182 struct smb2_hdr *hdr = (struct smb2_hdr *)buf;
183 __u64 wire_mid = le64_to_cpu(hdr->MessageId);
185 spin_lock(&GlobalMid_Lock);
186 list_for_each_entry(mid, &server->pending_mid_q, qhead) {
187 if ((mid->mid == wire_mid) &&
188 (mid->mid_state == MID_REQUEST_SUBMITTED) &&
189 (mid->command == hdr->Command)) {
190 spin_unlock(&GlobalMid_Lock);
191 return mid;
194 spin_unlock(&GlobalMid_Lock);
195 return NULL;
198 static void
199 smb2_dump_detail(void *buf)
201 #ifdef CONFIG_CIFS_DEBUG2
202 struct smb2_hdr *smb = (struct smb2_hdr *)buf;
204 cifs_dbg(VFS, "Cmd: %d Err: 0x%x Flags: 0x%x Mid: %llu Pid: %d\n",
205 smb->Command, smb->Status, smb->Flags, smb->MessageId,
206 smb->ProcessId);
207 cifs_dbg(VFS, "smb buf %p len %u\n", smb, smb2_calc_size(smb));
208 #endif
211 static bool
212 smb2_need_neg(struct TCP_Server_Info *server)
214 return server->max_read == 0;
217 static int
218 smb2_negotiate(const unsigned int xid, struct cifs_ses *ses)
220 int rc;
221 ses->server->CurrentMid = 0;
222 rc = SMB2_negotiate(xid, ses);
223 /* BB we probably don't need to retry with modern servers */
224 if (rc == -EAGAIN)
225 rc = -EHOSTDOWN;
226 return rc;
229 static unsigned int
230 smb2_negotiate_wsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
232 struct TCP_Server_Info *server = tcon->ses->server;
233 unsigned int wsize;
235 /* start with specified wsize, or default */
236 wsize = volume_info->wsize ? volume_info->wsize : CIFS_DEFAULT_IOSIZE;
237 wsize = min_t(unsigned int, wsize, server->max_write);
239 if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
240 wsize = min_t(unsigned int, wsize, SMB2_MAX_BUFFER_SIZE);
242 return wsize;
245 static unsigned int
246 smb2_negotiate_rsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
248 struct TCP_Server_Info *server = tcon->ses->server;
249 unsigned int rsize;
251 /* start with specified rsize, or default */
252 rsize = volume_info->rsize ? volume_info->rsize : CIFS_DEFAULT_IOSIZE;
253 rsize = min_t(unsigned int, rsize, server->max_read);
255 if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
256 rsize = min_t(unsigned int, rsize, SMB2_MAX_BUFFER_SIZE);
258 return rsize;
261 #ifdef CONFIG_CIFS_STATS2
262 static int
263 SMB3_request_interfaces(const unsigned int xid, struct cifs_tcon *tcon)
265 int rc;
266 unsigned int ret_data_len = 0;
267 struct network_interface_info_ioctl_rsp *out_buf;
269 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
270 FSCTL_QUERY_NETWORK_INTERFACE_INFO, true /* is_fsctl */,
271 NULL /* no data input */, 0 /* no data input */,
272 (char **)&out_buf, &ret_data_len);
273 if (rc != 0)
274 cifs_dbg(VFS, "error %d on ioctl to get interface list\n", rc);
275 else if (ret_data_len < sizeof(struct network_interface_info_ioctl_rsp)) {
276 cifs_dbg(VFS, "server returned bad net interface info buf\n");
277 rc = -EINVAL;
278 } else {
279 /* Dump info on first interface */
280 cifs_dbg(FYI, "Adapter Capability 0x%x\t",
281 le32_to_cpu(out_buf->Capability));
282 cifs_dbg(FYI, "Link Speed %lld\n",
283 le64_to_cpu(out_buf->LinkSpeed));
285 kfree(out_buf);
286 return rc;
288 #endif /* STATS2 */
290 static void
291 smb3_qfs_tcon(const unsigned int xid, struct cifs_tcon *tcon)
293 int rc;
294 __le16 srch_path = 0; /* Null - open root of share */
295 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
296 struct cifs_open_parms oparms;
297 struct cifs_fid fid;
299 oparms.tcon = tcon;
300 oparms.desired_access = FILE_READ_ATTRIBUTES;
301 oparms.disposition = FILE_OPEN;
302 oparms.create_options = 0;
303 oparms.fid = &fid;
304 oparms.reconnect = false;
306 rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL);
307 if (rc)
308 return;
310 #ifdef CONFIG_CIFS_STATS2
311 SMB3_request_interfaces(xid, tcon);
312 #endif /* STATS2 */
314 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
315 FS_ATTRIBUTE_INFORMATION);
316 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
317 FS_DEVICE_INFORMATION);
318 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
319 FS_SECTOR_SIZE_INFORMATION); /* SMB3 specific */
320 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
321 return;
324 static void
325 smb2_qfs_tcon(const unsigned int xid, struct cifs_tcon *tcon)
327 int rc;
328 __le16 srch_path = 0; /* Null - open root of share */
329 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
330 struct cifs_open_parms oparms;
331 struct cifs_fid fid;
333 oparms.tcon = tcon;
334 oparms.desired_access = FILE_READ_ATTRIBUTES;
335 oparms.disposition = FILE_OPEN;
336 oparms.create_options = 0;
337 oparms.fid = &fid;
338 oparms.reconnect = false;
340 rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL);
341 if (rc)
342 return;
344 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
345 FS_ATTRIBUTE_INFORMATION);
346 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
347 FS_DEVICE_INFORMATION);
348 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
349 return;
352 static int
353 smb2_is_path_accessible(const unsigned int xid, struct cifs_tcon *tcon,
354 struct cifs_sb_info *cifs_sb, const char *full_path)
356 int rc;
357 __le16 *utf16_path;
358 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
359 struct cifs_open_parms oparms;
360 struct cifs_fid fid;
362 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
363 if (!utf16_path)
364 return -ENOMEM;
366 oparms.tcon = tcon;
367 oparms.desired_access = FILE_READ_ATTRIBUTES;
368 oparms.disposition = FILE_OPEN;
369 oparms.create_options = 0;
370 oparms.fid = &fid;
371 oparms.reconnect = false;
373 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL);
374 if (rc) {
375 kfree(utf16_path);
376 return rc;
379 rc = SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
380 kfree(utf16_path);
381 return rc;
384 static int
385 smb2_get_srv_inum(const unsigned int xid, struct cifs_tcon *tcon,
386 struct cifs_sb_info *cifs_sb, const char *full_path,
387 u64 *uniqueid, FILE_ALL_INFO *data)
389 *uniqueid = le64_to_cpu(data->IndexNumber);
390 return 0;
393 static int
394 smb2_query_file_info(const unsigned int xid, struct cifs_tcon *tcon,
395 struct cifs_fid *fid, FILE_ALL_INFO *data)
397 int rc;
398 struct smb2_file_all_info *smb2_data;
400 smb2_data = kzalloc(sizeof(struct smb2_file_all_info) + PATH_MAX * 2,
401 GFP_KERNEL);
402 if (smb2_data == NULL)
403 return -ENOMEM;
405 rc = SMB2_query_info(xid, tcon, fid->persistent_fid, fid->volatile_fid,
406 smb2_data);
407 if (!rc)
408 move_smb2_info_to_cifs(data, smb2_data);
409 kfree(smb2_data);
410 return rc;
413 static bool
414 smb2_can_echo(struct TCP_Server_Info *server)
416 return server->echoes;
419 static void
420 smb2_clear_stats(struct cifs_tcon *tcon)
422 #ifdef CONFIG_CIFS_STATS
423 int i;
424 for (i = 0; i < NUMBER_OF_SMB2_COMMANDS; i++) {
425 atomic_set(&tcon->stats.smb2_stats.smb2_com_sent[i], 0);
426 atomic_set(&tcon->stats.smb2_stats.smb2_com_failed[i], 0);
428 #endif
431 static void
432 smb2_dump_share_caps(struct seq_file *m, struct cifs_tcon *tcon)
434 seq_puts(m, "\n\tShare Capabilities:");
435 if (tcon->capabilities & SMB2_SHARE_CAP_DFS)
436 seq_puts(m, " DFS,");
437 if (tcon->capabilities & SMB2_SHARE_CAP_CONTINUOUS_AVAILABILITY)
438 seq_puts(m, " CONTINUOUS AVAILABILITY,");
439 if (tcon->capabilities & SMB2_SHARE_CAP_SCALEOUT)
440 seq_puts(m, " SCALEOUT,");
441 if (tcon->capabilities & SMB2_SHARE_CAP_CLUSTER)
442 seq_puts(m, " CLUSTER,");
443 if (tcon->capabilities & SMB2_SHARE_CAP_ASYMMETRIC)
444 seq_puts(m, " ASYMMETRIC,");
445 if (tcon->capabilities == 0)
446 seq_puts(m, " None");
447 if (tcon->ss_flags & SSINFO_FLAGS_ALIGNED_DEVICE)
448 seq_puts(m, " Aligned,");
449 if (tcon->ss_flags & SSINFO_FLAGS_PARTITION_ALIGNED_ON_DEVICE)
450 seq_puts(m, " Partition Aligned,");
451 if (tcon->ss_flags & SSINFO_FLAGS_NO_SEEK_PENALTY)
452 seq_puts(m, " SSD,");
453 if (tcon->ss_flags & SSINFO_FLAGS_TRIM_ENABLED)
454 seq_puts(m, " TRIM-support,");
456 seq_printf(m, "\tShare Flags: 0x%x", tcon->share_flags);
457 if (tcon->perf_sector_size)
458 seq_printf(m, "\tOptimal sector size: 0x%x",
459 tcon->perf_sector_size);
462 static void
463 smb2_print_stats(struct seq_file *m, struct cifs_tcon *tcon)
465 #ifdef CONFIG_CIFS_STATS
466 atomic_t *sent = tcon->stats.smb2_stats.smb2_com_sent;
467 atomic_t *failed = tcon->stats.smb2_stats.smb2_com_failed;
468 seq_printf(m, "\nNegotiates: %d sent %d failed",
469 atomic_read(&sent[SMB2_NEGOTIATE_HE]),
470 atomic_read(&failed[SMB2_NEGOTIATE_HE]));
471 seq_printf(m, "\nSessionSetups: %d sent %d failed",
472 atomic_read(&sent[SMB2_SESSION_SETUP_HE]),
473 atomic_read(&failed[SMB2_SESSION_SETUP_HE]));
474 seq_printf(m, "\nLogoffs: %d sent %d failed",
475 atomic_read(&sent[SMB2_LOGOFF_HE]),
476 atomic_read(&failed[SMB2_LOGOFF_HE]));
477 seq_printf(m, "\nTreeConnects: %d sent %d failed",
478 atomic_read(&sent[SMB2_TREE_CONNECT_HE]),
479 atomic_read(&failed[SMB2_TREE_CONNECT_HE]));
480 seq_printf(m, "\nTreeDisconnects: %d sent %d failed",
481 atomic_read(&sent[SMB2_TREE_DISCONNECT_HE]),
482 atomic_read(&failed[SMB2_TREE_DISCONNECT_HE]));
483 seq_printf(m, "\nCreates: %d sent %d failed",
484 atomic_read(&sent[SMB2_CREATE_HE]),
485 atomic_read(&failed[SMB2_CREATE_HE]));
486 seq_printf(m, "\nCloses: %d sent %d failed",
487 atomic_read(&sent[SMB2_CLOSE_HE]),
488 atomic_read(&failed[SMB2_CLOSE_HE]));
489 seq_printf(m, "\nFlushes: %d sent %d failed",
490 atomic_read(&sent[SMB2_FLUSH_HE]),
491 atomic_read(&failed[SMB2_FLUSH_HE]));
492 seq_printf(m, "\nReads: %d sent %d failed",
493 atomic_read(&sent[SMB2_READ_HE]),
494 atomic_read(&failed[SMB2_READ_HE]));
495 seq_printf(m, "\nWrites: %d sent %d failed",
496 atomic_read(&sent[SMB2_WRITE_HE]),
497 atomic_read(&failed[SMB2_WRITE_HE]));
498 seq_printf(m, "\nLocks: %d sent %d failed",
499 atomic_read(&sent[SMB2_LOCK_HE]),
500 atomic_read(&failed[SMB2_LOCK_HE]));
501 seq_printf(m, "\nIOCTLs: %d sent %d failed",
502 atomic_read(&sent[SMB2_IOCTL_HE]),
503 atomic_read(&failed[SMB2_IOCTL_HE]));
504 seq_printf(m, "\nCancels: %d sent %d failed",
505 atomic_read(&sent[SMB2_CANCEL_HE]),
506 atomic_read(&failed[SMB2_CANCEL_HE]));
507 seq_printf(m, "\nEchos: %d sent %d failed",
508 atomic_read(&sent[SMB2_ECHO_HE]),
509 atomic_read(&failed[SMB2_ECHO_HE]));
510 seq_printf(m, "\nQueryDirectories: %d sent %d failed",
511 atomic_read(&sent[SMB2_QUERY_DIRECTORY_HE]),
512 atomic_read(&failed[SMB2_QUERY_DIRECTORY_HE]));
513 seq_printf(m, "\nChangeNotifies: %d sent %d failed",
514 atomic_read(&sent[SMB2_CHANGE_NOTIFY_HE]),
515 atomic_read(&failed[SMB2_CHANGE_NOTIFY_HE]));
516 seq_printf(m, "\nQueryInfos: %d sent %d failed",
517 atomic_read(&sent[SMB2_QUERY_INFO_HE]),
518 atomic_read(&failed[SMB2_QUERY_INFO_HE]));
519 seq_printf(m, "\nSetInfos: %d sent %d failed",
520 atomic_read(&sent[SMB2_SET_INFO_HE]),
521 atomic_read(&failed[SMB2_SET_INFO_HE]));
522 seq_printf(m, "\nOplockBreaks: %d sent %d failed",
523 atomic_read(&sent[SMB2_OPLOCK_BREAK_HE]),
524 atomic_read(&failed[SMB2_OPLOCK_BREAK_HE]));
525 #endif
528 static void
529 smb2_set_fid(struct cifsFileInfo *cfile, struct cifs_fid *fid, __u32 oplock)
531 struct cifsInodeInfo *cinode = CIFS_I(d_inode(cfile->dentry));
532 struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server;
534 cfile->fid.persistent_fid = fid->persistent_fid;
535 cfile->fid.volatile_fid = fid->volatile_fid;
536 server->ops->set_oplock_level(cinode, oplock, fid->epoch,
537 &fid->purge_cache);
538 cinode->can_cache_brlcks = CIFS_CACHE_WRITE(cinode);
539 memcpy(cfile->fid.create_guid, fid->create_guid, 16);
542 static void
543 smb2_close_file(const unsigned int xid, struct cifs_tcon *tcon,
544 struct cifs_fid *fid)
546 SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
549 static int
550 SMB2_request_res_key(const unsigned int xid, struct cifs_tcon *tcon,
551 u64 persistent_fid, u64 volatile_fid,
552 struct copychunk_ioctl *pcchunk)
554 int rc;
555 unsigned int ret_data_len;
556 struct resume_key_req *res_key;
558 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
559 FSCTL_SRV_REQUEST_RESUME_KEY, true /* is_fsctl */,
560 NULL, 0 /* no input */,
561 (char **)&res_key, &ret_data_len);
563 if (rc) {
564 cifs_dbg(VFS, "refcpy ioctl error %d getting resume key\n", rc);
565 goto req_res_key_exit;
567 if (ret_data_len < sizeof(struct resume_key_req)) {
568 cifs_dbg(VFS, "Invalid refcopy resume key length\n");
569 rc = -EINVAL;
570 goto req_res_key_exit;
572 memcpy(pcchunk->SourceKey, res_key->ResumeKey, COPY_CHUNK_RES_KEY_SIZE);
574 req_res_key_exit:
575 kfree(res_key);
576 return rc;
579 static int
580 smb2_clone_range(const unsigned int xid,
581 struct cifsFileInfo *srcfile,
582 struct cifsFileInfo *trgtfile, u64 src_off,
583 u64 len, u64 dest_off)
585 int rc;
586 unsigned int ret_data_len;
587 struct copychunk_ioctl *pcchunk;
588 struct copychunk_ioctl_rsp *retbuf = NULL;
589 struct cifs_tcon *tcon;
590 int chunks_copied = 0;
591 bool chunk_sizes_updated = false;
593 pcchunk = kmalloc(sizeof(struct copychunk_ioctl), GFP_KERNEL);
595 if (pcchunk == NULL)
596 return -ENOMEM;
598 cifs_dbg(FYI, "in smb2_clone_range - about to call request res key\n");
599 /* Request a key from the server to identify the source of the copy */
600 rc = SMB2_request_res_key(xid, tlink_tcon(srcfile->tlink),
601 srcfile->fid.persistent_fid,
602 srcfile->fid.volatile_fid, pcchunk);
604 /* Note: request_res_key sets res_key null only if rc !=0 */
605 if (rc)
606 goto cchunk_out;
608 /* For now array only one chunk long, will make more flexible later */
609 pcchunk->ChunkCount = cpu_to_le32(1);
610 pcchunk->Reserved = 0;
611 pcchunk->Reserved2 = 0;
613 tcon = tlink_tcon(trgtfile->tlink);
615 while (len > 0) {
616 pcchunk->SourceOffset = cpu_to_le64(src_off);
617 pcchunk->TargetOffset = cpu_to_le64(dest_off);
618 pcchunk->Length =
619 cpu_to_le32(min_t(u32, len, tcon->max_bytes_chunk));
621 /* Request server copy to target from src identified by key */
622 rc = SMB2_ioctl(xid, tcon, trgtfile->fid.persistent_fid,
623 trgtfile->fid.volatile_fid, FSCTL_SRV_COPYCHUNK_WRITE,
624 true /* is_fsctl */, (char *)pcchunk,
625 sizeof(struct copychunk_ioctl), (char **)&retbuf,
626 &ret_data_len);
627 if (rc == 0) {
628 if (ret_data_len !=
629 sizeof(struct copychunk_ioctl_rsp)) {
630 cifs_dbg(VFS, "invalid cchunk response size\n");
631 rc = -EIO;
632 goto cchunk_out;
634 if (retbuf->TotalBytesWritten == 0) {
635 cifs_dbg(FYI, "no bytes copied\n");
636 rc = -EIO;
637 goto cchunk_out;
640 * Check if server claimed to write more than we asked
642 if (le32_to_cpu(retbuf->TotalBytesWritten) >
643 le32_to_cpu(pcchunk->Length)) {
644 cifs_dbg(VFS, "invalid copy chunk response\n");
645 rc = -EIO;
646 goto cchunk_out;
648 if (le32_to_cpu(retbuf->ChunksWritten) != 1) {
649 cifs_dbg(VFS, "invalid num chunks written\n");
650 rc = -EIO;
651 goto cchunk_out;
653 chunks_copied++;
655 src_off += le32_to_cpu(retbuf->TotalBytesWritten);
656 dest_off += le32_to_cpu(retbuf->TotalBytesWritten);
657 len -= le32_to_cpu(retbuf->TotalBytesWritten);
659 cifs_dbg(FYI, "Chunks %d PartialChunk %d Total %d\n",
660 le32_to_cpu(retbuf->ChunksWritten),
661 le32_to_cpu(retbuf->ChunkBytesWritten),
662 le32_to_cpu(retbuf->TotalBytesWritten));
663 } else if (rc == -EINVAL) {
664 if (ret_data_len != sizeof(struct copychunk_ioctl_rsp))
665 goto cchunk_out;
667 cifs_dbg(FYI, "MaxChunks %d BytesChunk %d MaxCopy %d\n",
668 le32_to_cpu(retbuf->ChunksWritten),
669 le32_to_cpu(retbuf->ChunkBytesWritten),
670 le32_to_cpu(retbuf->TotalBytesWritten));
673 * Check if this is the first request using these sizes,
674 * (ie check if copy succeed once with original sizes
675 * and check if the server gave us different sizes after
676 * we already updated max sizes on previous request).
677 * if not then why is the server returning an error now
679 if ((chunks_copied != 0) || chunk_sizes_updated)
680 goto cchunk_out;
682 /* Check that server is not asking us to grow size */
683 if (le32_to_cpu(retbuf->ChunkBytesWritten) <
684 tcon->max_bytes_chunk)
685 tcon->max_bytes_chunk =
686 le32_to_cpu(retbuf->ChunkBytesWritten);
687 else
688 goto cchunk_out; /* server gave us bogus size */
690 /* No need to change MaxChunks since already set to 1 */
691 chunk_sizes_updated = true;
692 } else
693 goto cchunk_out;
696 cchunk_out:
697 kfree(pcchunk);
698 kfree(retbuf);
699 return rc;
702 static int
703 smb2_flush_file(const unsigned int xid, struct cifs_tcon *tcon,
704 struct cifs_fid *fid)
706 return SMB2_flush(xid, tcon, fid->persistent_fid, fid->volatile_fid);
709 static unsigned int
710 smb2_read_data_offset(char *buf)
712 struct smb2_read_rsp *rsp = (struct smb2_read_rsp *)buf;
713 return rsp->DataOffset;
716 static unsigned int
717 smb2_read_data_length(char *buf)
719 struct smb2_read_rsp *rsp = (struct smb2_read_rsp *)buf;
720 return le32_to_cpu(rsp->DataLength);
724 static int
725 smb2_sync_read(const unsigned int xid, struct cifs_fid *pfid,
726 struct cifs_io_parms *parms, unsigned int *bytes_read,
727 char **buf, int *buf_type)
729 parms->persistent_fid = pfid->persistent_fid;
730 parms->volatile_fid = pfid->volatile_fid;
731 return SMB2_read(xid, parms, bytes_read, buf, buf_type);
734 static int
735 smb2_sync_write(const unsigned int xid, struct cifs_fid *pfid,
736 struct cifs_io_parms *parms, unsigned int *written,
737 struct kvec *iov, unsigned long nr_segs)
740 parms->persistent_fid = pfid->persistent_fid;
741 parms->volatile_fid = pfid->volatile_fid;
742 return SMB2_write(xid, parms, written, iov, nr_segs);
745 /* Set or clear the SPARSE_FILE attribute based on value passed in setsparse */
746 static bool smb2_set_sparse(const unsigned int xid, struct cifs_tcon *tcon,
747 struct cifsFileInfo *cfile, struct inode *inode, __u8 setsparse)
749 struct cifsInodeInfo *cifsi;
750 int rc;
752 cifsi = CIFS_I(inode);
754 /* if file already sparse don't bother setting sparse again */
755 if ((cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) && setsparse)
756 return true; /* already sparse */
758 if (!(cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) && !setsparse)
759 return true; /* already not sparse */
762 * Can't check for sparse support on share the usual way via the
763 * FS attribute info (FILE_SUPPORTS_SPARSE_FILES) on the share
764 * since Samba server doesn't set the flag on the share, yet
765 * supports the set sparse FSCTL and returns sparse correctly
766 * in the file attributes. If we fail setting sparse though we
767 * mark that server does not support sparse files for this share
768 * to avoid repeatedly sending the unsupported fsctl to server
769 * if the file is repeatedly extended.
771 if (tcon->broken_sparse_sup)
772 return false;
774 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
775 cfile->fid.volatile_fid, FSCTL_SET_SPARSE,
776 true /* is_fctl */, &setsparse, 1, NULL, NULL);
777 if (rc) {
778 tcon->broken_sparse_sup = true;
779 cifs_dbg(FYI, "set sparse rc = %d\n", rc);
780 return false;
783 if (setsparse)
784 cifsi->cifsAttrs |= FILE_ATTRIBUTE_SPARSE_FILE;
785 else
786 cifsi->cifsAttrs &= (~FILE_ATTRIBUTE_SPARSE_FILE);
788 return true;
791 static int
792 smb2_set_file_size(const unsigned int xid, struct cifs_tcon *tcon,
793 struct cifsFileInfo *cfile, __u64 size, bool set_alloc)
795 __le64 eof = cpu_to_le64(size);
796 struct inode *inode;
799 * If extending file more than one page make sparse. Many Linux fs
800 * make files sparse by default when extending via ftruncate
802 inode = d_inode(cfile->dentry);
804 if (!set_alloc && (size > inode->i_size + 8192)) {
805 __u8 set_sparse = 1;
807 /* whether set sparse succeeds or not, extend the file */
808 smb2_set_sparse(xid, tcon, cfile, inode, set_sparse);
811 return SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
812 cfile->fid.volatile_fid, cfile->pid, &eof, false);
815 static int
816 smb2_duplicate_extents(const unsigned int xid,
817 struct cifsFileInfo *srcfile,
818 struct cifsFileInfo *trgtfile, u64 src_off,
819 u64 len, u64 dest_off)
821 int rc;
822 unsigned int ret_data_len;
823 struct duplicate_extents_to_file dup_ext_buf;
824 struct cifs_tcon *tcon = tlink_tcon(trgtfile->tlink);
826 /* server fileays advertise duplicate extent support with this flag */
827 if ((le32_to_cpu(tcon->fsAttrInfo.Attributes) &
828 FILE_SUPPORTS_BLOCK_REFCOUNTING) == 0)
829 return -EOPNOTSUPP;
831 dup_ext_buf.VolatileFileHandle = srcfile->fid.volatile_fid;
832 dup_ext_buf.PersistentFileHandle = srcfile->fid.persistent_fid;
833 dup_ext_buf.SourceFileOffset = cpu_to_le64(src_off);
834 dup_ext_buf.TargetFileOffset = cpu_to_le64(dest_off);
835 dup_ext_buf.ByteCount = cpu_to_le64(len);
836 cifs_dbg(FYI, "duplicate extents: src off %lld dst off %lld len %lld",
837 src_off, dest_off, len);
839 rc = smb2_set_file_size(xid, tcon, trgtfile, dest_off + len, false);
840 if (rc)
841 goto duplicate_extents_out;
843 rc = SMB2_ioctl(xid, tcon, trgtfile->fid.persistent_fid,
844 trgtfile->fid.volatile_fid,
845 FSCTL_DUPLICATE_EXTENTS_TO_FILE,
846 true /* is_fsctl */, (char *)&dup_ext_buf,
847 sizeof(struct duplicate_extents_to_file),
848 NULL,
849 &ret_data_len);
851 if (ret_data_len > 0)
852 cifs_dbg(FYI, "non-zero response length in duplicate extents");
854 duplicate_extents_out:
855 return rc;
858 static int
859 smb2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
860 struct cifsFileInfo *cfile)
862 return SMB2_set_compression(xid, tcon, cfile->fid.persistent_fid,
863 cfile->fid.volatile_fid);
866 static int
867 smb3_set_integrity(const unsigned int xid, struct cifs_tcon *tcon,
868 struct cifsFileInfo *cfile)
870 struct fsctl_set_integrity_information_req integr_info;
871 unsigned int ret_data_len;
873 integr_info.ChecksumAlgorithm = cpu_to_le16(CHECKSUM_TYPE_UNCHANGED);
874 integr_info.Flags = 0;
875 integr_info.Reserved = 0;
877 return SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
878 cfile->fid.volatile_fid,
879 FSCTL_SET_INTEGRITY_INFORMATION,
880 true /* is_fsctl */, (char *)&integr_info,
881 sizeof(struct fsctl_set_integrity_information_req),
882 NULL,
883 &ret_data_len);
887 static int
888 smb2_query_dir_first(const unsigned int xid, struct cifs_tcon *tcon,
889 const char *path, struct cifs_sb_info *cifs_sb,
890 struct cifs_fid *fid, __u16 search_flags,
891 struct cifs_search_info *srch_inf)
893 __le16 *utf16_path;
894 int rc;
895 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
896 struct cifs_open_parms oparms;
898 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
899 if (!utf16_path)
900 return -ENOMEM;
902 oparms.tcon = tcon;
903 oparms.desired_access = FILE_READ_ATTRIBUTES | FILE_READ_DATA;
904 oparms.disposition = FILE_OPEN;
905 oparms.create_options = 0;
906 oparms.fid = fid;
907 oparms.reconnect = false;
909 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL);
910 kfree(utf16_path);
911 if (rc) {
912 cifs_dbg(FYI, "open dir failed rc=%d\n", rc);
913 return rc;
916 srch_inf->entries_in_buffer = 0;
917 srch_inf->index_of_last_entry = 2;
919 rc = SMB2_query_directory(xid, tcon, fid->persistent_fid,
920 fid->volatile_fid, 0, srch_inf);
921 if (rc) {
922 cifs_dbg(FYI, "query directory failed rc=%d\n", rc);
923 SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
925 return rc;
928 static int
929 smb2_query_dir_next(const unsigned int xid, struct cifs_tcon *tcon,
930 struct cifs_fid *fid, __u16 search_flags,
931 struct cifs_search_info *srch_inf)
933 return SMB2_query_directory(xid, tcon, fid->persistent_fid,
934 fid->volatile_fid, 0, srch_inf);
937 static int
938 smb2_close_dir(const unsigned int xid, struct cifs_tcon *tcon,
939 struct cifs_fid *fid)
941 return SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
945 * If we negotiate SMB2 protocol and get STATUS_PENDING - update
946 * the number of credits and return true. Otherwise - return false.
948 static bool
949 smb2_is_status_pending(char *buf, struct TCP_Server_Info *server, int length)
951 struct smb2_hdr *hdr = (struct smb2_hdr *)buf;
953 if (hdr->Status != STATUS_PENDING)
954 return false;
956 if (!length) {
957 spin_lock(&server->req_lock);
958 server->credits += le16_to_cpu(hdr->CreditRequest);
959 spin_unlock(&server->req_lock);
960 wake_up(&server->request_q);
963 return true;
966 static bool
967 smb2_is_session_expired(char *buf)
969 struct smb2_hdr *hdr = (struct smb2_hdr *)buf;
971 if (hdr->Status != STATUS_NETWORK_SESSION_EXPIRED)
972 return false;
974 cifs_dbg(FYI, "Session expired\n");
975 return true;
978 static int
979 smb2_oplock_response(struct cifs_tcon *tcon, struct cifs_fid *fid,
980 struct cifsInodeInfo *cinode)
982 if (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_LEASING)
983 return SMB2_lease_break(0, tcon, cinode->lease_key,
984 smb2_get_lease_state(cinode));
986 return SMB2_oplock_break(0, tcon, fid->persistent_fid,
987 fid->volatile_fid,
988 CIFS_CACHE_READ(cinode) ? 1 : 0);
991 static int
992 smb2_queryfs(const unsigned int xid, struct cifs_tcon *tcon,
993 struct kstatfs *buf)
995 int rc;
996 __le16 srch_path = 0; /* Null - open root of share */
997 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
998 struct cifs_open_parms oparms;
999 struct cifs_fid fid;
1001 oparms.tcon = tcon;
1002 oparms.desired_access = FILE_READ_ATTRIBUTES;
1003 oparms.disposition = FILE_OPEN;
1004 oparms.create_options = 0;
1005 oparms.fid = &fid;
1006 oparms.reconnect = false;
1008 rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL);
1009 if (rc)
1010 return rc;
1011 buf->f_type = SMB2_MAGIC_NUMBER;
1012 rc = SMB2_QFS_info(xid, tcon, fid.persistent_fid, fid.volatile_fid,
1013 buf);
1014 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
1015 return rc;
1018 static bool
1019 smb2_compare_fids(struct cifsFileInfo *ob1, struct cifsFileInfo *ob2)
1021 return ob1->fid.persistent_fid == ob2->fid.persistent_fid &&
1022 ob1->fid.volatile_fid == ob2->fid.volatile_fid;
1025 static int
1026 smb2_mand_lock(const unsigned int xid, struct cifsFileInfo *cfile, __u64 offset,
1027 __u64 length, __u32 type, int lock, int unlock, bool wait)
1029 if (unlock && !lock)
1030 type = SMB2_LOCKFLAG_UNLOCK;
1031 return SMB2_lock(xid, tlink_tcon(cfile->tlink),
1032 cfile->fid.persistent_fid, cfile->fid.volatile_fid,
1033 current->tgid, length, offset, type, wait);
1036 static void
1037 smb2_get_lease_key(struct inode *inode, struct cifs_fid *fid)
1039 memcpy(fid->lease_key, CIFS_I(inode)->lease_key, SMB2_LEASE_KEY_SIZE);
1042 static void
1043 smb2_set_lease_key(struct inode *inode, struct cifs_fid *fid)
1045 memcpy(CIFS_I(inode)->lease_key, fid->lease_key, SMB2_LEASE_KEY_SIZE);
1048 static void
1049 smb2_new_lease_key(struct cifs_fid *fid)
1051 generate_random_uuid(fid->lease_key);
1054 #define SMB2_SYMLINK_STRUCT_SIZE \
1055 (sizeof(struct smb2_err_rsp) - 1 + sizeof(struct smb2_symlink_err_rsp))
1057 static int
1058 smb2_query_symlink(const unsigned int xid, struct cifs_tcon *tcon,
1059 const char *full_path, char **target_path,
1060 struct cifs_sb_info *cifs_sb)
1062 int rc;
1063 __le16 *utf16_path;
1064 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
1065 struct cifs_open_parms oparms;
1066 struct cifs_fid fid;
1067 struct smb2_err_rsp *err_buf = NULL;
1068 struct smb2_symlink_err_rsp *symlink;
1069 unsigned int sub_len;
1070 unsigned int sub_offset;
1071 unsigned int print_len;
1072 unsigned int print_offset;
1074 cifs_dbg(FYI, "%s: path: %s\n", __func__, full_path);
1076 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
1077 if (!utf16_path)
1078 return -ENOMEM;
1080 oparms.tcon = tcon;
1081 oparms.desired_access = FILE_READ_ATTRIBUTES;
1082 oparms.disposition = FILE_OPEN;
1083 oparms.create_options = 0;
1084 oparms.fid = &fid;
1085 oparms.reconnect = false;
1087 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, &err_buf);
1089 if (!rc || !err_buf) {
1090 kfree(utf16_path);
1091 return -ENOENT;
1094 if (le32_to_cpu(err_buf->ByteCount) < sizeof(struct smb2_symlink_err_rsp) ||
1095 get_rfc1002_length(err_buf) + 4 < SMB2_SYMLINK_STRUCT_SIZE) {
1096 kfree(utf16_path);
1097 return -ENOENT;
1100 /* open must fail on symlink - reset rc */
1101 rc = 0;
1102 symlink = (struct smb2_symlink_err_rsp *)err_buf->ErrorData;
1103 sub_len = le16_to_cpu(symlink->SubstituteNameLength);
1104 sub_offset = le16_to_cpu(symlink->SubstituteNameOffset);
1105 print_len = le16_to_cpu(symlink->PrintNameLength);
1106 print_offset = le16_to_cpu(symlink->PrintNameOffset);
1108 if (get_rfc1002_length(err_buf) + 4 <
1109 SMB2_SYMLINK_STRUCT_SIZE + sub_offset + sub_len) {
1110 kfree(utf16_path);
1111 return -ENOENT;
1114 if (get_rfc1002_length(err_buf) + 4 <
1115 SMB2_SYMLINK_STRUCT_SIZE + print_offset + print_len) {
1116 kfree(utf16_path);
1117 return -ENOENT;
1120 *target_path = cifs_strndup_from_utf16(
1121 (char *)symlink->PathBuffer + sub_offset,
1122 sub_len, true, cifs_sb->local_nls);
1123 if (!(*target_path)) {
1124 kfree(utf16_path);
1125 return -ENOMEM;
1127 convert_delimiter(*target_path, '/');
1128 cifs_dbg(FYI, "%s: target path: %s\n", __func__, *target_path);
1129 kfree(utf16_path);
1130 return rc;
1133 static long smb3_zero_range(struct file *file, struct cifs_tcon *tcon,
1134 loff_t offset, loff_t len, bool keep_size)
1136 struct inode *inode;
1137 struct cifsInodeInfo *cifsi;
1138 struct cifsFileInfo *cfile = file->private_data;
1139 struct file_zero_data_information fsctl_buf;
1140 long rc;
1141 unsigned int xid;
1143 xid = get_xid();
1145 inode = d_inode(cfile->dentry);
1146 cifsi = CIFS_I(inode);
1149 * We zero the range through ioctl, so we need remove the page caches
1150 * first, otherwise the data may be inconsistent with the server.
1152 truncate_pagecache_range(inode, offset, offset + len - 1);
1154 /* if file not oplocked can't be sure whether asking to extend size */
1155 if (!CIFS_CACHE_READ(cifsi))
1156 if (keep_size == false)
1157 return -EOPNOTSUPP;
1160 * Must check if file sparse since fallocate -z (zero range) assumes
1161 * non-sparse allocation
1163 if (!(cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE))
1164 return -EOPNOTSUPP;
1167 * need to make sure we are not asked to extend the file since the SMB3
1168 * fsctl does not change the file size. In the future we could change
1169 * this to zero the first part of the range then set the file size
1170 * which for a non sparse file would zero the newly extended range
1172 if (keep_size == false)
1173 if (i_size_read(inode) < offset + len)
1174 return -EOPNOTSUPP;
1176 cifs_dbg(FYI, "offset %lld len %lld", offset, len);
1178 fsctl_buf.FileOffset = cpu_to_le64(offset);
1179 fsctl_buf.BeyondFinalZero = cpu_to_le64(offset + len);
1181 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
1182 cfile->fid.volatile_fid, FSCTL_SET_ZERO_DATA,
1183 true /* is_fctl */, (char *)&fsctl_buf,
1184 sizeof(struct file_zero_data_information), NULL, NULL);
1185 free_xid(xid);
1186 return rc;
1189 static long smb3_punch_hole(struct file *file, struct cifs_tcon *tcon,
1190 loff_t offset, loff_t len)
1192 struct inode *inode;
1193 struct cifsInodeInfo *cifsi;
1194 struct cifsFileInfo *cfile = file->private_data;
1195 struct file_zero_data_information fsctl_buf;
1196 long rc;
1197 unsigned int xid;
1198 __u8 set_sparse = 1;
1200 xid = get_xid();
1202 inode = d_inode(cfile->dentry);
1203 cifsi = CIFS_I(inode);
1205 /* Need to make file sparse, if not already, before freeing range. */
1206 /* Consider adding equivalent for compressed since it could also work */
1207 if (!smb2_set_sparse(xid, tcon, cfile, inode, set_sparse))
1208 return -EOPNOTSUPP;
1211 * We implement the punch hole through ioctl, so we need remove the page
1212 * caches first, otherwise the data may be inconsistent with the server.
1214 truncate_pagecache_range(inode, offset, offset + len - 1);
1216 cifs_dbg(FYI, "offset %lld len %lld", offset, len);
1218 fsctl_buf.FileOffset = cpu_to_le64(offset);
1219 fsctl_buf.BeyondFinalZero = cpu_to_le64(offset + len);
1221 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
1222 cfile->fid.volatile_fid, FSCTL_SET_ZERO_DATA,
1223 true /* is_fctl */, (char *)&fsctl_buf,
1224 sizeof(struct file_zero_data_information), NULL, NULL);
1225 free_xid(xid);
1226 return rc;
1229 static long smb3_simple_falloc(struct file *file, struct cifs_tcon *tcon,
1230 loff_t off, loff_t len, bool keep_size)
1232 struct inode *inode;
1233 struct cifsInodeInfo *cifsi;
1234 struct cifsFileInfo *cfile = file->private_data;
1235 long rc = -EOPNOTSUPP;
1236 unsigned int xid;
1238 xid = get_xid();
1240 inode = d_inode(cfile->dentry);
1241 cifsi = CIFS_I(inode);
1243 /* if file not oplocked can't be sure whether asking to extend size */
1244 if (!CIFS_CACHE_READ(cifsi))
1245 if (keep_size == false)
1246 return -EOPNOTSUPP;
1249 * Files are non-sparse by default so falloc may be a no-op
1250 * Must check if file sparse. If not sparse, and not extending
1251 * then no need to do anything since file already allocated
1253 if ((cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) == 0) {
1254 if (keep_size == true)
1255 return 0;
1256 /* check if extending file */
1257 else if (i_size_read(inode) >= off + len)
1258 /* not extending file and already not sparse */
1259 return 0;
1260 /* BB: in future add else clause to extend file */
1261 else
1262 return -EOPNOTSUPP;
1265 if ((keep_size == true) || (i_size_read(inode) >= off + len)) {
1267 * Check if falloc starts within first few pages of file
1268 * and ends within a few pages of the end of file to
1269 * ensure that most of file is being forced to be
1270 * fallocated now. If so then setting whole file sparse
1271 * ie potentially making a few extra pages at the beginning
1272 * or end of the file non-sparse via set_sparse is harmless.
1274 if ((off > 8192) || (off + len + 8192 < i_size_read(inode)))
1275 return -EOPNOTSUPP;
1277 rc = smb2_set_sparse(xid, tcon, cfile, inode, false);
1279 /* BB: else ... in future add code to extend file and set sparse */
1282 free_xid(xid);
1283 return rc;
1287 static long smb3_fallocate(struct file *file, struct cifs_tcon *tcon, int mode,
1288 loff_t off, loff_t len)
1290 /* KEEP_SIZE already checked for by do_fallocate */
1291 if (mode & FALLOC_FL_PUNCH_HOLE)
1292 return smb3_punch_hole(file, tcon, off, len);
1293 else if (mode & FALLOC_FL_ZERO_RANGE) {
1294 if (mode & FALLOC_FL_KEEP_SIZE)
1295 return smb3_zero_range(file, tcon, off, len, true);
1296 return smb3_zero_range(file, tcon, off, len, false);
1297 } else if (mode == FALLOC_FL_KEEP_SIZE)
1298 return smb3_simple_falloc(file, tcon, off, len, true);
1299 else if (mode == 0)
1300 return smb3_simple_falloc(file, tcon, off, len, false);
1302 return -EOPNOTSUPP;
1305 static void
1306 smb2_downgrade_oplock(struct TCP_Server_Info *server,
1307 struct cifsInodeInfo *cinode, bool set_level2)
1309 if (set_level2)
1310 server->ops->set_oplock_level(cinode, SMB2_OPLOCK_LEVEL_II,
1311 0, NULL);
1312 else
1313 server->ops->set_oplock_level(cinode, 0, 0, NULL);
1316 static void
1317 smb2_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
1318 unsigned int epoch, bool *purge_cache)
1320 oplock &= 0xFF;
1321 if (oplock == SMB2_OPLOCK_LEVEL_NOCHANGE)
1322 return;
1323 if (oplock == SMB2_OPLOCK_LEVEL_BATCH) {
1324 cinode->oplock = CIFS_CACHE_RHW_FLG;
1325 cifs_dbg(FYI, "Batch Oplock granted on inode %p\n",
1326 &cinode->vfs_inode);
1327 } else if (oplock == SMB2_OPLOCK_LEVEL_EXCLUSIVE) {
1328 cinode->oplock = CIFS_CACHE_RW_FLG;
1329 cifs_dbg(FYI, "Exclusive Oplock granted on inode %p\n",
1330 &cinode->vfs_inode);
1331 } else if (oplock == SMB2_OPLOCK_LEVEL_II) {
1332 cinode->oplock = CIFS_CACHE_READ_FLG;
1333 cifs_dbg(FYI, "Level II Oplock granted on inode %p\n",
1334 &cinode->vfs_inode);
1335 } else
1336 cinode->oplock = 0;
1339 static void
1340 smb21_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
1341 unsigned int epoch, bool *purge_cache)
1343 char message[5] = {0};
1344 unsigned int new_oplock = 0;
1346 oplock &= 0xFF;
1347 if (oplock == SMB2_OPLOCK_LEVEL_NOCHANGE)
1348 return;
1350 /* Check if the server granted an oplock rather than a lease */
1351 if (oplock & SMB2_OPLOCK_LEVEL_EXCLUSIVE)
1352 return smb2_set_oplock_level(cinode, oplock, epoch,
1353 purge_cache);
1355 if (oplock & SMB2_LEASE_READ_CACHING_HE) {
1356 new_oplock |= CIFS_CACHE_READ_FLG;
1357 strcat(message, "R");
1359 if (oplock & SMB2_LEASE_HANDLE_CACHING_HE) {
1360 new_oplock |= CIFS_CACHE_HANDLE_FLG;
1361 strcat(message, "H");
1363 if (oplock & SMB2_LEASE_WRITE_CACHING_HE) {
1364 new_oplock |= CIFS_CACHE_WRITE_FLG;
1365 strcat(message, "W");
1367 if (!new_oplock)
1368 strncpy(message, "None", sizeof(message));
1370 cinode->oplock = new_oplock;
1371 cifs_dbg(FYI, "%s Lease granted on inode %p\n", message,
1372 &cinode->vfs_inode);
1375 static void
1376 smb3_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
1377 unsigned int epoch, bool *purge_cache)
1379 unsigned int old_oplock = cinode->oplock;
1381 smb21_set_oplock_level(cinode, oplock, epoch, purge_cache);
1383 if (purge_cache) {
1384 *purge_cache = false;
1385 if (old_oplock == CIFS_CACHE_READ_FLG) {
1386 if (cinode->oplock == CIFS_CACHE_READ_FLG &&
1387 (epoch - cinode->epoch > 0))
1388 *purge_cache = true;
1389 else if (cinode->oplock == CIFS_CACHE_RH_FLG &&
1390 (epoch - cinode->epoch > 1))
1391 *purge_cache = true;
1392 else if (cinode->oplock == CIFS_CACHE_RHW_FLG &&
1393 (epoch - cinode->epoch > 1))
1394 *purge_cache = true;
1395 else if (cinode->oplock == 0 &&
1396 (epoch - cinode->epoch > 0))
1397 *purge_cache = true;
1398 } else if (old_oplock == CIFS_CACHE_RH_FLG) {
1399 if (cinode->oplock == CIFS_CACHE_RH_FLG &&
1400 (epoch - cinode->epoch > 0))
1401 *purge_cache = true;
1402 else if (cinode->oplock == CIFS_CACHE_RHW_FLG &&
1403 (epoch - cinode->epoch > 1))
1404 *purge_cache = true;
1406 cinode->epoch = epoch;
1410 static bool
1411 smb2_is_read_op(__u32 oplock)
1413 return oplock == SMB2_OPLOCK_LEVEL_II;
1416 static bool
1417 smb21_is_read_op(__u32 oplock)
1419 return (oplock & SMB2_LEASE_READ_CACHING_HE) &&
1420 !(oplock & SMB2_LEASE_WRITE_CACHING_HE);
1423 static __le32
1424 map_oplock_to_lease(u8 oplock)
1426 if (oplock == SMB2_OPLOCK_LEVEL_EXCLUSIVE)
1427 return SMB2_LEASE_WRITE_CACHING | SMB2_LEASE_READ_CACHING;
1428 else if (oplock == SMB2_OPLOCK_LEVEL_II)
1429 return SMB2_LEASE_READ_CACHING;
1430 else if (oplock == SMB2_OPLOCK_LEVEL_BATCH)
1431 return SMB2_LEASE_HANDLE_CACHING | SMB2_LEASE_READ_CACHING |
1432 SMB2_LEASE_WRITE_CACHING;
1433 return 0;
1436 static char *
1437 smb2_create_lease_buf(u8 *lease_key, u8 oplock)
1439 struct create_lease *buf;
1441 buf = kzalloc(sizeof(struct create_lease), GFP_KERNEL);
1442 if (!buf)
1443 return NULL;
1445 buf->lcontext.LeaseKeyLow = cpu_to_le64(*((u64 *)lease_key));
1446 buf->lcontext.LeaseKeyHigh = cpu_to_le64(*((u64 *)(lease_key + 8)));
1447 buf->lcontext.LeaseState = map_oplock_to_lease(oplock);
1449 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1450 (struct create_lease, lcontext));
1451 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context));
1452 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1453 (struct create_lease, Name));
1454 buf->ccontext.NameLength = cpu_to_le16(4);
1455 /* SMB2_CREATE_REQUEST_LEASE is "RqLs" */
1456 buf->Name[0] = 'R';
1457 buf->Name[1] = 'q';
1458 buf->Name[2] = 'L';
1459 buf->Name[3] = 's';
1460 return (char *)buf;
1463 static char *
1464 smb3_create_lease_buf(u8 *lease_key, u8 oplock)
1466 struct create_lease_v2 *buf;
1468 buf = kzalloc(sizeof(struct create_lease_v2), GFP_KERNEL);
1469 if (!buf)
1470 return NULL;
1472 buf->lcontext.LeaseKeyLow = cpu_to_le64(*((u64 *)lease_key));
1473 buf->lcontext.LeaseKeyHigh = cpu_to_le64(*((u64 *)(lease_key + 8)));
1474 buf->lcontext.LeaseState = map_oplock_to_lease(oplock);
1476 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1477 (struct create_lease_v2, lcontext));
1478 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context_v2));
1479 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1480 (struct create_lease_v2, Name));
1481 buf->ccontext.NameLength = cpu_to_le16(4);
1482 /* SMB2_CREATE_REQUEST_LEASE is "RqLs" */
1483 buf->Name[0] = 'R';
1484 buf->Name[1] = 'q';
1485 buf->Name[2] = 'L';
1486 buf->Name[3] = 's';
1487 return (char *)buf;
1490 static __u8
1491 smb2_parse_lease_buf(void *buf, unsigned int *epoch)
1493 struct create_lease *lc = (struct create_lease *)buf;
1495 *epoch = 0; /* not used */
1496 if (lc->lcontext.LeaseFlags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS)
1497 return SMB2_OPLOCK_LEVEL_NOCHANGE;
1498 return le32_to_cpu(lc->lcontext.LeaseState);
1501 static __u8
1502 smb3_parse_lease_buf(void *buf, unsigned int *epoch)
1504 struct create_lease_v2 *lc = (struct create_lease_v2 *)buf;
1506 *epoch = le16_to_cpu(lc->lcontext.Epoch);
1507 if (lc->lcontext.LeaseFlags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS)
1508 return SMB2_OPLOCK_LEVEL_NOCHANGE;
1509 return le32_to_cpu(lc->lcontext.LeaseState);
1512 static unsigned int
1513 smb2_wp_retry_size(struct inode *inode)
1515 return min_t(unsigned int, CIFS_SB(inode->i_sb)->wsize,
1516 SMB2_MAX_BUFFER_SIZE);
1519 static bool
1520 smb2_dir_needs_close(struct cifsFileInfo *cfile)
1522 return !cfile->invalidHandle;
1525 struct smb_version_operations smb20_operations = {
1526 .compare_fids = smb2_compare_fids,
1527 .setup_request = smb2_setup_request,
1528 .setup_async_request = smb2_setup_async_request,
1529 .check_receive = smb2_check_receive,
1530 .add_credits = smb2_add_credits,
1531 .set_credits = smb2_set_credits,
1532 .get_credits_field = smb2_get_credits_field,
1533 .get_credits = smb2_get_credits,
1534 .wait_mtu_credits = cifs_wait_mtu_credits,
1535 .get_next_mid = smb2_get_next_mid,
1536 .read_data_offset = smb2_read_data_offset,
1537 .read_data_length = smb2_read_data_length,
1538 .map_error = map_smb2_to_linux_error,
1539 .find_mid = smb2_find_mid,
1540 .check_message = smb2_check_message,
1541 .dump_detail = smb2_dump_detail,
1542 .clear_stats = smb2_clear_stats,
1543 .print_stats = smb2_print_stats,
1544 .is_oplock_break = smb2_is_valid_oplock_break,
1545 .handle_cancelled_mid = smb2_handle_cancelled_mid,
1546 .downgrade_oplock = smb2_downgrade_oplock,
1547 .need_neg = smb2_need_neg,
1548 .negotiate = smb2_negotiate,
1549 .negotiate_wsize = smb2_negotiate_wsize,
1550 .negotiate_rsize = smb2_negotiate_rsize,
1551 .sess_setup = SMB2_sess_setup,
1552 .logoff = SMB2_logoff,
1553 .tree_connect = SMB2_tcon,
1554 .tree_disconnect = SMB2_tdis,
1555 .qfs_tcon = smb2_qfs_tcon,
1556 .is_path_accessible = smb2_is_path_accessible,
1557 .can_echo = smb2_can_echo,
1558 .echo = SMB2_echo,
1559 .query_path_info = smb2_query_path_info,
1560 .get_srv_inum = smb2_get_srv_inum,
1561 .query_file_info = smb2_query_file_info,
1562 .set_path_size = smb2_set_path_size,
1563 .set_file_size = smb2_set_file_size,
1564 .set_file_info = smb2_set_file_info,
1565 .set_compression = smb2_set_compression,
1566 .mkdir = smb2_mkdir,
1567 .mkdir_setinfo = smb2_mkdir_setinfo,
1568 .rmdir = smb2_rmdir,
1569 .unlink = smb2_unlink,
1570 .rename = smb2_rename_path,
1571 .create_hardlink = smb2_create_hardlink,
1572 .query_symlink = smb2_query_symlink,
1573 .open = smb2_open_file,
1574 .set_fid = smb2_set_fid,
1575 .close = smb2_close_file,
1576 .flush = smb2_flush_file,
1577 .async_readv = smb2_async_readv,
1578 .async_writev = smb2_async_writev,
1579 .sync_read = smb2_sync_read,
1580 .sync_write = smb2_sync_write,
1581 .query_dir_first = smb2_query_dir_first,
1582 .query_dir_next = smb2_query_dir_next,
1583 .close_dir = smb2_close_dir,
1584 .calc_smb_size = smb2_calc_size,
1585 .is_status_pending = smb2_is_status_pending,
1586 .is_session_expired = smb2_is_session_expired,
1587 .oplock_response = smb2_oplock_response,
1588 .queryfs = smb2_queryfs,
1589 .mand_lock = smb2_mand_lock,
1590 .mand_unlock_range = smb2_unlock_range,
1591 .push_mand_locks = smb2_push_mandatory_locks,
1592 .get_lease_key = smb2_get_lease_key,
1593 .set_lease_key = smb2_set_lease_key,
1594 .new_lease_key = smb2_new_lease_key,
1595 .calc_signature = smb2_calc_signature,
1596 .is_read_op = smb2_is_read_op,
1597 .set_oplock_level = smb2_set_oplock_level,
1598 .create_lease_buf = smb2_create_lease_buf,
1599 .parse_lease_buf = smb2_parse_lease_buf,
1600 .clone_range = smb2_clone_range,
1601 .wp_retry_size = smb2_wp_retry_size,
1602 .dir_needs_close = smb2_dir_needs_close,
1605 struct smb_version_operations smb21_operations = {
1606 .compare_fids = smb2_compare_fids,
1607 .setup_request = smb2_setup_request,
1608 .setup_async_request = smb2_setup_async_request,
1609 .check_receive = smb2_check_receive,
1610 .add_credits = smb2_add_credits,
1611 .set_credits = smb2_set_credits,
1612 .get_credits_field = smb2_get_credits_field,
1613 .get_credits = smb2_get_credits,
1614 .wait_mtu_credits = smb2_wait_mtu_credits,
1615 .get_next_mid = smb2_get_next_mid,
1616 .read_data_offset = smb2_read_data_offset,
1617 .read_data_length = smb2_read_data_length,
1618 .map_error = map_smb2_to_linux_error,
1619 .find_mid = smb2_find_mid,
1620 .check_message = smb2_check_message,
1621 .dump_detail = smb2_dump_detail,
1622 .clear_stats = smb2_clear_stats,
1623 .print_stats = smb2_print_stats,
1624 .is_oplock_break = smb2_is_valid_oplock_break,
1625 .handle_cancelled_mid = smb2_handle_cancelled_mid,
1626 .downgrade_oplock = smb2_downgrade_oplock,
1627 .need_neg = smb2_need_neg,
1628 .negotiate = smb2_negotiate,
1629 .negotiate_wsize = smb2_negotiate_wsize,
1630 .negotiate_rsize = smb2_negotiate_rsize,
1631 .sess_setup = SMB2_sess_setup,
1632 .logoff = SMB2_logoff,
1633 .tree_connect = SMB2_tcon,
1634 .tree_disconnect = SMB2_tdis,
1635 .qfs_tcon = smb2_qfs_tcon,
1636 .is_path_accessible = smb2_is_path_accessible,
1637 .can_echo = smb2_can_echo,
1638 .echo = SMB2_echo,
1639 .query_path_info = smb2_query_path_info,
1640 .get_srv_inum = smb2_get_srv_inum,
1641 .query_file_info = smb2_query_file_info,
1642 .set_path_size = smb2_set_path_size,
1643 .set_file_size = smb2_set_file_size,
1644 .set_file_info = smb2_set_file_info,
1645 .set_compression = smb2_set_compression,
1646 .mkdir = smb2_mkdir,
1647 .mkdir_setinfo = smb2_mkdir_setinfo,
1648 .rmdir = smb2_rmdir,
1649 .unlink = smb2_unlink,
1650 .rename = smb2_rename_path,
1651 .create_hardlink = smb2_create_hardlink,
1652 .query_symlink = smb2_query_symlink,
1653 .query_mf_symlink = smb3_query_mf_symlink,
1654 .create_mf_symlink = smb3_create_mf_symlink,
1655 .open = smb2_open_file,
1656 .set_fid = smb2_set_fid,
1657 .close = smb2_close_file,
1658 .flush = smb2_flush_file,
1659 .async_readv = smb2_async_readv,
1660 .async_writev = smb2_async_writev,
1661 .sync_read = smb2_sync_read,
1662 .sync_write = smb2_sync_write,
1663 .query_dir_first = smb2_query_dir_first,
1664 .query_dir_next = smb2_query_dir_next,
1665 .close_dir = smb2_close_dir,
1666 .calc_smb_size = smb2_calc_size,
1667 .is_status_pending = smb2_is_status_pending,
1668 .is_session_expired = smb2_is_session_expired,
1669 .oplock_response = smb2_oplock_response,
1670 .queryfs = smb2_queryfs,
1671 .mand_lock = smb2_mand_lock,
1672 .mand_unlock_range = smb2_unlock_range,
1673 .push_mand_locks = smb2_push_mandatory_locks,
1674 .get_lease_key = smb2_get_lease_key,
1675 .set_lease_key = smb2_set_lease_key,
1676 .new_lease_key = smb2_new_lease_key,
1677 .calc_signature = smb2_calc_signature,
1678 .is_read_op = smb21_is_read_op,
1679 .set_oplock_level = smb21_set_oplock_level,
1680 .create_lease_buf = smb2_create_lease_buf,
1681 .parse_lease_buf = smb2_parse_lease_buf,
1682 .clone_range = smb2_clone_range,
1683 .wp_retry_size = smb2_wp_retry_size,
1684 .dir_needs_close = smb2_dir_needs_close,
1687 struct smb_version_operations smb30_operations = {
1688 .compare_fids = smb2_compare_fids,
1689 .setup_request = smb2_setup_request,
1690 .setup_async_request = smb2_setup_async_request,
1691 .check_receive = smb2_check_receive,
1692 .add_credits = smb2_add_credits,
1693 .set_credits = smb2_set_credits,
1694 .get_credits_field = smb2_get_credits_field,
1695 .get_credits = smb2_get_credits,
1696 .wait_mtu_credits = smb2_wait_mtu_credits,
1697 .get_next_mid = smb2_get_next_mid,
1698 .read_data_offset = smb2_read_data_offset,
1699 .read_data_length = smb2_read_data_length,
1700 .map_error = map_smb2_to_linux_error,
1701 .find_mid = smb2_find_mid,
1702 .check_message = smb2_check_message,
1703 .dump_detail = smb2_dump_detail,
1704 .clear_stats = smb2_clear_stats,
1705 .print_stats = smb2_print_stats,
1706 .dump_share_caps = smb2_dump_share_caps,
1707 .is_oplock_break = smb2_is_valid_oplock_break,
1708 .handle_cancelled_mid = smb2_handle_cancelled_mid,
1709 .downgrade_oplock = smb2_downgrade_oplock,
1710 .need_neg = smb2_need_neg,
1711 .negotiate = smb2_negotiate,
1712 .negotiate_wsize = smb2_negotiate_wsize,
1713 .negotiate_rsize = smb2_negotiate_rsize,
1714 .sess_setup = SMB2_sess_setup,
1715 .logoff = SMB2_logoff,
1716 .tree_connect = SMB2_tcon,
1717 .tree_disconnect = SMB2_tdis,
1718 .qfs_tcon = smb3_qfs_tcon,
1719 .is_path_accessible = smb2_is_path_accessible,
1720 .can_echo = smb2_can_echo,
1721 .echo = SMB2_echo,
1722 .query_path_info = smb2_query_path_info,
1723 .get_srv_inum = smb2_get_srv_inum,
1724 .query_file_info = smb2_query_file_info,
1725 .set_path_size = smb2_set_path_size,
1726 .set_file_size = smb2_set_file_size,
1727 .set_file_info = smb2_set_file_info,
1728 .set_compression = smb2_set_compression,
1729 .mkdir = smb2_mkdir,
1730 .mkdir_setinfo = smb2_mkdir_setinfo,
1731 .rmdir = smb2_rmdir,
1732 .unlink = smb2_unlink,
1733 .rename = smb2_rename_path,
1734 .create_hardlink = smb2_create_hardlink,
1735 .query_symlink = smb2_query_symlink,
1736 .query_mf_symlink = smb3_query_mf_symlink,
1737 .create_mf_symlink = smb3_create_mf_symlink,
1738 .open = smb2_open_file,
1739 .set_fid = smb2_set_fid,
1740 .close = smb2_close_file,
1741 .flush = smb2_flush_file,
1742 .async_readv = smb2_async_readv,
1743 .async_writev = smb2_async_writev,
1744 .sync_read = smb2_sync_read,
1745 .sync_write = smb2_sync_write,
1746 .query_dir_first = smb2_query_dir_first,
1747 .query_dir_next = smb2_query_dir_next,
1748 .close_dir = smb2_close_dir,
1749 .calc_smb_size = smb2_calc_size,
1750 .is_status_pending = smb2_is_status_pending,
1751 .is_session_expired = smb2_is_session_expired,
1752 .oplock_response = smb2_oplock_response,
1753 .queryfs = smb2_queryfs,
1754 .mand_lock = smb2_mand_lock,
1755 .mand_unlock_range = smb2_unlock_range,
1756 .push_mand_locks = smb2_push_mandatory_locks,
1757 .get_lease_key = smb2_get_lease_key,
1758 .set_lease_key = smb2_set_lease_key,
1759 .new_lease_key = smb2_new_lease_key,
1760 .generate_signingkey = generate_smb3signingkey,
1761 .calc_signature = smb3_calc_signature,
1762 .set_integrity = smb3_set_integrity,
1763 .is_read_op = smb21_is_read_op,
1764 .set_oplock_level = smb3_set_oplock_level,
1765 .create_lease_buf = smb3_create_lease_buf,
1766 .parse_lease_buf = smb3_parse_lease_buf,
1767 .clone_range = smb2_clone_range,
1768 .duplicate_extents = smb2_duplicate_extents,
1769 .validate_negotiate = smb3_validate_negotiate,
1770 .wp_retry_size = smb2_wp_retry_size,
1771 .dir_needs_close = smb2_dir_needs_close,
1772 .fallocate = smb3_fallocate,
1775 #ifdef CONFIG_CIFS_SMB311
1776 struct smb_version_operations smb311_operations = {
1777 .compare_fids = smb2_compare_fids,
1778 .setup_request = smb2_setup_request,
1779 .setup_async_request = smb2_setup_async_request,
1780 .check_receive = smb2_check_receive,
1781 .add_credits = smb2_add_credits,
1782 .set_credits = smb2_set_credits,
1783 .get_credits_field = smb2_get_credits_field,
1784 .get_credits = smb2_get_credits,
1785 .wait_mtu_credits = smb2_wait_mtu_credits,
1786 .get_next_mid = smb2_get_next_mid,
1787 .read_data_offset = smb2_read_data_offset,
1788 .read_data_length = smb2_read_data_length,
1789 .map_error = map_smb2_to_linux_error,
1790 .find_mid = smb2_find_mid,
1791 .check_message = smb2_check_message,
1792 .dump_detail = smb2_dump_detail,
1793 .clear_stats = smb2_clear_stats,
1794 .print_stats = smb2_print_stats,
1795 .dump_share_caps = smb2_dump_share_caps,
1796 .is_oplock_break = smb2_is_valid_oplock_break,
1797 .handle_cancelled_mid = smb2_handle_cancelled_mid,
1798 .downgrade_oplock = smb2_downgrade_oplock,
1799 .need_neg = smb2_need_neg,
1800 .negotiate = smb2_negotiate,
1801 .negotiate_wsize = smb2_negotiate_wsize,
1802 .negotiate_rsize = smb2_negotiate_rsize,
1803 .sess_setup = SMB2_sess_setup,
1804 .logoff = SMB2_logoff,
1805 .tree_connect = SMB2_tcon,
1806 .tree_disconnect = SMB2_tdis,
1807 .qfs_tcon = smb3_qfs_tcon,
1808 .is_path_accessible = smb2_is_path_accessible,
1809 .can_echo = smb2_can_echo,
1810 .echo = SMB2_echo,
1811 .query_path_info = smb2_query_path_info,
1812 .get_srv_inum = smb2_get_srv_inum,
1813 .query_file_info = smb2_query_file_info,
1814 .set_path_size = smb2_set_path_size,
1815 .set_file_size = smb2_set_file_size,
1816 .set_file_info = smb2_set_file_info,
1817 .set_compression = smb2_set_compression,
1818 .mkdir = smb2_mkdir,
1819 .mkdir_setinfo = smb2_mkdir_setinfo,
1820 .rmdir = smb2_rmdir,
1821 .unlink = smb2_unlink,
1822 .rename = smb2_rename_path,
1823 .create_hardlink = smb2_create_hardlink,
1824 .query_symlink = smb2_query_symlink,
1825 .query_mf_symlink = smb3_query_mf_symlink,
1826 .create_mf_symlink = smb3_create_mf_symlink,
1827 .open = smb2_open_file,
1828 .set_fid = smb2_set_fid,
1829 .close = smb2_close_file,
1830 .flush = smb2_flush_file,
1831 .async_readv = smb2_async_readv,
1832 .async_writev = smb2_async_writev,
1833 .sync_read = smb2_sync_read,
1834 .sync_write = smb2_sync_write,
1835 .query_dir_first = smb2_query_dir_first,
1836 .query_dir_next = smb2_query_dir_next,
1837 .close_dir = smb2_close_dir,
1838 .calc_smb_size = smb2_calc_size,
1839 .is_status_pending = smb2_is_status_pending,
1840 .is_session_expired = smb2_is_session_expired,
1841 .oplock_response = smb2_oplock_response,
1842 .queryfs = smb2_queryfs,
1843 .mand_lock = smb2_mand_lock,
1844 .mand_unlock_range = smb2_unlock_range,
1845 .push_mand_locks = smb2_push_mandatory_locks,
1846 .get_lease_key = smb2_get_lease_key,
1847 .set_lease_key = smb2_set_lease_key,
1848 .new_lease_key = smb2_new_lease_key,
1849 .generate_signingkey = generate_smb3signingkey,
1850 .calc_signature = smb3_calc_signature,
1851 .set_integrity = smb3_set_integrity,
1852 .is_read_op = smb21_is_read_op,
1853 .set_oplock_level = smb3_set_oplock_level,
1854 .create_lease_buf = smb3_create_lease_buf,
1855 .parse_lease_buf = smb3_parse_lease_buf,
1856 .clone_range = smb2_clone_range,
1857 .duplicate_extents = smb2_duplicate_extents,
1858 /* .validate_negotiate = smb3_validate_negotiate, */ /* not used in 3.11 */
1859 .wp_retry_size = smb2_wp_retry_size,
1860 .dir_needs_close = smb2_dir_needs_close,
1861 .fallocate = smb3_fallocate,
1863 #endif /* CIFS_SMB311 */
1865 struct smb_version_values smb20_values = {
1866 .version_string = SMB20_VERSION_STRING,
1867 .protocol_id = SMB20_PROT_ID,
1868 .req_capabilities = 0, /* MBZ */
1869 .large_lock_type = 0,
1870 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
1871 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
1872 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
1873 .header_size = sizeof(struct smb2_hdr),
1874 .max_header_size = MAX_SMB2_HDR_SIZE,
1875 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
1876 .lock_cmd = SMB2_LOCK,
1877 .cap_unix = 0,
1878 .cap_nt_find = SMB2_NT_FIND,
1879 .cap_large_files = SMB2_LARGE_FILES,
1880 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
1881 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
1882 .create_lease_size = sizeof(struct create_lease),
1885 struct smb_version_values smb21_values = {
1886 .version_string = SMB21_VERSION_STRING,
1887 .protocol_id = SMB21_PROT_ID,
1888 .req_capabilities = 0, /* MBZ on negotiate req until SMB3 dialect */
1889 .large_lock_type = 0,
1890 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
1891 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
1892 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
1893 .header_size = sizeof(struct smb2_hdr),
1894 .max_header_size = MAX_SMB2_HDR_SIZE,
1895 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
1896 .lock_cmd = SMB2_LOCK,
1897 .cap_unix = 0,
1898 .cap_nt_find = SMB2_NT_FIND,
1899 .cap_large_files = SMB2_LARGE_FILES,
1900 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
1901 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
1902 .create_lease_size = sizeof(struct create_lease),
1905 struct smb_version_values smb30_values = {
1906 .version_string = SMB30_VERSION_STRING,
1907 .protocol_id = SMB30_PROT_ID,
1908 .req_capabilities = SMB2_GLOBAL_CAP_DFS | SMB2_GLOBAL_CAP_LEASING | SMB2_GLOBAL_CAP_LARGE_MTU | SMB2_GLOBAL_CAP_PERSISTENT_HANDLES,
1909 .large_lock_type = 0,
1910 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
1911 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
1912 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
1913 .header_size = sizeof(struct smb2_hdr),
1914 .max_header_size = MAX_SMB2_HDR_SIZE,
1915 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
1916 .lock_cmd = SMB2_LOCK,
1917 .cap_unix = 0,
1918 .cap_nt_find = SMB2_NT_FIND,
1919 .cap_large_files = SMB2_LARGE_FILES,
1920 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
1921 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
1922 .create_lease_size = sizeof(struct create_lease_v2),
1925 struct smb_version_values smb302_values = {
1926 .version_string = SMB302_VERSION_STRING,
1927 .protocol_id = SMB302_PROT_ID,
1928 .req_capabilities = SMB2_GLOBAL_CAP_DFS | SMB2_GLOBAL_CAP_LEASING | SMB2_GLOBAL_CAP_LARGE_MTU | SMB2_GLOBAL_CAP_PERSISTENT_HANDLES,
1929 .large_lock_type = 0,
1930 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
1931 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
1932 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
1933 .header_size = sizeof(struct smb2_hdr),
1934 .max_header_size = MAX_SMB2_HDR_SIZE,
1935 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
1936 .lock_cmd = SMB2_LOCK,
1937 .cap_unix = 0,
1938 .cap_nt_find = SMB2_NT_FIND,
1939 .cap_large_files = SMB2_LARGE_FILES,
1940 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
1941 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
1942 .create_lease_size = sizeof(struct create_lease_v2),
1945 #ifdef CONFIG_CIFS_SMB311
1946 struct smb_version_values smb311_values = {
1947 .version_string = SMB311_VERSION_STRING,
1948 .protocol_id = SMB311_PROT_ID,
1949 .req_capabilities = SMB2_GLOBAL_CAP_DFS | SMB2_GLOBAL_CAP_LEASING | SMB2_GLOBAL_CAP_LARGE_MTU | SMB2_GLOBAL_CAP_PERSISTENT_HANDLES,
1950 .large_lock_type = 0,
1951 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
1952 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
1953 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
1954 .header_size = sizeof(struct smb2_hdr),
1955 .max_header_size = MAX_SMB2_HDR_SIZE,
1956 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
1957 .lock_cmd = SMB2_LOCK,
1958 .cap_unix = 0,
1959 .cap_nt_find = SMB2_NT_FIND,
1960 .cap_large_files = SMB2_LARGE_FILES,
1961 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
1962 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
1963 .create_lease_size = sizeof(struct create_lease_v2),
1965 #endif /* SMB311 */