kfusd.c 87 KB

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