blob: 101670c2c3742635d4e00c4aa83c78eb892ca76d [file] [log] [blame]
Steve French1080ef72011-02-24 18:07:19 +00001/*
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
Pavel Shilovsky3a3bab52012-09-18 16:20:28 -070020#include <linux/pagemap.h>
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -070021#include <linux/vfs.h>
Steve Frenchf29ebb42014-07-19 21:44:58 -050022#include <linux/falloc.h>
Steve French1080ef72011-02-24 18:07:19 +000023#include "cifsglob.h"
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +040024#include "smb2pdu.h"
25#include "smb2proto.h"
Pavel Shilovsky28ea5292012-05-23 16:18:00 +040026#include "cifsproto.h"
27#include "cifs_debug.h"
Pavel Shilovskyb42bf882013-08-14 19:25:21 +040028#include "cifs_unicode.h"
Pavel Shilovsky2e44b282012-09-18 16:20:33 -070029#include "smb2status.h"
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -070030#include "smb2glob.h"
Pavel Shilovsky28ea5292012-05-23 16:18:00 +040031
32static int
33change_conf(struct TCP_Server_Info *server)
34{
35 server->credits += server->echo_credits + server->oplock_credits;
36 server->oplock_credits = server->echo_credits = 0;
37 switch (server->credits) {
38 case 0:
39 return -1;
40 case 1:
41 server->echoes = false;
42 server->oplocks = false;
Joe Perchesf96637b2013-05-04 22:12:25 -050043 cifs_dbg(VFS, "disabling echoes and oplocks\n");
Pavel Shilovsky28ea5292012-05-23 16:18:00 +040044 break;
45 case 2:
46 server->echoes = true;
47 server->oplocks = false;
48 server->echo_credits = 1;
Joe Perchesf96637b2013-05-04 22:12:25 -050049 cifs_dbg(FYI, "disabling oplocks\n");
Pavel Shilovsky28ea5292012-05-23 16:18:00 +040050 break;
51 default:
52 server->echoes = true;
53 server->oplocks = true;
54 server->echo_credits = 1;
55 server->oplock_credits = 1;
56 }
57 server->credits -= server->echo_credits + server->oplock_credits;
58 return 0;
59}
60
61static void
62smb2_add_credits(struct TCP_Server_Info *server, const unsigned int add,
63 const int optype)
64{
65 int *val, rc = 0;
66 spin_lock(&server->req_lock);
67 val = server->ops->get_credits_field(server, optype);
68 *val += add;
69 server->in_flight--;
Pavel Shilovskyec2e4522011-12-27 16:12:43 +040070 if (server->in_flight == 0 && (optype & CIFS_OP_MASK) != CIFS_NEG_OP)
Pavel Shilovsky28ea5292012-05-23 16:18:00 +040071 rc = change_conf(server);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -070072 /*
73 * Sometimes server returns 0 credits on oplock break ack - we need to
74 * rebalance credits in this case.
75 */
76 else if (server->in_flight > 0 && server->oplock_credits == 0 &&
77 server->oplocks) {
78 if (server->credits > 1) {
79 server->credits--;
80 server->oplock_credits++;
81 }
82 }
Pavel Shilovsky28ea5292012-05-23 16:18:00 +040083 spin_unlock(&server->req_lock);
84 wake_up(&server->request_q);
85 if (rc)
86 cifs_reconnect(server);
87}
88
89static void
90smb2_set_credits(struct TCP_Server_Info *server, const int val)
91{
92 spin_lock(&server->req_lock);
93 server->credits = val;
94 spin_unlock(&server->req_lock);
95}
96
97static int *
98smb2_get_credits_field(struct TCP_Server_Info *server, const int optype)
99{
100 switch (optype) {
101 case CIFS_ECHO_OP:
102 return &server->echo_credits;
103 case CIFS_OBREAK_OP:
104 return &server->oplock_credits;
105 default:
106 return &server->credits;
107 }
108}
109
110static unsigned int
111smb2_get_credits(struct mid_q_entry *mid)
112{
113 return le16_to_cpu(((struct smb2_hdr *)mid->resp_buf)->CreditRequest);
114}
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400115
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +0400116static int
117smb2_wait_mtu_credits(struct TCP_Server_Info *server, unsigned int size,
118 unsigned int *num, unsigned int *credits)
119{
120 int rc = 0;
121 unsigned int scredits;
122
123 spin_lock(&server->req_lock);
124 while (1) {
125 if (server->credits <= 0) {
126 spin_unlock(&server->req_lock);
127 cifs_num_waiters_inc(server);
128 rc = wait_event_killable(server->request_q,
129 has_credits(server, &server->credits));
130 cifs_num_waiters_dec(server);
131 if (rc)
132 return rc;
133 spin_lock(&server->req_lock);
134 } else {
135 if (server->tcpStatus == CifsExiting) {
136 spin_unlock(&server->req_lock);
137 return -ENOENT;
138 }
139
140 scredits = server->credits;
141 /* can deadlock with reopen */
142 if (scredits == 1) {
143 *num = SMB2_MAX_BUFFER_SIZE;
144 *credits = 0;
145 break;
146 }
147
148 /* leave one credit for a possible reopen */
149 scredits--;
150 *num = min_t(unsigned int, size,
151 scredits * SMB2_MAX_BUFFER_SIZE);
152
153 *credits = DIV_ROUND_UP(*num, SMB2_MAX_BUFFER_SIZE);
154 server->credits -= *credits;
155 server->in_flight++;
156 break;
157 }
158 }
159 spin_unlock(&server->req_lock);
160 return rc;
161}
162
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +0400163static __u64
164smb2_get_next_mid(struct TCP_Server_Info *server)
165{
166 __u64 mid;
167 /* for SMB2 we need the current value */
168 spin_lock(&GlobalMid_Lock);
169 mid = server->CurrentMid++;
170 spin_unlock(&GlobalMid_Lock);
171 return mid;
172}
Steve French1080ef72011-02-24 18:07:19 +0000173
Pavel Shilovsky093b2bd2011-06-08 15:51:07 +0400174static struct mid_q_entry *
175smb2_find_mid(struct TCP_Server_Info *server, char *buf)
176{
177 struct mid_q_entry *mid;
178 struct smb2_hdr *hdr = (struct smb2_hdr *)buf;
179
180 spin_lock(&GlobalMid_Lock);
181 list_for_each_entry(mid, &server->pending_mid_q, qhead) {
182 if ((mid->mid == hdr->MessageId) &&
183 (mid->mid_state == MID_REQUEST_SUBMITTED) &&
184 (mid->command == hdr->Command)) {
185 spin_unlock(&GlobalMid_Lock);
186 return mid;
187 }
188 }
189 spin_unlock(&GlobalMid_Lock);
190 return NULL;
191}
192
193static void
194smb2_dump_detail(void *buf)
195{
196#ifdef CONFIG_CIFS_DEBUG2
197 struct smb2_hdr *smb = (struct smb2_hdr *)buf;
198
Joe Perchesf96637b2013-05-04 22:12:25 -0500199 cifs_dbg(VFS, "Cmd: %d Err: 0x%x Flags: 0x%x Mid: %llu Pid: %d\n",
200 smb->Command, smb->Status, smb->Flags, smb->MessageId,
201 smb->ProcessId);
202 cifs_dbg(VFS, "smb buf %p len %u\n", smb, smb2_calc_size(smb));
Pavel Shilovsky093b2bd2011-06-08 15:51:07 +0400203#endif
204}
205
Pavel Shilovskyec2e4522011-12-27 16:12:43 +0400206static bool
207smb2_need_neg(struct TCP_Server_Info *server)
208{
209 return server->max_read == 0;
210}
211
212static int
213smb2_negotiate(const unsigned int xid, struct cifs_ses *ses)
214{
215 int rc;
216 ses->server->CurrentMid = 0;
217 rc = SMB2_negotiate(xid, ses);
218 /* BB we probably don't need to retry with modern servers */
219 if (rc == -EAGAIN)
220 rc = -EHOSTDOWN;
221 return rc;
222}
223
Pavel Shilovsky3a3bab52012-09-18 16:20:28 -0700224static unsigned int
225smb2_negotiate_wsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
226{
227 struct TCP_Server_Info *server = tcon->ses->server;
228 unsigned int wsize;
229
230 /* start with specified wsize, or default */
231 wsize = volume_info->wsize ? volume_info->wsize : CIFS_DEFAULT_IOSIZE;
232 wsize = min_t(unsigned int, wsize, server->max_write);
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +0400233
234 if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
235 wsize = min_t(unsigned int, wsize, SMB2_MAX_BUFFER_SIZE);
Pavel Shilovsky3a3bab52012-09-18 16:20:28 -0700236
Pavel Shilovsky3a3bab52012-09-18 16:20:28 -0700237 return wsize;
238}
239
240static unsigned int
241smb2_negotiate_rsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
242{
243 struct TCP_Server_Info *server = tcon->ses->server;
244 unsigned int rsize;
245
246 /* start with specified rsize, or default */
247 rsize = volume_info->rsize ? volume_info->rsize : CIFS_DEFAULT_IOSIZE;
248 rsize = min_t(unsigned int, rsize, server->max_read);
Pavel Shilovskybed9da02014-06-25 11:28:57 +0400249
250 if (!(server->capabilities & SMB2_GLOBAL_CAP_LARGE_MTU))
251 rsize = min_t(unsigned int, rsize, SMB2_MAX_BUFFER_SIZE);
Pavel Shilovsky3a3bab52012-09-18 16:20:28 -0700252
Pavel Shilovsky3a3bab52012-09-18 16:20:28 -0700253 return rsize;
254}
255
Steve Frenchc481e9f2013-10-14 01:21:53 -0500256#ifdef CONFIG_CIFS_STATS2
257static int
258SMB3_request_interfaces(const unsigned int xid, struct cifs_tcon *tcon)
259{
260 int rc;
261 unsigned int ret_data_len = 0;
262 struct network_interface_info_ioctl_rsp *out_buf;
263
264 rc = SMB2_ioctl(xid, tcon, NO_FILE_ID, NO_FILE_ID,
265 FSCTL_QUERY_NETWORK_INTERFACE_INFO, true /* is_fsctl */,
266 NULL /* no data input */, 0 /* no data input */,
267 (char **)&out_buf, &ret_data_len);
268
269 if ((rc == 0) && (ret_data_len > 0)) {
270 /* Dump info on first interface */
271 cifs_dbg(FYI, "Adapter Capability 0x%x\t",
272 le32_to_cpu(out_buf->Capability));
273 cifs_dbg(FYI, "Link Speed %lld\n",
274 le64_to_cpu(out_buf->LinkSpeed));
275 } else
276 cifs_dbg(VFS, "error %d on ioctl to get interface list\n", rc);
277
278 return rc;
279}
280#endif /* STATS2 */
281
Steve French34f62642013-10-09 02:07:00 -0500282static void
Steven Frenchaf6a12e2013-10-09 20:55:53 -0500283smb3_qfs_tcon(const unsigned int xid, struct cifs_tcon *tcon)
284{
285 int rc;
286 __le16 srch_path = 0; /* Null - open root of share */
287 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
288 struct cifs_open_parms oparms;
289 struct cifs_fid fid;
290
291 oparms.tcon = tcon;
292 oparms.desired_access = FILE_READ_ATTRIBUTES;
293 oparms.disposition = FILE_OPEN;
294 oparms.create_options = 0;
295 oparms.fid = &fid;
296 oparms.reconnect = false;
297
298 rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL);
299 if (rc)
300 return;
301
Steve Frenchc481e9f2013-10-14 01:21:53 -0500302#ifdef CONFIG_CIFS_STATS2
303 SMB3_request_interfaces(xid, tcon);
304#endif /* STATS2 */
305
Steven Frenchaf6a12e2013-10-09 20:55:53 -0500306 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
307 FS_ATTRIBUTE_INFORMATION);
308 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
309 FS_DEVICE_INFORMATION);
310 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
311 FS_SECTOR_SIZE_INFORMATION); /* SMB3 specific */
312 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
313 return;
314}
315
316static void
Steve French34f62642013-10-09 02:07:00 -0500317smb2_qfs_tcon(const unsigned int xid, struct cifs_tcon *tcon)
318{
319 int rc;
320 __le16 srch_path = 0; /* Null - open root of share */
321 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
322 struct cifs_open_parms oparms;
323 struct cifs_fid fid;
324
325 oparms.tcon = tcon;
326 oparms.desired_access = FILE_READ_ATTRIBUTES;
327 oparms.disposition = FILE_OPEN;
328 oparms.create_options = 0;
329 oparms.fid = &fid;
330 oparms.reconnect = false;
331
332 rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL);
333 if (rc)
334 return;
335
Steven French21671142013-10-09 13:36:35 -0500336 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
337 FS_ATTRIBUTE_INFORMATION);
338 SMB2_QFS_attr(xid, tcon, fid.persistent_fid, fid.volatile_fid,
339 FS_DEVICE_INFORMATION);
Steve French34f62642013-10-09 02:07:00 -0500340 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
341 return;
342}
343
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +0400344static int
345smb2_is_path_accessible(const unsigned int xid, struct cifs_tcon *tcon,
346 struct cifs_sb_info *cifs_sb, const char *full_path)
347{
348 int rc;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +0400349 __le16 *utf16_path;
Pavel Shilovsky2e44b282012-09-18 16:20:33 -0700350 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
Pavel Shilovsky064f6042013-07-09 18:20:30 +0400351 struct cifs_open_parms oparms;
352 struct cifs_fid fid;
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +0400353
354 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
355 if (!utf16_path)
356 return -ENOMEM;
357
Pavel Shilovsky064f6042013-07-09 18:20:30 +0400358 oparms.tcon = tcon;
359 oparms.desired_access = FILE_READ_ATTRIBUTES;
360 oparms.disposition = FILE_OPEN;
361 oparms.create_options = 0;
362 oparms.fid = &fid;
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +0400363 oparms.reconnect = false;
Pavel Shilovsky064f6042013-07-09 18:20:30 +0400364
Pavel Shilovskyb42bf882013-08-14 19:25:21 +0400365 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +0400366 if (rc) {
367 kfree(utf16_path);
368 return rc;
369 }
370
Pavel Shilovsky064f6042013-07-09 18:20:30 +0400371 rc = SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +0400372 kfree(utf16_path);
373 return rc;
374}
375
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +0400376static int
377smb2_get_srv_inum(const unsigned int xid, struct cifs_tcon *tcon,
378 struct cifs_sb_info *cifs_sb, const char *full_path,
379 u64 *uniqueid, FILE_ALL_INFO *data)
380{
381 *uniqueid = le64_to_cpu(data->IndexNumber);
382 return 0;
383}
384
Pavel Shilovskyb7546bc2012-09-18 16:20:27 -0700385static int
386smb2_query_file_info(const unsigned int xid, struct cifs_tcon *tcon,
387 struct cifs_fid *fid, FILE_ALL_INFO *data)
388{
389 int rc;
390 struct smb2_file_all_info *smb2_data;
391
392 smb2_data = kzalloc(sizeof(struct smb2_file_all_info) + MAX_NAME * 2,
393 GFP_KERNEL);
394 if (smb2_data == NULL)
395 return -ENOMEM;
396
397 rc = SMB2_query_info(xid, tcon, fid->persistent_fid, fid->volatile_fid,
398 smb2_data);
399 if (!rc)
400 move_smb2_info_to_cifs(data, smb2_data);
401 kfree(smb2_data);
402 return rc;
403}
404
Pavel Shilovsky9094fad2012-07-12 18:30:44 +0400405static bool
406smb2_can_echo(struct TCP_Server_Info *server)
407{
408 return server->echoes;
409}
410
Pavel Shilovskyd60622e2012-05-28 15:19:39 +0400411static void
412smb2_clear_stats(struct cifs_tcon *tcon)
413{
414#ifdef CONFIG_CIFS_STATS
415 int i;
416 for (i = 0; i < NUMBER_OF_SMB2_COMMANDS; i++) {
417 atomic_set(&tcon->stats.smb2_stats.smb2_com_sent[i], 0);
418 atomic_set(&tcon->stats.smb2_stats.smb2_com_failed[i], 0);
419 }
420#endif
421}
422
423static void
Steve French769ee6a2013-06-19 14:15:30 -0500424smb2_dump_share_caps(struct seq_file *m, struct cifs_tcon *tcon)
425{
426 seq_puts(m, "\n\tShare Capabilities:");
427 if (tcon->capabilities & SMB2_SHARE_CAP_DFS)
428 seq_puts(m, " DFS,");
429 if (tcon->capabilities & SMB2_SHARE_CAP_CONTINUOUS_AVAILABILITY)
430 seq_puts(m, " CONTINUOUS AVAILABILITY,");
431 if (tcon->capabilities & SMB2_SHARE_CAP_SCALEOUT)
432 seq_puts(m, " SCALEOUT,");
433 if (tcon->capabilities & SMB2_SHARE_CAP_CLUSTER)
434 seq_puts(m, " CLUSTER,");
435 if (tcon->capabilities & SMB2_SHARE_CAP_ASYMMETRIC)
436 seq_puts(m, " ASYMMETRIC,");
437 if (tcon->capabilities == 0)
438 seq_puts(m, " None");
Steven Frenchaf6a12e2013-10-09 20:55:53 -0500439 if (tcon->ss_flags & SSINFO_FLAGS_ALIGNED_DEVICE)
440 seq_puts(m, " Aligned,");
441 if (tcon->ss_flags & SSINFO_FLAGS_PARTITION_ALIGNED_ON_DEVICE)
442 seq_puts(m, " Partition Aligned,");
443 if (tcon->ss_flags & SSINFO_FLAGS_NO_SEEK_PENALTY)
444 seq_puts(m, " SSD,");
445 if (tcon->ss_flags & SSINFO_FLAGS_TRIM_ENABLED)
446 seq_puts(m, " TRIM-support,");
447
Steve French769ee6a2013-06-19 14:15:30 -0500448 seq_printf(m, "\tShare Flags: 0x%x", tcon->share_flags);
Steven Frenchaf6a12e2013-10-09 20:55:53 -0500449 if (tcon->perf_sector_size)
450 seq_printf(m, "\tOptimal sector size: 0x%x",
451 tcon->perf_sector_size);
Steve French769ee6a2013-06-19 14:15:30 -0500452}
453
454static void
Pavel Shilovskyd60622e2012-05-28 15:19:39 +0400455smb2_print_stats(struct seq_file *m, struct cifs_tcon *tcon)
456{
457#ifdef CONFIG_CIFS_STATS
458 atomic_t *sent = tcon->stats.smb2_stats.smb2_com_sent;
459 atomic_t *failed = tcon->stats.smb2_stats.smb2_com_failed;
460 seq_printf(m, "\nNegotiates: %d sent %d failed",
461 atomic_read(&sent[SMB2_NEGOTIATE_HE]),
462 atomic_read(&failed[SMB2_NEGOTIATE_HE]));
463 seq_printf(m, "\nSessionSetups: %d sent %d failed",
464 atomic_read(&sent[SMB2_SESSION_SETUP_HE]),
465 atomic_read(&failed[SMB2_SESSION_SETUP_HE]));
Pavel Shilovskyd60622e2012-05-28 15:19:39 +0400466 seq_printf(m, "\nLogoffs: %d sent %d failed",
467 atomic_read(&sent[SMB2_LOGOFF_HE]),
468 atomic_read(&failed[SMB2_LOGOFF_HE]));
469 seq_printf(m, "\nTreeConnects: %d sent %d failed",
470 atomic_read(&sent[SMB2_TREE_CONNECT_HE]),
471 atomic_read(&failed[SMB2_TREE_CONNECT_HE]));
472 seq_printf(m, "\nTreeDisconnects: %d sent %d failed",
473 atomic_read(&sent[SMB2_TREE_DISCONNECT_HE]),
474 atomic_read(&failed[SMB2_TREE_DISCONNECT_HE]));
475 seq_printf(m, "\nCreates: %d sent %d failed",
476 atomic_read(&sent[SMB2_CREATE_HE]),
477 atomic_read(&failed[SMB2_CREATE_HE]));
478 seq_printf(m, "\nCloses: %d sent %d failed",
479 atomic_read(&sent[SMB2_CLOSE_HE]),
480 atomic_read(&failed[SMB2_CLOSE_HE]));
481 seq_printf(m, "\nFlushes: %d sent %d failed",
482 atomic_read(&sent[SMB2_FLUSH_HE]),
483 atomic_read(&failed[SMB2_FLUSH_HE]));
484 seq_printf(m, "\nReads: %d sent %d failed",
485 atomic_read(&sent[SMB2_READ_HE]),
486 atomic_read(&failed[SMB2_READ_HE]));
487 seq_printf(m, "\nWrites: %d sent %d failed",
488 atomic_read(&sent[SMB2_WRITE_HE]),
489 atomic_read(&failed[SMB2_WRITE_HE]));
490 seq_printf(m, "\nLocks: %d sent %d failed",
491 atomic_read(&sent[SMB2_LOCK_HE]),
492 atomic_read(&failed[SMB2_LOCK_HE]));
493 seq_printf(m, "\nIOCTLs: %d sent %d failed",
494 atomic_read(&sent[SMB2_IOCTL_HE]),
495 atomic_read(&failed[SMB2_IOCTL_HE]));
496 seq_printf(m, "\nCancels: %d sent %d failed",
497 atomic_read(&sent[SMB2_CANCEL_HE]),
498 atomic_read(&failed[SMB2_CANCEL_HE]));
499 seq_printf(m, "\nEchos: %d sent %d failed",
500 atomic_read(&sent[SMB2_ECHO_HE]),
501 atomic_read(&failed[SMB2_ECHO_HE]));
502 seq_printf(m, "\nQueryDirectories: %d sent %d failed",
503 atomic_read(&sent[SMB2_QUERY_DIRECTORY_HE]),
504 atomic_read(&failed[SMB2_QUERY_DIRECTORY_HE]));
505 seq_printf(m, "\nChangeNotifies: %d sent %d failed",
506 atomic_read(&sent[SMB2_CHANGE_NOTIFY_HE]),
507 atomic_read(&failed[SMB2_CHANGE_NOTIFY_HE]));
508 seq_printf(m, "\nQueryInfos: %d sent %d failed",
509 atomic_read(&sent[SMB2_QUERY_INFO_HE]),
510 atomic_read(&failed[SMB2_QUERY_INFO_HE]));
511 seq_printf(m, "\nSetInfos: %d sent %d failed",
512 atomic_read(&sent[SMB2_SET_INFO_HE]),
513 atomic_read(&failed[SMB2_SET_INFO_HE]));
514 seq_printf(m, "\nOplockBreaks: %d sent %d failed",
515 atomic_read(&sent[SMB2_OPLOCK_BREAK_HE]),
516 atomic_read(&failed[SMB2_OPLOCK_BREAK_HE]));
517#endif
518}
519
Pavel Shilovskyf0df7372012-09-18 16:20:26 -0700520static void
521smb2_set_fid(struct cifsFileInfo *cfile, struct cifs_fid *fid, __u32 oplock)
522{
Pavel Shilovsky2e44b282012-09-18 16:20:33 -0700523 struct cifsInodeInfo *cinode = CIFS_I(cfile->dentry->d_inode);
Pavel Shilovsky53ef1012013-09-05 16:11:28 +0400524 struct TCP_Server_Info *server = tlink_tcon(cfile->tlink)->ses->server;
525
Pavel Shilovskyf0df7372012-09-18 16:20:26 -0700526 cfile->fid.persistent_fid = fid->persistent_fid;
527 cfile->fid.volatile_fid = fid->volatile_fid;
Pavel Shilovsky42873b02013-09-05 21:30:16 +0400528 server->ops->set_oplock_level(cinode, oplock, fid->epoch,
529 &fid->purge_cache);
Pavel Shilovsky18cceb62013-09-05 13:01:06 +0400530 cinode->can_cache_brlcks = CIFS_CACHE_WRITE(cinode);
Pavel Shilovskyf0df7372012-09-18 16:20:26 -0700531}
532
Pavel Shilovsky760ad0c2012-09-25 11:00:07 +0400533static void
Pavel Shilovskyf0df7372012-09-18 16:20:26 -0700534smb2_close_file(const unsigned int xid, struct cifs_tcon *tcon,
535 struct cifs_fid *fid)
536{
Pavel Shilovsky760ad0c2012-09-25 11:00:07 +0400537 SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
Pavel Shilovskyf0df7372012-09-18 16:20:26 -0700538}
539
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -0700540static int
Steve French41c13582013-11-14 00:05:36 -0600541SMB2_request_res_key(const unsigned int xid, struct cifs_tcon *tcon,
542 u64 persistent_fid, u64 volatile_fid,
543 struct copychunk_ioctl *pcchunk)
544{
545 int rc;
546 unsigned int ret_data_len;
547 struct resume_key_req *res_key;
548
549 rc = SMB2_ioctl(xid, tcon, persistent_fid, volatile_fid,
550 FSCTL_SRV_REQUEST_RESUME_KEY, true /* is_fsctl */,
551 NULL, 0 /* no input */,
552 (char **)&res_key, &ret_data_len);
553
554 if (rc) {
555 cifs_dbg(VFS, "refcpy ioctl error %d getting resume key\n", rc);
556 goto req_res_key_exit;
557 }
558 if (ret_data_len < sizeof(struct resume_key_req)) {
559 cifs_dbg(VFS, "Invalid refcopy resume key length\n");
560 rc = -EINVAL;
561 goto req_res_key_exit;
562 }
563 memcpy(pcchunk->SourceKey, res_key->ResumeKey, COPY_CHUNK_RES_KEY_SIZE);
564
565req_res_key_exit:
566 kfree(res_key);
567 return rc;
568}
569
570static int
571smb2_clone_range(const unsigned int xid,
572 struct cifsFileInfo *srcfile,
573 struct cifsFileInfo *trgtfile, u64 src_off,
574 u64 len, u64 dest_off)
575{
576 int rc;
577 unsigned int ret_data_len;
578 struct copychunk_ioctl *pcchunk;
Steve French9bf0c9c2013-11-16 18:05:28 -0600579 struct copychunk_ioctl_rsp *retbuf = NULL;
580 struct cifs_tcon *tcon;
581 int chunks_copied = 0;
582 bool chunk_sizes_updated = false;
Steve French41c13582013-11-14 00:05:36 -0600583
584 pcchunk = kmalloc(sizeof(struct copychunk_ioctl), GFP_KERNEL);
585
586 if (pcchunk == NULL)
587 return -ENOMEM;
588
589 cifs_dbg(FYI, "in smb2_clone_range - about to call request res key\n");
590 /* Request a key from the server to identify the source of the copy */
591 rc = SMB2_request_res_key(xid, tlink_tcon(srcfile->tlink),
592 srcfile->fid.persistent_fid,
593 srcfile->fid.volatile_fid, pcchunk);
594
595 /* Note: request_res_key sets res_key null only if rc !=0 */
596 if (rc)
Steve French9bf0c9c2013-11-16 18:05:28 -0600597 goto cchunk_out;
Steve French41c13582013-11-14 00:05:36 -0600598
599 /* For now array only one chunk long, will make more flexible later */
600 pcchunk->ChunkCount = __constant_cpu_to_le32(1);
601 pcchunk->Reserved = 0;
Steve French41c13582013-11-14 00:05:36 -0600602 pcchunk->Reserved2 = 0;
603
Steve French9bf0c9c2013-11-16 18:05:28 -0600604 tcon = tlink_tcon(trgtfile->tlink);
605
606 while (len > 0) {
607 pcchunk->SourceOffset = cpu_to_le64(src_off);
608 pcchunk->TargetOffset = cpu_to_le64(dest_off);
609 pcchunk->Length =
610 cpu_to_le32(min_t(u32, len, tcon->max_bytes_chunk));
611
612 /* Request server copy to target from src identified by key */
613 rc = SMB2_ioctl(xid, tcon, trgtfile->fid.persistent_fid,
Steve French41c13582013-11-14 00:05:36 -0600614 trgtfile->fid.volatile_fid, FSCTL_SRV_COPYCHUNK_WRITE,
615 true /* is_fsctl */, (char *)pcchunk,
Steve French9bf0c9c2013-11-16 18:05:28 -0600616 sizeof(struct copychunk_ioctl), (char **)&retbuf,
617 &ret_data_len);
618 if (rc == 0) {
619 if (ret_data_len !=
620 sizeof(struct copychunk_ioctl_rsp)) {
621 cifs_dbg(VFS, "invalid cchunk response size\n");
622 rc = -EIO;
623 goto cchunk_out;
624 }
625 if (retbuf->TotalBytesWritten == 0) {
626 cifs_dbg(FYI, "no bytes copied\n");
627 rc = -EIO;
628 goto cchunk_out;
629 }
630 /*
631 * Check if server claimed to write more than we asked
632 */
633 if (le32_to_cpu(retbuf->TotalBytesWritten) >
634 le32_to_cpu(pcchunk->Length)) {
635 cifs_dbg(VFS, "invalid copy chunk response\n");
636 rc = -EIO;
637 goto cchunk_out;
638 }
639 if (le32_to_cpu(retbuf->ChunksWritten) != 1) {
640 cifs_dbg(VFS, "invalid num chunks written\n");
641 rc = -EIO;
642 goto cchunk_out;
643 }
644 chunks_copied++;
Steve French41c13582013-11-14 00:05:36 -0600645
Steve French9bf0c9c2013-11-16 18:05:28 -0600646 src_off += le32_to_cpu(retbuf->TotalBytesWritten);
647 dest_off += le32_to_cpu(retbuf->TotalBytesWritten);
648 len -= le32_to_cpu(retbuf->TotalBytesWritten);
Steve French41c13582013-11-14 00:05:36 -0600649
Steve French9bf0c9c2013-11-16 18:05:28 -0600650 cifs_dbg(FYI, "Chunks %d PartialChunk %d Total %d\n",
651 le32_to_cpu(retbuf->ChunksWritten),
652 le32_to_cpu(retbuf->ChunkBytesWritten),
653 le32_to_cpu(retbuf->TotalBytesWritten));
654 } else if (rc == -EINVAL) {
655 if (ret_data_len != sizeof(struct copychunk_ioctl_rsp))
656 goto cchunk_out;
Steve French41c13582013-11-14 00:05:36 -0600657
Steve French9bf0c9c2013-11-16 18:05:28 -0600658 cifs_dbg(FYI, "MaxChunks %d BytesChunk %d MaxCopy %d\n",
659 le32_to_cpu(retbuf->ChunksWritten),
660 le32_to_cpu(retbuf->ChunkBytesWritten),
661 le32_to_cpu(retbuf->TotalBytesWritten));
662
663 /*
664 * Check if this is the first request using these sizes,
665 * (ie check if copy succeed once with original sizes
666 * and check if the server gave us different sizes after
667 * we already updated max sizes on previous request).
668 * if not then why is the server returning an error now
669 */
670 if ((chunks_copied != 0) || chunk_sizes_updated)
671 goto cchunk_out;
672
673 /* Check that server is not asking us to grow size */
674 if (le32_to_cpu(retbuf->ChunkBytesWritten) <
675 tcon->max_bytes_chunk)
676 tcon->max_bytes_chunk =
677 le32_to_cpu(retbuf->ChunkBytesWritten);
678 else
679 goto cchunk_out; /* server gave us bogus size */
680
681 /* No need to change MaxChunks since already set to 1 */
682 chunk_sizes_updated = true;
683 }
684 }
685
686cchunk_out:
Steve French41c13582013-11-14 00:05:36 -0600687 kfree(pcchunk);
688 return rc;
689}
690
691static int
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -0700692smb2_flush_file(const unsigned int xid, struct cifs_tcon *tcon,
693 struct cifs_fid *fid)
694{
695 return SMB2_flush(xid, tcon, fid->persistent_fid, fid->volatile_fid);
696}
697
Pavel Shilovsky09a47072012-09-18 16:20:29 -0700698static unsigned int
699smb2_read_data_offset(char *buf)
700{
701 struct smb2_read_rsp *rsp = (struct smb2_read_rsp *)buf;
702 return rsp->DataOffset;
703}
704
705static unsigned int
706smb2_read_data_length(char *buf)
707{
708 struct smb2_read_rsp *rsp = (struct smb2_read_rsp *)buf;
709 return le32_to_cpu(rsp->DataLength);
710}
711
Pavel Shilovskyd8e05032012-09-18 16:20:30 -0700712
713static int
714smb2_sync_read(const unsigned int xid, struct cifsFileInfo *cfile,
715 struct cifs_io_parms *parms, unsigned int *bytes_read,
716 char **buf, int *buf_type)
717{
718 parms->persistent_fid = cfile->fid.persistent_fid;
719 parms->volatile_fid = cfile->fid.volatile_fid;
720 return SMB2_read(xid, parms, bytes_read, buf, buf_type);
721}
722
Pavel Shilovsky009d3442012-09-18 16:20:30 -0700723static int
724smb2_sync_write(const unsigned int xid, struct cifsFileInfo *cfile,
725 struct cifs_io_parms *parms, unsigned int *written,
726 struct kvec *iov, unsigned long nr_segs)
727{
728
729 parms->persistent_fid = cfile->fid.persistent_fid;
730 parms->volatile_fid = cfile->fid.volatile_fid;
731 return SMB2_write(xid, parms, written, iov, nr_segs);
732}
733
Steve Frenchd43cc792014-08-13 17:16:29 -0500734/* Set or clear the SPARSE_FILE attribute based on value passed in setsparse */
735static bool smb2_set_sparse(const unsigned int xid, struct cifs_tcon *tcon,
736 struct cifsFileInfo *cfile, struct inode *inode, __u8 setsparse)
737{
738 struct cifsInodeInfo *cifsi;
739 int rc;
740
741 cifsi = CIFS_I(inode);
742
743 /* if file already sparse don't bother setting sparse again */
744 if ((cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) && setsparse)
745 return true; /* already sparse */
746
747 if (!(cifsi->cifsAttrs & FILE_ATTRIBUTE_SPARSE_FILE) && !setsparse)
748 return true; /* already not sparse */
749
750 /*
751 * Can't check for sparse support on share the usual way via the
752 * FS attribute info (FILE_SUPPORTS_SPARSE_FILES) on the share
753 * since Samba server doesn't set the flag on the share, yet
754 * supports the set sparse FSCTL and returns sparse correctly
755 * in the file attributes. If we fail setting sparse though we
756 * mark that server does not support sparse files for this share
757 * to avoid repeatedly sending the unsupported fsctl to server
758 * if the file is repeatedly extended.
759 */
760 if (tcon->broken_sparse_sup)
761 return false;
762
763 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
764 cfile->fid.volatile_fid, FSCTL_SET_SPARSE,
765 true /* is_fctl */, &setsparse, 1, NULL, NULL);
766 if (rc) {
767 tcon->broken_sparse_sup = true;
768 cifs_dbg(FYI, "set sparse rc = %d\n", rc);
769 return false;
770 }
771
772 if (setsparse)
773 cifsi->cifsAttrs |= FILE_ATTRIBUTE_SPARSE_FILE;
774 else
775 cifsi->cifsAttrs &= (~FILE_ATTRIBUTE_SPARSE_FILE);
776
777 return true;
778}
779
Pavel Shilovskyc839ff22012-09-18 16:20:32 -0700780static int
781smb2_set_file_size(const unsigned int xid, struct cifs_tcon *tcon,
782 struct cifsFileInfo *cfile, __u64 size, bool set_alloc)
783{
784 __le64 eof = cpu_to_le64(size);
Steve French3d1a3742014-08-11 21:05:25 -0500785 struct inode *inode;
786
787 /*
788 * If extending file more than one page make sparse. Many Linux fs
789 * make files sparse by default when extending via ftruncate
790 */
791 inode = cfile->dentry->d_inode;
792
793 if (!set_alloc && (size > inode->i_size + 8192)) {
Steve French3d1a3742014-08-11 21:05:25 -0500794 __u8 set_sparse = 1;
Steve French3d1a3742014-08-11 21:05:25 -0500795
Steve Frenchd43cc792014-08-13 17:16:29 -0500796 /* whether set sparse succeeds or not, extend the file */
797 smb2_set_sparse(xid, tcon, cfile, inode, set_sparse);
Steve French3d1a3742014-08-11 21:05:25 -0500798 }
799
Pavel Shilovskyc839ff22012-09-18 16:20:32 -0700800 return SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
Steve Frenchf29ebb42014-07-19 21:44:58 -0500801 cfile->fid.volatile_fid, cfile->pid, &eof, false);
Pavel Shilovskyc839ff22012-09-18 16:20:32 -0700802}
803
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -0700804static int
Steve French64a5cfa2013-10-14 15:31:32 -0500805smb2_set_compression(const unsigned int xid, struct cifs_tcon *tcon,
806 struct cifsFileInfo *cfile)
807{
808 return SMB2_set_compression(xid, tcon, cfile->fid.persistent_fid,
809 cfile->fid.volatile_fid);
810}
811
812static int
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -0700813smb2_query_dir_first(const unsigned int xid, struct cifs_tcon *tcon,
814 const char *path, struct cifs_sb_info *cifs_sb,
815 struct cifs_fid *fid, __u16 search_flags,
816 struct cifs_search_info *srch_inf)
817{
818 __le16 *utf16_path;
819 int rc;
Pavel Shilovsky2e44b282012-09-18 16:20:33 -0700820 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
Pavel Shilovsky064f6042013-07-09 18:20:30 +0400821 struct cifs_open_parms oparms;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -0700822
823 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
824 if (!utf16_path)
825 return -ENOMEM;
826
Pavel Shilovsky064f6042013-07-09 18:20:30 +0400827 oparms.tcon = tcon;
828 oparms.desired_access = FILE_READ_ATTRIBUTES | FILE_READ_DATA;
829 oparms.disposition = FILE_OPEN;
830 oparms.create_options = 0;
831 oparms.fid = fid;
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +0400832 oparms.reconnect = false;
Pavel Shilovsky064f6042013-07-09 18:20:30 +0400833
Pavel Shilovskyb42bf882013-08-14 19:25:21 +0400834 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, NULL);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -0700835 kfree(utf16_path);
836 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500837 cifs_dbg(VFS, "open dir failed\n");
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -0700838 return rc;
839 }
840
841 srch_inf->entries_in_buffer = 0;
842 srch_inf->index_of_last_entry = 0;
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -0700843
Pavel Shilovsky064f6042013-07-09 18:20:30 +0400844 rc = SMB2_query_directory(xid, tcon, fid->persistent_fid,
845 fid->volatile_fid, 0, srch_inf);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -0700846 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500847 cifs_dbg(VFS, "query directory failed\n");
Pavel Shilovsky064f6042013-07-09 18:20:30 +0400848 SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -0700849 }
850 return rc;
851}
852
853static int
854smb2_query_dir_next(const unsigned int xid, struct cifs_tcon *tcon,
855 struct cifs_fid *fid, __u16 search_flags,
856 struct cifs_search_info *srch_inf)
857{
858 return SMB2_query_directory(xid, tcon, fid->persistent_fid,
859 fid->volatile_fid, 0, srch_inf);
860}
861
862static int
863smb2_close_dir(const unsigned int xid, struct cifs_tcon *tcon,
864 struct cifs_fid *fid)
865{
866 return SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
867}
868
Pavel Shilovsky2e44b282012-09-18 16:20:33 -0700869/*
870* If we negotiate SMB2 protocol and get STATUS_PENDING - update
871* the number of credits and return true. Otherwise - return false.
872*/
873static bool
874smb2_is_status_pending(char *buf, struct TCP_Server_Info *server, int length)
875{
876 struct smb2_hdr *hdr = (struct smb2_hdr *)buf;
877
Steve French12e8a202012-09-19 09:19:39 -0700878 if (hdr->Status != STATUS_PENDING)
Pavel Shilovsky2e44b282012-09-18 16:20:33 -0700879 return false;
880
881 if (!length) {
882 spin_lock(&server->req_lock);
883 server->credits += le16_to_cpu(hdr->CreditRequest);
884 spin_unlock(&server->req_lock);
885 wake_up(&server->request_q);
886 }
887
888 return true;
889}
890
Pavel Shilovsky983c88a2012-09-18 16:20:33 -0700891static int
892smb2_oplock_response(struct cifs_tcon *tcon, struct cifs_fid *fid,
893 struct cifsInodeInfo *cinode)
894{
Pavel Shilovsky0822f512012-09-19 06:22:45 -0700895 if (tcon->ses->server->capabilities & SMB2_GLOBAL_CAP_LEASING)
896 return SMB2_lease_break(0, tcon, cinode->lease_key,
897 smb2_get_lease_state(cinode));
898
Pavel Shilovsky983c88a2012-09-18 16:20:33 -0700899 return SMB2_oplock_break(0, tcon, fid->persistent_fid,
900 fid->volatile_fid,
Pavel Shilovsky18cceb62013-09-05 13:01:06 +0400901 CIFS_CACHE_READ(cinode) ? 1 : 0);
Pavel Shilovsky983c88a2012-09-18 16:20:33 -0700902}
903
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -0700904static int
905smb2_queryfs(const unsigned int xid, struct cifs_tcon *tcon,
906 struct kstatfs *buf)
907{
908 int rc;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -0700909 __le16 srch_path = 0; /* Null - open root of share */
910 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
Pavel Shilovsky064f6042013-07-09 18:20:30 +0400911 struct cifs_open_parms oparms;
912 struct cifs_fid fid;
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -0700913
Pavel Shilovsky064f6042013-07-09 18:20:30 +0400914 oparms.tcon = tcon;
915 oparms.desired_access = FILE_READ_ATTRIBUTES;
916 oparms.disposition = FILE_OPEN;
917 oparms.create_options = 0;
918 oparms.fid = &fid;
Pavel Shilovsky9cbc0b72013-07-09 18:40:58 +0400919 oparms.reconnect = false;
Pavel Shilovsky064f6042013-07-09 18:20:30 +0400920
Pavel Shilovskyb42bf882013-08-14 19:25:21 +0400921 rc = SMB2_open(xid, &oparms, &srch_path, &oplock, NULL, NULL);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -0700922 if (rc)
923 return rc;
924 buf->f_type = SMB2_MAGIC_NUMBER;
Pavel Shilovsky064f6042013-07-09 18:20:30 +0400925 rc = SMB2_QFS_info(xid, tcon, fid.persistent_fid, fid.volatile_fid,
926 buf);
927 SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -0700928 return rc;
929}
930
Pavel Shilovsky027e8ee2012-09-19 06:22:43 -0700931static bool
932smb2_compare_fids(struct cifsFileInfo *ob1, struct cifsFileInfo *ob2)
933{
934 return ob1->fid.persistent_fid == ob2->fid.persistent_fid &&
935 ob1->fid.volatile_fid == ob2->fid.volatile_fid;
936}
937
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -0700938static int
939smb2_mand_lock(const unsigned int xid, struct cifsFileInfo *cfile, __u64 offset,
940 __u64 length, __u32 type, int lock, int unlock, bool wait)
941{
942 if (unlock && !lock)
943 type = SMB2_LOCKFLAG_UNLOCK;
944 return SMB2_lock(xid, tlink_tcon(cfile->tlink),
945 cfile->fid.persistent_fid, cfile->fid.volatile_fid,
946 current->tgid, length, offset, type, wait);
947}
948
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -0700949static void
950smb2_get_lease_key(struct inode *inode, struct cifs_fid *fid)
951{
952 memcpy(fid->lease_key, CIFS_I(inode)->lease_key, SMB2_LEASE_KEY_SIZE);
953}
954
955static void
956smb2_set_lease_key(struct inode *inode, struct cifs_fid *fid)
957{
958 memcpy(CIFS_I(inode)->lease_key, fid->lease_key, SMB2_LEASE_KEY_SIZE);
959}
960
961static void
962smb2_new_lease_key(struct cifs_fid *fid)
963{
964 get_random_bytes(fid->lease_key, SMB2_LEASE_KEY_SIZE);
965}
966
Pavel Shilovskyb42bf882013-08-14 19:25:21 +0400967static int
968smb2_query_symlink(const unsigned int xid, struct cifs_tcon *tcon,
969 const char *full_path, char **target_path,
970 struct cifs_sb_info *cifs_sb)
971{
972 int rc;
973 __le16 *utf16_path;
974 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
975 struct cifs_open_parms oparms;
976 struct cifs_fid fid;
977 struct smb2_err_rsp *err_buf = NULL;
978 struct smb2_symlink_err_rsp *symlink;
979 unsigned int sub_len, sub_offset;
980
981 cifs_dbg(FYI, "%s: path: %s\n", __func__, full_path);
982
983 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
984 if (!utf16_path)
985 return -ENOMEM;
986
987 oparms.tcon = tcon;
988 oparms.desired_access = FILE_READ_ATTRIBUTES;
989 oparms.disposition = FILE_OPEN;
990 oparms.create_options = 0;
991 oparms.fid = &fid;
992 oparms.reconnect = false;
993
994 rc = SMB2_open(xid, &oparms, utf16_path, &oplock, NULL, &err_buf);
995
996 if (!rc || !err_buf) {
997 kfree(utf16_path);
998 return -ENOENT;
999 }
1000 /* open must fail on symlink - reset rc */
1001 rc = 0;
1002 symlink = (struct smb2_symlink_err_rsp *)err_buf->ErrorData;
1003 sub_len = le16_to_cpu(symlink->SubstituteNameLength);
1004 sub_offset = le16_to_cpu(symlink->SubstituteNameOffset);
1005 *target_path = cifs_strndup_from_utf16(
1006 (char *)symlink->PathBuffer + sub_offset,
1007 sub_len, true, cifs_sb->local_nls);
1008 if (!(*target_path)) {
1009 kfree(utf16_path);
1010 return -ENOMEM;
1011 }
1012 convert_delimiter(*target_path, '/');
1013 cifs_dbg(FYI, "%s: target path: %s\n", __func__, *target_path);
1014 kfree(utf16_path);
1015 return rc;
1016}
1017
Steve French31742c52014-08-17 08:38:47 -05001018static long smb3_punch_hole(struct file *file, struct cifs_tcon *tcon,
1019 loff_t offset, loff_t len)
1020{
1021 struct inode *inode;
1022 struct cifsInodeInfo *cifsi;
1023 struct cifsFileInfo *cfile = file->private_data;
1024 struct file_zero_data_information fsctl_buf;
1025 long rc;
1026 unsigned int xid;
1027 __u8 set_sparse = 1;
1028
1029 xid = get_xid();
1030
1031 inode = cfile->dentry->d_inode;
1032 cifsi = CIFS_I(inode);
1033
1034 /* Need to make file sparse, if not already, before freeing range. */
1035 /* Consider adding equivalent for compressed since it could also work */
1036 if (!smb2_set_sparse(xid, tcon, cfile, inode, set_sparse))
1037 return -EOPNOTSUPP;
1038
1039 cifs_dbg(FYI, "offset %lld len %lld", offset, len);
1040
1041 fsctl_buf.FileOffset = cpu_to_le64(offset);
1042 fsctl_buf.BeyondFinalZero = cpu_to_le64(offset + len);
1043
1044 rc = SMB2_ioctl(xid, tcon, cfile->fid.persistent_fid,
1045 cfile->fid.volatile_fid, FSCTL_SET_ZERO_DATA,
1046 true /* is_fctl */, (char *)&fsctl_buf,
1047 sizeof(struct file_zero_data_information), NULL, NULL);
1048 free_xid(xid);
1049 return rc;
1050}
1051
1052static long smb3_fallocate(struct file *file, struct cifs_tcon *tcon, int mode,
1053 loff_t off, loff_t len)
1054{
1055 /* KEEP_SIZE already checked for by do_fallocate */
1056 if (mode & FALLOC_FL_PUNCH_HOLE)
1057 return smb3_punch_hole(file, tcon, off, len);
1058
1059 return -EOPNOTSUPP;
1060}
1061
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04001062static void
Sachin Prabhuc11f1df2014-03-11 16:11:47 +00001063smb2_downgrade_oplock(struct TCP_Server_Info *server,
1064 struct cifsInodeInfo *cinode, bool set_level2)
1065{
1066 if (set_level2)
1067 server->ops->set_oplock_level(cinode, SMB2_OPLOCK_LEVEL_II,
1068 0, NULL);
1069 else
1070 server->ops->set_oplock_level(cinode, 0, 0, NULL);
1071}
1072
1073static void
Pavel Shilovsky42873b02013-09-05 21:30:16 +04001074smb2_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
1075 unsigned int epoch, bool *purge_cache)
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04001076{
1077 oplock &= 0xFF;
1078 if (oplock == SMB2_OPLOCK_LEVEL_NOCHANGE)
1079 return;
1080 if (oplock == SMB2_OPLOCK_LEVEL_BATCH) {
Pavel Shilovsky42873b02013-09-05 21:30:16 +04001081 cinode->oplock = CIFS_CACHE_RHW_FLG;
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04001082 cifs_dbg(FYI, "Batch Oplock granted on inode %p\n",
1083 &cinode->vfs_inode);
1084 } else if (oplock == SMB2_OPLOCK_LEVEL_EXCLUSIVE) {
Pavel Shilovsky42873b02013-09-05 21:30:16 +04001085 cinode->oplock = CIFS_CACHE_RW_FLG;
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04001086 cifs_dbg(FYI, "Exclusive Oplock granted on inode %p\n",
1087 &cinode->vfs_inode);
1088 } else if (oplock == SMB2_OPLOCK_LEVEL_II) {
1089 cinode->oplock = CIFS_CACHE_READ_FLG;
1090 cifs_dbg(FYI, "Level II Oplock granted on inode %p\n",
1091 &cinode->vfs_inode);
1092 } else
1093 cinode->oplock = 0;
1094}
1095
1096static void
Pavel Shilovsky42873b02013-09-05 21:30:16 +04001097smb21_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
1098 unsigned int epoch, bool *purge_cache)
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04001099{
1100 char message[5] = {0};
1101
1102 oplock &= 0xFF;
1103 if (oplock == SMB2_OPLOCK_LEVEL_NOCHANGE)
1104 return;
1105
1106 cinode->oplock = 0;
1107 if (oplock & SMB2_LEASE_READ_CACHING_HE) {
1108 cinode->oplock |= CIFS_CACHE_READ_FLG;
1109 strcat(message, "R");
1110 }
1111 if (oplock & SMB2_LEASE_HANDLE_CACHING_HE) {
1112 cinode->oplock |= CIFS_CACHE_HANDLE_FLG;
1113 strcat(message, "H");
1114 }
1115 if (oplock & SMB2_LEASE_WRITE_CACHING_HE) {
1116 cinode->oplock |= CIFS_CACHE_WRITE_FLG;
1117 strcat(message, "W");
1118 }
1119 if (!cinode->oplock)
1120 strcat(message, "None");
1121 cifs_dbg(FYI, "%s Lease granted on inode %p\n", message,
1122 &cinode->vfs_inode);
1123}
1124
Pavel Shilovsky42873b02013-09-05 21:30:16 +04001125static void
1126smb3_set_oplock_level(struct cifsInodeInfo *cinode, __u32 oplock,
1127 unsigned int epoch, bool *purge_cache)
1128{
1129 unsigned int old_oplock = cinode->oplock;
1130
1131 smb21_set_oplock_level(cinode, oplock, epoch, purge_cache);
1132
1133 if (purge_cache) {
1134 *purge_cache = false;
1135 if (old_oplock == CIFS_CACHE_READ_FLG) {
1136 if (cinode->oplock == CIFS_CACHE_READ_FLG &&
1137 (epoch - cinode->epoch > 0))
1138 *purge_cache = true;
1139 else if (cinode->oplock == CIFS_CACHE_RH_FLG &&
1140 (epoch - cinode->epoch > 1))
1141 *purge_cache = true;
1142 else if (cinode->oplock == CIFS_CACHE_RHW_FLG &&
1143 (epoch - cinode->epoch > 1))
1144 *purge_cache = true;
1145 else if (cinode->oplock == 0 &&
1146 (epoch - cinode->epoch > 0))
1147 *purge_cache = true;
1148 } else if (old_oplock == CIFS_CACHE_RH_FLG) {
1149 if (cinode->oplock == CIFS_CACHE_RH_FLG &&
1150 (epoch - cinode->epoch > 0))
1151 *purge_cache = true;
1152 else if (cinode->oplock == CIFS_CACHE_RHW_FLG &&
1153 (epoch - cinode->epoch > 1))
1154 *purge_cache = true;
1155 }
1156 cinode->epoch = epoch;
1157 }
1158}
1159
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04001160static bool
1161smb2_is_read_op(__u32 oplock)
1162{
1163 return oplock == SMB2_OPLOCK_LEVEL_II;
1164}
1165
1166static bool
1167smb21_is_read_op(__u32 oplock)
1168{
1169 return (oplock & SMB2_LEASE_READ_CACHING_HE) &&
1170 !(oplock & SMB2_LEASE_WRITE_CACHING_HE);
1171}
1172
Pavel Shilovskyf0473902013-09-04 13:44:05 +04001173static __le32
1174map_oplock_to_lease(u8 oplock)
1175{
1176 if (oplock == SMB2_OPLOCK_LEVEL_EXCLUSIVE)
1177 return SMB2_LEASE_WRITE_CACHING | SMB2_LEASE_READ_CACHING;
1178 else if (oplock == SMB2_OPLOCK_LEVEL_II)
1179 return SMB2_LEASE_READ_CACHING;
1180 else if (oplock == SMB2_OPLOCK_LEVEL_BATCH)
1181 return SMB2_LEASE_HANDLE_CACHING | SMB2_LEASE_READ_CACHING |
1182 SMB2_LEASE_WRITE_CACHING;
1183 return 0;
1184}
1185
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001186static char *
1187smb2_create_lease_buf(u8 *lease_key, u8 oplock)
1188{
1189 struct create_lease *buf;
1190
1191 buf = kzalloc(sizeof(struct create_lease), GFP_KERNEL);
1192 if (!buf)
1193 return NULL;
1194
1195 buf->lcontext.LeaseKeyLow = cpu_to_le64(*((u64 *)lease_key));
1196 buf->lcontext.LeaseKeyHigh = cpu_to_le64(*((u64 *)(lease_key + 8)));
Pavel Shilovskyf0473902013-09-04 13:44:05 +04001197 buf->lcontext.LeaseState = map_oplock_to_lease(oplock);
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001198
1199 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1200 (struct create_lease, lcontext));
1201 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context));
1202 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1203 (struct create_lease, Name));
1204 buf->ccontext.NameLength = cpu_to_le16(4);
Steve French12197a72014-05-14 05:29:40 -07001205 /* SMB2_CREATE_REQUEST_LEASE is "RqLs" */
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001206 buf->Name[0] = 'R';
1207 buf->Name[1] = 'q';
1208 buf->Name[2] = 'L';
1209 buf->Name[3] = 's';
1210 return (char *)buf;
1211}
1212
Pavel Shilovskyf0473902013-09-04 13:44:05 +04001213static char *
1214smb3_create_lease_buf(u8 *lease_key, u8 oplock)
1215{
1216 struct create_lease_v2 *buf;
1217
1218 buf = kzalloc(sizeof(struct create_lease_v2), GFP_KERNEL);
1219 if (!buf)
1220 return NULL;
1221
1222 buf->lcontext.LeaseKeyLow = cpu_to_le64(*((u64 *)lease_key));
1223 buf->lcontext.LeaseKeyHigh = cpu_to_le64(*((u64 *)(lease_key + 8)));
1224 buf->lcontext.LeaseState = map_oplock_to_lease(oplock);
1225
1226 buf->ccontext.DataOffset = cpu_to_le16(offsetof
1227 (struct create_lease_v2, lcontext));
1228 buf->ccontext.DataLength = cpu_to_le32(sizeof(struct lease_context_v2));
1229 buf->ccontext.NameOffset = cpu_to_le16(offsetof
1230 (struct create_lease_v2, Name));
1231 buf->ccontext.NameLength = cpu_to_le16(4);
Steve French12197a72014-05-14 05:29:40 -07001232 /* SMB2_CREATE_REQUEST_LEASE is "RqLs" */
Pavel Shilovskyf0473902013-09-04 13:44:05 +04001233 buf->Name[0] = 'R';
1234 buf->Name[1] = 'q';
1235 buf->Name[2] = 'L';
1236 buf->Name[3] = 's';
1237 return (char *)buf;
1238}
1239
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001240static __u8
Pavel Shilovsky42873b02013-09-05 21:30:16 +04001241smb2_parse_lease_buf(void *buf, unsigned int *epoch)
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001242{
1243 struct create_lease *lc = (struct create_lease *)buf;
1244
Pavel Shilovsky42873b02013-09-05 21:30:16 +04001245 *epoch = 0; /* not used */
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001246 if (lc->lcontext.LeaseFlags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS)
1247 return SMB2_OPLOCK_LEVEL_NOCHANGE;
1248 return le32_to_cpu(lc->lcontext.LeaseState);
1249}
1250
Pavel Shilovskyf0473902013-09-04 13:44:05 +04001251static __u8
Pavel Shilovsky42873b02013-09-05 21:30:16 +04001252smb3_parse_lease_buf(void *buf, unsigned int *epoch)
Pavel Shilovskyf0473902013-09-04 13:44:05 +04001253{
1254 struct create_lease_v2 *lc = (struct create_lease_v2 *)buf;
1255
Pavel Shilovsky42873b02013-09-05 21:30:16 +04001256 *epoch = le16_to_cpu(lc->lcontext.Epoch);
Pavel Shilovskyf0473902013-09-04 13:44:05 +04001257 if (lc->lcontext.LeaseFlags & SMB2_LEASE_FLAG_BREAK_IN_PROGRESS)
1258 return SMB2_OPLOCK_LEVEL_NOCHANGE;
1259 return le32_to_cpu(lc->lcontext.LeaseState);
1260}
1261
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04001262static unsigned int
1263smb2_wp_retry_size(struct inode *inode)
1264{
1265 return min_t(unsigned int, CIFS_SB(inode->i_sb)->wsize,
1266 SMB2_MAX_BUFFER_SIZE);
1267}
1268
Pavel Shilovsky52755802014-08-18 20:49:57 +04001269static bool
1270smb2_dir_needs_close(struct cifsFileInfo *cfile)
1271{
1272 return !cfile->invalidHandle;
1273}
1274
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04001275struct smb_version_operations smb20_operations = {
1276 .compare_fids = smb2_compare_fids,
1277 .setup_request = smb2_setup_request,
1278 .setup_async_request = smb2_setup_async_request,
1279 .check_receive = smb2_check_receive,
1280 .add_credits = smb2_add_credits,
1281 .set_credits = smb2_set_credits,
1282 .get_credits_field = smb2_get_credits_field,
1283 .get_credits = smb2_get_credits,
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04001284 .wait_mtu_credits = cifs_wait_mtu_credits,
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04001285 .get_next_mid = smb2_get_next_mid,
1286 .read_data_offset = smb2_read_data_offset,
1287 .read_data_length = smb2_read_data_length,
1288 .map_error = map_smb2_to_linux_error,
1289 .find_mid = smb2_find_mid,
1290 .check_message = smb2_check_message,
1291 .dump_detail = smb2_dump_detail,
1292 .clear_stats = smb2_clear_stats,
1293 .print_stats = smb2_print_stats,
1294 .is_oplock_break = smb2_is_valid_oplock_break,
Sachin Prabhuc11f1df2014-03-11 16:11:47 +00001295 .downgrade_oplock = smb2_downgrade_oplock,
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04001296 .need_neg = smb2_need_neg,
1297 .negotiate = smb2_negotiate,
1298 .negotiate_wsize = smb2_negotiate_wsize,
1299 .negotiate_rsize = smb2_negotiate_rsize,
1300 .sess_setup = SMB2_sess_setup,
1301 .logoff = SMB2_logoff,
1302 .tree_connect = SMB2_tcon,
1303 .tree_disconnect = SMB2_tdis,
Steve French34f62642013-10-09 02:07:00 -05001304 .qfs_tcon = smb2_qfs_tcon,
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04001305 .is_path_accessible = smb2_is_path_accessible,
1306 .can_echo = smb2_can_echo,
1307 .echo = SMB2_echo,
1308 .query_path_info = smb2_query_path_info,
1309 .get_srv_inum = smb2_get_srv_inum,
1310 .query_file_info = smb2_query_file_info,
1311 .set_path_size = smb2_set_path_size,
1312 .set_file_size = smb2_set_file_size,
1313 .set_file_info = smb2_set_file_info,
Steve French64a5cfa2013-10-14 15:31:32 -05001314 .set_compression = smb2_set_compression,
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04001315 .mkdir = smb2_mkdir,
1316 .mkdir_setinfo = smb2_mkdir_setinfo,
1317 .rmdir = smb2_rmdir,
1318 .unlink = smb2_unlink,
1319 .rename = smb2_rename_path,
1320 .create_hardlink = smb2_create_hardlink,
1321 .query_symlink = smb2_query_symlink,
1322 .open = smb2_open_file,
1323 .set_fid = smb2_set_fid,
1324 .close = smb2_close_file,
1325 .flush = smb2_flush_file,
1326 .async_readv = smb2_async_readv,
1327 .async_writev = smb2_async_writev,
1328 .sync_read = smb2_sync_read,
1329 .sync_write = smb2_sync_write,
1330 .query_dir_first = smb2_query_dir_first,
1331 .query_dir_next = smb2_query_dir_next,
1332 .close_dir = smb2_close_dir,
1333 .calc_smb_size = smb2_calc_size,
1334 .is_status_pending = smb2_is_status_pending,
1335 .oplock_response = smb2_oplock_response,
1336 .queryfs = smb2_queryfs,
1337 .mand_lock = smb2_mand_lock,
1338 .mand_unlock_range = smb2_unlock_range,
1339 .push_mand_locks = smb2_push_mandatory_locks,
1340 .get_lease_key = smb2_get_lease_key,
1341 .set_lease_key = smb2_set_lease_key,
1342 .new_lease_key = smb2_new_lease_key,
1343 .calc_signature = smb2_calc_signature,
1344 .is_read_op = smb2_is_read_op,
1345 .set_oplock_level = smb2_set_oplock_level,
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001346 .create_lease_buf = smb2_create_lease_buf,
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001347 .parse_lease_buf = smb2_parse_lease_buf,
Steve French41c13582013-11-14 00:05:36 -06001348 .clone_range = smb2_clone_range,
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04001349 .wp_retry_size = smb2_wp_retry_size,
Pavel Shilovsky52755802014-08-18 20:49:57 +04001350 .dir_needs_close = smb2_dir_needs_close,
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04001351};
1352
Steve French1080ef72011-02-24 18:07:19 +00001353struct smb_version_operations smb21_operations = {
Pavel Shilovsky027e8ee2012-09-19 06:22:43 -07001354 .compare_fids = smb2_compare_fids,
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +04001355 .setup_request = smb2_setup_request,
Pavel Shilovskyc95b8ee2012-07-11 14:45:28 +04001356 .setup_async_request = smb2_setup_async_request,
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +04001357 .check_receive = smb2_check_receive,
Pavel Shilovsky28ea5292012-05-23 16:18:00 +04001358 .add_credits = smb2_add_credits,
1359 .set_credits = smb2_set_credits,
1360 .get_credits_field = smb2_get_credits_field,
1361 .get_credits = smb2_get_credits,
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04001362 .wait_mtu_credits = smb2_wait_mtu_credits,
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +04001363 .get_next_mid = smb2_get_next_mid,
Pavel Shilovsky09a47072012-09-18 16:20:29 -07001364 .read_data_offset = smb2_read_data_offset,
1365 .read_data_length = smb2_read_data_length,
1366 .map_error = map_smb2_to_linux_error,
Pavel Shilovsky093b2bd2011-06-08 15:51:07 +04001367 .find_mid = smb2_find_mid,
1368 .check_message = smb2_check_message,
1369 .dump_detail = smb2_dump_detail,
Pavel Shilovskyd60622e2012-05-28 15:19:39 +04001370 .clear_stats = smb2_clear_stats,
1371 .print_stats = smb2_print_stats,
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07001372 .is_oplock_break = smb2_is_valid_oplock_break,
Sachin Prabhuc11f1df2014-03-11 16:11:47 +00001373 .downgrade_oplock = smb2_downgrade_oplock,
Pavel Shilovskyec2e4522011-12-27 16:12:43 +04001374 .need_neg = smb2_need_neg,
1375 .negotiate = smb2_negotiate,
Pavel Shilovsky3a3bab52012-09-18 16:20:28 -07001376 .negotiate_wsize = smb2_negotiate_wsize,
1377 .negotiate_rsize = smb2_negotiate_rsize,
Pavel Shilovsky5478f9b2011-12-27 16:22:00 +04001378 .sess_setup = SMB2_sess_setup,
1379 .logoff = SMB2_logoff,
Pavel Shilovskyfaaf9462011-12-27 16:04:00 +04001380 .tree_connect = SMB2_tcon,
1381 .tree_disconnect = SMB2_tdis,
Steve French34f62642013-10-09 02:07:00 -05001382 .qfs_tcon = smb2_qfs_tcon,
Pavel Shilovsky2503a0d2011-12-26 22:58:46 +04001383 .is_path_accessible = smb2_is_path_accessible,
Pavel Shilovsky9094fad2012-07-12 18:30:44 +04001384 .can_echo = smb2_can_echo,
1385 .echo = SMB2_echo,
Pavel Shilovskybe4cb9e2011-12-29 17:06:33 +04001386 .query_path_info = smb2_query_path_info,
1387 .get_srv_inum = smb2_get_srv_inum,
Pavel Shilovskyb7546bc2012-09-18 16:20:27 -07001388 .query_file_info = smb2_query_file_info,
Pavel Shilovskyc839ff22012-09-18 16:20:32 -07001389 .set_path_size = smb2_set_path_size,
1390 .set_file_size = smb2_set_file_size,
Pavel Shilovsky1feeaac2012-09-18 16:20:32 -07001391 .set_file_info = smb2_set_file_info,
Steve French64a5cfa2013-10-14 15:31:32 -05001392 .set_compression = smb2_set_compression,
Pavel Shilovskya0e73182011-07-19 12:56:37 +04001393 .mkdir = smb2_mkdir,
1394 .mkdir_setinfo = smb2_mkdir_setinfo,
Pavel Shilovsky1a500f02012-07-10 16:14:38 +04001395 .rmdir = smb2_rmdir,
Pavel Shilovskycbe6f432012-09-18 16:20:25 -07001396 .unlink = smb2_unlink,
Pavel Shilovsky35143eb2012-09-18 16:20:31 -07001397 .rename = smb2_rename_path,
Pavel Shilovsky568798c2012-09-18 16:20:31 -07001398 .create_hardlink = smb2_create_hardlink,
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04001399 .query_symlink = smb2_query_symlink,
Pavel Shilovskyf0df7372012-09-18 16:20:26 -07001400 .open = smb2_open_file,
1401 .set_fid = smb2_set_fid,
1402 .close = smb2_close_file,
Pavel Shilovsky7a5cfb12012-09-18 16:20:28 -07001403 .flush = smb2_flush_file,
Pavel Shilovsky09a47072012-09-18 16:20:29 -07001404 .async_readv = smb2_async_readv,
Pavel Shilovsky33319142012-09-18 16:20:29 -07001405 .async_writev = smb2_async_writev,
Pavel Shilovskyd8e05032012-09-18 16:20:30 -07001406 .sync_read = smb2_sync_read,
Pavel Shilovsky009d3442012-09-18 16:20:30 -07001407 .sync_write = smb2_sync_write,
Pavel Shilovskyd324f08d2012-09-18 16:20:33 -07001408 .query_dir_first = smb2_query_dir_first,
1409 .query_dir_next = smb2_query_dir_next,
1410 .close_dir = smb2_close_dir,
1411 .calc_smb_size = smb2_calc_size,
Pavel Shilovsky2e44b282012-09-18 16:20:33 -07001412 .is_status_pending = smb2_is_status_pending,
Pavel Shilovsky983c88a2012-09-18 16:20:33 -07001413 .oplock_response = smb2_oplock_response,
Pavel Shilovsky6fc05c22012-09-18 16:20:34 -07001414 .queryfs = smb2_queryfs,
Pavel Shilovskyf7ba7fe2012-09-19 06:22:43 -07001415 .mand_lock = smb2_mand_lock,
1416 .mand_unlock_range = smb2_unlock_range,
Pavel Shilovskyb1407992012-09-19 06:22:44 -07001417 .push_mand_locks = smb2_push_mandatory_locks,
Pavel Shilovskyb8c32db2012-09-19 06:22:44 -07001418 .get_lease_key = smb2_get_lease_key,
1419 .set_lease_key = smb2_set_lease_key,
1420 .new_lease_key = smb2_new_lease_key,
Steve French38107d42012-12-08 22:08:06 -06001421 .calc_signature = smb2_calc_signature,
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04001422 .is_read_op = smb21_is_read_op,
1423 .set_oplock_level = smb21_set_oplock_level,
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001424 .create_lease_buf = smb2_create_lease_buf,
Pavel Shilovskyb5c7cde2013-09-05 20:16:45 +04001425 .parse_lease_buf = smb2_parse_lease_buf,
Steve French41c13582013-11-14 00:05:36 -06001426 .clone_range = smb2_clone_range,
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04001427 .wp_retry_size = smb2_wp_retry_size,
Pavel Shilovsky52755802014-08-18 20:49:57 +04001428 .dir_needs_close = smb2_dir_needs_close,
Steve French38107d42012-12-08 22:08:06 -06001429};
1430
Steve French38107d42012-12-08 22:08:06 -06001431struct smb_version_operations smb30_operations = {
1432 .compare_fids = smb2_compare_fids,
1433 .setup_request = smb2_setup_request,
1434 .setup_async_request = smb2_setup_async_request,
1435 .check_receive = smb2_check_receive,
1436 .add_credits = smb2_add_credits,
1437 .set_credits = smb2_set_credits,
1438 .get_credits_field = smb2_get_credits_field,
1439 .get_credits = smb2_get_credits,
Pavel Shilovskycb7e9ea2014-06-05 19:03:27 +04001440 .wait_mtu_credits = smb2_wait_mtu_credits,
Steve French38107d42012-12-08 22:08:06 -06001441 .get_next_mid = smb2_get_next_mid,
1442 .read_data_offset = smb2_read_data_offset,
1443 .read_data_length = smb2_read_data_length,
1444 .map_error = map_smb2_to_linux_error,
1445 .find_mid = smb2_find_mid,
1446 .check_message = smb2_check_message,
1447 .dump_detail = smb2_dump_detail,
1448 .clear_stats = smb2_clear_stats,
1449 .print_stats = smb2_print_stats,
Steve French769ee6a2013-06-19 14:15:30 -05001450 .dump_share_caps = smb2_dump_share_caps,
Steve French38107d42012-12-08 22:08:06 -06001451 .is_oplock_break = smb2_is_valid_oplock_break,
Sachin Prabhuc11f1df2014-03-11 16:11:47 +00001452 .downgrade_oplock = smb2_downgrade_oplock,
Steve French38107d42012-12-08 22:08:06 -06001453 .need_neg = smb2_need_neg,
1454 .negotiate = smb2_negotiate,
1455 .negotiate_wsize = smb2_negotiate_wsize,
1456 .negotiate_rsize = smb2_negotiate_rsize,
1457 .sess_setup = SMB2_sess_setup,
1458 .logoff = SMB2_logoff,
1459 .tree_connect = SMB2_tcon,
1460 .tree_disconnect = SMB2_tdis,
Steven Frenchaf6a12e2013-10-09 20:55:53 -05001461 .qfs_tcon = smb3_qfs_tcon,
Steve French38107d42012-12-08 22:08:06 -06001462 .is_path_accessible = smb2_is_path_accessible,
1463 .can_echo = smb2_can_echo,
1464 .echo = SMB2_echo,
1465 .query_path_info = smb2_query_path_info,
1466 .get_srv_inum = smb2_get_srv_inum,
1467 .query_file_info = smb2_query_file_info,
1468 .set_path_size = smb2_set_path_size,
1469 .set_file_size = smb2_set_file_size,
1470 .set_file_info = smb2_set_file_info,
Steve French64a5cfa2013-10-14 15:31:32 -05001471 .set_compression = smb2_set_compression,
Steve French38107d42012-12-08 22:08:06 -06001472 .mkdir = smb2_mkdir,
1473 .mkdir_setinfo = smb2_mkdir_setinfo,
1474 .rmdir = smb2_rmdir,
1475 .unlink = smb2_unlink,
1476 .rename = smb2_rename_path,
1477 .create_hardlink = smb2_create_hardlink,
Pavel Shilovskyb42bf882013-08-14 19:25:21 +04001478 .query_symlink = smb2_query_symlink,
Steve French38107d42012-12-08 22:08:06 -06001479 .open = smb2_open_file,
1480 .set_fid = smb2_set_fid,
1481 .close = smb2_close_file,
1482 .flush = smb2_flush_file,
1483 .async_readv = smb2_async_readv,
1484 .async_writev = smb2_async_writev,
1485 .sync_read = smb2_sync_read,
1486 .sync_write = smb2_sync_write,
1487 .query_dir_first = smb2_query_dir_first,
1488 .query_dir_next = smb2_query_dir_next,
1489 .close_dir = smb2_close_dir,
1490 .calc_smb_size = smb2_calc_size,
1491 .is_status_pending = smb2_is_status_pending,
1492 .oplock_response = smb2_oplock_response,
1493 .queryfs = smb2_queryfs,
1494 .mand_lock = smb2_mand_lock,
1495 .mand_unlock_range = smb2_unlock_range,
1496 .push_mand_locks = smb2_push_mandatory_locks,
1497 .get_lease_key = smb2_get_lease_key,
1498 .set_lease_key = smb2_set_lease_key,
1499 .new_lease_key = smb2_new_lease_key,
Steve Frenche65a5cb2013-06-27 01:06:50 -05001500 .generate_signingkey = generate_smb3signingkey,
Steve French38107d42012-12-08 22:08:06 -06001501 .calc_signature = smb3_calc_signature,
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04001502 .is_read_op = smb21_is_read_op,
Pavel Shilovsky42873b02013-09-05 21:30:16 +04001503 .set_oplock_level = smb3_set_oplock_level,
Pavel Shilovskyf0473902013-09-04 13:44:05 +04001504 .create_lease_buf = smb3_create_lease_buf,
1505 .parse_lease_buf = smb3_parse_lease_buf,
Steve French41c13582013-11-14 00:05:36 -06001506 .clone_range = smb2_clone_range,
Steve Frenchff1c0382013-11-19 23:44:46 -06001507 .validate_negotiate = smb3_validate_negotiate,
Pavel Shilovsky7f6c5002014-06-22 11:03:22 +04001508 .wp_retry_size = smb2_wp_retry_size,
Pavel Shilovsky52755802014-08-18 20:49:57 +04001509 .dir_needs_close = smb2_dir_needs_close,
Steve French31742c52014-08-17 08:38:47 -05001510 .fallocate = smb3_fallocate,
Steve French1080ef72011-02-24 18:07:19 +00001511};
1512
Steve Frenchdd446b12012-11-28 23:21:06 -06001513struct smb_version_values smb20_values = {
1514 .version_string = SMB20_VERSION_STRING,
1515 .protocol_id = SMB20_PROT_ID,
1516 .req_capabilities = 0, /* MBZ */
1517 .large_lock_type = 0,
1518 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
1519 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
1520 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
1521 .header_size = sizeof(struct smb2_hdr),
1522 .max_header_size = MAX_SMB2_HDR_SIZE,
1523 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
1524 .lock_cmd = SMB2_LOCK,
1525 .cap_unix = 0,
1526 .cap_nt_find = SMB2_NT_FIND,
1527 .cap_large_files = SMB2_LARGE_FILES,
Jeff Layton50285882013-06-27 12:45:00 -04001528 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
1529 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001530 .create_lease_size = sizeof(struct create_lease),
Steve Frenchdd446b12012-11-28 23:21:06 -06001531};
1532
Steve French1080ef72011-02-24 18:07:19 +00001533struct smb_version_values smb21_values = {
1534 .version_string = SMB21_VERSION_STRING,
Steve Frenche4aa25e2012-10-01 12:26:22 -05001535 .protocol_id = SMB21_PROT_ID,
1536 .req_capabilities = 0, /* MBZ on negotiate req until SMB3 dialect */
1537 .large_lock_type = 0,
1538 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
1539 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
1540 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
1541 .header_size = sizeof(struct smb2_hdr),
1542 .max_header_size = MAX_SMB2_HDR_SIZE,
1543 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
1544 .lock_cmd = SMB2_LOCK,
1545 .cap_unix = 0,
1546 .cap_nt_find = SMB2_NT_FIND,
1547 .cap_large_files = SMB2_LARGE_FILES,
Jeff Layton50285882013-06-27 12:45:00 -04001548 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
1549 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
Pavel Shilovskya41a28b2013-09-04 13:07:41 +04001550 .create_lease_size = sizeof(struct create_lease),
Steve Frenche4aa25e2012-10-01 12:26:22 -05001551};
1552
1553struct smb_version_values smb30_values = {
1554 .version_string = SMB30_VERSION_STRING,
1555 .protocol_id = SMB30_PROT_ID,
1556 .req_capabilities = SMB2_GLOBAL_CAP_DFS | SMB2_GLOBAL_CAP_LEASING | SMB2_GLOBAL_CAP_LARGE_MTU,
Pavel Shilovsky027e8ee2012-09-19 06:22:43 -07001557 .large_lock_type = 0,
1558 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
1559 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
1560 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
Pavel Shilovsky093b2bd2011-06-08 15:51:07 +04001561 .header_size = sizeof(struct smb2_hdr),
1562 .max_header_size = MAX_SMB2_HDR_SIZE,
Pavel Shilovsky09a47072012-09-18 16:20:29 -07001563 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
Pavel Shilovsky2dc7e1c2011-12-26 22:53:34 +04001564 .lock_cmd = SMB2_LOCK,
Pavel Shilovsky29e20f92012-07-13 13:58:14 +04001565 .cap_unix = 0,
1566 .cap_nt_find = SMB2_NT_FIND,
1567 .cap_large_files = SMB2_LARGE_FILES,
Jeff Layton50285882013-06-27 12:45:00 -04001568 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
1569 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
Pavel Shilovskyf0473902013-09-04 13:44:05 +04001570 .create_lease_size = sizeof(struct create_lease_v2),
Steve French1080ef72011-02-24 18:07:19 +00001571};
Steve French20b6d8b2013-06-12 22:48:41 -05001572
1573struct smb_version_values smb302_values = {
1574 .version_string = SMB302_VERSION_STRING,
1575 .protocol_id = SMB302_PROT_ID,
1576 .req_capabilities = SMB2_GLOBAL_CAP_DFS | SMB2_GLOBAL_CAP_LEASING | SMB2_GLOBAL_CAP_LARGE_MTU,
1577 .large_lock_type = 0,
1578 .exclusive_lock_type = SMB2_LOCKFLAG_EXCLUSIVE_LOCK,
1579 .shared_lock_type = SMB2_LOCKFLAG_SHARED_LOCK,
1580 .unlock_lock_type = SMB2_LOCKFLAG_UNLOCK,
1581 .header_size = sizeof(struct smb2_hdr),
1582 .max_header_size = MAX_SMB2_HDR_SIZE,
1583 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
1584 .lock_cmd = SMB2_LOCK,
1585 .cap_unix = 0,
1586 .cap_nt_find = SMB2_NT_FIND,
1587 .cap_large_files = SMB2_LARGE_FILES,
Jeff Layton50285882013-06-27 12:45:00 -04001588 .signing_enabled = SMB2_NEGOTIATE_SIGNING_ENABLED | SMB2_NEGOTIATE_SIGNING_REQUIRED,
1589 .signing_required = SMB2_NEGOTIATE_SIGNING_REQUIRED,
Pavel Shilovskyf0473902013-09-04 13:44:05 +04001590 .create_lease_size = sizeof(struct create_lease_v2),
Steve French20b6d8b2013-06-12 22:48:41 -05001591};