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