kfusd.c 82 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943
  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. * Linux Kernel Module
  34. *
  35. * Jeremy Elson <jelson@circlemud.org>
  36. * Copyright (c) 2001, Sensoria Corporation
  37. * Copyright (c) 2002-2003, Regents of the University of California
  38. *
  39. * $Id$
  40. */
  41. /*
  42. * Note on debugging messages: Unexpected errors (i.e., indicators of
  43. * bugs in this kernel module) should always contain '!'. Expected
  44. * conditions, even if exceptional (e.g., the device-driver-provider
  45. * disappears while a file is waiting for a return from a system call)
  46. * must NOT contain '!'.
  47. */
  48. #ifndef __KERNEL__
  49. #define __KERNEL__
  50. #endif
  51. #ifdef MODVERSIONS
  52. #include <linux/modversions.h>
  53. #endif
  54. //#include <linux/config.h>
  55. #include <linux/stddef.h>
  56. #include <linux/kernel.h>
  57. #include <linux/module.h>
  58. #include <linux/list.h>
  59. #include <linux/init.h>
  60. #include <linux/fs.h>
  61. #include <linux/mm.h>
  62. #include <linux/slab.h>
  63. #include <linux/vmalloc.h>
  64. //#include <linux/devfs_fs_kernel.h>
  65. #include <linux/poll.h>
  66. #include <linux/version.h>
  67. #include <linux/major.h>
  68. #include <linux/uio.h>
  69. #include <linux/cdev.h>
  70. #include <linux/device.h>
  71. #include <linux/highmem.h>
  72. #include <asm/atomic.h>
  73. #include <asm/uaccess.h>
  74. #include <asm/ioctl.h>
  75. #include <asm/pgtable.h>
  76. #include <asm/pgalloc.h>
  77. #define STATIC
  78. /* Define this if you want to emit debug messages (adds ~8K) */
  79. #define CONFIG_FUSD_DEBUG
  80. /* Default debug level for FUSD messages. Has no effect unless
  81. * CONFIG_FUSD_DEBUG is defined. */
  82. #ifndef CONFIG_FUSD_DEBUGLEVEL
  83. #define CONFIG_FUSD_DEBUGLEVEL 2
  84. #endif
  85. /* Define this to check for memory leaks */
  86. /*#define CONFIG_FUSD_MEMDEBUG*/
  87. /* Define this to use the faster wake_up_interruptible_sync instead of
  88. * the normal wake_up_interruptible. Note: you can't do this unless
  89. * you're bulding fusd as part of the kernel (not a module); or you've
  90. * patched kernel/ksyms.s to add __wake_up_sync in addition to
  91. * __wake_up. */
  92. /* #define CONFIG_FUSD_USE_WAKEUPSYNC */
  93. #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,4,9)
  94. # define vsnprintf(str, size, format, ap) vsprintf(str, format, ap)
  95. # define snprintf(str, len, args...) sprintf(str, args)
  96. #endif
  97. #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,13)
  98. #define CLASS class_simple
  99. #define class_create class_simple_create
  100. #define class_destroy class_simple_destroy
  101. #define CLASS_DEVICE_CREATE(a, b, c, d, e) class_simple_device_add(a, c, d, e)
  102. #define class_device_destroy(a, b) class_simple_device_remove(b)
  103. #else
  104. #define CLASS class
  105. #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,15)
  106. #define CLASS_DEVICE_CREATE(a, b, c, d, e) class_device_create(a, c, d, e)
  107. #else
  108. #define CLASS_DEVICE_CREATE(a, b, c, d, e) class_device_create(a, b, c, d, e)
  109. #endif
  110. #endif
  111. /**************************************************************************/
  112. #include "fusd.h"
  113. #include "fusd_msg.h"
  114. #include "kfusd.h"
  115. #if LINUX_VERSION_CODE < KERNEL_VERSION(2,4,0)
  116. # error "***FUSD doesn't work before Linux Kernel v2.4.0"
  117. #endif
  118. STATIC struct cdev* fusd_control_device;
  119. STATIC struct cdev* fusd_status_device;
  120. STATIC dev_t control_id;
  121. STATIC dev_t status_id;
  122. static struct CLASS *fusd_class;
  123. static struct class_device *fusd_control_class_device;
  124. static struct class_device *fusd_status_class_device;
  125. extern struct CLASS *sound_class;
  126. /* version number incremented for each registered device */
  127. STATIC int last_version = 1;
  128. /* version number incremented for each transaction to userspace */
  129. STATIC int last_transid = 1;
  130. /* wait queue that is awakened when new devices are registered */
  131. STATIC DECLARE_WAIT_QUEUE_HEAD(new_device_wait);
  132. /* the list of valid devices, and sem to protect it */
  133. LIST_HEAD(fusd_devlist_head);
  134. DECLARE_MUTEX(fusd_devlist_sem);
  135. //#ifdef MODULE_LICENSE
  136. MODULE_AUTHOR("Jeremy Elson <jelson@acm.org> (c)2001");
  137. MODULE_LICENSE("GPL");
  138. //#endif
  139. /***************************Debugging Support*****************************/
  140. #ifdef CONFIG_FUSD_DEBUG
  141. STATIC int fusd_debug_level = CONFIG_FUSD_DEBUGLEVEL;
  142. module_param(fusd_debug_level, int, S_IRUGO);
  143. #define BUFSIZE 1000 /* kernel's kmalloc pool has a 1012-sized bucket */
  144. STATIC void rdebug_real(char *fmt, ...)
  145. {
  146. va_list ap;
  147. int len;
  148. char *message;
  149. /* I'm kmallocing since you don't really want 1k on the stack. I've
  150. * had stack overflow problems before; the kernel stack is quite
  151. * small... */
  152. if ((message = KMALLOC(BUFSIZE, GFP_KERNEL)) == NULL)
  153. return;
  154. va_start(ap, fmt);
  155. len = vsnprintf(message, BUFSIZE-1, fmt, ap);
  156. va_end(ap);
  157. if (len >= BUFSIZE) {
  158. printk("WARNING: POSSIBLE KERNEL CORRUPTION; MESSAGE TOO LONG\n");
  159. } else {
  160. printk("fusd: %.975s\n", message); /* note msgs are truncated at
  161. * ~1000 chars to fit inside the 1024 printk
  162. * limit imposed by the kernel */
  163. }
  164. KFREE(message);
  165. }
  166. #endif /* CONFIG_FUSD_DEBUG */
  167. /******************** Memory Debugging ************************************/
  168. #ifdef CONFIG_FUSD_MEMDEBUG
  169. #define MAX_MEM_DEBUG 10000
  170. DECLARE_MUTEX(fusd_memdebug_sem);
  171. typedef struct {
  172. void *ptr;
  173. int line;
  174. int size;
  175. } mem_debug_t;
  176. mem_debug_t *mem_debug;
  177. STATIC int fusd_mem_init(void)
  178. {
  179. int i;
  180. mem_debug = kmalloc(sizeof(mem_debug_t) * MAX_MEM_DEBUG, GFP_KERNEL);
  181. if (mem_debug == NULL) {
  182. RDEBUG(2, "argh - memdebug malloc failed!");
  183. return -ENOMEM;
  184. }
  185. /* initialize */
  186. for (i = 0; i < MAX_MEM_DEBUG; i++)
  187. mem_debug[i].ptr = NULL;
  188. RDEBUG(2, "FUSD memory debugger activated");
  189. return 0;
  190. }
  191. STATIC void fusd_mem_cleanup(void)
  192. {
  193. int i;
  194. int count=0;
  195. for (i = 0; i < MAX_MEM_DEBUG; i++)
  196. if (mem_debug[i].ptr != NULL) {
  197. RDEBUG(0, "memdebug: failed to free memory allocated at line %d (%d b)",
  198. mem_debug[i].line, mem_debug[i].size);
  199. count++;
  200. }
  201. if (!count)
  202. RDEBUG(2, "congratulations - memory debugger is happy!");
  203. kfree(mem_debug);
  204. }
  205. STATIC void fusd_mem_add(void *ptr, int line, int size)
  206. {
  207. int i;
  208. if (ptr==NULL)
  209. return;
  210. for (i = 0; i < MAX_MEM_DEBUG; i++) {
  211. if (mem_debug[i].ptr == NULL) {
  212. mem_debug[i].ptr = ptr;
  213. mem_debug[i].line = line;
  214. mem_debug[i].size = size;
  215. return;
  216. }
  217. }
  218. RDEBUG(1, "WARNING - memdebug out of space!!!!");
  219. }
  220. STATIC void fusd_mem_del(void *ptr)
  221. {
  222. int i;
  223. for (i = 0; i < MAX_MEM_DEBUG; i++) {
  224. if (mem_debug[i].ptr == ptr) {
  225. mem_debug[i].ptr = NULL;
  226. return;
  227. }
  228. }
  229. RDEBUG(2, "WARNING - memdebug is confused!!!!");
  230. }
  231. STATIC void *fusd_kmalloc(size_t size, int type, int line)
  232. {
  233. void *ptr = kmalloc(size, type);
  234. down(&fusd_memdebug_sem);
  235. fusd_mem_add(ptr, line, size);
  236. up(&fusd_memdebug_sem);
  237. return ptr;
  238. }
  239. STATIC void fusd_kfree(void *ptr)
  240. {
  241. down(&fusd_memdebug_sem);
  242. fusd_mem_del(ptr);
  243. kfree(ptr);
  244. up(&fusd_memdebug_sem);
  245. }
  246. STATIC void *fusd_vmalloc(size_t size, int line)
  247. {
  248. void *ptr = vmalloc(size);
  249. down(&fusd_memdebug_sem);
  250. fusd_mem_add(ptr, line, size);
  251. up(&fusd_memdebug_sem);
  252. return ptr;
  253. }
  254. STATIC void fusd_vfree(void *ptr)
  255. {
  256. down(&fusd_memdebug_sem);
  257. fusd_mem_del(ptr);
  258. vfree(ptr);
  259. up(&fusd_memdebug_sem);
  260. }
  261. #endif /* CONFIG_FUSD_MEMDEBUG */
  262. /********************* FUSD Device List ***************************/
  263. /*************************************************************************/
  264. /************** STATE MANAGEMENT AND BOOKKEEPING UTILITIES ***************/
  265. /*************************************************************************/
  266. STATIC inline void init_fusd_msg(fusd_msg_t *fusd_msg)
  267. {
  268. if (fusd_msg == NULL)
  269. return;
  270. memset(fusd_msg, 0, sizeof(fusd_msg_t));
  271. fusd_msg->magic = FUSD_MSG_MAGIC;
  272. fusd_msg->cmd = FUSD_FOPS_CALL; /* typical, but can be overwritten */
  273. }
  274. /*
  275. * free a fusd_msg, and NULL out the pointer that points to that fusd_msg.
  276. */
  277. STATIC inline void free_fusd_msg(fusd_msg_t **fusd_msg)
  278. {
  279. if (fusd_msg == NULL || *fusd_msg == NULL)
  280. return;
  281. if ((*fusd_msg)->data != NULL) {
  282. VFREE((*fusd_msg)->data);
  283. (*fusd_msg)->data = NULL;
  284. }
  285. KFREE(*fusd_msg);
  286. *fusd_msg = NULL;
  287. }
  288. /* adjust the size of the 'files' array attached to the device to
  289. * better match the number of files. In all cases, size must be at
  290. * least MIN_ARRAY_SIZE. Subject to that constraint: if
  291. * num_files==array_size, the size is doubled; if
  292. * num_files<array_size/4, the size is halved. Array is kept as is if
  293. * the malloc fails. Returns a pointer to the new file struct or NULL
  294. * if there isn't one. */
  295. STATIC fusd_file_t **fusd_dev_adjsize(fusd_dev_t *fusd_dev)
  296. {
  297. fusd_file_t **old_array;
  298. int old_size;
  299. old_array = fusd_dev->files;
  300. old_size = fusd_dev->array_size;
  301. /* compute the new size of the array */
  302. if (fusd_dev->array_size > 4*fusd_dev->num_files)
  303. fusd_dev->array_size /= 2;
  304. else if (fusd_dev->array_size == fusd_dev->num_files)
  305. fusd_dev->array_size *= 2;
  306. /* respect the minimums and maximums (policy) */
  307. if (fusd_dev->array_size < MIN_FILEARRAY_SIZE)
  308. fusd_dev->array_size = MIN_FILEARRAY_SIZE;
  309. if (fusd_dev->array_size > MAX_FILEARRAY_SIZE)
  310. fusd_dev->array_size = MAX_FILEARRAY_SIZE;
  311. /* make sure it's sane */
  312. if (fusd_dev->array_size < fusd_dev->num_files) {
  313. RDEBUG(0, "fusd_dev_adjsize is royally screwed up!!!!!");
  314. return fusd_dev->files;
  315. }
  316. /* create a new array. if successful, copy the contents of the old
  317. * one. if not, revert back to the old. */
  318. fusd_dev->files = KMALLOC(fusd_dev->array_size * sizeof(fusd_file_t *),
  319. GFP_KERNEL);
  320. if (fusd_dev->files == NULL) {
  321. RDEBUG(1, "malloc failed in fusd_dev_adjsize!");
  322. fusd_dev->files = old_array;
  323. fusd_dev->array_size = old_size;
  324. } else {
  325. RDEBUG(10, "/dev/%s now has space for %d files (had %d)", NAME(fusd_dev),
  326. fusd_dev->array_size, old_size);
  327. memset(fusd_dev->files, 0, fusd_dev->array_size * sizeof(fusd_file_t *));
  328. memcpy(fusd_dev->files, old_array,
  329. fusd_dev->num_files * sizeof(fusd_file_t *));
  330. KFREE(old_array);
  331. }
  332. return fusd_dev->files;
  333. }
  334. /*
  335. * DEVICE LOCK MUST BE HELD TO CALL THIS FUNCTION
  336. *
  337. * This function frees a device IF there is nothing left that is
  338. * referencing it.
  339. *
  340. * Specifically, we do not free the device if:
  341. * - The driver is still active (i.e. device is not a zombie)
  342. * - There are still files with the device open
  343. * - There is an open in progress, i.e. a client has verified that
  344. * this is a valid device and is getting ready to add itself as an
  345. * open file.
  346. *
  347. * If the device is safe to free, it is removed from the valid list
  348. * (in verysafe mode only) and freed.
  349. *
  350. * Returns: 1 if the device was freed
  351. * 0 if the device still exists (and can be unlocked) */
  352. STATIC int maybe_free_fusd_dev(fusd_dev_t *fusd_dev)
  353. {
  354. fusd_msgC_t *ptr, *next;
  355. down(&fusd_devlist_sem);
  356. /* DON'T free the device under conditions listed above */
  357. if (!fusd_dev->zombie || fusd_dev->num_files || fusd_dev->open_in_progress) {
  358. up(&fusd_devlist_sem);
  359. return 0;
  360. }
  361. /* OK - bombs away! This fusd_dev_t is on its way out the door! */
  362. RDEBUG(8, "freeing state associated with /dev/%s", NAME(fusd_dev));
  363. /* delete it off the list of valid devices, and unlock */
  364. list_del(&fusd_dev->devlist);
  365. up(&fusd_devlist_sem);
  366. /* free any outgoing messages that the device might have waiting */
  367. for (ptr = fusd_dev->msg_head; ptr != NULL; ptr = next) {
  368. next = ptr->next;
  369. FREE_FUSD_MSGC(ptr);
  370. }
  371. /* free the device's dev name */
  372. if (fusd_dev->dev_name != NULL) {
  373. KFREE(fusd_dev->dev_name);
  374. fusd_dev->dev_name = NULL;
  375. }
  376. /* free the device's class name */
  377. if (fusd_dev->class_name != NULL) {
  378. KFREE(fusd_dev->class_name);
  379. fusd_dev->class_name = NULL;
  380. }
  381. /* free the device's name */
  382. if (fusd_dev->name != NULL) {
  383. KFREE(fusd_dev->name);
  384. fusd_dev->name = NULL;
  385. }
  386. /* free the array used to store pointers to fusd_file_t's */
  387. if (fusd_dev->files != NULL) {
  388. KFREE(fusd_dev->files);
  389. fusd_dev->files = NULL;
  390. }
  391. /* clear the structure and free it! */
  392. memset(fusd_dev, 0, sizeof(fusd_dev_t));
  393. KFREE(fusd_dev);
  394. /* notify fusd_status readers that there has been a change in the
  395. * list of registered devices */
  396. atomic_inc_and_ret(&last_version);
  397. wake_up_interruptible(&new_device_wait);
  398. //MOD_DEC_USE_COUNT;
  399. return 1;
  400. }
  401. /*
  402. *
  403. * DO NOT CALL THIS FUNCTION UNLESS THE DEVICE IS ALREADY LOCKED
  404. *
  405. * zombify_device: called when the driver disappears. Indicates that
  406. * the driver is no longer available to service requests. If there
  407. * are no outstanding system calls waiting for the fusd_dev state, the
  408. * device state itself is freed.
  409. *
  410. */
  411. STATIC void zombify_dev(fusd_dev_t *fusd_dev)
  412. {
  413. int i;
  414. if (fusd_dev->zombie) {
  415. RDEBUG(1, "zombify_device called on a zombie!!");
  416. return;
  417. }
  418. fusd_dev->zombie = 1;
  419. RDEBUG(3, "/dev/%s turning into a zombie (%d open files)", NAME(fusd_dev),
  420. fusd_dev->num_files);
  421. /* If there are files holding this device open, wake them up. */
  422. for (i = 0; i < fusd_dev->num_files; i++) {
  423. wake_up_interruptible(&fusd_dev->files[i]->file_wait);
  424. wake_up_interruptible(&fusd_dev->files[i]->poll_wait);
  425. }
  426. }
  427. /* utility function to find the index of a fusd_file in a fusd_dev.
  428. * returns index if found, -1 if not found. ASSUMES WE HAVE A VALID
  429. * fusd_dev. fusd_file may be NULL if we are searching for an empty
  430. * slot. */
  431. STATIC int find_fusd_file(fusd_dev_t *fusd_dev, fusd_file_t *fusd_file)
  432. {
  433. int i, num_files = fusd_dev->num_files;
  434. fusd_file_t **files = fusd_dev->files;
  435. for (i = 0; i < num_files; i++)
  436. if (files[i] == fusd_file)
  437. return i;
  438. return -1;
  439. }
  440. /*
  441. * DEVICE LOCK MUST BE HELD BEFORE THIS IS CALLED
  442. *
  443. * Returns 1 if the device was also freed. 0 if only the file was
  444. * freed. If the device is freed, then do not try to unlock it!
  445. * (Callers: Check the return value before unlocking!)
  446. */
  447. STATIC int free_fusd_file(fusd_dev_t *fusd_dev, fusd_file_t *fusd_file)
  448. {
  449. int i;
  450. struct list_head *tmp, *it;
  451. /* find the index of the file in the device's file-list... */
  452. if ((i = find_fusd_file(fusd_dev, fusd_file)) < 0)
  453. panic("corrupted fusd_dev: releasing a file that we think is closed");
  454. /* ...and remove it (by putting the last entry into its place) */
  455. fusd_dev->files[i] = fusd_dev->files[--(fusd_dev->num_files)];
  456. /* there might be an incoming message waiting for a restarted system
  457. * call. free it -- after possibly forging a close (see
  458. * fusd_forge_close). */
  459. list_for_each_safe(it, tmp, &fusd_file->transactions)
  460. {
  461. struct fusd_transaction* transaction = list_entry(it, struct fusd_transaction, list);
  462. if(transaction->msg_in)
  463. {
  464. if (transaction->msg_in->subcmd == FUSD_OPEN && transaction->msg_in->parm.fops_msg.retval == 0)
  465. fusd_forge_close(transaction->msg_in, fusd_dev);
  466. free_fusd_msg(&transaction->msg_in);
  467. }
  468. KFREE(transaction);
  469. }
  470. /* free state associated with this file */
  471. memset(fusd_file, 0, sizeof(fusd_file_t));
  472. KFREE(fusd_file);
  473. /* reduce the size of the file array if necessary */
  474. if (fusd_dev->array_size > MIN_FILEARRAY_SIZE &&
  475. fusd_dev->array_size > 4*fusd_dev->num_files)
  476. fusd_dev_adjsize(fusd_dev);
  477. /* renumber the array */
  478. for (i = 0; i < fusd_dev->num_files; i++)
  479. fusd_dev->files[i]->index = i;
  480. /* try to free the device -- this may have been its last file */
  481. return maybe_free_fusd_dev(fusd_dev);
  482. }
  483. /****************************************************************************/
  484. /********************** CLIENT CALLBACK FUNCTIONS ***************************/
  485. /****************************************************************************/
  486. /* todo
  487. * fusd_restart_check: Called from the beginning of most system calls
  488. * to see if we are restarting a system call.
  489. *
  490. * In the common case -- that this is NOT a restarted syscall -- we
  491. * return 0.
  492. *
  493. * In the much less common case, we return ERESTARTSYS, and expect the
  494. * caller to jump right to its fusd_fops_call() call.
  495. *
  496. * In the even LESS (hopefully very rare) case when one PID had an
  497. * interrupted syscall, but a different PID is the next to do a system
  498. * call on that file descriptor -- well, we lose. Clear state of that
  499. * old syscall out and continue as usual.
  500. */
  501. STATIC struct fusd_transaction* fusd_find_incomplete_transaction(fusd_file_t *fusd_file, int subcmd)
  502. {
  503. struct fusd_transaction* transaction = fusd_find_transaction_by_pid(fusd_file, current->pid);
  504. if(transaction == NULL)
  505. return NULL;
  506. if (transaction->subcmd != subcmd)
  507. {
  508. RDEBUG(2, "Incomplete transaction %ld thrown out, was expecting subcmd %d but received %d",
  509. transaction->transid, transaction->subcmd, subcmd);
  510. fusd_cleanup_transaction(fusd_file, transaction);
  511. return NULL;
  512. }
  513. RDEBUG(4, "pid %d restarting system call with transid %ld", current->pid,
  514. transaction->transid);
  515. return transaction;
  516. }
  517. STATIC int send_to_dev(fusd_dev_t *fusd_dev, fusd_msg_t *fusd_msg, int locked)
  518. {
  519. fusd_msgC_t *fusd_msgC;
  520. /* allocate a container for the message */
  521. if ((fusd_msgC = KMALLOC(sizeof(fusd_msgC_t), GFP_KERNEL)) == NULL)
  522. return -ENOMEM;
  523. memset(fusd_msgC, 0, sizeof(fusd_msgC_t));
  524. memcpy(&fusd_msgC->fusd_msg, fusd_msg, sizeof(fusd_msg_t));
  525. if (!locked)
  526. LOCK_FUSD_DEV(fusd_dev);
  527. /* put the message in the device's outgoing queue. */
  528. if (fusd_dev->msg_head == NULL) {
  529. fusd_dev->msg_head = fusd_dev->msg_tail = fusd_msgC;
  530. } else {
  531. fusd_dev->msg_tail->next = fusd_msgC;
  532. fusd_dev->msg_tail = fusd_msgC;
  533. }
  534. if (!locked)
  535. UNLOCK_FUSD_DEV(fusd_dev);
  536. /* wake up the driver, which now has a message waiting in its queue */
  537. WAKE_UP_INTERRUPTIBLE_SYNC(&fusd_dev->dev_wait);
  538. return 0;
  539. zombie_dev:
  540. KFREE(fusd_msgC);
  541. return -EPIPE;
  542. }
  543. /*
  544. * special case: if the driver sent back a successful "open", but
  545. * there is no file that is actually open, we forge a "close" so that
  546. * the driver can maintain balanced open/close pairs. We put calls to
  547. * this in fusd_fops_reply, when the reply first comes in; and,
  548. * free_fusd_file, when we throw away a reply that had been
  549. * pending for a restart.
  550. */
  551. STATIC void fusd_forge_close(fusd_msg_t *msg, fusd_dev_t *fusd_dev)
  552. {
  553. RDEBUG(2, "/dev/%s tried to complete an open for transid %ld, "
  554. "forging a close", NAME(fusd_dev), msg->parm.fops_msg.transid);
  555. msg->cmd = FUSD_FOPS_CALL_DROPREPLY;
  556. msg->subcmd = FUSD_CLOSE;
  557. msg->parm.fops_msg.transid = atomic_inc_and_ret(&last_transid);
  558. send_to_dev(fusd_dev, msg, 1);
  559. }
  560. /*
  561. * fusd_fops_call_send: send a fusd_msg into userspace.
  562. *
  563. * NOTE - we are already holding the lock on fusd_file_arg when this
  564. * function is called, but NOT the lock on the fusd_dev
  565. */
  566. STATIC int fusd_fops_call_send(fusd_file_t *fusd_file_arg,
  567. fusd_msg_t *fusd_msg, struct fusd_transaction** transaction)
  568. {
  569. fusd_dev_t *fusd_dev;
  570. fusd_file_t *fusd_file;
  571. /* I check this just in case, shouldn't be necessary. */
  572. GET_FUSD_FILE_AND_DEV(fusd_file_arg, fusd_file, fusd_dev);
  573. /* make sure message is sane */
  574. if ((fusd_msg->data == NULL) != (fusd_msg->datalen == 0)) {
  575. RDEBUG(2, "fusd_fops_call: data pointer and datalen mismatch");
  576. return -EINVAL;
  577. }
  578. /* fill the rest of the structure */
  579. fusd_msg->parm.fops_msg.pid = current->pid;
  580. fusd_msg->parm.fops_msg.uid = current->uid;
  581. fusd_msg->parm.fops_msg.gid = current->gid;
  582. fusd_msg->parm.fops_msg.flags = fusd_file->file->f_flags;
  583. fusd_msg->parm.fops_msg.offset = fusd_file->file->f_pos;
  584. fusd_msg->parm.fops_msg.device_info = fusd_dev->private_data;
  585. fusd_msg->parm.fops_msg.private_info = fusd_file->private_data;
  586. fusd_msg->parm.fops_msg.fusd_file = fusd_file;
  587. fusd_msg->parm.fops_msg.transid = atomic_inc_and_ret(&last_transid);
  588. /* set up certain state depending on if we expect a reply */
  589. switch (fusd_msg->cmd) {
  590. case FUSD_FOPS_CALL: /* common case */
  591. fusd_msg->parm.fops_msg.hint = fusd_file->index;
  592. break;
  593. case FUSD_FOPS_CALL_DROPREPLY:
  594. /* nothing needed */
  595. break;
  596. case FUSD_FOPS_NONBLOCK:
  597. fusd_msg->parm.fops_msg.hint = fusd_file->index;
  598. break;
  599. default:
  600. RDEBUG(0, "whoa - fusd_fops_call_send got msg with unknown cmd!");
  601. break;
  602. }
  603. if(transaction != NULL)
  604. {
  605. int retval;
  606. retval = fusd_add_transaction(fusd_file, fusd_msg->parm.fops_msg.transid, fusd_msg->subcmd,
  607. fusd_msg->parm.fops_msg.length, transaction);
  608. if(retval < 0)
  609. return retval;
  610. }
  611. /* now add the message to the device's outgoing queue! */
  612. return send_to_dev(fusd_dev, fusd_msg, 0);
  613. /* bizarre errors go straight here */
  614. invalid_dev:
  615. invalid_file:
  616. RDEBUG(0, "fusd_fops_call: got invalid device or file!!!!");
  617. return -EPIPE;
  618. }
  619. /*
  620. * fusd_fops_call_wait: wait for a driver to reply to a message
  621. *
  622. * NOTE - we are already holding the lock on fusd_file_arg when this
  623. * function is called, but NOT the lock on the fusd_dev
  624. */
  625. STATIC int fusd_fops_call_wait(fusd_file_t *fusd_file_arg,
  626. fusd_msg_t **fusd_msg_reply, struct fusd_transaction* transaction)
  627. {
  628. fusd_dev_t *fusd_dev;
  629. fusd_file_t *fusd_file;
  630. int retval;
  631. /* I check this just in case, shouldn't be necessary. */
  632. GET_FUSD_FILE_AND_DEV(fusd_file_arg, fusd_file, fusd_dev);
  633. /* initialize first to tell callers there is no reply (yet) */
  634. if (fusd_msg_reply != NULL)
  635. *fusd_msg_reply = NULL;
  636. /*
  637. * Now, lock the device, check for an incoming message, and sleep if
  638. * there is not a message already waiting for us. Note that we are
  639. * unrolling the interruptible_sleep_on, as in the kernel's
  640. * fs/pipe.c, to avoid race conditions between checking for the
  641. * sleep condition and sleeping.
  642. */
  643. LOCK_FUSD_DEV(fusd_dev);
  644. while (transaction->msg_in == NULL) {
  645. DECLARE_WAITQUEUE(wait, current);
  646. RDEBUG(10, "pid %d blocking on transid %ld", current->pid, transaction->transid);
  647. current->state = TASK_INTERRUPTIBLE;
  648. add_wait_queue(&fusd_file->file_wait, &wait);
  649. UNLOCK_FUSD_DEV(fusd_dev);
  650. UNLOCK_FUSD_FILE(fusd_file);
  651. schedule();
  652. remove_wait_queue(&fusd_file->file_wait, &wait);
  653. current->state = TASK_RUNNING;
  654. /*
  655. * If we woke up due to a signal -- and not due to a reply message
  656. * coming in -- then we are in some trouble. The driver is already
  657. * processing the request and might have changed some state that is
  658. * hard to roll back. So, we'll tell the process to restart the
  659. * system call, and come back to this point when the system call is
  660. * restarted. We need to remember the PID to avoid confusion in
  661. * case there is another process holding this file descriptor that
  662. * is also trying to make a call.
  663. */
  664. if (signal_pending(current)) {
  665. RDEBUG(5, "blocked pid %d got a signal; sending -ERESTARTSYS",
  666. current->pid);
  667. LOCK_FUSD_FILE(fusd_file);
  668. return -ERESTARTSYS;
  669. }
  670. LOCK_FUSD_FILE(fusd_file);
  671. /* re-lock the device, so we can do our msg_in check again */
  672. LOCK_FUSD_DEV(fusd_dev);
  673. }
  674. UNLOCK_FUSD_DEV(fusd_dev);
  675. /* ok - at this point we are awake due to a message received. */
  676. if (transaction->msg_in->cmd != FUSD_FOPS_REPLY ||
  677. transaction->msg_in->subcmd != transaction->subcmd ||
  678. transaction->msg_in->parm.fops_msg.transid != transaction->transid ||
  679. transaction->msg_in->parm.fops_msg.fusd_file != fusd_file) {
  680. RDEBUG(2, "fusd_fops_call: invalid reply!");
  681. goto invalid_reply;
  682. }
  683. /* copy metadata back from userspace */
  684. fusd_file->file->f_flags = transaction->msg_in->parm.fops_msg.flags;
  685. fusd_file->private_data = transaction->msg_in->parm.fops_msg.private_info;
  686. /* note, changes to device_info are NO LONGER honored here */
  687. /* if everything's okay, return the return value. if caller is
  688. * willing to take responsibility for freeing the message itself, we
  689. * return the message too. */
  690. retval = transaction->msg_in->parm.fops_msg.retval;
  691. if (fusd_msg_reply != NULL) {
  692. /* NOW TRANSFERRING RESPONSIBILITY FOR FREEING THIS DATA TO THE CALLER */
  693. *fusd_msg_reply = transaction->msg_in;
  694. transaction->msg_in = NULL;
  695. } else {
  696. /* free the message ourselves */
  697. free_fusd_msg(&transaction->msg_in);
  698. }
  699. /* success */
  700. fusd_cleanup_transaction(fusd_file, transaction);
  701. return retval;
  702. invalid_reply:
  703. fusd_cleanup_transaction(fusd_file, transaction);
  704. return -EPIPE;
  705. /* bizarre errors go straight here */
  706. invalid_dev:
  707. invalid_file:
  708. RDEBUG(0, "fusd_fops_call: got invalid device or file!!!!");
  709. return -EPIPE;
  710. zombie_dev:
  711. RDEBUG(2, "fusd_fops_call: %s zombified while waiting for reply",
  712. NAME(fusd_dev));
  713. return -EPIPE;
  714. }
  715. /* fusd client system call handlers should call this after they call
  716. * fops_call, to destroy the message that was returned to them. */
  717. STATIC void fusd_transaction_done(struct fusd_transaction *transaction)
  718. {
  719. transaction->transid = -1;
  720. transaction->pid = 0;
  721. }
  722. /********* Functions for opening a FUSD device *******************/
  723. /*
  724. * The process of having a client open a FUSD device is surprisingly
  725. * tricky -- perhaps the most complex piece of FUSD (or, a close
  726. * second to poll_diffs). Race conditions are rampant here.
  727. *
  728. * The main problem is that there is a race between clients trying to
  729. * open the FUSD device, and providers unregistering it (e.g., the
  730. * driver dying). If the device-unregister callback starts, and is
  731. * scheduled out after it locks the fusd device but before it
  732. * unregisters the device with devfs, the open callback might be
  733. * invoked in this interval. This means the client will down() on a
  734. * semaphore that is about to be freed when the device is destroyed.
  735. *
  736. * The only way to fix this, as far as I can tell, is for device
  737. * registration and unregistration to both share a global lock; the
  738. * client checks its 'private_data' pointer to make sure it's on the
  739. * list of valid devices. If so, it sets a flag (open_in_progress)
  740. * which means "Don't free this device yet!". Then, it releases the
  741. * global lock, grabs the device lock, and tries to add itself as a
  742. * "file" to the device array. It is then safe to decrement
  743. * open_in_progress, because being a member of the file array will
  744. * guarantee that the device will zombify instead of being freed.
  745. *
  746. * Another gotcha: To avoid infinitely dining with philosophers, the
  747. * global lock (fusd_devlist_sem) should always be acquired AFTER a
  748. * fusd device is locked. The code path that frees devices acquires
  749. * the device lock FIRST, so the code here must do the same.
  750. *
  751. * Because of the complexity of opening a file, I've broken it up into
  752. * multiple sub-functions.
  753. */
  754. /*
  755. * fusd_dev_is_valid: If a fusd device is valid, returns 1, and will have
  756. * set the "open_in_progress" flag on the device.
  757. */
  758. int fusd_dev_is_valid(fusd_dev_t *fusd_dev)
  759. {
  760. struct list_head *tmp;
  761. int dev_found = 0;
  762. /* The first thing we must do is acquire the global lock on the
  763. * device list, and make sure this device is valid; if so, mark it
  764. * as being "in use". If we don't do this, there's a race: after we
  765. * enter this function, the device may be unregistered. */
  766. down(&fusd_devlist_sem);
  767. list_for_each(tmp, &fusd_devlist_head) {
  768. fusd_dev_t *d = list_entry(tmp, fusd_dev_t, devlist);
  769. if (d == fusd_dev && d->magic == FUSD_DEV_MAGIC && !ZOMBIE(d)) {
  770. dev_found = 1;
  771. break;
  772. }
  773. }
  774. /* A device will not be deallocated when this counter is >0 */
  775. if (dev_found)
  776. fusd_dev->open_in_progress++;
  777. up(&fusd_devlist_sem);
  778. return dev_found;
  779. }
  780. int fusd_dev_add_file(struct file *file, fusd_dev_t *fusd_dev, fusd_file_t **fusd_file_ret)
  781. {
  782. fusd_file_t *fusd_file;
  783. int i;
  784. /* Make sure the device didn't become a zombie while we were waiting
  785. * for the device lock */
  786. if (ZOMBIE(fusd_dev))
  787. return -ENOENT;
  788. /* this shouldn't happen. maybe i'm insane, but i check anyway. */
  789. for (i = 0; i < fusd_dev->num_files; i++)
  790. if (fusd_dev->files[i]->file == file) {
  791. RDEBUG(1, "warning: fusd_client_open got open for already-open file!?");
  792. return -EIO;
  793. }
  794. /* You can't open your own file! Return -EDEADLOCK if someone tries to.
  795. *
  796. * XXX - TODO - FIXME - This should eventually be more general
  797. * deadlock detection of arbitrary length cycles */
  798. if (current->pid == fusd_dev->pid) {
  799. RDEBUG(3, "pid %d tried to open its own device (/dev/%s)",
  800. fusd_dev->pid, NAME(fusd_dev));
  801. return -EDEADLOCK;
  802. }
  803. /* make more space in the file array if we need it */
  804. if (fusd_dev->num_files == fusd_dev->array_size &&
  805. fusd_dev->array_size < MAX_FILEARRAY_SIZE)
  806. fusd_dev_adjsize(fusd_dev);
  807. /* make sure we have room... adjsize may have failed */
  808. if (fusd_dev->num_files >= fusd_dev->array_size) {
  809. RDEBUG(1, "/dev/%s out of state space for open files!", NAME(fusd_dev));
  810. return -ENOMEM;
  811. }
  812. /* create state for this file */
  813. if ((fusd_file = KMALLOC(sizeof(fusd_file_t), GFP_KERNEL)) == NULL) {
  814. RDEBUG(1, "yikes! kernel can't allocate memory");
  815. return -ENOMEM;
  816. }
  817. memset(fusd_file, 0, sizeof(fusd_file_t));
  818. init_waitqueue_head(&fusd_file->file_wait);
  819. init_waitqueue_head(&fusd_file->poll_wait);
  820. INIT_LIST_HEAD(&fusd_file->transactions);
  821. init_MUTEX(&fusd_file->file_sem);
  822. init_MUTEX(&fusd_file->transactions_sem);
  823. fusd_file->last_poll_sent = -1;
  824. fusd_file->magic = FUSD_FILE_MAGIC;
  825. fusd_file->fusd_dev = fusd_dev;
  826. fusd_file->fusd_dev_version = fusd_dev->version;
  827. fusd_file->file = file;
  828. /* add this file to the list of files managed by the device */
  829. fusd_file->index = fusd_dev->num_files++;
  830. fusd_dev->files[fusd_file->index] = fusd_file;
  831. /* store the pointer to this file with the kernel */
  832. file->private_data = fusd_file;
  833. *fusd_file_ret = fusd_file;
  834. /* success! */
  835. return 0;
  836. }
  837. STATIC struct fusd_dev_t_s* find_user_device(int dev_id)
  838. {
  839. struct list_head* entry;
  840. down(&fusd_devlist_sem);
  841. list_for_each(entry, &fusd_devlist_head)
  842. {
  843. fusd_dev_t *d = list_entry(entry, fusd_dev_t, devlist);
  844. if(d->dev_id == dev_id)
  845. {
  846. up(&fusd_devlist_sem);
  847. return d;
  848. }
  849. }
  850. up(&fusd_devlist_sem);
  851. return NULL;
  852. }
  853. /*
  854. * A client has called open() has been called on a registered device.
  855. * See comment higher up for detailed notes on this function.
  856. */
  857. STATIC int fusd_client_open(struct inode *inode, struct file *file)
  858. {
  859. int retval;
  860. int device_freed = 0;
  861. fusd_dev_t *fusd_dev = find_user_device(inode->i_rdev);
  862. fusd_file_t *fusd_file;
  863. fusd_msg_t fusd_msg;
  864. struct fusd_transaction* transaction;
  865. /* If the device wasn't on our valid list, stop here. */
  866. if (!fusd_dev_is_valid(fusd_dev))
  867. return -ENOENT;
  868. /* fusd_dev->open_in_progress now set */
  869. /* Lock the fusd device. Note, when we finally do acquire the lock,
  870. * the device might be a zombie (driver disappeared). */
  871. RAWLOCK_FUSD_DEV(fusd_dev);
  872. RDEBUG(3, "got an open for /dev/%s (owned by pid %d) from pid %d",
  873. NAME(fusd_dev), fusd_dev->pid, current->pid);
  874. /* Try to add ourselves to the device's file list. If retval==0, we
  875. are now part of the file array. */
  876. retval = fusd_dev_add_file(file, fusd_dev, &fusd_file);
  877. /*
  878. * It is now safe to unset the open_in_progress flag. Either:
  879. * 1) We are part of the file array, so dev won't be freed, or;
  880. * 2) Something failed, so we are returning a failure now and no
  881. * longer need the device.
  882. * Note, open_in_progress must be protected by the global sem, not
  883. * the device lock, due to the access of it in fusd_dev_is_valid().
  884. */
  885. down(&fusd_devlist_sem);
  886. fusd_dev->open_in_progress--;
  887. up(&fusd_devlist_sem);
  888. /* If adding ourselves to the device list failed, give up. Possibly
  889. * free the device if it was a zombie and waiting for us to complete
  890. * our open. */
  891. if (retval < 0) {
  892. if (!maybe_free_fusd_dev(fusd_dev))
  893. UNLOCK_FUSD_DEV(fusd_dev);
  894. return retval;
  895. }
  896. /* send message to userspace and get retval */
  897. init_fusd_msg(&fusd_msg);
  898. fusd_msg.subcmd = FUSD_OPEN;
  899. /* send message to userspace and get the reply. Device can't be
  900. * locked during that operation. */
  901. UNLOCK_FUSD_DEV(fusd_dev);
  902. retval = fusd_fops_call_send(fusd_file, &fusd_msg, &transaction);
  903. if (retval >= 0)
  904. retval = fusd_fops_call_wait(fusd_file, NULL, transaction);
  905. RAWLOCK_FUSD_DEV(fusd_dev);
  906. /* If the device zombified (while we were waiting to reacquire the
  907. * lock)... consider that a failure */
  908. if (ZOMBIE(fusd_dev))
  909. retval = -ENOENT;
  910. /* if retval is negative, throw away state... the file open failed */
  911. if (retval < 0) {
  912. RDEBUG(3, "...open failed for /dev/%s (owned by pid %d) from pid %d",
  913. NAME(fusd_dev), fusd_dev->pid, current->pid);
  914. device_freed = free_fusd_file(fusd_dev, fusd_file);
  915. }
  916. /* Now unlock the device, if it still exists. (It may have been
  917. * freed if the open failed, and we were the last outstanding
  918. * request for it.) */
  919. if (!device_freed)
  920. UNLOCK_FUSD_DEV(fusd_dev);
  921. return retval;
  922. }
  923. /* close() has been called on a registered device. like
  924. * fusd_client_open, we must lock the entire device. */
  925. STATIC int fusd_client_release(struct inode *inode, struct file *file)
  926. {
  927. int retval;
  928. fusd_file_t *fusd_file;
  929. fusd_dev_t *fusd_dev;
  930. fusd_msg_t fusd_msg;
  931. struct fusd_transaction* transaction;
  932. GET_FUSD_FILE_AND_DEV(file->private_data, fusd_file, fusd_dev);
  933. LOCK_FUSD_FILE(fusd_file);
  934. RDEBUG(3, "got a close on /dev/%s (owned by pid %d) from pid %d",
  935. NAME(fusd_dev), fusd_dev->pid, current->pid);
  936. /* Tell the driver that the file closed, if it still exists. */
  937. init_fusd_msg(&fusd_msg);
  938. fusd_msg.subcmd = FUSD_CLOSE;
  939. retval = fusd_fops_call_send(fusd_file, &fusd_msg, &transaction);
  940. RDEBUG(5, "fusd_client_release: send returned %d", retval);
  941. if (retval >= 0)
  942. retval = fusd_fops_call_wait(fusd_file, NULL, transaction);
  943. RDEBUG(5, "fusd_client_release: call_wait %d", retval);
  944. /* delete the file off the device's file-list, and free it. note
  945. * that device may be a zombie right now and may be freed when we
  946. * come back from free_fusd_file. we only release the lock if the
  947. * device still exists. */
  948. RAWLOCK_FUSD_DEV(fusd_dev);
  949. if (!free_fusd_file(fusd_dev, fusd_file)) {
  950. UNLOCK_FUSD_DEV(fusd_dev);
  951. }
  952. return retval;
  953. invalid_dev:
  954. invalid_file:
  955. RDEBUG(1, "got a close on client file from pid %d, INVALID DEVICE!",
  956. current->pid);
  957. return -EPIPE;
  958. }
  959. STATIC ssize_t fusd_client_read(struct file *file , char *buf,
  960. size_t count, loff_t *offset)
  961. {
  962. fusd_dev_t *fusd_dev;
  963. fusd_file_t *fusd_file;
  964. struct fusd_transaction* transaction;
  965. fusd_msg_t fusd_msg, *reply = NULL;
  966. int retval = -EPIPE;
  967. GET_FUSD_FILE_AND_DEV(file->private_data, fusd_file, fusd_dev);
  968. LOCK_FUSD_FILE(fusd_file);
  969. RDEBUG(3, "got a read on /dev/%s (owned by pid %d) from pid %d",
  970. NAME(fusd_dev), fusd_dev->pid, current->pid);
  971. transaction = fusd_find_incomplete_transaction(fusd_file, FUSD_READ);
  972. if (transaction && transaction->size > count)
  973. {
  974. RDEBUG(3, "Incomplete I/O transaction %ld thrown out, as the transaction's size of %d bytes was greater than "
  975. "the retry's size of %d bytes", transaction->transid, transaction->size, (int)count);
  976. fusd_cleanup_transaction(fusd_file, transaction);
  977. transaction = NULL;
  978. }
  979. if(transaction == NULL)
  980. {
  981. /* make sure we aren't trying to read too big of a buffer */
  982. if (count > MAX_RW_SIZE)
  983. count = MAX_RW_SIZE;
  984. /* send the message */
  985. init_fusd_msg(&fusd_msg);
  986. fusd_msg.subcmd = FUSD_READ;
  987. fusd_msg.parm.fops_msg.length = count;
  988. /* send message to userspace */
  989. if ((retval = fusd_fops_call_send(fusd_file, &fusd_msg, &transaction)) < 0)
  990. goto done;
  991. }
  992. /* and wait for the reply */
  993. /* todo: store and retrieve the transid from the interrupted messsage */
  994. retval = fusd_fops_call_wait(fusd_file, &reply, transaction);
  995. /* return immediately in case of error */
  996. if (retval < 0 || reply == NULL)
  997. goto done;
  998. /* adjust the reval if the retval indicates a larger read than the
  999. * data that was actually provided */
  1000. if (reply->datalen != retval) {
  1001. RDEBUG(1, "warning: /dev/%s driver (pid %d) claimed it returned %d bytes "
  1002. "on read but actually returned %d",
  1003. NAME(fusd_dev), fusd_dev->pid, retval, reply->datalen);
  1004. retval = reply->datalen;
  1005. }
  1006. /* adjust if the device driver gave us more data than the user asked for
  1007. * (bad! bad! why is the driver broken???) */
  1008. if (retval > count) {
  1009. RDEBUG(1, "warning: /dev/%s driver (pid %d) returned %d bytes on read but "
  1010. "the user only asked for %d",
  1011. NAME(fusd_dev), fusd_dev->pid, retval, (int) count);
  1012. retval = count;
  1013. }
  1014. /* copy the offset back from the message */
  1015. *offset = reply->parm.fops_msg.offset;
  1016. /* IFF return value indicates data present, copy it back */
  1017. if (retval > 0) {
  1018. if (copy_to_user(buf, reply->data, retval)) {
  1019. retval = -EFAULT;
  1020. goto done;
  1021. }
  1022. }
  1023. done:
  1024. /* clear the readable bit of our cached poll state */
  1025. fusd_file->cached_poll_state &= ~(FUSD_NOTIFY_INPUT);
  1026. free_fusd_msg(&reply);
  1027. UNLOCK_FUSD_FILE(fusd_file);
  1028. return retval;
  1029. invalid_file:
  1030. invalid_dev:
  1031. RDEBUG(3, "got a read on client file from pid %d, driver has disappeared",
  1032. current->pid);
  1033. return -EPIPE;
  1034. }
  1035. STATIC int fusd_add_transaction(fusd_file_t *fusd_file, int transid, int subcmd, int size, struct fusd_transaction** out_transaction)
  1036. {
  1037. struct fusd_transaction* transaction = (struct fusd_transaction*) KMALLOC(sizeof(struct fusd_transaction), GFP_KERNEL);
  1038. if(transaction == NULL)
  1039. return -ENOMEM;
  1040. transaction->msg_in = NULL;
  1041. transaction->transid = transid;
  1042. transaction->subcmd = subcmd;
  1043. transaction->pid = current->pid;
  1044. transaction->size = size;
  1045. down(&fusd_file->transactions_sem);
  1046. list_add_tail(&transaction->list, &fusd_file->transactions);
  1047. up(&fusd_file->transactions_sem);
  1048. if(out_transaction != NULL)
  1049. *out_transaction = transaction;
  1050. return 0;
  1051. }
  1052. STATIC void fusd_cleanup_transaction(fusd_file_t *fusd_file, struct fusd_transaction* transaction)
  1053. {
  1054. free_fusd_msg(&transaction->msg_in);
  1055. fusd_remove_transaction(fusd_file, transaction);
  1056. }
  1057. STATIC void fusd_remove_transaction(fusd_file_t *fusd_file, struct fusd_transaction* transaction)
  1058. {
  1059. down(&fusd_file->transactions_sem);
  1060. list_del(&transaction->list);
  1061. up(&fusd_file->transactions_sem);
  1062. KFREE(transaction);
  1063. }
  1064. STATIC struct fusd_transaction* fusd_find_transaction(fusd_file_t *fusd_file, int transid)
  1065. {
  1066. struct list_head* i;
  1067. down(&fusd_file->transactions_sem);
  1068. list_for_each(i, &fusd_file->transactions)
  1069. {
  1070. struct fusd_transaction* transaction = list_entry(i, struct fusd_transaction, list);
  1071. if(transaction->transid == transid)
  1072. {
  1073. up(&fusd_file->transactions_sem);
  1074. return transaction;
  1075. }
  1076. }
  1077. up(&fusd_file->transactions_sem);
  1078. return NULL;
  1079. }
  1080. STATIC struct fusd_transaction* fusd_find_transaction_by_pid(fusd_file_t *fusd_file, int pid)
  1081. {
  1082. struct list_head* i;
  1083. down(&fusd_file->transactions_sem);
  1084. list_for_each(i, &fusd_file->transactions)
  1085. {
  1086. struct fusd_transaction* transaction = list_entry(i, struct fusd_transaction, list);
  1087. if(transaction->pid == pid)
  1088. {
  1089. up(&fusd_file->transactions_sem);
  1090. return transaction;
  1091. }
  1092. }
  1093. up(&fusd_file->transactions_sem);
  1094. return NULL;
  1095. }
  1096. STATIC ssize_t fusd_client_write(struct file *file,
  1097. const char *buffer,
  1098. size_t length,
  1099. loff_t *offset)
  1100. {
  1101. fusd_dev_t *fusd_dev;
  1102. fusd_file_t *fusd_file;
  1103. fusd_msg_t fusd_msg;
  1104. fusd_msg_t *reply = NULL;
  1105. int retval = -EPIPE;
  1106. struct fusd_transaction* transaction;
  1107. GET_FUSD_FILE_AND_DEV(file->private_data, fusd_file, fusd_dev);
  1108. LOCK_FUSD_FILE(fusd_file);
  1109. RDEBUG(3, "got a write on /dev/%s (owned by pid %d) from pid %d",
  1110. NAME(fusd_dev), fusd_dev->pid, current->pid);
  1111. transaction = fusd_find_incomplete_transaction(fusd_file, FUSD_WRITE);
  1112. if (transaction && transaction->size == length)
  1113. {
  1114. RDEBUG(2, "Incomplete I/O transaction %ld thrown out, as the transaction's size of %d bytes was not equal to "
  1115. "the retry's size of %d bytes", transaction->transid, transaction->size, (int) length);
  1116. fusd_cleanup_transaction(fusd_file, transaction);
  1117. transaction = NULL;
  1118. }
  1119. if(transaction == NULL)
  1120. {
  1121. if (length < 0) {
  1122. RDEBUG(2, "fusd_client_write: got invalid length %d", (int) length);
  1123. retval = -EINVAL;
  1124. goto done;
  1125. }
  1126. if (length > MAX_RW_SIZE)
  1127. length = MAX_RW_SIZE;
  1128. init_fusd_msg(&fusd_msg);
  1129. /* sigh.. i guess zero length writes should be legal */
  1130. if (length > 0) {
  1131. if ((fusd_msg.data = VMALLOC(length)) == NULL) {
  1132. retval = -ENOMEM;
  1133. goto done;
  1134. }
  1135. if (copy_from_user(fusd_msg.data, buffer, length)) {
  1136. retval = -EFAULT;
  1137. goto done;
  1138. }
  1139. fusd_msg.datalen = length;
  1140. }
  1141. fusd_msg.subcmd = FUSD_WRITE;
  1142. fusd_msg.parm.fops_msg.length = length;
  1143. if ((retval = fusd_fops_call_send(fusd_file, &fusd_msg, &transaction)) < 0)
  1144. goto done;
  1145. }
  1146. /* todo: fix transid on restart */
  1147. retval = fusd_fops_call_wait(fusd_file, &reply, transaction);
  1148. if (retval < 0 || reply == NULL)
  1149. goto done;
  1150. /* drivers should not write more bytes than they were asked to! */
  1151. if (retval > length) {
  1152. RDEBUG(1, "warning: /dev/%s driver (pid %d) returned %d bytes on write; "
  1153. "the user only wanted %d",
  1154. NAME(fusd_dev), fusd_dev->pid, retval, (int) length);
  1155. retval = length;
  1156. }
  1157. *offset = reply->parm.fops_msg.offset;
  1158. /* all done! */
  1159. done:
  1160. /* clear the writable bit of our cached poll state */
  1161. fusd_file->cached_poll_state &= ~(FUSD_NOTIFY_OUTPUT);
  1162. free_fusd_msg(&reply);
  1163. UNLOCK_FUSD_FILE(fusd_file);
  1164. return retval;
  1165. invalid_file:
  1166. invalid_dev:
  1167. RDEBUG(3, "got a read on client file from pid %d, driver has disappeared",
  1168. current->pid);
  1169. return -EPIPE;
  1170. }
  1171. STATIC int fusd_client_ioctl(struct inode *inode, struct file *file,
  1172. unsigned int cmd, unsigned long arg)
  1173. {
  1174. fusd_dev_t *fusd_dev;
  1175. fusd_file_t *fusd_file;
  1176. fusd_msg_t fusd_msg, *reply = NULL;
  1177. int retval = -EPIPE, dir, length;
  1178. struct fusd_transaction* transaction;
  1179. GET_FUSD_FILE_AND_DEV(file->private_data, fusd_file, fusd_dev);
  1180. LOCK_FUSD_FILE(fusd_file);
  1181. RDEBUG(3, "got an ioctl on /dev/%s (owned by pid %d) from pid %d",
  1182. NAME(fusd_dev), fusd_dev->pid, current->pid);
  1183. dir = _IOC_DIR(cmd);
  1184. length = _IOC_SIZE(cmd);
  1185. transaction = fusd_find_incomplete_transaction(fusd_file, FUSD_IOCTL);
  1186. // todo: Check to make sure the transaction is for the same IOCTL
  1187. if(transaction == NULL)
  1188. {
  1189. /* if we're trying to read or write, make sure length is sane */
  1190. if ((dir & (_IOC_WRITE | _IOC_READ)) &&
  1191. (length <= 0 || length > MAX_RW_SIZE))
  1192. {
  1193. RDEBUG(2, "client ioctl got crazy IOC_SIZE of %d", length);
  1194. retval = -EINVAL;
  1195. goto done;
  1196. }
  1197. /* fill the struct */
  1198. init_fusd_msg(&fusd_msg);
  1199. fusd_msg.subcmd = FUSD_IOCTL;
  1200. fusd_msg.parm.fops_msg.cmd = cmd;
  1201. fusd_msg.parm.fops_msg.arg.arg = arg;
  1202. /* get the data if user is trying to write to the driver */
  1203. if (dir & _IOC_WRITE) {
  1204. if ((fusd_msg.data = VMALLOC(length)) == NULL) {
  1205. RDEBUG(2, "can't vmalloc for client ioctl!");
  1206. retval = -ENOMEM;
  1207. goto done;
  1208. }
  1209. if (copy_from_user(fusd_msg.data, (void *) arg, length)) {
  1210. retval = -EFAULT;
  1211. goto done;
  1212. }
  1213. fusd_msg.datalen = length;
  1214. }
  1215. /* send request to the driver */
  1216. if ((retval = fusd_fops_call_send(fusd_file, &fusd_msg, &transaction)) < 0)
  1217. goto done;
  1218. }
  1219. /* get the response */
  1220. /* todo: fix transid on restart */
  1221. if ((retval = fusd_fops_call_wait(fusd_file, &reply, transaction)) < 0 || reply == NULL)
  1222. goto done;
  1223. /* if user is trying to read from the driver, copy data back */
  1224. if (dir & _IOC_READ) {
  1225. if (reply->data == NULL || reply->datalen != length) {
  1226. RDEBUG(2, "client_ioctl read reply with screwy data (%d, %d)",
  1227. reply->datalen, length);
  1228. retval = -EIO;
  1229. goto done;
  1230. }
  1231. if (copy_to_user((void *)arg, reply->data, length)) {
  1232. retval = -EFAULT;
  1233. goto done;
  1234. }
  1235. }
  1236. /* all done! */
  1237. done:
  1238. free_fusd_msg(&reply);
  1239. UNLOCK_FUSD_FILE(fusd_file);
  1240. return retval;
  1241. invalid_file:
  1242. invalid_dev:
  1243. RDEBUG(3, "got a read on client file from pid %d, driver has disappeared",
  1244. current->pid);
  1245. return -EPIPE;
  1246. }
  1247. static void fusd_client_mm_open(struct vm_area_struct * vma);
  1248. static void fusd_client_mm_close(struct vm_area_struct * vma);
  1249. static struct page* fusd_client_nopage(struct vm_area_struct* vma, unsigned long address, int* type);
  1250. static struct vm_operations_struct fusd_remap_vm_ops =
  1251. {
  1252. open: fusd_client_mm_open,
  1253. close: fusd_client_mm_close,
  1254. nopage: fusd_client_nopage,
  1255. };
  1256. struct fusd_mmap_instance
  1257. {
  1258. fusd_dev_t* fusd_dev;
  1259. fusd_file_t* fusd_file;
  1260. unsigned long addr;
  1261. int size;
  1262. atomic_t refcount;
  1263. };
  1264. static void fusd_client_mm_open(struct vm_area_struct * vma)
  1265. {
  1266. struct fusd_mmap_instance* mmap_instance = (struct fusd_mmap_instance*) vma->vm_private_data;
  1267. atomic_inc(&mmap_instance->refcount);
  1268. }
  1269. static void fusd_client_mm_close(struct vm_area_struct * vma)
  1270. {
  1271. struct fusd_mmap_instance* mmap_instance = (struct fusd_mmap_instance*) vma->vm_private_data;
  1272. if(atomic_dec_and_test(&mmap_instance->refcount))
  1273. {
  1274. KFREE(mmap_instance);
  1275. }
  1276. }
  1277. static int fusd_client_mmap(struct file *file, struct vm_area_struct * vma)
  1278. {
  1279. fusd_dev_t *fusd_dev;
  1280. fusd_file_t *fusd_file;
  1281. struct fusd_transaction* transaction;
  1282. fusd_msg_t fusd_msg, *reply = NULL;
  1283. int retval = -EPIPE;
  1284. struct fusd_mmap_instance* mmap_instance;
  1285. GET_FUSD_FILE_AND_DEV(file->private_data, fusd_file, fusd_dev);
  1286. LOCK_FUSD_FILE(fusd_file);
  1287. RDEBUG(3, "got a mmap on /dev/%s (owned by pid %d) from pid %d",
  1288. NAME(fusd_dev), fusd_dev->pid, current->pid);
  1289. transaction = fusd_find_incomplete_transaction(fusd_file, FUSD_MMAP);
  1290. if(transaction == NULL)
  1291. {
  1292. /* send the message */
  1293. init_fusd_msg(&fusd_msg);
  1294. fusd_msg.subcmd = FUSD_MMAP;
  1295. fusd_msg.parm.fops_msg.offset = vma->vm_pgoff << PAGE_SHIFT;
  1296. fusd_msg.parm.fops_msg.flags = vma->vm_flags;
  1297. fusd_msg.parm.fops_msg.length = vma->vm_end - vma->vm_start;
  1298. /* send message to userspace */
  1299. if ((retval = fusd_fops_call_send(fusd_file, &fusd_msg, &transaction)) < 0)
  1300. goto done;
  1301. }
  1302. /* and wait for the reply */
  1303. /* todo: store and retrieve the transid from the interrupted messsage */
  1304. retval = fusd_fops_call_wait(fusd_file, &reply, transaction);
  1305. mmap_instance =
  1306. (struct fusd_mmap_instance*) KMALLOC(sizeof(struct fusd_mmap_instance), GFP_KERNEL);
  1307. // todo: free this thing at some point
  1308. mmap_instance->fusd_dev = fusd_dev;
  1309. mmap_instance->fusd_file = fusd_file;
  1310. mmap_instance->addr = reply->parm.fops_msg.arg.arg;
  1311. mmap_instance->size = reply->parm.fops_msg.length;
  1312. atomic_set(&mmap_instance->refcount, 0);
  1313. retval = reply->parm.fops_msg.retval;
  1314. vma->vm_private_data = mmap_instance;
  1315. vma->vm_ops = &fusd_remap_vm_ops;
  1316. vma->vm_flags |= VM_RESERVED;
  1317. fusd_client_mm_open(vma);
  1318. done:
  1319. free_fusd_msg(&reply);
  1320. UNLOCK_FUSD_FILE(fusd_file);
  1321. return retval;
  1322. invalid_file:
  1323. invalid_dev:
  1324. RDEBUG(3, "got a mmap on client file from pid %d, driver has disappeared",
  1325. current->pid);
  1326. return -EPIPE;
  1327. }
  1328. static struct page* fusd_client_nopage(struct vm_area_struct* vma, unsigned long address,
  1329. int* type)
  1330. {
  1331. struct fusd_mmap_instance* mmap_instance = (struct fusd_mmap_instance*) vma->vm_private_data;
  1332. unsigned long offset;
  1333. struct page *page = NOPAGE_SIGBUS;
  1334. int result;
  1335. offset = (address - vma->vm_start) + (vma->vm_pgoff << PAGE_SHIFT);
  1336. // todo: worry about size
  1337. if(offset > mmap_instance->size)
  1338. goto out;
  1339. down_read(&mmap_instance->fusd_dev->task->mm->mmap_sem);
  1340. result = get_user_pages(mmap_instance->fusd_dev->task, mmap_instance->fusd_dev->task->mm, mmap_instance->addr + offset, 1, 1, 0, &page, NULL);
  1341. up_read(&mmap_instance->fusd_dev->task->mm->mmap_sem);
  1342. if(PageAnon(page))
  1343. {
  1344. RDEBUG(2, "Cannot mmap anonymous pages. Be sure to allocate your shared buffer with MAP_SHARED | MAP_ANONYMOUS");
  1345. return NOPAGE_SIGBUS;
  1346. }
  1347. if(result > 0)
  1348. {
  1349. get_page(page);
  1350. if (type)
  1351. *type = VM_FAULT_MINOR;
  1352. }
  1353. out:
  1354. return page;
  1355. }
  1356. /*
  1357. * The design of poll for clients is a bit subtle.
  1358. *
  1359. * We don't want the select() call itself to block, so we keep a cache
  1360. * of the most recently known state supplied by the driver. The cache
  1361. * is initialized to 0 (meaning: nothing readable/writable).
  1362. *
  1363. * When a poll comes in, we do a non-blocking (!) dispatch of a
  1364. * command telling the driver "This is the state we have cached, reply
  1365. * to this call when the state changes.", and then immediately return
  1366. * the cached state. We tell the kernel's select to sleep on our
  1367. * poll_wait wait queue.
  1368. *
  1369. * When the driver replies, we update our cached info and wake up the
  1370. * wait queue. Waking up the wait queue will most likely immediately
  1371. * effect a poll again, in which case we will reply whatever we just
  1372. * cached from the driver.
  1373. *
  1374. */
  1375. STATIC unsigned int fusd_client_poll(struct file *file, poll_table *wait)
  1376. {
  1377. fusd_dev_t *fusd_dev;
  1378. fusd_file_t *fusd_file;
  1379. int kernel_bits = 0;
  1380. int send_poll = 0;
  1381. GET_FUSD_FILE_AND_DEV(file->private_data, fusd_file, fusd_dev);
  1382. LOCK_FUSD_FILE(fusd_file);
  1383. LOCK_FUSD_DEV(fusd_dev);
  1384. RDEBUG(3, "got a select on /dev/%s (owned by pid %d) from pid %d, cps=%d",
  1385. NAME(fusd_dev), fusd_dev->pid, current->pid,
  1386. fusd_file->cached_poll_state);
  1387. poll_wait(file, &fusd_file->poll_wait, wait);
  1388. /*
  1389. * If our currently cached poll state is not the same as the
  1390. * most-recently-sent polldiff request, then, dispatch a new
  1391. * request. (We DO NOT wait for a reply, but just dispatch the
  1392. * request).
  1393. *
  1394. * Also, don't send a new polldiff if the most recent one resulted
  1395. * in an error.
  1396. */
  1397. if (fusd_file->last_poll_sent != fusd_file->cached_poll_state &&
  1398. fusd_file->cached_poll_state >= 0) {
  1399. RDEBUG(3, "sending polldiff request because lps=%d, cps=%d",
  1400. fusd_file->last_poll_sent, fusd_file->cached_poll_state);
  1401. send_poll = 1;
  1402. fusd_file->last_poll_sent = fusd_file->cached_poll_state;
  1403. }
  1404. /* compute what to return for the state we had cached, converting to
  1405. * bits that have meaning to the kernel */
  1406. if (fusd_file->cached_poll_state > 0) {
  1407. if (fusd_file->cached_poll_state & FUSD_NOTIFY_INPUT)
  1408. kernel_bits |= POLLIN;
  1409. if (fusd_file->cached_poll_state & FUSD_NOTIFY_OUTPUT)
  1410. kernel_bits |= POLLOUT;
  1411. if (fusd_file->cached_poll_state & FUSD_NOTIFY_EXCEPT)
  1412. kernel_bits |= POLLPRI;
  1413. }
  1414. /* Now that we've committed to sending the poll, etc., it should be
  1415. * safe to unlock the device */
  1416. UNLOCK_FUSD_DEV(fusd_dev);
  1417. UNLOCK_FUSD_FILE(fusd_file);
  1418. if (send_poll) {
  1419. fusd_msg_t fusd_msg;
  1420. init_fusd_msg(&fusd_msg);
  1421. fusd_msg.cmd = FUSD_FOPS_NONBLOCK;
  1422. fusd_msg.subcmd = FUSD_POLL_DIFF;
  1423. fusd_msg.parm.fops_msg.cmd = fusd_file->cached_poll_state;
  1424. if (fusd_fops_call_send(fusd_file, &fusd_msg, NULL) < 0) {
  1425. /* If poll dispatched failed, set back to -1 so we try again.
  1426. * Not a race (I think), since sending an *extra* polldiff never
  1427. * hurts anything. */
  1428. fusd_file->last_poll_sent = -1;
  1429. }
  1430. }
  1431. return kernel_bits;
  1432. zombie_dev:
  1433. /* might jump here from LOCK_FUSD_DEV */
  1434. UNLOCK_FUSD_FILE(fusd_file);
  1435. invalid_dev:
  1436. invalid_file:
  1437. RDEBUG(3, "got a select on client file from pid %d, driver has disappeared",
  1438. current->pid);
  1439. return POLLPRI;
  1440. }
  1441. STATIC struct file_operations fusd_client_fops = {
  1442. owner: THIS_MODULE,
  1443. open: fusd_client_open,
  1444. release: fusd_client_release,
  1445. read: fusd_client_read,
  1446. write: fusd_client_write,
  1447. ioctl: fusd_client_ioctl,
  1448. poll: fusd_client_poll,
  1449. mmap: fusd_client_mmap
  1450. };
  1451. /*************************************************************************/
  1452. /*************************************************************************/
  1453. /*************************************************************************/
  1454. STATIC fusd_file_t *find_fusd_reply_file(fusd_dev_t *fusd_dev, fusd_msg_t *msg)
  1455. {
  1456. /* first, try the hint */
  1457. int i = msg->parm.fops_msg.hint;
  1458. if (i >= 0 &&
  1459. i < fusd_dev->num_files &&
  1460. fusd_dev->files[i] == msg->parm.fops_msg.fusd_file)
  1461. {
  1462. RDEBUG(15, "find_fusd_reply_file: hint worked");
  1463. } else {
  1464. /* hint didn't work, fall back to a search of the whole array */
  1465. i = find_fusd_file(fusd_dev, msg->parm.fops_msg.fusd_file);
  1466. RDEBUG(15, "find_fusd_reply_file: hint failed");
  1467. }
  1468. /* we couldn't find anyone waiting for this message! */
  1469. if (i < 0) {
  1470. return NULL;
  1471. } else {
  1472. return fusd_dev->files[i];
  1473. }
  1474. }
  1475. /* Process an incoming reply to a message dispatched by
  1476. * fusd_fops_call. Called by fusd_write when a driver writes to
  1477. * /dev/fusd. */
  1478. STATIC int fusd_fops_reply(fusd_dev_t *fusd_dev, fusd_msg_t *msg)
  1479. {
  1480. fusd_file_t *fusd_file;
  1481. struct fusd_transaction *transaction;
  1482. /* figure out the index of the file we are replying to. usually
  1483. * very fast (uses a hint) */
  1484. if ((fusd_file = find_fusd_reply_file(fusd_dev, msg)) == NULL) {
  1485. RDEBUG(2, "fusd_fops_reply: got a reply on /dev/%s with no connection",
  1486. NAME(fusd_dev));
  1487. goto discard;
  1488. }
  1489. /* make sure this is not an old reply going to an old instance that's gone */
  1490. /* todo: kor fix this */
  1491. /*
  1492. if (fusd_file->fusd_dev_version != fusd_dev->version ||
  1493. msg->parm.fops_msg.transid != fusd_file->transid_outstanding) {
  1494. RDEBUG(2, "fusd_fops_reply: got an old message, discarding");
  1495. goto discard;
  1496. }*/
  1497. transaction = fusd_find_transaction(fusd_file, msg->parm.fops_msg.transid);
  1498. if(transaction == NULL)
  1499. {
  1500. RDEBUG(2, "fusd_fops_reply: No transaction found with transid %ld", msg->parm.fops_msg.transid);
  1501. goto discard;
  1502. }
  1503. RDEBUG(10, "fusd_fops_reply: /dev/%s completed transid %ld (retval %d)",
  1504. NAME(fusd_dev), msg->parm.fops_msg.transid,
  1505. (int) msg->parm.fops_msg.retval);
  1506. transaction->msg_in = msg;
  1507. mb();
  1508. WAKE_UP_INTERRUPTIBLE_SYNC(&fusd_file->file_wait);
  1509. return 0;
  1510. discard:
  1511. if (msg->subcmd == FUSD_OPEN && msg->parm.fops_msg.retval == 0) {
  1512. fusd_forge_close(msg, fusd_dev);
  1513. return 0;
  1514. } else {
  1515. return -EPIPE;
  1516. }
  1517. }
  1518. /* special function to process responses to POLL_DIFF */
  1519. STATIC int fusd_polldiff_reply(fusd_dev_t *fusd_dev, fusd_msg_t *msg)
  1520. {
  1521. fusd_file_t *fusd_file;
  1522. /* figure out the index of the file we are replying to. usually
  1523. * very fast (uses a hint) */
  1524. if ((fusd_file = find_fusd_reply_file(fusd_dev, msg)) == NULL)
  1525. return -EPIPE;
  1526. /* record the poll state returned. convert all negative retvals to -1. */
  1527. if ((fusd_file->cached_poll_state = msg->parm.fops_msg.retval) < 0)
  1528. fusd_file->cached_poll_state = -1;
  1529. RDEBUG(3, "got updated poll state from /dev/%s driver: %d", NAME(fusd_dev),
  1530. fusd_file->cached_poll_state);
  1531. /* since the client has returned the polldiff we sent, set
  1532. * last_poll_sent to -1, so that we'll send a polldiff request on
  1533. * the next select. */
  1534. fusd_file->last_poll_sent = -1;
  1535. /* wake up select's queue so that a new polldiff is generated */
  1536. wake_up_interruptible(&fusd_file->poll_wait);
  1537. return 0;
  1538. }
  1539. STATIC int fusd_register_device(fusd_dev_t *fusd_dev,
  1540. register_msg_t register_msg)
  1541. {
  1542. int error = 0;
  1543. struct list_head *tmp;
  1544. int dev_id;
  1545. /* make sure args are valid */
  1546. if (fusd_dev == NULL) {
  1547. RDEBUG(0, "fusd_register_device: bug in arguments!");
  1548. return -EINVAL;
  1549. }
  1550. /* user can only register one device per instance */
  1551. // if (fusd_dev->handle != 0)
  1552. // return -EBUSY;
  1553. register_msg.name[FUSD_MAX_NAME_LENGTH] = '\0';
  1554. /* make sure that there isn't already a device by this name */
  1555. down(&fusd_devlist_sem);
  1556. list_for_each(tmp, &fusd_devlist_head) {
  1557. fusd_dev_t *d = list_entry(tmp, fusd_dev_t, devlist);
  1558. if (d && d->name && !d->zombie && !strcmp(d->name, register_msg.name)) {
  1559. error = -EEXIST;
  1560. break;
  1561. }
  1562. }
  1563. up(&fusd_devlist_sem);
  1564. if (error)
  1565. return error;
  1566. /* allocate memory for the name, and copy */
  1567. if ((fusd_dev->name = KMALLOC(strlen(register_msg.name)+1, GFP_KERNEL)) == NULL) {
  1568. RDEBUG(1, "yikes! kernel can't allocate memory");
  1569. return -ENOMEM;
  1570. }
  1571. strcpy(fusd_dev->name, register_msg.name);
  1572. /* allocate memory for the class name, and copy */
  1573. if ((fusd_dev->class_name = KMALLOC(strlen(register_msg.clazz)+1, GFP_KERNEL)) == NULL) {
  1574. RDEBUG(1, "yikes! kernel can't allocate memory");
  1575. return -ENOMEM;
  1576. }
  1577. strcpy(fusd_dev->class_name, register_msg.clazz);
  1578. /* allocate memory for the class name, and copy */
  1579. if ((fusd_dev->dev_name = KMALLOC(strlen(register_msg.devname)+1, GFP_KERNEL)) == NULL) {
  1580. RDEBUG(1, "yikes! kernel can't allocate memory");
  1581. return -ENOMEM;
  1582. }
  1583. strcpy(fusd_dev->dev_name, register_msg.devname);
  1584. dev_id = 0;
  1585. if((error = alloc_chrdev_region(&dev_id, 0, 1, fusd_dev->name)) < 0)
  1586. {
  1587. printk(KERN_ERR "alloc_chrdev_region failed status: %d\n", error);
  1588. goto register_failed;
  1589. }
  1590. fusd_dev->dev_id = dev_id;
  1591. fusd_dev->handle = cdev_alloc();
  1592. if(fusd_dev->handle == NULL)
  1593. {
  1594. printk(KERN_ERR "cdev_alloc() failed\n");
  1595. error = -ENOMEM;
  1596. goto register_failed3;
  1597. }
  1598. fusd_dev->handle->owner = THIS_MODULE;
  1599. fusd_dev->handle->ops = &fusd_client_fops;
  1600. kobject_set_name(&fusd_dev->handle->kobj, fusd_dev->name);
  1601. if((error = cdev_add(fusd_dev->handle, dev_id, 1)) < 0)
  1602. {
  1603. printk(KERN_ERR "cdev_add failed status: %d\n", error);
  1604. kobject_put(&fusd_dev->handle->kobj);
  1605. goto register_failed3;
  1606. }
  1607. // Hack to add my class to the sound class
  1608. if(strcmp("sound", register_msg.clazz) == 0)
  1609. {
  1610. fusd_dev->clazz = sound_class;
  1611. fusd_dev->owns_class = 0;
  1612. }
  1613. else
  1614. {
  1615. fusd_dev->clazz = class_create(THIS_MODULE, fusd_dev->class_name);
  1616. if(IS_ERR(fusd_dev->clazz))
  1617. {
  1618. error = PTR_ERR(fusd_dev->clazz);
  1619. printk(KERN_ERR "class_create failed status: %d\n", error);
  1620. goto register_failed4;
  1621. }
  1622. fusd_dev->owns_class = 1;
  1623. }
  1624. fusd_dev->class_device = CLASS_DEVICE_CREATE(fusd_dev->clazz, NULL, fusd_dev->dev_id, NULL, fusd_dev->dev_name);
  1625. if(fusd_dev->class_device == NULL)
  1626. {
  1627. error = PTR_ERR(fusd_dev->class_device);
  1628. printk(KERN_ERR "class_device_create failed status: %d\n", error);
  1629. goto register_failed5;
  1630. }
  1631. /* make sure the registration was successful */
  1632. /*
  1633. if (fusd_dev->handle == 0) {
  1634. error = -EIO;
  1635. goto register_failed;
  1636. }
  1637. */
  1638. /* remember the user's private data so we can pass it back later */
  1639. fusd_dev->private_data = register_msg.device_info;
  1640. /* everything ok */
  1641. fusd_dev->version = atomic_inc_and_ret(&last_version);
  1642. RDEBUG(3, "pid %d registered /dev/%s v%ld", fusd_dev->pid, NAME(fusd_dev),
  1643. fusd_dev->version);
  1644. wake_up_interruptible(&new_device_wait);
  1645. return 0;
  1646. register_failed5:
  1647. class_destroy(fusd_dev->clazz);
  1648. fusd_dev->clazz = NULL;
  1649. register_failed4:
  1650. cdev_del(fusd_dev->handle);
  1651. fusd_dev->handle = NULL;
  1652. register_failed3:
  1653. //register_failed2:
  1654. unregister_chrdev_region(dev_id, 1);
  1655. register_failed:
  1656. KFREE(fusd_dev->name);
  1657. fusd_dev->name = NULL;
  1658. return error;
  1659. }
  1660. /****************************************************************************/
  1661. /******************** CONTROL CHANNEL CALLBACK FUNCTIONS ********************/
  1662. /****************************************************************************/
  1663. /* open() called on /dev/fusd itself */
  1664. STATIC int fusd_open(struct inode *inode, struct file *file)
  1665. {
  1666. fusd_dev_t *fusd_dev = NULL;
  1667. fusd_file_t **file_array = NULL;
  1668. /* keep the module from being unloaded during initialization! */
  1669. //MOD_INC_USE_COUNT;
  1670. /* allocate memory for the device state */
  1671. if ((fusd_dev = KMALLOC(sizeof(fusd_dev_t), GFP_KERNEL)) == NULL)
  1672. goto dev_malloc_failed;
  1673. memset(fusd_dev, 0, sizeof(fusd_dev_t));
  1674. if ((file_array = fusd_dev_adjsize(fusd_dev)) == NULL)
  1675. goto file_malloc_failed;
  1676. init_waitqueue_head(&fusd_dev->dev_wait);
  1677. init_MUTEX(&fusd_dev->dev_sem);
  1678. fusd_dev->magic = FUSD_DEV_MAGIC;
  1679. fusd_dev->pid = current->pid;
  1680. fusd_dev->task = current;
  1681. file->private_data = fusd_dev;
  1682. /* add to the list of valid devices */
  1683. down(&fusd_devlist_sem);
  1684. list_add(&fusd_dev->devlist, &fusd_devlist_head);
  1685. up(&fusd_devlist_sem);
  1686. RDEBUG(3, "pid %d opened /dev/fusd", fusd_dev->pid);
  1687. return 0;
  1688. file_malloc_failed:
  1689. KFREE(fusd_dev);
  1690. dev_malloc_failed:
  1691. RDEBUG(1, "out of memory in fusd_open!");
  1692. //MOD_DEC_USE_COUNT;
  1693. return -ENOMEM;
  1694. }
  1695. /* close() called on /dev/fusd itself. destroy the device that
  1696. * was registered by it, if any. */
  1697. STATIC int fusd_release(struct inode *inode, struct file *file)
  1698. {
  1699. fusd_dev_t *fusd_dev;
  1700. GET_FUSD_DEV(file->private_data, fusd_dev);
  1701. LOCK_FUSD_DEV(fusd_dev);
  1702. if (fusd_dev->pid != current->pid) {
  1703. RDEBUG(2, "yikes!: when releasing device, pid mismatch");
  1704. }
  1705. RDEBUG(3, "pid %d closing /dev/fusd", current->pid);
  1706. #if 0
  1707. /* This delay is needed to exercise the openrace.c race condition,
  1708. * i.e. testing to make sure that our open_in_progress stuff works */
  1709. {
  1710. int target = jiffies + 10*HZ;
  1711. RDEBUG(1, "starting to wait");
  1712. while (jiffies < target)
  1713. schedule();
  1714. RDEBUG(1, "stopping wait");
  1715. }
  1716. #endif
  1717. if(fusd_dev->handle)
  1718. {
  1719. class_device_destroy(fusd_dev->clazz, fusd_dev->dev_id);
  1720. if(fusd_dev->owns_class)
  1721. {
  1722. class_destroy(fusd_dev->clazz);
  1723. }
  1724. cdev_del(fusd_dev->handle);
  1725. unregister_chrdev_region(fusd_dev->dev_id, 1);
  1726. }
  1727. /* mark the driver as being gone */
  1728. zombify_dev(fusd_dev);
  1729. /* ...and possibly free it. (Release lock if it hasn't been freed) */
  1730. if (!maybe_free_fusd_dev(fusd_dev))
  1731. UNLOCK_FUSD_DEV(fusd_dev);
  1732. /* notify fusd_status readers that there has been a change in the
  1733. * list of registered devices */
  1734. atomic_inc_and_ret(&last_version);
  1735. wake_up_interruptible(&new_device_wait);
  1736. return 0;
  1737. zombie_dev:
  1738. invalid_dev:
  1739. RDEBUG(1, "invalid device found in fusd_release!!");
  1740. return -ENODEV;
  1741. }
  1742. /*
  1743. * This function processes messages coming from userspace device drivers
  1744. * (i.e., writes to the /dev/fusd control channel.)
  1745. */
  1746. STATIC ssize_t fusd_process_write(struct file *file,
  1747. const char *user_msg_buffer, size_t user_msg_len,
  1748. const char *user_data_buffer, size_t user_data_len)
  1749. {
  1750. fusd_dev_t *fusd_dev;
  1751. fusd_msg_t *msg = NULL;
  1752. int retval = 0;
  1753. int yield = 0;
  1754. GET_FUSD_DEV(file->private_data, fusd_dev);
  1755. LOCK_FUSD_DEV(fusd_dev);
  1756. /* get the header from userspace (first make sure there's enough data) */
  1757. if (user_msg_len != sizeof(fusd_msg_t)) {
  1758. RDEBUG(6, "control channel got bad write of %d bytes (wanted %d)",
  1759. (int) user_msg_len, (int) sizeof(fusd_msg_t));
  1760. retval = -EINVAL;
  1761. goto out_no_free;
  1762. }
  1763. if ((msg = KMALLOC(sizeof(fusd_msg_t), GFP_KERNEL)) == NULL) {
  1764. retval = -ENOMEM;
  1765. RDEBUG(1, "yikes! kernel can't allocate memory");
  1766. goto out;
  1767. }
  1768. memset(msg, 0, sizeof(fusd_msg_t));
  1769. if (copy_from_user(msg, user_msg_buffer, sizeof(fusd_msg_t))) {
  1770. retval = -EFAULT;
  1771. goto out;
  1772. }
  1773. msg->data = NULL; /* pointers from userspace have no meaning */
  1774. /* check the magic number before acting on the message at all */
  1775. if (msg->magic != FUSD_MSG_MAGIC) {
  1776. RDEBUG(2, "got invalid magic number on /dev/fusd write from pid %d",
  1777. current->pid);
  1778. retval = -EIO;
  1779. goto out;
  1780. }
  1781. /* now get data portion of the message */
  1782. if (user_data_len < 0 || user_data_len > MAX_RW_SIZE) {
  1783. RDEBUG(2, "fusd_process_write: got invalid length %d", (int) user_data_len);
  1784. retval = -EINVAL;
  1785. goto out;
  1786. }
  1787. if (msg->datalen != user_data_len) {
  1788. RDEBUG(2, "msg->datalen(%d) != user_data_len(%d), sigh!",
  1789. msg->datalen, (int) user_data_len);
  1790. retval = -EINVAL;
  1791. goto out;
  1792. }
  1793. if (user_data_len > 0) {
  1794. if (user_data_buffer == NULL) {
  1795. RDEBUG(2, "msg->datalen and no data buffer, sigh!");
  1796. retval = -EINVAL;
  1797. goto out;
  1798. }
  1799. if ((msg->data = VMALLOC(user_data_len)) == NULL) {
  1800. retval = -ENOMEM;
  1801. RDEBUG(1, "yikes! kernel can't allocate memory");
  1802. goto out;
  1803. }
  1804. if (copy_from_user(msg->data, user_data_buffer, user_data_len)) {
  1805. retval = -EFAULT;
  1806. goto out;
  1807. }
  1808. }
  1809. /* before device registration, the only command allowed is 'register'. */
  1810. /*
  1811. if (!fusd_dev->handle && msg->cmd != FUSD_REGISTER_DEVICE) {
  1812. RDEBUG(2, "got a message other than 'register' on a new device!");
  1813. retval = -EINVAL;
  1814. goto out;
  1815. }
  1816. */
  1817. /* now dispatch the command to the appropriate handler */
  1818. switch (msg->cmd) {
  1819. case FUSD_REGISTER_DEVICE:
  1820. retval = fusd_register_device(fusd_dev, msg->parm.register_msg);
  1821. goto out;
  1822. break;
  1823. case FUSD_FOPS_REPLY:
  1824. /* if reply is successful, DO NOT free the message */
  1825. if ((retval = fusd_fops_reply(fusd_dev, msg)) == 0) {
  1826. yield = 1;
  1827. goto out_no_free;
  1828. }
  1829. break;
  1830. case FUSD_FOPS_NONBLOCK_REPLY:
  1831. switch (msg->subcmd) {
  1832. case FUSD_POLL_DIFF:
  1833. retval = fusd_polldiff_reply(fusd_dev, msg);
  1834. break;
  1835. default:
  1836. RDEBUG(2, "fusd_fops_nonblock got unknown subcmd %d", msg->subcmd);
  1837. retval = -EINVAL;
  1838. }
  1839. break;
  1840. default:
  1841. RDEBUG(2, "warning: unknown message type of %d received!", msg->cmd);
  1842. retval = -EINVAL;
  1843. goto out;
  1844. break;
  1845. }
  1846. out:
  1847. if (msg && msg->data) {
  1848. VFREE(msg->data);
  1849. msg->data = NULL;
  1850. }
  1851. if (msg != NULL) {
  1852. KFREE(msg);
  1853. msg = NULL;
  1854. }
  1855. out_no_free:
  1856. /* the functions we call indicate success by returning 0. we
  1857. * convert that into a success indication by changing the retval to
  1858. * the length of the write. */
  1859. if (retval == 0)
  1860. retval = user_data_len + user_msg_len;
  1861. UNLOCK_FUSD_DEV(fusd_dev);
  1862. /* if we successfully completed someone's syscall, yield the
  1863. * processor to them immediately as a throughput optimization. we
  1864. * also hope that in the case of bulk data transfer, their next
  1865. * syscall will come in before we are scheduled again. */
  1866. if (yield) {
  1867. #ifdef SCHED_YIELD
  1868. current->policy |= SCHED_YIELD;
  1869. #endif
  1870. schedule();
  1871. }
  1872. return retval;
  1873. zombie_dev:
  1874. invalid_dev:
  1875. RDEBUG(1, "fusd_process_write: got invalid device!");
  1876. return -EPIPE;
  1877. }
  1878. STATIC ssize_t fusd_write(struct file *file,
  1879. const char *buffer,
  1880. size_t length,
  1881. loff_t *offset)
  1882. {
  1883. return fusd_process_write(file, buffer, length, NULL, 0);
  1884. }
  1885. STATIC ssize_t fusd_writev(struct file *file,
  1886. const struct iovec *iov,
  1887. unsigned long count,
  1888. loff_t *offset)
  1889. {
  1890. if (count != 2) {
  1891. RDEBUG(2, "fusd_writev: got illegal iov count of %ld", count);
  1892. return -EINVAL;
  1893. }
  1894. return fusd_process_write(file,
  1895. iov[0].iov_base, iov[0].iov_len,
  1896. iov[1].iov_base, iov[1].iov_len);
  1897. }
  1898. /* fusd_read: a process is reading on /dev/fusd. return any messages
  1899. * waiting to go from kernel to userspace.
  1900. *
  1901. * Important note: there are 2 possible read modes;
  1902. * 1) header-read mode; just the fusd_msg structure is returned.
  1903. *
  1904. * 2) data-read mode; the data portion of a call (NOT including the
  1905. * fusd_msg structure) is returned.
  1906. *
  1907. * The protocol this function expects the user-space library to follow
  1908. * is:
  1909. * 1) Userspace library reads header.
  1910. * 2) If fusd_msg->datalen == 0, goto step 4.
  1911. * 3) Userspace library reads data.
  1912. * 4) Message gets dequeued by the kernel.
  1913. *
  1914. * In other words, userspace first reads the header. Then, if and
  1915. * only if the header you read indicates that data follows, userspace
  1916. * follows with a read for that data.
  1917. *
  1918. * For the header read, the length requested MUST be the exact length
  1919. * sizeof(fusd_msg_t). The corresponding data read must request
  1920. * exactly the number of bytes in the data portion of the message. NO
  1921. * OTHER READ LENGTHS ARE ALLOWED - ALL OTHER READ LENGTHS WILL GET AN
  1922. * -EINVAL. This is done as a basic safety measure to make sure we're
  1923. * talking to a userspace library that understands our protocol, and
  1924. * to detect framing errors.
  1925. *
  1926. * (note: normally you'd have to worry about reentrancy in a function
  1927. * like this because the process can block on the userspace access and
  1928. * another might try to read. usually we would copy the message into
  1929. * a temp location to make sure two processes don't get the same
  1930. * message. however in this very specialized case, we're okay,
  1931. * because each instance of /dev/fusd has a completely independent
  1932. * message queue.) */
  1933. /* do a "header" read: used by fusd_read */
  1934. STATIC int fusd_read_header(char *user_buffer, size_t user_length, fusd_msg_t *msg)
  1935. {
  1936. int len = sizeof(fusd_msg_t);
  1937. if (user_length != len) {
  1938. RDEBUG(4, "bad length of %d sent to /dev/fusd for peek", (int) user_length);
  1939. return -EINVAL;
  1940. }
  1941. if (copy_to_user(user_buffer, msg, len))
  1942. return -EFAULT;
  1943. return sizeof(fusd_msg_t);
  1944. }
  1945. /* do a "data" read: used by fusd_read */
  1946. STATIC int fusd_read_data(char *user_buffer, size_t user_length, fusd_msg_t *msg)
  1947. {
  1948. int len = msg->datalen;
  1949. if (len == 0 || msg->data == NULL) {
  1950. RDEBUG(1, "fusd_read_data: no data to send!");
  1951. return -EIO;
  1952. }
  1953. /* make sure the user is requesting exactly the right amount (as a
  1954. sanity check) */
  1955. if (user_length != len) {
  1956. RDEBUG(4, "bad read for %d bytes on /dev/fusd (need %d)", (int) user_length,len);
  1957. return -EINVAL;
  1958. }
  1959. /* now copy to userspace */
  1960. if (copy_to_user(user_buffer, msg->data, len))
  1961. return -EFAULT;
  1962. /* done! */
  1963. return len;
  1964. }
  1965. STATIC ssize_t fusd_read(struct file *file,
  1966. char *user_buffer, /* The buffer to fill with data */
  1967. size_t user_length, /* The length of the buffer */
  1968. loff_t *offset) /* Our offset in the file */
  1969. {
  1970. fusd_dev_t *fusd_dev;
  1971. fusd_msgC_t *msg_out;
  1972. int retval, dequeue = 0;
  1973. GET_FUSD_DEV(file->private_data, fusd_dev);
  1974. LOCK_FUSD_DEV(fusd_dev);
  1975. RDEBUG(15, "driver pid %d (/dev/%s) entering fusd_read", current->pid,
  1976. NAME(fusd_dev));
  1977. /* if no messages are waiting, either block or return EAGAIN */
  1978. while ((msg_out = fusd_dev->msg_head) == NULL) {
  1979. DECLARE_WAITQUEUE(wait, current);
  1980. if (file->f_flags & O_NONBLOCK) {
  1981. retval = -EAGAIN;
  1982. goto out;
  1983. }
  1984. /*
  1985. * sleep, waiting for a message to arrive. we are unrolling
  1986. * interruptible_sleep_on to avoid a race between unlocking the
  1987. * device and sleeping (what if a message arrives in that
  1988. * interval?)
  1989. */
  1990. current->state = TASK_INTERRUPTIBLE;
  1991. add_wait_queue(&fusd_dev->dev_wait, &wait);
  1992. UNLOCK_FUSD_DEV(fusd_dev);
  1993. schedule();
  1994. remove_wait_queue(&fusd_dev->dev_wait, &wait);
  1995. LOCK_FUSD_DEV(fusd_dev);
  1996. /* we're back awake! --see if a signal woke us up */
  1997. if (signal_pending(current)) {
  1998. retval = -ERESTARTSYS;
  1999. goto out;
  2000. }
  2001. }
  2002. /* is this a header read or data read? */
  2003. if (!msg_out->peeked) {
  2004. /* this is a header read (first read) */
  2005. retval = fusd_read_header(user_buffer, user_length, &msg_out->fusd_msg);
  2006. /* is there data? if so, make sure next read gets data. if not,
  2007. * make sure message is dequeued now.*/
  2008. if (msg_out->fusd_msg.datalen) {
  2009. msg_out->peeked = 1;
  2010. dequeue = 0;
  2011. } else {
  2012. dequeue = 1;
  2013. }
  2014. } else {
  2015. /* this is a data read (second read) */
  2016. retval = fusd_read_data(user_buffer, user_length, &msg_out->fusd_msg);
  2017. dequeue = 1; /* message should be dequeued */
  2018. }
  2019. /* if this message is done, take it out of the outgoing queue */
  2020. if (dequeue) {
  2021. if (fusd_dev->msg_tail == fusd_dev->msg_head)
  2022. fusd_dev->msg_tail = fusd_dev->msg_head = NULL;
  2023. else
  2024. fusd_dev->msg_head = msg_out->next;
  2025. FREE_FUSD_MSGC(msg_out);
  2026. }
  2027. out:
  2028. UNLOCK_FUSD_DEV(fusd_dev);
  2029. return retval;
  2030. zombie_dev:
  2031. invalid_dev:
  2032. RDEBUG(2, "got read on /dev/fusd for unknown device!");
  2033. return -EPIPE;
  2034. }
  2035. /* a poll on /dev/fusd itself (the control channel) */
  2036. STATIC unsigned int fusd_poll(struct file *file, poll_table *wait)
  2037. {
  2038. fusd_dev_t *fusd_dev;
  2039. GET_FUSD_DEV(file->private_data, fusd_dev);
  2040. poll_wait(file, &fusd_dev->dev_wait, wait);
  2041. if (fusd_dev->msg_head != NULL) {
  2042. return POLLIN | POLLRDNORM;
  2043. }
  2044. invalid_dev:
  2045. return 0;
  2046. }
  2047. STATIC struct file_operations fusd_fops = {
  2048. owner: THIS_MODULE,
  2049. open: fusd_open,
  2050. read: fusd_read,
  2051. write: fusd_write,
  2052. writev: fusd_writev,
  2053. release: fusd_release,
  2054. poll: fusd_poll,
  2055. };
  2056. /*************************************************************************/
  2057. typedef struct fusd_status_state {
  2058. int binary_status;
  2059. int need_new_status;
  2060. char *curr_status;
  2061. int curr_status_len;
  2062. int last_version_seen;
  2063. } fusd_statcontext_t;
  2064. /* open() called on /dev/fusd/status */
  2065. STATIC int fusd_status_open(struct inode *inode, struct file *file)
  2066. {
  2067. int error = 0;
  2068. fusd_statcontext_t *fs;
  2069. //MOD_INC_USE_COUNT;
  2070. if ((fs = KMALLOC(sizeof(fusd_statcontext_t), GFP_KERNEL)) == NULL) {
  2071. RDEBUG(1, "yikes! kernel can't allocate memory");
  2072. error = -ENOMEM;
  2073. goto out;
  2074. }
  2075. memset(fs, 0, sizeof(fusd_statcontext_t));
  2076. fs->need_new_status = 1;
  2077. file->private_data = (void *) fs;
  2078. out:
  2079. //if (error)
  2080. // MOD_DEC_USE_COUNT;
  2081. return error;
  2082. }
  2083. /* close on /dev/fusd_status */
  2084. STATIC int fusd_status_release(struct inode *inode, struct file *file)
  2085. {
  2086. fusd_statcontext_t *fs = (fusd_statcontext_t *) file->private_data;
  2087. if (fs) {
  2088. if (fs->curr_status)
  2089. KFREE(fs->curr_status);
  2090. KFREE(fs);
  2091. }
  2092. //MOD_DEC_USE_COUNT;
  2093. return 0;
  2094. }
  2095. /* ioctl() on /dev/fusd/status */
  2096. STATIC int fusd_status_ioctl(struct inode *inode, struct file *file,
  2097. unsigned int cmd, unsigned long arg)
  2098. {
  2099. fusd_statcontext_t *fs = (fusd_statcontext_t *) file->private_data;
  2100. if (!fs)
  2101. return -EIO;
  2102. switch (cmd) {
  2103. case FUSD_STATUS_USE_BINARY:
  2104. fs->binary_status = 1;
  2105. return 0;
  2106. default:
  2107. return -EINVAL;
  2108. break;
  2109. }
  2110. }
  2111. /*
  2112. * maybe_expand_buffer: expand a buffer exponentially as it fills. We
  2113. * are given:
  2114. *
  2115. * - A reference to a pointer to a buffer (buf)
  2116. * - A reference to the buffer's current capacity (buf_size)
  2117. * - The current amount of buffer space used (len)
  2118. * - The amount of space we want to ensure is free in the buffer (space_needed)
  2119. *
  2120. * If there isn't at least space_needed difference between buf_size
  2121. * and len, the existing contents are moved into a larger buffer.
  2122. */
  2123. STATIC int maybe_expand_buffer(char **buf, int *buf_size, int len,
  2124. int space_needed)
  2125. {
  2126. if (*buf_size - len < space_needed) {
  2127. char *old_buf = *buf;
  2128. *buf_size *= 2;
  2129. *buf = KMALLOC(*buf_size, GFP_KERNEL);
  2130. if (*buf != NULL)
  2131. memmove(*buf, old_buf, len);
  2132. KFREE(old_buf);
  2133. if (*buf == NULL) {
  2134. RDEBUG(1, "out of memory!");
  2135. return -1;
  2136. }
  2137. }
  2138. return 0;
  2139. }
  2140. /* Build a text buffer containing current fusd status. */
  2141. STATIC void fusd_status_build_text(fusd_statcontext_t *fs)
  2142. {
  2143. int buf_size = 512;
  2144. char *buf = KMALLOC(buf_size, GFP_KERNEL);
  2145. int len = 0, total_clients = 0, total_files = 0;
  2146. struct list_head *tmp;
  2147. if (buf == NULL) {
  2148. RDEBUG(1, "fusd_status_build: out of memory!");
  2149. return;
  2150. }
  2151. len += snprintf(buf + len, buf_size - len,
  2152. " PID Open Name\n"
  2153. "------ ---- -----------------\n");
  2154. down(&fusd_devlist_sem);
  2155. list_for_each(tmp, &fusd_devlist_head) {
  2156. fusd_dev_t *d = list_entry(tmp, fusd_dev_t, devlist);
  2157. if (!d)
  2158. continue;
  2159. /* Possibly expand the buffer if we need more space */
  2160. if (maybe_expand_buffer(&buf, &buf_size, len, FUSD_MAX_NAME_LENGTH+120) < 0)
  2161. goto out;
  2162. len += snprintf(buf + len, buf_size - len,
  2163. "%6d %4d %s%s\n", d->pid, d->num_files,
  2164. d->zombie ? "<zombie>" : "", NAME(d));
  2165. total_files++;
  2166. total_clients += d->num_files;
  2167. }
  2168. len += snprintf(buf + len, buf_size - len,
  2169. "\nFUSD $Revision: 1.97-kor-hacked-11 $ - %d devices used by %d clients\n",
  2170. total_files, total_clients);
  2171. out:
  2172. fs->last_version_seen = last_version;
  2173. up(&fusd_devlist_sem);
  2174. if (fs->curr_status)
  2175. KFREE(fs->curr_status);
  2176. fs->curr_status = buf;
  2177. fs->curr_status_len = len;
  2178. fs->need_new_status = 0;
  2179. }
  2180. /* Build the binary version of status */
  2181. STATIC void fusd_status_build_binary(fusd_statcontext_t *fs)
  2182. {
  2183. int buf_size = 512;
  2184. char *buf = KMALLOC(buf_size, GFP_KERNEL);
  2185. int len = 0, i = 0;
  2186. struct list_head *tmp;
  2187. fusd_status_t *s;
  2188. if (buf == NULL) {
  2189. RDEBUG(1, "out of memory!");
  2190. return;
  2191. }
  2192. down(&fusd_devlist_sem);
  2193. list_for_each(tmp, &fusd_devlist_head) {
  2194. fusd_dev_t *d = list_entry(tmp, fusd_dev_t, devlist);
  2195. if (!d)
  2196. continue;
  2197. /* Possibly expand the buffer if we need more space */
  2198. if (maybe_expand_buffer(&buf, &buf_size, len, sizeof(fusd_status_t)) < 0)
  2199. goto out;
  2200. s = &((fusd_status_t *) buf)[i];
  2201. /* construct this status entry */
  2202. memset(s, 0, sizeof(fusd_status_t));
  2203. strncpy(s->name, NAME(d), FUSD_MAX_NAME_LENGTH);
  2204. s->zombie = d->zombie;
  2205. s->pid = d->pid;
  2206. s->num_open = d->num_files;
  2207. i++;
  2208. len += sizeof(fusd_status_t);
  2209. }
  2210. out:
  2211. fs->last_version_seen = last_version;
  2212. up(&fusd_devlist_sem);
  2213. if (fs->curr_status)
  2214. KFREE(fs->curr_status);
  2215. fs->curr_status = buf;
  2216. fs->curr_status_len = len;
  2217. fs->need_new_status = 0;
  2218. }
  2219. STATIC ssize_t fusd_status_read(struct file *file,
  2220. char *user_buffer, /* The buffer to fill with data */
  2221. size_t user_length, /* The length of the buffer */
  2222. loff_t *offset) /* Our offset in the file */
  2223. {
  2224. fusd_statcontext_t *fs = (fusd_statcontext_t *) file->private_data;
  2225. if (!fs)
  2226. return -EIO;
  2227. /* create a new status page, if we aren't in the middle of one */
  2228. if (fs->need_new_status) {
  2229. if (fs->binary_status)
  2230. fusd_status_build_binary(fs);
  2231. else
  2232. fusd_status_build_text(fs);
  2233. }
  2234. /* return EOF if we're at the end */
  2235. if (fs->curr_status == NULL || fs->curr_status_len == 0) {
  2236. fs->need_new_status = 1;
  2237. return 0;
  2238. }
  2239. /* return only as much data as we have */
  2240. if (fs->curr_status_len < user_length)
  2241. user_length = fs->curr_status_len;
  2242. if (copy_to_user(user_buffer, fs->curr_status, user_length))
  2243. return -EFAULT;
  2244. /* update fs, so we don't return the same data next time */
  2245. fs->curr_status_len -= user_length;
  2246. if (fs->curr_status_len)
  2247. memmove(fs->curr_status, fs->curr_status + user_length, fs->curr_status_len);
  2248. else {
  2249. KFREE(fs->curr_status);
  2250. fs->curr_status = NULL;
  2251. }
  2252. return user_length;
  2253. }
  2254. /* a poll on /dev/fusd itself (the control channel) */
  2255. STATIC unsigned int fusd_status_poll(struct file *file, poll_table *wait)
  2256. {
  2257. fusd_statcontext_t *fs = (fusd_statcontext_t *) file->private_data;
  2258. poll_wait(file, &new_device_wait, wait);
  2259. if (fs->last_version_seen < last_version)
  2260. return POLLIN | POLLRDNORM;
  2261. else
  2262. return 0;
  2263. }
  2264. STATIC struct file_operations fusd_status_fops = {
  2265. owner: THIS_MODULE,
  2266. open: fusd_status_open,
  2267. ioctl: fusd_status_ioctl,
  2268. read: fusd_status_read,
  2269. release: fusd_status_release,
  2270. poll: fusd_status_poll,
  2271. };
  2272. /*************************************************************************/
  2273. STATIC int init_fusd(void)
  2274. {
  2275. int retval;
  2276. #ifdef CONFIG_FUSD_MEMDEBUG
  2277. if ((retval = fusd_mem_init()) < 0)
  2278. return retval;
  2279. #endif
  2280. printk(KERN_INFO
  2281. "fusd: starting, $Revision: 1.97-kor-hacked-11 $, $Date: 2003/07/11 22:29:39 $");
  2282. #ifdef CVSTAG
  2283. printk(", release %s", CVSTAG);
  2284. #endif
  2285. #ifdef CONFIG_FUSD_DEBUG
  2286. printk(", debuglevel=%d\n", fusd_debug_level);
  2287. #else
  2288. printk(", debugging messages disabled\n");
  2289. #endif
  2290. fusd_control_device = NULL;
  2291. fusd_status_device = NULL;
  2292. fusd_class = class_create(THIS_MODULE, "fusd");
  2293. if(IS_ERR(fusd_class))
  2294. {
  2295. retval = PTR_ERR(fusd_class);
  2296. printk(KERN_ERR "class_create failed status: %d\n", retval);
  2297. goto fail0;
  2298. }
  2299. control_id = 0;
  2300. if((retval = alloc_chrdev_region(&control_id, 0, 1, FUSD_CONTROL_FILENAME)) < 0)
  2301. {
  2302. printk(KERN_ERR "alloc_chrdev_region failed status: %d\n", retval);
  2303. goto fail1;
  2304. }
  2305. fusd_control_device = cdev_alloc();
  2306. if(fusd_control_device == NULL)
  2307. {
  2308. printk(KERN_ERR "cdev-alloc failed\n");
  2309. retval = -ENOMEM;
  2310. goto fail3;
  2311. }
  2312. fusd_control_device->owner = THIS_MODULE;
  2313. fusd_control_device->ops = &fusd_fops;
  2314. kobject_set_name(&fusd_control_device->kobj, FUSD_CONTROL_FILENAME);
  2315. printk(KERN_ERR "cdev control id: %d\n", control_id);
  2316. if((retval = cdev_add(fusd_control_device, control_id, 1)) < 0)
  2317. {
  2318. printk(KERN_ERR "cdev_add failed status: %d\n", retval);
  2319. kobject_put(&fusd_control_device->kobj);
  2320. goto fail4;
  2321. }
  2322. fusd_control_class_device = CLASS_DEVICE_CREATE(fusd_class, NULL, control_id, NULL, "control");
  2323. if(fusd_control_class_device == NULL)
  2324. {
  2325. retval = PTR_ERR(fusd_control_class_device);
  2326. printk("class_device_create failed status: %d\n", retval);
  2327. goto fail5;
  2328. }
  2329. status_id = 0;
  2330. if((retval = alloc_chrdev_region(&status_id, 0, 1, FUSD_STATUS_FILENAME)) < 0)
  2331. {
  2332. printk(KERN_ERR "alloc_chrdev_region failed status: %d\n", retval);
  2333. goto fail6;
  2334. }
  2335. fusd_status_device = cdev_alloc();
  2336. if(fusd_status_device == NULL)
  2337. {
  2338. retval = -ENOMEM;
  2339. goto fail8;
  2340. }
  2341. fusd_status_device->owner = THIS_MODULE;
  2342. fusd_status_device->ops = &fusd_status_fops;
  2343. kobject_set_name(&fusd_status_device->kobj, FUSD_STATUS_FILENAME);
  2344. if((retval = cdev_add(fusd_status_device, status_id, 1)) < 0)
  2345. {
  2346. printk(KERN_ERR "cdev_add failed status: %d\n", retval);
  2347. kobject_put(&fusd_status_device->kobj);
  2348. goto fail9;
  2349. }
  2350. fusd_status_class_device = CLASS_DEVICE_CREATE(fusd_class, NULL, status_id, NULL, "status");
  2351. if(fusd_status_class_device == NULL)
  2352. {
  2353. printk(KERN_ERR "class_device_create failed status: %d\n", retval);
  2354. retval = PTR_ERR(fusd_status_class_device);
  2355. goto fail10;
  2356. }
  2357. RDEBUG(1, "registration successful");
  2358. return 0;
  2359. fail10:
  2360. cdev_del(fusd_status_device);
  2361. fail9:
  2362. kfree(fusd_status_device);
  2363. fail8:
  2364. //fail7:
  2365. unregister_chrdev_region(status_id, 1);
  2366. fail6:
  2367. class_device_destroy(fusd_class, control_id);
  2368. fail5:
  2369. cdev_del(fusd_control_device);
  2370. fail4:
  2371. kfree(fusd_control_device);
  2372. fail3:
  2373. //fail2:
  2374. unregister_chrdev_region(control_id, 1);
  2375. fail1:
  2376. class_destroy(fusd_class);
  2377. fail0:
  2378. return retval;
  2379. }
  2380. STATIC void cleanup_fusd(void)
  2381. {
  2382. RDEBUG(1, "cleaning up");
  2383. class_device_destroy(fusd_class, status_id);
  2384. class_device_destroy(fusd_class, control_id);
  2385. cdev_del(fusd_control_device);
  2386. cdev_del(fusd_status_device);
  2387. class_destroy(fusd_class);
  2388. #ifdef CONFIG_FUSD_MEMDEBUG
  2389. fusd_mem_cleanup();
  2390. #endif
  2391. }
  2392. module_init(init_fusd);
  2393. module_exit(cleanup_fusd);