kfusd.c 89 KB

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