kfusd.c 84 KB

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