Merge branch 'master' of ssh://git.samba.org/data/git/samba into wspp-schema
[ira/wip.git] / source3 / modules / perfcount_onefs.c
1 /*
2  * Unix SMB/CIFS implementation.
3  * Support for OneFS protocol statistics / perfcounters
4  *
5  * Copyright (C) Todd Stecher 2008
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, see <http://www.gnu.org/licenses/>.
19  */
20
21 #include "includes.h"
22 #include <sys/isi_stats_protocol.h>
23 #include <sys/isi_stats_cifs.h>
24
25 struct onefs_op_counter {
26         struct isp_op_delta iod;
27         struct onefs_op_counter *next;
28         struct onefs_op_counter *prev;
29 };
30
31 struct onefs_stats_context {
32         bool alloced;
33         struct isp_op_delta iod;
34
35         /* ANDX commands stats stored here */
36         struct onefs_op_counter *ops_chain;
37 };
38
39 const char *onefs_stat_debug(struct isp_op_delta *iod);
40
41 struct onefs_stats_context g_context;
42
43 static void onefs_smb_statistics_end(struct smb_perfcount_data *pcd);
44
45 struct isp_op_delta *onefs_stats_get_op_delta(struct onefs_stats_context *ctxt)
46 {
47         /* operate on head of chain */
48         if (ctxt->ops_chain) {
49 #ifdef ONEFS_PERF_DEBUG
50                 DEBUG(0,("************* CHAINED *****\n"));
51 #endif
52                 return &ctxt->ops_chain->iod;
53         } else
54                 return &ctxt->iod;
55 }
56
57 /* statistics operations */
58 static void onefs_smb_statistics_start(struct smb_perfcount_data *pcd)
59 {
60
61 #ifdef ONEFS_PERF_DEBUG
62         if (g_context.iod.op) {
63                 DEBUG(0,("**************** OP Collision! %s(%d) \n",
64                         onefs_stat_debug(&g_context.iod), g_context.iod.op));
65         }
66
67 #endif
68
69         ISP_OP_BEG(&g_context.iod, ISP_PROTO_CIFS, 0);
70
71         if (g_context.iod.enabled)
72                 pcd->context = &g_context;
73         else
74                 pcd->context = NULL;
75
76
77 }
78
79 static void onefs_smb_statistics_add(struct smb_perfcount_data *pcd)
80 {
81         struct onefs_op_counter *oc;
82         struct onefs_stats_context *ctxt = pcd->context;
83
84         /* not enabled */
85         if (pcd->context == NULL)
86                 return;
87
88         oc = SMB_MALLOC_P(struct onefs_op_counter);
89
90         if (oc == NULL)
91                 return;
92
93 #ifdef ONEFS_PERF_DEBUG
94         DEBUG(0,("*********** add chained op \n"));
95 #endif
96
97         DLIST_ADD(ctxt->ops_chain, oc);
98         ISP_OP_BEG(&oc->iod, ISP_PROTO_CIFS, 0);
99 }
100
101 static void onefs_smb_statistics_set_op(struct smb_perfcount_data *pcd, int op)
102 {
103         struct onefs_stats_context *ctxt = pcd->context;
104         struct isp_op_delta *iod;
105
106         /* not enabled */
107         if (pcd->context == NULL)
108                 return;
109
110         iod = onefs_stats_get_op_delta(ctxt);
111         iod->op = isp_cifs_op_id(op);
112
113 #ifdef ONEFS_PERF_DEBUG
114         DEBUG(0,("***********SET op %s(%d)\n", onefs_stat_debug(iod), op));
115 #endif
116         /* no reply required */
117         if (op == SMBntcancel)
118                 onefs_smb_statistics_end(pcd);
119
120 }
121
122 static void onefs_smb_statistics_set_subop(struct smb_perfcount_data *pcd,
123                                            int subop)
124 {
125         struct onefs_stats_context *ctxt = pcd->context;
126         struct isp_op_delta *iod;
127
128         /* not enabled */
129         if (pcd->context == NULL)
130                 return;
131
132         iod = onefs_stats_get_op_delta(ctxt);
133         iod->op = isp_cifs_sub_op_id(iod->op, subop);
134
135 #ifdef ONEFS_PERF_DEBUG
136         DEBUG(0,("********************  SET subop %s(%d)\n",
137                 onefs_stat_debug(iod), subop));
138 #endif
139
140         /*
141          * finalize this one - we don't need to know when it
142          * is set, but its useful as a counter
143          */
144         if (subop == NT_TRANSACT_NOTIFY_CHANGE)
145                 onefs_smb_statistics_end(pcd);
146 }
147
148 static void onefs_smb_statistics_set_ioctl(struct smb_perfcount_data *pcd,
149                                            int io_ctl)
150 {
151         struct onefs_stats_context *ctxt = pcd->context;
152         struct isp_op_delta *iod;
153
154         /* not enabled */
155         if (pcd->context == NULL)
156                 return;
157
158         /* we only monitor shadow copy */
159         if (io_ctl != FSCTL_GET_SHADOW_COPY_DATA)
160                 return;
161
162         iod = onefs_stats_get_op_delta(ctxt);
163         iod->op = isp_cifs_sub_op_id(iod->op, ISP_CIFS_NTRN_IOCTL_FGSCD);
164 #ifdef ONEFS_PERF_DEBUG
165         DEBUG(0,("********************  SET ioctl %s(%d)\n",
166                 onefs_stat_debug(iod), io_ctl));
167 #endif
168 }
169
170 static void onefs_smb_statistics_set_msglen_in(struct smb_perfcount_data *pcd,
171                                                uint64_t in_bytes)
172 {
173         struct onefs_stats_context *ctxt = pcd->context;
174         struct isp_op_delta *iod;
175
176         /* not enabled */
177         if (pcd->context == NULL)
178                 return;
179
180         iod = onefs_stats_get_op_delta(ctxt);
181         iod->in_bytes = in_bytes;
182 }
183
184 static void onefs_smb_statistics_set_msglen_out(struct smb_perfcount_data *pcd,
185                                                 uint64_t out_bytes)
186 {
187         struct onefs_stats_context *ctxt = pcd->context;
188
189         /* not enabled */
190         if (pcd->context == NULL)
191                 return;
192
193         if (ctxt->ops_chain)
194                 ctxt->ops_chain->iod.out_bytes = out_bytes;
195
196         ctxt->iod.out_bytes = out_bytes;
197 }
198
199 static int onefs_copy_perfcount_context(struct onefs_stats_context *ctxt,
200                                         struct onefs_stats_context **dest)
201 {
202         struct onefs_stats_context *new_ctxt;
203
204         /* make an alloc'd copy of the data */
205         new_ctxt = SMB_MALLOC_P(struct onefs_stats_context);
206         if (!new_ctxt) {
207                 return -1;
208         }
209
210         memcpy(new_ctxt, ctxt, sizeof(struct onefs_stats_context));
211         new_ctxt->alloced = True;
212         *dest = new_ctxt;
213         return 0;
214 }
215
216 static void onefs_smb_statistics_copy_context(struct smb_perfcount_data *pcd,
217                                               struct smb_perfcount_data *dest)
218 {
219         struct onefs_stats_context *ctxt = pcd->context;
220         struct onefs_stats_context *new_ctxt;
221         int ret;
222
223         /* not enabled */
224         if (pcd->context == NULL)
225                 return;
226
227 #ifdef ONEFS_PERF_DEBUG
228         DEBUG(0,("********  COPYING op %s(%d)\n",
229                 onefs_stat_debug(&ctxt->iod), ctxt->iod.op));
230 #endif
231
232         ret = onefs_copy_perfcount_context(ctxt, &new_ctxt);
233         if (ret)
234                 return;
235
236         /* instrumentation */
237         if (ctxt == &g_context)
238                 ZERO_STRUCT(g_context);
239
240         dest->context = new_ctxt;
241 }
242
243 /*
244  * For perf reasons, we usually use the global - sometimes, though,
245  * when an operation is deferred, we need to alloc a copy.
246  */
247 static void onefs_smb_statistics_defer_op(struct smb_perfcount_data *pcd,
248                                           struct smb_perfcount_data *def_pcd)
249 {
250         struct onefs_stats_context *ctxt = pcd->context;
251         struct onefs_stats_context *deferred_ctxt;
252         int ret;
253
254         /* not enabled */
255         if (pcd->context == NULL)
256                 return;
257
258         /* already allocated ? */
259         if (ctxt->alloced)
260         {
261                 def_pcd->context = ctxt;
262                 pcd->context = NULL;
263                 return;
264         }
265
266 #ifdef ONEFS_PERF_DEBUG
267         DEBUG(0,("********  DEFERRING op %s(%d)\n",
268                 onefs_stat_debug(&ctxt->iod), ctxt->iod.op));
269 #endif
270
271         ret = onefs_copy_perfcount_context(ctxt, &deferred_ctxt);
272         if (ret)
273                 return;
274
275         def_pcd->context = (void*) deferred_ctxt;
276
277         /* instrumentation */
278         if (ctxt == &g_context)
279                 ZERO_STRUCT(g_context);
280
281         if (pcd != def_pcd)
282                 pcd->context = NULL;
283 }
284
285 static void onefs_smb_statistics_set_client(struct smb_perfcount_data *pcd,
286                                             uid_t uid, const char *user,
287                                             const char *domain)
288 {
289         // not implemented...
290         return;
291 }
292
293 static void onefs_smb_statistics_end(struct smb_perfcount_data *pcd)
294 {
295         struct onefs_stats_context *ctxt = pcd->context;
296         struct onefs_op_counter *tmp;
297
298         /* not enabled */
299         if (pcd->context == NULL)
300                 return;
301
302         /*
303          * bug here - we aren't getting the outlens right,
304          * when dealing w/ chained requests.
305          */
306         for (tmp = ctxt->ops_chain; tmp; tmp = tmp->next) {
307                 tmp->iod.out_bytes = ctxt->iod.out_bytes;
308                 ISP_OP_END(&tmp->iod);
309 #ifdef ONEFS_PERF_DEBUG
310                 DEBUG(0,("********  Finalized CHAIN op %s in:%llu, out:%llu\n",
311                         onefs_stat_debug(&tmp->iod),
312                         tmp->iod.in_bytes, tmp->iod.out_bytes));
313 #endif
314                 SAFE_FREE(tmp->prev);
315         }
316
317         ISP_OP_END(&ctxt->iod);
318 #ifdef ONEFS_PERF_DEBUG
319         DEBUG(0,("********  Finalized op %s in:%llu, out:%llu\n",
320                 onefs_stat_debug(&ctxt->iod),
321                 ctxt->iod.in_bytes, ctxt->iod.out_bytes));
322 #endif
323
324         if (ctxt->alloced)
325                 SAFE_FREE(ctxt);
326         else
327                 ZERO_STRUCTP(ctxt);
328
329         pcd->context = NULL;
330 }
331
332
333 static struct smb_perfcount_handlers onefs_pc_handlers = {
334         onefs_smb_statistics_start,
335         onefs_smb_statistics_add,
336         onefs_smb_statistics_set_op,
337         onefs_smb_statistics_set_subop,
338         onefs_smb_statistics_set_ioctl,
339         onefs_smb_statistics_set_msglen_in,
340         onefs_smb_statistics_set_msglen_out,
341         onefs_smb_statistics_set_client,
342         onefs_smb_statistics_copy_context,
343         onefs_smb_statistics_defer_op,
344         onefs_smb_statistics_end
345 };
346
347 NTSTATUS perfcount_onefs_init(void)
348 {
349         return smb_register_perfcounter(SMB_PERFCOUNTER_INTERFACE_VERSION,
350                                         "pc_onefs", &onefs_pc_handlers);
351 }
352
353 #ifdef ONEFS_PERF_DEBUG
354 /* debug helper */
355 struct op_debug {
356         int type;
357         const char *name;
358 };
359
360 struct op_debug op_debug_table[] = {
361         { 0x00, "mkdir"}, { 0x01, "rmdir"}, { 0x02, "open"}, { 0x03, "create"},
362         { 0x04, "close"}, { 0x05, "flush"}, { 0x06, "unlink"}, { 0x07, "mv"},
363         { 0x08, "getatr"}, { 0x09, "setatr"}, { 0x0a, "read"}, { 0x0b, "write"},
364         { 0x0c, "lock"}, { 0x0d, "unlock"}, { 0x0e, "ctemp"}, { 0x0f, "mknew"},
365         { 0x10, "chkpth"}, { 0x11, "exit"}, { 0x12, "lseek"}, { 0x13, "lockread"},
366         { 0x14, "writeunlock"}, { 0x1a, "readbraw"}, { 0x1b, "readbmpx"},
367         { 0x1c, "readbs"}, { 0x1d, "writebraw"}, { 0x1e, "writebmpx"},
368         { 0x1f, "writebs"}, { 0x20, "writec"}, { 0x22, "setattre"},
369         { 0x23, "getattre"}, { 0x24, "lockingx"}, { 0x25, "trans"},
370         { 0x26, "transs"}, { 0x27, "ioctl"}, { 0x28, "ioctls"}, { 0x29, "copy"},
371         { 0x2a, "move"}, { 0x2b, "echo"}, { 0x2c, "writeclose"}, { 0x2d, "openx"},
372         { 0x2e, "readx"}, { 0x2f, "writex"}, { 0x34, "findclose"},
373         { 0x35, "findnclose"}, { 0x70, "tcon"}, { 0x71, "tdis"},
374         { 0x72, "negprot"}, { 0x73, "sesssetupx"}, { 0x74, "ulogoffx"},
375         { 0x75, "tconx"}, { 0x80, "dskattr"}, { 0x81, "search"},
376         { 0x82, "ffirst"}, { 0x83, "funique"}, { 0x84, "fclose"},
377         { 0x400, "nttrans"},{ 0x500, "nttranss"},
378         { 0xa2, "ntcreatex"}, { 0xa4, "ntcancel"}, { 0xa5, "ntrename"},
379         { 0xc0, "splopen"}, { 0xc1, "splwr"}, { 0xc2, "splclose"},
380         { 0xc3, "splretq"}, { 0xd0, "sends"}, { 0xd1, "sendb"},
381         { 0xd2, "fwdname"}, { 0xd3, "cancelf"}, { 0xd4, "getmac"},
382         { 0xd5, "sendstrt"}, { 0xd6, "sendend"}, { 0xd7, "sendtxt"},
383         { ISP_CIFS_INVALID_OP, "unknown"},
384         { ISP_CIFS_TRNS2 + 0x00,  "trans2:open"},
385         { ISP_CIFS_TRNS2 + 0x01,  "trans2:findfirst"},
386         { ISP_CIFS_TRNS2 + 0x02,  "trans2:findnext"},
387         { ISP_CIFS_TRNS2 + 0x03,  "trans2:qfsinfo"},
388         { ISP_CIFS_TRNS2 + 0x04,  "trans2:setfsinfo"},
389         { ISP_CIFS_TRNS2 + 0x05,  "trans2:qpathinfo"},
390         { ISP_CIFS_TRNS2 + 0x06,  "trans2:setpathinfo"},
391         { ISP_CIFS_TRNS2 + 0x07,  "trans2:qfileinfo"},
392         { ISP_CIFS_TRNS2 + 0x08,  "trans2:setfileinfo"},
393         { ISP_CIFS_TRNS2 + 0x0a,  "trans2:ioctl"},
394         { ISP_CIFS_TRNS2 + 0x0b,  "trans2:findnotifyfirst"},
395         { ISP_CIFS_TRNS2 + 0x0c,  "trans2:findnotifynext"},
396         { ISP_CIFS_TRNS2 + 0x0d,  "trans2:mkdir"},
397         { ISP_CIFS_TRNS2 + 0x10,  "trans2:get_dfs_ref"},
398         { ISP_CIFS_TRNS2 + ISP_CIFS_SUBOP_UNKNOWN, "trans2:unknown"},
399         { ISP_CIFS_TRNSS2 +0x00, "transs2:open"},
400         { ISP_CIFS_TRNSS2 +0x01, "transs2:findfirst"},
401         { ISP_CIFS_TRNSS2 +0x02, "transs2:findnext"},
402         { ISP_CIFS_TRNSS2 +0x03, "transs2:qfsinfo"},
403         { ISP_CIFS_TRNSS2 +0x04, "transs2:setfsinfo"},
404         { ISP_CIFS_TRNSS2 +0x05, "transs2:qpathinfo"},
405         { ISP_CIFS_TRNSS2 +0x06, "transs2:setpathinfo"},
406         { ISP_CIFS_TRNSS2 +0x07, "transs2:qfileinfo"},
407         { ISP_CIFS_TRNSS2 +0x08, "transs2:setfileinfo"},
408         { ISP_CIFS_TRNSS2 +0x0a, "transs2:ioctl"},
409         { ISP_CIFS_TRNSS2 +0x0b, "transs2:findnotifyfirst"},
410         { ISP_CIFS_TRNSS2 +0x0c, "transs2:findnotifynext"},
411         { ISP_CIFS_TRNSS2 +0x0d, "transs2:mkdir"},
412         { ISP_CIFS_TRNSS2 +0x10, "transs2:get_dfs_referral"},
413         { ISP_CIFS_TRNSS2 + ISP_CIFS_SUBOP_UNKNOWN, "transs2:unknown"},
414         { ISP_CIFS_NTRNS + 0x1, "nttrans:create"},
415         { ISP_CIFS_NTRNS + 0x2, "nttrans:ioctl"},
416         { ISP_CIFS_NTRNS + 0x3, "nttrans:set_security_desc"},
417         { ISP_CIFS_NTRNS + 0x4, "nttrans:notify_change"},
418         { ISP_CIFS_NTRNS + 0x5, "nttrans:rename"},
419         { ISP_CIFS_NTRNS + 0x6, "nttrans:qry_security_desc"},
420         { ISP_CIFS_NTRNS + 0x7, "nttrans:get_user_quota"},
421         { ISP_CIFS_NTRNS + 0x8, "nttrans:set_user_quota"},
422         { ISP_CIFS_NTRNS + ISP_CIFS_NTRN_IOCTL_FGSCD,
423                 "nttrans:ioctl:get_shadow_copy_data"},
424         { ISP_CIFS_NTRNS + ISP_CIFS_SUBOP_UNKNOWN,
425                 "nttrans:unknown"},
426         { ISP_CIFS_NTRNSS + 0x1, "nttranss:create"},
427         { ISP_CIFS_NTRNSS + 0x2, "nttranss:ioctl"},
428         { ISP_CIFS_NTRNSS + 0x3, "nttranss:set_security_desc"},
429         { ISP_CIFS_NTRNSS + 0x4, "nttranss:notify_change"},
430         { ISP_CIFS_NTRNSS + 0x5, "nttranss:rename"},
431         { ISP_CIFS_NTRNSS + 0x6, "nttranss:qry_security_desc"},
432         { ISP_CIFS_NTRNSS + 0x7, "nttranss:get_user_quota"},
433         { ISP_CIFS_NTRNSS + 0x8, "nttranss:set_user_quota"},
434         { ISP_CIFS_NTRNSS + ISP_CIFS_NTRN_IOCTL_FGSCD,
435                 "nttranss:ioctl:get_shadow_copy_data"},
436         { ISP_CIFS_NTRNSS + ISP_CIFS_SUBOP_UNKNOWN,
437                 "nttranss:unknown"},
438 };
439
440 int op_debug_table_count = sizeof(op_debug_table) / sizeof(op_debug_table[0]);
441
442 const char *onefs_stat_debug(struct isp_op_delta *iod)
443 {
444         int i;
445         const char *unk = "unknown";
446         for (i=0; i < op_debug_table_count;i++) {
447                 if (iod->op == op_debug_table[i].type)
448                         return op_debug_table[i].name;
449         }
450
451         return unk;
452 }
453 #endif