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