9c2dd0c6d6f29d6e6df49e4c1e58da500cfe8927
[linux-2.6-block.git] / drivers / staging / unisys / visorutil / procobjecttree.c
1 /* procobjecttree.c
2  *
3  * Copyright © 2010 - 2013 UNISYS CORPORATION
4  * All rights reserved.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or (at
9  * your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
14  * NON INFRINGEMENT.  See the GNU General Public License for more
15  * details.
16  */
17
18 #include "procobjecttree.h"
19
20 #define MYDRVNAME "procobjecttree"
21
22
23
24 /** This is context info that we stash in each /proc file entry, which we
25  *  need in order to call the callback function that supplies the /proc read
26  *  info for that file.
27  */
28 typedef struct {
29         void (*show_property)(struct seq_file *, void *, int);
30         MYPROCOBJECT *procObject;
31         int propertyIndex;
32
33 } PROCDIRENTRYCONTEXT;
34
35 /** This describes the attributes of a tree rooted at
36  *  <procDirRoot>/<name[0]>/<name[1]>/...
37  *  Properties for each object of this type will be located under
38  *  <procDirRoot>/<name[0]>/<name[1]>/.../<objectName>/<propertyName>.
39  */
40 struct MYPROCTYPE_Tag {
41         const char **name;  /**< node names for this type, ending with NULL */
42         int nNames;         /**< num of node names in <name> */
43
44         /** root dir for this type tree in /proc */
45         struct proc_dir_entry *procDirRoot;
46
47         struct proc_dir_entry **procDirs;  /**< for each node in <name> */
48
49         /** bottom dir where objects will be rooted; i.e., this is
50          *  <procDirRoot>/<name[0]>/<name[1]>/.../, which is the same as the
51          *  last entry in the <procDirs> array. */
52         struct proc_dir_entry *procDir;
53
54         /** name for each property that objects of this type can have */
55         const char **propertyNames;
56
57         int nProperties;       /**< num of names in <propertyNames> */
58
59         /** Call this, passing MYPROCOBJECT.context and the property index
60          *  whenever someone reads the proc entry */
61         void (*show_property)(struct seq_file *, void *, int);
62 };
63
64
65
66 struct MYPROCOBJECT_Tag {
67         MYPROCTYPE *type;
68
69         /** This is the name of the dir node in /proc under which the
70          *  properties of this object will appear as files. */
71         char *name;
72
73         int namesize;   /**< number of bytes allocated for name */
74         void *context;  /**< passed to MYPROCTYPE.show_property */
75
76         /** <type.procDirRoot>/<type.name[0]>/<type.name[1]>/.../<name> */
77         struct proc_dir_entry *procDir;
78
79         /** a proc dir entry for each of the properties of the object;
80          *  properties are identified in MYPROCTYPE.propertyNames, so each of
81          *  the <procDirProperties> describes a single file like
82          *  <type.procDirRoot>/<type.name[0]>/<type.name[1]>/...
83          *           /<name>/<propertyName>
84          */
85         struct proc_dir_entry **procDirProperties;
86
87         /** this is a holding area for the context information that is needed
88          *  to run the /proc callback function */
89         PROCDIRENTRYCONTEXT *procDirPropertyContexts;
90 };
91
92
93
94 static struct proc_dir_entry *
95 createProcDir(const char *name, struct proc_dir_entry *parent)
96 {
97         struct proc_dir_entry *p = proc_mkdir_mode(name, S_IFDIR, parent);
98         if (p == NULL)
99                 ERRDRV("failed to create /proc directory %s", name);
100         return p;
101 }
102
103 static struct proc_dir_entry *
104 createProcFile(const char *name, struct proc_dir_entry *parent,
105                const struct file_operations *fops, void *data)
106 {
107         struct proc_dir_entry *p = proc_create_data(name, 0, parent,
108                                                     fops, data);
109         if (p == NULL)
110                 ERRDRV("failed to create /proc file %s", name);
111         return p;
112 }
113
114 static int seq_show(struct seq_file *seq, void *offset);
115 static int proc_open(struct inode *inode, struct file *file)
116 {
117         return single_open(file, seq_show, PDE_DATA(inode));
118 }
119
120 static const struct file_operations proc_fops = {
121         .open = proc_open,
122         .read = seq_read,
123         .llseek = seq_lseek,
124         .release = single_release,
125 };
126
127
128
129 MYPROCTYPE *visor_proc_CreateType(struct proc_dir_entry *procDirRoot,
130                                   const char **name,
131                                   const char **propertyNames,
132                                   void (*show_property)(struct seq_file *,
133                                                         void *, int))
134 {
135         int i = 0;
136         MYPROCTYPE *rc = NULL, *type = NULL;
137         struct proc_dir_entry *parent = NULL;
138
139         if (procDirRoot == NULL)
140                 FAIL("procDirRoot cannot be NULL!", 0);
141         if (name == NULL || name[0] == NULL)
142                 FAIL("name must contain at least 1 node name!", 0);
143         type = kzalloc(sizeof(MYPROCTYPE), GFP_KERNEL | __GFP_NORETRY);
144         if (type == NULL)
145                 FAIL("out of memory", 0);
146         type->name = name;
147         type->propertyNames = propertyNames;
148         type->nProperties = 0;
149         type->nNames = 0;
150         type->show_property = show_property;
151         type->procDirRoot = procDirRoot;
152         if (type->propertyNames != 0)
153                 while (type->propertyNames[type->nProperties] != NULL)
154                         type->nProperties++;
155         while (type->name[type->nNames] != NULL)
156                 type->nNames++;
157         type->procDirs = kzalloc((type->nNames + 1) *
158                                  sizeof(struct proc_dir_entry *),
159                                  GFP_KERNEL | __GFP_NORETRY);
160         if (type->procDirs == NULL)
161                 FAIL("out of memory", 0);
162         parent = procDirRoot;
163         for (i = 0; i < type->nNames; i++) {
164                 type->procDirs[i] = createProcDir(type->name[i], parent);
165                 if (type->procDirs[i] == NULL)
166                         RETPTR(NULL);
167                 parent = type->procDirs[i];
168         }
169         type->procDir = type->procDirs[type->nNames-1];
170         RETPTR(type);
171 Away:
172         if (rc == NULL) {
173                 if (type != NULL) {
174                         visor_proc_DestroyType(type);
175                         type = NULL;
176                 }
177         }
178         return rc;
179 }
180 EXPORT_SYMBOL_GPL(visor_proc_CreateType);
181
182
183
184 void visor_proc_DestroyType(MYPROCTYPE *type)
185 {
186         if (type == NULL)
187                 return;
188         if (type->procDirs != NULL) {
189                 int i = type->nNames-1;
190                 while (i >= 0) {
191                         if (type->procDirs[i] != NULL) {
192                                 struct proc_dir_entry *parent = NULL;
193                                 if (i == 0)
194                                         parent = type->procDirRoot;
195                                 else
196                                         parent = type->procDirs[i-1];
197                                 remove_proc_entry(type->name[i], parent);
198                         }
199                         i--;
200                 }
201                 kfree(type->procDirs);
202                 type->procDirs = NULL;
203         }
204         kfree(type);
205 }
206 EXPORT_SYMBOL_GPL(visor_proc_DestroyType);
207
208
209
210 MYPROCOBJECT *visor_proc_CreateObject(MYPROCTYPE *type,
211                                       const char *name, void *context)
212 {
213         MYPROCOBJECT *obj = NULL, *rc = NULL;
214         int i = 0;
215
216         if (type == NULL)
217                 FAIL("type cannot be NULL", 0);
218         obj = kzalloc(sizeof(MYPROCOBJECT), GFP_KERNEL | __GFP_NORETRY);
219         if (obj == NULL)
220                 FAIL("out of memory", 0);
221         obj->type = type;
222         obj->context = context;
223         if (name == NULL) {
224                 obj->name = NULL;
225                 obj->procDir = type->procDir;
226         } else {
227                 obj->namesize = strlen(name)+1;
228                 obj->name = kmalloc(obj->namesize, GFP_KERNEL | __GFP_NORETRY);
229                 if (obj->name == NULL) {
230                         obj->namesize = 0;
231                         FAIL("out of memory", 0);
232                 }
233                 strcpy(obj->name, name);
234                 obj->procDir = createProcDir(obj->name, type->procDir);
235                 if (obj->procDir == NULL)
236                         RETPTR(NULL);
237         }
238         obj->procDirPropertyContexts =
239                 kzalloc((type->nProperties + 1) * sizeof(PROCDIRENTRYCONTEXT),
240                         GFP_KERNEL | __GFP_NORETRY);
241         if (obj->procDirPropertyContexts == NULL)
242                 FAIL("out of memory", 0);
243         obj->procDirProperties =
244                 kzalloc((type->nProperties + 1) * sizeof(struct proc_dir_entry *),
245                         GFP_KERNEL | __GFP_NORETRY);
246         if (obj->procDirProperties == NULL)
247                 FAIL("out of memory", 0);
248         for (i = 0; i < type->nProperties; i++) {
249                 obj->procDirPropertyContexts[i].procObject = obj;
250                 obj->procDirPropertyContexts[i].propertyIndex = i;
251                 obj->procDirPropertyContexts[i].show_property =
252                         type->show_property;
253                 if (type->propertyNames[i][0] != '\0') {
254                         /* only create properties that have names */
255                         obj->procDirProperties[i] =
256                                 createProcFile(type->propertyNames[i],
257                                                obj->procDir, &proc_fops,
258                                                &obj->procDirPropertyContexts[i]);
259                         if (obj->procDirProperties[i] == NULL)
260                                 RETPTR(NULL);
261                 }
262         }
263         RETPTR(obj);
264 Away:
265         if (rc == NULL) {
266                 if (obj != NULL) {
267                         visor_proc_DestroyObject(obj);
268                         obj = NULL;
269                 }
270         }
271         return rc;
272 }
273 EXPORT_SYMBOL_GPL(visor_proc_CreateObject);
274
275
276
277 void visor_proc_DestroyObject(MYPROCOBJECT *obj)
278 {
279         MYPROCTYPE *type = NULL;
280         if (obj == NULL)
281                 return;
282         type = obj->type;
283         if (type == NULL)
284                 return;
285         if (obj->procDirProperties != NULL) {
286                 int i = 0;
287                 for (i = 0; i < type->nProperties; i++) {
288                         if (obj->procDirProperties[i] != NULL) {
289                                 remove_proc_entry(type->propertyNames[i],
290                                                   obj->procDir);
291                                 obj->procDirProperties[i] = NULL;
292                         }
293                 }
294                 kfree(obj->procDirProperties);
295                 obj->procDirProperties = NULL;
296         }
297         if (obj->procDirPropertyContexts != NULL) {
298                 kfree(obj->procDirPropertyContexts);
299                 obj->procDirPropertyContexts = NULL;
300         }
301         if (obj->procDir != NULL) {
302                 if (obj->name != NULL)
303                         remove_proc_entry(obj->name, type->procDir);
304                 obj->procDir = NULL;
305         }
306         if (obj->name != NULL) {
307                 kfree(obj->name);
308                 obj->name = NULL;
309         }
310         kfree(obj);
311 }
312 EXPORT_SYMBOL_GPL(visor_proc_DestroyObject);
313
314
315
316 static int seq_show(struct seq_file *seq, void *offset)
317 {
318         PROCDIRENTRYCONTEXT *ctx = (PROCDIRENTRYCONTEXT *)(seq->private);
319         if (ctx == NULL) {
320                 ERRDRV("I don't have a freakin' clue...");
321                 return 0;
322         }
323         (*ctx->show_property)(seq, ctx->procObject->context,
324                               ctx->propertyIndex);
325         return 0;
326 }