drbd_main.c 113 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955
  1. /*
  2. drbd.c
  3. This file is part of DRBD by Philipp Reisner and Lars Ellenberg.
  4. Copyright (C) 2001-2008, LINBIT Information Technologies GmbH.
  5. Copyright (C) 1999-2008, Philipp Reisner <philipp.reisner@linbit.com>.
  6. Copyright (C) 2002-2008, Lars Ellenberg <lars.ellenberg@linbit.com>.
  7. Thanks to Carter Burden, Bart Grantham and Gennadiy Nerubayev
  8. from Logicworks, Inc. for making SDP replication support possible.
  9. drbd is free software; you can redistribute it and/or modify
  10. it under the terms of the GNU General Public License as published by
  11. the Free Software Foundation; either version 2, or (at your option)
  12. any later version.
  13. drbd is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. GNU General Public License for more details.
  17. You should have received a copy of the GNU General Public License
  18. along with drbd; see the file COPYING. If not, write to
  19. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  22. #include <linux/module.h>
  23. #include <linux/jiffies.h>
  24. #include <linux/drbd.h>
  25. #include <linux/uaccess.h>
  26. #include <asm/types.h>
  27. #include <net/sock.h>
  28. #include <linux/ctype.h>
  29. #include <linux/mutex.h>
  30. #include <linux/fs.h>
  31. #include <linux/file.h>
  32. #include <linux/proc_fs.h>
  33. #include <linux/init.h>
  34. #include <linux/mm.h>
  35. #include <linux/memcontrol.h>
  36. #include <linux/mm_inline.h>
  37. #include <linux/slab.h>
  38. #include <linux/random.h>
  39. #include <linux/reboot.h>
  40. #include <linux/notifier.h>
  41. #include <linux/kthread.h>
  42. #include <linux/workqueue.h>
  43. #define __KERNEL_SYSCALLS__
  44. #include <linux/unistd.h>
  45. #include <linux/vmalloc.h>
  46. #include <linux/sched/signal.h>
  47. #include <linux/drbd_limits.h>
  48. #include "drbd_int.h"
  49. #include "drbd_protocol.h"
  50. #include "drbd_req.h" /* only for _req_mod in tl_release and tl_clear */
  51. #include "drbd_vli.h"
  52. #include "drbd_debugfs.h"
  53. static DEFINE_MUTEX(drbd_main_mutex);
  54. static int drbd_open(struct block_device *bdev, fmode_t mode);
  55. static void drbd_release(struct gendisk *gd, fmode_t mode);
  56. static void md_sync_timer_fn(unsigned long data);
  57. static int w_bitmap_io(struct drbd_work *w, int unused);
  58. MODULE_AUTHOR("Philipp Reisner <phil@linbit.com>, "
  59. "Lars Ellenberg <lars@linbit.com>");
  60. MODULE_DESCRIPTION("drbd - Distributed Replicated Block Device v" REL_VERSION);
  61. MODULE_VERSION(REL_VERSION);
  62. MODULE_LICENSE("GPL");
  63. MODULE_PARM_DESC(minor_count, "Approximate number of drbd devices ("
  64. __stringify(DRBD_MINOR_COUNT_MIN) "-" __stringify(DRBD_MINOR_COUNT_MAX) ")");
  65. MODULE_ALIAS_BLOCKDEV_MAJOR(DRBD_MAJOR);
  66. #include <linux/moduleparam.h>
  67. /* thanks to these macros, if compiled into the kernel (not-module),
  68. * these become boot parameters (e.g., drbd.minor_count) */
  69. #ifdef CONFIG_DRBD_FAULT_INJECTION
  70. int drbd_enable_faults;
  71. int drbd_fault_rate;
  72. static int drbd_fault_count;
  73. static int drbd_fault_devs;
  74. /* bitmap of enabled faults */
  75. module_param_named(enable_faults, drbd_enable_faults, int, 0664);
  76. /* fault rate % value - applies to all enabled faults */
  77. module_param_named(fault_rate, drbd_fault_rate, int, 0664);
  78. /* count of faults inserted */
  79. module_param_named(fault_count, drbd_fault_count, int, 0664);
  80. /* bitmap of devices to insert faults on */
  81. module_param_named(fault_devs, drbd_fault_devs, int, 0644);
  82. #endif
  83. /* module parameters we can keep static */
  84. static bool drbd_allow_oos; /* allow_open_on_secondary */
  85. static bool drbd_disable_sendpage;
  86. MODULE_PARM_DESC(allow_oos, "DONT USE!");
  87. module_param_named(allow_oos, drbd_allow_oos, bool, 0);
  88. module_param_named(disable_sendpage, drbd_disable_sendpage, bool, 0644);
  89. /* module parameters we share */
  90. int drbd_proc_details; /* Detail level in proc drbd*/
  91. module_param_named(proc_details, drbd_proc_details, int, 0644);
  92. /* module parameters shared with defaults */
  93. unsigned int drbd_minor_count = DRBD_MINOR_COUNT_DEF;
  94. /* Module parameter for setting the user mode helper program
  95. * to run. Default is /sbin/drbdadm */
  96. char drbd_usermode_helper[80] = "/sbin/drbdadm";
  97. module_param_named(minor_count, drbd_minor_count, uint, 0444);
  98. module_param_string(usermode_helper, drbd_usermode_helper, sizeof(drbd_usermode_helper), 0644);
  99. /* in 2.6.x, our device mapping and config info contains our virtual gendisks
  100. * as member "struct gendisk *vdisk;"
  101. */
  102. struct idr drbd_devices;
  103. struct list_head drbd_resources;
  104. struct mutex resources_mutex;
  105. struct kmem_cache *drbd_request_cache;
  106. struct kmem_cache *drbd_ee_cache; /* peer requests */
  107. struct kmem_cache *drbd_bm_ext_cache; /* bitmap extents */
  108. struct kmem_cache *drbd_al_ext_cache; /* activity log extents */
  109. mempool_t *drbd_request_mempool;
  110. mempool_t *drbd_ee_mempool;
  111. mempool_t *drbd_md_io_page_pool;
  112. struct bio_set *drbd_md_io_bio_set;
  113. struct bio_set *drbd_io_bio_set;
  114. /* I do not use a standard mempool, because:
  115. 1) I want to hand out the pre-allocated objects first.
  116. 2) I want to be able to interrupt sleeping allocation with a signal.
  117. Note: This is a single linked list, the next pointer is the private
  118. member of struct page.
  119. */
  120. struct page *drbd_pp_pool;
  121. spinlock_t drbd_pp_lock;
  122. int drbd_pp_vacant;
  123. wait_queue_head_t drbd_pp_wait;
  124. DEFINE_RATELIMIT_STATE(drbd_ratelimit_state, 5 * HZ, 5);
  125. static const struct block_device_operations drbd_ops = {
  126. .owner = THIS_MODULE,
  127. .open = drbd_open,
  128. .release = drbd_release,
  129. };
  130. struct bio *bio_alloc_drbd(gfp_t gfp_mask)
  131. {
  132. struct bio *bio;
  133. if (!drbd_md_io_bio_set)
  134. return bio_alloc(gfp_mask, 1);
  135. bio = bio_alloc_bioset(gfp_mask, 1, drbd_md_io_bio_set);
  136. if (!bio)
  137. return NULL;
  138. return bio;
  139. }
  140. #ifdef __CHECKER__
  141. /* When checking with sparse, and this is an inline function, sparse will
  142. give tons of false positives. When this is a real functions sparse works.
  143. */
  144. int _get_ldev_if_state(struct drbd_device *device, enum drbd_disk_state mins)
  145. {
  146. int io_allowed;
  147. atomic_inc(&device->local_cnt);
  148. io_allowed = (device->state.disk >= mins);
  149. if (!io_allowed) {
  150. if (atomic_dec_and_test(&device->local_cnt))
  151. wake_up(&device->misc_wait);
  152. }
  153. return io_allowed;
  154. }
  155. #endif
  156. /**
  157. * tl_release() - mark as BARRIER_ACKED all requests in the corresponding transfer log epoch
  158. * @connection: DRBD connection.
  159. * @barrier_nr: Expected identifier of the DRBD write barrier packet.
  160. * @set_size: Expected number of requests before that barrier.
  161. *
  162. * In case the passed barrier_nr or set_size does not match the oldest
  163. * epoch of not yet barrier-acked requests, this function will cause a
  164. * termination of the connection.
  165. */
  166. void tl_release(struct drbd_connection *connection, unsigned int barrier_nr,
  167. unsigned int set_size)
  168. {
  169. struct drbd_request *r;
  170. struct drbd_request *req = NULL;
  171. int expect_epoch = 0;
  172. int expect_size = 0;
  173. spin_lock_irq(&connection->resource->req_lock);
  174. /* find oldest not yet barrier-acked write request,
  175. * count writes in its epoch. */
  176. list_for_each_entry(r, &connection->transfer_log, tl_requests) {
  177. const unsigned s = r->rq_state;
  178. if (!req) {
  179. if (!(s & RQ_WRITE))
  180. continue;
  181. if (!(s & RQ_NET_MASK))
  182. continue;
  183. if (s & RQ_NET_DONE)
  184. continue;
  185. req = r;
  186. expect_epoch = req->epoch;
  187. expect_size ++;
  188. } else {
  189. if (r->epoch != expect_epoch)
  190. break;
  191. if (!(s & RQ_WRITE))
  192. continue;
  193. /* if (s & RQ_DONE): not expected */
  194. /* if (!(s & RQ_NET_MASK)): not expected */
  195. expect_size++;
  196. }
  197. }
  198. /* first some paranoia code */
  199. if (req == NULL) {
  200. drbd_err(connection, "BAD! BarrierAck #%u received, but no epoch in tl!?\n",
  201. barrier_nr);
  202. goto bail;
  203. }
  204. if (expect_epoch != barrier_nr) {
  205. drbd_err(connection, "BAD! BarrierAck #%u received, expected #%u!\n",
  206. barrier_nr, expect_epoch);
  207. goto bail;
  208. }
  209. if (expect_size != set_size) {
  210. drbd_err(connection, "BAD! BarrierAck #%u received with n_writes=%u, expected n_writes=%u!\n",
  211. barrier_nr, set_size, expect_size);
  212. goto bail;
  213. }
  214. /* Clean up list of requests processed during current epoch. */
  215. /* this extra list walk restart is paranoia,
  216. * to catch requests being barrier-acked "unexpectedly".
  217. * It usually should find the same req again, or some READ preceding it. */
  218. list_for_each_entry(req, &connection->transfer_log, tl_requests)
  219. if (req->epoch == expect_epoch)
  220. break;
  221. list_for_each_entry_safe_from(req, r, &connection->transfer_log, tl_requests) {
  222. if (req->epoch != expect_epoch)
  223. break;
  224. _req_mod(req, BARRIER_ACKED);
  225. }
  226. spin_unlock_irq(&connection->resource->req_lock);
  227. return;
  228. bail:
  229. spin_unlock_irq(&connection->resource->req_lock);
  230. conn_request_state(connection, NS(conn, C_PROTOCOL_ERROR), CS_HARD);
  231. }
  232. /**
  233. * _tl_restart() - Walks the transfer log, and applies an action to all requests
  234. * @connection: DRBD connection to operate on.
  235. * @what: The action/event to perform with all request objects
  236. *
  237. * @what might be one of CONNECTION_LOST_WHILE_PENDING, RESEND, FAIL_FROZEN_DISK_IO,
  238. * RESTART_FROZEN_DISK_IO.
  239. */
  240. /* must hold resource->req_lock */
  241. void _tl_restart(struct drbd_connection *connection, enum drbd_req_event what)
  242. {
  243. struct drbd_request *req, *r;
  244. list_for_each_entry_safe(req, r, &connection->transfer_log, tl_requests)
  245. _req_mod(req, what);
  246. }
  247. void tl_restart(struct drbd_connection *connection, enum drbd_req_event what)
  248. {
  249. spin_lock_irq(&connection->resource->req_lock);
  250. _tl_restart(connection, what);
  251. spin_unlock_irq(&connection->resource->req_lock);
  252. }
  253. /**
  254. * tl_clear() - Clears all requests and &struct drbd_tl_epoch objects out of the TL
  255. * @device: DRBD device.
  256. *
  257. * This is called after the connection to the peer was lost. The storage covered
  258. * by the requests on the transfer gets marked as our of sync. Called from the
  259. * receiver thread and the worker thread.
  260. */
  261. void tl_clear(struct drbd_connection *connection)
  262. {
  263. tl_restart(connection, CONNECTION_LOST_WHILE_PENDING);
  264. }
  265. /**
  266. * tl_abort_disk_io() - Abort disk I/O for all requests for a certain device in the TL
  267. * @device: DRBD device.
  268. */
  269. void tl_abort_disk_io(struct drbd_device *device)
  270. {
  271. struct drbd_connection *connection = first_peer_device(device)->connection;
  272. struct drbd_request *req, *r;
  273. spin_lock_irq(&connection->resource->req_lock);
  274. list_for_each_entry_safe(req, r, &connection->transfer_log, tl_requests) {
  275. if (!(req->rq_state & RQ_LOCAL_PENDING))
  276. continue;
  277. if (req->device != device)
  278. continue;
  279. _req_mod(req, ABORT_DISK_IO);
  280. }
  281. spin_unlock_irq(&connection->resource->req_lock);
  282. }
  283. static int drbd_thread_setup(void *arg)
  284. {
  285. struct drbd_thread *thi = (struct drbd_thread *) arg;
  286. struct drbd_resource *resource = thi->resource;
  287. unsigned long flags;
  288. int retval;
  289. snprintf(current->comm, sizeof(current->comm), "drbd_%c_%s",
  290. thi->name[0],
  291. resource->name);
  292. restart:
  293. retval = thi->function(thi);
  294. spin_lock_irqsave(&thi->t_lock, flags);
  295. /* if the receiver has been "EXITING", the last thing it did
  296. * was set the conn state to "StandAlone",
  297. * if now a re-connect request comes in, conn state goes C_UNCONNECTED,
  298. * and receiver thread will be "started".
  299. * drbd_thread_start needs to set "RESTARTING" in that case.
  300. * t_state check and assignment needs to be within the same spinlock,
  301. * so either thread_start sees EXITING, and can remap to RESTARTING,
  302. * or thread_start see NONE, and can proceed as normal.
  303. */
  304. if (thi->t_state == RESTARTING) {
  305. drbd_info(resource, "Restarting %s thread\n", thi->name);
  306. thi->t_state = RUNNING;
  307. spin_unlock_irqrestore(&thi->t_lock, flags);
  308. goto restart;
  309. }
  310. thi->task = NULL;
  311. thi->t_state = NONE;
  312. smp_mb();
  313. complete_all(&thi->stop);
  314. spin_unlock_irqrestore(&thi->t_lock, flags);
  315. drbd_info(resource, "Terminating %s\n", current->comm);
  316. /* Release mod reference taken when thread was started */
  317. if (thi->connection)
  318. kref_put(&thi->connection->kref, drbd_destroy_connection);
  319. kref_put(&resource->kref, drbd_destroy_resource);
  320. module_put(THIS_MODULE);
  321. return retval;
  322. }
  323. static void drbd_thread_init(struct drbd_resource *resource, struct drbd_thread *thi,
  324. int (*func) (struct drbd_thread *), const char *name)
  325. {
  326. spin_lock_init(&thi->t_lock);
  327. thi->task = NULL;
  328. thi->t_state = NONE;
  329. thi->function = func;
  330. thi->resource = resource;
  331. thi->connection = NULL;
  332. thi->name = name;
  333. }
  334. int drbd_thread_start(struct drbd_thread *thi)
  335. {
  336. struct drbd_resource *resource = thi->resource;
  337. struct task_struct *nt;
  338. unsigned long flags;
  339. /* is used from state engine doing drbd_thread_stop_nowait,
  340. * while holding the req lock irqsave */
  341. spin_lock_irqsave(&thi->t_lock, flags);
  342. switch (thi->t_state) {
  343. case NONE:
  344. drbd_info(resource, "Starting %s thread (from %s [%d])\n",
  345. thi->name, current->comm, current->pid);
  346. /* Get ref on module for thread - this is released when thread exits */
  347. if (!try_module_get(THIS_MODULE)) {
  348. drbd_err(resource, "Failed to get module reference in drbd_thread_start\n");
  349. spin_unlock_irqrestore(&thi->t_lock, flags);
  350. return false;
  351. }
  352. kref_get(&resource->kref);
  353. if (thi->connection)
  354. kref_get(&thi->connection->kref);
  355. init_completion(&thi->stop);
  356. thi->reset_cpu_mask = 1;
  357. thi->t_state = RUNNING;
  358. spin_unlock_irqrestore(&thi->t_lock, flags);
  359. flush_signals(current); /* otherw. may get -ERESTARTNOINTR */
  360. nt = kthread_create(drbd_thread_setup, (void *) thi,
  361. "drbd_%c_%s", thi->name[0], thi->resource->name);
  362. if (IS_ERR(nt)) {
  363. drbd_err(resource, "Couldn't start thread\n");
  364. if (thi->connection)
  365. kref_put(&thi->connection->kref, drbd_destroy_connection);
  366. kref_put(&resource->kref, drbd_destroy_resource);
  367. module_put(THIS_MODULE);
  368. return false;
  369. }
  370. spin_lock_irqsave(&thi->t_lock, flags);
  371. thi->task = nt;
  372. thi->t_state = RUNNING;
  373. spin_unlock_irqrestore(&thi->t_lock, flags);
  374. wake_up_process(nt);
  375. break;
  376. case EXITING:
  377. thi->t_state = RESTARTING;
  378. drbd_info(resource, "Restarting %s thread (from %s [%d])\n",
  379. thi->name, current->comm, current->pid);
  380. /* fall through */
  381. case RUNNING:
  382. case RESTARTING:
  383. default:
  384. spin_unlock_irqrestore(&thi->t_lock, flags);
  385. break;
  386. }
  387. return true;
  388. }
  389. void _drbd_thread_stop(struct drbd_thread *thi, int restart, int wait)
  390. {
  391. unsigned long flags;
  392. enum drbd_thread_state ns = restart ? RESTARTING : EXITING;
  393. /* may be called from state engine, holding the req lock irqsave */
  394. spin_lock_irqsave(&thi->t_lock, flags);
  395. if (thi->t_state == NONE) {
  396. spin_unlock_irqrestore(&thi->t_lock, flags);
  397. if (restart)
  398. drbd_thread_start(thi);
  399. return;
  400. }
  401. if (thi->t_state != ns) {
  402. if (thi->task == NULL) {
  403. spin_unlock_irqrestore(&thi->t_lock, flags);
  404. return;
  405. }
  406. thi->t_state = ns;
  407. smp_mb();
  408. init_completion(&thi->stop);
  409. if (thi->task != current)
  410. force_sig(DRBD_SIGKILL, thi->task);
  411. }
  412. spin_unlock_irqrestore(&thi->t_lock, flags);
  413. if (wait)
  414. wait_for_completion(&thi->stop);
  415. }
  416. int conn_lowest_minor(struct drbd_connection *connection)
  417. {
  418. struct drbd_peer_device *peer_device;
  419. int vnr = 0, minor = -1;
  420. rcu_read_lock();
  421. peer_device = idr_get_next(&connection->peer_devices, &vnr);
  422. if (peer_device)
  423. minor = device_to_minor(peer_device->device);
  424. rcu_read_unlock();
  425. return minor;
  426. }
  427. #ifdef CONFIG_SMP
  428. /**
  429. * drbd_calc_cpu_mask() - Generate CPU masks, spread over all CPUs
  430. *
  431. * Forces all threads of a resource onto the same CPU. This is beneficial for
  432. * DRBD's performance. May be overwritten by user's configuration.
  433. */
  434. static void drbd_calc_cpu_mask(cpumask_var_t *cpu_mask)
  435. {
  436. unsigned int *resources_per_cpu, min_index = ~0;
  437. resources_per_cpu = kzalloc(nr_cpu_ids * sizeof(*resources_per_cpu), GFP_KERNEL);
  438. if (resources_per_cpu) {
  439. struct drbd_resource *resource;
  440. unsigned int cpu, min = ~0;
  441. rcu_read_lock();
  442. for_each_resource_rcu(resource, &drbd_resources) {
  443. for_each_cpu(cpu, resource->cpu_mask)
  444. resources_per_cpu[cpu]++;
  445. }
  446. rcu_read_unlock();
  447. for_each_online_cpu(cpu) {
  448. if (resources_per_cpu[cpu] < min) {
  449. min = resources_per_cpu[cpu];
  450. min_index = cpu;
  451. }
  452. }
  453. kfree(resources_per_cpu);
  454. }
  455. if (min_index == ~0) {
  456. cpumask_setall(*cpu_mask);
  457. return;
  458. }
  459. cpumask_set_cpu(min_index, *cpu_mask);
  460. }
  461. /**
  462. * drbd_thread_current_set_cpu() - modifies the cpu mask of the _current_ thread
  463. * @device: DRBD device.
  464. * @thi: drbd_thread object
  465. *
  466. * call in the "main loop" of _all_ threads, no need for any mutex, current won't die
  467. * prematurely.
  468. */
  469. void drbd_thread_current_set_cpu(struct drbd_thread *thi)
  470. {
  471. struct drbd_resource *resource = thi->resource;
  472. struct task_struct *p = current;
  473. if (!thi->reset_cpu_mask)
  474. return;
  475. thi->reset_cpu_mask = 0;
  476. set_cpus_allowed_ptr(p, resource->cpu_mask);
  477. }
  478. #else
  479. #define drbd_calc_cpu_mask(A) ({})
  480. #endif
  481. /**
  482. * drbd_header_size - size of a packet header
  483. *
  484. * The header size is a multiple of 8, so any payload following the header is
  485. * word aligned on 64-bit architectures. (The bitmap send and receive code
  486. * relies on this.)
  487. */
  488. unsigned int drbd_header_size(struct drbd_connection *connection)
  489. {
  490. if (connection->agreed_pro_version >= 100) {
  491. BUILD_BUG_ON(!IS_ALIGNED(sizeof(struct p_header100), 8));
  492. return sizeof(struct p_header100);
  493. } else {
  494. BUILD_BUG_ON(sizeof(struct p_header80) !=
  495. sizeof(struct p_header95));
  496. BUILD_BUG_ON(!IS_ALIGNED(sizeof(struct p_header80), 8));
  497. return sizeof(struct p_header80);
  498. }
  499. }
  500. static unsigned int prepare_header80(struct p_header80 *h, enum drbd_packet cmd, int size)
  501. {
  502. h->magic = cpu_to_be32(DRBD_MAGIC);
  503. h->command = cpu_to_be16(cmd);
  504. h->length = cpu_to_be16(size);
  505. return sizeof(struct p_header80);
  506. }
  507. static unsigned int prepare_header95(struct p_header95 *h, enum drbd_packet cmd, int size)
  508. {
  509. h->magic = cpu_to_be16(DRBD_MAGIC_BIG);
  510. h->command = cpu_to_be16(cmd);
  511. h->length = cpu_to_be32(size);
  512. return sizeof(struct p_header95);
  513. }
  514. static unsigned int prepare_header100(struct p_header100 *h, enum drbd_packet cmd,
  515. int size, int vnr)
  516. {
  517. h->magic = cpu_to_be32(DRBD_MAGIC_100);
  518. h->volume = cpu_to_be16(vnr);
  519. h->command = cpu_to_be16(cmd);
  520. h->length = cpu_to_be32(size);
  521. h->pad = 0;
  522. return sizeof(struct p_header100);
  523. }
  524. static unsigned int prepare_header(struct drbd_connection *connection, int vnr,
  525. void *buffer, enum drbd_packet cmd, int size)
  526. {
  527. if (connection->agreed_pro_version >= 100)
  528. return prepare_header100(buffer, cmd, size, vnr);
  529. else if (connection->agreed_pro_version >= 95 &&
  530. size > DRBD_MAX_SIZE_H80_PACKET)
  531. return prepare_header95(buffer, cmd, size);
  532. else
  533. return prepare_header80(buffer, cmd, size);
  534. }
  535. static void *__conn_prepare_command(struct drbd_connection *connection,
  536. struct drbd_socket *sock)
  537. {
  538. if (!sock->socket)
  539. return NULL;
  540. return sock->sbuf + drbd_header_size(connection);
  541. }
  542. void *conn_prepare_command(struct drbd_connection *connection, struct drbd_socket *sock)
  543. {
  544. void *p;
  545. mutex_lock(&sock->mutex);
  546. p = __conn_prepare_command(connection, sock);
  547. if (!p)
  548. mutex_unlock(&sock->mutex);
  549. return p;
  550. }
  551. void *drbd_prepare_command(struct drbd_peer_device *peer_device, struct drbd_socket *sock)
  552. {
  553. return conn_prepare_command(peer_device->connection, sock);
  554. }
  555. static int __send_command(struct drbd_connection *connection, int vnr,
  556. struct drbd_socket *sock, enum drbd_packet cmd,
  557. unsigned int header_size, void *data,
  558. unsigned int size)
  559. {
  560. int msg_flags;
  561. int err;
  562. /*
  563. * Called with @data == NULL and the size of the data blocks in @size
  564. * for commands that send data blocks. For those commands, omit the
  565. * MSG_MORE flag: this will increase the likelihood that data blocks
  566. * which are page aligned on the sender will end up page aligned on the
  567. * receiver.
  568. */
  569. msg_flags = data ? MSG_MORE : 0;
  570. header_size += prepare_header(connection, vnr, sock->sbuf, cmd,
  571. header_size + size);
  572. err = drbd_send_all(connection, sock->socket, sock->sbuf, header_size,
  573. msg_flags);
  574. if (data && !err)
  575. err = drbd_send_all(connection, sock->socket, data, size, 0);
  576. /* DRBD protocol "pings" are latency critical.
  577. * This is supposed to trigger tcp_push_pending_frames() */
  578. if (!err && (cmd == P_PING || cmd == P_PING_ACK))
  579. drbd_tcp_nodelay(sock->socket);
  580. return err;
  581. }
  582. static int __conn_send_command(struct drbd_connection *connection, struct drbd_socket *sock,
  583. enum drbd_packet cmd, unsigned int header_size,
  584. void *data, unsigned int size)
  585. {
  586. return __send_command(connection, 0, sock, cmd, header_size, data, size);
  587. }
  588. int conn_send_command(struct drbd_connection *connection, struct drbd_socket *sock,
  589. enum drbd_packet cmd, unsigned int header_size,
  590. void *data, unsigned int size)
  591. {
  592. int err;
  593. err = __conn_send_command(connection, sock, cmd, header_size, data, size);
  594. mutex_unlock(&sock->mutex);
  595. return err;
  596. }
  597. int drbd_send_command(struct drbd_peer_device *peer_device, struct drbd_socket *sock,
  598. enum drbd_packet cmd, unsigned int header_size,
  599. void *data, unsigned int size)
  600. {
  601. int err;
  602. err = __send_command(peer_device->connection, peer_device->device->vnr,
  603. sock, cmd, header_size, data, size);
  604. mutex_unlock(&sock->mutex);
  605. return err;
  606. }
  607. int drbd_send_ping(struct drbd_connection *connection)
  608. {
  609. struct drbd_socket *sock;
  610. sock = &connection->meta;
  611. if (!conn_prepare_command(connection, sock))
  612. return -EIO;
  613. return conn_send_command(connection, sock, P_PING, 0, NULL, 0);
  614. }
  615. int drbd_send_ping_ack(struct drbd_connection *connection)
  616. {
  617. struct drbd_socket *sock;
  618. sock = &connection->meta;
  619. if (!conn_prepare_command(connection, sock))
  620. return -EIO;
  621. return conn_send_command(connection, sock, P_PING_ACK, 0, NULL, 0);
  622. }
  623. int drbd_send_sync_param(struct drbd_peer_device *peer_device)
  624. {
  625. struct drbd_socket *sock;
  626. struct p_rs_param_95 *p;
  627. int size;
  628. const int apv = peer_device->connection->agreed_pro_version;
  629. enum drbd_packet cmd;
  630. struct net_conf *nc;
  631. struct disk_conf *dc;
  632. sock = &peer_device->connection->data;
  633. p = drbd_prepare_command(peer_device, sock);
  634. if (!p)
  635. return -EIO;
  636. rcu_read_lock();
  637. nc = rcu_dereference(peer_device->connection->net_conf);
  638. size = apv <= 87 ? sizeof(struct p_rs_param)
  639. : apv == 88 ? sizeof(struct p_rs_param)
  640. + strlen(nc->verify_alg) + 1
  641. : apv <= 94 ? sizeof(struct p_rs_param_89)
  642. : /* apv >= 95 */ sizeof(struct p_rs_param_95);
  643. cmd = apv >= 89 ? P_SYNC_PARAM89 : P_SYNC_PARAM;
  644. /* initialize verify_alg and csums_alg */
  645. memset(p->verify_alg, 0, 2 * SHARED_SECRET_MAX);
  646. if (get_ldev(peer_device->device)) {
  647. dc = rcu_dereference(peer_device->device->ldev->disk_conf);
  648. p->resync_rate = cpu_to_be32(dc->resync_rate);
  649. p->c_plan_ahead = cpu_to_be32(dc->c_plan_ahead);
  650. p->c_delay_target = cpu_to_be32(dc->c_delay_target);
  651. p->c_fill_target = cpu_to_be32(dc->c_fill_target);
  652. p->c_max_rate = cpu_to_be32(dc->c_max_rate);
  653. put_ldev(peer_device->device);
  654. } else {
  655. p->resync_rate = cpu_to_be32(DRBD_RESYNC_RATE_DEF);
  656. p->c_plan_ahead = cpu_to_be32(DRBD_C_PLAN_AHEAD_DEF);
  657. p->c_delay_target = cpu_to_be32(DRBD_C_DELAY_TARGET_DEF);
  658. p->c_fill_target = cpu_to_be32(DRBD_C_FILL_TARGET_DEF);
  659. p->c_max_rate = cpu_to_be32(DRBD_C_MAX_RATE_DEF);
  660. }
  661. if (apv >= 88)
  662. strcpy(p->verify_alg, nc->verify_alg);
  663. if (apv >= 89)
  664. strcpy(p->csums_alg, nc->csums_alg);
  665. rcu_read_unlock();
  666. return drbd_send_command(peer_device, sock, cmd, size, NULL, 0);
  667. }
  668. int __drbd_send_protocol(struct drbd_connection *connection, enum drbd_packet cmd)
  669. {
  670. struct drbd_socket *sock;
  671. struct p_protocol *p;
  672. struct net_conf *nc;
  673. int size, cf;
  674. sock = &connection->data;
  675. p = __conn_prepare_command(connection, sock);
  676. if (!p)
  677. return -EIO;
  678. rcu_read_lock();
  679. nc = rcu_dereference(connection->net_conf);
  680. if (nc->tentative && connection->agreed_pro_version < 92) {
  681. rcu_read_unlock();
  682. mutex_unlock(&sock->mutex);
  683. drbd_err(connection, "--dry-run is not supported by peer");
  684. return -EOPNOTSUPP;
  685. }
  686. size = sizeof(*p);
  687. if (connection->agreed_pro_version >= 87)
  688. size += strlen(nc->integrity_alg) + 1;
  689. p->protocol = cpu_to_be32(nc->wire_protocol);
  690. p->after_sb_0p = cpu_to_be32(nc->after_sb_0p);
  691. p->after_sb_1p = cpu_to_be32(nc->after_sb_1p);
  692. p->after_sb_2p = cpu_to_be32(nc->after_sb_2p);
  693. p->two_primaries = cpu_to_be32(nc->two_primaries);
  694. cf = 0;
  695. if (nc->discard_my_data)
  696. cf |= CF_DISCARD_MY_DATA;
  697. if (nc->tentative)
  698. cf |= CF_DRY_RUN;
  699. p->conn_flags = cpu_to_be32(cf);
  700. if (connection->agreed_pro_version >= 87)
  701. strcpy(p->integrity_alg, nc->integrity_alg);
  702. rcu_read_unlock();
  703. return __conn_send_command(connection, sock, cmd, size, NULL, 0);
  704. }
  705. int drbd_send_protocol(struct drbd_connection *connection)
  706. {
  707. int err;
  708. mutex_lock(&connection->data.mutex);
  709. err = __drbd_send_protocol(connection, P_PROTOCOL);
  710. mutex_unlock(&connection->data.mutex);
  711. return err;
  712. }
  713. static int _drbd_send_uuids(struct drbd_peer_device *peer_device, u64 uuid_flags)
  714. {
  715. struct drbd_device *device = peer_device->device;
  716. struct drbd_socket *sock;
  717. struct p_uuids *p;
  718. int i;
  719. if (!get_ldev_if_state(device, D_NEGOTIATING))
  720. return 0;
  721. sock = &peer_device->connection->data;
  722. p = drbd_prepare_command(peer_device, sock);
  723. if (!p) {
  724. put_ldev(device);
  725. return -EIO;
  726. }
  727. spin_lock_irq(&device->ldev->md.uuid_lock);
  728. for (i = UI_CURRENT; i < UI_SIZE; i++)
  729. p->uuid[i] = cpu_to_be64(device->ldev->md.uuid[i]);
  730. spin_unlock_irq(&device->ldev->md.uuid_lock);
  731. device->comm_bm_set = drbd_bm_total_weight(device);
  732. p->uuid[UI_SIZE] = cpu_to_be64(device->comm_bm_set);
  733. rcu_read_lock();
  734. uuid_flags |= rcu_dereference(peer_device->connection->net_conf)->discard_my_data ? 1 : 0;
  735. rcu_read_unlock();
  736. uuid_flags |= test_bit(CRASHED_PRIMARY, &device->flags) ? 2 : 0;
  737. uuid_flags |= device->new_state_tmp.disk == D_INCONSISTENT ? 4 : 0;
  738. p->uuid[UI_FLAGS] = cpu_to_be64(uuid_flags);
  739. put_ldev(device);
  740. return drbd_send_command(peer_device, sock, P_UUIDS, sizeof(*p), NULL, 0);
  741. }
  742. int drbd_send_uuids(struct drbd_peer_device *peer_device)
  743. {
  744. return _drbd_send_uuids(peer_device, 0);
  745. }
  746. int drbd_send_uuids_skip_initial_sync(struct drbd_peer_device *peer_device)
  747. {
  748. return _drbd_send_uuids(peer_device, 8);
  749. }
  750. void drbd_print_uuids(struct drbd_device *device, const char *text)
  751. {
  752. if (get_ldev_if_state(device, D_NEGOTIATING)) {
  753. u64 *uuid = device->ldev->md.uuid;
  754. drbd_info(device, "%s %016llX:%016llX:%016llX:%016llX\n",
  755. text,
  756. (unsigned long long)uuid[UI_CURRENT],
  757. (unsigned long long)uuid[UI_BITMAP],
  758. (unsigned long long)uuid[UI_HISTORY_START],
  759. (unsigned long long)uuid[UI_HISTORY_END]);
  760. put_ldev(device);
  761. } else {
  762. drbd_info(device, "%s effective data uuid: %016llX\n",
  763. text,
  764. (unsigned long long)device->ed_uuid);
  765. }
  766. }
  767. void drbd_gen_and_send_sync_uuid(struct drbd_peer_device *peer_device)
  768. {
  769. struct drbd_device *device = peer_device->device;
  770. struct drbd_socket *sock;
  771. struct p_rs_uuid *p;
  772. u64 uuid;
  773. D_ASSERT(device, device->state.disk == D_UP_TO_DATE);
  774. uuid = device->ldev->md.uuid[UI_BITMAP];
  775. if (uuid && uuid != UUID_JUST_CREATED)
  776. uuid = uuid + UUID_NEW_BM_OFFSET;
  777. else
  778. get_random_bytes(&uuid, sizeof(u64));
  779. drbd_uuid_set(device, UI_BITMAP, uuid);
  780. drbd_print_uuids(device, "updated sync UUID");
  781. drbd_md_sync(device);
  782. sock = &peer_device->connection->data;
  783. p = drbd_prepare_command(peer_device, sock);
  784. if (p) {
  785. p->uuid = cpu_to_be64(uuid);
  786. drbd_send_command(peer_device, sock, P_SYNC_UUID, sizeof(*p), NULL, 0);
  787. }
  788. }
  789. /* communicated if (agreed_features & DRBD_FF_WSAME) */
  790. static void
  791. assign_p_sizes_qlim(struct drbd_device *device, struct p_sizes *p,
  792. struct request_queue *q)
  793. {
  794. if (q) {
  795. p->qlim->physical_block_size = cpu_to_be32(queue_physical_block_size(q));
  796. p->qlim->logical_block_size = cpu_to_be32(queue_logical_block_size(q));
  797. p->qlim->alignment_offset = cpu_to_be32(queue_alignment_offset(q));
  798. p->qlim->io_min = cpu_to_be32(queue_io_min(q));
  799. p->qlim->io_opt = cpu_to_be32(queue_io_opt(q));
  800. p->qlim->discard_enabled = blk_queue_discard(q);
  801. p->qlim->write_same_capable = !!q->limits.max_write_same_sectors;
  802. } else {
  803. q = device->rq_queue;
  804. p->qlim->physical_block_size = cpu_to_be32(queue_physical_block_size(q));
  805. p->qlim->logical_block_size = cpu_to_be32(queue_logical_block_size(q));
  806. p->qlim->alignment_offset = 0;
  807. p->qlim->io_min = cpu_to_be32(queue_io_min(q));
  808. p->qlim->io_opt = cpu_to_be32(queue_io_opt(q));
  809. p->qlim->discard_enabled = 0;
  810. p->qlim->write_same_capable = 0;
  811. }
  812. }
  813. int drbd_send_sizes(struct drbd_peer_device *peer_device, int trigger_reply, enum dds_flags flags)
  814. {
  815. struct drbd_device *device = peer_device->device;
  816. struct drbd_socket *sock;
  817. struct p_sizes *p;
  818. sector_t d_size, u_size;
  819. int q_order_type;
  820. unsigned int max_bio_size;
  821. unsigned int packet_size;
  822. sock = &peer_device->connection->data;
  823. p = drbd_prepare_command(peer_device, sock);
  824. if (!p)
  825. return -EIO;
  826. packet_size = sizeof(*p);
  827. if (peer_device->connection->agreed_features & DRBD_FF_WSAME)
  828. packet_size += sizeof(p->qlim[0]);
  829. memset(p, 0, packet_size);
  830. if (get_ldev_if_state(device, D_NEGOTIATING)) {
  831. struct request_queue *q = bdev_get_queue(device->ldev->backing_bdev);
  832. d_size = drbd_get_max_capacity(device->ldev);
  833. rcu_read_lock();
  834. u_size = rcu_dereference(device->ldev->disk_conf)->disk_size;
  835. rcu_read_unlock();
  836. q_order_type = drbd_queue_order_type(device);
  837. max_bio_size = queue_max_hw_sectors(q) << 9;
  838. max_bio_size = min(max_bio_size, DRBD_MAX_BIO_SIZE);
  839. assign_p_sizes_qlim(device, p, q);
  840. put_ldev(device);
  841. } else {
  842. d_size = 0;
  843. u_size = 0;
  844. q_order_type = QUEUE_ORDERED_NONE;
  845. max_bio_size = DRBD_MAX_BIO_SIZE; /* ... multiple BIOs per peer_request */
  846. assign_p_sizes_qlim(device, p, NULL);
  847. }
  848. if (peer_device->connection->agreed_pro_version <= 94)
  849. max_bio_size = min(max_bio_size, DRBD_MAX_SIZE_H80_PACKET);
  850. else if (peer_device->connection->agreed_pro_version < 100)
  851. max_bio_size = min(max_bio_size, DRBD_MAX_BIO_SIZE_P95);
  852. p->d_size = cpu_to_be64(d_size);
  853. p->u_size = cpu_to_be64(u_size);
  854. p->c_size = cpu_to_be64(trigger_reply ? 0 : drbd_get_capacity(device->this_bdev));
  855. p->max_bio_size = cpu_to_be32(max_bio_size);
  856. p->queue_order_type = cpu_to_be16(q_order_type);
  857. p->dds_flags = cpu_to_be16(flags);
  858. return drbd_send_command(peer_device, sock, P_SIZES, packet_size, NULL, 0);
  859. }
  860. /**
  861. * drbd_send_current_state() - Sends the drbd state to the peer
  862. * @peer_device: DRBD peer device.
  863. */
  864. int drbd_send_current_state(struct drbd_peer_device *peer_device)
  865. {
  866. struct drbd_socket *sock;
  867. struct p_state *p;
  868. sock = &peer_device->connection->data;
  869. p = drbd_prepare_command(peer_device, sock);
  870. if (!p)
  871. return -EIO;
  872. p->state = cpu_to_be32(peer_device->device->state.i); /* Within the send mutex */
  873. return drbd_send_command(peer_device, sock, P_STATE, sizeof(*p), NULL, 0);
  874. }
  875. /**
  876. * drbd_send_state() - After a state change, sends the new state to the peer
  877. * @peer_device: DRBD peer device.
  878. * @state: the state to send, not necessarily the current state.
  879. *
  880. * Each state change queues an "after_state_ch" work, which will eventually
  881. * send the resulting new state to the peer. If more state changes happen
  882. * between queuing and processing of the after_state_ch work, we still
  883. * want to send each intermediary state in the order it occurred.
  884. */
  885. int drbd_send_state(struct drbd_peer_device *peer_device, union drbd_state state)
  886. {
  887. struct drbd_socket *sock;
  888. struct p_state *p;
  889. sock = &peer_device->connection->data;
  890. p = drbd_prepare_command(peer_device, sock);
  891. if (!p)
  892. return -EIO;
  893. p->state = cpu_to_be32(state.i); /* Within the send mutex */
  894. return drbd_send_command(peer_device, sock, P_STATE, sizeof(*p), NULL, 0);
  895. }
  896. int drbd_send_state_req(struct drbd_peer_device *peer_device, union drbd_state mask, union drbd_state val)
  897. {
  898. struct drbd_socket *sock;
  899. struct p_req_state *p;
  900. sock = &peer_device->connection->data;
  901. p = drbd_prepare_command(peer_device, sock);
  902. if (!p)
  903. return -EIO;
  904. p->mask = cpu_to_be32(mask.i);
  905. p->val = cpu_to_be32(val.i);
  906. return drbd_send_command(peer_device, sock, P_STATE_CHG_REQ, sizeof(*p), NULL, 0);
  907. }
  908. int conn_send_state_req(struct drbd_connection *connection, union drbd_state mask, union drbd_state val)
  909. {
  910. enum drbd_packet cmd;
  911. struct drbd_socket *sock;
  912. struct p_req_state *p;
  913. cmd = connection->agreed_pro_version < 100 ? P_STATE_CHG_REQ : P_CONN_ST_CHG_REQ;
  914. sock = &connection->data;
  915. p = conn_prepare_command(connection, sock);
  916. if (!p)
  917. return -EIO;
  918. p->mask = cpu_to_be32(mask.i);
  919. p->val = cpu_to_be32(val.i);
  920. return conn_send_command(connection, sock, cmd, sizeof(*p), NULL, 0);
  921. }
  922. void drbd_send_sr_reply(struct drbd_peer_device *peer_device, enum drbd_state_rv retcode)
  923. {
  924. struct drbd_socket *sock;
  925. struct p_req_state_reply *p;
  926. sock = &peer_device->connection->meta;
  927. p = drbd_prepare_command(peer_device, sock);
  928. if (p) {
  929. p->retcode = cpu_to_be32(retcode);
  930. drbd_send_command(peer_device, sock, P_STATE_CHG_REPLY, sizeof(*p), NULL, 0);
  931. }
  932. }
  933. void conn_send_sr_reply(struct drbd_connection *connection, enum drbd_state_rv retcode)
  934. {
  935. struct drbd_socket *sock;
  936. struct p_req_state_reply *p;
  937. enum drbd_packet cmd = connection->agreed_pro_version < 100 ? P_STATE_CHG_REPLY : P_CONN_ST_CHG_REPLY;
  938. sock = &connection->meta;
  939. p = conn_prepare_command(connection, sock);
  940. if (p) {
  941. p->retcode = cpu_to_be32(retcode);
  942. conn_send_command(connection, sock, cmd, sizeof(*p), NULL, 0);
  943. }
  944. }
  945. static void dcbp_set_code(struct p_compressed_bm *p, enum drbd_bitmap_code code)
  946. {
  947. BUG_ON(code & ~0xf);
  948. p->encoding = (p->encoding & ~0xf) | code;
  949. }
  950. static void dcbp_set_start(struct p_compressed_bm *p, int set)
  951. {
  952. p->encoding = (p->encoding & ~0x80) | (set ? 0x80 : 0);
  953. }
  954. static void dcbp_set_pad_bits(struct p_compressed_bm *p, int n)
  955. {
  956. BUG_ON(n & ~0x7);
  957. p->encoding = (p->encoding & (~0x7 << 4)) | (n << 4);
  958. }
  959. static int fill_bitmap_rle_bits(struct drbd_device *device,
  960. struct p_compressed_bm *p,
  961. unsigned int size,
  962. struct bm_xfer_ctx *c)
  963. {
  964. struct bitstream bs;
  965. unsigned long plain_bits;
  966. unsigned long tmp;
  967. unsigned long rl;
  968. unsigned len;
  969. unsigned toggle;
  970. int bits, use_rle;
  971. /* may we use this feature? */
  972. rcu_read_lock();
  973. use_rle = rcu_dereference(first_peer_device(device)->connection->net_conf)->use_rle;
  974. rcu_read_unlock();
  975. if (!use_rle || first_peer_device(device)->connection->agreed_pro_version < 90)
  976. return 0;
  977. if (c->bit_offset >= c->bm_bits)
  978. return 0; /* nothing to do. */
  979. /* use at most thus many bytes */
  980. bitstream_init(&bs, p->code, size, 0);
  981. memset(p->code, 0, size);
  982. /* plain bits covered in this code string */
  983. plain_bits = 0;
  984. /* p->encoding & 0x80 stores whether the first run length is set.
  985. * bit offset is implicit.
  986. * start with toggle == 2 to be able to tell the first iteration */
  987. toggle = 2;
  988. /* see how much plain bits we can stuff into one packet
  989. * using RLE and VLI. */
  990. do {
  991. tmp = (toggle == 0) ? _drbd_bm_find_next_zero(device, c->bit_offset)
  992. : _drbd_bm_find_next(device, c->bit_offset);
  993. if (tmp == -1UL)
  994. tmp = c->bm_bits;
  995. rl = tmp - c->bit_offset;
  996. if (toggle == 2) { /* first iteration */
  997. if (rl == 0) {
  998. /* the first checked bit was set,
  999. * store start value, */
  1000. dcbp_set_start(p, 1);
  1001. /* but skip encoding of zero run length */
  1002. toggle = !toggle;
  1003. continue;
  1004. }
  1005. dcbp_set_start(p, 0);
  1006. }
  1007. /* paranoia: catch zero runlength.
  1008. * can only happen if bitmap is modified while we scan it. */
  1009. if (rl == 0) {
  1010. drbd_err(device, "unexpected zero runlength while encoding bitmap "
  1011. "t:%u bo:%lu\n", toggle, c->bit_offset);
  1012. return -1;
  1013. }
  1014. bits = vli_encode_bits(&bs, rl);
  1015. if (bits == -ENOBUFS) /* buffer full */
  1016. break;
  1017. if (bits <= 0) {
  1018. drbd_err(device, "error while encoding bitmap: %d\n", bits);
  1019. return 0;
  1020. }
  1021. toggle = !toggle;
  1022. plain_bits += rl;
  1023. c->bit_offset = tmp;
  1024. } while (c->bit_offset < c->bm_bits);
  1025. len = bs.cur.b - p->code + !!bs.cur.bit;
  1026. if (plain_bits < (len << 3)) {
  1027. /* incompressible with this method.
  1028. * we need to rewind both word and bit position. */
  1029. c->bit_offset -= plain_bits;
  1030. bm_xfer_ctx_bit_to_word_offset(c);
  1031. c->bit_offset = c->word_offset * BITS_PER_LONG;
  1032. return 0;
  1033. }
  1034. /* RLE + VLI was able to compress it just fine.
  1035. * update c->word_offset. */
  1036. bm_xfer_ctx_bit_to_word_offset(c);
  1037. /* store pad_bits */
  1038. dcbp_set_pad_bits(p, (8 - bs.cur.bit) & 0x7);
  1039. return len;
  1040. }
  1041. /**
  1042. * send_bitmap_rle_or_plain
  1043. *
  1044. * Return 0 when done, 1 when another iteration is needed, and a negative error
  1045. * code upon failure.
  1046. */
  1047. static int
  1048. send_bitmap_rle_or_plain(struct drbd_device *device, struct bm_xfer_ctx *c)
  1049. {
  1050. struct drbd_socket *sock = &first_peer_device(device)->connection->data;
  1051. unsigned int header_size = drbd_header_size(first_peer_device(device)->connection);
  1052. struct p_compressed_bm *p = sock->sbuf + header_size;
  1053. int len, err;
  1054. len = fill_bitmap_rle_bits(device, p,
  1055. DRBD_SOCKET_BUFFER_SIZE - header_size - sizeof(*p), c);
  1056. if (len < 0)
  1057. return -EIO;
  1058. if (len) {
  1059. dcbp_set_code(p, RLE_VLI_Bits);
  1060. err = __send_command(first_peer_device(device)->connection, device->vnr, sock,
  1061. P_COMPRESSED_BITMAP, sizeof(*p) + len,
  1062. NULL, 0);
  1063. c->packets[0]++;
  1064. c->bytes[0] += header_size + sizeof(*p) + len;
  1065. if (c->bit_offset >= c->bm_bits)
  1066. len = 0; /* DONE */
  1067. } else {
  1068. /* was not compressible.
  1069. * send a buffer full of plain text bits instead. */
  1070. unsigned int data_size;
  1071. unsigned long num_words;
  1072. unsigned long *p = sock->sbuf + header_size;
  1073. data_size = DRBD_SOCKET_BUFFER_SIZE - header_size;
  1074. num_words = min_t(size_t, data_size / sizeof(*p),
  1075. c->bm_words - c->word_offset);
  1076. len = num_words * sizeof(*p);
  1077. if (len)
  1078. drbd_bm_get_lel(device, c->word_offset, num_words, p);
  1079. err = __send_command(first_peer_device(device)->connection, device->vnr, sock, P_BITMAP, len, NULL, 0);
  1080. c->word_offset += num_words;
  1081. c->bit_offset = c->word_offset * BITS_PER_LONG;
  1082. c->packets[1]++;
  1083. c->bytes[1] += header_size + len;
  1084. if (c->bit_offset > c->bm_bits)
  1085. c->bit_offset = c->bm_bits;
  1086. }
  1087. if (!err) {
  1088. if (len == 0) {
  1089. INFO_bm_xfer_stats(device, "send", c);
  1090. return 0;
  1091. } else
  1092. return 1;
  1093. }
  1094. return -EIO;
  1095. }
  1096. /* See the comment at receive_bitmap() */
  1097. static int _drbd_send_bitmap(struct drbd_device *device)
  1098. {
  1099. struct bm_xfer_ctx c;
  1100. int err;
  1101. if (!expect(device->bitmap))
  1102. return false;
  1103. if (get_ldev(device)) {
  1104. if (drbd_md_test_flag(device->ldev, MDF_FULL_SYNC)) {
  1105. drbd_info(device, "Writing the whole bitmap, MDF_FullSync was set.\n");
  1106. drbd_bm_set_all(device);
  1107. if (drbd_bm_write(device)) {
  1108. /* write_bm did fail! Leave full sync flag set in Meta P_DATA
  1109. * but otherwise process as per normal - need to tell other
  1110. * side that a full resync is required! */
  1111. drbd_err(device, "Failed to write bitmap to disk!\n");
  1112. } else {
  1113. drbd_md_clear_flag(device, MDF_FULL_SYNC);
  1114. drbd_md_sync(device);
  1115. }
  1116. }
  1117. put_ldev(device);
  1118. }
  1119. c = (struct bm_xfer_ctx) {
  1120. .bm_bits = drbd_bm_bits(device),
  1121. .bm_words = drbd_bm_words(device),
  1122. };
  1123. do {
  1124. err = send_bitmap_rle_or_plain(device, &c);
  1125. } while (err > 0);
  1126. return err == 0;
  1127. }
  1128. int drbd_send_bitmap(struct drbd_device *device)
  1129. {
  1130. struct drbd_socket *sock = &first_peer_device(device)->connection->data;
  1131. int err = -1;
  1132. mutex_lock(&sock->mutex);
  1133. if (sock->socket)
  1134. err = !_drbd_send_bitmap(device);
  1135. mutex_unlock(&sock->mutex);
  1136. return err;
  1137. }
  1138. void drbd_send_b_ack(struct drbd_connection *connection, u32 barrier_nr, u32 set_size)
  1139. {
  1140. struct drbd_socket *sock;
  1141. struct p_barrier_ack *p;
  1142. if (connection->cstate < C_WF_REPORT_PARAMS)
  1143. return;
  1144. sock = &connection->meta;
  1145. p = conn_prepare_command(connection, sock);
  1146. if (!p)
  1147. return;
  1148. p->barrier = barrier_nr;
  1149. p->set_size = cpu_to_be32(set_size);
  1150. conn_send_command(connection, sock, P_BARRIER_ACK, sizeof(*p), NULL, 0);
  1151. }
  1152. /**
  1153. * _drbd_send_ack() - Sends an ack packet
  1154. * @device: DRBD device.
  1155. * @cmd: Packet command code.
  1156. * @sector: sector, needs to be in big endian byte order
  1157. * @blksize: size in byte, needs to be in big endian byte order
  1158. * @block_id: Id, big endian byte order
  1159. */
  1160. static int _drbd_send_ack(struct drbd_peer_device *peer_device, enum drbd_packet cmd,
  1161. u64 sector, u32 blksize, u64 block_id)
  1162. {
  1163. struct drbd_socket *sock;
  1164. struct p_block_ack *p;
  1165. if (peer_device->device->state.conn < C_CONNECTED)
  1166. return -EIO;
  1167. sock = &peer_device->connection->meta;
  1168. p = drbd_prepare_command(peer_device, sock);
  1169. if (!p)
  1170. return -EIO;
  1171. p->sector = sector;
  1172. p->block_id = block_id;
  1173. p->blksize = blksize;
  1174. p->seq_num = cpu_to_be32(atomic_inc_return(&peer_device->device->packet_seq));
  1175. return drbd_send_command(peer_device, sock, cmd, sizeof(*p), NULL, 0);
  1176. }
  1177. /* dp->sector and dp->block_id already/still in network byte order,
  1178. * data_size is payload size according to dp->head,
  1179. * and may need to be corrected for digest size. */
  1180. void drbd_send_ack_dp(struct drbd_peer_device *peer_device, enum drbd_packet cmd,
  1181. struct p_data *dp, int data_size)
  1182. {
  1183. if (peer_device->connection->peer_integrity_tfm)
  1184. data_size -= crypto_ahash_digestsize(peer_device->connection->peer_integrity_tfm);
  1185. _drbd_send_ack(peer_device, cmd, dp->sector, cpu_to_be32(data_size),
  1186. dp->block_id);
  1187. }
  1188. void drbd_send_ack_rp(struct drbd_peer_device *peer_device, enum drbd_packet cmd,
  1189. struct p_block_req *rp)
  1190. {
  1191. _drbd_send_ack(peer_device, cmd, rp->sector, rp->blksize, rp->block_id);
  1192. }
  1193. /**
  1194. * drbd_send_ack() - Sends an ack packet
  1195. * @device: DRBD device
  1196. * @cmd: packet command code
  1197. * @peer_req: peer request
  1198. */
  1199. int drbd_send_ack(struct drbd_peer_device *peer_device, enum drbd_packet cmd,
  1200. struct drbd_peer_request *peer_req)
  1201. {
  1202. return _drbd_send_ack(peer_device, cmd,
  1203. cpu_to_be64(peer_req->i.sector),
  1204. cpu_to_be32(peer_req->i.size),
  1205. peer_req->block_id);
  1206. }
  1207. /* This function misuses the block_id field to signal if the blocks
  1208. * are is sync or not. */
  1209. int drbd_send_ack_ex(struct drbd_peer_device *peer_device, enum drbd_packet cmd,
  1210. sector_t sector, int blksize, u64 block_id)
  1211. {
  1212. return _drbd_send_ack(peer_device, cmd,
  1213. cpu_to_be64(sector),
  1214. cpu_to_be32(blksize),
  1215. cpu_to_be64(block_id));
  1216. }
  1217. int drbd_send_rs_deallocated(struct drbd_peer_device *peer_device,
  1218. struct drbd_peer_request *peer_req)
  1219. {
  1220. struct drbd_socket *sock;
  1221. struct p_block_desc *p;
  1222. sock = &peer_device->connection->data;
  1223. p = drbd_prepare_command(peer_device, sock);
  1224. if (!p)
  1225. return -EIO;
  1226. p->sector = cpu_to_be64(peer_req->i.sector);
  1227. p->blksize = cpu_to_be32(peer_req->i.size);
  1228. p->pad = 0;
  1229. return drbd_send_command(peer_device, sock, P_RS_DEALLOCATED, sizeof(*p), NULL, 0);
  1230. }
  1231. int drbd_send_drequest(struct drbd_peer_device *peer_device, int cmd,
  1232. sector_t sector, int size, u64 block_id)
  1233. {
  1234. struct drbd_socket *sock;
  1235. struct p_block_req *p;
  1236. sock = &peer_device->connection->data;
  1237. p = drbd_prepare_command(peer_device, sock);
  1238. if (!p)
  1239. return -EIO;
  1240. p->sector = cpu_to_be64(sector);
  1241. p->block_id = block_id;
  1242. p->blksize = cpu_to_be32(size);
  1243. return drbd_send_command(peer_device, sock, cmd, sizeof(*p), NULL, 0);
  1244. }
  1245. int drbd_send_drequest_csum(struct drbd_peer_device *peer_device, sector_t sector, int size,
  1246. void *digest, int digest_size, enum drbd_packet cmd)
  1247. {
  1248. struct drbd_socket *sock;
  1249. struct p_block_req *p;
  1250. /* FIXME: Put the digest into the preallocated socket buffer. */
  1251. sock = &peer_device->connection->data;
  1252. p = drbd_prepare_command(peer_device, sock);
  1253. if (!p)
  1254. return -EIO;
  1255. p->sector = cpu_to_be64(sector);
  1256. p->block_id = ID_SYNCER /* unused */;
  1257. p->blksize = cpu_to_be32(size);
  1258. return drbd_send_command(peer_device, sock, cmd, sizeof(*p), digest, digest_size);
  1259. }
  1260. int drbd_send_ov_request(struct drbd_peer_device *peer_device, sector_t sector, int size)
  1261. {
  1262. struct drbd_socket *sock;
  1263. struct p_block_req *p;
  1264. sock = &peer_device->connection->data;
  1265. p = drbd_prepare_command(peer_device, sock);
  1266. if (!p)
  1267. return -EIO;
  1268. p->sector = cpu_to_be64(sector);
  1269. p->block_id = ID_SYNCER /* unused */;
  1270. p->blksize = cpu_to_be32(size);
  1271. return drbd_send_command(peer_device, sock, P_OV_REQUEST, sizeof(*p), NULL, 0);
  1272. }
  1273. /* called on sndtimeo
  1274. * returns false if we should retry,
  1275. * true if we think connection is dead
  1276. */
  1277. static int we_should_drop_the_connection(struct drbd_connection *connection, struct socket *sock)
  1278. {
  1279. int drop_it;
  1280. /* long elapsed = (long)(jiffies - device->last_received); */
  1281. drop_it = connection->meta.socket == sock
  1282. || !connection->ack_receiver.task
  1283. || get_t_state(&connection->ack_receiver) != RUNNING
  1284. || connection->cstate < C_WF_REPORT_PARAMS;
  1285. if (drop_it)
  1286. return true;
  1287. drop_it = !--connection->ko_count;
  1288. if (!drop_it) {
  1289. drbd_err(connection, "[%s/%d] sock_sendmsg time expired, ko = %u\n",
  1290. current->comm, current->pid, connection->ko_count);
  1291. request_ping(connection);
  1292. }
  1293. return drop_it; /* && (device->state == R_PRIMARY) */;
  1294. }
  1295. static void drbd_update_congested(struct drbd_connection *connection)
  1296. {
  1297. struct sock *sk = connection->data.socket->sk;
  1298. if (sk->sk_wmem_queued > sk->sk_sndbuf * 4 / 5)
  1299. set_bit(NET_CONGESTED, &connection->flags);
  1300. }
  1301. /* The idea of sendpage seems to be to put some kind of reference
  1302. * to the page into the skb, and to hand it over to the NIC. In
  1303. * this process get_page() gets called.
  1304. *
  1305. * As soon as the page was really sent over the network put_page()
  1306. * gets called by some part of the network layer. [ NIC driver? ]
  1307. *
  1308. * [ get_page() / put_page() increment/decrement the count. If count
  1309. * reaches 0 the page will be freed. ]
  1310. *
  1311. * This works nicely with pages from FSs.
  1312. * But this means that in protocol A we might signal IO completion too early!
  1313. *
  1314. * In order not to corrupt data during a resync we must make sure
  1315. * that we do not reuse our own buffer pages (EEs) to early, therefore
  1316. * we have the net_ee list.
  1317. *
  1318. * XFS seems to have problems, still, it submits pages with page_count == 0!
  1319. * As a workaround, we disable sendpage on pages
  1320. * with page_count == 0 or PageSlab.
  1321. */
  1322. static int _drbd_no_send_page(struct drbd_peer_device *peer_device, struct page *page,
  1323. int offset, size_t size, unsigned msg_flags)
  1324. {
  1325. struct socket *socket;
  1326. void *addr;
  1327. int err;
  1328. socket = peer_device->connection->data.socket;
  1329. addr = kmap(page) + offset;
  1330. err = drbd_send_all(peer_device->connection, socket, addr, size, msg_flags);
  1331. kunmap(page);
  1332. if (!err)
  1333. peer_device->device->send_cnt += size >> 9;
  1334. return err;
  1335. }
  1336. static int _drbd_send_page(struct drbd_peer_device *peer_device, struct page *page,
  1337. int offset, size_t size, unsigned msg_flags)
  1338. {
  1339. struct socket *socket = peer_device->connection->data.socket;
  1340. int len = size;
  1341. int err = -EIO;
  1342. /* e.g. XFS meta- & log-data is in slab pages, which have a
  1343. * page_count of 0 and/or have PageSlab() set.
  1344. * we cannot use send_page for those, as that does get_page();
  1345. * put_page(); and would cause either a VM_BUG directly, or
  1346. * __page_cache_release a page that would actually still be referenced
  1347. * by someone, leading to some obscure delayed Oops somewhere else. */
  1348. if (drbd_disable_sendpage || (page_count(page) < 1) || PageSlab(page))
  1349. return _drbd_no_send_page(peer_device, page, offset, size, msg_flags);
  1350. msg_flags |= MSG_NOSIGNAL;
  1351. drbd_update_congested(peer_device->connection);
  1352. do {
  1353. int sent;
  1354. sent = socket->ops->sendpage(socket, page, offset, len, msg_flags);
  1355. if (sent <= 0) {
  1356. if (sent == -EAGAIN) {
  1357. if (we_should_drop_the_connection(peer_device->connection, socket))
  1358. break;
  1359. continue;
  1360. }
  1361. drbd_warn(peer_device->device, "%s: size=%d len=%d sent=%d\n",
  1362. __func__, (int)size, len, sent);
  1363. if (sent < 0)
  1364. err = sent;
  1365. break;
  1366. }
  1367. len -= sent;
  1368. offset += sent;
  1369. } while (len > 0 /* THINK && device->cstate >= C_CONNECTED*/);
  1370. clear_bit(NET_CONGESTED, &peer_device->connection->flags);
  1371. if (len == 0) {
  1372. err = 0;
  1373. peer_device->device->send_cnt += size >> 9;
  1374. }
  1375. return err;
  1376. }
  1377. static int _drbd_send_bio(struct drbd_peer_device *peer_device, struct bio *bio)
  1378. {
  1379. struct bio_vec bvec;
  1380. struct bvec_iter iter;
  1381. /* hint all but last page with MSG_MORE */
  1382. bio_for_each_segment(bvec, bio, iter) {
  1383. int err;
  1384. err = _drbd_no_send_page(peer_device, bvec.bv_page,
  1385. bvec.bv_offset, bvec.bv_len,
  1386. bio_iter_last(bvec, iter)
  1387. ? 0 : MSG_MORE);
  1388. if (err)
  1389. return err;
  1390. /* REQ_OP_WRITE_SAME has only one segment */
  1391. if (bio_op(bio) == REQ_OP_WRITE_SAME)
  1392. break;
  1393. }
  1394. return 0;
  1395. }
  1396. static int _drbd_send_zc_bio(struct drbd_peer_device *peer_device, struct bio *bio)
  1397. {
  1398. struct bio_vec bvec;
  1399. struct bvec_iter iter;
  1400. /* hint all but last page with MSG_MORE */
  1401. bio_for_each_segment(bvec, bio, iter) {
  1402. int err;
  1403. err = _drbd_send_page(peer_device, bvec.bv_page,
  1404. bvec.bv_offset, bvec.bv_len,
  1405. bio_iter_last(bvec, iter) ? 0 : MSG_MORE);
  1406. if (err)
  1407. return err;
  1408. /* REQ_OP_WRITE_SAME has only one segment */
  1409. if (bio_op(bio) == REQ_OP_WRITE_SAME)
  1410. break;
  1411. }
  1412. return 0;
  1413. }
  1414. static int _drbd_send_zc_ee(struct drbd_peer_device *peer_device,
  1415. struct drbd_peer_request *peer_req)
  1416. {
  1417. struct page *page = peer_req->pages;
  1418. unsigned len = peer_req->i.size;
  1419. int err;
  1420. /* hint all but last page with MSG_MORE */
  1421. page_chain_for_each(page) {
  1422. unsigned l = min_t(unsigned, len, PAGE_SIZE);
  1423. err = _drbd_send_page(peer_device, page, 0, l,
  1424. page_chain_next(page) ? MSG_MORE : 0);
  1425. if (err)
  1426. return err;
  1427. len -= l;
  1428. }
  1429. return 0;
  1430. }
  1431. static u32 bio_flags_to_wire(struct drbd_connection *connection,
  1432. struct bio *bio)
  1433. {
  1434. if (connection->agreed_pro_version >= 95)
  1435. return (bio->bi_opf & REQ_SYNC ? DP_RW_SYNC : 0) |
  1436. (bio->bi_opf & REQ_FUA ? DP_FUA : 0) |
  1437. (bio->bi_opf & REQ_PREFLUSH ? DP_FLUSH : 0) |
  1438. (bio_op(bio) == REQ_OP_WRITE_SAME ? DP_WSAME : 0) |
  1439. (bio_op(bio) == REQ_OP_DISCARD ? DP_DISCARD : 0) |
  1440. (bio_op(bio) == REQ_OP_WRITE_ZEROES ? DP_DISCARD : 0);
  1441. else
  1442. return bio->bi_opf & REQ_SYNC ? DP_RW_SYNC : 0;
  1443. }
  1444. /* Used to send write or TRIM aka REQ_DISCARD requests
  1445. * R_PRIMARY -> Peer (P_DATA, P_TRIM)
  1446. */
  1447. int drbd_send_dblock(struct drbd_peer_device *peer_device, struct drbd_request *req)
  1448. {
  1449. struct drbd_device *device = peer_device->device;
  1450. struct drbd_socket *sock;
  1451. struct p_data *p;
  1452. struct p_wsame *wsame = NULL;
  1453. void *digest_out;
  1454. unsigned int dp_flags = 0;
  1455. int digest_size;
  1456. int err;
  1457. sock = &peer_device->connection->data;
  1458. p = drbd_prepare_command(peer_device, sock);
  1459. digest_size = peer_device->connection->integrity_tfm ?
  1460. crypto_ahash_digestsize(peer_device->connection->integrity_tfm) : 0;
  1461. if (!p)
  1462. return -EIO;
  1463. p->sector = cpu_to_be64(req->i.sector);
  1464. p->block_id = (unsigned long)req;
  1465. p->seq_num = cpu_to_be32(atomic_inc_return(&device->packet_seq));
  1466. dp_flags = bio_flags_to_wire(peer_device->connection, req->master_bio);
  1467. if (device->state.conn >= C_SYNC_SOURCE &&
  1468. device->state.conn <= C_PAUSED_SYNC_T)
  1469. dp_flags |= DP_MAY_SET_IN_SYNC;
  1470. if (peer_device->connection->agreed_pro_version >= 100) {
  1471. if (req->rq_state & RQ_EXP_RECEIVE_ACK)
  1472. dp_flags |= DP_SEND_RECEIVE_ACK;
  1473. /* During resync, request an explicit write ack,
  1474. * even in protocol != C */
  1475. if (req->rq_state & RQ_EXP_WRITE_ACK
  1476. || (dp_flags & DP_MAY_SET_IN_SYNC))
  1477. dp_flags |= DP_SEND_WRITE_ACK;
  1478. }
  1479. p->dp_flags = cpu_to_be32(dp_flags);
  1480. if (dp_flags & DP_DISCARD) {
  1481. struct p_trim *t = (struct p_trim*)p;
  1482. t->size = cpu_to_be32(req->i.size);
  1483. err = __send_command(peer_device->connection, device->vnr, sock, P_TRIM, sizeof(*t), NULL, 0);
  1484. goto out;
  1485. }
  1486. if (dp_flags & DP_WSAME) {
  1487. /* this will only work if DRBD_FF_WSAME is set AND the
  1488. * handshake agreed that all nodes and backend devices are
  1489. * WRITE_SAME capable and agree on logical_block_size */
  1490. wsame = (struct p_wsame*)p;
  1491. digest_out = wsame + 1;
  1492. wsame->size = cpu_to_be32(req->i.size);
  1493. } else
  1494. digest_out = p + 1;
  1495. /* our digest is still only over the payload.
  1496. * TRIM does not carry any payload. */
  1497. if (digest_size)
  1498. drbd_csum_bio(peer_device->connection->integrity_tfm, req->master_bio, digest_out);
  1499. if (wsame) {
  1500. err =
  1501. __send_command(peer_device->connection, device->vnr, sock, P_WSAME,
  1502. sizeof(*wsame) + digest_size, NULL,
  1503. bio_iovec(req->master_bio).bv_len);
  1504. } else
  1505. err =
  1506. __send_command(peer_device->connection, device->vnr, sock, P_DATA,
  1507. sizeof(*p) + digest_size, NULL, req->i.size);
  1508. if (!err) {
  1509. /* For protocol A, we have to memcpy the payload into
  1510. * socket buffers, as we may complete right away
  1511. * as soon as we handed it over to tcp, at which point the data
  1512. * pages may become invalid.
  1513. *
  1514. * For data-integrity enabled, we copy it as well, so we can be
  1515. * sure that even if the bio pages may still be modified, it
  1516. * won't change the data on the wire, thus if the digest checks
  1517. * out ok after sending on this side, but does not fit on the
  1518. * receiving side, we sure have detected corruption elsewhere.
  1519. */
  1520. if (!(req->rq_state & (RQ_EXP_RECEIVE_ACK | RQ_EXP_WRITE_ACK)) || digest_size)
  1521. err = _drbd_send_bio(peer_device, req->master_bio);
  1522. else
  1523. err = _drbd_send_zc_bio(peer_device, req->master_bio);
  1524. /* double check digest, sometimes buffers have been modified in flight. */
  1525. if (digest_size > 0 && digest_size <= 64) {
  1526. /* 64 byte, 512 bit, is the largest digest size
  1527. * currently supported in kernel crypto. */
  1528. unsigned char digest[64];
  1529. drbd_csum_bio(peer_device->connection->integrity_tfm, req->master_bio, digest);
  1530. if (memcmp(p + 1, digest, digest_size)) {
  1531. drbd_warn(device,
  1532. "Digest mismatch, buffer modified by upper layers during write: %llus +%u\n",
  1533. (unsigned long long)req->i.sector, req->i.size);
  1534. }
  1535. } /* else if (digest_size > 64) {
  1536. ... Be noisy about digest too large ...
  1537. } */
  1538. }
  1539. out:
  1540. mutex_unlock(&sock->mutex); /* locked by drbd_prepare_command() */
  1541. return err;
  1542. }
  1543. /* answer packet, used to send data back for read requests:
  1544. * Peer -> (diskless) R_PRIMARY (P_DATA_REPLY)
  1545. * C_SYNC_SOURCE -> C_SYNC_TARGET (P_RS_DATA_REPLY)
  1546. */
  1547. int drbd_send_block(struct drbd_peer_device *peer_device, enum drbd_packet cmd,
  1548. struct drbd_peer_request *peer_req)
  1549. {
  1550. struct drbd_device *device = peer_device->device;
  1551. struct drbd_socket *sock;
  1552. struct p_data *p;
  1553. int err;
  1554. int digest_size;
  1555. sock = &peer_device->connection->data;
  1556. p = drbd_prepare_command(peer_device, sock);
  1557. digest_size = peer_device->connection->integrity_tfm ?
  1558. crypto_ahash_digestsize(peer_device->connection->integrity_tfm) : 0;
  1559. if (!p)
  1560. return -EIO;
  1561. p->sector = cpu_to_be64(peer_req->i.sector);
  1562. p->block_id = peer_req->block_id;
  1563. p->seq_num = 0; /* unused */
  1564. p->dp_flags = 0;
  1565. if (digest_size)
  1566. drbd_csum_ee(peer_device->connection->integrity_tfm, peer_req, p + 1);
  1567. err = __send_command(peer_device->connection, device->vnr, sock, cmd, sizeof(*p) + digest_size, NULL, peer_req->i.size);
  1568. if (!err)
  1569. err = _drbd_send_zc_ee(peer_device, peer_req);
  1570. mutex_unlock(&sock->mutex); /* locked by drbd_prepare_command() */
  1571. return err;
  1572. }
  1573. int drbd_send_out_of_sync(struct drbd_peer_device *peer_device, struct drbd_request *req)
  1574. {
  1575. struct drbd_socket *sock;
  1576. struct p_block_desc *p;
  1577. sock = &peer_device->connection->data;
  1578. p = drbd_prepare_command(peer_device, sock);
  1579. if (!p)
  1580. return -EIO;
  1581. p->sector = cpu_to_be64(req->i.sector);
  1582. p->blksize = cpu_to_be32(req->i.size);
  1583. return drbd_send_command(peer_device, sock, P_OUT_OF_SYNC, sizeof(*p), NULL, 0);
  1584. }
  1585. /*
  1586. drbd_send distinguishes two cases:
  1587. Packets sent via the data socket "sock"
  1588. and packets sent via the meta data socket "msock"
  1589. sock msock
  1590. -----------------+-------------------------+------------------------------
  1591. timeout conf.timeout / 2 conf.timeout / 2
  1592. timeout action send a ping via msock Abort communication
  1593. and close all sockets
  1594. */
  1595. /*
  1596. * you must have down()ed the appropriate [m]sock_mutex elsewhere!
  1597. */
  1598. int drbd_send(struct drbd_connection *connection, struct socket *sock,
  1599. void *buf, size_t size, unsigned msg_flags)
  1600. {
  1601. struct kvec iov = {.iov_base = buf, .iov_len = size};
  1602. struct msghdr msg;
  1603. int rv, sent = 0;
  1604. if (!sock)
  1605. return -EBADR;
  1606. /* THINK if (signal_pending) return ... ? */
  1607. msg.msg_name = NULL;
  1608. msg.msg_namelen = 0;
  1609. msg.msg_control = NULL;
  1610. msg.msg_controllen = 0;
  1611. msg.msg_flags = msg_flags | MSG_NOSIGNAL;
  1612. iov_iter_kvec(&msg.msg_iter, WRITE | ITER_KVEC, &iov, 1, size);
  1613. if (sock == connection->data.socket) {
  1614. rcu_read_lock();
  1615. connection->ko_count = rcu_dereference(connection->net_conf)->ko_count;
  1616. rcu_read_unlock();
  1617. drbd_update_congested(connection);
  1618. }
  1619. do {
  1620. rv = sock_sendmsg(sock, &msg);
  1621. if (rv == -EAGAIN) {
  1622. if (we_should_drop_the_connection(connection, sock))
  1623. break;
  1624. else
  1625. continue;
  1626. }
  1627. if (rv == -EINTR) {
  1628. flush_signals(current);
  1629. rv = 0;
  1630. }
  1631. if (rv < 0)
  1632. break;
  1633. sent += rv;
  1634. } while (sent < size);
  1635. if (sock == connection->data.socket)
  1636. clear_bit(NET_CONGESTED, &connection->flags);
  1637. if (rv <= 0) {
  1638. if (rv != -EAGAIN) {
  1639. drbd_err(connection, "%s_sendmsg returned %d\n",
  1640. sock == connection->meta.socket ? "msock" : "sock",
  1641. rv);
  1642. conn_request_state(connection, NS(conn, C_BROKEN_PIPE), CS_HARD);
  1643. } else
  1644. conn_request_state(connection, NS(conn, C_TIMEOUT), CS_HARD);
  1645. }
  1646. return sent;
  1647. }
  1648. /**
  1649. * drbd_send_all - Send an entire buffer
  1650. *
  1651. * Returns 0 upon success and a negative error value otherwise.
  1652. */
  1653. int drbd_send_all(struct drbd_connection *connection, struct socket *sock, void *buffer,
  1654. size_t size, unsigned msg_flags)
  1655. {
  1656. int err;
  1657. err = drbd_send(connection, sock, buffer, size, msg_flags);
  1658. if (err < 0)
  1659. return err;
  1660. if (err != size)
  1661. return -EIO;
  1662. return 0;
  1663. }
  1664. static int drbd_open(struct block_device *bdev, fmode_t mode)
  1665. {
  1666. struct drbd_device *device = bdev->bd_disk->private_data;
  1667. unsigned long flags;
  1668. int rv = 0;
  1669. mutex_lock(&drbd_main_mutex);
  1670. spin_lock_irqsave(&device->resource->req_lock, flags);
  1671. /* to have a stable device->state.role
  1672. * and no race with updating open_cnt */
  1673. if (device->state.role != R_PRIMARY) {
  1674. if (mode & FMODE_WRITE)
  1675. rv = -EROFS;
  1676. else if (!drbd_allow_oos)
  1677. rv = -EMEDIUMTYPE;
  1678. }
  1679. if (!rv)
  1680. device->open_cnt++;
  1681. spin_unlock_irqrestore(&device->resource->req_lock, flags);
  1682. mutex_unlock(&drbd_main_mutex);
  1683. return rv;
  1684. }
  1685. static void drbd_release(struct gendisk *gd, fmode_t mode)
  1686. {
  1687. struct drbd_device *device = gd->private_data;
  1688. mutex_lock(&drbd_main_mutex);
  1689. device->open_cnt--;
  1690. mutex_unlock(&drbd_main_mutex);
  1691. }
  1692. /* need to hold resource->req_lock */
  1693. void drbd_queue_unplug(struct drbd_device *device)
  1694. {
  1695. if (device->state.pdsk >= D_INCONSISTENT && device->state.conn >= C_CONNECTED) {
  1696. D_ASSERT(device, device->state.role == R_PRIMARY);
  1697. if (test_and_clear_bit(UNPLUG_REMOTE, &device->flags)) {
  1698. drbd_queue_work_if_unqueued(
  1699. &first_peer_device(device)->connection->sender_work,
  1700. &device->unplug_work);
  1701. }
  1702. }
  1703. }
  1704. static void drbd_set_defaults(struct drbd_device *device)
  1705. {
  1706. /* Beware! The actual layout differs
  1707. * between big endian and little endian */
  1708. device->state = (union drbd_dev_state) {
  1709. { .role = R_SECONDARY,
  1710. .peer = R_UNKNOWN,
  1711. .conn = C_STANDALONE,
  1712. .disk = D_DISKLESS,
  1713. .pdsk = D_UNKNOWN,
  1714. } };
  1715. }
  1716. void drbd_init_set_defaults(struct drbd_device *device)
  1717. {
  1718. /* the memset(,0,) did most of this.
  1719. * note: only assignments, no allocation in here */
  1720. drbd_set_defaults(device);
  1721. atomic_set(&device->ap_bio_cnt, 0);
  1722. atomic_set(&device->ap_actlog_cnt, 0);
  1723. atomic_set(&device->ap_pending_cnt, 0);
  1724. atomic_set(&device->rs_pending_cnt, 0);
  1725. atomic_set(&device->unacked_cnt, 0);
  1726. atomic_set(&device->local_cnt, 0);
  1727. atomic_set(&device->pp_in_use_by_net, 0);
  1728. atomic_set(&device->rs_sect_in, 0);
  1729. atomic_set(&device->rs_sect_ev, 0);
  1730. atomic_set(&device->ap_in_flight, 0);
  1731. atomic_set(&device->md_io.in_use, 0);
  1732. mutex_init(&device->own_state_mutex);
  1733. device->state_mutex = &device->own_state_mutex;
  1734. spin_lock_init(&device->al_lock);
  1735. spin_lock_init(&device->peer_seq_lock);
  1736. INIT_LIST_HEAD(&device->active_ee);
  1737. INIT_LIST_HEAD(&device->sync_ee);
  1738. INIT_LIST_HEAD(&device->done_ee);
  1739. INIT_LIST_HEAD(&device->read_ee);
  1740. INIT_LIST_HEAD(&device->net_ee);
  1741. INIT_LIST_HEAD(&device->resync_reads);
  1742. INIT_LIST_HEAD(&device->resync_work.list);
  1743. INIT_LIST_HEAD(&device->unplug_work.list);
  1744. INIT_LIST_HEAD(&device->bm_io_work.w.list);
  1745. INIT_LIST_HEAD(&device->pending_master_completion[0]);
  1746. INIT_LIST_HEAD(&device->pending_master_completion[1]);
  1747. INIT_LIST_HEAD(&device->pending_completion[0]);
  1748. INIT_LIST_HEAD(&device->pending_completion[1]);
  1749. device->resync_work.cb = w_resync_timer;
  1750. device->unplug_work.cb = w_send_write_hint;
  1751. device->bm_io_work.w.cb = w_bitmap_io;
  1752. setup_timer(&device->resync_timer, resync_timer_fn,
  1753. (unsigned long)device);
  1754. setup_timer(&device->md_sync_timer, md_sync_timer_fn,
  1755. (unsigned long)device);
  1756. setup_timer(&device->start_resync_timer, start_resync_timer_fn,
  1757. (unsigned long)device);
  1758. setup_timer(&device->request_timer, request_timer_fn,
  1759. (unsigned long)device);
  1760. init_waitqueue_head(&device->misc_wait);
  1761. init_waitqueue_head(&device->state_wait);
  1762. init_waitqueue_head(&device->ee_wait);
  1763. init_waitqueue_head(&device->al_wait);
  1764. init_waitqueue_head(&device->seq_wait);
  1765. device->resync_wenr = LC_FREE;
  1766. device->peer_max_bio_size = DRBD_MAX_BIO_SIZE_SAFE;
  1767. device->local_max_bio_size = DRBD_MAX_BIO_SIZE_SAFE;
  1768. }
  1769. void drbd_device_cleanup(struct drbd_device *device)
  1770. {
  1771. int i;
  1772. if (first_peer_device(device)->connection->receiver.t_state != NONE)
  1773. drbd_err(device, "ASSERT FAILED: receiver t_state == %d expected 0.\n",
  1774. first_peer_device(device)->connection->receiver.t_state);
  1775. device->al_writ_cnt =
  1776. device->bm_writ_cnt =
  1777. device->read_cnt =
  1778. device->recv_cnt =
  1779. device->send_cnt =
  1780. device->writ_cnt =
  1781. device->p_size =
  1782. device->rs_start =
  1783. device->rs_total =
  1784. device->rs_failed = 0;
  1785. device->rs_last_events = 0;
  1786. device->rs_last_sect_ev = 0;
  1787. for (i = 0; i < DRBD_SYNC_MARKS; i++) {
  1788. device->rs_mark_left[i] = 0;
  1789. device->rs_mark_time[i] = 0;
  1790. }
  1791. D_ASSERT(device, first_peer_device(device)->connection->net_conf == NULL);
  1792. drbd_set_my_capacity(device, 0);
  1793. if (device->bitmap) {
  1794. /* maybe never allocated. */
  1795. drbd_bm_resize(device, 0, 1);
  1796. drbd_bm_cleanup(device);
  1797. }
  1798. drbd_backing_dev_free(device, device->ldev);
  1799. device->ldev = NULL;
  1800. clear_bit(AL_SUSPENDED, &device->flags);
  1801. D_ASSERT(device, list_empty(&device->active_ee));
  1802. D_ASSERT(device, list_empty(&device->sync_ee));
  1803. D_ASSERT(device, list_empty(&device->done_ee));
  1804. D_ASSERT(device, list_empty(&device->read_ee));
  1805. D_ASSERT(device, list_empty(&device->net_ee));
  1806. D_ASSERT(device, list_empty(&device->resync_reads));
  1807. D_ASSERT(device, list_empty(&first_peer_device(device)->connection->sender_work.q));
  1808. D_ASSERT(device, list_empty(&device->resync_work.list));
  1809. D_ASSERT(device, list_empty(&device->unplug_work.list));
  1810. drbd_set_defaults(device);
  1811. }
  1812. static void drbd_destroy_mempools(void)
  1813. {
  1814. struct page *page;
  1815. while (drbd_pp_pool) {
  1816. page = drbd_pp_pool;
  1817. drbd_pp_pool = (struct page *)page_private(page);
  1818. __free_page(page);
  1819. drbd_pp_vacant--;
  1820. }
  1821. /* D_ASSERT(device, atomic_read(&drbd_pp_vacant)==0); */
  1822. if (drbd_io_bio_set)
  1823. bioset_free(drbd_io_bio_set);
  1824. if (drbd_md_io_bio_set)
  1825. bioset_free(drbd_md_io_bio_set);
  1826. if (drbd_md_io_page_pool)
  1827. mempool_destroy(drbd_md_io_page_pool);
  1828. if (drbd_ee_mempool)
  1829. mempool_destroy(drbd_ee_mempool);
  1830. if (drbd_request_mempool)
  1831. mempool_destroy(drbd_request_mempool);
  1832. if (drbd_ee_cache)
  1833. kmem_cache_destroy(drbd_ee_cache);
  1834. if (drbd_request_cache)
  1835. kmem_cache_destroy(drbd_request_cache);
  1836. if (drbd_bm_ext_cache)
  1837. kmem_cache_destroy(drbd_bm_ext_cache);
  1838. if (drbd_al_ext_cache)
  1839. kmem_cache_destroy(drbd_al_ext_cache);
  1840. drbd_io_bio_set = NULL;
  1841. drbd_md_io_bio_set = NULL;
  1842. drbd_md_io_page_pool = NULL;
  1843. drbd_ee_mempool = NULL;
  1844. drbd_request_mempool = NULL;
  1845. drbd_ee_cache = NULL;
  1846. drbd_request_cache = NULL;
  1847. drbd_bm_ext_cache = NULL;
  1848. drbd_al_ext_cache = NULL;
  1849. return;
  1850. }
  1851. static int drbd_create_mempools(void)
  1852. {
  1853. struct page *page;
  1854. const int number = (DRBD_MAX_BIO_SIZE/PAGE_SIZE) * drbd_minor_count;
  1855. int i;
  1856. /* prepare our caches and mempools */
  1857. drbd_request_mempool = NULL;
  1858. drbd_ee_cache = NULL;
  1859. drbd_request_cache = NULL;
  1860. drbd_bm_ext_cache = NULL;
  1861. drbd_al_ext_cache = NULL;
  1862. drbd_pp_pool = NULL;
  1863. drbd_md_io_page_pool = NULL;
  1864. drbd_md_io_bio_set = NULL;
  1865. drbd_io_bio_set = NULL;
  1866. /* caches */
  1867. drbd_request_cache = kmem_cache_create(
  1868. "drbd_req", sizeof(struct drbd_request), 0, 0, NULL);
  1869. if (drbd_request_cache == NULL)
  1870. goto Enomem;
  1871. drbd_ee_cache = kmem_cache_create(
  1872. "drbd_ee", sizeof(struct drbd_peer_request), 0, 0, NULL);
  1873. if (drbd_ee_cache == NULL)
  1874. goto Enomem;
  1875. drbd_bm_ext_cache = kmem_cache_create(
  1876. "drbd_bm", sizeof(struct bm_extent), 0, 0, NULL);
  1877. if (drbd_bm_ext_cache == NULL)
  1878. goto Enomem;
  1879. drbd_al_ext_cache = kmem_cache_create(
  1880. "drbd_al", sizeof(struct lc_element), 0, 0, NULL);
  1881. if (drbd_al_ext_cache == NULL)
  1882. goto Enomem;
  1883. /* mempools */
  1884. drbd_io_bio_set = bioset_create(BIO_POOL_SIZE, 0, BIOSET_NEED_RESCUER);
  1885. if (drbd_io_bio_set == NULL)
  1886. goto Enomem;
  1887. drbd_md_io_bio_set = bioset_create(DRBD_MIN_POOL_PAGES, 0,
  1888. BIOSET_NEED_BVECS |
  1889. BIOSET_NEED_RESCUER);
  1890. if (drbd_md_io_bio_set == NULL)
  1891. goto Enomem;
  1892. drbd_md_io_page_pool = mempool_create_page_pool(DRBD_MIN_POOL_PAGES, 0);
  1893. if (drbd_md_io_page_pool == NULL)
  1894. goto Enomem;
  1895. drbd_request_mempool = mempool_create_slab_pool(number,
  1896. drbd_request_cache);
  1897. if (drbd_request_mempool == NULL)
  1898. goto Enomem;
  1899. drbd_ee_mempool = mempool_create_slab_pool(number, drbd_ee_cache);
  1900. if (drbd_ee_mempool == NULL)
  1901. goto Enomem;
  1902. /* drbd's page pool */
  1903. spin_lock_init(&drbd_pp_lock);
  1904. for (i = 0; i < number; i++) {
  1905. page = alloc_page(GFP_HIGHUSER);
  1906. if (!page)
  1907. goto Enomem;
  1908. set_page_private(page, (unsigned long)drbd_pp_pool);
  1909. drbd_pp_pool = page;
  1910. }
  1911. drbd_pp_vacant = number;
  1912. return 0;
  1913. Enomem:
  1914. drbd_destroy_mempools(); /* in case we allocated some */
  1915. return -ENOMEM;
  1916. }
  1917. static void drbd_release_all_peer_reqs(struct drbd_device *device)
  1918. {
  1919. int rr;
  1920. rr = drbd_free_peer_reqs(device, &device->active_ee);
  1921. if (rr)
  1922. drbd_err(device, "%d EEs in active list found!\n", rr);
  1923. rr = drbd_free_peer_reqs(device, &device->sync_ee);
  1924. if (rr)
  1925. drbd_err(device, "%d EEs in sync list found!\n", rr);
  1926. rr = drbd_free_peer_reqs(device, &device->read_ee);
  1927. if (rr)
  1928. drbd_err(device, "%d EEs in read list found!\n", rr);
  1929. rr = drbd_free_peer_reqs(device, &device->done_ee);
  1930. if (rr)
  1931. drbd_err(device, "%d EEs in done list found!\n", rr);
  1932. rr = drbd_free_peer_reqs(device, &device->net_ee);
  1933. if (rr)
  1934. drbd_err(device, "%d EEs in net list found!\n", rr);
  1935. }
  1936. /* caution. no locking. */
  1937. void drbd_destroy_device(struct kref *kref)
  1938. {
  1939. struct drbd_device *device = container_of(kref, struct drbd_device, kref);
  1940. struct drbd_resource *resource = device->resource;
  1941. struct drbd_peer_device *peer_device, *tmp_peer_device;
  1942. del_timer_sync(&device->request_timer);
  1943. /* paranoia asserts */
  1944. D_ASSERT(device, device->open_cnt == 0);
  1945. /* end paranoia asserts */
  1946. /* cleanup stuff that may have been allocated during
  1947. * device (re-)configuration or state changes */
  1948. if (device->this_bdev)
  1949. bdput(device->this_bdev);
  1950. drbd_backing_dev_free(device, device->ldev);
  1951. device->ldev = NULL;
  1952. drbd_release_all_peer_reqs(device);
  1953. lc_destroy(device->act_log);
  1954. lc_destroy(device->resync);
  1955. kfree(device->p_uuid);
  1956. /* device->p_uuid = NULL; */
  1957. if (device->bitmap) /* should no longer be there. */
  1958. drbd_bm_cleanup(device);
  1959. __free_page(device->md_io.page);
  1960. put_disk(device->vdisk);
  1961. blk_cleanup_queue(device->rq_queue);
  1962. kfree(device->rs_plan_s);
  1963. /* not for_each_connection(connection, resource):
  1964. * those may have been cleaned up and disassociated already.
  1965. */
  1966. for_each_peer_device_safe(peer_device, tmp_peer_device, device) {
  1967. kref_put(&peer_device->connection->kref, drbd_destroy_connection);
  1968. kfree(peer_device);
  1969. }
  1970. memset(device, 0xfd, sizeof(*device));
  1971. kfree(device);
  1972. kref_put(&resource->kref, drbd_destroy_resource);
  1973. }
  1974. /* One global retry thread, if we need to push back some bio and have it
  1975. * reinserted through our make request function.
  1976. */
  1977. static struct retry_worker {
  1978. struct workqueue_struct *wq;
  1979. struct work_struct worker;
  1980. spinlock_t lock;
  1981. struct list_head writes;
  1982. } retry;
  1983. static void do_retry(struct work_struct *ws)
  1984. {
  1985. struct retry_worker *retry = container_of(ws, struct retry_worker, worker);
  1986. LIST_HEAD(writes);
  1987. struct drbd_request *req, *tmp;
  1988. spin_lock_irq(&retry->lock);
  1989. list_splice_init(&retry->writes, &writes);
  1990. spin_unlock_irq(&retry->lock);
  1991. list_for_each_entry_safe(req, tmp, &writes, tl_requests) {
  1992. struct drbd_device *device = req->device;
  1993. struct bio *bio = req->master_bio;
  1994. unsigned long start_jif = req->start_jif;
  1995. bool expected;
  1996. expected =
  1997. expect(atomic_read(&req->completion_ref) == 0) &&
  1998. expect(req->rq_state & RQ_POSTPONED) &&
  1999. expect((req->rq_state & RQ_LOCAL_PENDING) == 0 ||
  2000. (req->rq_state & RQ_LOCAL_ABORTED) != 0);
  2001. if (!expected)
  2002. drbd_err(device, "req=%p completion_ref=%d rq_state=%x\n",
  2003. req, atomic_read(&req->completion_ref),
  2004. req->rq_state);
  2005. /* We still need to put one kref associated with the
  2006. * "completion_ref" going zero in the code path that queued it
  2007. * here. The request object may still be referenced by a
  2008. * frozen local req->private_bio, in case we force-detached.
  2009. */
  2010. kref_put(&req->kref, drbd_req_destroy);
  2011. /* A single suspended or otherwise blocking device may stall
  2012. * all others as well. Fortunately, this code path is to
  2013. * recover from a situation that "should not happen":
  2014. * concurrent writes in multi-primary setup.
  2015. * In a "normal" lifecycle, this workqueue is supposed to be
  2016. * destroyed without ever doing anything.
  2017. * If it turns out to be an issue anyways, we can do per
  2018. * resource (replication group) or per device (minor) retry
  2019. * workqueues instead.
  2020. */
  2021. /* We are not just doing generic_make_request(),
  2022. * as we want to keep the start_time information. */
  2023. inc_ap_bio(device);
  2024. __drbd_make_request(device, bio, start_jif);
  2025. }
  2026. }
  2027. /* called via drbd_req_put_completion_ref(),
  2028. * holds resource->req_lock */
  2029. void drbd_restart_request(struct drbd_request *req)
  2030. {
  2031. unsigned long flags;
  2032. spin_lock_irqsave(&retry.lock, flags);
  2033. list_move_tail(&req->tl_requests, &retry.writes);
  2034. spin_unlock_irqrestore(&retry.lock, flags);
  2035. /* Drop the extra reference that would otherwise
  2036. * have been dropped by complete_master_bio.
  2037. * do_retry() needs to grab a new one. */
  2038. dec_ap_bio(req->device);
  2039. queue_work(retry.wq, &retry.worker);
  2040. }
  2041. void drbd_destroy_resource(struct kref *kref)
  2042. {
  2043. struct drbd_resource *resource =
  2044. container_of(kref, struct drbd_resource, kref);
  2045. idr_destroy(&resource->devices);
  2046. free_cpumask_var(resource->cpu_mask);
  2047. kfree(resource->name);
  2048. memset(resource, 0xf2, sizeof(*resource));
  2049. kfree(resource);
  2050. }
  2051. void drbd_free_resource(struct drbd_resource *resource)
  2052. {
  2053. struct drbd_connection *connection, *tmp;
  2054. for_each_connection_safe(connection, tmp, resource) {
  2055. list_del(&connection->connections);
  2056. drbd_debugfs_connection_cleanup(connection);
  2057. kref_put(&connection->kref, drbd_destroy_connection);
  2058. }
  2059. drbd_debugfs_resource_cleanup(resource);
  2060. kref_put(&resource->kref, drbd_destroy_resource);
  2061. }
  2062. static void drbd_cleanup(void)
  2063. {
  2064. unsigned int i;
  2065. struct drbd_device *device;
  2066. struct drbd_resource *resource, *tmp;
  2067. /* first remove proc,
  2068. * drbdsetup uses it's presence to detect
  2069. * whether DRBD is loaded.
  2070. * If we would get stuck in proc removal,
  2071. * but have netlink already deregistered,
  2072. * some drbdsetup commands may wait forever
  2073. * for an answer.
  2074. */
  2075. if (drbd_proc)
  2076. remove_proc_entry("drbd", NULL);
  2077. if (retry.wq)
  2078. destroy_workqueue(retry.wq);
  2079. drbd_genl_unregister();
  2080. idr_for_each_entry(&drbd_devices, device, i)
  2081. drbd_delete_device(device);
  2082. /* not _rcu since, no other updater anymore. Genl already unregistered */
  2083. for_each_resource_safe(resource, tmp, &drbd_resources) {
  2084. list_del(&resource->resources);
  2085. drbd_free_resource(resource);
  2086. }
  2087. drbd_debugfs_cleanup();
  2088. drbd_destroy_mempools();
  2089. unregister_blkdev(DRBD_MAJOR, "drbd");
  2090. idr_destroy(&drbd_devices);
  2091. pr_info("module cleanup done.\n");
  2092. }
  2093. /**
  2094. * drbd_congested() - Callback for the flusher thread
  2095. * @congested_data: User data
  2096. * @bdi_bits: Bits the BDI flusher thread is currently interested in
  2097. *
  2098. * Returns 1<<WB_async_congested and/or 1<<WB_sync_congested if we are congested.
  2099. */
  2100. static int drbd_congested(void *congested_data, int bdi_bits)
  2101. {
  2102. struct drbd_device *device = congested_data;
  2103. struct request_queue *q;
  2104. char reason = '-';
  2105. int r = 0;
  2106. if (!may_inc_ap_bio(device)) {
  2107. /* DRBD has frozen IO */
  2108. r = bdi_bits;
  2109. reason = 'd';
  2110. goto out;
  2111. }
  2112. if (test_bit(CALLBACK_PENDING, &first_peer_device(device)->connection->flags)) {
  2113. r |= (1 << WB_async_congested);
  2114. /* Without good local data, we would need to read from remote,
  2115. * and that would need the worker thread as well, which is
  2116. * currently blocked waiting for that usermode helper to
  2117. * finish.
  2118. */
  2119. if (!get_ldev_if_state(device, D_UP_TO_DATE))
  2120. r |= (1 << WB_sync_congested);
  2121. else
  2122. put_ldev(device);
  2123. r &= bdi_bits;
  2124. reason = 'c';
  2125. goto out;
  2126. }
  2127. if (get_ldev(device)) {
  2128. q = bdev_get_queue(device->ldev->backing_bdev);
  2129. r = bdi_congested(q->backing_dev_info, bdi_bits);
  2130. put_ldev(device);
  2131. if (r)
  2132. reason = 'b';
  2133. }
  2134. if (bdi_bits & (1 << WB_async_congested) &&
  2135. test_bit(NET_CONGESTED, &first_peer_device(device)->connection->flags)) {
  2136. r |= (1 << WB_async_congested);
  2137. reason = reason == 'b' ? 'a' : 'n';
  2138. }
  2139. out:
  2140. device->congestion_reason = reason;
  2141. return r;
  2142. }
  2143. static void drbd_init_workqueue(struct drbd_work_queue* wq)
  2144. {
  2145. spin_lock_init(&wq->q_lock);
  2146. INIT_LIST_HEAD(&wq->q);
  2147. init_waitqueue_head(&wq->q_wait);
  2148. }
  2149. struct completion_work {
  2150. struct drbd_work w;
  2151. struct completion done;
  2152. };
  2153. static int w_complete(struct drbd_work *w, int cancel)
  2154. {
  2155. struct completion_work *completion_work =
  2156. container_of(w, struct completion_work, w);
  2157. complete(&completion_work->done);
  2158. return 0;
  2159. }
  2160. void drbd_flush_workqueue(struct drbd_work_queue *work_queue)
  2161. {
  2162. struct completion_work completion_work;
  2163. completion_work.w.cb = w_complete;
  2164. init_completion(&completion_work.done);
  2165. drbd_queue_work(work_queue, &completion_work.w);
  2166. wait_for_completion(&completion_work.done);
  2167. }
  2168. struct drbd_resource *drbd_find_resource(const char *name)
  2169. {
  2170. struct drbd_resource *resource;
  2171. if (!name || !name[0])
  2172. return NULL;
  2173. rcu_read_lock();
  2174. for_each_resource_rcu(resource, &drbd_resources) {
  2175. if (!strcmp(resource->name, name)) {
  2176. kref_get(&resource->kref);
  2177. goto found;
  2178. }
  2179. }
  2180. resource = NULL;
  2181. found:
  2182. rcu_read_unlock();
  2183. return resource;
  2184. }
  2185. struct drbd_connection *conn_get_by_addrs(void *my_addr, int my_addr_len,
  2186. void *peer_addr, int peer_addr_len)
  2187. {
  2188. struct drbd_resource *resource;
  2189. struct drbd_connection *connection;
  2190. rcu_read_lock();
  2191. for_each_resource_rcu(resource, &drbd_resources) {
  2192. for_each_connection_rcu(connection, resource) {
  2193. if (connection->my_addr_len == my_addr_len &&
  2194. connection->peer_addr_len == peer_addr_len &&
  2195. !memcmp(&connection->my_addr, my_addr, my_addr_len) &&
  2196. !memcmp(&connection->peer_addr, peer_addr, peer_addr_len)) {
  2197. kref_get(&connection->kref);
  2198. goto found;
  2199. }
  2200. }
  2201. }
  2202. connection = NULL;
  2203. found:
  2204. rcu_read_unlock();
  2205. return connection;
  2206. }
  2207. static int drbd_alloc_socket(struct drbd_socket *socket)
  2208. {
  2209. socket->rbuf = (void *) __get_free_page(GFP_KERNEL);
  2210. if (!socket->rbuf)
  2211. return -ENOMEM;
  2212. socket->sbuf = (void *) __get_free_page(GFP_KERNEL);
  2213. if (!socket->sbuf)
  2214. return -ENOMEM;
  2215. return 0;
  2216. }
  2217. static void drbd_free_socket(struct drbd_socket *socket)
  2218. {
  2219. free_page((unsigned long) socket->sbuf);
  2220. free_page((unsigned long) socket->rbuf);
  2221. }
  2222. void conn_free_crypto(struct drbd_connection *connection)
  2223. {
  2224. drbd_free_sock(connection);
  2225. crypto_free_ahash(connection->csums_tfm);
  2226. crypto_free_ahash(connection->verify_tfm);
  2227. crypto_free_shash(connection->cram_hmac_tfm);
  2228. crypto_free_ahash(connection->integrity_tfm);
  2229. crypto_free_ahash(connection->peer_integrity_tfm);
  2230. kfree(connection->int_dig_in);
  2231. kfree(connection->int_dig_vv);
  2232. connection->csums_tfm = NULL;
  2233. connection->verify_tfm = NULL;
  2234. connection->cram_hmac_tfm = NULL;
  2235. connection->integrity_tfm = NULL;
  2236. connection->peer_integrity_tfm = NULL;
  2237. connection->int_dig_in = NULL;
  2238. connection->int_dig_vv = NULL;
  2239. }
  2240. int set_resource_options(struct drbd_resource *resource, struct res_opts *res_opts)
  2241. {
  2242. struct drbd_connection *connection;
  2243. cpumask_var_t new_cpu_mask;
  2244. int err;
  2245. if (!zalloc_cpumask_var(&new_cpu_mask, GFP_KERNEL))
  2246. return -ENOMEM;
  2247. /* silently ignore cpu mask on UP kernel */
  2248. if (nr_cpu_ids > 1 && res_opts->cpu_mask[0] != 0) {
  2249. err = bitmap_parse(res_opts->cpu_mask, DRBD_CPU_MASK_SIZE,
  2250. cpumask_bits(new_cpu_mask), nr_cpu_ids);
  2251. if (err == -EOVERFLOW) {
  2252. /* So what. mask it out. */
  2253. cpumask_var_t tmp_cpu_mask;
  2254. if (zalloc_cpumask_var(&tmp_cpu_mask, GFP_KERNEL)) {
  2255. cpumask_setall(tmp_cpu_mask);
  2256. cpumask_and(new_cpu_mask, new_cpu_mask, tmp_cpu_mask);
  2257. drbd_warn(resource, "Overflow in bitmap_parse(%.12s%s), truncating to %u bits\n",
  2258. res_opts->cpu_mask,
  2259. strlen(res_opts->cpu_mask) > 12 ? "..." : "",
  2260. nr_cpu_ids);
  2261. free_cpumask_var(tmp_cpu_mask);
  2262. err = 0;
  2263. }
  2264. }
  2265. if (err) {
  2266. drbd_warn(resource, "bitmap_parse() failed with %d\n", err);
  2267. /* retcode = ERR_CPU_MASK_PARSE; */
  2268. goto fail;
  2269. }
  2270. }
  2271. resource->res_opts = *res_opts;
  2272. if (cpumask_empty(new_cpu_mask))
  2273. drbd_calc_cpu_mask(&new_cpu_mask);
  2274. if (!cpumask_equal(resource->cpu_mask, new_cpu_mask)) {
  2275. cpumask_copy(resource->cpu_mask, new_cpu_mask);
  2276. for_each_connection_rcu(connection, resource) {
  2277. connection->receiver.reset_cpu_mask = 1;
  2278. connection->ack_receiver.reset_cpu_mask = 1;
  2279. connection->worker.reset_cpu_mask = 1;
  2280. }
  2281. }
  2282. err = 0;
  2283. fail:
  2284. free_cpumask_var(new_cpu_mask);
  2285. return err;
  2286. }
  2287. struct drbd_resource *drbd_create_resource(const char *name)
  2288. {
  2289. struct drbd_resource *resource;
  2290. resource = kzalloc(sizeof(struct drbd_resource), GFP_KERNEL);
  2291. if (!resource)
  2292. goto fail;
  2293. resource->name = kstrdup(name, GFP_KERNEL);
  2294. if (!resource->name)
  2295. goto fail_free_resource;
  2296. if (!zalloc_cpumask_var(&resource->cpu_mask, GFP_KERNEL))
  2297. goto fail_free_name;
  2298. kref_init(&resource->kref);
  2299. idr_init(&resource->devices);
  2300. INIT_LIST_HEAD(&resource->connections);
  2301. resource->write_ordering = WO_BDEV_FLUSH;
  2302. list_add_tail_rcu(&resource->resources, &drbd_resources);
  2303. mutex_init(&resource->conf_update);
  2304. mutex_init(&resource->adm_mutex);
  2305. spin_lock_init(&resource->req_lock);
  2306. drbd_debugfs_resource_add(resource);
  2307. return resource;
  2308. fail_free_name:
  2309. kfree(resource->name);
  2310. fail_free_resource:
  2311. kfree(resource);
  2312. fail:
  2313. return NULL;
  2314. }
  2315. /* caller must be under adm_mutex */
  2316. struct drbd_connection *conn_create(const char *name, struct res_opts *res_opts)
  2317. {
  2318. struct drbd_resource *resource;
  2319. struct drbd_connection *connection;
  2320. connection = kzalloc(sizeof(struct drbd_connection), GFP_KERNEL);
  2321. if (!connection)
  2322. return NULL;
  2323. if (drbd_alloc_socket(&connection->data))
  2324. goto fail;
  2325. if (drbd_alloc_socket(&connection->meta))
  2326. goto fail;
  2327. connection->current_epoch = kzalloc(sizeof(struct drbd_epoch), GFP_KERNEL);
  2328. if (!connection->current_epoch)
  2329. goto fail;
  2330. INIT_LIST_HEAD(&connection->transfer_log);
  2331. INIT_LIST_HEAD(&connection->current_epoch->list);
  2332. connection->epochs = 1;
  2333. spin_lock_init(&connection->epoch_lock);
  2334. connection->send.seen_any_write_yet = false;
  2335. connection->send.current_epoch_nr = 0;
  2336. connection->send.current_epoch_writes = 0;
  2337. resource = drbd_create_resource(name);
  2338. if (!resource)
  2339. goto fail;
  2340. connection->cstate = C_STANDALONE;
  2341. mutex_init(&connection->cstate_mutex);
  2342. init_waitqueue_head(&connection->ping_wait);
  2343. idr_init(&connection->peer_devices);
  2344. drbd_init_workqueue(&connection->sender_work);
  2345. mutex_init(&connection->data.mutex);
  2346. mutex_init(&connection->meta.mutex);
  2347. drbd_thread_init(resource, &connection->receiver, drbd_receiver, "receiver");
  2348. connection->receiver.connection = connection;
  2349. drbd_thread_init(resource, &connection->worker, drbd_worker, "worker");
  2350. connection->worker.connection = connection;
  2351. drbd_thread_init(resource, &connection->ack_receiver, drbd_ack_receiver, "ack_recv");
  2352. connection->ack_receiver.connection = connection;
  2353. kref_init(&connection->kref);
  2354. connection->resource = resource;
  2355. if (set_resource_options(resource, res_opts))
  2356. goto fail_resource;
  2357. kref_get(&resource->kref);
  2358. list_add_tail_rcu(&connection->connections, &resource->connections);
  2359. drbd_debugfs_connection_add(connection);
  2360. return connection;
  2361. fail_resource:
  2362. list_del(&resource->resources);
  2363. drbd_free_resource(resource);
  2364. fail:
  2365. kfree(connection->current_epoch);
  2366. drbd_free_socket(&connection->meta);
  2367. drbd_free_socket(&connection->data);
  2368. kfree(connection);
  2369. return NULL;
  2370. }
  2371. void drbd_destroy_connection(struct kref *kref)
  2372. {
  2373. struct drbd_connection *connection = container_of(kref, struct drbd_connection, kref);
  2374. struct drbd_resource *resource = connection->resource;
  2375. if (atomic_read(&connection->current_epoch->epoch_size) != 0)
  2376. drbd_err(connection, "epoch_size:%d\n", atomic_read(&connection->current_epoch->epoch_size));
  2377. kfree(connection->current_epoch);
  2378. idr_destroy(&connection->peer_devices);
  2379. drbd_free_socket(&connection->meta);
  2380. drbd_free_socket(&connection->data);
  2381. kfree(connection->int_dig_in);
  2382. kfree(connection->int_dig_vv);
  2383. memset(connection, 0xfc, sizeof(*connection));
  2384. kfree(connection);
  2385. kref_put(&resource->kref, drbd_destroy_resource);
  2386. }
  2387. static int init_submitter(struct drbd_device *device)
  2388. {
  2389. /* opencoded create_singlethread_workqueue(),
  2390. * to be able to say "drbd%d", ..., minor */
  2391. device->submit.wq =
  2392. alloc_ordered_workqueue("drbd%u_submit", WQ_MEM_RECLAIM, device->minor);
  2393. if (!device->submit.wq)
  2394. return -ENOMEM;
  2395. INIT_WORK(&device->submit.worker, do_submit);
  2396. INIT_LIST_HEAD(&device->submit.writes);
  2397. return 0;
  2398. }
  2399. enum drbd_ret_code drbd_create_device(struct drbd_config_context *adm_ctx, unsigned int minor)
  2400. {
  2401. struct drbd_resource *resource = adm_ctx->resource;
  2402. struct drbd_connection *connection;
  2403. struct drbd_device *device;
  2404. struct drbd_peer_device *peer_device, *tmp_peer_device;
  2405. struct gendisk *disk;
  2406. struct request_queue *q;
  2407. int id;
  2408. int vnr = adm_ctx->volume;
  2409. enum drbd_ret_code err = ERR_NOMEM;
  2410. device = minor_to_device(minor);
  2411. if (device)
  2412. return ERR_MINOR_OR_VOLUME_EXISTS;
  2413. /* GFP_KERNEL, we are outside of all write-out paths */
  2414. device = kzalloc(sizeof(struct drbd_device), GFP_KERNEL);
  2415. if (!device)
  2416. return ERR_NOMEM;
  2417. kref_init(&device->kref);
  2418. kref_get(&resource->kref);
  2419. device->resource = resource;
  2420. device->minor = minor;
  2421. device->vnr = vnr;
  2422. drbd_init_set_defaults(device);
  2423. q = blk_alloc_queue(GFP_KERNEL);
  2424. if (!q)
  2425. goto out_no_q;
  2426. device->rq_queue = q;
  2427. q->queuedata = device;
  2428. disk = alloc_disk(1);
  2429. if (!disk)
  2430. goto out_no_disk;
  2431. device->vdisk = disk;
  2432. set_disk_ro(disk, true);
  2433. disk->queue = q;
  2434. disk->major = DRBD_MAJOR;
  2435. disk->first_minor = minor;
  2436. disk->fops = &drbd_ops;
  2437. sprintf(disk->disk_name, "drbd%d", minor);
  2438. disk->private_data = device;
  2439. device->this_bdev = bdget(MKDEV(DRBD_MAJOR, minor));
  2440. /* we have no partitions. we contain only ourselves. */
  2441. device->this_bdev->bd_contains = device->this_bdev;
  2442. q->backing_dev_info->congested_fn = drbd_congested;
  2443. q->backing_dev_info->congested_data = device;
  2444. blk_queue_make_request(q, drbd_make_request);
  2445. blk_queue_write_cache(q, true, true);
  2446. /* Setting the max_hw_sectors to an odd value of 8kibyte here
  2447. This triggers a max_bio_size message upon first attach or connect */
  2448. blk_queue_max_hw_sectors(q, DRBD_MAX_BIO_SIZE_SAFE >> 8);
  2449. q->queue_lock = &resource->req_lock;
  2450. device->md_io.page = alloc_page(GFP_KERNEL);
  2451. if (!device->md_io.page)
  2452. goto out_no_io_page;
  2453. if (drbd_bm_init(device))
  2454. goto out_no_bitmap;
  2455. device->read_requests = RB_ROOT;
  2456. device->write_requests = RB_ROOT;
  2457. id = idr_alloc(&drbd_devices, device, minor, minor + 1, GFP_KERNEL);
  2458. if (id < 0) {
  2459. if (id == -ENOSPC)
  2460. err = ERR_MINOR_OR_VOLUME_EXISTS;
  2461. goto out_no_minor_idr;
  2462. }
  2463. kref_get(&device->kref);
  2464. id = idr_alloc(&resource->devices, device, vnr, vnr + 1, GFP_KERNEL);
  2465. if (id < 0) {
  2466. if (id == -ENOSPC)
  2467. err = ERR_MINOR_OR_VOLUME_EXISTS;
  2468. goto out_idr_remove_minor;
  2469. }
  2470. kref_get(&device->kref);
  2471. INIT_LIST_HEAD(&device->peer_devices);
  2472. INIT_LIST_HEAD(&device->pending_bitmap_io);
  2473. for_each_connection(connection, resource) {
  2474. peer_device = kzalloc(sizeof(struct drbd_peer_device), GFP_KERNEL);
  2475. if (!peer_device)
  2476. goto out_idr_remove_from_resource;
  2477. peer_device->connection = connection;
  2478. peer_device->device = device;
  2479. list_add(&peer_device->peer_devices, &device->peer_devices);
  2480. kref_get(&device->kref);
  2481. id = idr_alloc(&connection->peer_devices, peer_device, vnr, vnr + 1, GFP_KERNEL);
  2482. if (id < 0) {
  2483. if (id == -ENOSPC)
  2484. err = ERR_INVALID_REQUEST;
  2485. goto out_idr_remove_from_resource;
  2486. }
  2487. kref_get(&connection->kref);
  2488. INIT_WORK(&peer_device->send_acks_work, drbd_send_acks_wf);
  2489. }
  2490. if (init_submitter(device)) {
  2491. err = ERR_NOMEM;
  2492. goto out_idr_remove_vol;
  2493. }
  2494. add_disk(disk);
  2495. /* inherit the connection state */
  2496. device->state.conn = first_connection(resource)->cstate;
  2497. if (device->state.conn == C_WF_REPORT_PARAMS) {
  2498. for_each_peer_device(peer_device, device)
  2499. drbd_connected(peer_device);
  2500. }
  2501. /* move to create_peer_device() */
  2502. for_each_peer_device(peer_device, device)
  2503. drbd_debugfs_peer_device_add(peer_device);
  2504. drbd_debugfs_device_add(device);
  2505. return NO_ERROR;
  2506. out_idr_remove_vol:
  2507. idr_remove(&connection->peer_devices, vnr);
  2508. out_idr_remove_from_resource:
  2509. for_each_connection(connection, resource) {
  2510. peer_device = idr_remove(&connection->peer_devices, vnr);
  2511. if (peer_device)
  2512. kref_put(&connection->kref, drbd_destroy_connection);
  2513. }
  2514. for_each_peer_device_safe(peer_device, tmp_peer_device, device) {
  2515. list_del(&peer_device->peer_devices);
  2516. kfree(peer_device);
  2517. }
  2518. idr_remove(&resource->devices, vnr);
  2519. out_idr_remove_minor:
  2520. idr_remove(&drbd_devices, minor);
  2521. synchronize_rcu();
  2522. out_no_minor_idr:
  2523. drbd_bm_cleanup(device);
  2524. out_no_bitmap:
  2525. __free_page(device->md_io.page);
  2526. out_no_io_page:
  2527. put_disk(disk);
  2528. out_no_disk:
  2529. blk_cleanup_queue(q);
  2530. out_no_q:
  2531. kref_put(&resource->kref, drbd_destroy_resource);
  2532. kfree(device);
  2533. return err;
  2534. }
  2535. void drbd_delete_device(struct drbd_device *device)
  2536. {
  2537. struct drbd_resource *resource = device->resource;
  2538. struct drbd_connection *connection;
  2539. struct drbd_peer_device *peer_device;
  2540. /* move to free_peer_device() */
  2541. for_each_peer_device(peer_device, device)
  2542. drbd_debugfs_peer_device_cleanup(peer_device);
  2543. drbd_debugfs_device_cleanup(device);
  2544. for_each_connection(connection, resource) {
  2545. idr_remove(&connection->peer_devices, device->vnr);
  2546. kref_put(&device->kref, drbd_destroy_device);
  2547. }
  2548. idr_remove(&resource->devices, device->vnr);
  2549. kref_put(&device->kref, drbd_destroy_device);
  2550. idr_remove(&drbd_devices, device_to_minor(device));
  2551. kref_put(&device->kref, drbd_destroy_device);
  2552. del_gendisk(device->vdisk);
  2553. synchronize_rcu();
  2554. kref_put(&device->kref, drbd_destroy_device);
  2555. }
  2556. static int __init drbd_init(void)
  2557. {
  2558. int err;
  2559. if (drbd_minor_count < DRBD_MINOR_COUNT_MIN || drbd_minor_count > DRBD_MINOR_COUNT_MAX) {
  2560. pr_err("invalid minor_count (%d)\n", drbd_minor_count);
  2561. #ifdef MODULE
  2562. return -EINVAL;
  2563. #else
  2564. drbd_minor_count = DRBD_MINOR_COUNT_DEF;
  2565. #endif
  2566. }
  2567. err = register_blkdev(DRBD_MAJOR, "drbd");
  2568. if (err) {
  2569. pr_err("unable to register block device major %d\n",
  2570. DRBD_MAJOR);
  2571. return err;
  2572. }
  2573. /*
  2574. * allocate all necessary structs
  2575. */
  2576. init_waitqueue_head(&drbd_pp_wait);
  2577. drbd_proc = NULL; /* play safe for drbd_cleanup */
  2578. idr_init(&drbd_devices);
  2579. mutex_init(&resources_mutex);
  2580. INIT_LIST_HEAD(&drbd_resources);
  2581. err = drbd_genl_register();
  2582. if (err) {
  2583. pr_err("unable to register generic netlink family\n");
  2584. goto fail;
  2585. }
  2586. err = drbd_create_mempools();
  2587. if (err)
  2588. goto fail;
  2589. err = -ENOMEM;
  2590. drbd_proc = proc_create_data("drbd", S_IFREG | S_IRUGO , NULL, &drbd_proc_fops, NULL);
  2591. if (!drbd_proc) {
  2592. pr_err("unable to register proc file\n");
  2593. goto fail;
  2594. }
  2595. retry.wq = create_singlethread_workqueue("drbd-reissue");
  2596. if (!retry.wq) {
  2597. pr_err("unable to create retry workqueue\n");
  2598. goto fail;
  2599. }
  2600. INIT_WORK(&retry.worker, do_retry);
  2601. spin_lock_init(&retry.lock);
  2602. INIT_LIST_HEAD(&retry.writes);
  2603. if (drbd_debugfs_init())
  2604. pr_notice("failed to initialize debugfs -- will not be available\n");
  2605. pr_info("initialized. "
  2606. "Version: " REL_VERSION " (api:%d/proto:%d-%d)\n",
  2607. API_VERSION, PRO_VERSION_MIN, PRO_VERSION_MAX);
  2608. pr_info("%s\n", drbd_buildtag());
  2609. pr_info("registered as block device major %d\n", DRBD_MAJOR);
  2610. return 0; /* Success! */
  2611. fail:
  2612. drbd_cleanup();
  2613. if (err == -ENOMEM)
  2614. pr_err("ran out of memory\n");
  2615. else
  2616. pr_err("initialization failure\n");
  2617. return err;
  2618. }
  2619. static void drbd_free_one_sock(struct drbd_socket *ds)
  2620. {
  2621. struct socket *s;
  2622. mutex_lock(&ds->mutex);
  2623. s = ds->socket;
  2624. ds->socket = NULL;
  2625. mutex_unlock(&ds->mutex);
  2626. if (s) {
  2627. /* so debugfs does not need to mutex_lock() */
  2628. synchronize_rcu();
  2629. kernel_sock_shutdown(s, SHUT_RDWR);
  2630. sock_release(s);
  2631. }
  2632. }
  2633. void drbd_free_sock(struct drbd_connection *connection)
  2634. {
  2635. if (connection->data.socket)
  2636. drbd_free_one_sock(&connection->data);
  2637. if (connection->meta.socket)
  2638. drbd_free_one_sock(&connection->meta);
  2639. }
  2640. /* meta data management */
  2641. void conn_md_sync(struct drbd_connection *connection)
  2642. {
  2643. struct drbd_peer_device *peer_device;
  2644. int vnr;
  2645. rcu_read_lock();
  2646. idr_for_each_entry(&connection->peer_devices, peer_device, vnr) {
  2647. struct drbd_device *device = peer_device->device;
  2648. kref_get(&device->kref);
  2649. rcu_read_unlock();
  2650. drbd_md_sync(device);
  2651. kref_put(&device->kref, drbd_destroy_device);
  2652. rcu_read_lock();
  2653. }
  2654. rcu_read_unlock();
  2655. }
  2656. /* aligned 4kByte */
  2657. struct meta_data_on_disk {
  2658. u64 la_size_sect; /* last agreed size. */
  2659. u64 uuid[UI_SIZE]; /* UUIDs. */
  2660. u64 device_uuid;
  2661. u64 reserved_u64_1;
  2662. u32 flags; /* MDF */
  2663. u32 magic;
  2664. u32 md_size_sect;
  2665. u32 al_offset; /* offset to this block */
  2666. u32 al_nr_extents; /* important for restoring the AL (userspace) */
  2667. /* `-- act_log->nr_elements <-- ldev->dc.al_extents */
  2668. u32 bm_offset; /* offset to the bitmap, from here */
  2669. u32 bm_bytes_per_bit; /* BM_BLOCK_SIZE */
  2670. u32 la_peer_max_bio_size; /* last peer max_bio_size */
  2671. /* see al_tr_number_to_on_disk_sector() */
  2672. u32 al_stripes;
  2673. u32 al_stripe_size_4k;
  2674. u8 reserved_u8[4096 - (7*8 + 10*4)];
  2675. } __packed;
  2676. void drbd_md_write(struct drbd_device *device, void *b)
  2677. {
  2678. struct meta_data_on_disk *buffer = b;
  2679. sector_t sector;
  2680. int i;
  2681. memset(buffer, 0, sizeof(*buffer));
  2682. buffer->la_size_sect = cpu_to_be64(drbd_get_capacity(device->this_bdev));
  2683. for (i = UI_CURRENT; i < UI_SIZE; i++)
  2684. buffer->uuid[i] = cpu_to_be64(device->ldev->md.uuid[i]);
  2685. buffer->flags = cpu_to_be32(device->ldev->md.flags);
  2686. buffer->magic = cpu_to_be32(DRBD_MD_MAGIC_84_UNCLEAN);
  2687. buffer->md_size_sect = cpu_to_be32(device->ldev->md.md_size_sect);
  2688. buffer->al_offset = cpu_to_be32(device->ldev->md.al_offset);
  2689. buffer->al_nr_extents = cpu_to_be32(device->act_log->nr_elements);
  2690. buffer->bm_bytes_per_bit = cpu_to_be32(BM_BLOCK_SIZE);
  2691. buffer->device_uuid = cpu_to_be64(device->ldev->md.device_uuid);
  2692. buffer->bm_offset = cpu_to_be32(device->ldev->md.bm_offset);
  2693. buffer->la_peer_max_bio_size = cpu_to_be32(device->peer_max_bio_size);
  2694. buffer->al_stripes = cpu_to_be32(device->ldev->md.al_stripes);
  2695. buffer->al_stripe_size_4k = cpu_to_be32(device->ldev->md.al_stripe_size_4k);
  2696. D_ASSERT(device, drbd_md_ss(device->ldev) == device->ldev->md.md_offset);
  2697. sector = device->ldev->md.md_offset;
  2698. if (drbd_md_sync_page_io(device, device->ldev, sector, REQ_OP_WRITE)) {
  2699. /* this was a try anyways ... */
  2700. drbd_err(device, "meta data update failed!\n");
  2701. drbd_chk_io_error(device, 1, DRBD_META_IO_ERROR);
  2702. }
  2703. }
  2704. /**
  2705. * drbd_md_sync() - Writes the meta data super block if the MD_DIRTY flag bit is set
  2706. * @device: DRBD device.
  2707. */
  2708. void drbd_md_sync(struct drbd_device *device)
  2709. {
  2710. struct meta_data_on_disk *buffer;
  2711. /* Don't accidentally change the DRBD meta data layout. */
  2712. BUILD_BUG_ON(UI_SIZE != 4);
  2713. BUILD_BUG_ON(sizeof(struct meta_data_on_disk) != 4096);
  2714. del_timer(&device->md_sync_timer);
  2715. /* timer may be rearmed by drbd_md_mark_dirty() now. */
  2716. if (!test_and_clear_bit(MD_DIRTY, &device->flags))
  2717. return;
  2718. /* We use here D_FAILED and not D_ATTACHING because we try to write
  2719. * metadata even if we detach due to a disk failure! */
  2720. if (!get_ldev_if_state(device, D_FAILED))
  2721. return;
  2722. buffer = drbd_md_get_buffer(device, __func__);
  2723. if (!buffer)
  2724. goto out;
  2725. drbd_md_write(device, buffer);
  2726. /* Update device->ldev->md.la_size_sect,
  2727. * since we updated it on metadata. */
  2728. device->ldev->md.la_size_sect = drbd_get_capacity(device->this_bdev);
  2729. drbd_md_put_buffer(device);
  2730. out:
  2731. put_ldev(device);
  2732. }
  2733. static int check_activity_log_stripe_size(struct drbd_device *device,
  2734. struct meta_data_on_disk *on_disk,
  2735. struct drbd_md *in_core)
  2736. {
  2737. u32 al_stripes = be32_to_cpu(on_disk->al_stripes);
  2738. u32 al_stripe_size_4k = be32_to_cpu(on_disk->al_stripe_size_4k);
  2739. u64 al_size_4k;
  2740. /* both not set: default to old fixed size activity log */
  2741. if (al_stripes == 0 && al_stripe_size_4k == 0) {
  2742. al_stripes = 1;
  2743. al_stripe_size_4k = MD_32kB_SECT/8;
  2744. }
  2745. /* some paranoia plausibility checks */
  2746. /* we need both values to be set */
  2747. if (al_stripes == 0 || al_stripe_size_4k == 0)
  2748. goto err;
  2749. al_size_4k = (u64)al_stripes * al_stripe_size_4k;
  2750. /* Upper limit of activity log area, to avoid potential overflow
  2751. * problems in al_tr_number_to_on_disk_sector(). As right now, more
  2752. * than 72 * 4k blocks total only increases the amount of history,
  2753. * limiting this arbitrarily to 16 GB is not a real limitation ;-) */
  2754. if (al_size_4k > (16 * 1024 * 1024/4))
  2755. goto err;
  2756. /* Lower limit: we need at least 8 transaction slots (32kB)
  2757. * to not break existing setups */
  2758. if (al_size_4k < MD_32kB_SECT/8)
  2759. goto err;
  2760. in_core->al_stripe_size_4k = al_stripe_size_4k;
  2761. in_core->al_stripes = al_stripes;
  2762. in_core->al_size_4k = al_size_4k;
  2763. return 0;
  2764. err:
  2765. drbd_err(device, "invalid activity log striping: al_stripes=%u, al_stripe_size_4k=%u\n",
  2766. al_stripes, al_stripe_size_4k);
  2767. return -EINVAL;
  2768. }
  2769. static int check_offsets_and_sizes(struct drbd_device *device, struct drbd_backing_dev *bdev)
  2770. {
  2771. sector_t capacity = drbd_get_capacity(bdev->md_bdev);
  2772. struct drbd_md *in_core = &bdev->md;
  2773. s32 on_disk_al_sect;
  2774. s32 on_disk_bm_sect;
  2775. /* The on-disk size of the activity log, calculated from offsets, and
  2776. * the size of the activity log calculated from the stripe settings,
  2777. * should match.
  2778. * Though we could relax this a bit: it is ok, if the striped activity log
  2779. * fits in the available on-disk activity log size.
  2780. * Right now, that would break how resize is implemented.
  2781. * TODO: make drbd_determine_dev_size() (and the drbdmeta tool) aware
  2782. * of possible unused padding space in the on disk layout. */
  2783. if (in_core->al_offset < 0) {
  2784. if (in_core->bm_offset > in_core->al_offset)
  2785. goto err;
  2786. on_disk_al_sect = -in_core->al_offset;
  2787. on_disk_bm_sect = in_core->al_offset - in_core->bm_offset;
  2788. } else {
  2789. if (in_core->al_offset != MD_4kB_SECT)
  2790. goto err;
  2791. if (in_core->bm_offset < in_core->al_offset + in_core->al_size_4k * MD_4kB_SECT)
  2792. goto err;
  2793. on_disk_al_sect = in_core->bm_offset - MD_4kB_SECT;
  2794. on_disk_bm_sect = in_core->md_size_sect - in_core->bm_offset;
  2795. }
  2796. /* old fixed size meta data is exactly that: fixed. */
  2797. if (in_core->meta_dev_idx >= 0) {
  2798. if (in_core->md_size_sect != MD_128MB_SECT
  2799. || in_core->al_offset != MD_4kB_SECT
  2800. || in_core->bm_offset != MD_4kB_SECT + MD_32kB_SECT
  2801. || in_core->al_stripes != 1
  2802. || in_core->al_stripe_size_4k != MD_32kB_SECT/8)
  2803. goto err;
  2804. }
  2805. if (capacity < in_core->md_size_sect)
  2806. goto err;
  2807. if (capacity - in_core->md_size_sect < drbd_md_first_sector(bdev))
  2808. goto err;
  2809. /* should be aligned, and at least 32k */
  2810. if ((on_disk_al_sect & 7) || (on_disk_al_sect < MD_32kB_SECT))
  2811. goto err;
  2812. /* should fit (for now: exactly) into the available on-disk space;
  2813. * overflow prevention is in check_activity_log_stripe_size() above. */
  2814. if (on_disk_al_sect != in_core->al_size_4k * MD_4kB_SECT)
  2815. goto err;
  2816. /* again, should be aligned */
  2817. if (in_core->bm_offset & 7)
  2818. goto err;
  2819. /* FIXME check for device grow with flex external meta data? */
  2820. /* can the available bitmap space cover the last agreed device size? */
  2821. if (on_disk_bm_sect < (in_core->la_size_sect+7)/MD_4kB_SECT/8/512)
  2822. goto err;
  2823. return 0;
  2824. err:
  2825. drbd_err(device, "meta data offsets don't make sense: idx=%d "
  2826. "al_s=%u, al_sz4k=%u, al_offset=%d, bm_offset=%d, "
  2827. "md_size_sect=%u, la_size=%llu, md_capacity=%llu\n",
  2828. in_core->meta_dev_idx,
  2829. in_core->al_stripes, in_core->al_stripe_size_4k,
  2830. in_core->al_offset, in_core->bm_offset, in_core->md_size_sect,
  2831. (unsigned long long)in_core->la_size_sect,
  2832. (unsigned long long)capacity);
  2833. return -EINVAL;
  2834. }
  2835. /**
  2836. * drbd_md_read() - Reads in the meta data super block
  2837. * @device: DRBD device.
  2838. * @bdev: Device from which the meta data should be read in.
  2839. *
  2840. * Return NO_ERROR on success, and an enum drbd_ret_code in case
  2841. * something goes wrong.
  2842. *
  2843. * Called exactly once during drbd_adm_attach(), while still being D_DISKLESS,
  2844. * even before @bdev is assigned to @device->ldev.
  2845. */
  2846. int drbd_md_read(struct drbd_device *device, struct drbd_backing_dev *bdev)
  2847. {
  2848. struct meta_data_on_disk *buffer;
  2849. u32 magic, flags;
  2850. int i, rv = NO_ERROR;
  2851. if (device->state.disk != D_DISKLESS)
  2852. return ERR_DISK_CONFIGURED;
  2853. buffer = drbd_md_get_buffer(device, __func__);
  2854. if (!buffer)
  2855. return ERR_NOMEM;
  2856. /* First, figure out where our meta data superblock is located,
  2857. * and read it. */
  2858. bdev->md.meta_dev_idx = bdev->disk_conf->meta_dev_idx;
  2859. bdev->md.md_offset = drbd_md_ss(bdev);
  2860. /* Even for (flexible or indexed) external meta data,
  2861. * initially restrict us to the 4k superblock for now.
  2862. * Affects the paranoia out-of-range access check in drbd_md_sync_page_io(). */
  2863. bdev->md.md_size_sect = 8;
  2864. if (drbd_md_sync_page_io(device, bdev, bdev->md.md_offset,
  2865. REQ_OP_READ)) {
  2866. /* NOTE: can't do normal error processing here as this is
  2867. called BEFORE disk is attached */
  2868. drbd_err(device, "Error while reading metadata.\n");
  2869. rv = ERR_IO_MD_DISK;
  2870. goto err;
  2871. }
  2872. magic = be32_to_cpu(buffer->magic);
  2873. flags = be32_to_cpu(buffer->flags);
  2874. if (magic == DRBD_MD_MAGIC_84_UNCLEAN ||
  2875. (magic == DRBD_MD_MAGIC_08 && !(flags & MDF_AL_CLEAN))) {
  2876. /* btw: that's Activity Log clean, not "all" clean. */
  2877. drbd_err(device, "Found unclean meta data. Did you \"drbdadm apply-al\"?\n");
  2878. rv = ERR_MD_UNCLEAN;
  2879. goto err;
  2880. }
  2881. rv = ERR_MD_INVALID;
  2882. if (magic != DRBD_MD_MAGIC_08) {
  2883. if (magic == DRBD_MD_MAGIC_07)
  2884. drbd_err(device, "Found old (0.7) meta data magic. Did you \"drbdadm create-md\"?\n");
  2885. else
  2886. drbd_err(device, "Meta data magic not found. Did you \"drbdadm create-md\"?\n");
  2887. goto err;
  2888. }
  2889. if (be32_to_cpu(buffer->bm_bytes_per_bit) != BM_BLOCK_SIZE) {
  2890. drbd_err(device, "unexpected bm_bytes_per_bit: %u (expected %u)\n",
  2891. be32_to_cpu(buffer->bm_bytes_per_bit), BM_BLOCK_SIZE);
  2892. goto err;
  2893. }
  2894. /* convert to in_core endian */
  2895. bdev->md.la_size_sect = be64_to_cpu(buffer->la_size_sect);
  2896. for (i = UI_CURRENT; i < UI_SIZE; i++)
  2897. bdev->md.uuid[i] = be64_to_cpu(buffer->uuid[i]);
  2898. bdev->md.flags = be32_to_cpu(buffer->flags);
  2899. bdev->md.device_uuid = be64_to_cpu(buffer->device_uuid);
  2900. bdev->md.md_size_sect = be32_to_cpu(buffer->md_size_sect);
  2901. bdev->md.al_offset = be32_to_cpu(buffer->al_offset);
  2902. bdev->md.bm_offset = be32_to_cpu(buffer->bm_offset);
  2903. if (check_activity_log_stripe_size(device, buffer, &bdev->md))
  2904. goto err;
  2905. if (check_offsets_and_sizes(device, bdev))
  2906. goto err;
  2907. if (be32_to_cpu(buffer->bm_offset) != bdev->md.bm_offset) {
  2908. drbd_err(device, "unexpected bm_offset: %d (expected %d)\n",
  2909. be32_to_cpu(buffer->bm_offset), bdev->md.bm_offset);
  2910. goto err;
  2911. }
  2912. if (be32_to_cpu(buffer->md_size_sect) != bdev->md.md_size_sect) {
  2913. drbd_err(device, "unexpected md_size: %u (expected %u)\n",
  2914. be32_to_cpu(buffer->md_size_sect), bdev->md.md_size_sect);
  2915. goto err;
  2916. }
  2917. rv = NO_ERROR;
  2918. spin_lock_irq(&device->resource->req_lock);
  2919. if (device->state.conn < C_CONNECTED) {
  2920. unsigned int peer;
  2921. peer = be32_to_cpu(buffer->la_peer_max_bio_size);
  2922. peer = max(peer, DRBD_MAX_BIO_SIZE_SAFE);
  2923. device->peer_max_bio_size = peer;
  2924. }
  2925. spin_unlock_irq(&device->resource->req_lock);
  2926. err:
  2927. drbd_md_put_buffer(device);
  2928. return rv;
  2929. }
  2930. /**
  2931. * drbd_md_mark_dirty() - Mark meta data super block as dirty
  2932. * @device: DRBD device.
  2933. *
  2934. * Call this function if you change anything that should be written to
  2935. * the meta-data super block. This function sets MD_DIRTY, and starts a
  2936. * timer that ensures that within five seconds you have to call drbd_md_sync().
  2937. */
  2938. #ifdef DEBUG
  2939. void drbd_md_mark_dirty_(struct drbd_device *device, unsigned int line, const char *func)
  2940. {
  2941. if (!test_and_set_bit(MD_DIRTY, &device->flags)) {
  2942. mod_timer(&device->md_sync_timer, jiffies + HZ);
  2943. device->last_md_mark_dirty.line = line;
  2944. device->last_md_mark_dirty.func = func;
  2945. }
  2946. }
  2947. #else
  2948. void drbd_md_mark_dirty(struct drbd_device *device)
  2949. {
  2950. if (!test_and_set_bit(MD_DIRTY, &device->flags))
  2951. mod_timer(&device->md_sync_timer, jiffies + 5*HZ);
  2952. }
  2953. #endif
  2954. void drbd_uuid_move_history(struct drbd_device *device) __must_hold(local)
  2955. {
  2956. int i;
  2957. for (i = UI_HISTORY_START; i < UI_HISTORY_END; i++)
  2958. device->ldev->md.uuid[i+1] = device->ldev->md.uuid[i];
  2959. }
  2960. void __drbd_uuid_set(struct drbd_device *device, int idx, u64 val) __must_hold(local)
  2961. {
  2962. if (idx == UI_CURRENT) {
  2963. if (device->state.role == R_PRIMARY)
  2964. val |= 1;
  2965. else
  2966. val &= ~((u64)1);
  2967. drbd_set_ed_uuid(device, val);
  2968. }
  2969. device->ldev->md.uuid[idx] = val;
  2970. drbd_md_mark_dirty(device);
  2971. }
  2972. void _drbd_uuid_set(struct drbd_device *device, int idx, u64 val) __must_hold(local)
  2973. {
  2974. unsigned long flags;
  2975. spin_lock_irqsave(&device->ldev->md.uuid_lock, flags);
  2976. __drbd_uuid_set(device, idx, val);
  2977. spin_unlock_irqrestore(&device->ldev->md.uuid_lock, flags);
  2978. }
  2979. void drbd_uuid_set(struct drbd_device *device, int idx, u64 val) __must_hold(local)
  2980. {
  2981. unsigned long flags;
  2982. spin_lock_irqsave(&device->ldev->md.uuid_lock, flags);
  2983. if (device->ldev->md.uuid[idx]) {
  2984. drbd_uuid_move_history(device);
  2985. device->ldev->md.uuid[UI_HISTORY_START] = device->ldev->md.uuid[idx];
  2986. }
  2987. __drbd_uuid_set(device, idx, val);
  2988. spin_unlock_irqrestore(&device->ldev->md.uuid_lock, flags);
  2989. }
  2990. /**
  2991. * drbd_uuid_new_current() - Creates a new current UUID
  2992. * @device: DRBD device.
  2993. *
  2994. * Creates a new current UUID, and rotates the old current UUID into
  2995. * the bitmap slot. Causes an incremental resync upon next connect.
  2996. */
  2997. void drbd_uuid_new_current(struct drbd_device *device) __must_hold(local)
  2998. {
  2999. u64 val;
  3000. unsigned long long bm_uuid;
  3001. get_random_bytes(&val, sizeof(u64));
  3002. spin_lock_irq(&device->ldev->md.uuid_lock);
  3003. bm_uuid = device->ldev->md.uuid[UI_BITMAP];
  3004. if (bm_uuid)
  3005. drbd_warn(device, "bm UUID was already set: %llX\n", bm_uuid);
  3006. device->ldev->md.uuid[UI_BITMAP] = device->ldev->md.uuid[UI_CURRENT];
  3007. __drbd_uuid_set(device, UI_CURRENT, val);
  3008. spin_unlock_irq(&device->ldev->md.uuid_lock);
  3009. drbd_print_uuids(device, "new current UUID");
  3010. /* get it to stable storage _now_ */
  3011. drbd_md_sync(device);
  3012. }
  3013. void drbd_uuid_set_bm(struct drbd_device *device, u64 val) __must_hold(local)
  3014. {
  3015. unsigned long flags;
  3016. if (device->ldev->md.uuid[UI_BITMAP] == 0 && val == 0)
  3017. return;
  3018. spin_lock_irqsave(&device->ldev->md.uuid_lock, flags);
  3019. if (val == 0) {
  3020. drbd_uuid_move_history(device);
  3021. device->ldev->md.uuid[UI_HISTORY_START] = device->ldev->md.uuid[UI_BITMAP];
  3022. device->ldev->md.uuid[UI_BITMAP] = 0;
  3023. } else {
  3024. unsigned long long bm_uuid = device->ldev->md.uuid[UI_BITMAP];
  3025. if (bm_uuid)
  3026. drbd_warn(device, "bm UUID was already set: %llX\n", bm_uuid);
  3027. device->ldev->md.uuid[UI_BITMAP] = val & ~((u64)1);
  3028. }
  3029. spin_unlock_irqrestore(&device->ldev->md.uuid_lock, flags);
  3030. drbd_md_mark_dirty(device);
  3031. }
  3032. /**
  3033. * drbd_bmio_set_n_write() - io_fn for drbd_queue_bitmap_io() or drbd_bitmap_io()
  3034. * @device: DRBD device.
  3035. *
  3036. * Sets all bits in the bitmap and writes the whole bitmap to stable storage.
  3037. */
  3038. int drbd_bmio_set_n_write(struct drbd_device *device) __must_hold(local)
  3039. {
  3040. int rv = -EIO;
  3041. drbd_md_set_flag(device, MDF_FULL_SYNC);
  3042. drbd_md_sync(device);
  3043. drbd_bm_set_all(device);
  3044. rv = drbd_bm_write(device);
  3045. if (!rv) {
  3046. drbd_md_clear_flag(device, MDF_FULL_SYNC);
  3047. drbd_md_sync(device);
  3048. }
  3049. return rv;
  3050. }
  3051. /**
  3052. * drbd_bmio_clear_n_write() - io_fn for drbd_queue_bitmap_io() or drbd_bitmap_io()
  3053. * @device: DRBD device.
  3054. *
  3055. * Clears all bits in the bitmap and writes the whole bitmap to stable storage.
  3056. */
  3057. int drbd_bmio_clear_n_write(struct drbd_device *device) __must_hold(local)
  3058. {
  3059. drbd_resume_al(device);
  3060. drbd_bm_clear_all(device);
  3061. return drbd_bm_write(device);
  3062. }
  3063. static int w_bitmap_io(struct drbd_work *w, int unused)
  3064. {
  3065. struct drbd_device *device =
  3066. container_of(w, struct drbd_device, bm_io_work.w);
  3067. struct bm_io_work *work = &device->bm_io_work;
  3068. int rv = -EIO;
  3069. if (work->flags != BM_LOCKED_CHANGE_ALLOWED) {
  3070. int cnt = atomic_read(&device->ap_bio_cnt);
  3071. if (cnt)
  3072. drbd_err(device, "FIXME: ap_bio_cnt %d, expected 0; queued for '%s'\n",
  3073. cnt, work->why);
  3074. }
  3075. if (get_ldev(device)) {
  3076. drbd_bm_lock(device, work->why, work->flags);
  3077. rv = work->io_fn(device);
  3078. drbd_bm_unlock(device);
  3079. put_ldev(device);
  3080. }
  3081. clear_bit_unlock(BITMAP_IO, &device->flags);
  3082. wake_up(&device->misc_wait);
  3083. if (work->done)
  3084. work->done(device, rv);
  3085. clear_bit(BITMAP_IO_QUEUED, &device->flags);
  3086. work->why = NULL;
  3087. work->flags = 0;
  3088. return 0;
  3089. }
  3090. /**
  3091. * drbd_queue_bitmap_io() - Queues an IO operation on the whole bitmap
  3092. * @device: DRBD device.
  3093. * @io_fn: IO callback to be called when bitmap IO is possible
  3094. * @done: callback to be called after the bitmap IO was performed
  3095. * @why: Descriptive text of the reason for doing the IO
  3096. *
  3097. * While IO on the bitmap happens we freeze application IO thus we ensure
  3098. * that drbd_set_out_of_sync() can not be called. This function MAY ONLY be
  3099. * called from worker context. It MUST NOT be used while a previous such
  3100. * work is still pending!
  3101. *
  3102. * Its worker function encloses the call of io_fn() by get_ldev() and
  3103. * put_ldev().
  3104. */
  3105. void drbd_queue_bitmap_io(struct drbd_device *device,
  3106. int (*io_fn)(struct drbd_device *),
  3107. void (*done)(struct drbd_device *, int),
  3108. char *why, enum bm_flag flags)
  3109. {
  3110. D_ASSERT(device, current == first_peer_device(device)->connection->worker.task);
  3111. D_ASSERT(device, !test_bit(BITMAP_IO_QUEUED, &device->flags));
  3112. D_ASSERT(device, !test_bit(BITMAP_IO, &device->flags));
  3113. D_ASSERT(device, list_empty(&device->bm_io_work.w.list));
  3114. if (device->bm_io_work.why)
  3115. drbd_err(device, "FIXME going to queue '%s' but '%s' still pending?\n",
  3116. why, device->bm_io_work.why);
  3117. device->bm_io_work.io_fn = io_fn;
  3118. device->bm_io_work.done = done;
  3119. device->bm_io_work.why = why;
  3120. device->bm_io_work.flags = flags;
  3121. spin_lock_irq(&device->resource->req_lock);
  3122. set_bit(BITMAP_IO, &device->flags);
  3123. /* don't wait for pending application IO if the caller indicates that
  3124. * application IO does not conflict anyways. */
  3125. if (flags == BM_LOCKED_CHANGE_ALLOWED || atomic_read(&device->ap_bio_cnt) == 0) {
  3126. if (!test_and_set_bit(BITMAP_IO_QUEUED, &device->flags))
  3127. drbd_queue_work(&first_peer_device(device)->connection->sender_work,
  3128. &device->bm_io_work.w);
  3129. }
  3130. spin_unlock_irq(&device->resource->req_lock);
  3131. }
  3132. /**
  3133. * drbd_bitmap_io() - Does an IO operation on the whole bitmap
  3134. * @device: DRBD device.
  3135. * @io_fn: IO callback to be called when bitmap IO is possible
  3136. * @why: Descriptive text of the reason for doing the IO
  3137. *
  3138. * freezes application IO while that the actual IO operations runs. This
  3139. * functions MAY NOT be called from worker context.
  3140. */
  3141. int drbd_bitmap_io(struct drbd_device *device, int (*io_fn)(struct drbd_device *),
  3142. char *why, enum bm_flag flags)
  3143. {
  3144. /* Only suspend io, if some operation is supposed to be locked out */
  3145. const bool do_suspend_io = flags & (BM_DONT_CLEAR|BM_DONT_SET|BM_DONT_TEST);
  3146. int rv;
  3147. D_ASSERT(device, current != first_peer_device(device)->connection->worker.task);
  3148. if (do_suspend_io)
  3149. drbd_suspend_io(device);
  3150. drbd_bm_lock(device, why, flags);
  3151. rv = io_fn(device);
  3152. drbd_bm_unlock(device);
  3153. if (do_suspend_io)
  3154. drbd_resume_io(device);
  3155. return rv;
  3156. }
  3157. void drbd_md_set_flag(struct drbd_device *device, int flag) __must_hold(local)
  3158. {
  3159. if ((device->ldev->md.flags & flag) != flag) {
  3160. drbd_md_mark_dirty(device);
  3161. device->ldev->md.flags |= flag;
  3162. }
  3163. }
  3164. void drbd_md_clear_flag(struct drbd_device *device, int flag) __must_hold(local)
  3165. {
  3166. if ((device->ldev->md.flags & flag) != 0) {
  3167. drbd_md_mark_dirty(device);
  3168. device->ldev->md.flags &= ~flag;
  3169. }
  3170. }
  3171. int drbd_md_test_flag(struct drbd_backing_dev *bdev, int flag)
  3172. {
  3173. return (bdev->md.flags & flag) != 0;
  3174. }
  3175. static void md_sync_timer_fn(unsigned long data)
  3176. {
  3177. struct drbd_device *device = (struct drbd_device *) data;
  3178. drbd_device_post_work(device, MD_SYNC);
  3179. }
  3180. const char *cmdname(enum drbd_packet cmd)
  3181. {
  3182. /* THINK may need to become several global tables
  3183. * when we want to support more than
  3184. * one PRO_VERSION */
  3185. static const char *cmdnames[] = {
  3186. [P_DATA] = "Data",
  3187. [P_WSAME] = "WriteSame",
  3188. [P_TRIM] = "Trim",
  3189. [P_DATA_REPLY] = "DataReply",
  3190. [P_RS_DATA_REPLY] = "RSDataReply",
  3191. [P_BARRIER] = "Barrier",
  3192. [P_BITMAP] = "ReportBitMap",
  3193. [P_BECOME_SYNC_TARGET] = "BecomeSyncTarget",
  3194. [P_BECOME_SYNC_SOURCE] = "BecomeSyncSource",
  3195. [P_UNPLUG_REMOTE] = "UnplugRemote",
  3196. [P_DATA_REQUEST] = "DataRequest",
  3197. [P_RS_DATA_REQUEST] = "RSDataRequest",
  3198. [P_SYNC_PARAM] = "SyncParam",
  3199. [P_SYNC_PARAM89] = "SyncParam89",
  3200. [P_PROTOCOL] = "ReportProtocol",
  3201. [P_UUIDS] = "ReportUUIDs",
  3202. [P_SIZES] = "ReportSizes",
  3203. [P_STATE] = "ReportState",
  3204. [P_SYNC_UUID] = "ReportSyncUUID",
  3205. [P_AUTH_CHALLENGE] = "AuthChallenge",
  3206. [P_AUTH_RESPONSE] = "AuthResponse",
  3207. [P_PING] = "Ping",
  3208. [P_PING_ACK] = "PingAck",
  3209. [P_RECV_ACK] = "RecvAck",
  3210. [P_WRITE_ACK] = "WriteAck",
  3211. [P_RS_WRITE_ACK] = "RSWriteAck",
  3212. [P_SUPERSEDED] = "Superseded",
  3213. [P_NEG_ACK] = "NegAck",
  3214. [P_NEG_DREPLY] = "NegDReply",
  3215. [P_NEG_RS_DREPLY] = "NegRSDReply",
  3216. [P_BARRIER_ACK] = "BarrierAck",
  3217. [P_STATE_CHG_REQ] = "StateChgRequest",
  3218. [P_STATE_CHG_REPLY] = "StateChgReply",
  3219. [P_OV_REQUEST] = "OVRequest",
  3220. [P_OV_REPLY] = "OVReply",
  3221. [P_OV_RESULT] = "OVResult",
  3222. [P_CSUM_RS_REQUEST] = "CsumRSRequest",
  3223. [P_RS_IS_IN_SYNC] = "CsumRSIsInSync",
  3224. [P_COMPRESSED_BITMAP] = "CBitmap",
  3225. [P_DELAY_PROBE] = "DelayProbe",
  3226. [P_OUT_OF_SYNC] = "OutOfSync",
  3227. [P_RETRY_WRITE] = "RetryWrite",
  3228. [P_RS_CANCEL] = "RSCancel",
  3229. [P_CONN_ST_CHG_REQ] = "conn_st_chg_req",
  3230. [P_CONN_ST_CHG_REPLY] = "conn_st_chg_reply",
  3231. [P_RETRY_WRITE] = "retry_write",
  3232. [P_PROTOCOL_UPDATE] = "protocol_update",
  3233. [P_RS_THIN_REQ] = "rs_thin_req",
  3234. [P_RS_DEALLOCATED] = "rs_deallocated",
  3235. /* enum drbd_packet, but not commands - obsoleted flags:
  3236. * P_MAY_IGNORE
  3237. * P_MAX_OPT_CMD
  3238. */
  3239. };
  3240. /* too big for the array: 0xfffX */
  3241. if (cmd == P_INITIAL_META)
  3242. return "InitialMeta";
  3243. if (cmd == P_INITIAL_DATA)
  3244. return "InitialData";
  3245. if (cmd == P_CONNECTION_FEATURES)
  3246. return "ConnectionFeatures";
  3247. if (cmd >= ARRAY_SIZE(cmdnames))
  3248. return "Unknown";
  3249. return cmdnames[cmd];
  3250. }
  3251. /**
  3252. * drbd_wait_misc - wait for a request to make progress
  3253. * @device: device associated with the request
  3254. * @i: the struct drbd_interval embedded in struct drbd_request or
  3255. * struct drbd_peer_request
  3256. */
  3257. int drbd_wait_misc(struct drbd_device *device, struct drbd_interval *i)
  3258. {
  3259. struct net_conf *nc;
  3260. DEFINE_WAIT(wait);
  3261. long timeout;
  3262. rcu_read_lock();
  3263. nc = rcu_dereference(first_peer_device(device)->connection->net_conf);
  3264. if (!nc) {
  3265. rcu_read_unlock();
  3266. return -ETIMEDOUT;
  3267. }
  3268. timeout = nc->ko_count ? nc->timeout * HZ / 10 * nc->ko_count : MAX_SCHEDULE_TIMEOUT;
  3269. rcu_read_unlock();
  3270. /* Indicate to wake up device->misc_wait on progress. */
  3271. i->waiting = true;
  3272. prepare_to_wait(&device->misc_wait, &wait, TASK_INTERRUPTIBLE);
  3273. spin_unlock_irq(&device->resource->req_lock);
  3274. timeout = schedule_timeout(timeout);
  3275. finish_wait(&device->misc_wait, &wait);
  3276. spin_lock_irq(&device->resource->req_lock);
  3277. if (!timeout || device->state.conn < C_CONNECTED)
  3278. return -ETIMEDOUT;
  3279. if (signal_pending(current))
  3280. return -ERESTARTSYS;
  3281. return 0;
  3282. }
  3283. void lock_all_resources(void)
  3284. {
  3285. struct drbd_resource *resource;
  3286. int __maybe_unused i = 0;
  3287. mutex_lock(&resources_mutex);
  3288. local_irq_disable();
  3289. for_each_resource(resource, &drbd_resources)
  3290. spin_lock_nested(&resource->req_lock, i++);
  3291. }
  3292. void unlock_all_resources(void)
  3293. {
  3294. struct drbd_resource *resource;
  3295. for_each_resource(resource, &drbd_resources)
  3296. spin_unlock(&resource->req_lock);
  3297. local_irq_enable();
  3298. mutex_unlock(&resources_mutex);
  3299. }
  3300. #ifdef CONFIG_DRBD_FAULT_INJECTION
  3301. /* Fault insertion support including random number generator shamelessly
  3302. * stolen from kernel/rcutorture.c */
  3303. struct fault_random_state {
  3304. unsigned long state;
  3305. unsigned long count;
  3306. };
  3307. #define FAULT_RANDOM_MULT 39916801 /* prime */
  3308. #define FAULT_RANDOM_ADD 479001701 /* prime */
  3309. #define FAULT_RANDOM_REFRESH 10000
  3310. /*
  3311. * Crude but fast random-number generator. Uses a linear congruential
  3312. * generator, with occasional help from get_random_bytes().
  3313. */
  3314. static unsigned long
  3315. _drbd_fault_random(struct fault_random_state *rsp)
  3316. {
  3317. long refresh;
  3318. if (!rsp->count--) {
  3319. get_random_bytes(&refresh, sizeof(refresh));
  3320. rsp->state += refresh;
  3321. rsp->count = FAULT_RANDOM_REFRESH;
  3322. }
  3323. rsp->state = rsp->state * FAULT_RANDOM_MULT + FAULT_RANDOM_ADD;
  3324. return swahw32(rsp->state);
  3325. }
  3326. static char *
  3327. _drbd_fault_str(unsigned int type) {
  3328. static char *_faults[] = {
  3329. [DRBD_FAULT_MD_WR] = "Meta-data write",
  3330. [DRBD_FAULT_MD_RD] = "Meta-data read",
  3331. [DRBD_FAULT_RS_WR] = "Resync write",
  3332. [DRBD_FAULT_RS_RD] = "Resync read",
  3333. [DRBD_FAULT_DT_WR] = "Data write",
  3334. [DRBD_FAULT_DT_RD] = "Data read",
  3335. [DRBD_FAULT_DT_RA] = "Data read ahead",
  3336. [DRBD_FAULT_BM_ALLOC] = "BM allocation",
  3337. [DRBD_FAULT_AL_EE] = "EE allocation",
  3338. [DRBD_FAULT_RECEIVE] = "receive data corruption",
  3339. };
  3340. return (type < DRBD_FAULT_MAX) ? _faults[type] : "**Unknown**";
  3341. }
  3342. unsigned int
  3343. _drbd_insert_fault(struct drbd_device *device, unsigned int type)
  3344. {
  3345. static struct fault_random_state rrs = {0, 0};
  3346. unsigned int ret = (
  3347. (drbd_fault_devs == 0 ||
  3348. ((1 << device_to_minor(device)) & drbd_fault_devs) != 0) &&
  3349. (((_drbd_fault_random(&rrs) % 100) + 1) <= drbd_fault_rate));
  3350. if (ret) {
  3351. drbd_fault_count++;
  3352. if (__ratelimit(&drbd_ratelimit_state))
  3353. drbd_warn(device, "***Simulating %s failure\n",
  3354. _drbd_fault_str(type));
  3355. }
  3356. return ret;
  3357. }
  3358. #endif
  3359. const char *drbd_buildtag(void)
  3360. {
  3361. /* DRBD built from external sources has here a reference to the
  3362. git hash of the source code. */
  3363. static char buildtag[38] = "\0uilt-in";
  3364. if (buildtag[0] == 0) {
  3365. #ifdef MODULE
  3366. sprintf(buildtag, "srcversion: %-24s", THIS_MODULE->srcversion);
  3367. #else
  3368. buildtag[0] = 'b';
  3369. #endif
  3370. }
  3371. return buildtag;
  3372. }
  3373. module_init(drbd_init)
  3374. module_exit(drbd_cleanup)
  3375. EXPORT_SYMBOL(drbd_conn_str);
  3376. EXPORT_SYMBOL(drbd_role_str);
  3377. EXPORT_SYMBOL(drbd_disk_str);
  3378. EXPORT_SYMBOL(drbd_set_st_err_str);