Skip to content

Commit b38f99c

Browse files
bahubali-bgsmfrench
authored andcommitted
ksmbd: add procfs interface for runtime monitoring and statistics
This patch introduces a /proc filesystem interface to ksmbd, providing visibility into the internal state of the SMB server. This allows administrators and developers to monitor active connections, user sessions, and opened files in real-time without relying on external tools or heavy debugging. Key changes include: - Connection Monitoring (/proc/fs/ksmbd/clients): Displays a list of active network connections, including client IP addresses, SMB dialects, credits, and last active timestamps. - Session Management (/proc/fs/ksmbd/sessions/): Adds a global sessions file to list all authenticated users and their session IDs. - Creates individual session entries (e.g., /proc/fs/ksmbd/sessions/<id>) detailing capabilities (DFS, Multi-channel, etc.), signing/encryption algorithms, and connected tree shares. - File Tracking (/proc/fs/ksmbd/files): Shows all currently opened files across the server, including tree IDs, process IDs (PID), access modes (daccess/saccess), and oplock/lease states. - Statistics & Counters: Implements internal counters for global server metrics, such as the number of tree connections, total sessions, and processed read/write bytes. Signed-off-by: Hyunchul Lee <hyc.lee@gmail.com> Signed-off-by: Bahubali B Gumaji <bahubali.bg@samsung.com> Signed-off-by: Sang-Soo Lee <constant.lee@samsung.com> Signed-off-by: Namjae Jeon <linkinjeon@kernel.org> Signed-off-by: Steve French <stfrench@microsoft.com>
1 parent 010eb01 commit b38f99c

18 files changed

Lines changed: 661 additions & 8 deletions

fs/smb/server/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,4 @@ $(obj)/ksmbd_spnego_negtokeninit.asn1.o: $(obj)/ksmbd_spnego_negtokeninit.asn1.c
1818
$(obj)/ksmbd_spnego_negtokentarg.asn1.o: $(obj)/ksmbd_spnego_negtokentarg.asn1.c $(obj)/ksmbd_spnego_negtokentarg.asn1.h
1919

2020
ksmbd-$(CONFIG_SMB_SERVER_SMBDIRECT) += transport_rdma.o
21+
ksmbd-$(CONFIG_PROC_FS) += proc.o

fs/smb/server/connection.c

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "connection.h"
1515
#include "transport_tcp.h"
1616
#include "transport_rdma.h"
17+
#include "misc.h"
1718

1819
static DEFINE_MUTEX(init_lock);
1920

@@ -22,6 +23,60 @@ static struct ksmbd_conn_ops default_conn_ops;
2223
DEFINE_HASHTABLE(conn_list, CONN_HASH_BITS);
2324
DECLARE_RWSEM(conn_list_lock);
2425

