kfusd.c 88 KB

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