kfusd.c 98 KB

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