Loading...
1/*
2 * System Trace Module (STM) master/channel allocation policy management
3 * Copyright (c) 2014, Intel Corporation.
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * A master/channel allocation policy allows mapping string identifiers to
15 * master and channel ranges, where allocation can be done.
16 */
17
18#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
19
20#include <linux/types.h>
21#include <linux/module.h>
22#include <linux/device.h>
23#include <linux/configfs.h>
24#include <linux/slab.h>
25#include <linux/stm.h>
26#include "stm.h"
27
28/*
29 * STP Master/Channel allocation policy configfs layout.
30 */
31
32struct stp_policy {
33 struct config_group group;
34 struct stm_device *stm;
35};
36
37struct stp_policy_node {
38 struct config_group group;
39 struct stp_policy *policy;
40 unsigned int first_master;
41 unsigned int last_master;
42 unsigned int first_channel;
43 unsigned int last_channel;
44};
45
46static struct configfs_subsystem stp_policy_subsys;
47
48void stp_policy_node_get_ranges(struct stp_policy_node *policy_node,
49 unsigned int *mstart, unsigned int *mend,
50 unsigned int *cstart, unsigned int *cend)
51{
52 *mstart = policy_node->first_master;
53 *mend = policy_node->last_master;
54 *cstart = policy_node->first_channel;
55 *cend = policy_node->last_channel;
56}
57
58static inline char *stp_policy_node_name(struct stp_policy_node *policy_node)
59{
60 return policy_node->group.cg_item.ci_name ? : "<none>";
61}
62
63static inline struct stp_policy *to_stp_policy(struct config_item *item)
64{
65 return item ?
66 container_of(to_config_group(item), struct stp_policy, group) :
67 NULL;
68}
69
70static inline struct stp_policy_node *
71to_stp_policy_node(struct config_item *item)
72{
73 return item ?
74 container_of(to_config_group(item), struct stp_policy_node,
75 group) :
76 NULL;
77}
78
79static ssize_t
80stp_policy_node_masters_show(struct config_item *item, char *page)
81{
82 struct stp_policy_node *policy_node = to_stp_policy_node(item);
83 ssize_t count;
84
85 count = sprintf(page, "%u %u\n", policy_node->first_master,
86 policy_node->last_master);
87
88 return count;
89}
90
91static ssize_t
92stp_policy_node_masters_store(struct config_item *item, const char *page,
93 size_t count)
94{
95 struct stp_policy_node *policy_node = to_stp_policy_node(item);
96 unsigned int first, last;
97 struct stm_device *stm;
98 char *p = (char *)page;
99 ssize_t ret = -ENODEV;
100
101 if (sscanf(p, "%u %u", &first, &last) != 2)
102 return -EINVAL;
103
104 mutex_lock(&stp_policy_subsys.su_mutex);
105 stm = policy_node->policy->stm;
106 if (!stm)
107 goto unlock;
108
109 /* must be within [sw_start..sw_end], which is an inclusive range */
110 if (first > INT_MAX || last > INT_MAX || first > last ||
111 first < stm->data->sw_start ||
112 last > stm->data->sw_end) {
113 ret = -ERANGE;
114 goto unlock;
115 }
116
117 ret = count;
118 policy_node->first_master = first;
119 policy_node->last_master = last;
120
121unlock:
122 mutex_unlock(&stp_policy_subsys.su_mutex);
123
124 return ret;
125}
126
127static ssize_t
128stp_policy_node_channels_show(struct config_item *item, char *page)
129{
130 struct stp_policy_node *policy_node = to_stp_policy_node(item);
131 ssize_t count;
132
133 count = sprintf(page, "%u %u\n", policy_node->first_channel,
134 policy_node->last_channel);
135
136 return count;
137}
138
139static ssize_t
140stp_policy_node_channels_store(struct config_item *item, const char *page,
141 size_t count)
142{
143 struct stp_policy_node *policy_node = to_stp_policy_node(item);
144 unsigned int first, last;
145 struct stm_device *stm;
146 char *p = (char *)page;
147 ssize_t ret = -ENODEV;
148
149 if (sscanf(p, "%u %u", &first, &last) != 2)
150 return -EINVAL;
151
152 mutex_lock(&stp_policy_subsys.su_mutex);
153 stm = policy_node->policy->stm;
154 if (!stm)
155 goto unlock;
156
157 if (first > INT_MAX || last > INT_MAX || first > last ||
158 last >= stm->data->sw_nchannels) {
159 ret = -ERANGE;
160 goto unlock;
161 }
162
163 ret = count;
164 policy_node->first_channel = first;
165 policy_node->last_channel = last;
166
167unlock:
168 mutex_unlock(&stp_policy_subsys.su_mutex);
169
170 return ret;
171}
172
173static void stp_policy_node_release(struct config_item *item)
174{
175 kfree(to_stp_policy_node(item));
176}
177
178static struct configfs_item_operations stp_policy_node_item_ops = {
179 .release = stp_policy_node_release,
180};
181
182CONFIGFS_ATTR(stp_policy_node_, masters);
183CONFIGFS_ATTR(stp_policy_node_, channels);
184
185static struct configfs_attribute *stp_policy_node_attrs[] = {
186 &stp_policy_node_attr_masters,
187 &stp_policy_node_attr_channels,
188 NULL,
189};
190
191static struct config_item_type stp_policy_type;
192static struct config_item_type stp_policy_node_type;
193
194static struct config_group *
195stp_policy_node_make(struct config_group *group, const char *name)
196{
197 struct stp_policy_node *policy_node, *parent_node;
198 struct stp_policy *policy;
199
200 if (group->cg_item.ci_type == &stp_policy_type) {
201 policy = container_of(group, struct stp_policy, group);
202 } else {
203 parent_node = container_of(group, struct stp_policy_node,
204 group);
205 policy = parent_node->policy;
206 }
207
208 if (!policy->stm)
209 return ERR_PTR(-ENODEV);
210
211 policy_node = kzalloc(sizeof(struct stp_policy_node), GFP_KERNEL);
212 if (!policy_node)
213 return ERR_PTR(-ENOMEM);
214
215 config_group_init_type_name(&policy_node->group, name,
216 &stp_policy_node_type);
217
218 policy_node->policy = policy;
219
220 /* default values for the attributes */
221 policy_node->first_master = policy->stm->data->sw_start;
222 policy_node->last_master = policy->stm->data->sw_end;
223 policy_node->first_channel = 0;
224 policy_node->last_channel = policy->stm->data->sw_nchannels - 1;
225
226 return &policy_node->group;
227}
228
229static void
230stp_policy_node_drop(struct config_group *group, struct config_item *item)
231{
232 config_item_put(item);
233}
234
235static struct configfs_group_operations stp_policy_node_group_ops = {
236 .make_group = stp_policy_node_make,
237 .drop_item = stp_policy_node_drop,
238};
239
240static struct config_item_type stp_policy_node_type = {
241 .ct_item_ops = &stp_policy_node_item_ops,
242 .ct_group_ops = &stp_policy_node_group_ops,
243 .ct_attrs = stp_policy_node_attrs,
244 .ct_owner = THIS_MODULE,
245};
246
247/*
248 * Root group: policies.
249 */
250static ssize_t stp_policy_device_show(struct config_item *item,
251 char *page)
252{
253 struct stp_policy *policy = to_stp_policy(item);
254 ssize_t count;
255
256 count = sprintf(page, "%s\n",
257 (policy && policy->stm) ?
258 policy->stm->data->name :
259 "<none>");
260
261 return count;
262}
263
264CONFIGFS_ATTR_RO(stp_policy_, device);
265
266static struct configfs_attribute *stp_policy_attrs[] = {
267 &stp_policy_attr_device,
268 NULL,
269};
270
271void stp_policy_unbind(struct stp_policy *policy)
272{
273 struct stm_device *stm = policy->stm;
274
275 /*
276 * stp_policy_release() will not call here if the policy is already
277 * unbound; other users should not either, as no link exists between
278 * this policy and anything else in that case
279 */
280 if (WARN_ON_ONCE(!policy->stm))
281 return;
282
283 lockdep_assert_held(&stm->policy_mutex);
284
285 stm->policy = NULL;
286 policy->stm = NULL;
287
288 stm_put_device(stm);
289}
290
291static void stp_policy_release(struct config_item *item)
292{
293 struct stp_policy *policy = to_stp_policy(item);
294 struct stm_device *stm = policy->stm;
295
296 /* a policy *can* be unbound and still exist in configfs tree */
297 if (!stm)
298 return;
299
300 mutex_lock(&stm->policy_mutex);
301 stp_policy_unbind(policy);
302 mutex_unlock(&stm->policy_mutex);
303
304 kfree(policy);
305}
306
307static struct configfs_item_operations stp_policy_item_ops = {
308 .release = stp_policy_release,
309};
310
311static struct configfs_group_operations stp_policy_group_ops = {
312 .make_group = stp_policy_node_make,
313};
314
315static struct config_item_type stp_policy_type = {
316 .ct_item_ops = &stp_policy_item_ops,
317 .ct_group_ops = &stp_policy_group_ops,
318 .ct_attrs = stp_policy_attrs,
319 .ct_owner = THIS_MODULE,
320};
321
322static struct config_group *
323stp_policies_make(struct config_group *group, const char *name)
324{
325 struct config_group *ret;
326 struct stm_device *stm;
327 char *devname, *p;
328
329 devname = kasprintf(GFP_KERNEL, "%s", name);
330 if (!devname)
331 return ERR_PTR(-ENOMEM);
332
333 /*
334 * node must look like <device_name>.<policy_name>, where
335 * <device_name> is the name of an existing stm device; may
336 * contain dots;
337 * <policy_name> is an arbitrary string; may not contain dots
338 */
339 p = strrchr(devname, '.');
340 if (!p) {
341 kfree(devname);
342 return ERR_PTR(-EINVAL);
343 }
344
345 *p++ = '\0';
346
347 stm = stm_find_device(devname);
348 kfree(devname);
349
350 if (!stm)
351 return ERR_PTR(-ENODEV);
352
353 mutex_lock(&stm->policy_mutex);
354 if (stm->policy) {
355 ret = ERR_PTR(-EBUSY);
356 goto unlock_policy;
357 }
358
359 stm->policy = kzalloc(sizeof(*stm->policy), GFP_KERNEL);
360 if (!stm->policy) {
361 ret = ERR_PTR(-ENOMEM);
362 goto unlock_policy;
363 }
364
365 config_group_init_type_name(&stm->policy->group, name,
366 &stp_policy_type);
367 stm->policy->stm = stm;
368
369 ret = &stm->policy->group;
370
371unlock_policy:
372 mutex_unlock(&stm->policy_mutex);
373
374 if (IS_ERR(ret))
375 stm_put_device(stm);
376
377 return ret;
378}
379
380static struct configfs_group_operations stp_policies_group_ops = {
381 .make_group = stp_policies_make,
382};
383
384static struct config_item_type stp_policies_type = {
385 .ct_group_ops = &stp_policies_group_ops,
386 .ct_owner = THIS_MODULE,
387};
388
389static struct configfs_subsystem stp_policy_subsys = {
390 .su_group = {
391 .cg_item = {
392 .ci_namebuf = "stp-policy",
393 .ci_type = &stp_policies_type,
394 },
395 },
396};
397
398/*
399 * Lock the policy mutex from the outside
400 */
401static struct stp_policy_node *
402__stp_policy_node_lookup(struct stp_policy *policy, char *s)
403{
404 struct stp_policy_node *policy_node, *ret;
405 struct list_head *head = &policy->group.cg_children;
406 struct config_item *item;
407 char *start, *end = s;
408
409 if (list_empty(head))
410 return NULL;
411
412 /* return the first entry if everything else fails */
413 item = list_entry(head->next, struct config_item, ci_entry);
414 ret = to_stp_policy_node(item);
415
416next:
417 for (;;) {
418 start = strsep(&end, "/");
419 if (!start)
420 break;
421
422 if (!*start)
423 continue;
424
425 list_for_each_entry(item, head, ci_entry) {
426 policy_node = to_stp_policy_node(item);
427
428 if (!strcmp(start,
429 policy_node->group.cg_item.ci_name)) {
430 ret = policy_node;
431
432 if (!end)
433 goto out;
434
435 head = &policy_node->group.cg_children;
436 goto next;
437 }
438 }
439 break;
440 }
441
442out:
443 return ret;
444}
445
446
447struct stp_policy_node *
448stp_policy_node_lookup(struct stm_device *stm, char *s)
449{
450 struct stp_policy_node *policy_node = NULL;
451
452 mutex_lock(&stp_policy_subsys.su_mutex);
453
454 mutex_lock(&stm->policy_mutex);
455 if (stm->policy)
456 policy_node = __stp_policy_node_lookup(stm->policy, s);
457 mutex_unlock(&stm->policy_mutex);
458
459 if (policy_node)
460 config_item_get(&policy_node->group.cg_item);
461 mutex_unlock(&stp_policy_subsys.su_mutex);
462
463 return policy_node;
464}
465
466void stp_policy_node_put(struct stp_policy_node *policy_node)
467{
468 config_item_put(&policy_node->group.cg_item);
469}
470
471int __init stp_configfs_init(void)
472{
473 int err;
474
475 config_group_init(&stp_policy_subsys.su_group);
476 mutex_init(&stp_policy_subsys.su_mutex);
477 err = configfs_register_subsystem(&stp_policy_subsys);
478
479 return err;
480}
481
482void __exit stp_configfs_exit(void)
483{
484 configfs_unregister_subsystem(&stp_policy_subsys);
485}
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * System Trace Module (STM) master/channel allocation policy management
4 * Copyright (c) 2014, Intel Corporation.
5 *
6 * A master/channel allocation policy allows mapping string identifiers to
7 * master and channel ranges, where allocation can be done.
8 */
9
10#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11
12#include <linux/types.h>
13#include <linux/module.h>
14#include <linux/device.h>
15#include <linux/configfs.h>
16#include <linux/slab.h>
17#include <linux/stm.h>
18#include "stm.h"
19
20/*
21 * STP Master/Channel allocation policy configfs layout.
22 */
23
24struct stp_policy {
25 struct config_group group;
26 struct stm_device *stm;
27};
28
29struct stp_policy_node {
30 struct config_group group;
31 struct stp_policy *policy;
32 unsigned int first_master;
33 unsigned int last_master;
34 unsigned int first_channel;
35 unsigned int last_channel;
36 /* this is the one that's exposed to the attributes */
37 unsigned char priv[0];
38};
39
40void *stp_policy_node_priv(struct stp_policy_node *pn)
41{
42 if (!pn)
43 return NULL;
44
45 return pn->priv;
46}
47
48static struct configfs_subsystem stp_policy_subsys;
49
50void stp_policy_node_get_ranges(struct stp_policy_node *policy_node,
51 unsigned int *mstart, unsigned int *mend,
52 unsigned int *cstart, unsigned int *cend)
53{
54 *mstart = policy_node->first_master;
55 *mend = policy_node->last_master;
56 *cstart = policy_node->first_channel;
57 *cend = policy_node->last_channel;
58}
59
60static inline char *stp_policy_node_name(struct stp_policy_node *policy_node)
61{
62 return policy_node->group.cg_item.ci_name ? : "<none>";
63}
64
65static inline struct stp_policy *to_stp_policy(struct config_item *item)
66{
67 return item ?
68 container_of(to_config_group(item), struct stp_policy, group) :
69 NULL;
70}
71
72static inline struct stp_policy_node *
73to_stp_policy_node(struct config_item *item)
74{
75 return item ?
76 container_of(to_config_group(item), struct stp_policy_node,
77 group) :
78 NULL;
79}
80
81void *to_pdrv_policy_node(struct config_item *item)
82{
83 struct stp_policy_node *node = to_stp_policy_node(item);
84
85 return stp_policy_node_priv(node);
86}
87EXPORT_SYMBOL_GPL(to_pdrv_policy_node);
88
89static ssize_t
90stp_policy_node_masters_show(struct config_item *item, char *page)
91{
92 struct stp_policy_node *policy_node = to_stp_policy_node(item);
93 ssize_t count;
94
95 count = sprintf(page, "%u %u\n", policy_node->first_master,
96 policy_node->last_master);
97
98 return count;
99}
100
101static ssize_t
102stp_policy_node_masters_store(struct config_item *item, const char *page,
103 size_t count)
104{
105 struct stp_policy_node *policy_node = to_stp_policy_node(item);
106 unsigned int first, last;
107 struct stm_device *stm;
108 char *p = (char *)page;
109 ssize_t ret = -ENODEV;
110
111 if (sscanf(p, "%u %u", &first, &last) != 2)
112 return -EINVAL;
113
114 mutex_lock(&stp_policy_subsys.su_mutex);
115 stm = policy_node->policy->stm;
116 if (!stm)
117 goto unlock;
118
119 /* must be within [sw_start..sw_end], which is an inclusive range */
120 if (first > last || first < stm->data->sw_start ||
121 last > stm->data->sw_end) {
122 ret = -ERANGE;
123 goto unlock;
124 }
125
126 ret = count;
127 policy_node->first_master = first;
128 policy_node->last_master = last;
129
130unlock:
131 mutex_unlock(&stp_policy_subsys.su_mutex);
132
133 return ret;
134}
135
136static ssize_t
137stp_policy_node_channels_show(struct config_item *item, char *page)
138{
139 struct stp_policy_node *policy_node = to_stp_policy_node(item);
140 ssize_t count;
141
142 count = sprintf(page, "%u %u\n", policy_node->first_channel,
143 policy_node->last_channel);
144
145 return count;
146}
147
148static ssize_t
149stp_policy_node_channels_store(struct config_item *item, const char *page,
150 size_t count)
151{
152 struct stp_policy_node *policy_node = to_stp_policy_node(item);
153 unsigned int first, last;
154 struct stm_device *stm;
155 char *p = (char *)page;
156 ssize_t ret = -ENODEV;
157
158 if (sscanf(p, "%u %u", &first, &last) != 2)
159 return -EINVAL;
160
161 mutex_lock(&stp_policy_subsys.su_mutex);
162 stm = policy_node->policy->stm;
163 if (!stm)
164 goto unlock;
165
166 if (first > INT_MAX || last > INT_MAX || first > last ||
167 last >= stm->data->sw_nchannels) {
168 ret = -ERANGE;
169 goto unlock;
170 }
171
172 ret = count;
173 policy_node->first_channel = first;
174 policy_node->last_channel = last;
175
176unlock:
177 mutex_unlock(&stp_policy_subsys.su_mutex);
178
179 return ret;
180}
181
182static void stp_policy_node_release(struct config_item *item)
183{
184 struct stp_policy_node *node = to_stp_policy_node(item);
185
186 kfree(node);
187}
188
189static struct configfs_item_operations stp_policy_node_item_ops = {
190 .release = stp_policy_node_release,
191};
192
193CONFIGFS_ATTR(stp_policy_node_, masters);
194CONFIGFS_ATTR(stp_policy_node_, channels);
195
196static struct configfs_attribute *stp_policy_node_attrs[] = {
197 &stp_policy_node_attr_masters,
198 &stp_policy_node_attr_channels,
199 NULL,
200};
201
202static const struct config_item_type stp_policy_type;
203static const struct config_item_type stp_policy_node_type;
204
205const struct config_item_type *
206get_policy_node_type(struct configfs_attribute **attrs)
207{
208 struct config_item_type *type;
209 struct configfs_attribute **merged;
210
211 type = kmemdup(&stp_policy_node_type, sizeof(stp_policy_node_type),
212 GFP_KERNEL);
213 if (!type)
214 return NULL;
215
216 merged = memcat_p(stp_policy_node_attrs, attrs);
217 if (!merged) {
218 kfree(type);
219 return NULL;
220 }
221
222 type->ct_attrs = merged;
223
224 return type;
225}
226
227static struct config_group *
228stp_policy_node_make(struct config_group *group, const char *name)
229{
230 const struct config_item_type *type = &stp_policy_node_type;
231 struct stp_policy_node *policy_node, *parent_node;
232 const struct stm_protocol_driver *pdrv;
233 struct stp_policy *policy;
234
235 if (group->cg_item.ci_type == &stp_policy_type) {
236 policy = container_of(group, struct stp_policy, group);
237 } else {
238 parent_node = container_of(group, struct stp_policy_node,
239 group);
240 policy = parent_node->policy;
241 }
242
243 if (!policy->stm)
244 return ERR_PTR(-ENODEV);
245
246 pdrv = policy->stm->pdrv;
247 policy_node =
248 kzalloc(offsetof(struct stp_policy_node, priv[pdrv->priv_sz]),
249 GFP_KERNEL);
250 if (!policy_node)
251 return ERR_PTR(-ENOMEM);
252
253 if (pdrv->policy_node_init)
254 pdrv->policy_node_init((void *)policy_node->priv);
255
256 if (policy->stm->pdrv_node_type)
257 type = policy->stm->pdrv_node_type;
258
259 config_group_init_type_name(&policy_node->group, name, type);
260
261 policy_node->policy = policy;
262
263 /* default values for the attributes */
264 policy_node->first_master = policy->stm->data->sw_start;
265 policy_node->last_master = policy->stm->data->sw_end;
266 policy_node->first_channel = 0;
267 policy_node->last_channel = policy->stm->data->sw_nchannels - 1;
268
269 return &policy_node->group;
270}
271
272static void
273stp_policy_node_drop(struct config_group *group, struct config_item *item)
274{
275 config_item_put(item);
276}
277
278static struct configfs_group_operations stp_policy_node_group_ops = {
279 .make_group = stp_policy_node_make,
280 .drop_item = stp_policy_node_drop,
281};
282
283static const struct config_item_type stp_policy_node_type = {
284 .ct_item_ops = &stp_policy_node_item_ops,
285 .ct_group_ops = &stp_policy_node_group_ops,
286 .ct_attrs = stp_policy_node_attrs,
287 .ct_owner = THIS_MODULE,
288};
289
290/*
291 * Root group: policies.
292 */
293static ssize_t stp_policy_device_show(struct config_item *item,
294 char *page)
295{
296 struct stp_policy *policy = to_stp_policy(item);
297 ssize_t count;
298
299 count = sprintf(page, "%s\n",
300 (policy && policy->stm) ?
301 policy->stm->data->name :
302 "<none>");
303
304 return count;
305}
306
307CONFIGFS_ATTR_RO(stp_policy_, device);
308
309static ssize_t stp_policy_protocol_show(struct config_item *item,
310 char *page)
311{
312 struct stp_policy *policy = to_stp_policy(item);
313 ssize_t count;
314
315 count = sprintf(page, "%s\n",
316 (policy && policy->stm) ?
317 policy->stm->pdrv->name :
318 "<none>");
319
320 return count;
321}
322
323CONFIGFS_ATTR_RO(stp_policy_, protocol);
324
325static struct configfs_attribute *stp_policy_attrs[] = {
326 &stp_policy_attr_device,
327 &stp_policy_attr_protocol,
328 NULL,
329};
330
331void stp_policy_unbind(struct stp_policy *policy)
332{
333 struct stm_device *stm = policy->stm;
334
335 /*
336 * stp_policy_release() will not call here if the policy is already
337 * unbound; other users should not either, as no link exists between
338 * this policy and anything else in that case
339 */
340 if (WARN_ON_ONCE(!policy->stm))
341 return;
342
343 lockdep_assert_held(&stm->policy_mutex);
344
345 stm->policy = NULL;
346 policy->stm = NULL;
347
348 stm_put_protocol(stm->pdrv);
349 stm_put_device(stm);
350}
351
352static void stp_policy_release(struct config_item *item)
353{
354 struct stp_policy *policy = to_stp_policy(item);
355 struct stm_device *stm = policy->stm;
356
357 /* a policy *can* be unbound and still exist in configfs tree */
358 if (!stm)
359 return;
360
361 mutex_lock(&stm->policy_mutex);
362 stp_policy_unbind(policy);
363 mutex_unlock(&stm->policy_mutex);
364
365 kfree(policy);
366}
367
368static struct configfs_item_operations stp_policy_item_ops = {
369 .release = stp_policy_release,
370};
371
372static struct configfs_group_operations stp_policy_group_ops = {
373 .make_group = stp_policy_node_make,
374};
375
376static const struct config_item_type stp_policy_type = {
377 .ct_item_ops = &stp_policy_item_ops,
378 .ct_group_ops = &stp_policy_group_ops,
379 .ct_attrs = stp_policy_attrs,
380 .ct_owner = THIS_MODULE,
381};
382
383static struct config_group *
384stp_policy_make(struct config_group *group, const char *name)
385{
386 const struct config_item_type *pdrv_node_type;
387 const struct stm_protocol_driver *pdrv;
388 char *devname, *proto, *p;
389 struct config_group *ret;
390 struct stm_device *stm;
391 int err;
392
393 devname = kasprintf(GFP_KERNEL, "%s", name);
394 if (!devname)
395 return ERR_PTR(-ENOMEM);
396
397 /*
398 * node must look like <device_name>.<policy_name>, where
399 * <device_name> is the name of an existing stm device; may
400 * contain dots;
401 * <policy_name> is an arbitrary string; may not contain dots
402 * <device_name>:<protocol_name>.<policy_name>
403 */
404 p = strrchr(devname, '.');
405 if (!p) {
406 kfree(devname);
407 return ERR_PTR(-EINVAL);
408 }
409
410 *p = '\0';
411
412 /*
413 * look for ":<protocol_name>":
414 * + no protocol suffix: fall back to whatever is available;
415 * + unknown protocol: fail the whole thing
416 */
417 proto = strrchr(devname, ':');
418 if (proto)
419 *proto++ = '\0';
420
421 stm = stm_find_device(devname);
422 if (!stm) {
423 kfree(devname);
424 return ERR_PTR(-ENODEV);
425 }
426
427 err = stm_lookup_protocol(proto, &pdrv, &pdrv_node_type);
428 kfree(devname);
429
430 if (err) {
431 stm_put_device(stm);
432 return ERR_PTR(-ENODEV);
433 }
434
435 mutex_lock(&stm->policy_mutex);
436 if (stm->policy) {
437 ret = ERR_PTR(-EBUSY);
438 goto unlock_policy;
439 }
440
441 stm->policy = kzalloc(sizeof(*stm->policy), GFP_KERNEL);
442 if (!stm->policy) {
443 ret = ERR_PTR(-ENOMEM);
444 goto unlock_policy;
445 }
446
447 config_group_init_type_name(&stm->policy->group, name,
448 &stp_policy_type);
449
450 stm->pdrv = pdrv;
451 stm->pdrv_node_type = pdrv_node_type;
452 stm->policy->stm = stm;
453 ret = &stm->policy->group;
454
455unlock_policy:
456 mutex_unlock(&stm->policy_mutex);
457
458 if (IS_ERR(ret)) {
459 /*
460 * pdrv and stm->pdrv at this point can be quite different,
461 * and only one of them needs to be 'put'
462 */
463 stm_put_protocol(pdrv);
464 stm_put_device(stm);
465 }
466
467 return ret;
468}
469
470static struct configfs_group_operations stp_policy_root_group_ops = {
471 .make_group = stp_policy_make,
472};
473
474static const struct config_item_type stp_policy_root_type = {
475 .ct_group_ops = &stp_policy_root_group_ops,
476 .ct_owner = THIS_MODULE,
477};
478
479static struct configfs_subsystem stp_policy_subsys = {
480 .su_group = {
481 .cg_item = {
482 .ci_namebuf = "stp-policy",
483 .ci_type = &stp_policy_root_type,
484 },
485 },
486};
487
488/*
489 * Lock the policy mutex from the outside
490 */
491static struct stp_policy_node *
492__stp_policy_node_lookup(struct stp_policy *policy, char *s)
493{
494 struct stp_policy_node *policy_node, *ret = NULL;
495 struct list_head *head = &policy->group.cg_children;
496 struct config_item *item;
497 char *start, *end = s;
498
499 if (list_empty(head))
500 return NULL;
501
502next:
503 for (;;) {
504 start = strsep(&end, "/");
505 if (!start)
506 break;
507
508 if (!*start)
509 continue;
510
511 list_for_each_entry(item, head, ci_entry) {
512 policy_node = to_stp_policy_node(item);
513
514 if (!strcmp(start,
515 policy_node->group.cg_item.ci_name)) {
516 ret = policy_node;
517
518 if (!end)
519 goto out;
520
521 head = &policy_node->group.cg_children;
522 goto next;
523 }
524 }
525 break;
526 }
527
528out:
529 return ret;
530}
531
532
533struct stp_policy_node *
534stp_policy_node_lookup(struct stm_device *stm, char *s)
535{
536 struct stp_policy_node *policy_node = NULL;
537
538 mutex_lock(&stp_policy_subsys.su_mutex);
539
540 mutex_lock(&stm->policy_mutex);
541 if (stm->policy)
542 policy_node = __stp_policy_node_lookup(stm->policy, s);
543 mutex_unlock(&stm->policy_mutex);
544
545 if (policy_node)
546 config_item_get(&policy_node->group.cg_item);
547 else
548 mutex_unlock(&stp_policy_subsys.su_mutex);
549
550 return policy_node;
551}
552
553void stp_policy_node_put(struct stp_policy_node *policy_node)
554{
555 lockdep_assert_held(&stp_policy_subsys.su_mutex);
556
557 mutex_unlock(&stp_policy_subsys.su_mutex);
558 config_item_put(&policy_node->group.cg_item);
559}
560
561int __init stp_configfs_init(void)
562{
563 config_group_init(&stp_policy_subsys.su_group);
564 mutex_init(&stp_policy_subsys.su_mutex);
565 return configfs_register_subsystem(&stp_policy_subsys);
566}
567
568void __exit stp_configfs_exit(void)
569{
570 configfs_unregister_subsystem(&stp_policy_subsys);
571}