kfusd.c 82 KB

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