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