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