[POWERPC] Fix OF node refcnt underflow in 836x and 832x platform code
[linux-2.6/x86.git] / net / irda / irias_object.c
blobb1ee99a59c0cee416137e1d4cbcb371117d6a271
1 /*********************************************************************
3 * Filename: irias_object.c
4 * Version: 0.3
5 * Description: IAS object database and functions
6 * Status: Experimental.
7 * Author: Dag Brattli <dagb@cs.uit.no>
8 * Created at: Thu Oct 1 22:50:04 1998
9 * Modified at: Wed Dec 15 11:23:16 1999
10 * Modified by: Dag Brattli <dagb@cs.uit.no>
12 * Copyright (c) 1998-1999 Dag Brattli, All Rights Reserved.
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License as
16 * published by the Free Software Foundation; either version 2 of
17 * the License, or (at your option) any later version.
19 * Neither Dag Brattli nor University of Tromsø admit liability nor
20 * provide warranty for any of this software. This material is
21 * provided "AS-IS" and at no charge.
23 ********************************************************************/
25 #include <linux/string.h>
26 #include <linux/socket.h>
27 #include <linux/module.h>
29 #include <net/irda/irda.h>
30 #include <net/irda/irias_object.h>
32 hashbin_t *irias_objects;
35 * Used when a missing value needs to be returned
37 struct ias_value irias_missing = { IAS_MISSING, 0, 0, 0, {0}};
40 * Function strndup (str, max)
42 * My own kernel version of strndup!
44 * Faster, check boundary... Jean II
46 static char *strndup(char *str, size_t max)
48 char *new_str;
49 int len;
51 /* Check string */
52 if (str == NULL)
53 return NULL;
54 /* Check length, truncate */
55 len = strlen(str);
56 if(len > max)
57 len = max;
59 /* Allocate new string */
60 new_str = kmalloc(len + 1, GFP_ATOMIC);
61 if (new_str == NULL) {
62 IRDA_WARNING("%s: Unable to kmalloc!\n", __FUNCTION__);
63 return NULL;
66 /* Copy and truncate */
67 memcpy(new_str, str, len);
68 new_str[len] = '\0';
70 return new_str;
74 * Function ias_new_object (name, id)
76 * Create a new IAS object
79 struct ias_object *irias_new_object( char *name, int id)
81 struct ias_object *obj;
83 IRDA_DEBUG( 4, "%s()\n", __FUNCTION__);
85 obj = kzalloc(sizeof(struct ias_object), GFP_ATOMIC);
86 if (obj == NULL) {
87 IRDA_WARNING("%s(), Unable to allocate object!\n",
88 __FUNCTION__);
89 return NULL;
92 obj->magic = IAS_OBJECT_MAGIC;
93 obj->name = strndup(name, IAS_MAX_CLASSNAME);
94 obj->id = id;
96 /* Locking notes : the attrib spinlock has lower precendence
97 * than the objects spinlock. Never grap the objects spinlock
98 * while holding any attrib spinlock (risk of deadlock). Jean II */
99 obj->attribs = hashbin_new(HB_LOCK);
101 if (obj->attribs == NULL) {
102 IRDA_WARNING("%s(), Unable to allocate attribs!\n",
103 __FUNCTION__);
104 kfree(obj);
105 return NULL;
108 return obj;
110 EXPORT_SYMBOL(irias_new_object);
113 * Function irias_delete_attrib (attrib)
115 * Delete given attribute and deallocate all its memory
118 static void __irias_delete_attrib(struct ias_attrib *attrib)
120 IRDA_ASSERT(attrib != NULL, return;);
121 IRDA_ASSERT(attrib->magic == IAS_ATTRIB_MAGIC, return;);
123 kfree(attrib->name);
125 irias_delete_value(attrib->value);
126 attrib->magic = ~IAS_ATTRIB_MAGIC;
128 kfree(attrib);
131 void __irias_delete_object(struct ias_object *obj)
133 IRDA_ASSERT(obj != NULL, return;);
134 IRDA_ASSERT(obj->magic == IAS_OBJECT_MAGIC, return;);
136 kfree(obj->name);
138 hashbin_delete(obj->attribs, (FREE_FUNC) __irias_delete_attrib);
140 obj->magic = ~IAS_OBJECT_MAGIC;
142 kfree(obj);
146 * Function irias_delete_object (obj)
148 * Remove object from hashbin and deallocate all attributes associated with
149 * with this object and the object itself
152 int irias_delete_object(struct ias_object *obj)
154 struct ias_object *node;
156 IRDA_ASSERT(obj != NULL, return -1;);
157 IRDA_ASSERT(obj->magic == IAS_OBJECT_MAGIC, return -1;);
159 /* Remove from list */
160 node = hashbin_remove_this(irias_objects, (irda_queue_t *) obj);
161 if (!node)
162 IRDA_DEBUG( 0, "%s(), object already removed!\n",
163 __FUNCTION__);
165 /* Destroy */
166 __irias_delete_object(obj);
168 return 0;
170 EXPORT_SYMBOL(irias_delete_object);
173 * Function irias_delete_attrib (obj)
175 * Remove attribute from hashbin and, if it was the last attribute of
176 * the object, remove the object as well.
179 int irias_delete_attrib(struct ias_object *obj, struct ias_attrib *attrib,
180 int cleanobject)
182 struct ias_attrib *node;
184 IRDA_ASSERT(obj != NULL, return -1;);
185 IRDA_ASSERT(obj->magic == IAS_OBJECT_MAGIC, return -1;);
186 IRDA_ASSERT(attrib != NULL, return -1;);
188 /* Remove attribute from object */
189 node = hashbin_remove_this(obj->attribs, (irda_queue_t *) attrib);
190 if (!node)
191 return 0; /* Already removed or non-existent */
193 /* Deallocate attribute */
194 __irias_delete_attrib(node);
196 /* Check if object has still some attributes, destroy it if none.
197 * At first glance, this look dangerous, as the kernel reference
198 * various IAS objects. However, we only use this function on
199 * user attributes, not kernel attributes, so there is no risk
200 * of deleting a kernel object this way. Jean II */
201 node = (struct ias_attrib *) hashbin_get_first(obj->attribs);
202 if (cleanobject && !node)
203 irias_delete_object(obj);
205 return 0;
209 * Function irias_insert_object (obj)
211 * Insert an object into the LM-IAS database
214 void irias_insert_object(struct ias_object *obj)
216 IRDA_ASSERT(obj != NULL, return;);
217 IRDA_ASSERT(obj->magic == IAS_OBJECT_MAGIC, return;);
219 hashbin_insert(irias_objects, (irda_queue_t *) obj, 0, obj->name);
221 EXPORT_SYMBOL(irias_insert_object);
224 * Function irias_find_object (name)
226 * Find object with given name
229 struct ias_object *irias_find_object(char *name)
231 IRDA_ASSERT(name != NULL, return NULL;);
233 /* Unsafe (locking), object might change */
234 return hashbin_lock_find(irias_objects, 0, name);
236 EXPORT_SYMBOL(irias_find_object);
239 * Function irias_find_attrib (obj, name)
241 * Find named attribute in object
244 struct ias_attrib *irias_find_attrib(struct ias_object *obj, char *name)
246 struct ias_attrib *attrib;
248 IRDA_ASSERT(obj != NULL, return NULL;);
249 IRDA_ASSERT(obj->magic == IAS_OBJECT_MAGIC, return NULL;);
250 IRDA_ASSERT(name != NULL, return NULL;);
252 attrib = hashbin_lock_find(obj->attribs, 0, name);
253 if (attrib == NULL)
254 return NULL;
256 /* Unsafe (locking), attrib might change */
257 return attrib;
261 * Function irias_add_attribute (obj, attrib)
263 * Add attribute to object
266 static void irias_add_attrib(struct ias_object *obj, struct ias_attrib *attrib,
267 int owner)
269 IRDA_ASSERT(obj != NULL, return;);
270 IRDA_ASSERT(obj->magic == IAS_OBJECT_MAGIC, return;);
272 IRDA_ASSERT(attrib != NULL, return;);
273 IRDA_ASSERT(attrib->magic == IAS_ATTRIB_MAGIC, return;);
275 /* Set if attrib is owned by kernel or user space */
276 attrib->value->owner = owner;
278 hashbin_insert(obj->attribs, (irda_queue_t *) attrib, 0, attrib->name);
282 * Function irias_object_change_attribute (obj_name, attrib_name, new_value)
284 * Change the value of an objects attribute.
287 int irias_object_change_attribute(char *obj_name, char *attrib_name,
288 struct ias_value *new_value)
290 struct ias_object *obj;
291 struct ias_attrib *attrib;
292 unsigned long flags;
294 /* Find object */
295 obj = hashbin_lock_find(irias_objects, 0, obj_name);
296 if (obj == NULL) {
297 IRDA_WARNING("%s: Unable to find object: %s\n", __FUNCTION__,
298 obj_name);
299 return -1;
302 /* Slightly unsafe (obj might get removed under us) */
303 spin_lock_irqsave(&obj->attribs->hb_spinlock, flags);
305 /* Find attribute */
306 attrib = hashbin_find(obj->attribs, 0, attrib_name);
307 if (attrib == NULL) {
308 IRDA_WARNING("%s: Unable to find attribute: %s\n",
309 __FUNCTION__, attrib_name);
310 spin_unlock_irqrestore(&obj->attribs->hb_spinlock, flags);
311 return -1;
314 if ( attrib->value->type != new_value->type) {
315 IRDA_DEBUG( 0, "%s(), changing value type not allowed!\n",
316 __FUNCTION__);
317 spin_unlock_irqrestore(&obj->attribs->hb_spinlock, flags);
318 return -1;
321 /* Delete old value */
322 irias_delete_value(attrib->value);
324 /* Insert new value */
325 attrib->value = new_value;
327 /* Success */
328 spin_unlock_irqrestore(&obj->attribs->hb_spinlock, flags);
329 return 0;
331 EXPORT_SYMBOL(irias_object_change_attribute);
334 * Function irias_object_add_integer_attrib (obj, name, value)
336 * Add an integer attribute to an LM-IAS object
339 void irias_add_integer_attrib(struct ias_object *obj, char *name, int value,
340 int owner)
342 struct ias_attrib *attrib;
344 IRDA_ASSERT(obj != NULL, return;);
345 IRDA_ASSERT(obj->magic == IAS_OBJECT_MAGIC, return;);
346 IRDA_ASSERT(name != NULL, return;);
348 attrib = kzalloc(sizeof(struct ias_attrib), GFP_ATOMIC);
349 if (attrib == NULL) {
350 IRDA_WARNING("%s: Unable to allocate attribute!\n",
351 __FUNCTION__);
352 return;
355 attrib->magic = IAS_ATTRIB_MAGIC;
356 attrib->name = strndup(name, IAS_MAX_ATTRIBNAME);
358 /* Insert value */
359 attrib->value = irias_new_integer_value(value);
361 irias_add_attrib(obj, attrib, owner);
363 EXPORT_SYMBOL(irias_add_integer_attrib);
366 * Function irias_add_octseq_attrib (obj, name, octet_seq, len)
368 * Add a octet sequence attribute to an LM-IAS object
372 void irias_add_octseq_attrib(struct ias_object *obj, char *name, __u8 *octets,
373 int len, int owner)
375 struct ias_attrib *attrib;
377 IRDA_ASSERT(obj != NULL, return;);
378 IRDA_ASSERT(obj->magic == IAS_OBJECT_MAGIC, return;);
380 IRDA_ASSERT(name != NULL, return;);
381 IRDA_ASSERT(octets != NULL, return;);
383 attrib = kzalloc(sizeof(struct ias_attrib), GFP_ATOMIC);
384 if (attrib == NULL) {
385 IRDA_WARNING("%s: Unable to allocate attribute!\n",
386 __FUNCTION__);
387 return;
390 attrib->magic = IAS_ATTRIB_MAGIC;
391 attrib->name = strndup(name, IAS_MAX_ATTRIBNAME);
393 attrib->value = irias_new_octseq_value( octets, len);
395 irias_add_attrib(obj, attrib, owner);
397 EXPORT_SYMBOL(irias_add_octseq_attrib);
400 * Function irias_object_add_string_attrib (obj, string)
402 * Add a string attribute to an LM-IAS object
405 void irias_add_string_attrib(struct ias_object *obj, char *name, char *value,
406 int owner)
408 struct ias_attrib *attrib;
410 IRDA_ASSERT(obj != NULL, return;);
411 IRDA_ASSERT(obj->magic == IAS_OBJECT_MAGIC, return;);
413 IRDA_ASSERT(name != NULL, return;);
414 IRDA_ASSERT(value != NULL, return;);
416 attrib = kzalloc(sizeof( struct ias_attrib), GFP_ATOMIC);
417 if (attrib == NULL) {
418 IRDA_WARNING("%s: Unable to allocate attribute!\n",
419 __FUNCTION__);
420 return;
423 attrib->magic = IAS_ATTRIB_MAGIC;
424 attrib->name = strndup(name, IAS_MAX_ATTRIBNAME);
426 attrib->value = irias_new_string_value(value);
428 irias_add_attrib(obj, attrib, owner);
430 EXPORT_SYMBOL(irias_add_string_attrib);
433 * Function irias_new_integer_value (integer)
435 * Create new IAS integer value
438 struct ias_value *irias_new_integer_value(int integer)
440 struct ias_value *value;
442 value = kzalloc(sizeof(struct ias_value), GFP_ATOMIC);
443 if (value == NULL) {
444 IRDA_WARNING("%s: Unable to kmalloc!\n", __FUNCTION__);
445 return NULL;
448 value->type = IAS_INTEGER;
449 value->len = 4;
450 value->t.integer = integer;
452 return value;
454 EXPORT_SYMBOL(irias_new_integer_value);
457 * Function irias_new_string_value (string)
459 * Create new IAS string value
461 * Per IrLMP 1.1, 4.3.3.2, strings are up to 256 chars - Jean II
463 struct ias_value *irias_new_string_value(char *string)
465 struct ias_value *value;
467 value = kzalloc(sizeof(struct ias_value), GFP_ATOMIC);
468 if (value == NULL) {
469 IRDA_WARNING("%s: Unable to kmalloc!\n", __FUNCTION__);
470 return NULL;
473 value->type = IAS_STRING;
474 value->charset = CS_ASCII;
475 value->t.string = strndup(string, IAS_MAX_STRING);
476 value->len = strlen(value->t.string);
478 return value;
482 * Function irias_new_octseq_value (octets, len)
484 * Create new IAS octet-sequence value
486 * Per IrLMP 1.1, 4.3.3.2, octet-sequence are up to 1024 bytes - Jean II
488 struct ias_value *irias_new_octseq_value(__u8 *octseq , int len)
490 struct ias_value *value;
492 value = kzalloc(sizeof(struct ias_value), GFP_ATOMIC);
493 if (value == NULL) {
494 IRDA_WARNING("%s: Unable to kmalloc!\n", __FUNCTION__);
495 return NULL;
498 value->type = IAS_OCT_SEQ;
499 /* Check length */
500 if(len > IAS_MAX_OCTET_STRING)
501 len = IAS_MAX_OCTET_STRING;
502 value->len = len;
504 value->t.oct_seq = kmemdup(octseq, len, GFP_ATOMIC);
505 if (value->t.oct_seq == NULL){
506 IRDA_WARNING("%s: Unable to kmalloc!\n", __FUNCTION__);
507 kfree(value);
508 return NULL;
510 return value;
513 struct ias_value *irias_new_missing_value(void)
515 struct ias_value *value;
517 value = kzalloc(sizeof(struct ias_value), GFP_ATOMIC);
518 if (value == NULL) {
519 IRDA_WARNING("%s: Unable to kmalloc!\n", __FUNCTION__);
520 return NULL;
523 value->type = IAS_MISSING;
525 return value;
529 * Function irias_delete_value (value)
531 * Delete IAS value
534 void irias_delete_value(struct ias_value *value)
536 IRDA_DEBUG(4, "%s()\n", __FUNCTION__);
538 IRDA_ASSERT(value != NULL, return;);
540 switch (value->type) {
541 case IAS_INTEGER: /* Fallthrough */
542 case IAS_MISSING:
543 /* No need to deallocate */
544 break;
545 case IAS_STRING:
546 /* Deallocate string */
547 kfree(value->t.string);
548 break;
549 case IAS_OCT_SEQ:
550 /* Deallocate byte stream */
551 kfree(value->t.oct_seq);
552 break;
553 default:
554 IRDA_DEBUG(0, "%s(), Unknown value type!\n", __FUNCTION__);
555 break;
557 kfree(value);
559 EXPORT_SYMBOL(irias_delete_value);