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