kfusd.c 88 KB

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