kfusd.c 101 KB

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