1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * fs/cifs_debug.c
4 *
5 * Copyright (C) International Business Machines Corp., 2000,2005
6 *
7 * Modified by Steve French (sfrench@us.ibm.com)
8 */
9 #include <linux/fs.h>
10 #include <linux/string.h>
11 #include <linux/ctype.h>
12 #include <linux/module.h>
13 #include <linux/proc_fs.h>
14 #include <linux/uaccess.h>
15 #include "cifspdu.h"
16 #include "cifsglob.h"
17 #include "cifsproto.h"
18 #include "cifs_debug.h"
19 #include "cifsfs.h"
20 #include "fs_context.h"
21 #ifdef CONFIG_CIFS_DFS_UPCALL
22 #include "dfs_cache.h"
23 #endif
24 #ifdef CONFIG_CIFS_SMB_DIRECT
25 #include "smbdirect.h"
26 #endif
27 #include "cifs_swn.h"
28
29 void
cifs_dump_mem(char * label,void * data,int length)30 cifs_dump_mem(char *label, void *data, int length)
31 {
32 pr_debug("%s: dump of %d bytes of data at 0x%p\n", label, length, data);
33 print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 16, 4,
34 data, length, true);
35 }
36
cifs_dump_detail(void * buf,struct TCP_Server_Info * server)37 void cifs_dump_detail(void *buf, struct TCP_Server_Info *server)
38 {
39 #ifdef CONFIG_CIFS_DEBUG2
40 struct smb_hdr *smb = (struct smb_hdr *)buf;
41
42 cifs_dbg(VFS, "Cmd: %d Err: 0x%x Flags: 0x%x Flgs2: 0x%x Mid: %d Pid: %d\n",
43 smb->Command, smb->Status.CifsError,
44 smb->Flags, smb->Flags2, smb->Mid, smb->Pid);
45 cifs_dbg(VFS, "smb buf %p len %u\n", smb,
46 server->ops->calc_smb_size(smb, server));
47 #endif /* CONFIG_CIFS_DEBUG2 */
48 }
49
cifs_dump_mids(struct TCP_Server_Info * server)50 void cifs_dump_mids(struct TCP_Server_Info *server)
51 {
52 #ifdef CONFIG_CIFS_DEBUG2
53 struct list_head *tmp;
54 struct mid_q_entry *mid_entry;
55
56 if (server == NULL)
57 return;
58
59 cifs_dbg(VFS, "Dump pending requests:\n");
60 spin_lock(&GlobalMid_Lock);
61 list_for_each(tmp, &server->pending_mid_q) {
62 mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
63 cifs_dbg(VFS, "State: %d Cmd: %d Pid: %d Cbdata: %p Mid %llu\n",
64 mid_entry->mid_state,
65 le16_to_cpu(mid_entry->command),
66 mid_entry->pid,
67 mid_entry->callback_data,
68 mid_entry->mid);
69 #ifdef CONFIG_CIFS_STATS2
70 cifs_dbg(VFS, "IsLarge: %d buf: %p time rcv: %ld now: %ld\n",
71 mid_entry->large_buf,
72 mid_entry->resp_buf,
73 mid_entry->when_received,
74 jiffies);
75 #endif /* STATS2 */
76 cifs_dbg(VFS, "IsMult: %d IsEnd: %d\n",
77 mid_entry->multiRsp, mid_entry->multiEnd);
78 if (mid_entry->resp_buf) {
79 cifs_dump_detail(mid_entry->resp_buf, server);
80 cifs_dump_mem("existing buf: ",
81 mid_entry->resp_buf, 62);
82 }
83 }
84 spin_unlock(&GlobalMid_Lock);
85 #endif /* CONFIG_CIFS_DEBUG2 */
86 }
87
88 #ifdef CONFIG_PROC_FS
cifs_debug_tcon(struct seq_file * m,struct cifs_tcon * tcon)89 static void cifs_debug_tcon(struct seq_file *m, struct cifs_tcon *tcon)
90 {
91 __u32 dev_type = le32_to_cpu(tcon->fsDevInfo.DeviceType);
92
93 seq_printf(m, "%s Mounts: %d ", tcon->treeName, tcon->tc_count);
94 if (tcon->nativeFileSystem)
95 seq_printf(m, "Type: %s ", tcon->nativeFileSystem);
96 seq_printf(m, "DevInfo: 0x%x Attributes: 0x%x\n\tPathComponentMax: %d Status: %d",
97 le32_to_cpu(tcon->fsDevInfo.DeviceCharacteristics),
98 le32_to_cpu(tcon->fsAttrInfo.Attributes),
99 le32_to_cpu(tcon->fsAttrInfo.MaxPathNameComponentLength),
100 tcon->tidStatus);
101 if (dev_type == FILE_DEVICE_DISK)
102 seq_puts(m, " type: DISK ");
103 else if (dev_type == FILE_DEVICE_CD_ROM)
104 seq_puts(m, " type: CDROM ");
105 else
106 seq_printf(m, " type: %d ", dev_type);
107
108 seq_printf(m, "Serial Number: 0x%x", tcon->vol_serial_number);
109
110 if ((tcon->seal) ||
111 (tcon->ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA) ||
112 (tcon->share_flags & SHI1005_FLAGS_ENCRYPT_DATA))
113 seq_printf(m, " Encrypted");
114 if (tcon->nocase)
115 seq_printf(m, " nocase");
116 if (tcon->unix_ext)
117 seq_printf(m, " POSIX Extensions");
118 if (tcon->ses->server->ops->dump_share_caps)
119 tcon->ses->server->ops->dump_share_caps(m, tcon);
120 if (tcon->use_witness)
121 seq_puts(m, " Witness");
122
123 if (tcon->need_reconnect)
124 seq_puts(m, "\tDISCONNECTED ");
125 seq_putc(m, '\n');
126 }
127
128 static void
cifs_dump_channel(struct seq_file * m,int i,struct cifs_chan * chan)129 cifs_dump_channel(struct seq_file *m, int i, struct cifs_chan *chan)
130 {
131 struct TCP_Server_Info *server = chan->server;
132
133 seq_printf(m, "\n\n\t\tChannel: %d ConnectionId: 0x%llx"
134 "\n\t\tNumber of credits: %d Dialect 0x%x"
135 "\n\t\tTCP status: %d Instance: %d"
136 "\n\t\tLocal Users To Server: %d SecMode: 0x%x Req On Wire: %d"
137 "\n\t\tIn Send: %d In MaxReq Wait: %d",
138 i+1, server->conn_id,
139 server->credits,
140 server->dialect,
141 server->tcpStatus,
142 server->reconnect_instance,
143 server->srv_count,
144 server->sec_mode,
145 in_flight(server),
146 atomic_read(&server->in_send),
147 atomic_read(&server->num_waiters));
148 }
149
150 static void
cifs_dump_iface(struct seq_file * m,struct cifs_server_iface * iface)151 cifs_dump_iface(struct seq_file *m, struct cifs_server_iface *iface)
152 {
153 struct sockaddr_in *ipv4 = (struct sockaddr_in *)&iface->sockaddr;
154 struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)&iface->sockaddr;
155
156 seq_printf(m, "\tSpeed: %zu bps\n", iface->speed);
157 seq_puts(m, "\t\tCapabilities: ");
158 if (iface->rdma_capable)
159 seq_puts(m, "rdma ");
160 if (iface->rss_capable)
161 seq_puts(m, "rss ");
162 seq_putc(m, '\n');
163 if (iface->sockaddr.ss_family == AF_INET)
164 seq_printf(m, "\t\tIPv4: %pI4\n", &ipv4->sin_addr);
165 else if (iface->sockaddr.ss_family == AF_INET6)
166 seq_printf(m, "\t\tIPv6: %pI6\n", &ipv6->sin6_addr);
167 }
168
cifs_debug_files_proc_show(struct seq_file * m,void * v)169 static int cifs_debug_files_proc_show(struct seq_file *m, void *v)
170 {
171 struct list_head *stmp, *tmp, *tmp1, *tmp2;
172 struct TCP_Server_Info *server;
173 struct cifs_ses *ses;
174 struct cifs_tcon *tcon;
175 struct cifsFileInfo *cfile;
176
177 seq_puts(m, "# Version:1\n");
178 seq_puts(m, "# Format:\n");
179 seq_puts(m, "# <tree id> <persistent fid> <flags> <count> <pid> <uid>");
180 #ifdef CONFIG_CIFS_DEBUG2
181 seq_printf(m, " <filename> <mid>\n");
182 #else
183 seq_printf(m, " <filename>\n");
184 #endif /* CIFS_DEBUG2 */
185 spin_lock(&cifs_tcp_ses_lock);
186 list_for_each(stmp, &cifs_tcp_ses_list) {
187 server = list_entry(stmp, struct TCP_Server_Info,
188 tcp_ses_list);
189 list_for_each(tmp, &server->smb_ses_list) {
190 ses = list_entry(tmp, struct cifs_ses, smb_ses_list);
191 list_for_each(tmp1, &ses->tcon_list) {
192 tcon = list_entry(tmp1, struct cifs_tcon, tcon_list);
193 spin_lock(&tcon->open_file_lock);
194 list_for_each(tmp2, &tcon->openFileList) {
195 cfile = list_entry(tmp2, struct cifsFileInfo,
196 tlist);
197 seq_printf(m,
198 "0x%x 0x%llx 0x%x %d %d %d %pd",
199 tcon->tid,
200 cfile->fid.persistent_fid,
201 cfile->f_flags,
202 cfile->count,
203 cfile->pid,
204 from_kuid(&init_user_ns, cfile->uid),
205 cfile->dentry);
206 #ifdef CONFIG_CIFS_DEBUG2
207 seq_printf(m, " %llu\n", cfile->fid.mid);
208 #else
209 seq_printf(m, "\n");
210 #endif /* CIFS_DEBUG2 */
211 }
212 spin_unlock(&tcon->open_file_lock);
213 }
214 }
215 }
216 spin_unlock(&cifs_tcp_ses_lock);
217 seq_putc(m, '\n');
218 return 0;
219 }
220
cifs_debug_data_proc_show(struct seq_file * m,void * v)221 static int cifs_debug_data_proc_show(struct seq_file *m, void *v)
222 {
223 struct list_head *tmp1, *tmp2, *tmp3;
224 struct mid_q_entry *mid_entry;
225 struct TCP_Server_Info *server;
226 struct cifs_ses *ses;
227 struct cifs_tcon *tcon;
228 int c, i, j;
229
230 seq_puts(m,
231 "Display Internal CIFS Data Structures for Debugging\n"
232 "---------------------------------------------------\n");
233 seq_printf(m, "CIFS Version %s\n", CIFS_VERSION);
234 seq_printf(m, "Features:");
235 #ifdef CONFIG_CIFS_DFS_UPCALL
236 seq_printf(m, " DFS");
237 #endif
238 #ifdef CONFIG_CIFS_FSCACHE
239 seq_printf(m, ",FSCACHE");
240 #endif
241 #ifdef CONFIG_CIFS_SMB_DIRECT
242 seq_printf(m, ",SMB_DIRECT");
243 #endif
244 #ifdef CONFIG_CIFS_STATS2
245 seq_printf(m, ",STATS2");
246 #else
247 seq_printf(m, ",STATS");
248 #endif
249 #ifdef CONFIG_CIFS_DEBUG2
250 seq_printf(m, ",DEBUG2");
251 #elif defined(CONFIG_CIFS_DEBUG)
252 seq_printf(m, ",DEBUG");
253 #endif
254 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
255 seq_printf(m, ",ALLOW_INSECURE_LEGACY");
256 #endif
257 #ifdef CONFIG_CIFS_WEAK_PW_HASH
258 seq_printf(m, ",WEAK_PW_HASH");
259 #endif
260 #ifdef CONFIG_CIFS_POSIX
261 seq_printf(m, ",CIFS_POSIX");
262 #endif
263 #ifdef CONFIG_CIFS_UPCALL
264 seq_printf(m, ",UPCALL(SPNEGO)");
265 #endif
266 #ifdef CONFIG_CIFS_XATTR
267 seq_printf(m, ",XATTR");
268 #endif
269 seq_printf(m, ",ACL");
270 #ifdef CONFIG_CIFS_SWN_UPCALL
271 seq_puts(m, ",WITNESS");
272 #endif
273 seq_putc(m, '\n');
274 seq_printf(m, "CIFSMaxBufSize: %d\n", CIFSMaxBufSize);
275 seq_printf(m, "Active VFS Requests: %d\n", GlobalTotalActiveXid);
276
277 seq_printf(m, "\nServers: ");
278
279 c = 0;
280 spin_lock(&cifs_tcp_ses_lock);
281 list_for_each(tmp1, &cifs_tcp_ses_list) {
282 server = list_entry(tmp1, struct TCP_Server_Info,
283 tcp_ses_list);
284
285 /* channel info will be printed as a part of sessions below */
286 if (server->is_channel)
287 continue;
288
289 c++;
290 seq_printf(m, "\n%d) ConnectionId: 0x%llx ",
291 c, server->conn_id);
292
293 if (server->hostname)
294 seq_printf(m, "Hostname: %s ", server->hostname);
295 #ifdef CONFIG_CIFS_SMB_DIRECT
296 if (!server->rdma)
297 goto skip_rdma;
298
299 if (!server->smbd_conn) {
300 seq_printf(m, "\nSMBDirect transport not available");
301 goto skip_rdma;
302 }
303
304 seq_printf(m, "\nSMBDirect (in hex) protocol version: %x "
305 "transport status: %x",
306 server->smbd_conn->protocol,
307 server->smbd_conn->transport_status);
308 seq_printf(m, "\nConn receive_credit_max: %x "
309 "send_credit_target: %x max_send_size: %x",
310 server->smbd_conn->receive_credit_max,
311 server->smbd_conn->send_credit_target,
312 server->smbd_conn->max_send_size);
313 seq_printf(m, "\nConn max_fragmented_recv_size: %x "
314 "max_fragmented_send_size: %x max_receive_size:%x",
315 server->smbd_conn->max_fragmented_recv_size,
316 server->smbd_conn->max_fragmented_send_size,
317 server->smbd_conn->max_receive_size);
318 seq_printf(m, "\nConn keep_alive_interval: %x "
319 "max_readwrite_size: %x rdma_readwrite_threshold: %x",
320 server->smbd_conn->keep_alive_interval,
321 server->smbd_conn->max_readwrite_size,
322 server->smbd_conn->rdma_readwrite_threshold);
323 seq_printf(m, "\nDebug count_get_receive_buffer: %x "
324 "count_put_receive_buffer: %x count_send_empty: %x",
325 server->smbd_conn->count_get_receive_buffer,
326 server->smbd_conn->count_put_receive_buffer,
327 server->smbd_conn->count_send_empty);
328 seq_printf(m, "\nRead Queue count_reassembly_queue: %x "
329 "count_enqueue_reassembly_queue: %x "
330 "count_dequeue_reassembly_queue: %x "
331 "fragment_reassembly_remaining: %x "
332 "reassembly_data_length: %x "
333 "reassembly_queue_length: %x",
334 server->smbd_conn->count_reassembly_queue,
335 server->smbd_conn->count_enqueue_reassembly_queue,
336 server->smbd_conn->count_dequeue_reassembly_queue,
337 server->smbd_conn->fragment_reassembly_remaining,
338 server->smbd_conn->reassembly_data_length,
339 server->smbd_conn->reassembly_queue_length);
340 seq_printf(m, "\nCurrent Credits send_credits: %x "
341 "receive_credits: %x receive_credit_target: %x",
342 atomic_read(&server->smbd_conn->send_credits),
343 atomic_read(&server->smbd_conn->receive_credits),
344 server->smbd_conn->receive_credit_target);
345 seq_printf(m, "\nPending send_pending: %x ",
346 atomic_read(&server->smbd_conn->send_pending));
347 seq_printf(m, "\nReceive buffers count_receive_queue: %x "
348 "count_empty_packet_queue: %x",
349 server->smbd_conn->count_receive_queue,
350 server->smbd_conn->count_empty_packet_queue);
351 seq_printf(m, "\nMR responder_resources: %x "
352 "max_frmr_depth: %x mr_type: %x",
353 server->smbd_conn->responder_resources,
354 server->smbd_conn->max_frmr_depth,
355 server->smbd_conn->mr_type);
356 seq_printf(m, "\nMR mr_ready_count: %x mr_used_count: %x",
357 atomic_read(&server->smbd_conn->mr_ready_count),
358 atomic_read(&server->smbd_conn->mr_used_count));
359 skip_rdma:
360 #endif
361 seq_printf(m, "\nNumber of credits: %d Dialect 0x%x",
362 server->credits, server->dialect);
363 if (server->compress_algorithm == SMB3_COMPRESS_LZNT1)
364 seq_printf(m, " COMPRESS_LZNT1");
365 else if (server->compress_algorithm == SMB3_COMPRESS_LZ77)
366 seq_printf(m, " COMPRESS_LZ77");
367 else if (server->compress_algorithm == SMB3_COMPRESS_LZ77_HUFF)
368 seq_printf(m, " COMPRESS_LZ77_HUFF");
369 if (server->sign)
370 seq_printf(m, " signed");
371 if (server->posix_ext_supported)
372 seq_printf(m, " posix");
373
374 if (server->rdma)
375 seq_printf(m, "\nRDMA ");
376 seq_printf(m, "\nTCP status: %d Instance: %d"
377 "\nLocal Users To Server: %d SecMode: 0x%x Req On Wire: %d",
378 server->tcpStatus,
379 server->reconnect_instance,
380 server->srv_count,
381 server->sec_mode, in_flight(server));
382
383 seq_printf(m, "\nIn Send: %d In MaxReq Wait: %d",
384 atomic_read(&server->in_send),
385 atomic_read(&server->num_waiters));
386
387 seq_printf(m, "\n\n\tSessions: ");
388 i = 0;
389 list_for_each(tmp2, &server->smb_ses_list) {
390 ses = list_entry(tmp2, struct cifs_ses,
391 smb_ses_list);
392 i++;
393 if ((ses->serverDomain == NULL) ||
394 (ses->serverOS == NULL) ||
395 (ses->serverNOS == NULL)) {
396 seq_printf(m, "\n\t%d) Address: %s Uses: %d Capability: 0x%x\tSession Status: %d ",
397 i, ses->ip_addr, ses->ses_count,
398 ses->capabilities, ses->status);
399 if (ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST)
400 seq_printf(m, "Guest ");
401 else if (ses->session_flags & SMB2_SESSION_FLAG_IS_NULL)
402 seq_printf(m, "Anonymous ");
403 } else {
404 seq_printf(m,
405 "\n\t%d) Name: %s Domain: %s Uses: %d OS: %s "
406 "\n\tNOS: %s\tCapability: 0x%x"
407 "\n\tSMB session status: %d ",
408 i, ses->ip_addr, ses->serverDomain,
409 ses->ses_count, ses->serverOS, ses->serverNOS,
410 ses->capabilities, ses->status);
411 }
412
413 seq_printf(m, "\n\tSecurity type: %s ",
414 get_security_type_str(server->ops->select_sectype(server, ses->sectype)));
415
416 /* dump session id helpful for use with network trace */
417 seq_printf(m, " SessionId: 0x%llx", ses->Suid);
418 if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA)
419 seq_puts(m, " encrypted");
420 if (ses->sign)
421 seq_puts(m, " signed");
422
423 seq_printf(m, "\n\tUser: %d Cred User: %d",
424 from_kuid(&init_user_ns, ses->linux_uid),
425 from_kuid(&init_user_ns, ses->cred_uid));
426
427 if (ses->chan_count > 1) {
428 seq_printf(m, "\n\n\tExtra Channels: %zu ",
429 ses->chan_count-1);
430 for (j = 1; j < ses->chan_count; j++)
431 cifs_dump_channel(m, j, &ses->chans[j]);
432 }
433
434 seq_puts(m, "\n\n\tShares: ");
435 j = 0;
436
437 seq_printf(m, "\n\t%d) IPC: ", j);
438 if (ses->tcon_ipc)
439 cifs_debug_tcon(m, ses->tcon_ipc);
440 else
441 seq_puts(m, "none\n");
442
443 list_for_each(tmp3, &ses->tcon_list) {
444 tcon = list_entry(tmp3, struct cifs_tcon,
445 tcon_list);
446 ++j;
447 seq_printf(m, "\n\t%d) ", j);
448 cifs_debug_tcon(m, tcon);
449 }
450
451 spin_lock(&ses->iface_lock);
452 if (ses->iface_count)
453 seq_printf(m, "\n\n\tServer interfaces: %zu",
454 ses->iface_count);
455 for (j = 0; j < ses->iface_count; j++) {
456 struct cifs_server_iface *iface;
457
458 iface = &ses->iface_list[j];
459 seq_printf(m, "\n\t%d)", j+1);
460 cifs_dump_iface(m, iface);
461 if (is_ses_using_iface(ses, iface))
462 seq_puts(m, "\t\t[CONNECTED]\n");
463 }
464 spin_unlock(&ses->iface_lock);
465 }
466 if (i == 0)
467 seq_printf(m, "\n\t\t[NONE]");
468
469 seq_puts(m, "\n\n\tMIDs: ");
470 spin_lock(&GlobalMid_Lock);
471 list_for_each(tmp3, &server->pending_mid_q) {
472 mid_entry = list_entry(tmp3, struct mid_q_entry,
473 qhead);
474 seq_printf(m, "\n\tState: %d com: %d pid:"
475 " %d cbdata: %p mid %llu\n",
476 mid_entry->mid_state,
477 le16_to_cpu(mid_entry->command),
478 mid_entry->pid,
479 mid_entry->callback_data,
480 mid_entry->mid);
481 }
482 spin_unlock(&GlobalMid_Lock);
483 seq_printf(m, "\n--\n");
484 }
485 if (c == 0)
486 seq_printf(m, "\n\t[NONE]");
487
488 spin_unlock(&cifs_tcp_ses_lock);
489 seq_putc(m, '\n');
490 cifs_swn_dump(m);
491
492 /* BB add code to dump additional info such as TCP session info now */
493 return 0;
494 }
495
cifs_stats_proc_write(struct file * file,const char __user * buffer,size_t count,loff_t * ppos)496 static ssize_t cifs_stats_proc_write(struct file *file,
497 const char __user *buffer, size_t count, loff_t *ppos)
498 {
499 bool bv;
500 int rc;
501 struct list_head *tmp1, *tmp2, *tmp3;
502 struct TCP_Server_Info *server;
503 struct cifs_ses *ses;
504 struct cifs_tcon *tcon;
505
506 rc = kstrtobool_from_user(buffer, count, &bv);
507 if (rc == 0) {
508 #ifdef CONFIG_CIFS_STATS2
509 int i;
510
511 atomic_set(&totBufAllocCount, 0);
512 atomic_set(&totSmBufAllocCount, 0);
513 #endif /* CONFIG_CIFS_STATS2 */
514 atomic_set(&tcpSesReconnectCount, 0);
515 atomic_set(&tconInfoReconnectCount, 0);
516
517 spin_lock(&GlobalMid_Lock);
518 GlobalMaxActiveXid = 0;
519 GlobalCurrentXid = 0;
520 spin_unlock(&GlobalMid_Lock);
521 spin_lock(&cifs_tcp_ses_lock);
522 list_for_each(tmp1, &cifs_tcp_ses_list) {
523 server = list_entry(tmp1, struct TCP_Server_Info,
524 tcp_ses_list);
525 server->max_in_flight = 0;
526 #ifdef CONFIG_CIFS_STATS2
527 for (i = 0; i < NUMBER_OF_SMB2_COMMANDS; i++) {
528 atomic_set(&server->num_cmds[i], 0);
529 atomic_set(&server->smb2slowcmd[i], 0);
530 server->time_per_cmd[i] = 0;
531 server->slowest_cmd[i] = 0;
532 server->fastest_cmd[0] = 0;
533 }
534 #endif /* CONFIG_CIFS_STATS2 */
535 list_for_each(tmp2, &server->smb_ses_list) {
536 ses = list_entry(tmp2, struct cifs_ses,
537 smb_ses_list);
538 list_for_each(tmp3, &ses->tcon_list) {
539 tcon = list_entry(tmp3,
540 struct cifs_tcon,
541 tcon_list);
542 atomic_set(&tcon->num_smbs_sent, 0);
543 spin_lock(&tcon->stat_lock);
544 tcon->bytes_read = 0;
545 tcon->bytes_written = 0;
546 spin_unlock(&tcon->stat_lock);
547 if (server->ops->clear_stats)
548 server->ops->clear_stats(tcon);
549 }
550 }
551 }
552 spin_unlock(&cifs_tcp_ses_lock);
553 } else {
554 return rc;
555 }
556
557 return count;
558 }
559
cifs_stats_proc_show(struct seq_file * m,void * v)560 static int cifs_stats_proc_show(struct seq_file *m, void *v)
561 {
562 int i;
563 #ifdef CONFIG_CIFS_STATS2
564 int j;
565 #endif /* STATS2 */
566 struct list_head *tmp1, *tmp2, *tmp3;
567 struct TCP_Server_Info *server;
568 struct cifs_ses *ses;
569 struct cifs_tcon *tcon;
570
571 seq_printf(m, "Resources in use\nCIFS Session: %d\n",
572 sesInfoAllocCount.counter);
573 seq_printf(m, "Share (unique mount targets): %d\n",
574 tconInfoAllocCount.counter);
575 seq_printf(m, "SMB Request/Response Buffer: %d Pool size: %d\n",
576 bufAllocCount.counter,
577 cifs_min_rcv + tcpSesAllocCount.counter);
578 seq_printf(m, "SMB Small Req/Resp Buffer: %d Pool size: %d\n",
579 smBufAllocCount.counter, cifs_min_small);
580 #ifdef CONFIG_CIFS_STATS2
581 seq_printf(m, "Total Large %d Small %d Allocations\n",
582 atomic_read(&totBufAllocCount),
583 atomic_read(&totSmBufAllocCount));
584 #endif /* CONFIG_CIFS_STATS2 */
585
586 seq_printf(m, "Operations (MIDs): %d\n", atomic_read(&midCount));
587 seq_printf(m,
588 "\n%d session %d share reconnects\n",
589 tcpSesReconnectCount.counter, tconInfoReconnectCount.counter);
590
591 seq_printf(m,
592 "Total vfs operations: %d maximum at one time: %d\n",
593 GlobalCurrentXid, GlobalMaxActiveXid);
594
595 i = 0;
596 spin_lock(&cifs_tcp_ses_lock);
597 list_for_each(tmp1, &cifs_tcp_ses_list) {
598 server = list_entry(tmp1, struct TCP_Server_Info,
599 tcp_ses_list);
600 seq_printf(m, "\nMax requests in flight: %d", server->max_in_flight);
601 #ifdef CONFIG_CIFS_STATS2
602 seq_puts(m, "\nTotal time spent processing by command. Time ");
603 seq_printf(m, "units are jiffies (%d per second)\n", HZ);
604 seq_puts(m, " SMB3 CMD\tNumber\tTotal Time\tFastest\tSlowest\n");
605 seq_puts(m, " --------\t------\t----------\t-------\t-------\n");
606 for (j = 0; j < NUMBER_OF_SMB2_COMMANDS; j++)
607 seq_printf(m, " %d\t\t%d\t%llu\t\t%u\t%u\n", j,
608 atomic_read(&server->num_cmds[j]),
609 server->time_per_cmd[j],
610 server->fastest_cmd[j],
611 server->slowest_cmd[j]);
612 for (j = 0; j < NUMBER_OF_SMB2_COMMANDS; j++)
613 if (atomic_read(&server->smb2slowcmd[j]))
614 seq_printf(m, " %d slow responses from %s for command %d\n",
615 atomic_read(&server->smb2slowcmd[j]),
616 server->hostname, j);
617 #endif /* STATS2 */
618 list_for_each(tmp2, &server->smb_ses_list) {
619 ses = list_entry(tmp2, struct cifs_ses,
620 smb_ses_list);
621 list_for_each(tmp3, &ses->tcon_list) {
622 tcon = list_entry(tmp3,
623 struct cifs_tcon,
624 tcon_list);
625 i++;
626 seq_printf(m, "\n%d) %s", i, tcon->treeName);
627 if (tcon->need_reconnect)
628 seq_puts(m, "\tDISCONNECTED ");
629 seq_printf(m, "\nSMBs: %d",
630 atomic_read(&tcon->num_smbs_sent));
631 if (server->ops->print_stats)
632 server->ops->print_stats(m, tcon);
633 }
634 }
635 }
636 spin_unlock(&cifs_tcp_ses_lock);
637
638 seq_putc(m, '\n');
639 return 0;
640 }
641
cifs_stats_proc_open(struct inode * inode,struct file * file)642 static int cifs_stats_proc_open(struct inode *inode, struct file *file)
643 {
644 return single_open(file, cifs_stats_proc_show, NULL);
645 }
646
647 static const struct proc_ops cifs_stats_proc_ops = {
648 .proc_open = cifs_stats_proc_open,
649 .proc_read = seq_read,
650 .proc_lseek = seq_lseek,
651 .proc_release = single_release,
652 .proc_write = cifs_stats_proc_write,
653 };
654
655 #ifdef CONFIG_CIFS_SMB_DIRECT
656 #define PROC_FILE_DEFINE(name) \
657 static ssize_t name##_write(struct file *file, const char __user *buffer, \
658 size_t count, loff_t *ppos) \
659 { \
660 int rc; \
661 rc = kstrtoint_from_user(buffer, count, 10, & name); \
662 if (rc) \
663 return rc; \
664 return count; \
665 } \
666 static int name##_proc_show(struct seq_file *m, void *v) \
667 { \
668 seq_printf(m, "%d\n", name ); \
669 return 0; \
670 } \
671 static int name##_open(struct inode *inode, struct file *file) \
672 { \
673 return single_open(file, name##_proc_show, NULL); \
674 } \
675 \
676 static const struct proc_ops cifs_##name##_proc_fops = { \
677 .proc_open = name##_open, \
678 .proc_read = seq_read, \
679 .proc_lseek = seq_lseek, \
680 .proc_release = single_release, \
681 .proc_write = name##_write, \
682 }
683
684 PROC_FILE_DEFINE(rdma_readwrite_threshold);
685 PROC_FILE_DEFINE(smbd_max_frmr_depth);
686 PROC_FILE_DEFINE(smbd_keep_alive_interval);
687 PROC_FILE_DEFINE(smbd_max_receive_size);
688 PROC_FILE_DEFINE(smbd_max_fragmented_recv_size);
689 PROC_FILE_DEFINE(smbd_max_send_size);
690 PROC_FILE_DEFINE(smbd_send_credit_target);
691 PROC_FILE_DEFINE(smbd_receive_credit_max);
692 #endif
693
694 static struct proc_dir_entry *proc_fs_cifs;
695 static const struct proc_ops cifsFYI_proc_ops;
696 static const struct proc_ops cifs_lookup_cache_proc_ops;
697 static const struct proc_ops traceSMB_proc_ops;
698 static const struct proc_ops cifs_security_flags_proc_ops;
699 static const struct proc_ops cifs_linux_ext_proc_ops;
700 static const struct proc_ops cifs_mount_params_proc_ops;
701
702 void
cifs_proc_init(void)703 cifs_proc_init(void)
704 {
705 proc_fs_cifs = proc_mkdir("fs/cifs", NULL);
706 if (proc_fs_cifs == NULL)
707 return;
708
709 proc_create_single("DebugData", 0, proc_fs_cifs,
710 cifs_debug_data_proc_show);
711
712 proc_create_single("open_files", 0400, proc_fs_cifs,
713 cifs_debug_files_proc_show);
714
715 proc_create("Stats", 0644, proc_fs_cifs, &cifs_stats_proc_ops);
716 proc_create("cifsFYI", 0644, proc_fs_cifs, &cifsFYI_proc_ops);
717 proc_create("traceSMB", 0644, proc_fs_cifs, &traceSMB_proc_ops);
718 proc_create("LinuxExtensionsEnabled", 0644, proc_fs_cifs,
719 &cifs_linux_ext_proc_ops);
720 proc_create("SecurityFlags", 0644, proc_fs_cifs,
721 &cifs_security_flags_proc_ops);
722 proc_create("LookupCacheEnabled", 0644, proc_fs_cifs,
723 &cifs_lookup_cache_proc_ops);
724
725 proc_create("mount_params", 0444, proc_fs_cifs, &cifs_mount_params_proc_ops);
726
727 #ifdef CONFIG_CIFS_DFS_UPCALL
728 proc_create("dfscache", 0644, proc_fs_cifs, &dfscache_proc_ops);
729 #endif
730
731 #ifdef CONFIG_CIFS_SMB_DIRECT
732 proc_create("rdma_readwrite_threshold", 0644, proc_fs_cifs,
733 &cifs_rdma_readwrite_threshold_proc_fops);
734 proc_create("smbd_max_frmr_depth", 0644, proc_fs_cifs,
735 &cifs_smbd_max_frmr_depth_proc_fops);
736 proc_create("smbd_keep_alive_interval", 0644, proc_fs_cifs,
737 &cifs_smbd_keep_alive_interval_proc_fops);
738 proc_create("smbd_max_receive_size", 0644, proc_fs_cifs,
739 &cifs_smbd_max_receive_size_proc_fops);
740 proc_create("smbd_max_fragmented_recv_size", 0644, proc_fs_cifs,
741 &cifs_smbd_max_fragmented_recv_size_proc_fops);
742 proc_create("smbd_max_send_size", 0644, proc_fs_cifs,
743 &cifs_smbd_max_send_size_proc_fops);
744 proc_create("smbd_send_credit_target", 0644, proc_fs_cifs,
745 &cifs_smbd_send_credit_target_proc_fops);
746 proc_create("smbd_receive_credit_max", 0644, proc_fs_cifs,
747 &cifs_smbd_receive_credit_max_proc_fops);
748 #endif
749 }
750
751 void
cifs_proc_clean(void)752 cifs_proc_clean(void)
753 {
754 if (proc_fs_cifs == NULL)
755 return;
756
757 remove_proc_entry("DebugData", proc_fs_cifs);
758 remove_proc_entry("open_files", proc_fs_cifs);
759 remove_proc_entry("cifsFYI", proc_fs_cifs);
760 remove_proc_entry("traceSMB", proc_fs_cifs);
761 remove_proc_entry("Stats", proc_fs_cifs);
762 remove_proc_entry("SecurityFlags", proc_fs_cifs);
763 remove_proc_entry("LinuxExtensionsEnabled", proc_fs_cifs);
764 remove_proc_entry("LookupCacheEnabled", proc_fs_cifs);
765 remove_proc_entry("mount_params", proc_fs_cifs);
766
767 #ifdef CONFIG_CIFS_DFS_UPCALL
768 remove_proc_entry("dfscache", proc_fs_cifs);
769 #endif
770 #ifdef CONFIG_CIFS_SMB_DIRECT
771 remove_proc_entry("rdma_readwrite_threshold", proc_fs_cifs);
772 remove_proc_entry("smbd_max_frmr_depth", proc_fs_cifs);
773 remove_proc_entry("smbd_keep_alive_interval", proc_fs_cifs);
774 remove_proc_entry("smbd_max_receive_size", proc_fs_cifs);
775 remove_proc_entry("smbd_max_fragmented_recv_size", proc_fs_cifs);
776 remove_proc_entry("smbd_max_send_size", proc_fs_cifs);
777 remove_proc_entry("smbd_send_credit_target", proc_fs_cifs);
778 remove_proc_entry("smbd_receive_credit_max", proc_fs_cifs);
779 #endif
780 remove_proc_entry("fs/cifs", NULL);
781 }
782
cifsFYI_proc_show(struct seq_file * m,void * v)783 static int cifsFYI_proc_show(struct seq_file *m, void *v)
784 {
785 seq_printf(m, "%d\n", cifsFYI);
786 return 0;
787 }
788
cifsFYI_proc_open(struct inode * inode,struct file * file)789 static int cifsFYI_proc_open(struct inode *inode, struct file *file)
790 {
791 return single_open(file, cifsFYI_proc_show, NULL);
792 }
793
cifsFYI_proc_write(struct file * file,const char __user * buffer,size_t count,loff_t * ppos)794 static ssize_t cifsFYI_proc_write(struct file *file, const char __user *buffer,
795 size_t count, loff_t *ppos)
796 {
797 char c[2] = { '\0' };
798 bool bv;
799 int rc;
800
801 rc = get_user(c[0], buffer);
802 if (rc)
803 return rc;
804 if (strtobool(c, &bv) == 0)
805 cifsFYI = bv;
806 else if ((c[0] > '1') && (c[0] <= '9'))
807 cifsFYI = (int) (c[0] - '0'); /* see cifs_debug.h for meanings */
808 else
809 return -EINVAL;
810
811 return count;
812 }
813
814 static const struct proc_ops cifsFYI_proc_ops = {
815 .proc_open = cifsFYI_proc_open,
816 .proc_read = seq_read,
817 .proc_lseek = seq_lseek,
818 .proc_release = single_release,
819 .proc_write = cifsFYI_proc_write,
820 };
821
cifs_linux_ext_proc_show(struct seq_file * m,void * v)822 static int cifs_linux_ext_proc_show(struct seq_file *m, void *v)
823 {
824 seq_printf(m, "%d\n", linuxExtEnabled);
825 return 0;
826 }
827
cifs_linux_ext_proc_open(struct inode * inode,struct file * file)828 static int cifs_linux_ext_proc_open(struct inode *inode, struct file *file)
829 {
830 return single_open(file, cifs_linux_ext_proc_show, NULL);
831 }
832
cifs_linux_ext_proc_write(struct file * file,const char __user * buffer,size_t count,loff_t * ppos)833 static ssize_t cifs_linux_ext_proc_write(struct file *file,
834 const char __user *buffer, size_t count, loff_t *ppos)
835 {
836 int rc;
837
838 rc = kstrtobool_from_user(buffer, count, &linuxExtEnabled);
839 if (rc)
840 return rc;
841
842 return count;
843 }
844
845 static const struct proc_ops cifs_linux_ext_proc_ops = {
846 .proc_open = cifs_linux_ext_proc_open,
847 .proc_read = seq_read,
848 .proc_lseek = seq_lseek,
849 .proc_release = single_release,
850 .proc_write = cifs_linux_ext_proc_write,
851 };
852
cifs_lookup_cache_proc_show(struct seq_file * m,void * v)853 static int cifs_lookup_cache_proc_show(struct seq_file *m, void *v)
854 {
855 seq_printf(m, "%d\n", lookupCacheEnabled);
856 return 0;
857 }
858
cifs_lookup_cache_proc_open(struct inode * inode,struct file * file)859 static int cifs_lookup_cache_proc_open(struct inode *inode, struct file *file)
860 {
861 return single_open(file, cifs_lookup_cache_proc_show, NULL);
862 }
863
cifs_lookup_cache_proc_write(struct file * file,const char __user * buffer,size_t count,loff_t * ppos)864 static ssize_t cifs_lookup_cache_proc_write(struct file *file,
865 const char __user *buffer, size_t count, loff_t *ppos)
866 {
867 int rc;
868
869 rc = kstrtobool_from_user(buffer, count, &lookupCacheEnabled);
870 if (rc)
871 return rc;
872
873 return count;
874 }
875
876 static const struct proc_ops cifs_lookup_cache_proc_ops = {
877 .proc_open = cifs_lookup_cache_proc_open,
878 .proc_read = seq_read,
879 .proc_lseek = seq_lseek,
880 .proc_release = single_release,
881 .proc_write = cifs_lookup_cache_proc_write,
882 };
883
traceSMB_proc_show(struct seq_file * m,void * v)884 static int traceSMB_proc_show(struct seq_file *m, void *v)
885 {
886 seq_printf(m, "%d\n", traceSMB);
887 return 0;
888 }
889
traceSMB_proc_open(struct inode * inode,struct file * file)890 static int traceSMB_proc_open(struct inode *inode, struct file *file)
891 {
892 return single_open(file, traceSMB_proc_show, NULL);
893 }
894
traceSMB_proc_write(struct file * file,const char __user * buffer,size_t count,loff_t * ppos)895 static ssize_t traceSMB_proc_write(struct file *file, const char __user *buffer,
896 size_t count, loff_t *ppos)
897 {
898 int rc;
899
900 rc = kstrtobool_from_user(buffer, count, &traceSMB);
901 if (rc)
902 return rc;
903
904 return count;
905 }
906
907 static const struct proc_ops traceSMB_proc_ops = {
908 .proc_open = traceSMB_proc_open,
909 .proc_read = seq_read,
910 .proc_lseek = seq_lseek,
911 .proc_release = single_release,
912 .proc_write = traceSMB_proc_write,
913 };
914
cifs_security_flags_proc_show(struct seq_file * m,void * v)915 static int cifs_security_flags_proc_show(struct seq_file *m, void *v)
916 {
917 seq_printf(m, "0x%x\n", global_secflags);
918 return 0;
919 }
920
cifs_security_flags_proc_open(struct inode * inode,struct file * file)921 static int cifs_security_flags_proc_open(struct inode *inode, struct file *file)
922 {
923 return single_open(file, cifs_security_flags_proc_show, NULL);
924 }
925
926 /*
927 * Ensure that if someone sets a MUST flag, that we disable all other MAY
928 * flags except for the ones corresponding to the given MUST flag. If there are
929 * multiple MUST flags, then try to prefer more secure ones.
930 */
931 static void
cifs_security_flags_handle_must_flags(unsigned int * flags)932 cifs_security_flags_handle_must_flags(unsigned int *flags)
933 {
934 unsigned int signflags = *flags & CIFSSEC_MUST_SIGN;
935
936 if ((*flags & CIFSSEC_MUST_KRB5) == CIFSSEC_MUST_KRB5)
937 *flags = CIFSSEC_MUST_KRB5;
938 else if ((*flags & CIFSSEC_MUST_NTLMSSP) == CIFSSEC_MUST_NTLMSSP)
939 *flags = CIFSSEC_MUST_NTLMSSP;
940 else if ((*flags & CIFSSEC_MUST_NTLMV2) == CIFSSEC_MUST_NTLMV2)
941 *flags = CIFSSEC_MUST_NTLMV2;
942 else if ((*flags & CIFSSEC_MUST_NTLM) == CIFSSEC_MUST_NTLM)
943 *flags = CIFSSEC_MUST_NTLM;
944 else if (CIFSSEC_MUST_LANMAN &&
945 (*flags & CIFSSEC_MUST_LANMAN) == CIFSSEC_MUST_LANMAN)
946 *flags = CIFSSEC_MUST_LANMAN;
947 else if (CIFSSEC_MUST_PLNTXT &&
948 (*flags & CIFSSEC_MUST_PLNTXT) == CIFSSEC_MUST_PLNTXT)
949 *flags = CIFSSEC_MUST_PLNTXT;
950
951 *flags |= signflags;
952 }
953
cifs_security_flags_proc_write(struct file * file,const char __user * buffer,size_t count,loff_t * ppos)954 static ssize_t cifs_security_flags_proc_write(struct file *file,
955 const char __user *buffer, size_t count, loff_t *ppos)
956 {
957 int rc;
958 unsigned int flags;
959 char flags_string[12];
960 bool bv;
961
962 if ((count < 1) || (count > 11))
963 return -EINVAL;
964
965 memset(flags_string, 0, 12);
966
967 if (copy_from_user(flags_string, buffer, count))
968 return -EFAULT;
969
970 if (count < 3) {
971 /* single char or single char followed by null */
972 if (strtobool(flags_string, &bv) == 0) {
973 global_secflags = bv ? CIFSSEC_MAX : CIFSSEC_DEF;
974 return count;
975 } else if (!isdigit(flags_string[0])) {
976 cifs_dbg(VFS, "Invalid SecurityFlags: %s\n",
977 flags_string);
978 return -EINVAL;
979 }
980 }
981
982 /* else we have a number */
983 rc = kstrtouint(flags_string, 0, &flags);
984 if (rc) {
985 cifs_dbg(VFS, "Invalid SecurityFlags: %s\n",
986 flags_string);
987 return rc;
988 }
989
990 cifs_dbg(FYI, "sec flags 0x%x\n", flags);
991
992 if (flags == 0) {
993 cifs_dbg(VFS, "Invalid SecurityFlags: %s\n", flags_string);
994 return -EINVAL;
995 }
996
997 if (flags & ~CIFSSEC_MASK) {
998 cifs_dbg(VFS, "Unsupported security flags: 0x%x\n",
999 flags & ~CIFSSEC_MASK);
1000 return -EINVAL;
1001 }
1002
1003 cifs_security_flags_handle_must_flags(&flags);
1004
1005 /* flags look ok - update the global security flags for cifs module */
1006 global_secflags = flags;
1007 if (global_secflags & CIFSSEC_MUST_SIGN) {
1008 /* requiring signing implies signing is allowed */
1009 global_secflags |= CIFSSEC_MAY_SIGN;
1010 cifs_dbg(FYI, "packet signing now required\n");
1011 } else if ((global_secflags & CIFSSEC_MAY_SIGN) == 0) {
1012 cifs_dbg(FYI, "packet signing disabled\n");
1013 }
1014 /* BB should we turn on MAY flags for other MUST options? */
1015 return count;
1016 }
1017
1018 static const struct proc_ops cifs_security_flags_proc_ops = {
1019 .proc_open = cifs_security_flags_proc_open,
1020 .proc_read = seq_read,
1021 .proc_lseek = seq_lseek,
1022 .proc_release = single_release,
1023 .proc_write = cifs_security_flags_proc_write,
1024 };
1025
1026 /* To make it easier to debug, can help to show mount params */
cifs_mount_params_proc_show(struct seq_file * m,void * v)1027 static int cifs_mount_params_proc_show(struct seq_file *m, void *v)
1028 {
1029 const struct fs_parameter_spec *p;
1030 const char *type;
1031
1032 for (p = smb3_fs_parameters; p->name; p++) {
1033 /* cannot use switch with pointers... */
1034 if (!p->type) {
1035 if (p->flags == fs_param_neg_with_no)
1036 type = "noflag";
1037 else
1038 type = "flag";
1039 } else if (p->type == fs_param_is_bool)
1040 type = "bool";
1041 else if (p->type == fs_param_is_u32)
1042 type = "u32";
1043 else if (p->type == fs_param_is_u64)
1044 type = "u64";
1045 else if (p->type == fs_param_is_string)
1046 type = "string";
1047 else
1048 type = "unknown";
1049
1050 seq_printf(m, "%s:%s\n", p->name, type);
1051 }
1052
1053 return 0;
1054 }
1055
cifs_mount_params_proc_open(struct inode * inode,struct file * file)1056 static int cifs_mount_params_proc_open(struct inode *inode, struct file *file)
1057 {
1058 return single_open(file, cifs_mount_params_proc_show, NULL);
1059 }
1060
1061 static const struct proc_ops cifs_mount_params_proc_ops = {
1062 .proc_open = cifs_mount_params_proc_open,
1063 .proc_read = seq_read,
1064 .proc_lseek = seq_lseek,
1065 .proc_release = single_release,
1066 /* No need for write for now */
1067 /* .proc_write = cifs_mount_params_proc_write, */
1068 };
1069
1070 #else
cifs_proc_init(void)1071 inline void cifs_proc_init(void)
1072 {
1073 }
1074
cifs_proc_clean(void)1075 inline void cifs_proc_clean(void)
1076 {
1077 }
1078 #endif /* PROC_FS */
1079