intermodule.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /* Deprecated, do not use. Moved from module.c to here. --RR */
  2. /* Written by Keith Owens <kaos@ocs.com.au> Oct 2000 */
  3. #include <linux/module.h>
  4. #include <linux/kmod.h>
  5. #include <linux/spinlock.h>
  6. #include <linux/list.h>
  7. #include <linux/slab.h>
  8. /* inter_module functions are always available, even when the kernel is
  9. * compiled without modules. Consumers of inter_module_xxx routines
  10. * will always work, even when both are built into the kernel, this
  11. * approach removes lots of #ifdefs in mainline code.
  12. */
  13. static struct list_head ime_list = LIST_HEAD_INIT(ime_list);
  14. static DEFINE_SPINLOCK(ime_lock);
  15. static int kmalloc_failed;
  16. struct inter_module_entry {
  17. struct list_head list;
  18. const char *im_name;
  19. struct module *owner;
  20. const void *userdata;
  21. };
  22. /**
  23. * inter_module_register - register a new set of inter module data.
  24. * @im_name: an arbitrary string to identify the data, must be unique
  25. * @owner: module that is registering the data, always use THIS_MODULE
  26. * @userdata: pointer to arbitrary userdata to be registered
  27. *
  28. * Description: Check that the im_name has not already been registered,
  29. * complain if it has. For new data, add it to the inter_module_entry
  30. * list.
  31. */
  32. void inter_module_register(const char *im_name, struct module *owner, const void *userdata)
  33. {
  34. struct list_head *tmp;
  35. struct inter_module_entry *ime, *ime_new;
  36. if (!(ime_new = kmalloc(sizeof(*ime), GFP_KERNEL))) {
  37. /* Overloaded kernel, not fatal */
  38. printk(KERN_ERR
  39. "Aiee, inter_module_register: cannot kmalloc entry for '%s'\n",
  40. im_name);
  41. kmalloc_failed = 1;
  42. return;
  43. }
  44. memset(ime_new, 0, sizeof(*ime_new));
  45. ime_new->im_name = im_name;
  46. ime_new->owner = owner;
  47. ime_new->userdata = userdata;
  48. spin_lock(&ime_lock);
  49. list_for_each(tmp, &ime_list) {
  50. ime = list_entry(tmp, struct inter_module_entry, list);
  51. if (strcmp(ime->im_name, im_name) == 0) {
  52. spin_unlock(&ime_lock);
  53. kfree(ime_new);
  54. /* Program logic error, fatal */
  55. printk(KERN_ERR "inter_module_register: duplicate im_name '%s'", im_name);
  56. BUG();
  57. }
  58. }
  59. list_add(&(ime_new->list), &ime_list);
  60. spin_unlock(&ime_lock);
  61. }
  62. /**
  63. * inter_module_unregister - unregister a set of inter module data.
  64. * @im_name: an arbitrary string to identify the data, must be unique
  65. *
  66. * Description: Check that the im_name has been registered, complain if
  67. * it has not. For existing data, remove it from the
  68. * inter_module_entry list.
  69. */
  70. void inter_module_unregister(const char *im_name)
  71. {
  72. struct list_head *tmp;
  73. struct inter_module_entry *ime;
  74. spin_lock(&ime_lock);
  75. list_for_each(tmp, &ime_list) {
  76. ime = list_entry(tmp, struct inter_module_entry, list);
  77. if (strcmp(ime->im_name, im_name) == 0) {
  78. list_del(&(ime->list));
  79. spin_unlock(&ime_lock);
  80. kfree(ime);
  81. return;
  82. }
  83. }
  84. spin_unlock(&ime_lock);
  85. if (kmalloc_failed) {
  86. printk(KERN_ERR
  87. "inter_module_unregister: no entry for '%s', "
  88. "probably caused by previous kmalloc failure\n",
  89. im_name);
  90. return;
  91. }
  92. else {
  93. /* Program logic error, fatal */
  94. printk(KERN_ERR "inter_module_unregister: no entry for '%s'", im_name);
  95. BUG();
  96. }
  97. }
  98. /**
  99. * inter_module_get - return arbitrary userdata from another module.
  100. * @im_name: an arbitrary string to identify the data, must be unique
  101. *
  102. * Description: If the im_name has not been registered, return NULL.
  103. * Try to increment the use count on the owning module, if that fails
  104. * then return NULL. Otherwise return the userdata.
  105. */
  106. static const void *inter_module_get(const char *im_name)
  107. {
  108. struct list_head *tmp;
  109. struct inter_module_entry *ime;
  110. const void *result = NULL;
  111. spin_lock(&ime_lock);
  112. list_for_each(tmp, &ime_list) {
  113. ime = list_entry(tmp, struct inter_module_entry, list);
  114. if (strcmp(ime->im_name, im_name) == 0) {
  115. if (try_module_get(ime->owner))
  116. result = ime->userdata;
  117. break;
  118. }
  119. }
  120. spin_unlock(&ime_lock);
  121. return(result);
  122. }
  123. /**
  124. * inter_module_get_request - im get with automatic request_module.
  125. * @im_name: an arbitrary string to identify the data, must be unique
  126. * @modname: module that is expected to register im_name
  127. *
  128. * Description: If inter_module_get fails, do request_module then retry.
  129. */
  130. const void *inter_module_get_request(const char *im_name, const char *modname)
  131. {
  132. const void *result = inter_module_get(im_name);
  133. if (!result) {
  134. request_module("%s", modname);
  135. result = inter_module_get(im_name);
  136. }
  137. return(result);
  138. }
  139. /**
  140. * inter_module_put - release use of data from another module.
  141. * @im_name: an arbitrary string to identify the data, must be unique
  142. *
  143. * Description: If the im_name has not been registered, complain,
  144. * otherwise decrement the use count on the owning module.
  145. */
  146. void inter_module_put(const char *im_name)
  147. {
  148. struct list_head *tmp;
  149. struct inter_module_entry *ime;
  150. spin_lock(&ime_lock);
  151. list_for_each(tmp, &ime_list) {
  152. ime = list_entry(tmp, struct inter_module_entry, list);
  153. if (strcmp(ime->im_name, im_name) == 0) {
  154. if (ime->owner)
  155. module_put(ime->owner);
  156. spin_unlock(&ime_lock);
  157. return;
  158. }
  159. }
  160. spin_unlock(&ime_lock);
  161. printk(KERN_ERR "inter_module_put: no entry for '%s'", im_name);
  162. BUG();
  163. }
  164. EXPORT_SYMBOL(inter_module_register);
  165. EXPORT_SYMBOL(inter_module_unregister);
  166. EXPORT_SYMBOL(inter_module_get_request);
  167. EXPORT_SYMBOL(inter_module_put);