kfusd.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /*
  2. *
  3. * Copyright (c) 2003 The Regents of the University of California. All
  4. * rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * - Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. *
  13. * - Neither the name of the University nor the names of its
  14. * contributors may be used to endorse or promote products derived
  15. * from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS''
  18. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  19. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
  20. * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
  21. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  22. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  23. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  24. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  25. * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  27. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. */
  30. /*
  31. * FUSD: the Framework for User-Space Devices
  32. *
  33. * Jeremy Elson <jelson@circlemud.org>
  34. * Copyright (c) Sensoria Corporation 2001
  35. *
  36. * Private header file used by the Linux Kernel Module
  37. *
  38. * $Id: kfusd.h 12351 2007-01-19 07:22:54Z xiphmont $
  39. */
  40. #ifndef __KFUSD_H__
  41. # define __KFUSD_H__
  42. # include "fusd_msg.h"
  43. # include <linux/version.h>
  44. /* magic numbers for structure checking; unique w.r.t
  45. * /usr/src/linux/Documentation/magic-number.txt */
  46. # define FUSD_DEV_MAGIC 0x8b43a123
  47. # define FUSD_FILE_MAGIC 0x613aa8fe
  48. /* number of devices that can be created with fusd */
  49. # define MAX_FUSD_DEVICES 128
  50. /* number of times each device can be opened simultaneously */
  51. # define MIN_FILEARRAY_SIZE 8 /* initialize allocation */
  52. # define MAX_FILEARRAY_SIZE 1024 /* maximum it can grow to */
  53. /* maximum read/write size we're willing to service */
  54. # define MAX_RW_SIZE (1024*128)
  55. /********************** Structure Definitions *******************************/
  56. /* Container for a fusd msg */
  57. typedef struct fusd_msgC_s_t fusd_msgC_t;
  58. struct fusd_msgC_s_t {
  59. fusd_msg_t fusd_msg; /* the message itself */
  60. fusd_msgC_t *next; /* pointer to next one in the list */
  61. /* 1-bit flags */
  62. unsigned int peeked:1; /* has the first half of this been read? */
  63. };
  64. struct fusd_transaction
  65. {
  66. struct list_head list;
  67. long transid;
  68. int subcmd;
  69. int pid;
  70. int size;
  71. fusd_msg_t* msg_in;
  72. };
  73. /* magical forward declarations to break the circular dependency */
  74. struct fusd_dev_t_s;
  75. typedef struct fusd_dev_t_s fusd_dev_t;
  76. struct CLASS;
  77. struct device;
  78. /* state kept per opened file (i.e., an instance of a device) */
  79. typedef struct {
  80. /* general state management */
  81. int magic; /* magic number for sanity checking */
  82. fusd_dev_t *fusd_dev; /* fusd device associated with this file */
  83. long fusd_dev_version; /* version number of fusd device */
  84. void *private_data; /* the user's private data (we ignore it) */
  85. struct file *file; /* kernel's file pointer for this file */
  86. int index; /* our index in our device's file array */
  87. struct semaphore file_sem; /* Semaphore for file structure */
  88. int cached_poll_state; /* Latest result from a poll diff req */
  89. int last_poll_sent; /* Last polldiff request we sent */
  90. /* structures used for messaging */
  91. wait_queue_head_t file_wait; /* Wait on this for a user->kernel msg */
  92. wait_queue_head_t poll_wait; /* Given to kernel for poll() queue */
  93. struct list_head transactions;
  94. struct semaphore transactions_sem;
  95. } fusd_file_t;
  96. /* state kept per device registered under fusd */
  97. struct fusd_dev_t_s {
  98. int magic; /* Magic number for sanity checking */
  99. long version; /* Instance number of this device */
  100. int zombie; /* Is the device dead? */
  101. pid_t pid; /* PID of device driver */
  102. struct task_struct* task;
  103. char *name; /* Name of the device under devfs (/dev) */
  104. char *class_name;
  105. char *dev_name;
  106. struct CLASS *clazz;
  107. int owns_class;
  108. #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,29)
  109. struct class_device *device;
  110. #else
  111. struct device *device;
  112. #endif
  113. void *private_data; /* User's private data */
  114. struct cdev* handle;
  115. dev_t dev_id;
  116. fusd_file_t **files; /* Array of this device's open files */
  117. int array_size; /* Size of the array pointed to by 'files' */
  118. int num_files; /* Number of array entries that are valid */
  119. int open_in_progress; /* File is referencing this struct,
  120. but not yet part of the file array */
  121. /* messaging */
  122. fusd_msgC_t *msg_head; /* linked list head for message queue */
  123. fusd_msgC_t *msg_tail; /* linked list tail for message queue */
  124. /* synchronization */
  125. wait_queue_head_t dev_wait; /* Wait queue for kernel->user msgs */
  126. struct semaphore dev_sem; /* Sempahore for device structure */
  127. /* pointer to allow a dev to be placed on a dev_list */
  128. struct list_head devlist;
  129. };
  130. /**** Function Prototypes ****/
  131. STATIC int maybe_free_fusd_dev(fusd_dev_t *fusd_dev);
  132. STATIC int find_fusd_file(fusd_dev_t *fusd_dev, fusd_file_t *fusd_file);
  133. STATIC int free_fusd_file(fusd_dev_t *fusd_dev, fusd_file_t *fusd_file);
  134. STATIC int fusd_fops_call_send(fusd_file_t *fusd_file_arg,
  135. fusd_msg_t *fusd_msg, struct fusd_transaction** transaction);
  136. STATIC int fusd_fops_call_wait(fusd_file_t *fusd_file_arg,
  137. fusd_msg_t **fusd_msg_reply, struct fusd_transaction* transaction);
  138. STATIC void fusd_fops_call_done(fusd_file_t *fusd_file);
  139. STATIC void fusd_forge_close(fusd_msg_t *msg, fusd_dev_t *fusd_dev);
  140. STATIC int fusd_add_transaction(fusd_file_t *fusd_file, int transid, int subcmd, int size, struct fusd_transaction** out_transaction);
  141. STATIC void fusd_cleanup_transaction(fusd_file_t *fusd_file, struct fusd_transaction* transaction);
  142. STATIC void fusd_remove_transaction(fusd_file_t *fusd_file, struct fusd_transaction* transaction);
  143. STATIC struct fusd_transaction* fusd_find_transaction(fusd_file_t *fusd_file, int transid);
  144. STATIC struct fusd_transaction* fusd_find_transaction_by_pid(fusd_file_t *fusd_file, int pid);
  145. /**** Utility functions & macros ****/
  146. # ifdef CONFIG_FUSD_USE_WAKEUPSYNC
  147. # define WAKE_UP_INTERRUPTIBLE_SYNC(x) wake_up_interruptible_sync(x)
  148. # else
  149. # define WAKE_UP_INTERRUPTIBLE_SYNC(x) wake_up_interruptible(x)
  150. # endif /* CONFIG_FUSD_USE_WAKEUPSYNC */
  151. # ifdef CONFIG_FUSD_DEBUG
  152. static void rdebug_real(char *fmt, ...)
  153. __attribute__ ((format (printf, 1, 2)));
  154. # define RDEBUG(message_level, args...) do { \
  155. if (fusd_debug_level >= message_level) rdebug_real(args); \
  156. } while(0)
  157. # else
  158. # define RDEBUG(message_level, args...)
  159. # endif /* CONFIG_FUSD_DEBUG */
  160. # define ZOMBIE(fusd_dev) ((fusd_dev)->zombie)
  161. # define GET_FUSD_DEV(candidate, fusd_dev) do { \
  162. fusd_dev = candidate; \
  163. if (fusd_dev == NULL || fusd_dev->magic != FUSD_DEV_MAGIC) \
  164. goto invalid_dev; \
  165. } while (0)
  166. # define GET_FUSD_FILE_AND_DEV(candidate, fusd_file, fusd_dev) do { \
  167. fusd_file = candidate; \
  168. if (fusd_file == NULL || fusd_file->magic != FUSD_FILE_MAGIC) \
  169. goto invalid_file; \
  170. GET_FUSD_DEV(fusd_file->fusd_dev, fusd_dev); \
  171. if (fusd_dev->version != fusd_file->fusd_dev_version) \
  172. goto invalid_file; \
  173. } while (0)
  174. # define LOCK_FUSD_DEV(fusd_dev) \
  175. do { down(&fusd_dev->dev_sem); \
  176. if (ZOMBIE(fusd_dev)) { up(&fusd_dev->dev_sem); goto zombie_dev; } \
  177. } while (0)
  178. /* rawlock does not do a zombie check */
  179. # define RAWLOCK_FUSD_DEV(fusd_dev) \
  180. do { down(&fusd_dev->dev_sem); } while (0)
  181. # define UNLOCK_FUSD_DEV(fusd_dev) \
  182. do { up(&fusd_dev->dev_sem); } while (0)
  183. # define LOCK_FUSD_FILE(fusd_file) \
  184. do { down(&fusd_file->file_sem); \
  185. } while (0)
  186. # define UNLOCK_FUSD_FILE(fusd_file) \
  187. do { up(&fusd_file->file_sem); } while (0)
  188. # define FREE_FUSD_MSGC(fusd_msgc) do { \
  189. if ((fusd_msgc)->fusd_msg.data != NULL) VFREE(fusd_msgc->fusd_msg.data); \
  190. KFREE(fusd_msgc); \
  191. } while (0)
  192. # define FREE_FUSD_MSGC(fusd_msgc) do { \
  193. if ((fusd_msgc)->fusd_msg.data != NULL) VFREE(fusd_msgc->fusd_msg.data); \
  194. KFREE(fusd_msgc); \
  195. } while (0)
  196. # define NAME(fusd_dev) ((fusd_dev)->name == NULL ? \
  197. "<noname>" : (fusd_dev)->name)
  198. # ifdef CONFIG_FUSD_MEMDEBUG
  199. static int fusd_mem_init(void);
  200. static void fusd_mem_cleanup(void);
  201. static void fusd_mem_add(void *ptr, int line, int size);
  202. static void fusd_mem_del(void *ptr);
  203. static void *fusd_kmalloc(size_t size, int type, int line);
  204. static void fusd_kfree(void *ptr);
  205. static void *fusd_vmalloc(size_t size, int line);
  206. static void fusd_vfree(void *ptr);
  207. # define KMALLOC(size, type) fusd_kmalloc(size, type, __LINE__)
  208. # define KFREE(ptr) fusd_kfree(ptr)
  209. # define VMALLOC(size) fusd_vmalloc(size, __LINE__)
  210. # define VFREE(ptr) fusd_vfree(ptr)
  211. # else /* no memory debugging */
  212. # define KMALLOC(size, type) kmalloc(size, type)
  213. # define KFREE(ptr) kfree(ptr)
  214. /*# define VMALLOC(size) vmalloc(size)*/
  215. # define VMALLOC(size) kmalloc(size, GFP_KERNEL)
  216. # define VFREE(ptr) kfree(ptr)
  217. # endif /* CONFIG_FUSD_MEMDEBUG */
  218. /* Functions like this should be in the kernel, but they are not. Sigh. */
  219. # ifdef CONFIG_SMP
  220. #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,35)
  221. DECLARE_MUTEX(atomic_ops);
  222. #else
  223. DEFINE_SEMAPHORE(atomic_ops);
  224. #endif
  225. static __inline__ int atomic_inc_and_ret(int *i)
  226. {
  227. int val;
  228. down(&atomic_ops);
  229. val = (++(*i));
  230. up(&atomic_ops);
  231. return val;
  232. }
  233. # else
  234. static __inline__ int atomic_inc_and_ret(int *i)
  235. {
  236. return (++(*i));
  237. }
  238. # endif
  239. #endif /* __KFUSD_H__ */