drbd_main.c 109 KB

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