kfusd.c 95 KB

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