kfusd.c 94 KB

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