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