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
22 #include "smb2proto.h"
23 #include "cifsproto.h"
24 #include "cifs_debug.h"
27 change_conf(struct TCP_Server_Info *server)
29 server->credits += server->echo_credits + server->oplock_credits;
30 server->oplock_credits = server->echo_credits = 0;
31 switch (server->credits) {
35 server->echoes = false;
36 server->oplocks = false;
37 cERROR(1, "disabling echoes and oplocks");
40 server->echoes = true;
41 server->oplocks = false;
42 server->echo_credits = 1;
43 cFYI(1, "disabling oplocks");
46 server->echoes = true;
47 server->oplocks = true;
48 server->echo_credits = 1;
49 server->oplock_credits = 1;
51 server->credits -= server->echo_credits + server->oplock_credits;
56 smb2_add_credits(struct TCP_Server_Info *server, const unsigned int add,
60 spin_lock(&server->req_lock);
61 val = server->ops->get_credits_field(server, optype);
64 if (server->in_flight == 0 && (optype & CIFS_OP_MASK) != CIFS_NEG_OP)
65 rc = change_conf(server);
66 spin_unlock(&server->req_lock);
67 wake_up(&server->request_q);
69 cifs_reconnect(server);
73 smb2_set_credits(struct TCP_Server_Info *server, const int val)
75 spin_lock(&server->req_lock);
76 server->credits = val;
77 spin_unlock(&server->req_lock);
81 smb2_get_credits_field(struct TCP_Server_Info *server, const int optype)
85 return &server->echo_credits;
87 return &server->oplock_credits;
89 return &server->credits;
94 smb2_get_credits(struct mid_q_entry *mid)
96 return le16_to_cpu(((struct smb2_hdr *)mid->resp_buf)->CreditRequest);
100 smb2_get_next_mid(struct TCP_Server_Info *server)
103 /* for SMB2 we need the current value */
104 spin_lock(&GlobalMid_Lock);
105 mid = server->CurrentMid++;
106 spin_unlock(&GlobalMid_Lock);
110 static struct mid_q_entry *
111 smb2_find_mid(struct TCP_Server_Info *server, char *buf)
113 struct mid_q_entry *mid;
114 struct smb2_hdr *hdr = (struct smb2_hdr *)buf;
116 spin_lock(&GlobalMid_Lock);
117 list_for_each_entry(mid, &server->pending_mid_q, qhead) {
118 if ((mid->mid == hdr->MessageId) &&
119 (mid->mid_state == MID_REQUEST_SUBMITTED) &&
120 (mid->command == hdr->Command)) {
121 spin_unlock(&GlobalMid_Lock);
125 spin_unlock(&GlobalMid_Lock);
130 smb2_dump_detail(void *buf)
132 #ifdef CONFIG_CIFS_DEBUG2
133 struct smb2_hdr *smb = (struct smb2_hdr *)buf;
135 cERROR(1, "Cmd: %d Err: 0x%x Flags: 0x%x Mid: %llu Pid: %d",
136 smb->Command, smb->Status, smb->Flags, smb->MessageId,
138 cERROR(1, "smb buf %p len %u", smb, smb2_calc_size(smb));
143 smb2_need_neg(struct TCP_Server_Info *server)
145 return server->max_read == 0;
149 smb2_negotiate(const unsigned int xid, struct cifs_ses *ses)
152 ses->server->CurrentMid = 0;
153 rc = SMB2_negotiate(xid, ses);
154 /* BB we probably don't need to retry with modern servers */
161 smb2_is_path_accessible(const unsigned int xid, struct cifs_tcon *tcon,
162 struct cifs_sb_info *cifs_sb, const char *full_path)
165 __u64 persistent_fid, volatile_fid;
168 utf16_path = cifs_convert_path_to_utf16(full_path, cifs_sb);
172 rc = SMB2_open(xid, tcon, utf16_path, &persistent_fid, &volatile_fid,
173 FILE_READ_ATTRIBUTES, FILE_OPEN, 0, 0, NULL);
179 rc = SMB2_close(xid, tcon, persistent_fid, volatile_fid);
185 smb2_get_srv_inum(const unsigned int xid, struct cifs_tcon *tcon,
186 struct cifs_sb_info *cifs_sb, const char *full_path,
187 u64 *uniqueid, FILE_ALL_INFO *data)
189 *uniqueid = le64_to_cpu(data->IndexNumber);
194 smb2_query_file_info(const unsigned int xid, struct cifs_tcon *tcon,
195 struct cifs_fid *fid, FILE_ALL_INFO *data)
198 struct smb2_file_all_info *smb2_data;
200 smb2_data = kzalloc(sizeof(struct smb2_file_all_info) + MAX_NAME * 2,
202 if (smb2_data == NULL)
205 rc = SMB2_query_info(xid, tcon, fid->persistent_fid, fid->volatile_fid,
208 move_smb2_info_to_cifs(data, smb2_data);
214 smb2_build_path_to_root(struct smb_vol *vol, struct cifs_sb_info *cifs_sb,
215 struct cifs_tcon *tcon)
217 int pplen = vol->prepath ? strlen(vol->prepath) : 0;
218 char *full_path = NULL;
220 /* if no prefix path, simply set path to the root of share to "" */
222 full_path = kzalloc(2, GFP_KERNEL);
226 cERROR(1, "prefixpath is not supported for SMB2 now");
231 smb2_can_echo(struct TCP_Server_Info *server)
233 return server->echoes;
237 smb2_clear_stats(struct cifs_tcon *tcon)
239 #ifdef CONFIG_CIFS_STATS
241 for (i = 0; i < NUMBER_OF_SMB2_COMMANDS; i++) {
242 atomic_set(&tcon->stats.smb2_stats.smb2_com_sent[i], 0);
243 atomic_set(&tcon->stats.smb2_stats.smb2_com_failed[i], 0);
249 smb2_print_stats(struct seq_file *m, struct cifs_tcon *tcon)
251 #ifdef CONFIG_CIFS_STATS
252 atomic_t *sent = tcon->stats.smb2_stats.smb2_com_sent;
253 atomic_t *failed = tcon->stats.smb2_stats.smb2_com_failed;
254 seq_printf(m, "\nNegotiates: %d sent %d failed",
255 atomic_read(&sent[SMB2_NEGOTIATE_HE]),
256 atomic_read(&failed[SMB2_NEGOTIATE_HE]));
257 seq_printf(m, "\nSessionSetups: %d sent %d failed",
258 atomic_read(&sent[SMB2_SESSION_SETUP_HE]),
259 atomic_read(&failed[SMB2_SESSION_SETUP_HE]));
260 #define SMB2LOGOFF 0x0002 /* trivial request/resp */
261 seq_printf(m, "\nLogoffs: %d sent %d failed",
262 atomic_read(&sent[SMB2_LOGOFF_HE]),
263 atomic_read(&failed[SMB2_LOGOFF_HE]));
264 seq_printf(m, "\nTreeConnects: %d sent %d failed",
265 atomic_read(&sent[SMB2_TREE_CONNECT_HE]),
266 atomic_read(&failed[SMB2_TREE_CONNECT_HE]));
267 seq_printf(m, "\nTreeDisconnects: %d sent %d failed",
268 atomic_read(&sent[SMB2_TREE_DISCONNECT_HE]),
269 atomic_read(&failed[SMB2_TREE_DISCONNECT_HE]));
270 seq_printf(m, "\nCreates: %d sent %d failed",
271 atomic_read(&sent[SMB2_CREATE_HE]),
272 atomic_read(&failed[SMB2_CREATE_HE]));
273 seq_printf(m, "\nCloses: %d sent %d failed",
274 atomic_read(&sent[SMB2_CLOSE_HE]),
275 atomic_read(&failed[SMB2_CLOSE_HE]));
276 seq_printf(m, "\nFlushes: %d sent %d failed",
277 atomic_read(&sent[SMB2_FLUSH_HE]),
278 atomic_read(&failed[SMB2_FLUSH_HE]));
279 seq_printf(m, "\nReads: %d sent %d failed",
280 atomic_read(&sent[SMB2_READ_HE]),
281 atomic_read(&failed[SMB2_READ_HE]));
282 seq_printf(m, "\nWrites: %d sent %d failed",
283 atomic_read(&sent[SMB2_WRITE_HE]),
284 atomic_read(&failed[SMB2_WRITE_HE]));
285 seq_printf(m, "\nLocks: %d sent %d failed",
286 atomic_read(&sent[SMB2_LOCK_HE]),
287 atomic_read(&failed[SMB2_LOCK_HE]));
288 seq_printf(m, "\nIOCTLs: %d sent %d failed",
289 atomic_read(&sent[SMB2_IOCTL_HE]),
290 atomic_read(&failed[SMB2_IOCTL_HE]));
291 seq_printf(m, "\nCancels: %d sent %d failed",
292 atomic_read(&sent[SMB2_CANCEL_HE]),
293 atomic_read(&failed[SMB2_CANCEL_HE]));
294 seq_printf(m, "\nEchos: %d sent %d failed",
295 atomic_read(&sent[SMB2_ECHO_HE]),
296 atomic_read(&failed[SMB2_ECHO_HE]));
297 seq_printf(m, "\nQueryDirectories: %d sent %d failed",
298 atomic_read(&sent[SMB2_QUERY_DIRECTORY_HE]),
299 atomic_read(&failed[SMB2_QUERY_DIRECTORY_HE]));
300 seq_printf(m, "\nChangeNotifies: %d sent %d failed",
301 atomic_read(&sent[SMB2_CHANGE_NOTIFY_HE]),
302 atomic_read(&failed[SMB2_CHANGE_NOTIFY_HE]));
303 seq_printf(m, "\nQueryInfos: %d sent %d failed",
304 atomic_read(&sent[SMB2_QUERY_INFO_HE]),
305 atomic_read(&failed[SMB2_QUERY_INFO_HE]));
306 seq_printf(m, "\nSetInfos: %d sent %d failed",
307 atomic_read(&sent[SMB2_SET_INFO_HE]),
308 atomic_read(&failed[SMB2_SET_INFO_HE]));
309 seq_printf(m, "\nOplockBreaks: %d sent %d failed",
310 atomic_read(&sent[SMB2_OPLOCK_BREAK_HE]),
311 atomic_read(&failed[SMB2_OPLOCK_BREAK_HE]));
316 smb2_set_fid(struct cifsFileInfo *cfile, struct cifs_fid *fid, __u32 oplock)
318 /* struct cifsInodeInfo *cinode = CIFS_I(cfile->dentry->d_inode); */
319 cfile->fid.persistent_fid = fid->persistent_fid;
320 cfile->fid.volatile_fid = fid->volatile_fid;
321 /* cifs_set_oplock_level(cinode, oplock); */
322 /* cinode->can_cache_brlcks = cinode->clientCanCacheAll; */
326 smb2_close_file(const unsigned int xid, struct cifs_tcon *tcon,
327 struct cifs_fid *fid)
329 return SMB2_close(xid, tcon, fid->persistent_fid, fid->volatile_fid);
332 struct smb_version_operations smb21_operations = {
333 .setup_request = smb2_setup_request,
334 .setup_async_request = smb2_setup_async_request,
335 .check_receive = smb2_check_receive,
336 .add_credits = smb2_add_credits,
337 .set_credits = smb2_set_credits,
338 .get_credits_field = smb2_get_credits_field,
339 .get_credits = smb2_get_credits,
340 .get_next_mid = smb2_get_next_mid,
341 .find_mid = smb2_find_mid,
342 .check_message = smb2_check_message,
343 .dump_detail = smb2_dump_detail,
344 .clear_stats = smb2_clear_stats,
345 .print_stats = smb2_print_stats,
346 .need_neg = smb2_need_neg,
347 .negotiate = smb2_negotiate,
348 .sess_setup = SMB2_sess_setup,
349 .logoff = SMB2_logoff,
350 .tree_connect = SMB2_tcon,
351 .tree_disconnect = SMB2_tdis,
352 .is_path_accessible = smb2_is_path_accessible,
353 .can_echo = smb2_can_echo,
355 .query_path_info = smb2_query_path_info,
356 .get_srv_inum = smb2_get_srv_inum,
357 .query_file_info = smb2_query_file_info,
358 .build_path_to_root = smb2_build_path_to_root,
360 .mkdir_setinfo = smb2_mkdir_setinfo,
362 .unlink = smb2_unlink,
363 .open = smb2_open_file,
364 .set_fid = smb2_set_fid,
365 .close = smb2_close_file,
368 struct smb_version_values smb21_values = {
369 .version_string = SMB21_VERSION_STRING,
370 .header_size = sizeof(struct smb2_hdr),
371 .max_header_size = MAX_SMB2_HDR_SIZE,
372 .lock_cmd = SMB2_LOCK,
374 .cap_nt_find = SMB2_NT_FIND,
375 .cap_large_files = SMB2_LARGE_FILES,