2 * SMB2 version specific operations
4 * Copyright (c) 2012, Jeff Layton <jlayton@redhat.com>
6 * This library is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License v2 as published
8 * by the Free Software Foundation.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
13 * the GNU Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 #include <linux/pagemap.h>
21 #include <linux/vfs.h>
24 #include "smb2proto.h"
25 #include "cifsproto.h"
26 #include "cifs_debug.h"
27 #include "smb2status.h"
31 change_conf(struct TCP_Server_Info *server)
33 server->credits += server->echo_credits + server->oplock_credits;
34 server->oplock_credits = server->echo_credits = 0;
35 switch (server->credits) {
39 server->echoes = false;
40 server->oplocks = false;
41 cERROR(1, "disabling echoes and oplocks");
44 server->echoes = true;
45 server->oplocks = false;
46 server->echo_credits = 1;
47 cFYI(1, "disabling oplocks");
50 server->echoes = true;
51 server->oplocks = true;
52 server->echo_credits = 1;
53 server->oplock_credits = 1;
55 server->credits -= server->echo_credits + server->oplock_credits;
60 smb2_add_credits(struct TCP_Server_Info *server, const unsigned int add,
64 spin_lock(&server->req_lock);
65 val = server->ops->get_credits_field(server, optype);
68 if (server->in_flight == 0 && (optype & CIFS_OP_MASK) != CIFS_NEG_OP)
69 rc = change_conf(server);
71 * Sometimes server returns 0 credits on oplock break ack - we need to
72 * rebalance credits in this case.
74 else if (server->in_flight > 0 && server->oplock_credits == 0 &&
76 if (server->credits > 1) {
78 server->oplock_credits++;
81 spin_unlock(&server->req_lock);
82 wake_up(&server->request_q);
84 cifs_reconnect(server);
88 smb2_set_credits(struct TCP_Server_Info *server, const int val)
90 spin_lock(&server->req_lock);
91 server->credits = val;
92 spin_unlock(&server->req_lock);
96 smb2_get_credits_field(struct TCP_Server_Info *server, const int optype)
100 return &server->echo_credits;
102 return &server->oplock_credits;
104 return &server->credits;
109 smb2_get_credits(struct mid_q_entry *mid)
111 return le16_to_cpu(((struct smb2_hdr *)mid->resp_buf)->CreditRequest);
115 smb2_get_next_mid(struct TCP_Server_Info *server)
118 /* for SMB2 we need the current value */
119 spin_lock(&GlobalMid_Lock);
120 mid = server->CurrentMid++;
121 spin_unlock(&GlobalMid_Lock);
125 static struct mid_q_entry *
126 smb2_find_mid(struct TCP_Server_Info *server, char *buf)
128 struct mid_q_entry *mid;
129 struct smb2_hdr *hdr = (struct smb2_hdr *)buf;
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);
140 spin_unlock(&GlobalMid_Lock);
145 smb2_dump_detail(void *buf)
147 #ifdef CONFIG_CIFS_DEBUG2
148 struct smb2_hdr *smb = (struct smb2_hdr *)buf;
150 cERROR(1, "Cmd: %d Err: 0x%x Flags: 0x%x Mid: %llu Pid: %d",
151 smb->Command, smb->Status, smb->Flags, smb->MessageId,
153 cERROR(1, "smb buf %p len %u", smb, smb2_calc_size(smb));
158 smb2_need_neg(struct TCP_Server_Info *server)
160 return server->max_read == 0;
164 smb2_negotiate(const unsigned int xid, struct cifs_ses *ses)
167 ses->server->CurrentMid = 0;
168 rc = SMB2_negotiate(xid, ses);
169 /* BB we probably don't need to retry with modern servers */
176 smb2_negotiate_wsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
178 struct TCP_Server_Info *server = tcon->ses->server;
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);
185 * limit write size to 2 ** 16, because we don't support multicredit
188 wsize = min_t(unsigned int, wsize, 2 << 15);
194 smb2_negotiate_rsize(struct cifs_tcon *tcon, struct smb_vol *volume_info)
196 struct TCP_Server_Info *server = tcon->ses->server;
199 /* start with specified rsize, or default */
200 rsize = volume_info->rsize ? volume_info->rsize : CIFS_DEFAULT_IOSIZE;
201 rsize = min_t(unsigned int, rsize, server->max_read);
203 * limit write size to 2 ** 16, because we don't support multicredit
206 rsize = min_t(unsigned int, rsize, 2 << 15);
208 /* limit to the amount that we can kmap at once */
209 rsize = min_t(unsigned int, rsize, CIFS_KMAP_SIZE_LIMIT);
215 smb2_is_path_accessible(const unsigned int xid, struct cifs_tcon *tcon,
216 struct cifs_sb_info *cifs_sb, const char *full_path)
219 __u64 persistent_fid, volatile_fid;
221 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
223 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
227 rc = SMB2_open(xid, tcon, utf16_path, &persistent_fid, &volatile_fid,
228 FILE_READ_ATTRIBUTES, FILE_OPEN, 0, 0, &oplock, NULL);
234 rc = SMB2_close(xid, tcon, persistent_fid, volatile_fid);
240 smb2_get_srv_inum(const unsigned int xid, struct cifs_tcon *tcon,
241 struct cifs_sb_info *cifs_sb, const char *full_path,
242 u64 *uniqueid, FILE_ALL_INFO *data)
244 *uniqueid = le64_to_cpu(data->IndexNumber);
249 smb2_query_file_info(const unsigned int xid, struct cifs_tcon *tcon,
250 struct cifs_fid *fid, FILE_ALL_INFO *data)
253 struct smb2_file_all_info *smb2_data;
255 smb2_data = kzalloc(sizeof(struct smb2_file_all_info) + MAX_NAME * 2,
257 if (smb2_data == NULL)
260 rc = SMB2_query_info(xid, tcon, fid->persistent_fid, fid->volatile_fid,
263 move_smb2_info_to_cifs(data, smb2_data);
269 smb2_build_path_to_root(struct smb_vol *vol, struct cifs_sb_info *cifs_sb,
270 struct cifs_tcon *tcon)
272 int pplen = vol->prepath ? strlen(vol->prepath) : 0;
273 char *full_path = NULL;
275 /* if no prefix path, simply set path to the root of share to "" */
277 full_path = kzalloc(2, GFP_KERNEL);
281 cERROR(1, "prefixpath is not supported for SMB2 now");
286 smb2_can_echo(struct TCP_Server_Info *server)
288 return server->echoes;
292 smb2_clear_stats(struct cifs_tcon *tcon)
294 #ifdef CONFIG_CIFS_STATS
296 for (i = 0; i < NUMBER_OF_SMB2_COMMANDS; i++) {
297 atomic_set(&tcon->stats.smb2_stats.smb2_com_sent[i], 0);
298 atomic_set(&tcon->stats.smb2_stats.smb2_com_failed[i], 0);
304 smb2_print_stats(struct seq_file *m, struct cifs_tcon *tcon)
306 #ifdef CONFIG_CIFS_STATS
307 atomic_t *sent = tcon->stats.smb2_stats.smb2_com_sent;
308 atomic_t *failed = tcon->stats.smb2_stats.smb2_com_failed;
309 seq_printf(m, "\nNegotiates: %d sent %d failed",
310 atomic_read(&sent[SMB2_NEGOTIATE_HE]),
311 atomic_read(&failed[SMB2_NEGOTIATE_HE]));
312 seq_printf(m, "\nSessionSetups: %d sent %d failed",
313 atomic_read(&sent[SMB2_SESSION_SETUP_HE]),
314 atomic_read(&failed[SMB2_SESSION_SETUP_HE]));
315 #define SMB2LOGOFF 0x0002 /* trivial request/resp */
316 seq_printf(m, "\nLogoffs: %d sent %d failed",
317 atomic_read(&sent[SMB2_LOGOFF_HE]),
318 atomic_read(&failed[SMB2_LOGOFF_HE]));
319 seq_printf(m, "\nTreeConnects: %d sent %d failed",
320 atomic_read(&sent[SMB2_TREE_CONNECT_HE]),
321 atomic_read(&failed[SMB2_TREE_CONNECT_HE]));
322 seq_printf(m, "\nTreeDisconnects: %d sent %d failed",
323 atomic_read(&sent[SMB2_TREE_DISCONNECT_HE]),
324 atomic_read(&failed[SMB2_TREE_DISCONNECT_HE]));
325 seq_printf(m, "\nCreates: %d sent %d failed",
326 atomic_read(&sent[SMB2_CREATE_HE]),
327 atomic_read(&failed[SMB2_CREATE_HE]));
328 seq_printf(m, "\nCloses: %d sent %d failed",
329 atomic_read(&sent[SMB2_CLOSE_HE]),
330 atomic_read(&failed[SMB2_CLOSE_HE]));
331 seq_printf(m, "\nFlushes: %d sent %d failed",
332 atomic_read(&sent[SMB2_FLUSH_HE]),
333 atomic_read(&failed[SMB2_FLUSH_HE]));
334 seq_printf(m, "\nReads: %d sent %d failed",
335 atomic_read(&sent[SMB2_READ_HE]),
336 atomic_read(&failed[SMB2_READ_HE]));
337 seq_printf(m, "\nWrites: %d sent %d failed",
338 atomic_read(&sent[SMB2_WRITE_HE]),
339 atomic_read(&failed[SMB2_WRITE_HE]));
340 seq_printf(m, "\nLocks: %d sent %d failed",
341 atomic_read(&sent[SMB2_LOCK_HE]),
342 atomic_read(&failed[SMB2_LOCK_HE]));
343 seq_printf(m, "\nIOCTLs: %d sent %d failed",
344 atomic_read(&sent[SMB2_IOCTL_HE]),
345 atomic_read(&failed[SMB2_IOCTL_HE]));
346 seq_printf(m, "\nCancels: %d sent %d failed",
347 atomic_read(&sent[SMB2_CANCEL_HE]),
348 atomic_read(&failed[SMB2_CANCEL_HE]));
349 seq_printf(m, "\nEchos: %d sent %d failed",
350 atomic_read(&sent[SMB2_ECHO_HE]),
351 atomic_read(&failed[SMB2_ECHO_HE]));
352 seq_printf(m, "\nQueryDirectories: %d sent %d failed",
353 atomic_read(&sent[SMB2_QUERY_DIRECTORY_HE]),
354 atomic_read(&failed[SMB2_QUERY_DIRECTORY_HE]));
355 seq_printf(m, "\nChangeNotifies: %d sent %d failed",
356 atomic_read(&sent[SMB2_CHANGE_NOTIFY_HE]),
357 atomic_read(&failed[SMB2_CHANGE_NOTIFY_HE]));
358 seq_printf(m, "\nQueryInfos: %d sent %d failed",
359 atomic_read(&sent[SMB2_QUERY_INFO_HE]),
360 atomic_read(&failed[SMB2_QUERY_INFO_HE]));
361 seq_printf(m, "\nSetInfos: %d sent %d failed",
362 atomic_read(&sent[SMB2_SET_INFO_HE]),
363 atomic_read(&failed[SMB2_SET_INFO_HE]));
364 seq_printf(m, "\nOplockBreaks: %d sent %d failed",
365 atomic_read(&sent[SMB2_OPLOCK_BREAK_HE]),
366 atomic_read(&failed[SMB2_OPLOCK_BREAK_HE]));
371 smb2_set_fid(struct cifsFileInfo *cfile, struct cifs_fid *fid, __u32 oplock)
373 struct cifsInodeInfo *cinode = CIFS_I(cfile->dentry->d_inode);
374 cfile->fid.persistent_fid = fid->persistent_fid;
375 cfile->fid.volatile_fid = fid->volatile_fid;
376 smb2_set_oplock_level(cinode, oplock);
377 /* cinode->can_cache_brlcks = cinode->clientCanCacheAll; */
381 smb2_close_file(const unsigned int xid, struct cifs_tcon *tcon,
382 struct cifs_fid *fid)
384 return SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
388 smb2_flush_file(const unsigned int xid, struct cifs_tcon *tcon,
389 struct cifs_fid *fid)
391 return SMB2_flush(xid, tcon, fid->persistent_fid, fid->volatile_fid);
395 smb2_read_data_offset(char *buf)
397 struct smb2_read_rsp *rsp = (struct smb2_read_rsp *)buf;
398 return rsp->DataOffset;
402 smb2_read_data_length(char *buf)
404 struct smb2_read_rsp *rsp = (struct smb2_read_rsp *)buf;
405 return le32_to_cpu(rsp->DataLength);
410 smb2_sync_read(const unsigned int xid, struct cifsFileInfo *cfile,
411 struct cifs_io_parms *parms, unsigned int *bytes_read,
412 char **buf, int *buf_type)
414 parms->persistent_fid = cfile->fid.persistent_fid;
415 parms->volatile_fid = cfile->fid.volatile_fid;
416 return SMB2_read(xid, parms, bytes_read, buf, buf_type);
420 smb2_sync_write(const unsigned int xid, struct cifsFileInfo *cfile,
421 struct cifs_io_parms *parms, unsigned int *written,
422 struct kvec *iov, unsigned long nr_segs)
425 parms->persistent_fid = cfile->fid.persistent_fid;
426 parms->volatile_fid = cfile->fid.volatile_fid;
427 return SMB2_write(xid, parms, written, iov, nr_segs);
431 smb2_set_file_size(const unsigned int xid, struct cifs_tcon *tcon,
432 struct cifsFileInfo *cfile, __u64 size, bool set_alloc)
434 __le64 eof = cpu_to_le64(size);
435 return SMB2_set_eof(xid, tcon, cfile->fid.persistent_fid,
436 cfile->fid.volatile_fid, cfile->pid, &eof);
440 smb2_query_dir_first(const unsigned int xid, struct cifs_tcon *tcon,
441 const char *path, struct cifs_sb_info *cifs_sb,
442 struct cifs_fid *fid, __u16 search_flags,
443 struct cifs_search_info *srch_inf)
447 __u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
448 __u64 persistent_fid, volatile_fid;
450 utf16_path = cifs_convert_path_to_utf16(path, cifs_sb);
454 rc = SMB2_open(xid, tcon, utf16_path, &persistent_fid, &volatile_fid,
455 FILE_READ_ATTRIBUTES | FILE_READ_DATA, FILE_OPEN, 0, 0,
459 cERROR(1, "open dir failed");
463 srch_inf->entries_in_buffer = 0;
464 srch_inf->index_of_last_entry = 0;
465 fid->persistent_fid = persistent_fid;
466 fid->volatile_fid = volatile_fid;
468 rc = SMB2_query_directory(xid, tcon, persistent_fid, volatile_fid, 0,
471 cERROR(1, "query directory failed");
472 SMB2_close(xid, tcon, persistent_fid, volatile_fid);
478 smb2_query_dir_next(const unsigned int xid, struct cifs_tcon *tcon,
479 struct cifs_fid *fid, __u16 search_flags,
480 struct cifs_search_info *srch_inf)
482 return SMB2_query_directory(xid, tcon, fid->persistent_fid,
483 fid->volatile_fid, 0, srch_inf);
487 smb2_close_dir(const unsigned int xid, struct cifs_tcon *tcon,
488 struct cifs_fid *fid)
490 return SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
494 * If we negotiate SMB2 protocol and get STATUS_PENDING - update
495 * the number of credits and return true. Otherwise - return false.
498 smb2_is_status_pending(char *buf, struct TCP_Server_Info *server, int length)
500 struct smb2_hdr *hdr = (struct smb2_hdr *)buf;
502 if (le32_to_cpu(hdr->Status) != STATUS_PENDING)
506 spin_lock(&server->req_lock);
507 server->credits += le16_to_cpu(hdr->CreditRequest);
508 spin_unlock(&server->req_lock);
509 wake_up(&server->request_q);
516 smb2_oplock_response(struct cifs_tcon *tcon, struct cifs_fid *fid,
517 struct cifsInodeInfo *cinode)
519 return SMB2_oplock_break(0, tcon, fid->persistent_fid,
521 cinode->clientCanCacheRead ? 1 : 0);
525 smb2_queryfs(const unsigned int xid, struct cifs_tcon *tcon,
529 u64 persistent_fid, volatile_fid;
530 __le16 srch_path = 0; /* Null - open root of share */
531 u8 oplock = SMB2_OPLOCK_LEVEL_NONE;
533 rc = SMB2_open(xid, tcon, &srch_path, &persistent_fid, &volatile_fid,
534 FILE_READ_ATTRIBUTES, FILE_OPEN, 0, 0, &oplock, NULL);
537 buf->f_type = SMB2_MAGIC_NUMBER;
538 rc = SMB2_QFS_info(xid, tcon, persistent_fid, volatile_fid, buf);
539 SMB2_close(xid, tcon, persistent_fid, volatile_fid);
543 struct smb_version_operations smb21_operations = {
544 .setup_request = smb2_setup_request,
545 .setup_async_request = smb2_setup_async_request,
546 .check_receive = smb2_check_receive,
547 .add_credits = smb2_add_credits,
548 .set_credits = smb2_set_credits,
549 .get_credits_field = smb2_get_credits_field,
550 .get_credits = smb2_get_credits,
551 .get_next_mid = smb2_get_next_mid,
552 .read_data_offset = smb2_read_data_offset,
553 .read_data_length = smb2_read_data_length,
554 .map_error = map_smb2_to_linux_error,
555 .find_mid = smb2_find_mid,
556 .check_message = smb2_check_message,
557 .dump_detail = smb2_dump_detail,
558 .clear_stats = smb2_clear_stats,
559 .print_stats = smb2_print_stats,
560 .is_oplock_break = smb2_is_valid_oplock_break,
561 .need_neg = smb2_need_neg,
562 .negotiate = smb2_negotiate,
563 .negotiate_wsize = smb2_negotiate_wsize,
564 .negotiate_rsize = smb2_negotiate_rsize,
565 .sess_setup = SMB2_sess_setup,
566 .logoff = SMB2_logoff,
567 .tree_connect = SMB2_tcon,
568 .tree_disconnect = SMB2_tdis,
569 .is_path_accessible = smb2_is_path_accessible,
570 .can_echo = smb2_can_echo,
572 .query_path_info = smb2_query_path_info,
573 .get_srv_inum = smb2_get_srv_inum,
574 .query_file_info = smb2_query_file_info,
575 .set_path_size = smb2_set_path_size,
576 .set_file_size = smb2_set_file_size,
577 .set_file_info = smb2_set_file_info,
578 .build_path_to_root = smb2_build_path_to_root,
580 .mkdir_setinfo = smb2_mkdir_setinfo,
582 .unlink = smb2_unlink,
583 .rename = smb2_rename_path,
584 .create_hardlink = smb2_create_hardlink,
585 .open = smb2_open_file,
586 .set_fid = smb2_set_fid,
587 .close = smb2_close_file,
588 .flush = smb2_flush_file,
589 .async_readv = smb2_async_readv,
590 .async_writev = smb2_async_writev,
591 .sync_read = smb2_sync_read,
592 .sync_write = smb2_sync_write,
593 .query_dir_first = smb2_query_dir_first,
594 .query_dir_next = smb2_query_dir_next,
595 .close_dir = smb2_close_dir,
596 .calc_smb_size = smb2_calc_size,
597 .is_status_pending = smb2_is_status_pending,
598 .oplock_response = smb2_oplock_response,
599 .queryfs = smb2_queryfs,
602 struct smb_version_values smb21_values = {
603 .version_string = SMB21_VERSION_STRING,
604 .header_size = sizeof(struct smb2_hdr),
605 .max_header_size = MAX_SMB2_HDR_SIZE,
606 .read_rsp_size = sizeof(struct smb2_read_rsp) - 1,
607 .lock_cmd = SMB2_LOCK,
609 .cap_nt_find = SMB2_NT_FIND,
610 .cap_large_files = SMB2_LARGE_FILES,