xref: /linux/fs/smb/server/stats.h (revision c3d85c669d09007aeb9eb9f3d28d8c863401f83f)
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3  *   Copyright (C) 2025, LG Electronics.
4  *   Author(s): Hyunchul Lee <hyc.lee@gmail.com>
5  *   Copyright (C) 2025, Samsung Electronics.
6  *   Author(s): Vedansh Bhardwaj <v.bhardwaj@samsung.com>
7  */
8 
9 #ifndef __KSMBD_STATS_H__
10 #define __KSMBD_STATS_H__
11 
12 #include "../common/smb2status.h"
13 
14 #define KSMBD_COUNTER_MAX_REQS	19
15 
16 enum {
17 	KSMBD_COUNTER_SESSIONS = 0,
18 	KSMBD_COUNTER_SESSION_TIMEOUTS,
19 	KSMBD_COUNTER_TREE_CONNS,
20 	KSMBD_COUNTER_REQUESTS,
21 	KSMBD_COUNTER_STATUS_SUCCESS,
22 	KSMBD_COUNTER_STATUS_INFORMATIONAL,
23 	KSMBD_COUNTER_STATUS_WARNING,
24 	KSMBD_COUNTER_STATUS_ERROR,
25 	KSMBD_COUNTER_ERROR_ACCESS_DENIED,
26 	KSMBD_COUNTER_ERROR_NOT_FOUND,
27 	KSMBD_COUNTER_ERROR_INVALID_PARAMETER,
28 	KSMBD_COUNTER_ERROR_SHARING_VIOLATION,
29 	KSMBD_COUNTER_ERROR_NOT_SUPPORTED,
30 	KSMBD_COUNTER_ERROR_OTHER,
31 	KSMBD_COUNTER_READ_BYTES,
32 	KSMBD_COUNTER_WRITE_BYTES,
33 	KSMBD_COUNTER_FIRST_REQ,
34 	KSMBD_COUNTER_LAST_REQ = KSMBD_COUNTER_FIRST_REQ +
35 				KSMBD_COUNTER_MAX_REQS - 1,
36 	KSMBD_COUNTER_MAX,
37 };
38 
39 #ifdef CONFIG_PROC_FS
40 extern struct ksmbd_counters ksmbd_counters;
41 
42 struct ksmbd_counters {
43 	struct percpu_counter	counters[KSMBD_COUNTER_MAX];
44 };
45 
ksmbd_counter_inc(int type)46 static inline void ksmbd_counter_inc(int type)
47 {
48 	percpu_counter_inc(&ksmbd_counters.counters[type]);
49 }
50 
ksmbd_counter_dec(int type)51 static inline void ksmbd_counter_dec(int type)
52 {
53 	percpu_counter_dec(&ksmbd_counters.counters[type]);
54 }
55 
ksmbd_counter_add(int type,s64 value)56 static inline void ksmbd_counter_add(int type, s64 value)
57 {
58 	percpu_counter_add(&ksmbd_counters.counters[type], value);
59 }
60 
ksmbd_counter_sub(int type,s64 value)61 static inline void ksmbd_counter_sub(int type, s64 value)
62 {
63 	percpu_counter_sub(&ksmbd_counters.counters[type], value);
64 }
65 
ksmbd_counter_inc_reqs(unsigned int cmd,__le32 status)66 static inline void ksmbd_counter_inc_reqs(unsigned int cmd, __le32 status)
67 {
68 	unsigned int severity = le32_to_cpu(status) >> 30;
69 	int type;
70 
71 	switch (severity) {
72 	case 0:
73 		type = KSMBD_COUNTER_STATUS_SUCCESS;
74 		break;
75 	case 1:
76 		type = KSMBD_COUNTER_STATUS_INFORMATIONAL;
77 		break;
78 	case 2:
79 		type = KSMBD_COUNTER_STATUS_WARNING;
80 		break;
81 	default:
82 		type = KSMBD_COUNTER_STATUS_ERROR;
83 		break;
84 	}
85 	percpu_counter_inc(&ksmbd_counters.counters[type]);
86 
87 	if (severity == 3) {
88 		if (status == STATUS_ACCESS_DENIED)
89 			type = KSMBD_COUNTER_ERROR_ACCESS_DENIED;
90 		else if (status == STATUS_OBJECT_NAME_NOT_FOUND ||
91 			 status == STATUS_NO_SUCH_FILE)
92 			type = KSMBD_COUNTER_ERROR_NOT_FOUND;
93 		else if (status == STATUS_INVALID_PARAMETER)
94 			type = KSMBD_COUNTER_ERROR_INVALID_PARAMETER;
95 		else if (status == STATUS_SHARING_VIOLATION)
96 			type = KSMBD_COUNTER_ERROR_SHARING_VIOLATION;
97 		else if (status == STATUS_NOT_SUPPORTED ||
98 			 status == STATUS_NOT_IMPLEMENTED)
99 			type = KSMBD_COUNTER_ERROR_NOT_SUPPORTED;
100 		else
101 			type = KSMBD_COUNTER_ERROR_OTHER;
102 		percpu_counter_inc(&ksmbd_counters.counters[type]);
103 	}
104 
105 	if (cmd < KSMBD_COUNTER_MAX_REQS) {
106 		percpu_counter_inc(&ksmbd_counters.counters[KSMBD_COUNTER_REQUESTS]);
107 		percpu_counter_inc(&ksmbd_counters.counters[KSMBD_COUNTER_FIRST_REQ + cmd]);
108 	}
109 }
110 
ksmbd_counter_sum(int type)111 static inline s64 ksmbd_counter_sum(int type)
112 {
113 	return percpu_counter_sum_positive(&ksmbd_counters.counters[type]);
114 }
115 #else
116 
ksmbd_counter_inc(int type)117 static inline void ksmbd_counter_inc(int type) {}
ksmbd_counter_dec(int type)118 static inline void ksmbd_counter_dec(int type) {}
ksmbd_counter_add(int type,s64 value)119 static inline void ksmbd_counter_add(int type, s64 value) {}
ksmbd_counter_sub(int type,s64 value)120 static inline void ksmbd_counter_sub(int type, s64 value) {}
ksmbd_counter_inc_reqs(unsigned int cmd,__le32 status)121 static inline void ksmbd_counter_inc_reqs(unsigned int cmd, __le32 status) {}
ksmbd_counter_sum(int type)122 static inline s64 ksmbd_counter_sum(int type) { return 0; }
123 #endif
124 
125 #endif
126