kfusd.c 89 KB

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