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