Whamcloud - gitweb
Adding to the source repository. Will be linked in to the makefile after
[fs/lustre-release.git] / lustre / obdclass / lprocfs_snmp.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  Copyright (C) 2002 Cluster File Systems, Inc.
5  *
6  *   This file is part of Lustre, http://www.lustre.org.
7  *
8  *   Lustre is free software; you can redistribute it and/or
9  *   modify it under the terms of version 2 of the GNU General Public
10  *   License as published by the Free Software Foundation.
11  *
12  *   Lustre 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 Lustre; if not, write to the Free Software
19  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  *
21  */
22
23 #define EXPORT_SYMTAB
24 #include <linux/config.h>
25 #include <linux/module.h>
26 #include <linux/version.h>
27 #include <linux/proc_fs.h>
28 #include <linux/slab.h>
29 #include <linux/types.h>
30
31 #define DEBUG_SUBSYSTEM S_CLASS
32 #define MAX_STRING_SIZE 100
33
34 #include <linux/obd_support.h>
35 #include <linux/obd_class.h>
36 #include <linux/lprocfs.h>
37 #include <linux/string.h>
38 #include <linux/lustre_lib.h>
39
40 #ifdef LPROCFS_EXISTS
41
42 #define DEFAULT_MODE 0644
43 /*
44  * Tokenizer array. Change this array to include special
45  * characters for string tokenizing
46  */
47 char tok[] = {'/', (char)0};
48
49
50
51 /*
52  * Externs
53  */
54 extern struct proc_dir_entry proc_root; /* Defined in proc/root.c */
55 extern struct obd_type *class_nm_to_type(char *nm);
56
57 /*
58  * Globals
59  */
60 struct proc_dir_entry *proc_lustre_root = 0;
61 struct proc_dir_entry *proc_lustre_dev_root = 0;
62
63 /*
64  *  API implementations
65  */
66 /*
67  * lprocfs_reg_obd: Registers an instance of the OBD device in the
68  *                  proc hierarchy
69  */
70
71 int lprocfs_reg_obd(struct obd_device* device, 
72                     lprocfs_vars_t* list, 
73                     void* data)
74 {
75         
76         int retval = 0;
77         struct proc_dir_entry* this_dev_root=0;
78         
79
80         /* Obtain this device root */
81         this_dev_root = lprocfs_mkinitdir(device);
82         if (this_dev_root == 0) {
83                 CERROR("Could not create initial directory");
84                 return LPROCFS_FAILURE;
85         }
86         
87         device->obd_proc_entry=this_dev_root;
88         retval=lprocfs_new_vars(device,             \
89                                 this_dev_root, list, \
90                                 (const char*)tok, 
91                                 data);
92                 
93         return retval;
94 }
95
96 int lprocfs_add_new_vars(struct obd_device* device,
97                          lprocfs_vars_t* var, 
98                          void* data)
99 {
100         int retval=0;
101         if(!device) {
102                 CERROR("Null pointer passed!");
103                 return LPROCFS_FAILURE;
104         }
105         if(!(device->obd_proc_entry)){
106                 CDEBUG(D_OTHER, \
107                        "Device instance not registered yet!!");
108                 return LPROCFS_FAILURE;
109       
110         }
111         retval=lprocfs_new_vars(device,
112                                 device->obd_proc_entry, \
113                                 var, (const char*) tok, data);
114         return retval;
115 }
116
117 int lprocfs_dereg_obd(struct obd_device* device)
118 {
119         struct proc_dir_entry* parent;
120
121         CDEBUG(D_OTHER, "LPROCFS removing device = %s\n", \
122                device->obd_name);
123
124         if (!device) {
125                 CDEBUG(D_OTHER, "! LProcfs:  Null pointer !\n");
126                 return LPROCFS_SUCCESS;
127         }
128
129         if (!(device->obd_name)) {
130                 CERROR(" !! Device does not have a name !! \n");
131                 return LPROCFS_FAILURE;
132         }
133         if(!(device->obd_proc_entry)){
134                 CERROR("This device has not been registered\n");
135                 return LPROCFS_FAILURE;
136         }
137         parent=device->obd_proc_entry->parent;
138         lprocfs_remove_all(device->obd_proc_entry);
139
140         /*
141          * Free the memory held by counters
142          */
143         if (device->counters)
144                 OBD_FREE(device->counters, device->cntr_mem_size);
145
146         
147         while((!(parent->subdir) && \
148                memcmp(parent, &proc_root, sizeof(*parent)))) {
149                 remove_proc_entry(parent->name, parent->parent);
150                 parent=parent->parent;
151         }
152
153         CDEBUG(D_OTHER, "LPROCFS removed device = %s\n", \
154                device->obd_name);
155
156         return LPROCFS_SUCCESS;
157 }
158 struct proc_dir_entry* lprocfs_mkinitdir(struct obd_device* device)
159 {
160         struct proc_dir_entry* this_dev_root = 0;
161         struct proc_dir_entry* temp_proc = 0;
162
163         /*
164          * First check if /proc/lustre exits. If it does not,
165          * instantiate the same and the devices directory
166          */
167         if (proc_lustre_root==0) {
168                 proc_lustre_root = lprocfs_mkdir("lustre", &proc_root);
169                 if (!proc_lustre_root) {
170                         CERROR(" !! Cannot create /proc/lustre !! \n");
171                         return 0;
172                 }
173         }
174         if (proc_lustre_dev_root==0) {
175                 proc_lustre_dev_root =
176                         lprocfs_mkdir("devices", proc_lustre_root);
177                 
178                 if (!proc_lustre_dev_root) {
179                         CERROR(" !! Cannot create /proc/lustre/devices !! \n");
180                         return 0;
181                 }
182                 
183         }
184
185         /*
186          * Check if this is the first instance for a device of
187          * this class in the lprocfs hierarchy.
188          */
189         temp_proc = lprocfs_srch(proc_lustre_dev_root,
190                                  device->obd_type->typ_name);
191
192         if (!temp_proc) {
193                 temp_proc = lprocfs_mkdir(device->obd_type->typ_name,
194                                           proc_lustre_dev_root);
195                 if (!temp_proc) {
196                         CERROR("! Proc dir for device class %s !!\n",
197                                device->obd_type->typ_name);
198                         return 0;
199                 }
200         }
201
202         /* Next create the proc_dir_entry for this instance */
203         this_dev_root = lprocfs_mkdir(device->obd_name, temp_proc);
204         if (!this_dev_root) {
205                 CERROR("!Can't create proc entry for instance %s !! \n",
206                        device->obd_name);
207                 return 0;
208         }
209
210         return this_dev_root;
211 }
212
213 struct proc_dir_entry* lprocfs_mkdir(const char* dname,
214                                      struct proc_dir_entry *parent)
215 {
216         struct proc_dir_entry *child_dir_entry;
217
218         child_dir_entry = proc_mkdir(dname, parent);
219
220         if (!child_dir_entry)
221                 CERROR("lustre: failed to create /proc entry %s\n", dname);
222
223         return child_dir_entry;
224 }
225 struct proc_dir_entry* lprocfs_srch(struct proc_dir_entry* head,
226                                     const char* name)
227 {
228         struct proc_dir_entry* temp;
229         
230         if (!head)
231                 return 0;
232         temp = head->subdir;
233         while (temp != NULL) {
234                 if (!strcmp(temp->name, name))
235                         return temp;
236                 temp = temp->next;
237         }
238         
239         return 0;
240 }
241
242 #warning FIXME: recursive code is VERY bad in the kernel because of stack limit
243 struct proc_dir_entry* lprocfs_bfs_srch(struct proc_dir_entry* root,
244                                         const char* name)
245 {
246         struct proc_dir_entry* temp = root;
247
248         if (!temp)
249                 return 0;
250
251         if (!strcmp(temp->name, name))
252                 return temp;
253
254         if ((temp = lprocfs_bfs_srch(root->next, name)) != 0)
255                 return temp;
256
257         if ((temp = lprocfs_bfs_srch(root->subdir, name)) != 0)
258                 return temp;
259
260         return temp;
261 }
262 void lprocfs_remove_all(struct proc_dir_entry* root)
263 {
264         if (root->subdir != 0)
265                 lprocfs_remove_all(root->subdir);
266
267         if (root->next != 0)
268                 lprocfs_remove_all(root->next);
269
270         if (root->parent != 0)
271                 remove_proc_entry(root->name, root->parent);
272         else
273                 remove_proc_entry(root->name, NULL);
274 }
275 /*
276  * This function will be invoked during a module loading. The path parameter
277  * is relative to /proc/lustre, and hence needs to begin as 
278  * dir_1/dir_2 etc
279  * The list is a simple variable list of names, which will be created under
280  * the "path". If none is specified, no variable entries will be created.
281  * Returns: The root for this module.
282  */
283
284 struct proc_dir_entry* lprocfs_reg_module(char* name, char* path, 
285                                           lprocfs_vars_t* list, 
286                                           void* data)
287 {
288         struct proc_dir_entry* root=0;
289         int retVal=0;
290         if(!name){
291                 CERROR("LProcFS: Null pointer for name\n");
292                 return 0;
293         }
294         if(!path){
295                 CERROR("LProcFS: Insertion path not provided\n");
296                 return 0;
297         }
298         if (proc_lustre_root==0) {
299                 proc_lustre_root = lprocfs_mkdir("lustre", &proc_root);
300                 if (!proc_lustre_root) {
301                         CERROR(" !! Cannot create /proc/lustre !! \n");
302                         return 0;
303                 }
304         }
305         if((root=lprocfs_new_dir(proc_lustre_root, \
306                                  path, (const char*)tok))==0){
307
308                 CERROR("!! LProcFS: Failed to create dirs");
309                 return 0;
310         }
311
312         root=lprocfs_mkdir(name, root);        
313         retVal=lprocfs_new_vars(NULL, root, list, \
314                                 (const char*)tok, data);
315         if(retVal==LPROCFS_FAILURE) 
316                 return 0;
317         return root;
318         
319 }
320
321 int lprocfs_dereg_module(char* name)
322 {
323         struct proc_dir_entry* temp=0;
324         struct proc_dir_entry* parent=0;
325         if (proc_lustre_root==0) {
326                 CERROR(" !! LProc Does not exist !! \n");
327                 return 0;
328         }
329         temp = lprocfs_bfs_srch(proc_lustre_root->subdir, \
330                                 name);
331         if (temp == 0) {
332                 CDEBUG(D_OTHER, "!! Module not inserted!!");
333                 return LPROCFS_FAILURE;
334         }
335         parent=temp->parent;
336         lprocfs_remove_all(temp);
337         while((!(parent->subdir) && \
338                memcmp(parent, &proc_root, sizeof(*parent)))) {
339                 remove_proc_entry(parent->name, parent->parent);
340                 parent=parent->parent;
341         }
342
343         CDEBUG(D_OTHER, "LPROCFS removed = %s\n", \
344                name);
345         
346
347         return LPROCFS_SUCCESS;
348
349 }
350
351 struct proc_dir_entry* lprocfs_new_dir(struct proc_dir_entry* root,
352                                        const char* string, 
353                                        const char* tok)
354 {
355         struct proc_dir_entry* new_root = 0;
356         struct proc_dir_entry* temp_entry = 0;
357         
358         char temp_string[MAX_STRING_SIZE];
359         char* my_str;
360         char* mover_str;
361
362         /*
363          * Remove trailing escaping character
364          */
365         memset(temp_string, 0, MAX_STRING_SIZE);
366         if (strlen(string) >= MAX_STRING_SIZE) {
367                 CDEBUG(D_OTHER, "Directory namespace too long");
368                 return 0;
369         }
370        
371         strcpy(temp_string, string);
372         temp_string[strlen(string) + 1] = '\0';
373         
374         new_root=root;
375         mover_str=temp_string;
376         while ((my_str = strsep(&mover_str, tok))) {
377                 if(!*my_str)
378                         continue;
379                 CDEBUG(D_OTHER, "SEARCH= %s\t, ROOT=%s\n", \
380                        my_str, new_root->name);
381                 temp_entry = lprocfs_srch(new_root, my_str);
382                 if (temp_entry == 0) {
383                         CDEBUG(D_OTHER, "Adding: %s\n", my_str);
384                         temp_entry = lprocfs_mkdir(my_str, new_root);
385                         if (temp_entry == 0) {
386                                 CDEBUG(D_OTHER, 
387                                        "! Did not create new dir %s !!\n",
388                                        my_str);
389                                 return 0;
390                         }
391                 }
392                 new_root = temp_entry;
393         }
394
395         return new_root;
396 }
397
398
399 int lprocfs_add_mod_vars(struct proc_dir_entry* root, 
400                          lprocfs_vars_t* list,
401                          void* data)
402 {
403         int retval=0;
404         retval=lprocfs_new_vars(NULL, root, list, \
405                                 (const char*) tok, data);
406         return retval;
407 }
408
409 int lprocfs_new_vars(struct obd_device* dev, 
410                      struct proc_dir_entry* root,
411                      lprocfs_vars_t* list,
412                      const char* tok, 
413                      void* data)
414 {
415         struct proc_dir_entry* temp_root=0;
416         struct proc_dir_entry* new_leaf=0;
417         struct proc_dir_entry* new_parent=0;
418         char temp_string[MAX_STRING_SIZE];
419         
420         if(!list)
421                 return LPROCFS_SUCCESS;
422
423         while(list->name){
424                 temp_root=lprocfs_new_dir(root, \
425                                           list->name, \
426                                           tok);
427                 
428                 if(!temp_root){
429                         CDEBUG(D_OTHER, "!LProcFS: Mods: No root!");
430                         return LPROCFS_FAILURE;
431                 }
432                 /* Convert the last element into a leaf-node */
433                 memset(temp_string, 0, MAX_STRING_SIZE);
434                 strcpy(temp_string, temp_root->name);
435                 temp_string[strlen(temp_root->name) + 1] = '\0';
436                 new_parent=temp_root->parent;
437                 if (new_parent != 0){
438                         remove_proc_entry(temp_root->name, new_parent);
439                 } else {
440                         remove_proc_entry(temp_root->name, NULL);
441                 }
442                 new_leaf = create_proc_entry(temp_string, \
443                                              DEFAULT_MODE, \
444                                              new_parent);
445                 new_leaf->read_proc = list->read_fptr;
446                 new_leaf->write_proc = list->write_fptr;
447                 new_leaf->data=data;
448                 if(dev)
449                         new_leaf->data=dev;
450                 list++;
451         }
452         return LPROCFS_SUCCESS;
453
454 }
455 int lprocfs_ll_rd(char *page, char **start, off_t off,
456                   int count, int *eof, void *data)
457 {
458         __u64 *temp = (__u64 *)data;
459         int len;
460
461         len = snprintf(page, count, LPU64"\n", *temp);
462
463         return len;
464 }
465
466 #endif /* LPROCFS_EXISTS */
467
468
469 EXPORT_SYMBOL(lprocfs_reg_obd);
470 EXPORT_SYMBOL(lprocfs_dereg_obd);
471 EXPORT_SYMBOL(lprocfs_add_new_vars);
472 EXPORT_SYMBOL(lprocfs_reg_module);
473 EXPORT_SYMBOL(lprocfs_dereg_module);
474 EXPORT_SYMBOL(lprocfs_ll_rd);
475 EXPORT_SYMBOL(lprocfs_add_mod_vars);
476