kfusd.c 87 KB

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