kfusd.c 88 KB

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