26+
#ifdef CONFIG_PROC_FS
27+
static struct proc_dir_entry *proc_clients;
28+
29+
static int proc_show_clients(struct seq_file *m, void *v)
30+
{
31+
struct ksmbd_conn *conn;
32+
struct timespec64 now, t;
33+
int i;
34+
35+
seq_printf(m, "#%-20s %-10s %-10s %-10s %-10s %-10s\n",
36+
"<name>", "<dialect>", "<credits>", "<open files>",
37+
"<requests>", "<last active>");
38+
39+
down_read(&conn_list_lock);
40+
hash_for_each(conn_list, i, conn, hlist) {
41+
jiffies_to_timespec64(jiffies - conn->last_active, &t);
42+
ktime_get_real_ts64(&now);
43+
t = timespec64_sub(now, t);
44+
if (conn->inet_addr)
45+
seq_printf(m, "%-20pI4", &conn->inet_addr);
46+
else
47+
seq_printf(m, "%-20pI6c", &conn->inet6_addr);
48+
seq_printf(m, " 0x%-10x %-10u %-12d %-10d %ptT\n",
49+
conn->dialect,
50+
conn->total_credits,
51+
atomic_read(&conn->stats.open_files_count),
52+
atomic_read(&conn->req_running),
53+
&t);
54+
}
55+
up_read(&conn_list_lock);
56+
return 0;
57+
}
58+
59+
static int create_proc_clients(void)
60+
{
61+
proc_clients = ksmbd_proc_create("clients",
62+
proc_show_clients, NULL);
63+
if (!proc_clients)
64+
return -ENOMEM;
65+
return 0;
66+
}
67+
68+
static void delete_proc_clients(void)
69+
{
70+
if (proc_clients) {
71+
proc_remove(proc_clients);
72+
proc_clients = NULL;
73+
}
74+
}
75+
#else
76+
static int create_proc_clients(void) { return 0; }
77+
static void delete_proc_clients(void) {}
78+
#endif
79+
2580
/**
2681
* ksmbd_conn_free() - free resources of the connection instance
2782
*
@@ -472,6 +527,7 @@ int ksmbd_conn_transport_init(void)
472527
}
473528
out:
474529
mutex_unlock(&init_lock);
530+
create_proc_clients();
475531
return ret;
476532
}
477533

@@ -502,6 +558,7 @@ static void stop_sessions(void)
502558

503559
void ksmbd_conn_transport_destroy(void)
504560
{
561+
delete_proc_clients();
505562
mutex_lock(&init_lock);
506563
ksmbd_tcp_destroy();
507564
ksmbd_rdma_stop_listening();

fs/smb/server/connection.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#define __KSMBD_CONNECTION_H__
88

99
#include <linux/list.h>
10+
#include <linux/inet.h>
1011
#include <linux/ip.h>
1112
#include <net/sock.h>
1213
#include <net/tcp.h>
@@ -33,7 +34,7 @@ enum {
3334
KSMBD_SESS_RELEASING
3435
};
3536

36-
struct ksmbd_stats {
37+
struct ksmbd_conn_stats {
3738
atomic_t open_files_count;
3839
atomic64_t request_served;
3940
};
@@ -78,7 +79,7 @@ struct ksmbd_conn {
7879
struct list_head requests;
7980
struct list_head async_requests;
8081
int connection_type;
81-
struct ksmbd_stats stats;
82+
struct ksmbd_conn_stats stats;
8283
char ClientGUID[SMB2_CLIENT_GUID_SIZE];
8384
struct ntlmssp_auth ntlmssp;
8485

fs/smb/server/mgmt/tree_connect.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#include "../transport_ipc.h"
1111
#include "../connection.h"
12+
#include "../stats.h"
1213

1314
#include "tree_connect.h"
1415
#include "user_config.h"
@@ -85,6 +86,7 @@ ksmbd_tree_conn_connect(struct ksmbd_work *work, const char *share_name)
8586
status.ret = -ENOMEM;
8687
goto out_error;
8788
}
89+
ksmbd_counter_inc(KSMBD_COUNTER_TREE_CONNS);
8890
kvfree(resp);
8991
return status;
9092

@@ -115,6 +117,7 @@ int ksmbd_tree_conn_disconnect(struct ksmbd_session *sess,
115117
ret = ksmbd_ipc_tree_disconnect_request(sess->id, tree_conn->id);
116118
ksmbd_release_tree_conn_id(sess, tree_conn->id);
117119
ksmbd_share_config_put(tree_conn->share_conf);
120+
ksmbd_counter_dec(KSMBD_COUNTER_TREE_CONNS);
118121
if (atomic_dec_and_test(&tree_conn->refcount))
119122
kfree(tree_conn);
120123
return ret;

fs/smb/server/mgmt/user_config.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,9 @@ void ksmbd_free_user(struct ksmbd_user *user)
9090
kfree(user);
9191
}
9292

93-
int ksmbd_anonymous_user(struct ksmbd_user *user)
93+
bool ksmbd_anonymous_user(struct ksmbd_user *user)
9494
{
95-
if (user->name[0] == '\0')
96-
return 1;
97-
return 0;
95+
return user->name[0] == '\0';
9896
}
9997

10098
bool ksmbd_compare_user(struct ksmbd_user *u1, struct ksmbd_user *u2)

fs/smb/server/mgmt/user_config.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,6 @@ struct ksmbd_user *ksmbd_login_user(const char *account);
6565
struct ksmbd_user *ksmbd_alloc_user(struct ksmbd_login_response *resp,
6666
struct ksmbd_login_response_ext *resp_ext);
6767
void ksmbd_free_user(struct ksmbd_user *user);
68-
int ksmbd_anonymous_user(struct ksmbd_user *user);
68+
bool ksmbd_anonymous_user(struct ksmbd_user *user);
6969
bool ksmbd_compare_user(struct ksmbd_user *u1, struct ksmbd_user *u2);
7070
#endif /* __USER_CONFIG_MANAGEMENT_H__ */

0 commit comments

Comments
 (0)