kfusd.c 101 KB

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