xref: /linux/fs/smb/client/cifs_debug.c (revision e5ac3ff6c6e7ea3bd0859edb80b9241135fe10c0)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *
4  *   Copyright (C) International Business Machines  Corp., 2000,2005
5  *
6  *   Modified by Steve French (sfrench@us.ibm.com)
7  */
8 #include <linux/fs.h>
9 #include <linux/string.h>
10 #include <linux/ctype.h>
11 #include <linux/kstrtox.h>
12 #include <linux/module.h>
13 #include <linux/proc_fs.h>
14 #include <linux/uaccess.h>
15 #include <uapi/linux/ethtool.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 #include "../common/smbdirect/smbdirect_pdu.h"
27 #endif
28 #include "cifs_swn.h"
29 #include "cached_dir.h"
30 
31 void
32 cifs_dump_mem(char *label, void *data, int length)
33 {
34 	pr_debug("%s: dump of %d bytes of data at 0x%p\n", label, length, data);
35 	print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 16, 4,
36 		       data, length, true);
37 }
38 
39 void cifs_dump_mids(struct TCP_Server_Info *server)
40 {
41 #ifdef CONFIG_CIFS_DEBUG2
42 	struct mid_q_entry *mid_entry;
43 
44 	if (server == NULL)
45 		return;
46 
47 	cifs_dbg(VFS, "Dump pending requests:\n");
48 	spin_lock(&server->mid_queue_lock);
49 	list_for_each_entry(mid_entry, &server->pending_mid_q, qhead) {
50 		cifs_dbg(VFS, "State: %d Cmd: %d Pid: %d Cbdata: %p Mid %llu\n",
51 			 mid_entry->mid_state,
52 			 le16_to_cpu(mid_entry->command),
53 			 mid_entry->pid,
54 			 mid_entry->callback_data,
55 			 mid_entry->mid);
56 #ifdef CONFIG_CIFS_STATS2
57 		cifs_dbg(VFS, "IsLarge: %d buf: %p time rcv: %ld now: %ld\n",
58 			 mid_entry->large_buf,
59 			 mid_entry->resp_buf,
60 			 mid_entry->when_received,
61 			 jiffies);
62 #endif /* STATS2 */
63 		cifs_dbg(VFS, "IsMult: %d IsEnd: %d\n",
64 			 mid_entry->multiRsp, mid_entry->multiEnd);
65 		if (mid_entry->resp_buf) {
66 			server->ops->dump_detail(mid_entry->resp_buf,
67 					 mid_entry->response_pdu_len, server);
68 			cifs_dump_mem("existing buf: ", mid_entry->resp_buf, 62);
69 		}
70 	}
71 	spin_unlock(&server->mid_queue_lock);
72 #endif /* CONFIG_CIFS_DEBUG2 */
73 }
74 
75 #ifdef CONFIG_PROC_FS
76 static void cifs_debug_tcon(struct seq_file *m, struct cifs_tcon *tcon)
77 {
78 	__u32 dev_type = le32_to_cpu(tcon->fsDevInfo.DeviceType);
79 
80 	seq_printf(m, "%s Mounts: %d ", tcon->tree_name, tcon->tc_count);
81 	if (tcon->nativeFileSystem)
82 		seq_printf(m, "Type: %s ", tcon->nativeFileSystem);
83 	seq_printf(m, "DevInfo: 0x%x Attributes: 0x%x\n\tPathComponentMax: %d Status: %d",
84 		   le32_to_cpu(tcon->fsDevInfo.DeviceCharacteristics),
85 		   le32_to_cpu(tcon->fsAttrInfo.Attributes),
86 		   le32_to_cpu(tcon->fsAttrInfo.MaxPathNameComponentLength),
87 		   tcon->status);
88 	if (dev_type == FILE_DEVICE_DISK)
89 		seq_puts(m, " type: DISK ");
90 	else if (dev_type == FILE_DEVICE_CD_ROM)
91 		seq_puts(m, " type: CDROM ");
92 	else
93 		seq_printf(m, " type: %d ", dev_type);
94 
95 	seq_printf(m, "Serial Number: 0x%x", tcon->vol_serial_number);
96 
97 	if ((tcon->seal) ||
98 	    (tcon->ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA) ||
99 	    (tcon->share_flags & SHI1005_FLAGS_ENCRYPT_DATA))
100 		seq_puts(m, " encrypted");
101 	if (tcon->nocase)
102 		seq_printf(m, " nocase");
103 	if (tcon->unix_ext)
104 		seq_printf(m, " POSIX Extensions");
105 	if (tcon->ses->server->ops->dump_share_caps)
106 		tcon->ses->server->ops->dump_share_caps(m, tcon);
107 	if (tcon->use_witness)
108 		seq_puts(m, " Witness");
109 	if (tcon->broken_sparse_sup)
110 		seq_puts(m, " nosparse");
111 	if (tcon->need_reconnect)
112 		seq_puts(m, "\tDISCONNECTED ");
113 	spin_lock(&tcon->tc_lock);
114 	if (tcon->origin_fullpath) {
115 		seq_printf(m, "\n\tDFS origin fullpath: %s",
116 			   tcon->origin_fullpath);
117 	}
118 	spin_unlock(&tcon->tc_lock);
119 	seq_putc(m, '\n');
120 }
121 
122 static void
123 cifs_dump_channel(struct seq_file *m, int i, struct cifs_chan *chan)
124 {
125 	struct TCP_Server_Info *server = chan->server;
126 
127 	if (!server) {
128 		seq_printf(m, "\n\n\t\tChannel: %d DISABLED", i+1);
129 		return;
130 	}
131 
132 	seq_printf(m, "\n\n\t\tChannel: %d ConnectionId: 0x%llx"
133 		   "\n\t\tNumber of credits: %d,%d,%d Dialect 0x%x"
134 		   "\n\t\tTCP status: %d Instance: %d"
135 		   "\n\t\tLocal Users To Server: %d SecMode: 0x%x Req On Wire: %d"
136 		   "\n\t\tIn Send: %d In MaxReq Wait: %d",
137 		   i+1, server->conn_id,
138 		   server->credits,
139 		   server->echo_credits,
140 		   server->oplock_credits,
141 		   server->dialect,
142 		   server->tcpStatus,
143 		   server->reconnect_instance,
144 		   server->srv_count,
145 		   server->sec_mode,
146 		   in_flight(server),
147 		   atomic_read(&server->in_send),
148 		   atomic_read(&server->num_waiters));
149 #ifdef CONFIG_NET_NS
150 	if (server->net)
151 		seq_printf(m, " Net namespace: %u ", server->net->ns.inum);
152 #endif /* NET_NS */
153 
154 }
155 
156 static inline const char *smb_speed_to_str(size_t bps)
157 {
158 	size_t mbps = bps / 1000 / 1000;
159 
160 	switch (mbps) {
161 	case SPEED_10:
162 		return "10Mbps";
163 	case SPEED_100:
164 		return "100Mbps";
165 	case SPEED_1000:
166 		return "1Gbps";
167 	case SPEED_2500:
168 		return "2.5Gbps";
169 	case SPEED_5000:
170 		return "5Gbps";
171 	case SPEED_10000:
172 		return "10Gbps";
173 	case SPEED_14000:
174 		return "14Gbps";
175 	case SPEED_20000:
176 		return "20Gbps";
177 	case SPEED_25000:
178 		return "25Gbps";
179 	case SPEED_40000:
180 		return "40Gbps";
181 	case SPEED_50000:
182 		return "50Gbps";
183 	case SPEED_56000:
184 		return "56Gbps";
185 	case SPEED_100000:
186 		return "100Gbps";
187 	case SPEED_200000:
188 		return "200Gbps";
189 	case SPEED_400000:
190 		return "400Gbps";
191 	case SPEED_800000:
192 		return "800Gbps";
193 	default:
194 		return "Unknown";
195 	}
196 }
197 
198 static void
199 cifs_dump_iface(struct seq_file *m, struct cifs_server_iface *iface)
200 {
201 	struct sockaddr_in *ipv4 = (struct sockaddr_in *)&iface->sockaddr;
202 	struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)&iface->sockaddr;
203 
204 	seq_printf(m, "\tSpeed: %s\n", smb_speed_to_str(iface->speed));
205 	seq_puts(m, "\t\tCapabilities: ");
206 	if (iface->rdma_capable)
207 		seq_puts(m, "rdma ");
208 	if (iface->rss_capable)
209 		seq_puts(m, "rss ");
210 	if (!iface->rdma_capable && !iface->rss_capable)
211 		seq_puts(m, "None");
212 	seq_putc(m, '\n');
213 	if (iface->sockaddr.ss_family == AF_INET)
214 		seq_printf(m, "\t\tIPv4: %pI4\n", &ipv4->sin_addr);
215 	else if (iface->sockaddr.ss_family == AF_INET6)
216 		seq_printf(m, "\t\tIPv6: %pI6\n", &ipv6->sin6_addr);
217 	if (!iface->is_active)
218 		seq_puts(m, "\t\t[for-cleanup]\n");
219 }
220 
221 static int cifs_debug_files_proc_show(struct seq_file *m, void *v)
222 {
223 	struct TCP_Server_Info *server;
224 	struct cifs_ses *ses;
225 	struct cifs_tcon *tcon;
226 	struct cifsFileInfo *cfile;
227 	struct inode *inode;
228 	struct cifsInodeInfo *cinode;
229 	char lease[4];
230 	int n;
231 
232 	seq_puts(m, "# Version:1\n");
233 	seq_puts(m, "# Format:\n");
234 	seq_puts(m, "# <tree id> <ses id> <persistent fid> <flags> <count> <pid> <uid>");
235 #ifdef CONFIG_CIFS_DEBUG2
236 	seq_puts(m, " <filename> <lease> <lease-key> <mid>\n");
237 #else
238 	seq_puts(m, " <filename> <lease> <lease-key>\n");
239 #endif /* CIFS_DEBUG2 */
240 	spin_lock(&cifs_tcp_ses_lock);
241 	list_for_each_entry(server, &cifs_tcp_ses_list, tcp_ses_list) {
242 		list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
243 			if (cifs_ses_exiting(ses))
244 				continue;
245 			list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
246 				spin_lock(&tcon->open_file_lock);
247 				list_for_each_entry(cfile, &tcon->openFileList, tlist) {
248 					seq_printf(m,
249 						"0x%x 0x%llx 0x%llx 0x%x %d %d %d %pd",
250 						tcon->tid,
251 						ses->Suid,
252 						cfile->fid.persistent_fid,
253 						cfile->f_flags,
254 						cfile->count,
255 						cfile->pid,
256 						from_kuid(&init_user_ns, cfile->uid),
257 						cfile->dentry);
258 
259 					/* Append lease/oplock caching state as RHW letters */
260 					inode = d_inode(cfile->dentry);
261 					cinode = NULL;
262 					n = 0;
263 					if (inode) {
264 						cinode = CIFS_I(inode);
265 						if (CIFS_CACHE_READ(cinode))
266 							lease[n++] = 'R';
267 						if (CIFS_CACHE_HANDLE(cinode))
268 							lease[n++] = 'H';
269 						if (CIFS_CACHE_WRITE(cinode))
270 							lease[n++] = 'W';
271 					}
272 					lease[n] = '\0';
273 					seq_puts(m, " ");
274 					if (n)
275 						seq_printf(m, "%s", lease);
276 					else
277 						seq_puts(m, "NONE");
278 
279 					seq_puts(m, " ");
280 					if (cinode && cinode->lease_granted)
281 						seq_printf(m, "%pUl", cinode->lease_key);
282 					else
283 						seq_puts(m, "-");
284 
285 #ifdef CONFIG_CIFS_DEBUG2
286 					seq_printf(m, " %llu", cfile->fid.mid);
287 #endif /* CONFIG_CIFS_DEBUG2 */
288 					seq_printf(m, "\n");
289 				}
290 				spin_unlock(&tcon->open_file_lock);
291 			}
292 		}
293 	}
294 	spin_unlock(&cifs_tcp_ses_lock);
295 	seq_putc(m, '\n');
296 	return 0;
297 }
298 
299 static int cifs_debug_dirs_proc_show(struct seq_file *m, void *v)
300 {
301 	struct list_head *stmp, *tmp, *tmp1;
302 	struct TCP_Server_Info *server;
303 	struct cifs_ses *ses;
304 	struct cifs_tcon *tcon;
305 	struct cached_fids *cfids;
306 	struct cached_fid *cfid;
307 	LIST_HEAD(entry);
308 
309 	seq_puts(m, "# Version:1\n");
310 	seq_puts(m, "# Format:\n");
311 	seq_puts(m, "# <tree id> <sess id> <persistent fid> <lease-key> <path>\n");
312 
313 	spin_lock(&cifs_tcp_ses_lock);
314 	list_for_each(stmp, &cifs_tcp_ses_list) {
315 		server = list_entry(stmp, struct TCP_Server_Info,
316 				    tcp_ses_list);
317 		list_for_each(tmp, &server->smb_ses_list) {
318 			ses = list_entry(tmp, struct cifs_ses, smb_ses_list);
319 			list_for_each(tmp1, &ses->tcon_list) {
320 				tcon = list_entry(tmp1, struct cifs_tcon, tcon_list);
321 				cfids = tcon->cfids;
322 				if (!cfids)
323 					continue;
324 				spin_lock(&cfids->cfid_list_lock); /* check lock ordering */
325 				seq_printf(m, "Num entries: %d, cached_dirents: %lu entries, %llu bytes\n",
326 						cfids->num_entries,
327 						(unsigned long)atomic_long_read(&cfids->total_dirents_entries),
328 						(unsigned long long)atomic64_read(&cfids->total_dirents_bytes));
329 				list_for_each_entry(cfid, &cfids->entries, entry) {
330 					seq_printf(m, "0x%x 0x%llx 0x%llx ",
331 						tcon->tid,
332 						ses->Suid,
333 						cfid->fid.persistent_fid);
334 					if (cfid->has_lease)
335 						seq_printf(m, "%pUl ", cfid->fid.lease_key);
336 					else
337 						seq_puts(m, "- ");
338 					seq_printf(m, "%s", cfid->path);
339 					if (cfid->file_all_info_is_valid)
340 						seq_printf(m, "\tvalid file info");
341 					if (cfid->dirents.is_valid)
342 						seq_printf(m, ", valid dirents");
343 					if (!list_empty(&cfid->dirents.entries))
344 						seq_printf(m, ", dirents: %lu entries, %lu bytes",
345 						cfid->dirents.entries_count, cfid->dirents.bytes_used);
346 					seq_printf(m, "\n");
347 				}
348 				spin_unlock(&cfids->cfid_list_lock);
349 			}
350 		}
351 	}
352 	spin_unlock(&cifs_tcp_ses_lock);
353 	seq_putc(m, '\n');
354 	return 0;
355 }
356 
357 static __always_inline const char *compression_alg_str(__le16 alg)
358 {
359 	switch (alg) {
360 	case SMB3_COMPRESS_NONE:
361 		return "NONE";
362 	case SMB3_COMPRESS_LZNT1:
363 		return "LZNT1";
364 	case SMB3_COMPRESS_LZ77:
365 		return "LZ77";
366 	case SMB3_COMPRESS_LZ77_HUFF:
367 		return "LZ77-Huffman";
368 	case SMB3_COMPRESS_PATTERN:
369 		return "Pattern_V1";
370 	default:
371 		return "invalid";
372 	}
373 }
374 
375 static __always_inline const char *cipher_alg_str(__le16 cipher)
376 {
377 	switch (cipher) {
378 	case SMB2_ENCRYPTION_AES128_CCM:
379 		return "AES128-CCM";
380 	case SMB2_ENCRYPTION_AES128_GCM:
381 		return "AES128-GCM";
382 	case SMB2_ENCRYPTION_AES256_CCM:
383 		return "AES256-CCM";
384 	case SMB2_ENCRYPTION_AES256_GCM:
385 		return "AES256-GCM";
386 	default:
387 		return "UNKNOWN";
388 	}
389 }
390 
391 static int cifs_debug_data_proc_show(struct seq_file *m, void *v)
392 {
393 	struct mid_q_entry *mid_entry;
394 	struct TCP_Server_Info *server;
395 	struct TCP_Server_Info *chan_server;
396 	struct cifs_ses *ses;
397 	struct cifs_tcon *tcon;
398 	struct cifs_server_iface *iface;
399 	size_t iface_weight = 0, iface_min_speed = 0;
400 	struct cifs_server_iface *last_iface = NULL;
401 	int c, i, j;
402 
403 	seq_puts(m,
404 		    "Display Internal CIFS Data Structures for Debugging\n"
405 		    "---------------------------------------------------\n");
406 	seq_printf(m, "CIFS Version %s\n", CIFS_VERSION);
407 	seq_printf(m, "Features:");
408 #ifdef CONFIG_CIFS_DFS_UPCALL
409 	seq_printf(m, " DFS");
410 #endif
411 #ifdef CONFIG_CIFS_FSCACHE
412 	seq_printf(m, ",FSCACHE");
413 #endif
414 #ifdef CONFIG_CIFS_SMB_DIRECT
415 	seq_printf(m, ",SMB_DIRECT");
416 #endif
417 #ifdef CONFIG_CIFS_STATS2
418 	seq_printf(m, ",STATS2");
419 #else
420 	seq_printf(m, ",STATS");
421 #endif
422 #ifdef CONFIG_CIFS_DEBUG2
423 	seq_printf(m, ",DEBUG2");
424 #elif defined(CONFIG_CIFS_DEBUG)
425 	seq_printf(m, ",DEBUG");
426 #endif
427 #ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
428 	seq_printf(m, ",ALLOW_INSECURE_LEGACY");
429 #endif
430 #ifdef CONFIG_CIFS_POSIX
431 	seq_printf(m, ",CIFS_POSIX");
432 #endif
433 #ifdef CONFIG_CIFS_UPCALL
434 	seq_printf(m, ",UPCALL(SPNEGO)");
435 #endif
436 #ifdef CONFIG_CIFS_XATTR
437 	seq_printf(m, ",XATTR");
438 #endif
439 	seq_printf(m, ",ACL");
440 #ifdef CONFIG_CIFS_SWN_UPCALL
441 	seq_puts(m, ",WITNESS");
442 #endif
443 #ifdef CONFIG_CIFS_COMPRESSION
444 	seq_puts(m, ",COMPRESSION");
445 #endif
446 	seq_putc(m, '\n');
447 	seq_printf(m, "CIFSMaxBufSize: %d\n", CIFSMaxBufSize);
448 	seq_printf(m, "Active VFS Requests: %d\n", GlobalTotalActiveXid);
449 
450 	seq_printf(m, "\nServers: ");
451 
452 	c = 0;
453 	spin_lock(&cifs_tcp_ses_lock);
454 	list_for_each_entry(server, &cifs_tcp_ses_list, tcp_ses_list) {
455 #ifdef CONFIG_CIFS_SMB_DIRECT
456 		struct smbdirect_socket *sc;
457 		struct smbdirect_socket_parameters *sp;
458 #endif
459 
460 		/* channel info will be printed as a part of sessions below */
461 		if (SERVER_IS_CHAN(server))
462 			continue;
463 
464 		c++;
465 		seq_printf(m, "\n%d) ConnectionId: 0x%llx ",
466 			c, server->conn_id);
467 
468 		spin_lock(&server->srv_lock);
469 		if (server->hostname)
470 			seq_printf(m, "Hostname: %s ", server->hostname);
471 		seq_printf(m, "\nClientGUID: %pUL", server->client_guid);
472 		spin_unlock(&server->srv_lock);
473 #ifdef CONFIG_CIFS_SMB_DIRECT
474 		if (!server->rdma)
475 			goto skip_rdma;
476 
477 		if (!server->smbd_conn) {
478 			seq_printf(m, "\nSMBDirect transport not available");
479 			goto skip_rdma;
480 		}
481 		sc = &server->smbd_conn->socket;
482 		sp = &sc->parameters;
483 
484 		seq_printf(m, "\nSMBDirect protocol version: 0x%x "
485 			"transport status: %s (%u)",
486 			SMBDIRECT_V1,
487 			smbdirect_socket_status_string(sc->status),
488 			sc->status);
489 		seq_printf(m, "\nConn receive_credit_max: %u "
490 			"send_credit_target: %u max_send_size: %u",
491 			sp->recv_credit_max,
492 			sp->send_credit_target,
493 			sp->max_send_size);
494 		seq_printf(m, "\nConn max_fragmented_recv_size: %u "
495 			"max_fragmented_send_size: %u max_receive_size:%u",
496 			sp->max_fragmented_recv_size,
497 			sp->max_fragmented_send_size,
498 			sp->max_recv_size);
499 		seq_printf(m, "\nConn keep_alive_interval: %u "
500 			"max_readwrite_size: %u rdma_readwrite_threshold: %u",
501 			sp->keepalive_interval_msec * 1000,
502 			sp->max_read_write_size,
503 			server->rdma_readwrite_threshold);
504 		seq_printf(m, "\nDebug count_get_receive_buffer: %llu "
505 			"count_put_receive_buffer: %llu count_send_empty: %llu",
506 			sc->statistics.get_receive_buffer,
507 			sc->statistics.put_receive_buffer,
508 			sc->statistics.send_empty);
509 		seq_printf(m, "\nRead Queue "
510 			"count_enqueue_reassembly_queue: %llu "
511 			"count_dequeue_reassembly_queue: %llu "
512 			"reassembly_data_length: %u "
513 			"reassembly_queue_length: %u",
514 			sc->statistics.enqueue_reassembly_queue,
515 			sc->statistics.dequeue_reassembly_queue,
516 			sc->recv_io.reassembly.data_length,
517 			sc->recv_io.reassembly.queue_length);
518 		seq_printf(m, "\nCurrent Credits send_credits: %u "
519 			"receive_credits: %u receive_credit_target: %u",
520 			atomic_read(&sc->send_io.credits.count),
521 			atomic_read(&sc->recv_io.credits.count),
522 			sc->recv_io.credits.target);
523 		seq_printf(m, "\nPending send_pending: %u ",
524 			atomic_read(&sc->send_io.pending.count));
525 		seq_printf(m, "\nMR responder_resources: %u "
526 			"max_frmr_depth: %u mr_type: 0x%x",
527 			sp->responder_resources,
528 			sp->max_frmr_depth,
529 			sc->mr_io.type);
530 		seq_printf(m, "\nMR mr_ready_count: %u mr_used_count: %u",
531 			atomic_read(&sc->mr_io.ready.count),
532 			atomic_read(&sc->mr_io.used.count));
533 skip_rdma:
534 #endif
535 		seq_printf(m, "\nNumber of credits: %d,%d,%d Dialect 0x%x",
536 			server->credits,
537 			server->echo_credits,
538 			server->oplock_credits,
539 			server->dialect);
540 		if (server->sign)
541 			seq_printf(m, " signed");
542 		if (server->posix_ext_supported)
543 			seq_printf(m, " posix");
544 		if (server->nosharesock)
545 			seq_printf(m, " nosharesock");
546 
547 		seq_printf(m, "\nServer capabilities: 0x%x", server->capabilities);
548 
549 		if (server->rdma)
550 			seq_printf(m, "\nRDMA ");
551 		seq_printf(m, "\nTCP status: %d Instance: %d"
552 				"\nLocal Users To Server: %d SecMode: 0x%x Req On Wire: %d",
553 				server->tcpStatus,
554 				server->reconnect_instance,
555 				server->srv_count,
556 				server->sec_mode, in_flight(server));
557 #ifdef CONFIG_NET_NS
558 		if (server->net)
559 			seq_printf(m, " Net namespace: %u ", server->net->ns.inum);
560 #endif /* NET_NS */
561 
562 		seq_printf(m, "\nIn Send: %d In MaxReq Wait: %d",
563 				atomic_read(&server->in_send),
564 				atomic_read(&server->num_waiters));
565 
566 		if (server->leaf_fullpath) {
567 			seq_printf(m, "\nDFS leaf full path: %s",
568 				   server->leaf_fullpath);
569 		}
570 
571 		seq_puts(m, "\nCompression: ");
572 		if (!IS_ENABLED(CONFIG_CIFS_COMPRESSION))
573 			seq_puts(m, "no built-in support");
574 		else if (!server->compression.requested)
575 			seq_puts(m, "disabled on mount");
576 		else if (server->compression.enabled)
577 			seq_printf(m, "enabled (%s)", compression_alg_str(server->compression.alg));
578 		else
579 			seq_puts(m, "disabled (not supported by this server)");
580 
581 		/* Show negotiated encryption cipher, even if not required */
582 		seq_puts(m, "\nEncryption: ");
583 		if (server->cipher_type)
584 			seq_printf(m, "Negotiated cipher (%s)", cipher_alg_str(server->cipher_type));
585 
586 		seq_printf(m, "\n\n\tSessions: ");
587 		i = 0;
588 		list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
589 			spin_lock(&ses->ses_lock);
590 			if (ses->ses_status == SES_EXITING) {
591 				spin_unlock(&ses->ses_lock);
592 				continue;
593 			}
594 			i++;
595 			if ((ses->serverDomain == NULL) ||
596 				(ses->serverOS == NULL) ||
597 				(ses->serverNOS == NULL)) {
598 				seq_printf(m, "\n\t%d) Address: %s Uses: %d Capability: 0x%x\tSession Status: %d ",
599 					i, ses->ip_addr, ses->ses_count,
600 					ses->capabilities, ses->ses_status);
601 				if (ses->session_flags & SMB2_SESSION_FLAG_IS_GUEST)
602 					seq_printf(m, "Guest ");
603 				else if (ses->session_flags & SMB2_SESSION_FLAG_IS_NULL)
604 					seq_printf(m, "Anonymous ");
605 			} else {
606 				seq_printf(m,
607 				    "\n\t%d) Name: %s  Domain: %s Uses: %d OS: %s "
608 				    "\n\tNOS: %s\tCapability: 0x%x"
609 					"\n\tSMB session status: %d ",
610 				i, ses->ip_addr, ses->serverDomain,
611 				ses->ses_count, ses->serverOS, ses->serverNOS,
612 				ses->capabilities, ses->ses_status);
613 			}
614 			if (ses->expired_pwd)
615 				seq_puts(m, "password no longer valid ");
616 			spin_unlock(&ses->ses_lock);
617 
618 			seq_printf(m, "\n\tSecurity type: %s ",
619 				get_security_type_str(server->ops->select_sectype(server, ses->sectype)));
620 
621 			/* dump session id helpful for use with network trace */
622 			seq_printf(m, " SessionId: 0x%llx", ses->Suid);
623 			if (ses->session_flags & SMB2_SESSION_FLAG_ENCRYPT_DATA)
624 				seq_puts(m, " encrypted");
625 			if (ses->sign)
626 				seq_puts(m, " signed");
627 
628 			seq_printf(m, "\n\tUser: %d Cred User: %d",
629 				   from_kuid(&init_user_ns, ses->linux_uid),
630 				   from_kuid(&init_user_ns, ses->cred_uid));
631 
632 			if (ses->dfs_root_ses) {
633 				seq_printf(m, "\n\tDFS root session id: 0x%llx",
634 					   ses->dfs_root_ses->Suid);
635 			}
636 
637 			spin_lock(&ses->chan_lock);
638 			if (CIFS_CHAN_NEEDS_RECONNECT(ses, 0))
639 				seq_puts(m, "\tPrimary channel: DISCONNECTED ");
640 			if (CIFS_CHAN_IN_RECONNECT(ses, 0))
641 				seq_puts(m, "\t[RECONNECTING] ");
642 
643 			if (ses->chan_count > 1) {
644 				seq_printf(m, "\n\n\tExtra Channels: %zu ",
645 					   ses->chan_count-1);
646 				for (j = 1; j < ses->chan_count; j++) {
647 					cifs_dump_channel(m, j, &ses->chans[j]);
648 					if (CIFS_CHAN_NEEDS_RECONNECT(ses, j))
649 						seq_puts(m, "\tDISCONNECTED ");
650 					if (CIFS_CHAN_IN_RECONNECT(ses, j))
651 						seq_puts(m, "\t[RECONNECTING] ");
652 				}
653 			}
654 			spin_unlock(&ses->chan_lock);
655 
656 			seq_puts(m, "\n\n\tShares: ");
657 			j = 0;
658 
659 			seq_printf(m, "\n\t%d) IPC: ", j);
660 			if (ses->tcon_ipc)
661 				cifs_debug_tcon(m, ses->tcon_ipc);
662 			else
663 				seq_puts(m, "none\n");
664 
665 			list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
666 				++j;
667 				seq_printf(m, "\n\t%d) ", j);
668 				cifs_debug_tcon(m, tcon);
669 			}
670 
671 			spin_lock(&ses->iface_lock);
672 			if (ses->iface_count)
673 				seq_printf(m, "\n\n\tServer interfaces: %zu"
674 					   "\tLast updated: %lu seconds ago",
675 					   ses->iface_count,
676 					   (jiffies - ses->iface_last_update) / HZ);
677 
678 			last_iface = list_last_entry(&ses->iface_list,
679 						     struct cifs_server_iface,
680 						     iface_head);
681 			iface_min_speed = last_iface->speed;
682 
683 			j = 0;
684 			list_for_each_entry(iface, &ses->iface_list,
685 						 iface_head) {
686 				seq_printf(m, "\n\t%d)", ++j);
687 				cifs_dump_iface(m, iface);
688 
689 				iface_weight = iface->speed / iface_min_speed;
690 				seq_printf(m, "\t\tWeight (cur,total): (%zu,%zu)"
691 					   "\n\t\tAllocated channels: %u\n",
692 					   iface->weight_fulfilled,
693 					   iface_weight,
694 					   iface->num_channels);
695 
696 				if (is_ses_using_iface(ses, iface))
697 					seq_puts(m, "\t\t[CONNECTED]\n");
698 			}
699 			spin_unlock(&ses->iface_lock);
700 
701 			seq_puts(m, "\n\n\tMIDs: ");
702 			spin_lock(&ses->chan_lock);
703 			for (j = 0; j < ses->chan_count; j++) {
704 				chan_server = ses->chans[j].server;
705 				if (!chan_server)
706 					continue;
707 
708 				if (list_empty(&chan_server->pending_mid_q))
709 					continue;
710 
711 				seq_printf(m, "\n\tServer ConnectionId: 0x%llx",
712 					   chan_server->conn_id);
713 				spin_lock(&chan_server->mid_queue_lock);
714 				list_for_each_entry(mid_entry, &chan_server->pending_mid_q, qhead) {
715 					seq_printf(m, "\n\t\tState: %d com: %d pid: %d cbdata: %p mid %llu",
716 						   mid_entry->mid_state,
717 						   le16_to_cpu(mid_entry->command),
718 						   mid_entry->pid,
719 						   mid_entry->callback_data,
720 						   mid_entry->mid);
721 				}
722 				spin_unlock(&chan_server->mid_queue_lock);
723 			}
724 			spin_unlock(&ses->chan_lock);
725 			seq_puts(m, "\n--\n");
726 		}
727 		if (i == 0)
728 			seq_printf(m, "\n\t\t[NONE]");
729 	}
730 	if (c == 0)
731 		seq_printf(m, "\n\t[NONE]");
732 
733 	spin_unlock(&cifs_tcp_ses_lock);
734 	seq_putc(m, '\n');
735 	cifs_swn_dump(m);
736 
737 	/* BB add code to dump additional info such as TCP session info now */
738 	return 0;
739 }
740 
741 static ssize_t cifs_stats_proc_write(struct file *file,
742 		const char __user *buffer, size_t count, loff_t *ppos)
743 {
744 	bool bv;
745 	int rc;
746 	struct TCP_Server_Info *server;
747 	struct cifs_ses *ses;
748 	struct cifs_tcon *tcon;
749 
750 	rc = kstrtobool_from_user(buffer, count, &bv);
751 	if (rc == 0) {
752 #ifdef CONFIG_CIFS_STATS2
753 		int i;
754 
755 		atomic_set(&total_buf_alloc_count, 0);
756 		atomic_set(&total_small_buf_alloc_count, 0);
757 #endif /* CONFIG_CIFS_STATS2 */
758 		atomic_set(&tcpSesReconnectCount, 0);
759 		atomic_set(&tconInfoReconnectCount, 0);
760 
761 		spin_lock(&GlobalMid_Lock);
762 		GlobalMaxActiveXid = 0;
763 		GlobalCurrentXid = 0;
764 		spin_unlock(&GlobalMid_Lock);
765 		spin_lock(&cifs_tcp_ses_lock);
766 		list_for_each_entry(server, &cifs_tcp_ses_list, tcp_ses_list) {
767 			server->max_in_flight = 0;
768 #ifdef CONFIG_CIFS_STATS2
769 			for (i = 0; i < NUMBER_OF_SMB2_COMMANDS; i++) {
770 				atomic_set(&server->num_cmds[i], 0);
771 				atomic_set(&server->smb2slowcmd[i], 0);
772 				server->time_per_cmd[i] = 0;
773 				server->slowest_cmd[i] = 0;
774 				server->fastest_cmd[0] = 0;
775 			}
776 #endif /* CONFIG_CIFS_STATS2 */
777 			list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
778 				if (cifs_ses_exiting(ses))
779 					continue;
780 				list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
781 					atomic_set(&tcon->num_smbs_sent, 0);
782 					spin_lock(&tcon->stat_lock);
783 					tcon->bytes_read = 0;
784 					tcon->bytes_written = 0;
785 					tcon->stats_from_time = ktime_get_real_seconds();
786 					spin_unlock(&tcon->stat_lock);
787 					if (server->ops->clear_stats)
788 						server->ops->clear_stats(tcon);
789 				}
790 			}
791 		}
792 		spin_unlock(&cifs_tcp_ses_lock);
793 	} else {
794 		return rc;
795 	}
796 
797 	return count;
798 }
799 
800 static int cifs_stats_proc_show(struct seq_file *m, void *v)
801 {
802 	int i;
803 #ifdef CONFIG_CIFS_STATS2
804 	int j;
805 #endif /* STATS2 */
806 	struct TCP_Server_Info *server;
807 	struct cifs_ses *ses;
808 	struct cifs_tcon *tcon;
809 
810 	seq_printf(m, "Resources in use\nCIFS Session: %d\n",
811 			sesInfoAllocCount.counter);
812 	seq_printf(m, "Share (unique mount targets): %d\n",
813 			tconInfoAllocCount.counter);
814 	seq_printf(m, "SMB Request/Response Buffer: %d Pool size: %d\n",
815 			buf_alloc_count.counter,
816 			cifs_min_rcv + tcpSesAllocCount.counter);
817 	seq_printf(m, "SMB Small Req/Resp Buffer: %d Pool size: %d\n",
818 			small_buf_alloc_count.counter, cifs_min_small);
819 #ifdef CONFIG_CIFS_STATS2
820 	seq_printf(m, "Total Large %d Small %d Allocations\n",
821 				atomic_read(&total_buf_alloc_count),
822 				atomic_read(&total_small_buf_alloc_count));
823 #endif /* CONFIG_CIFS_STATS2 */
824 
825 	seq_printf(m, "Operations (MIDs): %d\n", atomic_read(&mid_count));
826 	seq_printf(m,
827 		"\n%d session %d share reconnects\n",
828 		tcpSesReconnectCount.counter, tconInfoReconnectCount.counter);
829 
830 	seq_printf(m,
831 		"Total vfs operations: %d maximum at one time: %d\n",
832 		GlobalCurrentXid, GlobalMaxActiveXid);
833 
834 	i = 0;
835 	spin_lock(&cifs_tcp_ses_lock);
836 	list_for_each_entry(server, &cifs_tcp_ses_list, tcp_ses_list) {
837 		seq_printf(m, "\nMax requests in flight: %d", server->max_in_flight);
838 #ifdef CONFIG_CIFS_STATS2
839 		seq_puts(m, "\nTotal time spent processing by command. Time ");
840 		seq_printf(m, "units are jiffies (%d per second)\n", HZ);
841 		seq_puts(m, "  SMB3 CMD\tNumber\tTotal Time\tFastest\tSlowest\n");
842 		seq_puts(m, "  --------\t------\t----------\t-------\t-------\n");
843 		for (j = 0; j < NUMBER_OF_SMB2_COMMANDS; j++)
844 			seq_printf(m, "  %d\t\t%d\t%llu\t\t%u\t%u\n", j,
845 				atomic_read(&server->num_cmds[j]),
846 				server->time_per_cmd[j],
847 				server->fastest_cmd[j],
848 				server->slowest_cmd[j]);
849 		for (j = 0; j < NUMBER_OF_SMB2_COMMANDS; j++)
850 			if (atomic_read(&server->smb2slowcmd[j])) {
851 				spin_lock(&server->srv_lock);
852 				seq_printf(m, "  %d slow responses from %s for command %d\n",
853 					atomic_read(&server->smb2slowcmd[j]),
854 					server->hostname, j);
855 				spin_unlock(&server->srv_lock);
856 			}
857 #endif /* STATS2 */
858 		list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
859 			if (cifs_ses_exiting(ses))
860 				continue;
861 			list_for_each_entry(tcon, &ses->tcon_list, tcon_list) {
862 				i++;
863 				seq_printf(m, "\n%d) %s", i, tcon->tree_name);
864 				if (tcon->need_reconnect)
865 					seq_puts(m, "\tDISCONNECTED ");
866 				seq_printf(m, "\nSMBs: %d since %ptTs UTC",
867 					   atomic_read(&tcon->num_smbs_sent),
868 					   &tcon->stats_from_time);
869 				if (server->ops->print_stats)
870 					server->ops->print_stats(m, tcon);
871 			}
872 		}
873 	}
874 	spin_unlock(&cifs_tcp_ses_lock);
875 
876 	seq_putc(m, '\n');
877 	return 0;
878 }
879 
880 static int cifs_stats_proc_open(struct inode *inode, struct file *file)
881 {
882 	return single_open(file, cifs_stats_proc_show, NULL);
883 }
884 
885 static const struct proc_ops cifs_stats_proc_ops = {
886 	.proc_open	= cifs_stats_proc_open,
887 	.proc_read	= seq_read,
888 	.proc_lseek	= seq_lseek,
889 	.proc_release	= single_release,
890 	.proc_write	= cifs_stats_proc_write,
891 };
892 
893 #ifdef CONFIG_CIFS_SMB_DIRECT
894 #define PROC_FILE_DEFINE(name) \
895 static ssize_t name##_write(struct file *file, const char __user *buffer, \
896 	size_t count, loff_t *ppos) \
897 { \
898 	int rc; \
899 	rc = kstrtoint_from_user(buffer, count, 10, &name); \
900 	if (rc) \
901 		return rc; \
902 	return count; \
903 } \
904 static int name##_proc_show(struct seq_file *m, void *v) \
905 { \
906 	seq_printf(m, "%d\n", name); \
907 	return 0; \
908 } \
909 static int name##_open(struct inode *inode, struct file *file) \
910 { \
911 	return single_open(file, name##_proc_show, NULL); \
912 } \
913 \
914 static const struct proc_ops cifs_##name##_proc_fops = { \
915 	.proc_open	= name##_open, \
916 	.proc_read	= seq_read, \
917 	.proc_lseek	= seq_lseek, \
918 	.proc_release	= single_release, \
919 	.proc_write	= name##_write, \
920 }
921 
922 PROC_FILE_DEFINE(rdma_readwrite_threshold);
923 PROC_FILE_DEFINE(smbd_max_frmr_depth);
924 PROC_FILE_DEFINE(smbd_keep_alive_interval);
925 PROC_FILE_DEFINE(smbd_max_receive_size);
926 PROC_FILE_DEFINE(smbd_max_fragmented_recv_size);
927 PROC_FILE_DEFINE(smbd_max_send_size);
928 PROC_FILE_DEFINE(smbd_send_credit_target);
929 PROC_FILE_DEFINE(smbd_receive_credit_max);
930 #endif
931 
932 static struct proc_dir_entry *proc_fs_cifs;
933 static const struct proc_ops cifsFYI_proc_ops;
934 static const struct proc_ops cifs_lookup_cache_proc_ops;
935 static const struct proc_ops traceSMB_proc_ops;
936 static const struct proc_ops cifs_security_flags_proc_ops;
937 static const struct proc_ops cifs_linux_ext_proc_ops;
938 static const struct proc_ops cifs_mount_params_proc_ops;
939 
940 void
941 cifs_proc_init(void)
942 {
943 	proc_fs_cifs = proc_mkdir("fs/cifs", NULL);
944 	if (proc_fs_cifs == NULL)
945 		return;
946 
947 	proc_create_single("DebugData", 0, proc_fs_cifs,
948 			cifs_debug_data_proc_show);
949 
950 	proc_create_single("open_files", 0400, proc_fs_cifs,
951 			cifs_debug_files_proc_show);
952 
953 	proc_create_single("open_dirs", 0400, proc_fs_cifs,
954 			cifs_debug_dirs_proc_show);
955 
956 	proc_create("Stats", 0644, proc_fs_cifs, &cifs_stats_proc_ops);
957 	proc_create("cifsFYI", 0644, proc_fs_cifs, &cifsFYI_proc_ops);
958 	proc_create("traceSMB", 0644, proc_fs_cifs, &traceSMB_proc_ops);
959 	proc_create("LinuxExtensionsEnabled", 0644, proc_fs_cifs,
960 		    &cifs_linux_ext_proc_ops);
961 	proc_create("SecurityFlags", 0644, proc_fs_cifs,
962 		    &cifs_security_flags_proc_ops);
963 	proc_create("LookupCacheEnabled", 0644, proc_fs_cifs,
964 		    &cifs_lookup_cache_proc_ops);
965 
966 	proc_create("mount_params", 0444, proc_fs_cifs, &cifs_mount_params_proc_ops);
967 
968 #ifdef CONFIG_CIFS_DFS_UPCALL
969 	proc_create("dfscache", 0644, proc_fs_cifs, &dfscache_proc_ops);
970 #endif
971 
972 #ifdef CONFIG_CIFS_SMB_DIRECT
973 	proc_create("rdma_readwrite_threshold", 0644, proc_fs_cifs,
974 		&cifs_rdma_readwrite_threshold_proc_fops);
975 	proc_create("smbd_max_frmr_depth", 0644, proc_fs_cifs,
976 		&cifs_smbd_max_frmr_depth_proc_fops);
977 	proc_create("smbd_keep_alive_interval", 0644, proc_fs_cifs,
978 		&cifs_smbd_keep_alive_interval_proc_fops);
979 	proc_create("smbd_max_receive_size", 0644, proc_fs_cifs,
980 		&cifs_smbd_max_receive_size_proc_fops);
981 	proc_create("smbd_max_fragmented_recv_size", 0644, proc_fs_cifs,
982 		&cifs_smbd_max_fragmented_recv_size_proc_fops);
983 	proc_create("smbd_max_send_size", 0644, proc_fs_cifs,
984 		&cifs_smbd_max_send_size_proc_fops);
985 	proc_create("smbd_send_credit_target", 0644, proc_fs_cifs,
986 		&cifs_smbd_send_credit_target_proc_fops);
987 	proc_create("smbd_receive_credit_max", 0644, proc_fs_cifs,
988 		&cifs_smbd_receive_credit_max_proc_fops);
989 #endif
990 }
991 
992 void
993 cifs_proc_clean(void)
994 {
995 	if (proc_fs_cifs == NULL)
996 		return;
997 
998 	remove_proc_entry("DebugData", proc_fs_cifs);
999 	remove_proc_entry("open_files", proc_fs_cifs);
1000 	remove_proc_entry("open_dirs", proc_fs_cifs);
1001 	remove_proc_entry("cifsFYI", proc_fs_cifs);
1002 	remove_proc_entry("traceSMB", proc_fs_cifs);
1003 	remove_proc_entry("Stats", proc_fs_cifs);
1004 	remove_proc_entry("SecurityFlags", proc_fs_cifs);
1005 	remove_proc_entry("LinuxExtensionsEnabled", proc_fs_cifs);
1006 	remove_proc_entry("LookupCacheEnabled", proc_fs_cifs);
1007 	remove_proc_entry("mount_params", proc_fs_cifs);
1008 
1009 #ifdef CONFIG_CIFS_DFS_UPCALL
1010 	remove_proc_entry("dfscache", proc_fs_cifs);
1011 #endif
1012 #ifdef CONFIG_CIFS_SMB_DIRECT
1013 	remove_proc_entry("rdma_readwrite_threshold", proc_fs_cifs);
1014 	remove_proc_entry("smbd_max_frmr_depth", proc_fs_cifs);
1015 	remove_proc_entry("smbd_keep_alive_interval", proc_fs_cifs);
1016 	remove_proc_entry("smbd_max_receive_size", proc_fs_cifs);
1017 	remove_proc_entry("smbd_max_fragmented_recv_size", proc_fs_cifs);
1018 	remove_proc_entry("smbd_max_send_size", proc_fs_cifs);
1019 	remove_proc_entry("smbd_send_credit_target", proc_fs_cifs);
1020 	remove_proc_entry("smbd_receive_credit_max", proc_fs_cifs);
1021 #endif
1022 	remove_proc_entry("fs/cifs", NULL);
1023 }
1024 
1025 static int cifsFYI_proc_show(struct seq_file *m, void *v)
1026 {
1027 	seq_printf(m, "%d\n", cifsFYI);
1028 	return 0;
1029 }
1030 
1031 static int cifsFYI_proc_open(struct inode *inode, struct file *file)
1032 {
1033 	return single_open(file, cifsFYI_proc_show, NULL);
1034 }
1035 
1036 static ssize_t cifsFYI_proc_write(struct file *file, const char __user *buffer,
1037 		size_t count, loff_t *ppos)
1038 {
1039 	char c[2] = { '\0' };
1040 	bool bv;
1041 	int rc;
1042 
1043 	rc = get_user(c[0], buffer);
1044 	if (rc)
1045 		return rc;
1046 	if (kstrtobool(c, &bv) == 0)
1047 		cifsFYI = bv;
1048 	else if ((c[0] > '1') && (c[0] <= '9'))
1049 		cifsFYI = (int) (c[0] - '0'); /* see cifs_debug.h for meanings */
1050 	else
1051 		return -EINVAL;
1052 
1053 	return count;
1054 }
1055 
1056 static const struct proc_ops cifsFYI_proc_ops = {
1057 	.proc_open	= cifsFYI_proc_open,
1058 	.proc_read	= seq_read,
1059 	.proc_lseek	= seq_lseek,
1060 	.proc_release	= single_release,
1061 	.proc_write	= cifsFYI_proc_write,
1062 };
1063 
1064 static int cifs_linux_ext_proc_show(struct seq_file *m, void *v)
1065 {
1066 	seq_printf(m, "%d\n", linuxExtEnabled);
1067 	return 0;
1068 }
1069 
1070 static int cifs_linux_ext_proc_open(struct inode *inode, struct file *file)
1071 {
1072 	return single_open(file, cifs_linux_ext_proc_show, NULL);
1073 }
1074 
1075 static ssize_t cifs_linux_ext_proc_write(struct file *file,
1076 		const char __user *buffer, size_t count, loff_t *ppos)
1077 {
1078 	int rc;
1079 
1080 	rc = kstrtobool_from_user(buffer, count, &linuxExtEnabled);
1081 	if (rc)
1082 		return rc;
1083 
1084 	return count;
1085 }
1086 
1087 static const struct proc_ops cifs_linux_ext_proc_ops = {
1088 	.proc_open	= cifs_linux_ext_proc_open,
1089 	.proc_read	= seq_read,
1090 	.proc_lseek	= seq_lseek,
1091 	.proc_release	= single_release,
1092 	.proc_write	= cifs_linux_ext_proc_write,
1093 };
1094 
1095 static int cifs_lookup_cache_proc_show(struct seq_file *m, void *v)
1096 {
1097 	seq_printf(m, "%d\n", lookupCacheEnabled);
1098 	return 0;
1099 }
1100 
1101 static int cifs_lookup_cache_proc_open(struct inode *inode, struct file *file)
1102 {
1103 	return single_open(file, cifs_lookup_cache_proc_show, NULL);
1104 }
1105 
1106 static ssize_t cifs_lookup_cache_proc_write(struct file *file,
1107 		const char __user *buffer, size_t count, loff_t *ppos)
1108 {
1109 	int rc;
1110 
1111 	rc = kstrtobool_from_user(buffer, count, &lookupCacheEnabled);
1112 	if (rc)
1113 		return rc;
1114 
1115 	return count;
1116 }
1117 
1118 static const struct proc_ops cifs_lookup_cache_proc_ops = {
1119 	.proc_open	= cifs_lookup_cache_proc_open,
1120 	.proc_read	= seq_read,
1121 	.proc_lseek	= seq_lseek,
1122 	.proc_release	= single_release,
1123 	.proc_write	= cifs_lookup_cache_proc_write,
1124 };
1125 
1126 static int traceSMB_proc_show(struct seq_file *m, void *v)
1127 {
1128 	seq_printf(m, "%d\n", traceSMB);
1129 	return 0;
1130 }
1131 
1132 static int traceSMB_proc_open(struct inode *inode, struct file *file)
1133 {
1134 	return single_open(file, traceSMB_proc_show, NULL);
1135 }
1136 
1137 static ssize_t traceSMB_proc_write(struct file *file, const char __user *buffer,
1138 		size_t count, loff_t *ppos)
1139 {
1140 	int rc;
1141 
1142 	rc = kstrtobool_from_user(buffer, count, &traceSMB);
1143 	if (rc)
1144 		return rc;
1145 
1146 	return count;
1147 }
1148 
1149 static const struct proc_ops traceSMB_proc_ops = {
1150 	.proc_open	= traceSMB_proc_open,
1151 	.proc_read	= seq_read,
1152 	.proc_lseek	= seq_lseek,
1153 	.proc_release	= single_release,
1154 	.proc_write	= traceSMB_proc_write,
1155 };
1156 
1157 static int cifs_security_flags_proc_show(struct seq_file *m, void *v)
1158 {
1159 	seq_printf(m, "0x%x\n", global_secflags);
1160 	return 0;
1161 }
1162 
1163 static int cifs_security_flags_proc_open(struct inode *inode, struct file *file)
1164 {
1165 	return single_open(file, cifs_security_flags_proc_show, NULL);
1166 }
1167 
1168 /*
1169  * Ensure that if someone sets a MUST flag, that we disable all other MAY
1170  * flags except for the ones corresponding to the given MUST flag. If there are
1171  * multiple MUST flags, then try to prefer more secure ones.
1172  */
1173 static void
1174 cifs_security_flags_handle_must_flags(unsigned int *flags)
1175 {
1176 	unsigned int signflags = *flags & (CIFSSEC_MUST_SIGN | CIFSSEC_MUST_SEAL);
1177 
1178 	if ((*flags & CIFSSEC_MUST_KRB5) == CIFSSEC_MUST_KRB5)
1179 		*flags = CIFSSEC_MUST_KRB5;
1180 	else if ((*flags & CIFSSEC_MUST_NTLMSSP) == CIFSSEC_MUST_NTLMSSP)
1181 		*flags = CIFSSEC_MUST_NTLMSSP;
1182 	else if ((*flags & CIFSSEC_MUST_NTLMV2) == CIFSSEC_MUST_NTLMV2)
1183 		*flags = CIFSSEC_MUST_NTLMV2;
1184 
1185 	*flags |= signflags;
1186 }
1187 
1188 static ssize_t cifs_security_flags_proc_write(struct file *file,
1189 		const char __user *buffer, size_t count, loff_t *ppos)
1190 {
1191 	int rc;
1192 	unsigned int flags;
1193 	char flags_string[12];
1194 	bool bv;
1195 
1196 	if ((count < 1) || (count > 11))
1197 		return -EINVAL;
1198 
1199 	memset(flags_string, 0, sizeof(flags_string));
1200 
1201 	if (copy_from_user(flags_string, buffer, count))
1202 		return -EFAULT;
1203 
1204 	if (count < 3) {
1205 		/* single char or single char followed by null */
1206 		if (kstrtobool(flags_string, &bv) == 0) {
1207 			global_secflags = bv ? CIFSSEC_MAX : CIFSSEC_DEF;
1208 			return count;
1209 		} else if (!isdigit(flags_string[0])) {
1210 			cifs_dbg(VFS, "Invalid SecurityFlags: %s\n",
1211 					flags_string);
1212 			return -EINVAL;
1213 		}
1214 	}
1215 
1216 	/* else we have a number */
1217 	rc = kstrtouint(flags_string, 0, &flags);
1218 	if (rc) {
1219 		cifs_dbg(VFS, "Invalid SecurityFlags: %s\n",
1220 				flags_string);
1221 		return rc;
1222 	}
1223 
1224 	cifs_dbg(FYI, "sec flags 0x%x\n", flags);
1225 
1226 	if (flags == 0)  {
1227 		cifs_dbg(VFS, "Invalid SecurityFlags: %s\n", flags_string);
1228 		return -EINVAL;
1229 	}
1230 
1231 	if (flags & ~CIFSSEC_MASK) {
1232 		cifs_dbg(VFS, "Unsupported security flags: 0x%x\n",
1233 			 flags & ~CIFSSEC_MASK);
1234 		return -EINVAL;
1235 	}
1236 
1237 	cifs_security_flags_handle_must_flags(&flags);
1238 
1239 	/* flags look ok - update the global security flags for cifs module */
1240 	global_secflags = flags;
1241 	if (global_secflags & CIFSSEC_MUST_SIGN) {
1242 		/* requiring signing implies signing is allowed */
1243 		global_secflags |= CIFSSEC_MAY_SIGN;
1244 		cifs_dbg(FYI, "packet signing now required\n");
1245 	} else if ((global_secflags & CIFSSEC_MAY_SIGN) == 0) {
1246 		cifs_dbg(FYI, "packet signing disabled\n");
1247 	}
1248 	/* BB should we turn on MAY flags for other MUST options? */
1249 	return count;
1250 }
1251 
1252 static const struct proc_ops cifs_security_flags_proc_ops = {
1253 	.proc_open	= cifs_security_flags_proc_open,
1254 	.proc_read	= seq_read,
1255 	.proc_lseek	= seq_lseek,
1256 	.proc_release	= single_release,
1257 	.proc_write	= cifs_security_flags_proc_write,
1258 };
1259 
1260 /* To make it easier to debug, can help to show mount params */
1261 static int cifs_mount_params_proc_show(struct seq_file *m, void *v)
1262 {
1263 	const struct fs_parameter_spec *p;
1264 	const char *type;
1265 
1266 	for (p = smb3_fs_parameters; p->name; p++) {
1267 		/* cannot use switch with pointers... */
1268 		if (!p->type) {
1269 			if (p->flags == fs_param_neg_with_no)
1270 				type = "noflag";
1271 			else
1272 				type = "flag";
1273 		} else if (p->type == fs_param_is_bool)
1274 			type = "bool";
1275 		else if (p->type == fs_param_is_u32)
1276 			type = "u32";
1277 		else if (p->type == fs_param_is_u64)
1278 			type = "u64";
1279 		else if (p->type == fs_param_is_string)
1280 			type = "string";
1281 		else
1282 			type = "unknown";
1283 
1284 		seq_printf(m, "%s:%s\n", p->name, type);
1285 	}
1286 
1287 	return 0;
1288 }
1289 
1290 static int cifs_mount_params_proc_open(struct inode *inode, struct file *file)
1291 {
1292 	return single_open(file, cifs_mount_params_proc_show, NULL);
1293 }
1294 
1295 static const struct proc_ops cifs_mount_params_proc_ops = {
1296 	.proc_open	= cifs_mount_params_proc_open,
1297 	.proc_read	= seq_read,
1298 	.proc_lseek	= seq_lseek,
1299 	.proc_release	= single_release,
1300 	/* No need for write for now */
1301 	/* .proc_write	= cifs_mount_params_proc_write, */
1302 };
1303 
1304 #else
1305 void cifs_proc_init(void)
1306 {
1307 }
1308 
1309 void cifs_proc_clean(void)
1310 {
1311 }
1312 #endif /* PROC_FS */
1313