kfusd.c 94 KB

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