drbd_main.c 115 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204
  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. #include <linux/module.h>
  22. #include <linux/drbd.h>
  23. #include <asm/uaccess.h>
  24. #include <asm/types.h>
  25. #include <net/sock.h>
  26. #include <linux/ctype.h>
  27. #include <linux/mutex.h>
  28. #include <linux/fs.h>
  29. #include <linux/file.h>
  30. #include <linux/proc_fs.h>
  31. #include <linux/init.h>
  32. #include <linux/mm.h>
  33. #include <linux/memcontrol.h>
  34. #include <linux/mm_inline.h>
  35. #include <linux/slab.h>
  36. #include <linux/random.h>
  37. #include <linux/reboot.h>
  38. #include <linux/notifier.h>
  39. #include <linux/kthread.h>
  40. #define __KERNEL_SYSCALLS__
  41. #include <linux/unistd.h>
  42. #include <linux/vmalloc.h>
  43. #include <linux/drbd_limits.h>
  44. #include "drbd_int.h"
  45. #include "drbd_req.h" /* only for _req_mod in tl_release and tl_clear */
  46. #include "drbd_vli.h"
  47. struct after_state_chg_work {
  48. struct drbd_work w;
  49. union drbd_state os;
  50. union drbd_state ns;
  51. enum chg_state_flags flags;
  52. struct completion *done;
  53. };
  54. static DEFINE_MUTEX(drbd_main_mutex);
  55. int drbdd_init(struct drbd_thread *);
  56. int drbd_worker(struct drbd_thread *);
  57. int drbd_asender(struct drbd_thread *);
  58. int drbd_init(void);
  59. static int drbd_open(struct block_device *bdev, fmode_t mode);
  60. static int drbd_release(struct gendisk *gd, fmode_t mode);
  61. static int w_after_state_ch(struct drbd_conf *mdev, struct drbd_work *w, int unused);
  62. static void after_state_ch(struct drbd_conf *mdev, union drbd_state os,
  63. union drbd_state ns, enum chg_state_flags flags);
  64. static int w_md_sync(struct drbd_conf *mdev, struct drbd_work *w, int unused);
  65. static void md_sync_timer_fn(unsigned long data);
  66. static int w_bitmap_io(struct drbd_conf *mdev, struct drbd_work *w, int unused);
  67. static int w_go_diskless(struct drbd_conf *mdev, struct drbd_work *w, int unused);
  68. MODULE_AUTHOR("Philipp Reisner <phil@linbit.com>, "
  69. "Lars Ellenberg <lars@linbit.com>");
  70. MODULE_DESCRIPTION("drbd - Distributed Replicated Block Device v" REL_VERSION);
  71. MODULE_VERSION(REL_VERSION);
  72. MODULE_LICENSE("GPL");
  73. MODULE_PARM_DESC(minor_count, "Maximum number of drbd devices ("
  74. __stringify(DRBD_MINOR_COUNT_MIN) "-" __stringify(DRBD_MINOR_COUNT_MAX) ")");
  75. MODULE_ALIAS_BLOCKDEV_MAJOR(DRBD_MAJOR);
  76. #include <linux/moduleparam.h>
  77. /* allow_open_on_secondary */
  78. MODULE_PARM_DESC(allow_oos, "DONT USE!");
  79. /* thanks to these macros, if compiled into the kernel (not-module),
  80. * this becomes the boot parameter drbd.minor_count */
  81. module_param(minor_count, uint, 0444);
  82. module_param(disable_sendpage, bool, 0644);
  83. module_param(allow_oos, bool, 0);
  84. module_param(cn_idx, uint, 0444);
  85. module_param(proc_details, int, 0644);
  86. #ifdef CONFIG_DRBD_FAULT_INJECTION
  87. int enable_faults;
  88. int fault_rate;
  89. static int fault_count;
  90. int fault_devs;
  91. /* bitmap of enabled faults */
  92. module_param(enable_faults, int, 0664);
  93. /* fault rate % value - applies to all enabled faults */
  94. module_param(fault_rate, int, 0664);
  95. /* count of faults inserted */
  96. module_param(fault_count, int, 0664);
  97. /* bitmap of devices to insert faults on */
  98. module_param(fault_devs, int, 0644);
  99. #endif
  100. /* module parameter, defined */
  101. unsigned int minor_count = DRBD_MINOR_COUNT_DEF;
  102. int disable_sendpage;
  103. int allow_oos;
  104. unsigned int cn_idx = CN_IDX_DRBD;
  105. int proc_details; /* Detail level in proc drbd*/
  106. /* Module parameter for setting the user mode helper program
  107. * to run. Default is /sbin/drbdadm */
  108. char usermode_helper[80] = "/sbin/drbdadm";
  109. module_param_string(usermode_helper, usermode_helper, sizeof(usermode_helper), 0644);
  110. /* in 2.6.x, our device mapping and config info contains our virtual gendisks
  111. * as member "struct gendisk *vdisk;"
  112. */
  113. struct drbd_conf **minor_table;
  114. struct kmem_cache *drbd_request_cache;
  115. struct kmem_cache *drbd_ee_cache; /* epoch entries */
  116. struct kmem_cache *drbd_bm_ext_cache; /* bitmap extents */
  117. struct kmem_cache *drbd_al_ext_cache; /* activity log extents */
  118. mempool_t *drbd_request_mempool;
  119. mempool_t *drbd_ee_mempool;
  120. /* I do not use a standard mempool, because:
  121. 1) I want to hand out the pre-allocated objects first.
  122. 2) I want to be able to interrupt sleeping allocation with a signal.
  123. Note: This is a single linked list, the next pointer is the private
  124. member of struct page.
  125. */
  126. struct page *drbd_pp_pool;
  127. spinlock_t drbd_pp_lock;
  128. int drbd_pp_vacant;
  129. wait_queue_head_t drbd_pp_wait;
  130. DEFINE_RATELIMIT_STATE(drbd_ratelimit_state, 5 * HZ, 5);
  131. static const struct block_device_operations drbd_ops = {
  132. .owner = THIS_MODULE,
  133. .open = drbd_open,
  134. .release = drbd_release,
  135. };
  136. #define ARRY_SIZE(A) (sizeof(A)/sizeof(A[0]))
  137. #ifdef __CHECKER__
  138. /* When checking with sparse, and this is an inline function, sparse will
  139. give tons of false positives. When this is a real functions sparse works.
  140. */
  141. int _get_ldev_if_state(struct drbd_conf *mdev, enum drbd_disk_state mins)
  142. {
  143. int io_allowed;
  144. atomic_inc(&mdev->local_cnt);
  145. io_allowed = (mdev->state.disk >= mins);
  146. if (!io_allowed) {
  147. if (atomic_dec_and_test(&mdev->local_cnt))
  148. wake_up(&mdev->misc_wait);
  149. }
  150. return io_allowed;
  151. }
  152. #endif
  153. /**
  154. * DOC: The transfer log
  155. *
  156. * The transfer log is a single linked list of &struct drbd_tl_epoch objects.
  157. * mdev->newest_tle points to the head, mdev->oldest_tle points to the tail
  158. * of the list. There is always at least one &struct drbd_tl_epoch object.
  159. *
  160. * Each &struct drbd_tl_epoch has a circular double linked list of requests
  161. * attached.
  162. */
  163. static int tl_init(struct drbd_conf *mdev)
  164. {
  165. struct drbd_tl_epoch *b;
  166. /* during device minor initialization, we may well use GFP_KERNEL */
  167. b = kmalloc(sizeof(struct drbd_tl_epoch), GFP_KERNEL);
  168. if (!b)
  169. return 0;
  170. INIT_LIST_HEAD(&b->requests);
  171. INIT_LIST_HEAD(&b->w.list);
  172. b->next = NULL;
  173. b->br_number = 4711;
  174. b->n_writes = 0;
  175. b->w.cb = NULL; /* if this is != NULL, we need to dec_ap_pending in tl_clear */
  176. mdev->oldest_tle = b;
  177. mdev->newest_tle = b;
  178. INIT_LIST_HEAD(&mdev->out_of_sequence_requests);
  179. mdev->tl_hash = NULL;
  180. mdev->tl_hash_s = 0;
  181. return 1;
  182. }
  183. static void tl_cleanup(struct drbd_conf *mdev)
  184. {
  185. D_ASSERT(mdev->oldest_tle == mdev->newest_tle);
  186. D_ASSERT(list_empty(&mdev->out_of_sequence_requests));
  187. kfree(mdev->oldest_tle);
  188. mdev->oldest_tle = NULL;
  189. kfree(mdev->unused_spare_tle);
  190. mdev->unused_spare_tle = NULL;
  191. kfree(mdev->tl_hash);
  192. mdev->tl_hash = NULL;
  193. mdev->tl_hash_s = 0;
  194. }
  195. /**
  196. * _tl_add_barrier() - Adds a barrier to the transfer log
  197. * @mdev: DRBD device.
  198. * @new: Barrier to be added before the current head of the TL.
  199. *
  200. * The caller must hold the req_lock.
  201. */
  202. void _tl_add_barrier(struct drbd_conf *mdev, struct drbd_tl_epoch *new)
  203. {
  204. struct drbd_tl_epoch *newest_before;
  205. INIT_LIST_HEAD(&new->requests);
  206. INIT_LIST_HEAD(&new->w.list);
  207. new->w.cb = NULL; /* if this is != NULL, we need to dec_ap_pending in tl_clear */
  208. new->next = NULL;
  209. new->n_writes = 0;
  210. newest_before = mdev->newest_tle;
  211. /* never send a barrier number == 0, because that is special-cased
  212. * when using TCQ for our write ordering code */
  213. new->br_number = (newest_before->br_number+1) ?: 1;
  214. if (mdev->newest_tle != new) {
  215. mdev->newest_tle->next = new;
  216. mdev->newest_tle = new;
  217. }
  218. }
  219. /**
  220. * tl_release() - Free or recycle the oldest &struct drbd_tl_epoch object of the TL
  221. * @mdev: DRBD device.
  222. * @barrier_nr: Expected identifier of the DRBD write barrier packet.
  223. * @set_size: Expected number of requests before that barrier.
  224. *
  225. * In case the passed barrier_nr or set_size does not match the oldest
  226. * &struct drbd_tl_epoch objects this function will cause a termination
  227. * of the connection.
  228. */
  229. void tl_release(struct drbd_conf *mdev, unsigned int barrier_nr,
  230. unsigned int set_size)
  231. {
  232. struct drbd_tl_epoch *b, *nob; /* next old barrier */
  233. struct list_head *le, *tle;
  234. struct drbd_request *r;
  235. spin_lock_irq(&mdev->req_lock);
  236. b = mdev->oldest_tle;
  237. /* first some paranoia code */
  238. if (b == NULL) {
  239. dev_err(DEV, "BAD! BarrierAck #%u received, but no epoch in tl!?\n",
  240. barrier_nr);
  241. goto bail;
  242. }
  243. if (b->br_number != barrier_nr) {
  244. dev_err(DEV, "BAD! BarrierAck #%u received, expected #%u!\n",
  245. barrier_nr, b->br_number);
  246. goto bail;
  247. }
  248. if (b->n_writes != set_size) {
  249. dev_err(DEV, "BAD! BarrierAck #%u received with n_writes=%u, expected n_writes=%u!\n",
  250. barrier_nr, set_size, b->n_writes);
  251. goto bail;
  252. }
  253. /* Clean up list of requests processed during current epoch */
  254. list_for_each_safe(le, tle, &b->requests) {
  255. r = list_entry(le, struct drbd_request, tl_requests);
  256. _req_mod(r, barrier_acked);
  257. }
  258. /* There could be requests on the list waiting for completion
  259. of the write to the local disk. To avoid corruptions of
  260. slab's data structures we have to remove the lists head.
  261. Also there could have been a barrier ack out of sequence, overtaking
  262. the write acks - which would be a bug and violating write ordering.
  263. To not deadlock in case we lose connection while such requests are
  264. still pending, we need some way to find them for the
  265. _req_mode(connection_lost_while_pending).
  266. These have been list_move'd to the out_of_sequence_requests list in
  267. _req_mod(, barrier_acked) above.
  268. */
  269. list_del_init(&b->requests);
  270. nob = b->next;
  271. if (test_and_clear_bit(CREATE_BARRIER, &mdev->flags)) {
  272. _tl_add_barrier(mdev, b);
  273. if (nob)
  274. mdev->oldest_tle = nob;
  275. /* if nob == NULL b was the only barrier, and becomes the new
  276. barrier. Therefore mdev->oldest_tle points already to b */
  277. } else {
  278. D_ASSERT(nob != NULL);
  279. mdev->oldest_tle = nob;
  280. kfree(b);
  281. }
  282. spin_unlock_irq(&mdev->req_lock);
  283. dec_ap_pending(mdev);
  284. return;
  285. bail:
  286. spin_unlock_irq(&mdev->req_lock);
  287. drbd_force_state(mdev, NS(conn, C_PROTOCOL_ERROR));
  288. }
  289. /**
  290. * _tl_restart() - Walks the transfer log, and applies an action to all requests
  291. * @mdev: DRBD device.
  292. * @what: The action/event to perform with all request objects
  293. *
  294. * @what might be one of connection_lost_while_pending, resend, fail_frozen_disk_io,
  295. * restart_frozen_disk_io.
  296. */
  297. static void _tl_restart(struct drbd_conf *mdev, enum drbd_req_event what)
  298. {
  299. struct drbd_tl_epoch *b, *tmp, **pn;
  300. struct list_head *le, *tle, carry_reads;
  301. struct drbd_request *req;
  302. int rv, n_writes, n_reads;
  303. b = mdev->oldest_tle;
  304. pn = &mdev->oldest_tle;
  305. while (b) {
  306. n_writes = 0;
  307. n_reads = 0;
  308. INIT_LIST_HEAD(&carry_reads);
  309. list_for_each_safe(le, tle, &b->requests) {
  310. req = list_entry(le, struct drbd_request, tl_requests);
  311. rv = _req_mod(req, what);
  312. n_writes += (rv & MR_WRITE) >> MR_WRITE_SHIFT;
  313. n_reads += (rv & MR_READ) >> MR_READ_SHIFT;
  314. }
  315. tmp = b->next;
  316. if (n_writes) {
  317. if (what == resend) {
  318. b->n_writes = n_writes;
  319. if (b->w.cb == NULL) {
  320. b->w.cb = w_send_barrier;
  321. inc_ap_pending(mdev);
  322. set_bit(CREATE_BARRIER, &mdev->flags);
  323. }
  324. drbd_queue_work(&mdev->data.work, &b->w);
  325. }
  326. pn = &b->next;
  327. } else {
  328. if (n_reads)
  329. list_add(&carry_reads, &b->requests);
  330. /* there could still be requests on that ring list,
  331. * in case local io is still pending */
  332. list_del(&b->requests);
  333. /* dec_ap_pending corresponding to queue_barrier.
  334. * the newest barrier may not have been queued yet,
  335. * in which case w.cb is still NULL. */
  336. if (b->w.cb != NULL)
  337. dec_ap_pending(mdev);
  338. if (b == mdev->newest_tle) {
  339. /* recycle, but reinit! */
  340. D_ASSERT(tmp == NULL);
  341. INIT_LIST_HEAD(&b->requests);
  342. list_splice(&carry_reads, &b->requests);
  343. INIT_LIST_HEAD(&b->w.list);
  344. b->w.cb = NULL;
  345. b->br_number = net_random();
  346. b->n_writes = 0;
  347. *pn = b;
  348. break;
  349. }
  350. *pn = tmp;
  351. kfree(b);
  352. }
  353. b = tmp;
  354. list_splice(&carry_reads, &b->requests);
  355. }
  356. }
  357. /**
  358. * tl_clear() - Clears all requests and &struct drbd_tl_epoch objects out of the TL
  359. * @mdev: DRBD device.
  360. *
  361. * This is called after the connection to the peer was lost. The storage covered
  362. * by the requests on the transfer gets marked as our of sync. Called from the
  363. * receiver thread and the worker thread.
  364. */
  365. void tl_clear(struct drbd_conf *mdev)
  366. {
  367. struct list_head *le, *tle;
  368. struct drbd_request *r;
  369. spin_lock_irq(&mdev->req_lock);
  370. _tl_restart(mdev, connection_lost_while_pending);
  371. /* we expect this list to be empty. */
  372. D_ASSERT(list_empty(&mdev->out_of_sequence_requests));
  373. /* but just in case, clean it up anyways! */
  374. list_for_each_safe(le, tle, &mdev->out_of_sequence_requests) {
  375. r = list_entry(le, struct drbd_request, tl_requests);
  376. /* It would be nice to complete outside of spinlock.
  377. * But this is easier for now. */
  378. _req_mod(r, connection_lost_while_pending);
  379. }
  380. /* ensure bit indicating barrier is required is clear */
  381. clear_bit(CREATE_BARRIER, &mdev->flags);
  382. memset(mdev->app_reads_hash, 0, APP_R_HSIZE*sizeof(void *));
  383. spin_unlock_irq(&mdev->req_lock);
  384. }
  385. void tl_restart(struct drbd_conf *mdev, enum drbd_req_event what)
  386. {
  387. spin_lock_irq(&mdev->req_lock);
  388. _tl_restart(mdev, what);
  389. spin_unlock_irq(&mdev->req_lock);
  390. }
  391. /**
  392. * cl_wide_st_chg() - true if the state change is a cluster wide one
  393. * @mdev: DRBD device.
  394. * @os: old (current) state.
  395. * @ns: new (wanted) state.
  396. */
  397. static int cl_wide_st_chg(struct drbd_conf *mdev,
  398. union drbd_state os, union drbd_state ns)
  399. {
  400. return (os.conn >= C_CONNECTED && ns.conn >= C_CONNECTED &&
  401. ((os.role != R_PRIMARY && ns.role == R_PRIMARY) ||
  402. (os.conn != C_STARTING_SYNC_T && ns.conn == C_STARTING_SYNC_T) ||
  403. (os.conn != C_STARTING_SYNC_S && ns.conn == C_STARTING_SYNC_S) ||
  404. (os.disk != D_DISKLESS && ns.disk == D_DISKLESS))) ||
  405. (os.conn >= C_CONNECTED && ns.conn == C_DISCONNECTING) ||
  406. (os.conn == C_CONNECTED && ns.conn == C_VERIFY_S);
  407. }
  408. enum drbd_state_rv
  409. drbd_change_state(struct drbd_conf *mdev, enum chg_state_flags f,
  410. union drbd_state mask, union drbd_state val)
  411. {
  412. unsigned long flags;
  413. union drbd_state os, ns;
  414. enum drbd_state_rv rv;
  415. spin_lock_irqsave(&mdev->req_lock, flags);
  416. os = mdev->state;
  417. ns.i = (os.i & ~mask.i) | val.i;
  418. rv = _drbd_set_state(mdev, ns, f, NULL);
  419. ns = mdev->state;
  420. spin_unlock_irqrestore(&mdev->req_lock, flags);
  421. return rv;
  422. }
  423. /**
  424. * drbd_force_state() - Impose a change which happens outside our control on our state
  425. * @mdev: DRBD device.
  426. * @mask: mask of state bits to change.
  427. * @val: value of new state bits.
  428. */
  429. void drbd_force_state(struct drbd_conf *mdev,
  430. union drbd_state mask, union drbd_state val)
  431. {
  432. drbd_change_state(mdev, CS_HARD, mask, val);
  433. }
  434. static enum drbd_state_rv is_valid_state(struct drbd_conf *, union drbd_state);
  435. static enum drbd_state_rv is_valid_state_transition(struct drbd_conf *,
  436. union drbd_state,
  437. union drbd_state);
  438. static union drbd_state sanitize_state(struct drbd_conf *mdev, union drbd_state os,
  439. union drbd_state ns, const char **warn_sync_abort);
  440. int drbd_send_state_req(struct drbd_conf *,
  441. union drbd_state, union drbd_state);
  442. static enum drbd_state_rv
  443. _req_st_cond(struct drbd_conf *mdev, union drbd_state mask,
  444. union drbd_state val)
  445. {
  446. union drbd_state os, ns;
  447. unsigned long flags;
  448. enum drbd_state_rv rv;
  449. if (test_and_clear_bit(CL_ST_CHG_SUCCESS, &mdev->flags))
  450. return SS_CW_SUCCESS;
  451. if (test_and_clear_bit(CL_ST_CHG_FAIL, &mdev->flags))
  452. return SS_CW_FAILED_BY_PEER;
  453. rv = 0;
  454. spin_lock_irqsave(&mdev->req_lock, flags);
  455. os = mdev->state;
  456. ns.i = (os.i & ~mask.i) | val.i;
  457. ns = sanitize_state(mdev, os, ns, NULL);
  458. if (!cl_wide_st_chg(mdev, os, ns))
  459. rv = SS_CW_NO_NEED;
  460. if (!rv) {
  461. rv = is_valid_state(mdev, ns);
  462. if (rv == SS_SUCCESS) {
  463. rv = is_valid_state_transition(mdev, ns, os);
  464. if (rv == SS_SUCCESS)
  465. rv = SS_UNKNOWN_ERROR; /* cont waiting, otherwise fail. */
  466. }
  467. }
  468. spin_unlock_irqrestore(&mdev->req_lock, flags);
  469. return rv;
  470. }
  471. /**
  472. * drbd_req_state() - Perform an eventually cluster wide state change
  473. * @mdev: DRBD device.
  474. * @mask: mask of state bits to change.
  475. * @val: value of new state bits.
  476. * @f: flags
  477. *
  478. * Should not be called directly, use drbd_request_state() or
  479. * _drbd_request_state().
  480. */
  481. static enum drbd_state_rv
  482. drbd_req_state(struct drbd_conf *mdev, union drbd_state mask,
  483. union drbd_state val, enum chg_state_flags f)
  484. {
  485. struct completion done;
  486. unsigned long flags;
  487. union drbd_state os, ns;
  488. enum drbd_state_rv rv;
  489. init_completion(&done);
  490. if (f & CS_SERIALIZE)
  491. mutex_lock(&mdev->state_mutex);
  492. spin_lock_irqsave(&mdev->req_lock, flags);
  493. os = mdev->state;
  494. ns.i = (os.i & ~mask.i) | val.i;
  495. ns = sanitize_state(mdev, os, ns, NULL);
  496. if (cl_wide_st_chg(mdev, os, ns)) {
  497. rv = is_valid_state(mdev, ns);
  498. if (rv == SS_SUCCESS)
  499. rv = is_valid_state_transition(mdev, ns, os);
  500. spin_unlock_irqrestore(&mdev->req_lock, flags);
  501. if (rv < SS_SUCCESS) {
  502. if (f & CS_VERBOSE)
  503. print_st_err(mdev, os, ns, rv);
  504. goto abort;
  505. }
  506. drbd_state_lock(mdev);
  507. if (!drbd_send_state_req(mdev, mask, val)) {
  508. drbd_state_unlock(mdev);
  509. rv = SS_CW_FAILED_BY_PEER;
  510. if (f & CS_VERBOSE)
  511. print_st_err(mdev, os, ns, rv);
  512. goto abort;
  513. }
  514. wait_event(mdev->state_wait,
  515. (rv = _req_st_cond(mdev, mask, val)));
  516. if (rv < SS_SUCCESS) {
  517. drbd_state_unlock(mdev);
  518. if (f & CS_VERBOSE)
  519. print_st_err(mdev, os, ns, rv);
  520. goto abort;
  521. }
  522. spin_lock_irqsave(&mdev->req_lock, flags);
  523. os = mdev->state;
  524. ns.i = (os.i & ~mask.i) | val.i;
  525. rv = _drbd_set_state(mdev, ns, f, &done);
  526. drbd_state_unlock(mdev);
  527. } else {
  528. rv = _drbd_set_state(mdev, ns, f, &done);
  529. }
  530. spin_unlock_irqrestore(&mdev->req_lock, flags);
  531. if (f & CS_WAIT_COMPLETE && rv == SS_SUCCESS) {
  532. D_ASSERT(current != mdev->worker.task);
  533. wait_for_completion(&done);
  534. }
  535. abort:
  536. if (f & CS_SERIALIZE)
  537. mutex_unlock(&mdev->state_mutex);
  538. return rv;
  539. }
  540. /**
  541. * _drbd_request_state() - Request a state change (with flags)
  542. * @mdev: DRBD device.
  543. * @mask: mask of state bits to change.
  544. * @val: value of new state bits.
  545. * @f: flags
  546. *
  547. * Cousin of drbd_request_state(), useful with the CS_WAIT_COMPLETE
  548. * flag, or when logging of failed state change requests is not desired.
  549. */
  550. enum drbd_state_rv
  551. _drbd_request_state(struct drbd_conf *mdev, union drbd_state mask,
  552. union drbd_state val, enum chg_state_flags f)
  553. {
  554. enum drbd_state_rv rv;
  555. wait_event(mdev->state_wait,
  556. (rv = drbd_req_state(mdev, mask, val, f)) != SS_IN_TRANSIENT_STATE);
  557. return rv;
  558. }
  559. static void print_st(struct drbd_conf *mdev, char *name, union drbd_state ns)
  560. {
  561. dev_err(DEV, " %s = { cs:%s ro:%s/%s ds:%s/%s %c%c%c%c }\n",
  562. name,
  563. drbd_conn_str(ns.conn),
  564. drbd_role_str(ns.role),
  565. drbd_role_str(ns.peer),
  566. drbd_disk_str(ns.disk),
  567. drbd_disk_str(ns.pdsk),
  568. is_susp(ns) ? 's' : 'r',
  569. ns.aftr_isp ? 'a' : '-',
  570. ns.peer_isp ? 'p' : '-',
  571. ns.user_isp ? 'u' : '-'
  572. );
  573. }
  574. void print_st_err(struct drbd_conf *mdev, union drbd_state os,
  575. union drbd_state ns, enum drbd_state_rv err)
  576. {
  577. if (err == SS_IN_TRANSIENT_STATE)
  578. return;
  579. dev_err(DEV, "State change failed: %s\n", drbd_set_st_err_str(err));
  580. print_st(mdev, " state", os);
  581. print_st(mdev, "wanted", ns);
  582. }
  583. /**
  584. * is_valid_state() - Returns an SS_ error code if ns is not valid
  585. * @mdev: DRBD device.
  586. * @ns: State to consider.
  587. */
  588. static enum drbd_state_rv
  589. is_valid_state(struct drbd_conf *mdev, union drbd_state ns)
  590. {
  591. /* See drbd_state_sw_errors in drbd_strings.c */
  592. enum drbd_fencing_p fp;
  593. enum drbd_state_rv rv = SS_SUCCESS;
  594. fp = FP_DONT_CARE;
  595. if (get_ldev(mdev)) {
  596. fp = mdev->ldev->dc.fencing;
  597. put_ldev(mdev);
  598. }
  599. if (get_net_conf(mdev)) {
  600. if (!mdev->net_conf->two_primaries &&
  601. ns.role == R_PRIMARY && ns.peer == R_PRIMARY)
  602. rv = SS_TWO_PRIMARIES;
  603. put_net_conf(mdev);
  604. }
  605. if (rv <= 0)
  606. /* already found a reason to abort */;
  607. else if (ns.role == R_SECONDARY && mdev->open_cnt)
  608. rv = SS_DEVICE_IN_USE;
  609. else if (ns.role == R_PRIMARY && ns.conn < C_CONNECTED && ns.disk < D_UP_TO_DATE)
  610. rv = SS_NO_UP_TO_DATE_DISK;
  611. else if (fp >= FP_RESOURCE &&
  612. ns.role == R_PRIMARY && ns.conn < C_CONNECTED && ns.pdsk >= D_UNKNOWN)
  613. rv = SS_PRIMARY_NOP;
  614. else if (ns.role == R_PRIMARY && ns.disk <= D_INCONSISTENT && ns.pdsk <= D_INCONSISTENT)
  615. rv = SS_NO_UP_TO_DATE_DISK;
  616. else if (ns.conn > C_CONNECTED && ns.disk < D_INCONSISTENT)
  617. rv = SS_NO_LOCAL_DISK;
  618. else if (ns.conn > C_CONNECTED && ns.pdsk < D_INCONSISTENT)
  619. rv = SS_NO_REMOTE_DISK;
  620. else if (ns.conn > C_CONNECTED && ns.disk < D_UP_TO_DATE && ns.pdsk < D_UP_TO_DATE)
  621. rv = SS_NO_UP_TO_DATE_DISK;
  622. else if ((ns.conn == C_CONNECTED ||
  623. ns.conn == C_WF_BITMAP_S ||
  624. ns.conn == C_SYNC_SOURCE ||
  625. ns.conn == C_PAUSED_SYNC_S) &&
  626. ns.disk == D_OUTDATED)
  627. rv = SS_CONNECTED_OUTDATES;
  628. else if ((ns.conn == C_VERIFY_S || ns.conn == C_VERIFY_T) &&
  629. (mdev->sync_conf.verify_alg[0] == 0))
  630. rv = SS_NO_VERIFY_ALG;
  631. else if ((ns.conn == C_VERIFY_S || ns.conn == C_VERIFY_T) &&
  632. mdev->agreed_pro_version < 88)
  633. rv = SS_NOT_SUPPORTED;
  634. else if (ns.conn >= C_CONNECTED && ns.pdsk == D_UNKNOWN)
  635. rv = SS_CONNECTED_OUTDATES;
  636. return rv;
  637. }
  638. /**
  639. * is_valid_state_transition() - Returns an SS_ error code if the state transition is not possible
  640. * @mdev: DRBD device.
  641. * @ns: new state.
  642. * @os: old state.
  643. */
  644. static enum drbd_state_rv
  645. is_valid_state_transition(struct drbd_conf *mdev, union drbd_state ns,
  646. union drbd_state os)
  647. {
  648. enum drbd_state_rv rv = SS_SUCCESS;
  649. if ((ns.conn == C_STARTING_SYNC_T || ns.conn == C_STARTING_SYNC_S) &&
  650. os.conn > C_CONNECTED)
  651. rv = SS_RESYNC_RUNNING;
  652. if (ns.conn == C_DISCONNECTING && os.conn == C_STANDALONE)
  653. rv = SS_ALREADY_STANDALONE;
  654. if (ns.disk > D_ATTACHING && os.disk == D_DISKLESS)
  655. rv = SS_IS_DISKLESS;
  656. if (ns.conn == C_WF_CONNECTION && os.conn < C_UNCONNECTED)
  657. rv = SS_NO_NET_CONFIG;
  658. if (ns.disk == D_OUTDATED && os.disk < D_OUTDATED && os.disk != D_ATTACHING)
  659. rv = SS_LOWER_THAN_OUTDATED;
  660. if (ns.conn == C_DISCONNECTING && os.conn == C_UNCONNECTED)
  661. rv = SS_IN_TRANSIENT_STATE;
  662. if (ns.conn == os.conn && ns.conn == C_WF_REPORT_PARAMS)
  663. rv = SS_IN_TRANSIENT_STATE;
  664. if ((ns.conn == C_VERIFY_S || ns.conn == C_VERIFY_T) && os.conn < C_CONNECTED)
  665. rv = SS_NEED_CONNECTION;
  666. if ((ns.conn == C_VERIFY_S || ns.conn == C_VERIFY_T) &&
  667. ns.conn != os.conn && os.conn > C_CONNECTED)
  668. rv = SS_RESYNC_RUNNING;
  669. if ((ns.conn == C_STARTING_SYNC_S || ns.conn == C_STARTING_SYNC_T) &&
  670. os.conn < C_CONNECTED)
  671. rv = SS_NEED_CONNECTION;
  672. if ((ns.conn == C_SYNC_TARGET || ns.conn == C_SYNC_SOURCE)
  673. && os.conn < C_WF_REPORT_PARAMS)
  674. rv = SS_NEED_CONNECTION; /* No NetworkFailure -> SyncTarget etc... */
  675. return rv;
  676. }
  677. /**
  678. * sanitize_state() - Resolves implicitly necessary additional changes to a state transition
  679. * @mdev: DRBD device.
  680. * @os: old state.
  681. * @ns: new state.
  682. * @warn_sync_abort:
  683. *
  684. * When we loose connection, we have to set the state of the peers disk (pdsk)
  685. * to D_UNKNOWN. This rule and many more along those lines are in this function.
  686. */
  687. static union drbd_state sanitize_state(struct drbd_conf *mdev, union drbd_state os,
  688. union drbd_state ns, const char **warn_sync_abort)
  689. {
  690. enum drbd_fencing_p fp;
  691. enum drbd_disk_state disk_min, disk_max, pdsk_min, pdsk_max;
  692. fp = FP_DONT_CARE;
  693. if (get_ldev(mdev)) {
  694. fp = mdev->ldev->dc.fencing;
  695. put_ldev(mdev);
  696. }
  697. /* Disallow Network errors to configure a device's network part */
  698. if ((ns.conn >= C_TIMEOUT && ns.conn <= C_TEAR_DOWN) &&
  699. os.conn <= C_DISCONNECTING)
  700. ns.conn = os.conn;
  701. /* After a network error (+C_TEAR_DOWN) only C_UNCONNECTED or C_DISCONNECTING can follow.
  702. * If you try to go into some Sync* state, that shall fail (elsewhere). */
  703. if (os.conn >= C_TIMEOUT && os.conn <= C_TEAR_DOWN &&
  704. ns.conn != C_UNCONNECTED && ns.conn != C_DISCONNECTING && ns.conn <= C_TEAR_DOWN)
  705. ns.conn = os.conn;
  706. /* we cannot fail (again) if we already detached */
  707. if (ns.disk == D_FAILED && os.disk == D_DISKLESS)
  708. ns.disk = D_DISKLESS;
  709. /* if we are only D_ATTACHING yet,
  710. * we can (and should) go directly to D_DISKLESS. */
  711. if (ns.disk == D_FAILED && os.disk == D_ATTACHING)
  712. ns.disk = D_DISKLESS;
  713. /* After C_DISCONNECTING only C_STANDALONE may follow */
  714. if (os.conn == C_DISCONNECTING && ns.conn != C_STANDALONE)
  715. ns.conn = os.conn;
  716. if (ns.conn < C_CONNECTED) {
  717. ns.peer_isp = 0;
  718. ns.peer = R_UNKNOWN;
  719. if (ns.pdsk > D_UNKNOWN || ns.pdsk < D_INCONSISTENT)
  720. ns.pdsk = D_UNKNOWN;
  721. }
  722. /* Clear the aftr_isp when becoming unconfigured */
  723. if (ns.conn == C_STANDALONE && ns.disk == D_DISKLESS && ns.role == R_SECONDARY)
  724. ns.aftr_isp = 0;
  725. /* Abort resync if a disk fails/detaches */
  726. if (os.conn > C_CONNECTED && ns.conn > C_CONNECTED &&
  727. (ns.disk <= D_FAILED || ns.pdsk <= D_FAILED)) {
  728. if (warn_sync_abort)
  729. *warn_sync_abort =
  730. os.conn == C_VERIFY_S || os.conn == C_VERIFY_T ?
  731. "Online-verify" : "Resync";
  732. ns.conn = C_CONNECTED;
  733. }
  734. /* Connection breaks down before we finished "Negotiating" */
  735. if (ns.conn < C_CONNECTED && ns.disk == D_NEGOTIATING &&
  736. get_ldev_if_state(mdev, D_NEGOTIATING)) {
  737. if (mdev->ed_uuid == mdev->ldev->md.uuid[UI_CURRENT]) {
  738. ns.disk = mdev->new_state_tmp.disk;
  739. ns.pdsk = mdev->new_state_tmp.pdsk;
  740. } else {
  741. dev_alert(DEV, "Connection lost while negotiating, no data!\n");
  742. ns.disk = D_DISKLESS;
  743. ns.pdsk = D_UNKNOWN;
  744. }
  745. put_ldev(mdev);
  746. }
  747. /* D_CONSISTENT and D_OUTDATED vanish when we get connected */
  748. if (ns.conn >= C_CONNECTED && ns.conn < C_AHEAD) {
  749. if (ns.disk == D_CONSISTENT || ns.disk == D_OUTDATED)
  750. ns.disk = D_UP_TO_DATE;
  751. if (ns.pdsk == D_CONSISTENT || ns.pdsk == D_OUTDATED)
  752. ns.pdsk = D_UP_TO_DATE;
  753. }
  754. /* Implications of the connection stat on the disk states */
  755. disk_min = D_DISKLESS;
  756. disk_max = D_UP_TO_DATE;
  757. pdsk_min = D_INCONSISTENT;
  758. pdsk_max = D_UNKNOWN;
  759. switch ((enum drbd_conns)ns.conn) {
  760. case C_WF_BITMAP_T:
  761. case C_PAUSED_SYNC_T:
  762. case C_STARTING_SYNC_T:
  763. case C_WF_SYNC_UUID:
  764. case C_BEHIND:
  765. disk_min = D_INCONSISTENT;
  766. disk_max = D_OUTDATED;
  767. pdsk_min = D_UP_TO_DATE;
  768. pdsk_max = D_UP_TO_DATE;
  769. break;
  770. case C_VERIFY_S:
  771. case C_VERIFY_T:
  772. disk_min = D_UP_TO_DATE;
  773. disk_max = D_UP_TO_DATE;
  774. pdsk_min = D_UP_TO_DATE;
  775. pdsk_max = D_UP_TO_DATE;
  776. break;
  777. case C_CONNECTED:
  778. disk_min = D_DISKLESS;
  779. disk_max = D_UP_TO_DATE;
  780. pdsk_min = D_DISKLESS;
  781. pdsk_max = D_UP_TO_DATE;
  782. break;
  783. case C_WF_BITMAP_S:
  784. case C_PAUSED_SYNC_S:
  785. case C_STARTING_SYNC_S:
  786. case C_AHEAD:
  787. disk_min = D_UP_TO_DATE;
  788. disk_max = D_UP_TO_DATE;
  789. pdsk_min = D_INCONSISTENT;
  790. pdsk_max = D_CONSISTENT; /* D_OUTDATED would be nice. But explicit outdate necessary*/
  791. break;
  792. case C_SYNC_TARGET:
  793. disk_min = D_INCONSISTENT;
  794. disk_max = D_INCONSISTENT;
  795. pdsk_min = D_UP_TO_DATE;
  796. pdsk_max = D_UP_TO_DATE;
  797. break;
  798. case C_SYNC_SOURCE:
  799. disk_min = D_UP_TO_DATE;
  800. disk_max = D_UP_TO_DATE;
  801. pdsk_min = D_INCONSISTENT;
  802. pdsk_max = D_INCONSISTENT;
  803. break;
  804. case C_STANDALONE:
  805. case C_DISCONNECTING:
  806. case C_UNCONNECTED:
  807. case C_TIMEOUT:
  808. case C_BROKEN_PIPE:
  809. case C_NETWORK_FAILURE:
  810. case C_PROTOCOL_ERROR:
  811. case C_TEAR_DOWN:
  812. case C_WF_CONNECTION:
  813. case C_WF_REPORT_PARAMS:
  814. case C_MASK:
  815. break;
  816. }
  817. if (ns.disk > disk_max)
  818. ns.disk = disk_max;
  819. if (ns.disk < disk_min) {
  820. dev_warn(DEV, "Implicitly set disk from %s to %s\n",
  821. drbd_disk_str(ns.disk), drbd_disk_str(disk_min));
  822. ns.disk = disk_min;
  823. }
  824. if (ns.pdsk > pdsk_max)
  825. ns.pdsk = pdsk_max;
  826. if (ns.pdsk < pdsk_min) {
  827. dev_warn(DEV, "Implicitly set pdsk from %s to %s\n",
  828. drbd_disk_str(ns.pdsk), drbd_disk_str(pdsk_min));
  829. ns.pdsk = pdsk_min;
  830. }
  831. if (fp == FP_STONITH &&
  832. (ns.role == R_PRIMARY && ns.conn < C_CONNECTED && ns.pdsk > D_OUTDATED) &&
  833. !(os.role == R_PRIMARY && os.conn < C_CONNECTED && os.pdsk > D_OUTDATED))
  834. ns.susp_fen = 1; /* Suspend IO while fence-peer handler runs (peer lost) */
  835. if (mdev->sync_conf.on_no_data == OND_SUSPEND_IO &&
  836. (ns.role == R_PRIMARY && ns.disk < D_UP_TO_DATE && ns.pdsk < D_UP_TO_DATE) &&
  837. !(os.role == R_PRIMARY && os.disk < D_UP_TO_DATE && os.pdsk < D_UP_TO_DATE))
  838. ns.susp_nod = 1; /* Suspend IO while no data available (no accessible data available) */
  839. if (ns.aftr_isp || ns.peer_isp || ns.user_isp) {
  840. if (ns.conn == C_SYNC_SOURCE)
  841. ns.conn = C_PAUSED_SYNC_S;
  842. if (ns.conn == C_SYNC_TARGET)
  843. ns.conn = C_PAUSED_SYNC_T;
  844. } else {
  845. if (ns.conn == C_PAUSED_SYNC_S)
  846. ns.conn = C_SYNC_SOURCE;
  847. if (ns.conn == C_PAUSED_SYNC_T)
  848. ns.conn = C_SYNC_TARGET;
  849. }
  850. return ns;
  851. }
  852. /* helper for __drbd_set_state */
  853. static void set_ov_position(struct drbd_conf *mdev, enum drbd_conns cs)
  854. {
  855. if (mdev->agreed_pro_version < 90)
  856. mdev->ov_start_sector = 0;
  857. mdev->rs_total = drbd_bm_bits(mdev);
  858. mdev->ov_position = 0;
  859. if (cs == C_VERIFY_T) {
  860. /* starting online verify from an arbitrary position
  861. * does not fit well into the existing protocol.
  862. * on C_VERIFY_T, we initialize ov_left and friends
  863. * implicitly in receive_DataRequest once the
  864. * first P_OV_REQUEST is received */
  865. mdev->ov_start_sector = ~(sector_t)0;
  866. } else {
  867. unsigned long bit = BM_SECT_TO_BIT(mdev->ov_start_sector);
  868. if (bit >= mdev->rs_total) {
  869. mdev->ov_start_sector =
  870. BM_BIT_TO_SECT(mdev->rs_total - 1);
  871. mdev->rs_total = 1;
  872. } else
  873. mdev->rs_total -= bit;
  874. mdev->ov_position = mdev->ov_start_sector;
  875. }
  876. mdev->ov_left = mdev->rs_total;
  877. }
  878. static void drbd_resume_al(struct drbd_conf *mdev)
  879. {
  880. if (test_and_clear_bit(AL_SUSPENDED, &mdev->flags))
  881. dev_info(DEV, "Resumed AL updates\n");
  882. }
  883. /**
  884. * __drbd_set_state() - Set a new DRBD state
  885. * @mdev: DRBD device.
  886. * @ns: new state.
  887. * @flags: Flags
  888. * @done: Optional completion, that will get completed after the after_state_ch() finished
  889. *
  890. * Caller needs to hold req_lock, and global_state_lock. Do not call directly.
  891. */
  892. enum drbd_state_rv
  893. __drbd_set_state(struct drbd_conf *mdev, union drbd_state ns,
  894. enum chg_state_flags flags, struct completion *done)
  895. {
  896. union drbd_state os;
  897. enum drbd_state_rv rv = SS_SUCCESS;
  898. const char *warn_sync_abort = NULL;
  899. struct after_state_chg_work *ascw;
  900. os = mdev->state;
  901. ns = sanitize_state(mdev, os, ns, &warn_sync_abort);
  902. if (ns.i == os.i)
  903. return SS_NOTHING_TO_DO;
  904. if (!(flags & CS_HARD)) {
  905. /* pre-state-change checks ; only look at ns */
  906. /* See drbd_state_sw_errors in drbd_strings.c */
  907. rv = is_valid_state(mdev, ns);
  908. if (rv < SS_SUCCESS) {
  909. /* If the old state was illegal as well, then let
  910. this happen...*/
  911. if (is_valid_state(mdev, os) == rv)
  912. rv = is_valid_state_transition(mdev, ns, os);
  913. } else
  914. rv = is_valid_state_transition(mdev, ns, os);
  915. }
  916. if (rv < SS_SUCCESS) {
  917. if (flags & CS_VERBOSE)
  918. print_st_err(mdev, os, ns, rv);
  919. return rv;
  920. }
  921. if (warn_sync_abort)
  922. dev_warn(DEV, "%s aborted.\n", warn_sync_abort);
  923. {
  924. char *pbp, pb[300];
  925. pbp = pb;
  926. *pbp = 0;
  927. if (ns.role != os.role)
  928. pbp += sprintf(pbp, "role( %s -> %s ) ",
  929. drbd_role_str(os.role),
  930. drbd_role_str(ns.role));
  931. if (ns.peer != os.peer)
  932. pbp += sprintf(pbp, "peer( %s -> %s ) ",
  933. drbd_role_str(os.peer),
  934. drbd_role_str(ns.peer));
  935. if (ns.conn != os.conn)
  936. pbp += sprintf(pbp, "conn( %s -> %s ) ",
  937. drbd_conn_str(os.conn),
  938. drbd_conn_str(ns.conn));
  939. if (ns.disk != os.disk)
  940. pbp += sprintf(pbp, "disk( %s -> %s ) ",
  941. drbd_disk_str(os.disk),
  942. drbd_disk_str(ns.disk));
  943. if (ns.pdsk != os.pdsk)
  944. pbp += sprintf(pbp, "pdsk( %s -> %s ) ",
  945. drbd_disk_str(os.pdsk),
  946. drbd_disk_str(ns.pdsk));
  947. if (is_susp(ns) != is_susp(os))
  948. pbp += sprintf(pbp, "susp( %d -> %d ) ",
  949. is_susp(os),
  950. is_susp(ns));
  951. if (ns.aftr_isp != os.aftr_isp)
  952. pbp += sprintf(pbp, "aftr_isp( %d -> %d ) ",
  953. os.aftr_isp,
  954. ns.aftr_isp);
  955. if (ns.peer_isp != os.peer_isp)
  956. pbp += sprintf(pbp, "peer_isp( %d -> %d ) ",
  957. os.peer_isp,
  958. ns.peer_isp);
  959. if (ns.user_isp != os.user_isp)
  960. pbp += sprintf(pbp, "user_isp( %d -> %d ) ",
  961. os.user_isp,
  962. ns.user_isp);
  963. dev_info(DEV, "%s\n", pb);
  964. }
  965. /* solve the race between becoming unconfigured,
  966. * worker doing the cleanup, and
  967. * admin reconfiguring us:
  968. * on (re)configure, first set CONFIG_PENDING,
  969. * then wait for a potentially exiting worker,
  970. * start the worker, and schedule one no_op.
  971. * then proceed with configuration.
  972. */
  973. if (ns.disk == D_DISKLESS &&
  974. ns.conn == C_STANDALONE &&
  975. ns.role == R_SECONDARY &&
  976. !test_and_set_bit(CONFIG_PENDING, &mdev->flags))
  977. set_bit(DEVICE_DYING, &mdev->flags);
  978. /* if we are going -> D_FAILED or D_DISKLESS, grab one extra reference
  979. * on the ldev here, to be sure the transition -> D_DISKLESS resp.
  980. * drbd_ldev_destroy() won't happen before our corresponding
  981. * after_state_ch works run, where we put_ldev again. */
  982. if ((os.disk != D_FAILED && ns.disk == D_FAILED) ||
  983. (os.disk != D_DISKLESS && ns.disk == D_DISKLESS))
  984. atomic_inc(&mdev->local_cnt);
  985. mdev->state = ns;
  986. if (os.disk == D_ATTACHING && ns.disk >= D_NEGOTIATING)
  987. drbd_print_uuids(mdev, "attached to UUIDs");
  988. wake_up(&mdev->misc_wait);
  989. wake_up(&mdev->state_wait);
  990. /* aborted verify run. log the last position */
  991. if ((os.conn == C_VERIFY_S || os.conn == C_VERIFY_T) &&
  992. ns.conn < C_CONNECTED) {
  993. mdev->ov_start_sector =
  994. BM_BIT_TO_SECT(drbd_bm_bits(mdev) - mdev->ov_left);
  995. dev_info(DEV, "Online Verify reached sector %llu\n",
  996. (unsigned long long)mdev->ov_start_sector);
  997. }
  998. if ((os.conn == C_PAUSED_SYNC_T || os.conn == C_PAUSED_SYNC_S) &&
  999. (ns.conn == C_SYNC_TARGET || ns.conn == C_SYNC_SOURCE)) {
  1000. dev_info(DEV, "Syncer continues.\n");
  1001. mdev->rs_paused += (long)jiffies
  1002. -(long)mdev->rs_mark_time[mdev->rs_last_mark];
  1003. if (ns.conn == C_SYNC_TARGET)
  1004. mod_timer(&mdev->resync_timer, jiffies);
  1005. }
  1006. if ((os.conn == C_SYNC_TARGET || os.conn == C_SYNC_SOURCE) &&
  1007. (ns.conn == C_PAUSED_SYNC_T || ns.conn == C_PAUSED_SYNC_S)) {
  1008. dev_info(DEV, "Resync suspended\n");
  1009. mdev->rs_mark_time[mdev->rs_last_mark] = jiffies;
  1010. }
  1011. if (os.conn == C_CONNECTED &&
  1012. (ns.conn == C_VERIFY_S || ns.conn == C_VERIFY_T)) {
  1013. unsigned long now = jiffies;
  1014. int i;
  1015. set_ov_position(mdev, ns.conn);
  1016. mdev->rs_start = now;
  1017. mdev->rs_last_events = 0;
  1018. mdev->rs_last_sect_ev = 0;
  1019. mdev->ov_last_oos_size = 0;
  1020. mdev->ov_last_oos_start = 0;
  1021. for (i = 0; i < DRBD_SYNC_MARKS; i++) {
  1022. mdev->rs_mark_left[i] = mdev->ov_left;
  1023. mdev->rs_mark_time[i] = now;
  1024. }
  1025. drbd_rs_controller_reset(mdev);
  1026. if (ns.conn == C_VERIFY_S) {
  1027. dev_info(DEV, "Starting Online Verify from sector %llu\n",
  1028. (unsigned long long)mdev->ov_position);
  1029. mod_timer(&mdev->resync_timer, jiffies);
  1030. }
  1031. }
  1032. if (get_ldev(mdev)) {
  1033. u32 mdf = mdev->ldev->md.flags & ~(MDF_CONSISTENT|MDF_PRIMARY_IND|
  1034. MDF_CONNECTED_IND|MDF_WAS_UP_TO_DATE|
  1035. MDF_PEER_OUT_DATED|MDF_CRASHED_PRIMARY);
  1036. if (test_bit(CRASHED_PRIMARY, &mdev->flags))
  1037. mdf |= MDF_CRASHED_PRIMARY;
  1038. if (mdev->state.role == R_PRIMARY ||
  1039. (mdev->state.pdsk < D_INCONSISTENT && mdev->state.peer == R_PRIMARY))
  1040. mdf |= MDF_PRIMARY_IND;
  1041. if (mdev->state.conn > C_WF_REPORT_PARAMS)
  1042. mdf |= MDF_CONNECTED_IND;
  1043. if (mdev->state.disk > D_INCONSISTENT)
  1044. mdf |= MDF_CONSISTENT;
  1045. if (mdev->state.disk > D_OUTDATED)
  1046. mdf |= MDF_WAS_UP_TO_DATE;
  1047. if (mdev->state.pdsk <= D_OUTDATED && mdev->state.pdsk >= D_INCONSISTENT)
  1048. mdf |= MDF_PEER_OUT_DATED;
  1049. if (mdf != mdev->ldev->md.flags) {
  1050. mdev->ldev->md.flags = mdf;
  1051. drbd_md_mark_dirty(mdev);
  1052. }
  1053. if (os.disk < D_CONSISTENT && ns.disk >= D_CONSISTENT)
  1054. drbd_set_ed_uuid(mdev, mdev->ldev->md.uuid[UI_CURRENT]);
  1055. put_ldev(mdev);
  1056. }
  1057. /* Peer was forced D_UP_TO_DATE & R_PRIMARY, consider to resync */
  1058. if (os.disk == D_INCONSISTENT && os.pdsk == D_INCONSISTENT &&
  1059. os.peer == R_SECONDARY && ns.peer == R_PRIMARY)
  1060. set_bit(CONSIDER_RESYNC, &mdev->flags);
  1061. /* Receiver should clean up itself */
  1062. if (os.conn != C_DISCONNECTING && ns.conn == C_DISCONNECTING)
  1063. drbd_thread_stop_nowait(&mdev->receiver);
  1064. /* Now the receiver finished cleaning up itself, it should die */
  1065. if (os.conn != C_STANDALONE && ns.conn == C_STANDALONE)
  1066. drbd_thread_stop_nowait(&mdev->receiver);
  1067. /* Upon network failure, we need to restart the receiver. */
  1068. if (os.conn > C_TEAR_DOWN &&
  1069. ns.conn <= C_TEAR_DOWN && ns.conn >= C_TIMEOUT)
  1070. drbd_thread_restart_nowait(&mdev->receiver);
  1071. /* Resume AL writing if we get a connection */
  1072. if (os.conn < C_CONNECTED && ns.conn >= C_CONNECTED)
  1073. drbd_resume_al(mdev);
  1074. ascw = kmalloc(sizeof(*ascw), GFP_ATOMIC);
  1075. if (ascw) {
  1076. ascw->os = os;
  1077. ascw->ns = ns;
  1078. ascw->flags = flags;
  1079. ascw->w.cb = w_after_state_ch;
  1080. ascw->done = done;
  1081. drbd_queue_work(&mdev->data.work, &ascw->w);
  1082. } else {
  1083. dev_warn(DEV, "Could not kmalloc an ascw\n");
  1084. }
  1085. return rv;
  1086. }
  1087. static int w_after_state_ch(struct drbd_conf *mdev, struct drbd_work *w, int unused)
  1088. {
  1089. struct after_state_chg_work *ascw =
  1090. container_of(w, struct after_state_chg_work, w);
  1091. after_state_ch(mdev, ascw->os, ascw->ns, ascw->flags);
  1092. if (ascw->flags & CS_WAIT_COMPLETE) {
  1093. D_ASSERT(ascw->done != NULL);
  1094. complete(ascw->done);
  1095. }
  1096. kfree(ascw);
  1097. return 1;
  1098. }
  1099. static void abw_start_sync(struct drbd_conf *mdev, int rv)
  1100. {
  1101. if (rv) {
  1102. dev_err(DEV, "Writing the bitmap failed not starting resync.\n");
  1103. _drbd_request_state(mdev, NS(conn, C_CONNECTED), CS_VERBOSE);
  1104. return;
  1105. }
  1106. switch (mdev->state.conn) {
  1107. case C_STARTING_SYNC_T:
  1108. _drbd_request_state(mdev, NS(conn, C_WF_SYNC_UUID), CS_VERBOSE);
  1109. break;
  1110. case C_STARTING_SYNC_S:
  1111. drbd_start_resync(mdev, C_SYNC_SOURCE);
  1112. break;
  1113. }
  1114. }
  1115. int drbd_bitmap_io_from_worker(struct drbd_conf *mdev,
  1116. int (*io_fn)(struct drbd_conf *),
  1117. char *why, enum bm_flag flags)
  1118. {
  1119. int rv;
  1120. D_ASSERT(current == mdev->worker.task);
  1121. /* open coded non-blocking drbd_suspend_io(mdev); */
  1122. set_bit(SUSPEND_IO, &mdev->flags);
  1123. drbd_bm_lock(mdev, why, flags);
  1124. rv = io_fn(mdev);
  1125. drbd_bm_unlock(mdev);
  1126. drbd_resume_io(mdev);
  1127. return rv;
  1128. }
  1129. /**
  1130. * after_state_ch() - Perform after state change actions that may sleep
  1131. * @mdev: DRBD device.
  1132. * @os: old state.
  1133. * @ns: new state.
  1134. * @flags: Flags
  1135. */
  1136. static void after_state_ch(struct drbd_conf *mdev, union drbd_state os,
  1137. union drbd_state ns, enum chg_state_flags flags)
  1138. {
  1139. enum drbd_fencing_p fp;
  1140. enum drbd_req_event what = nothing;
  1141. union drbd_state nsm = (union drbd_state){ .i = -1 };
  1142. if (os.conn != C_CONNECTED && ns.conn == C_CONNECTED) {
  1143. clear_bit(CRASHED_PRIMARY, &mdev->flags);
  1144. if (mdev->p_uuid)
  1145. mdev->p_uuid[UI_FLAGS] &= ~((u64)2);
  1146. }
  1147. fp = FP_DONT_CARE;
  1148. if (get_ldev(mdev)) {
  1149. fp = mdev->ldev->dc.fencing;
  1150. put_ldev(mdev);
  1151. }
  1152. /* Inform userspace about the change... */
  1153. drbd_bcast_state(mdev, ns);
  1154. if (!(os.role == R_PRIMARY && os.disk < D_UP_TO_DATE && os.pdsk < D_UP_TO_DATE) &&
  1155. (ns.role == R_PRIMARY && ns.disk < D_UP_TO_DATE && ns.pdsk < D_UP_TO_DATE))
  1156. drbd_khelper(mdev, "pri-on-incon-degr");
  1157. /* Here we have the actions that are performed after a
  1158. state change. This function might sleep */
  1159. nsm.i = -1;
  1160. if (ns.susp_nod) {
  1161. if (os.conn < C_CONNECTED && ns.conn >= C_CONNECTED)
  1162. what = resend;
  1163. if (os.disk == D_ATTACHING && ns.disk > D_ATTACHING)
  1164. what = restart_frozen_disk_io;
  1165. if (what != nothing)
  1166. nsm.susp_nod = 0;
  1167. }
  1168. if (ns.susp_fen) {
  1169. /* case1: The outdate peer handler is successful: */
  1170. if (os.pdsk > D_OUTDATED && ns.pdsk <= D_OUTDATED) {
  1171. tl_clear(mdev);
  1172. if (test_bit(NEW_CUR_UUID, &mdev->flags)) {
  1173. drbd_uuid_new_current(mdev);
  1174. clear_bit(NEW_CUR_UUID, &mdev->flags);
  1175. }
  1176. spin_lock_irq(&mdev->req_lock);
  1177. _drbd_set_state(_NS(mdev, susp_fen, 0), CS_VERBOSE, NULL);
  1178. spin_unlock_irq(&mdev->req_lock);
  1179. }
  1180. /* case2: The connection was established again: */
  1181. if (os.conn < C_CONNECTED && ns.conn >= C_CONNECTED) {
  1182. clear_bit(NEW_CUR_UUID, &mdev->flags);
  1183. what = resend;
  1184. nsm.susp_fen = 0;
  1185. }
  1186. }
  1187. if (what != nothing) {
  1188. spin_lock_irq(&mdev->req_lock);
  1189. _tl_restart(mdev, what);
  1190. nsm.i &= mdev->state.i;
  1191. _drbd_set_state(mdev, nsm, CS_VERBOSE, NULL);
  1192. spin_unlock_irq(&mdev->req_lock);
  1193. }
  1194. /* Became sync source. With protocol >= 96, we still need to send out
  1195. * the sync uuid now. Need to do that before any drbd_send_state, or
  1196. * the other side may go "paused sync" before receiving the sync uuids,
  1197. * which is unexpected. */
  1198. if ((os.conn != C_SYNC_SOURCE && os.conn != C_PAUSED_SYNC_S) &&
  1199. (ns.conn == C_SYNC_SOURCE || ns.conn == C_PAUSED_SYNC_S) &&
  1200. mdev->agreed_pro_version >= 96 && get_ldev(mdev)) {
  1201. drbd_gen_and_send_sync_uuid(mdev);
  1202. put_ldev(mdev);
  1203. }
  1204. /* Do not change the order of the if above and the two below... */
  1205. if (os.pdsk == D_DISKLESS && ns.pdsk > D_DISKLESS) { /* attach on the peer */
  1206. drbd_send_uuids(mdev);
  1207. drbd_send_state(mdev);
  1208. }
  1209. /* No point in queuing send_bitmap if we don't have a connection
  1210. * anymore, so check also the _current_ state, not only the new state
  1211. * at the time this work was queued. */
  1212. if (os.conn != C_WF_BITMAP_S && ns.conn == C_WF_BITMAP_S &&
  1213. mdev->state.conn == C_WF_BITMAP_S)
  1214. drbd_queue_bitmap_io(mdev, &drbd_send_bitmap, NULL,
  1215. "send_bitmap (WFBitMapS)",
  1216. BM_LOCKED_TEST_ALLOWED);
  1217. /* Lost contact to peer's copy of the data */
  1218. if ((os.pdsk >= D_INCONSISTENT &&
  1219. os.pdsk != D_UNKNOWN &&
  1220. os.pdsk != D_OUTDATED)
  1221. && (ns.pdsk < D_INCONSISTENT ||
  1222. ns.pdsk == D_UNKNOWN ||
  1223. ns.pdsk == D_OUTDATED)) {
  1224. if (get_ldev(mdev)) {
  1225. if ((ns.role == R_PRIMARY || ns.peer == R_PRIMARY) &&
  1226. mdev->ldev->md.uuid[UI_BITMAP] == 0 && ns.disk >= D_UP_TO_DATE) {
  1227. if (is_susp(mdev->state)) {
  1228. set_bit(NEW_CUR_UUID, &mdev->flags);
  1229. } else {
  1230. drbd_uuid_new_current(mdev);
  1231. drbd_send_uuids(mdev);
  1232. }
  1233. }
  1234. put_ldev(mdev);
  1235. }
  1236. }
  1237. if (ns.pdsk < D_INCONSISTENT && get_ldev(mdev)) {
  1238. if (ns.peer == R_PRIMARY && mdev->ldev->md.uuid[UI_BITMAP] == 0) {
  1239. drbd_uuid_new_current(mdev);
  1240. drbd_send_uuids(mdev);
  1241. }
  1242. /* D_DISKLESS Peer becomes secondary */
  1243. if (os.peer == R_PRIMARY && ns.peer == R_SECONDARY)
  1244. /* We may still be Primary ourselves.
  1245. * No harm done if the bitmap still changes,
  1246. * redirtied pages will follow later. */
  1247. drbd_bitmap_io_from_worker(mdev, &drbd_bm_write,
  1248. "demote diskless peer", BM_LOCKED_SET_ALLOWED);
  1249. put_ldev(mdev);
  1250. }
  1251. /* Write out all changed bits on demote.
  1252. * Though, no need to da that just yet
  1253. * if there is a resync going on still */
  1254. if (os.role == R_PRIMARY && ns.role == R_SECONDARY &&
  1255. mdev->state.conn <= C_CONNECTED && get_ldev(mdev)) {
  1256. /* No changes to the bitmap expected this time, so assert that,
  1257. * even though no harm was done if it did change. */
  1258. drbd_bitmap_io_from_worker(mdev, &drbd_bm_write,
  1259. "demote", BM_LOCKED_TEST_ALLOWED);
  1260. put_ldev(mdev);
  1261. }
  1262. /* Last part of the attaching process ... */
  1263. if (ns.conn >= C_CONNECTED &&
  1264. os.disk == D_ATTACHING && ns.disk == D_NEGOTIATING) {
  1265. drbd_send_sizes(mdev, 0, 0); /* to start sync... */
  1266. drbd_send_uuids(mdev);
  1267. drbd_send_state(mdev);
  1268. }
  1269. /* We want to pause/continue resync, tell peer. */
  1270. if (ns.conn >= C_CONNECTED &&
  1271. ((os.aftr_isp != ns.aftr_isp) ||
  1272. (os.user_isp != ns.user_isp)))
  1273. drbd_send_state(mdev);
  1274. /* In case one of the isp bits got set, suspend other devices. */
  1275. if ((!os.aftr_isp && !os.peer_isp && !os.user_isp) &&
  1276. (ns.aftr_isp || ns.peer_isp || ns.user_isp))
  1277. suspend_other_sg(mdev);
  1278. /* Make sure the peer gets informed about eventual state
  1279. changes (ISP bits) while we were in WFReportParams. */
  1280. if (os.conn == C_WF_REPORT_PARAMS && ns.conn >= C_CONNECTED)
  1281. drbd_send_state(mdev);
  1282. if (os.conn != C_AHEAD && ns.conn == C_AHEAD)
  1283. drbd_send_state(mdev);
  1284. /* We are in the progress to start a full sync... */
  1285. if ((os.conn != C_STARTING_SYNC_T && ns.conn == C_STARTING_SYNC_T) ||
  1286. (os.conn != C_STARTING_SYNC_S && ns.conn == C_STARTING_SYNC_S))
  1287. /* no other bitmap changes expected during this phase */
  1288. drbd_queue_bitmap_io(mdev,
  1289. &drbd_bmio_set_n_write, &abw_start_sync,
  1290. "set_n_write from StartingSync", BM_LOCKED_TEST_ALLOWED);
  1291. /* We are invalidating our self... */
  1292. if (os.conn < C_CONNECTED && ns.conn < C_CONNECTED &&
  1293. os.disk > D_INCONSISTENT && ns.disk == D_INCONSISTENT)
  1294. /* other bitmap operation expected during this phase */
  1295. drbd_queue_bitmap_io(mdev, &drbd_bmio_set_n_write, NULL,
  1296. "set_n_write from invalidate", BM_LOCKED_MASK);
  1297. /* first half of local IO error, failure to attach,
  1298. * or administrative detach */
  1299. if (os.disk != D_FAILED && ns.disk == D_FAILED) {
  1300. enum drbd_io_error_p eh;
  1301. int was_io_error;
  1302. /* corresponding get_ldev was in __drbd_set_state, to serialize
  1303. * our cleanup here with the transition to D_DISKLESS,
  1304. * so it is safe to dreference ldev here. */
  1305. eh = mdev->ldev->dc.on_io_error;
  1306. was_io_error = test_and_clear_bit(WAS_IO_ERROR, &mdev->flags);
  1307. /* current state still has to be D_FAILED,
  1308. * there is only one way out: to D_DISKLESS,
  1309. * and that may only happen after our put_ldev below. */
  1310. if (mdev->state.disk != D_FAILED)
  1311. dev_err(DEV,
  1312. "ASSERT FAILED: disk is %s during detach\n",
  1313. drbd_disk_str(mdev->state.disk));
  1314. if (drbd_send_state(mdev))
  1315. dev_warn(DEV, "Notified peer that I am detaching my disk\n");
  1316. else
  1317. dev_err(DEV, "Sending state for detaching disk failed\n");
  1318. drbd_rs_cancel_all(mdev);
  1319. /* In case we want to get something to stable storage still,
  1320. * this may be the last chance.
  1321. * Following put_ldev may transition to D_DISKLESS. */
  1322. drbd_md_sync(mdev);
  1323. put_ldev(mdev);
  1324. if (was_io_error && eh == EP_CALL_HELPER)
  1325. drbd_khelper(mdev, "local-io-error");
  1326. }
  1327. /* second half of local IO error, failure to attach,
  1328. * or administrative detach,
  1329. * after local_cnt references have reached zero again */
  1330. if (os.disk != D_DISKLESS && ns.disk == D_DISKLESS) {
  1331. /* We must still be diskless,
  1332. * re-attach has to be serialized with this! */
  1333. if (mdev->state.disk != D_DISKLESS)
  1334. dev_err(DEV,
  1335. "ASSERT FAILED: disk is %s while going diskless\n",
  1336. drbd_disk_str(mdev->state.disk));
  1337. mdev->rs_total = 0;
  1338. mdev->rs_failed = 0;
  1339. atomic_set(&mdev->rs_pending_cnt, 0);
  1340. if (drbd_send_state(mdev))
  1341. dev_warn(DEV, "Notified peer that I'm now diskless.\n");
  1342. /* corresponding get_ldev in __drbd_set_state
  1343. * this may finally trigger drbd_ldev_destroy. */
  1344. put_ldev(mdev);
  1345. }
  1346. /* Notify peer that I had a local IO error, and did not detached.. */
  1347. if (os.disk == D_UP_TO_DATE && ns.disk == D_INCONSISTENT)
  1348. drbd_send_state(mdev);
  1349. /* Disks got bigger while they were detached */
  1350. if (ns.disk > D_NEGOTIATING && ns.pdsk > D_NEGOTIATING &&
  1351. test_and_clear_bit(RESYNC_AFTER_NEG, &mdev->flags)) {
  1352. if (ns.conn == C_CONNECTED)
  1353. resync_after_online_grow(mdev);
  1354. }
  1355. /* A resync finished or aborted, wake paused devices... */
  1356. if ((os.conn > C_CONNECTED && ns.conn <= C_CONNECTED) ||
  1357. (os.peer_isp && !ns.peer_isp) ||
  1358. (os.user_isp && !ns.user_isp))
  1359. resume_next_sg(mdev);
  1360. /* sync target done with resync. Explicitly notify peer, even though
  1361. * it should (at least for non-empty resyncs) already know itself. */
  1362. if (os.disk < D_UP_TO_DATE && os.conn >= C_SYNC_SOURCE && ns.conn == C_CONNECTED)
  1363. drbd_send_state(mdev);
  1364. /* This triggers bitmap writeout of potentially still unwritten pages
  1365. * if the resync finished cleanly, or aborted because of peer disk
  1366. * failure, or because of connection loss.
  1367. * For resync aborted because of local disk failure, we cannot do
  1368. * any bitmap writeout anymore.
  1369. * No harm done if some bits change during this phase.
  1370. */
  1371. if (os.conn > C_CONNECTED && ns.conn <= C_CONNECTED && get_ldev(mdev)) {
  1372. drbd_queue_bitmap_io(mdev, &drbd_bm_write, NULL,
  1373. "write from resync_finished", BM_LOCKED_SET_ALLOWED);
  1374. put_ldev(mdev);
  1375. }
  1376. /* free tl_hash if we Got thawed and are C_STANDALONE */
  1377. if (ns.conn == C_STANDALONE && !is_susp(ns) && mdev->tl_hash)
  1378. drbd_free_tl_hash(mdev);
  1379. /* Upon network connection, we need to start the receiver */
  1380. if (os.conn == C_STANDALONE && ns.conn == C_UNCONNECTED)
  1381. drbd_thread_start(&mdev->receiver);
  1382. /* Terminate worker thread if we are unconfigured - it will be
  1383. restarted as needed... */
  1384. if (ns.disk == D_DISKLESS &&
  1385. ns.conn == C_STANDALONE &&
  1386. ns.role == R_SECONDARY) {
  1387. if (os.aftr_isp != ns.aftr_isp)
  1388. resume_next_sg(mdev);
  1389. /* set in __drbd_set_state, unless CONFIG_PENDING was set */
  1390. if (test_bit(DEVICE_DYING, &mdev->flags))
  1391. drbd_thread_stop_nowait(&mdev->worker);
  1392. }
  1393. drbd_md_sync(mdev);
  1394. }
  1395. static int drbd_thread_setup(void *arg)
  1396. {
  1397. struct drbd_thread *thi = (struct drbd_thread *) arg;
  1398. struct drbd_conf *mdev = thi->mdev;
  1399. unsigned long flags;
  1400. int retval;
  1401. restart:
  1402. retval = thi->function(thi);
  1403. spin_lock_irqsave(&thi->t_lock, flags);
  1404. /* if the receiver has been "Exiting", the last thing it did
  1405. * was set the conn state to "StandAlone",
  1406. * if now a re-connect request comes in, conn state goes C_UNCONNECTED,
  1407. * and receiver thread will be "started".
  1408. * drbd_thread_start needs to set "Restarting" in that case.
  1409. * t_state check and assignment needs to be within the same spinlock,
  1410. * so either thread_start sees Exiting, and can remap to Restarting,
  1411. * or thread_start see None, and can proceed as normal.
  1412. */
  1413. if (thi->t_state == Restarting) {
  1414. dev_info(DEV, "Restarting %s\n", current->comm);
  1415. thi->t_state = Running;
  1416. spin_unlock_irqrestore(&thi->t_lock, flags);
  1417. goto restart;
  1418. }
  1419. thi->task = NULL;
  1420. thi->t_state = None;
  1421. smp_mb();
  1422. complete(&thi->stop);
  1423. spin_unlock_irqrestore(&thi->t_lock, flags);
  1424. dev_info(DEV, "Terminating %s\n", current->comm);
  1425. /* Release mod reference taken when thread was started */
  1426. module_put(THIS_MODULE);
  1427. return retval;
  1428. }
  1429. static void drbd_thread_init(struct drbd_conf *mdev, struct drbd_thread *thi,
  1430. int (*func) (struct drbd_thread *))
  1431. {
  1432. spin_lock_init(&thi->t_lock);
  1433. thi->task = NULL;
  1434. thi->t_state = None;
  1435. thi->function = func;
  1436. thi->mdev = mdev;
  1437. }
  1438. int drbd_thread_start(struct drbd_thread *thi)
  1439. {
  1440. struct drbd_conf *mdev = thi->mdev;
  1441. struct task_struct *nt;
  1442. unsigned long flags;
  1443. const char *me =
  1444. thi == &mdev->receiver ? "receiver" :
  1445. thi == &mdev->asender ? "asender" :
  1446. thi == &mdev->worker ? "worker" : "NONSENSE";
  1447. /* is used from state engine doing drbd_thread_stop_nowait,
  1448. * while holding the req lock irqsave */
  1449. spin_lock_irqsave(&thi->t_lock, flags);
  1450. switch (thi->t_state) {
  1451. case None:
  1452. dev_info(DEV, "Starting %s thread (from %s [%d])\n",
  1453. me, current->comm, current->pid);
  1454. /* Get ref on module for thread - this is released when thread exits */
  1455. if (!try_module_get(THIS_MODULE)) {
  1456. dev_err(DEV, "Failed to get module reference in drbd_thread_start\n");
  1457. spin_unlock_irqrestore(&thi->t_lock, flags);
  1458. return false;
  1459. }
  1460. init_completion(&thi->stop);
  1461. D_ASSERT(thi->task == NULL);
  1462. thi->reset_cpu_mask = 1;
  1463. thi->t_state = Running;
  1464. spin_unlock_irqrestore(&thi->t_lock, flags);
  1465. flush_signals(current); /* otherw. may get -ERESTARTNOINTR */
  1466. nt = kthread_create(drbd_thread_setup, (void *) thi,
  1467. "drbd%d_%s", mdev_to_minor(mdev), me);
  1468. if (IS_ERR(nt)) {
  1469. dev_err(DEV, "Couldn't start thread\n");
  1470. module_put(THIS_MODULE);
  1471. return false;
  1472. }
  1473. spin_lock_irqsave(&thi->t_lock, flags);
  1474. thi->task = nt;
  1475. thi->t_state = Running;
  1476. spin_unlock_irqrestore(&thi->t_lock, flags);
  1477. wake_up_process(nt);
  1478. break;
  1479. case Exiting:
  1480. thi->t_state = Restarting;
  1481. dev_info(DEV, "Restarting %s thread (from %s [%d])\n",
  1482. me, current->comm, current->pid);
  1483. /* fall through */
  1484. case Running:
  1485. case Restarting:
  1486. default:
  1487. spin_unlock_irqrestore(&thi->t_lock, flags);
  1488. break;
  1489. }
  1490. return true;
  1491. }
  1492. void _drbd_thread_stop(struct drbd_thread *thi, int restart, int wait)
  1493. {
  1494. unsigned long flags;
  1495. enum drbd_thread_state ns = restart ? Restarting : Exiting;
  1496. /* may be called from state engine, holding the req lock irqsave */
  1497. spin_lock_irqsave(&thi->t_lock, flags);
  1498. if (thi->t_state == None) {
  1499. spin_unlock_irqrestore(&thi->t_lock, flags);
  1500. if (restart)
  1501. drbd_thread_start(thi);
  1502. return;
  1503. }
  1504. if (thi->t_state != ns) {
  1505. if (thi->task == NULL) {
  1506. spin_unlock_irqrestore(&thi->t_lock, flags);
  1507. return;
  1508. }
  1509. thi->t_state = ns;
  1510. smp_mb();
  1511. init_completion(&thi->stop);
  1512. if (thi->task != current)
  1513. force_sig(DRBD_SIGKILL, thi->task);
  1514. }
  1515. spin_unlock_irqrestore(&thi->t_lock, flags);
  1516. if (wait)
  1517. wait_for_completion(&thi->stop);
  1518. }
  1519. #ifdef CONFIG_SMP
  1520. /**
  1521. * drbd_calc_cpu_mask() - Generate CPU masks, spread over all CPUs
  1522. * @mdev: DRBD device.
  1523. *
  1524. * Forces all threads of a device onto the same CPU. This is beneficial for
  1525. * DRBD's performance. May be overwritten by user's configuration.
  1526. */
  1527. void drbd_calc_cpu_mask(struct drbd_conf *mdev)
  1528. {
  1529. int ord, cpu;
  1530. /* user override. */
  1531. if (cpumask_weight(mdev->cpu_mask))
  1532. return;
  1533. ord = mdev_to_minor(mdev) % cpumask_weight(cpu_online_mask);
  1534. for_each_online_cpu(cpu) {
  1535. if (ord-- == 0) {
  1536. cpumask_set_cpu(cpu, mdev->cpu_mask);
  1537. return;
  1538. }
  1539. }
  1540. /* should not be reached */
  1541. cpumask_setall(mdev->cpu_mask);
  1542. }
  1543. /**
  1544. * drbd_thread_current_set_cpu() - modifies the cpu mask of the _current_ thread
  1545. * @mdev: DRBD device.
  1546. *
  1547. * call in the "main loop" of _all_ threads, no need for any mutex, current won't die
  1548. * prematurely.
  1549. */
  1550. void drbd_thread_current_set_cpu(struct drbd_conf *mdev)
  1551. {
  1552. struct task_struct *p = current;
  1553. struct drbd_thread *thi =
  1554. p == mdev->asender.task ? &mdev->asender :
  1555. p == mdev->receiver.task ? &mdev->receiver :
  1556. p == mdev->worker.task ? &mdev->worker :
  1557. NULL;
  1558. ERR_IF(thi == NULL)
  1559. return;
  1560. if (!thi->reset_cpu_mask)
  1561. return;
  1562. thi->reset_cpu_mask = 0;
  1563. set_cpus_allowed_ptr(p, mdev->cpu_mask);
  1564. }
  1565. #endif
  1566. /* the appropriate socket mutex must be held already */
  1567. int _drbd_send_cmd(struct drbd_conf *mdev, struct socket *sock,
  1568. enum drbd_packets cmd, struct p_header80 *h,
  1569. size_t size, unsigned msg_flags)
  1570. {
  1571. int sent, ok;
  1572. ERR_IF(!h) return false;
  1573. ERR_IF(!size) return false;
  1574. h->magic = BE_DRBD_MAGIC;
  1575. h->command = cpu_to_be16(cmd);
  1576. h->length = cpu_to_be16(size-sizeof(struct p_header80));
  1577. sent = drbd_send(mdev, sock, h, size, msg_flags);
  1578. ok = (sent == size);
  1579. if (!ok && !signal_pending(current))
  1580. dev_warn(DEV, "short sent %s size=%d sent=%d\n",
  1581. cmdname(cmd), (int)size, sent);
  1582. return ok;
  1583. }
  1584. /* don't pass the socket. we may only look at it
  1585. * when we hold the appropriate socket mutex.
  1586. */
  1587. int drbd_send_cmd(struct drbd_conf *mdev, int use_data_socket,
  1588. enum drbd_packets cmd, struct p_header80 *h, size_t size)
  1589. {
  1590. int ok = 0;
  1591. struct socket *sock;
  1592. if (use_data_socket) {
  1593. mutex_lock(&mdev->data.mutex);
  1594. sock = mdev->data.socket;
  1595. } else {
  1596. mutex_lock(&mdev->meta.mutex);
  1597. sock = mdev->meta.socket;
  1598. }
  1599. /* drbd_disconnect() could have called drbd_free_sock()
  1600. * while we were waiting in down()... */
  1601. if (likely(sock != NULL))
  1602. ok = _drbd_send_cmd(mdev, sock, cmd, h, size, 0);
  1603. if (use_data_socket)
  1604. mutex_unlock(&mdev->data.mutex);
  1605. else
  1606. mutex_unlock(&mdev->meta.mutex);
  1607. return ok;
  1608. }
  1609. int drbd_send_cmd2(struct drbd_conf *mdev, enum drbd_packets cmd, char *data,
  1610. size_t size)
  1611. {
  1612. struct p_header80 h;
  1613. int ok;
  1614. h.magic = BE_DRBD_MAGIC;
  1615. h.command = cpu_to_be16(cmd);
  1616. h.length = cpu_to_be16(size);
  1617. if (!drbd_get_data_sock(mdev))
  1618. return 0;
  1619. ok = (sizeof(h) ==
  1620. drbd_send(mdev, mdev->data.socket, &h, sizeof(h), 0));
  1621. ok = ok && (size ==
  1622. drbd_send(mdev, mdev->data.socket, data, size, 0));
  1623. drbd_put_data_sock(mdev);
  1624. return ok;
  1625. }
  1626. int drbd_send_sync_param(struct drbd_conf *mdev, struct syncer_conf *sc)
  1627. {
  1628. struct p_rs_param_95 *p;
  1629. struct socket *sock;
  1630. int size, rv;
  1631. const int apv = mdev->agreed_pro_version;
  1632. size = apv <= 87 ? sizeof(struct p_rs_param)
  1633. : apv == 88 ? sizeof(struct p_rs_param)
  1634. + strlen(mdev->sync_conf.verify_alg) + 1
  1635. : apv <= 94 ? sizeof(struct p_rs_param_89)
  1636. : /* apv >= 95 */ sizeof(struct p_rs_param_95);
  1637. /* used from admin command context and receiver/worker context.
  1638. * to avoid kmalloc, grab the socket right here,
  1639. * then use the pre-allocated sbuf there */
  1640. mutex_lock(&mdev->data.mutex);
  1641. sock = mdev->data.socket;
  1642. if (likely(sock != NULL)) {
  1643. enum drbd_packets cmd = apv >= 89 ? P_SYNC_PARAM89 : P_SYNC_PARAM;
  1644. p = &mdev->data.sbuf.rs_param_95;
  1645. /* initialize verify_alg and csums_alg */
  1646. memset(p->verify_alg, 0, 2 * SHARED_SECRET_MAX);
  1647. p->rate = cpu_to_be32(sc->rate);
  1648. p->c_plan_ahead = cpu_to_be32(sc->c_plan_ahead);
  1649. p->c_delay_target = cpu_to_be32(sc->c_delay_target);
  1650. p->c_fill_target = cpu_to_be32(sc->c_fill_target);
  1651. p->c_max_rate = cpu_to_be32(sc->c_max_rate);
  1652. if (apv >= 88)
  1653. strcpy(p->verify_alg, mdev->sync_conf.verify_alg);
  1654. if (apv >= 89)
  1655. strcpy(p->csums_alg, mdev->sync_conf.csums_alg);
  1656. rv = _drbd_send_cmd(mdev, sock, cmd, &p->head, size, 0);
  1657. } else
  1658. rv = 0; /* not ok */
  1659. mutex_unlock(&mdev->data.mutex);
  1660. return rv;
  1661. }
  1662. int drbd_send_protocol(struct drbd_conf *mdev)
  1663. {
  1664. struct p_protocol *p;
  1665. int size, cf, rv;
  1666. size = sizeof(struct p_protocol);
  1667. if (mdev->agreed_pro_version >= 87)
  1668. size += strlen(mdev->net_conf->integrity_alg) + 1;
  1669. /* we must not recurse into our own queue,
  1670. * as that is blocked during handshake */
  1671. p = kmalloc(size, GFP_NOIO);
  1672. if (p == NULL)
  1673. return 0;
  1674. p->protocol = cpu_to_be32(mdev->net_conf->wire_protocol);
  1675. p->after_sb_0p = cpu_to_be32(mdev->net_conf->after_sb_0p);
  1676. p->after_sb_1p = cpu_to_be32(mdev->net_conf->after_sb_1p);
  1677. p->after_sb_2p = cpu_to_be32(mdev->net_conf->after_sb_2p);
  1678. p->two_primaries = cpu_to_be32(mdev->net_conf->two_primaries);
  1679. cf = 0;
  1680. if (mdev->net_conf->want_lose)
  1681. cf |= CF_WANT_LOSE;
  1682. if (mdev->net_conf->dry_run) {
  1683. if (mdev->agreed_pro_version >= 92)
  1684. cf |= CF_DRY_RUN;
  1685. else {
  1686. dev_err(DEV, "--dry-run is not supported by peer");
  1687. kfree(p);
  1688. return -1;
  1689. }
  1690. }
  1691. p->conn_flags = cpu_to_be32(cf);
  1692. if (mdev->agreed_pro_version >= 87)
  1693. strcpy(p->integrity_alg, mdev->net_conf->integrity_alg);
  1694. rv = drbd_send_cmd(mdev, USE_DATA_SOCKET, P_PROTOCOL,
  1695. (struct p_header80 *)p, size);
  1696. kfree(p);
  1697. return rv;
  1698. }
  1699. int _drbd_send_uuids(struct drbd_conf *mdev, u64 uuid_flags)
  1700. {
  1701. struct p_uuids p;
  1702. int i;
  1703. if (!get_ldev_if_state(mdev, D_NEGOTIATING))
  1704. return 1;
  1705. for (i = UI_CURRENT; i < UI_SIZE; i++)
  1706. p.uuid[i] = mdev->ldev ? cpu_to_be64(mdev->ldev->md.uuid[i]) : 0;
  1707. mdev->comm_bm_set = drbd_bm_total_weight(mdev);
  1708. p.uuid[UI_SIZE] = cpu_to_be64(mdev->comm_bm_set);
  1709. uuid_flags |= mdev->net_conf->want_lose ? 1 : 0;
  1710. uuid_flags |= test_bit(CRASHED_PRIMARY, &mdev->flags) ? 2 : 0;
  1711. uuid_flags |= mdev->new_state_tmp.disk == D_INCONSISTENT ? 4 : 0;
  1712. p.uuid[UI_FLAGS] = cpu_to_be64(uuid_flags);
  1713. put_ldev(mdev);
  1714. return drbd_send_cmd(mdev, USE_DATA_SOCKET, P_UUIDS,
  1715. (struct p_header80 *)&p, sizeof(p));
  1716. }
  1717. int drbd_send_uuids(struct drbd_conf *mdev)
  1718. {
  1719. return _drbd_send_uuids(mdev, 0);
  1720. }
  1721. int drbd_send_uuids_skip_initial_sync(struct drbd_conf *mdev)
  1722. {
  1723. return _drbd_send_uuids(mdev, 8);
  1724. }
  1725. void drbd_print_uuids(struct drbd_conf *mdev, const char *text)
  1726. {
  1727. if (get_ldev_if_state(mdev, D_NEGOTIATING)) {
  1728. u64 *uuid = mdev->ldev->md.uuid;
  1729. dev_info(DEV, "%s %016llX:%016llX:%016llX:%016llX\n",
  1730. text,
  1731. (unsigned long long)uuid[UI_CURRENT],
  1732. (unsigned long long)uuid[UI_BITMAP],
  1733. (unsigned long long)uuid[UI_HISTORY_START],
  1734. (unsigned long long)uuid[UI_HISTORY_END]);
  1735. put_ldev(mdev);
  1736. } else {
  1737. dev_info(DEV, "%s effective data uuid: %016llX\n",
  1738. text,
  1739. (unsigned long long)mdev->ed_uuid);
  1740. }
  1741. }
  1742. int drbd_gen_and_send_sync_uuid(struct drbd_conf *mdev)
  1743. {
  1744. struct p_rs_uuid p;
  1745. u64 uuid;
  1746. D_ASSERT(mdev->state.disk == D_UP_TO_DATE);
  1747. uuid = mdev->ldev->md.uuid[UI_BITMAP] + UUID_NEW_BM_OFFSET;
  1748. drbd_uuid_set(mdev, UI_BITMAP, uuid);
  1749. drbd_print_uuids(mdev, "updated sync UUID");
  1750. drbd_md_sync(mdev);
  1751. p.uuid = cpu_to_be64(uuid);
  1752. return drbd_send_cmd(mdev, USE_DATA_SOCKET, P_SYNC_UUID,
  1753. (struct p_header80 *)&p, sizeof(p));
  1754. }
  1755. int drbd_send_sizes(struct drbd_conf *mdev, int trigger_reply, enum dds_flags flags)
  1756. {
  1757. struct p_sizes p;
  1758. sector_t d_size, u_size;
  1759. int q_order_type, max_bio_size;
  1760. int ok;
  1761. if (get_ldev_if_state(mdev, D_NEGOTIATING)) {
  1762. D_ASSERT(mdev->ldev->backing_bdev);
  1763. d_size = drbd_get_max_capacity(mdev->ldev);
  1764. u_size = mdev->ldev->dc.disk_size;
  1765. q_order_type = drbd_queue_order_type(mdev);
  1766. max_bio_size = queue_max_hw_sectors(mdev->ldev->backing_bdev->bd_disk->queue) << 9;
  1767. max_bio_size = min_t(int, max_bio_size, DRBD_MAX_BIO_SIZE);
  1768. put_ldev(mdev);
  1769. } else {
  1770. d_size = 0;
  1771. u_size = 0;
  1772. q_order_type = QUEUE_ORDERED_NONE;
  1773. max_bio_size = DRBD_MAX_BIO_SIZE; /* ... multiple BIOs per peer_request */
  1774. }
  1775. p.d_size = cpu_to_be64(d_size);
  1776. p.u_size = cpu_to_be64(u_size);
  1777. p.c_size = cpu_to_be64(trigger_reply ? 0 : drbd_get_capacity(mdev->this_bdev));
  1778. p.max_bio_size = cpu_to_be32(max_bio_size);
  1779. p.queue_order_type = cpu_to_be16(q_order_type);
  1780. p.dds_flags = cpu_to_be16(flags);
  1781. ok = drbd_send_cmd(mdev, USE_DATA_SOCKET, P_SIZES,
  1782. (struct p_header80 *)&p, sizeof(p));
  1783. return ok;
  1784. }
  1785. /**
  1786. * drbd_send_state() - Sends the drbd state to the peer
  1787. * @mdev: DRBD device.
  1788. */
  1789. int drbd_send_state(struct drbd_conf *mdev)
  1790. {
  1791. struct socket *sock;
  1792. struct p_state p;
  1793. int ok = 0;
  1794. /* Grab state lock so we wont send state if we're in the middle
  1795. * of a cluster wide state change on another thread */
  1796. drbd_state_lock(mdev);
  1797. mutex_lock(&mdev->data.mutex);
  1798. p.state = cpu_to_be32(mdev->state.i); /* Within the send mutex */
  1799. sock = mdev->data.socket;
  1800. if (likely(sock != NULL)) {
  1801. ok = _drbd_send_cmd(mdev, sock, P_STATE,
  1802. (struct p_header80 *)&p, sizeof(p), 0);
  1803. }
  1804. mutex_unlock(&mdev->data.mutex);
  1805. drbd_state_unlock(mdev);
  1806. return ok;
  1807. }
  1808. int drbd_send_state_req(struct drbd_conf *mdev,
  1809. union drbd_state mask, union drbd_state val)
  1810. {
  1811. struct p_req_state p;
  1812. p.mask = cpu_to_be32(mask.i);
  1813. p.val = cpu_to_be32(val.i);
  1814. return drbd_send_cmd(mdev, USE_DATA_SOCKET, P_STATE_CHG_REQ,
  1815. (struct p_header80 *)&p, sizeof(p));
  1816. }
  1817. int drbd_send_sr_reply(struct drbd_conf *mdev, enum drbd_state_rv retcode)
  1818. {
  1819. struct p_req_state_reply p;
  1820. p.retcode = cpu_to_be32(retcode);
  1821. return drbd_send_cmd(mdev, USE_META_SOCKET, P_STATE_CHG_REPLY,
  1822. (struct p_header80 *)&p, sizeof(p));
  1823. }
  1824. int fill_bitmap_rle_bits(struct drbd_conf *mdev,
  1825. struct p_compressed_bm *p,
  1826. struct bm_xfer_ctx *c)
  1827. {
  1828. struct bitstream bs;
  1829. unsigned long plain_bits;
  1830. unsigned long tmp;
  1831. unsigned long rl;
  1832. unsigned len;
  1833. unsigned toggle;
  1834. int bits;
  1835. /* may we use this feature? */
  1836. if ((mdev->sync_conf.use_rle == 0) ||
  1837. (mdev->agreed_pro_version < 90))
  1838. return 0;
  1839. if (c->bit_offset >= c->bm_bits)
  1840. return 0; /* nothing to do. */
  1841. /* use at most thus many bytes */
  1842. bitstream_init(&bs, p->code, BM_PACKET_VLI_BYTES_MAX, 0);
  1843. memset(p->code, 0, BM_PACKET_VLI_BYTES_MAX);
  1844. /* plain bits covered in this code string */
  1845. plain_bits = 0;
  1846. /* p->encoding & 0x80 stores whether the first run length is set.
  1847. * bit offset is implicit.
  1848. * start with toggle == 2 to be able to tell the first iteration */
  1849. toggle = 2;
  1850. /* see how much plain bits we can stuff into one packet
  1851. * using RLE and VLI. */
  1852. do {
  1853. tmp = (toggle == 0) ? _drbd_bm_find_next_zero(mdev, c->bit_offset)
  1854. : _drbd_bm_find_next(mdev, c->bit_offset);
  1855. if (tmp == -1UL)
  1856. tmp = c->bm_bits;
  1857. rl = tmp - c->bit_offset;
  1858. if (toggle == 2) { /* first iteration */
  1859. if (rl == 0) {
  1860. /* the first checked bit was set,
  1861. * store start value, */
  1862. DCBP_set_start(p, 1);
  1863. /* but skip encoding of zero run length */
  1864. toggle = !toggle;
  1865. continue;
  1866. }
  1867. DCBP_set_start(p, 0);
  1868. }
  1869. /* paranoia: catch zero runlength.
  1870. * can only happen if bitmap is modified while we scan it. */
  1871. if (rl == 0) {
  1872. dev_err(DEV, "unexpected zero runlength while encoding bitmap "
  1873. "t:%u bo:%lu\n", toggle, c->bit_offset);
  1874. return -1;
  1875. }
  1876. bits = vli_encode_bits(&bs, rl);
  1877. if (bits == -ENOBUFS) /* buffer full */
  1878. break;
  1879. if (bits <= 0) {
  1880. dev_err(DEV, "error while encoding bitmap: %d\n", bits);
  1881. return 0;
  1882. }
  1883. toggle = !toggle;
  1884. plain_bits += rl;
  1885. c->bit_offset = tmp;
  1886. } while (c->bit_offset < c->bm_bits);
  1887. len = bs.cur.b - p->code + !!bs.cur.bit;
  1888. if (plain_bits < (len << 3)) {
  1889. /* incompressible with this method.
  1890. * we need to rewind both word and bit position. */
  1891. c->bit_offset -= plain_bits;
  1892. bm_xfer_ctx_bit_to_word_offset(c);
  1893. c->bit_offset = c->word_offset * BITS_PER_LONG;
  1894. return 0;
  1895. }
  1896. /* RLE + VLI was able to compress it just fine.
  1897. * update c->word_offset. */
  1898. bm_xfer_ctx_bit_to_word_offset(c);
  1899. /* store pad_bits */
  1900. DCBP_set_pad_bits(p, (8 - bs.cur.bit) & 0x7);
  1901. return len;
  1902. }
  1903. /**
  1904. * send_bitmap_rle_or_plain
  1905. *
  1906. * Return 0 when done, 1 when another iteration is needed, and a negative error
  1907. * code upon failure.
  1908. */
  1909. static int
  1910. send_bitmap_rle_or_plain(struct drbd_conf *mdev,
  1911. struct p_header80 *h, struct bm_xfer_ctx *c)
  1912. {
  1913. struct p_compressed_bm *p = (void*)h;
  1914. unsigned long num_words;
  1915. int len;
  1916. int ok;
  1917. len = fill_bitmap_rle_bits(mdev, p, c);
  1918. if (len < 0)
  1919. return -EIO;
  1920. if (len) {
  1921. DCBP_set_code(p, RLE_VLI_Bits);
  1922. ok = _drbd_send_cmd(mdev, mdev->data.socket, P_COMPRESSED_BITMAP, h,
  1923. sizeof(*p) + len, 0);
  1924. c->packets[0]++;
  1925. c->bytes[0] += sizeof(*p) + len;
  1926. if (c->bit_offset >= c->bm_bits)
  1927. len = 0; /* DONE */
  1928. } else {
  1929. /* was not compressible.
  1930. * send a buffer full of plain text bits instead. */
  1931. num_words = min_t(size_t, BM_PACKET_WORDS, c->bm_words - c->word_offset);
  1932. len = num_words * sizeof(long);
  1933. if (len)
  1934. drbd_bm_get_lel(mdev, c->word_offset, num_words, (unsigned long*)h->payload);
  1935. ok = _drbd_send_cmd(mdev, mdev->data.socket, P_BITMAP,
  1936. h, sizeof(struct p_header80) + len, 0);
  1937. c->word_offset += num_words;
  1938. c->bit_offset = c->word_offset * BITS_PER_LONG;
  1939. c->packets[1]++;
  1940. c->bytes[1] += sizeof(struct p_header80) + len;
  1941. if (c->bit_offset > c->bm_bits)
  1942. c->bit_offset = c->bm_bits;
  1943. }
  1944. if (ok) {
  1945. if (len == 0) {
  1946. INFO_bm_xfer_stats(mdev, "send", c);
  1947. return 0;
  1948. } else
  1949. return 1;
  1950. }
  1951. return -EIO;
  1952. }
  1953. /* See the comment at receive_bitmap() */
  1954. int _drbd_send_bitmap(struct drbd_conf *mdev)
  1955. {
  1956. struct bm_xfer_ctx c;
  1957. struct p_header80 *p;
  1958. int err;
  1959. ERR_IF(!mdev->bitmap) return false;
  1960. /* maybe we should use some per thread scratch page,
  1961. * and allocate that during initial device creation? */
  1962. p = (struct p_header80 *) __get_free_page(GFP_NOIO);
  1963. if (!p) {
  1964. dev_err(DEV, "failed to allocate one page buffer in %s\n", __func__);
  1965. return false;
  1966. }
  1967. if (get_ldev(mdev)) {
  1968. if (drbd_md_test_flag(mdev->ldev, MDF_FULL_SYNC)) {
  1969. dev_info(DEV, "Writing the whole bitmap, MDF_FullSync was set.\n");
  1970. drbd_bm_set_all(mdev);
  1971. if (drbd_bm_write(mdev)) {
  1972. /* write_bm did fail! Leave full sync flag set in Meta P_DATA
  1973. * but otherwise process as per normal - need to tell other
  1974. * side that a full resync is required! */
  1975. dev_err(DEV, "Failed to write bitmap to disk!\n");
  1976. } else {
  1977. drbd_md_clear_flag(mdev, MDF_FULL_SYNC);
  1978. drbd_md_sync(mdev);
  1979. }
  1980. }
  1981. put_ldev(mdev);
  1982. }
  1983. c = (struct bm_xfer_ctx) {
  1984. .bm_bits = drbd_bm_bits(mdev),
  1985. .bm_words = drbd_bm_words(mdev),
  1986. };
  1987. do {
  1988. err = send_bitmap_rle_or_plain(mdev, p, &c);
  1989. } while (err > 0);
  1990. free_page((unsigned long) p);
  1991. return err == 0;
  1992. }
  1993. int drbd_send_bitmap(struct drbd_conf *mdev)
  1994. {
  1995. int err;
  1996. if (!drbd_get_data_sock(mdev))
  1997. return -1;
  1998. err = !_drbd_send_bitmap(mdev);
  1999. drbd_put_data_sock(mdev);
  2000. return err;
  2001. }
  2002. int drbd_send_b_ack(struct drbd_conf *mdev, u32 barrier_nr, u32 set_size)
  2003. {
  2004. int ok;
  2005. struct p_barrier_ack p;
  2006. p.barrier = barrier_nr;
  2007. p.set_size = cpu_to_be32(set_size);
  2008. if (mdev->state.conn < C_CONNECTED)
  2009. return false;
  2010. ok = drbd_send_cmd(mdev, USE_META_SOCKET, P_BARRIER_ACK,
  2011. (struct p_header80 *)&p, sizeof(p));
  2012. return ok;
  2013. }
  2014. /**
  2015. * _drbd_send_ack() - Sends an ack packet
  2016. * @mdev: DRBD device.
  2017. * @cmd: Packet command code.
  2018. * @sector: sector, needs to be in big endian byte order
  2019. * @blksize: size in byte, needs to be in big endian byte order
  2020. * @block_id: Id, big endian byte order
  2021. */
  2022. static int _drbd_send_ack(struct drbd_conf *mdev, enum drbd_packets cmd,
  2023. u64 sector,
  2024. u32 blksize,
  2025. u64 block_id)
  2026. {
  2027. int ok;
  2028. struct p_block_ack p;
  2029. p.sector = sector;
  2030. p.block_id = block_id;
  2031. p.blksize = blksize;
  2032. p.seq_num = cpu_to_be32(atomic_add_return(1, &mdev->packet_seq));
  2033. if (!mdev->meta.socket || mdev->state.conn < C_CONNECTED)
  2034. return false;
  2035. ok = drbd_send_cmd(mdev, USE_META_SOCKET, cmd,
  2036. (struct p_header80 *)&p, sizeof(p));
  2037. return ok;
  2038. }
  2039. /* dp->sector and dp->block_id already/still in network byte order,
  2040. * data_size is payload size according to dp->head,
  2041. * and may need to be corrected for digest size. */
  2042. int drbd_send_ack_dp(struct drbd_conf *mdev, enum drbd_packets cmd,
  2043. struct p_data *dp, int data_size)
  2044. {
  2045. data_size -= (mdev->agreed_pro_version >= 87 && mdev->integrity_r_tfm) ?
  2046. crypto_hash_digestsize(mdev->integrity_r_tfm) : 0;
  2047. return _drbd_send_ack(mdev, cmd, dp->sector, cpu_to_be32(data_size),
  2048. dp->block_id);
  2049. }
  2050. int drbd_send_ack_rp(struct drbd_conf *mdev, enum drbd_packets cmd,
  2051. struct p_block_req *rp)
  2052. {
  2053. return _drbd_send_ack(mdev, cmd, rp->sector, rp->blksize, rp->block_id);
  2054. }
  2055. /**
  2056. * drbd_send_ack() - Sends an ack packet
  2057. * @mdev: DRBD device.
  2058. * @cmd: Packet command code.
  2059. * @e: Epoch entry.
  2060. */
  2061. int drbd_send_ack(struct drbd_conf *mdev,
  2062. enum drbd_packets cmd, struct drbd_epoch_entry *e)
  2063. {
  2064. return _drbd_send_ack(mdev, cmd,
  2065. cpu_to_be64(e->sector),
  2066. cpu_to_be32(e->size),
  2067. e->block_id);
  2068. }
  2069. /* This function misuses the block_id field to signal if the blocks
  2070. * are is sync or not. */
  2071. int drbd_send_ack_ex(struct drbd_conf *mdev, enum drbd_packets cmd,
  2072. sector_t sector, int blksize, u64 block_id)
  2073. {
  2074. return _drbd_send_ack(mdev, cmd,
  2075. cpu_to_be64(sector),
  2076. cpu_to_be32(blksize),
  2077. cpu_to_be64(block_id));
  2078. }
  2079. int drbd_send_drequest(struct drbd_conf *mdev, int cmd,
  2080. sector_t sector, int size, u64 block_id)
  2081. {
  2082. int ok;
  2083. struct p_block_req p;
  2084. p.sector = cpu_to_be64(sector);
  2085. p.block_id = block_id;
  2086. p.blksize = cpu_to_be32(size);
  2087. ok = drbd_send_cmd(mdev, USE_DATA_SOCKET, cmd,
  2088. (struct p_header80 *)&p, sizeof(p));
  2089. return ok;
  2090. }
  2091. int drbd_send_drequest_csum(struct drbd_conf *mdev,
  2092. sector_t sector, int size,
  2093. void *digest, int digest_size,
  2094. enum drbd_packets cmd)
  2095. {
  2096. int ok;
  2097. struct p_block_req p;
  2098. p.sector = cpu_to_be64(sector);
  2099. p.block_id = BE_DRBD_MAGIC + 0xbeef;
  2100. p.blksize = cpu_to_be32(size);
  2101. p.head.magic = BE_DRBD_MAGIC;
  2102. p.head.command = cpu_to_be16(cmd);
  2103. p.head.length = cpu_to_be16(sizeof(p) - sizeof(struct p_header80) + digest_size);
  2104. mutex_lock(&mdev->data.mutex);
  2105. ok = (sizeof(p) == drbd_send(mdev, mdev->data.socket, &p, sizeof(p), 0));
  2106. ok = ok && (digest_size == drbd_send(mdev, mdev->data.socket, digest, digest_size, 0));
  2107. mutex_unlock(&mdev->data.mutex);
  2108. return ok;
  2109. }
  2110. int drbd_send_ov_request(struct drbd_conf *mdev, sector_t sector, int size)
  2111. {
  2112. int ok;
  2113. struct p_block_req p;
  2114. p.sector = cpu_to_be64(sector);
  2115. p.block_id = BE_DRBD_MAGIC + 0xbabe;
  2116. p.blksize = cpu_to_be32(size);
  2117. ok = drbd_send_cmd(mdev, USE_DATA_SOCKET, P_OV_REQUEST,
  2118. (struct p_header80 *)&p, sizeof(p));
  2119. return ok;
  2120. }
  2121. /* called on sndtimeo
  2122. * returns false if we should retry,
  2123. * true if we think connection is dead
  2124. */
  2125. static int we_should_drop_the_connection(struct drbd_conf *mdev, struct socket *sock)
  2126. {
  2127. int drop_it;
  2128. /* long elapsed = (long)(jiffies - mdev->last_received); */
  2129. drop_it = mdev->meta.socket == sock
  2130. || !mdev->asender.task
  2131. || get_t_state(&mdev->asender) != Running
  2132. || mdev->state.conn < C_CONNECTED;
  2133. if (drop_it)
  2134. return true;
  2135. drop_it = !--mdev->ko_count;
  2136. if (!drop_it) {
  2137. dev_err(DEV, "[%s/%d] sock_sendmsg time expired, ko = %u\n",
  2138. current->comm, current->pid, mdev->ko_count);
  2139. request_ping(mdev);
  2140. }
  2141. return drop_it; /* && (mdev->state == R_PRIMARY) */;
  2142. }
  2143. /* The idea of sendpage seems to be to put some kind of reference
  2144. * to the page into the skb, and to hand it over to the NIC. In
  2145. * this process get_page() gets called.
  2146. *
  2147. * As soon as the page was really sent over the network put_page()
  2148. * gets called by some part of the network layer. [ NIC driver? ]
  2149. *
  2150. * [ get_page() / put_page() increment/decrement the count. If count
  2151. * reaches 0 the page will be freed. ]
  2152. *
  2153. * This works nicely with pages from FSs.
  2154. * But this means that in protocol A we might signal IO completion too early!
  2155. *
  2156. * In order not to corrupt data during a resync we must make sure
  2157. * that we do not reuse our own buffer pages (EEs) to early, therefore
  2158. * we have the net_ee list.
  2159. *
  2160. * XFS seems to have problems, still, it submits pages with page_count == 0!
  2161. * As a workaround, we disable sendpage on pages
  2162. * with page_count == 0 or PageSlab.
  2163. */
  2164. static int _drbd_no_send_page(struct drbd_conf *mdev, struct page *page,
  2165. int offset, size_t size, unsigned msg_flags)
  2166. {
  2167. int sent = drbd_send(mdev, mdev->data.socket, kmap(page) + offset, size, msg_flags);
  2168. kunmap(page);
  2169. if (sent == size)
  2170. mdev->send_cnt += size>>9;
  2171. return sent == size;
  2172. }
  2173. static int _drbd_send_page(struct drbd_conf *mdev, struct page *page,
  2174. int offset, size_t size, unsigned msg_flags)
  2175. {
  2176. mm_segment_t oldfs = get_fs();
  2177. int sent, ok;
  2178. int len = size;
  2179. /* e.g. XFS meta- & log-data is in slab pages, which have a
  2180. * page_count of 0 and/or have PageSlab() set.
  2181. * we cannot use send_page for those, as that does get_page();
  2182. * put_page(); and would cause either a VM_BUG directly, or
  2183. * __page_cache_release a page that would actually still be referenced
  2184. * by someone, leading to some obscure delayed Oops somewhere else. */
  2185. if (disable_sendpage || (page_count(page) < 1) || PageSlab(page))
  2186. return _drbd_no_send_page(mdev, page, offset, size, msg_flags);
  2187. msg_flags |= MSG_NOSIGNAL;
  2188. drbd_update_congested(mdev);
  2189. set_fs(KERNEL_DS);
  2190. do {
  2191. sent = mdev->data.socket->ops->sendpage(mdev->data.socket, page,
  2192. offset, len,
  2193. msg_flags);
  2194. if (sent == -EAGAIN) {
  2195. if (we_should_drop_the_connection(mdev,
  2196. mdev->data.socket))
  2197. break;
  2198. else
  2199. continue;
  2200. }
  2201. if (sent <= 0) {
  2202. dev_warn(DEV, "%s: size=%d len=%d sent=%d\n",
  2203. __func__, (int)size, len, sent);
  2204. break;
  2205. }
  2206. len -= sent;
  2207. offset += sent;
  2208. } while (len > 0 /* THINK && mdev->cstate >= C_CONNECTED*/);
  2209. set_fs(oldfs);
  2210. clear_bit(NET_CONGESTED, &mdev->flags);
  2211. ok = (len == 0);
  2212. if (likely(ok))
  2213. mdev->send_cnt += size>>9;
  2214. return ok;
  2215. }
  2216. static int _drbd_send_bio(struct drbd_conf *mdev, struct bio *bio)
  2217. {
  2218. struct bio_vec *bvec;
  2219. int i;
  2220. /* hint all but last page with MSG_MORE */
  2221. __bio_for_each_segment(bvec, bio, i, 0) {
  2222. if (!_drbd_no_send_page(mdev, bvec->bv_page,
  2223. bvec->bv_offset, bvec->bv_len,
  2224. i == bio->bi_vcnt -1 ? 0 : MSG_MORE))
  2225. return 0;
  2226. }
  2227. return 1;
  2228. }
  2229. static int _drbd_send_zc_bio(struct drbd_conf *mdev, struct bio *bio)
  2230. {
  2231. struct bio_vec *bvec;
  2232. int i;
  2233. /* hint all but last page with MSG_MORE */
  2234. __bio_for_each_segment(bvec, bio, i, 0) {
  2235. if (!_drbd_send_page(mdev, bvec->bv_page,
  2236. bvec->bv_offset, bvec->bv_len,
  2237. i == bio->bi_vcnt -1 ? 0 : MSG_MORE))
  2238. return 0;
  2239. }
  2240. return 1;
  2241. }
  2242. static int _drbd_send_zc_ee(struct drbd_conf *mdev, struct drbd_epoch_entry *e)
  2243. {
  2244. struct page *page = e->pages;
  2245. unsigned len = e->size;
  2246. /* hint all but last page with MSG_MORE */
  2247. page_chain_for_each(page) {
  2248. unsigned l = min_t(unsigned, len, PAGE_SIZE);
  2249. if (!_drbd_send_page(mdev, page, 0, l,
  2250. page_chain_next(page) ? MSG_MORE : 0))
  2251. return 0;
  2252. len -= l;
  2253. }
  2254. return 1;
  2255. }
  2256. static u32 bio_flags_to_wire(struct drbd_conf *mdev, unsigned long bi_rw)
  2257. {
  2258. if (mdev->agreed_pro_version >= 95)
  2259. return (bi_rw & REQ_SYNC ? DP_RW_SYNC : 0) |
  2260. (bi_rw & REQ_FUA ? DP_FUA : 0) |
  2261. (bi_rw & REQ_FLUSH ? DP_FLUSH : 0) |
  2262. (bi_rw & REQ_DISCARD ? DP_DISCARD : 0);
  2263. else
  2264. return bi_rw & REQ_SYNC ? DP_RW_SYNC : 0;
  2265. }
  2266. /* Used to send write requests
  2267. * R_PRIMARY -> Peer (P_DATA)
  2268. */
  2269. int drbd_send_dblock(struct drbd_conf *mdev, struct drbd_request *req)
  2270. {
  2271. int ok = 1;
  2272. struct p_data p;
  2273. unsigned int dp_flags = 0;
  2274. void *dgb;
  2275. int dgs;
  2276. if (!drbd_get_data_sock(mdev))
  2277. return 0;
  2278. dgs = (mdev->agreed_pro_version >= 87 && mdev->integrity_w_tfm) ?
  2279. crypto_hash_digestsize(mdev->integrity_w_tfm) : 0;
  2280. if (req->size <= DRBD_MAX_SIZE_H80_PACKET) {
  2281. p.head.h80.magic = BE_DRBD_MAGIC;
  2282. p.head.h80.command = cpu_to_be16(P_DATA);
  2283. p.head.h80.length =
  2284. cpu_to_be16(sizeof(p) - sizeof(union p_header) + dgs + req->size);
  2285. } else {
  2286. p.head.h95.magic = BE_DRBD_MAGIC_BIG;
  2287. p.head.h95.command = cpu_to_be16(P_DATA);
  2288. p.head.h95.length =
  2289. cpu_to_be32(sizeof(p) - sizeof(union p_header) + dgs + req->size);
  2290. }
  2291. p.sector = cpu_to_be64(req->sector);
  2292. p.block_id = (unsigned long)req;
  2293. p.seq_num = cpu_to_be32(req->seq_num =
  2294. atomic_add_return(1, &mdev->packet_seq));
  2295. dp_flags = bio_flags_to_wire(mdev, req->master_bio->bi_rw);
  2296. if (mdev->state.conn >= C_SYNC_SOURCE &&
  2297. mdev->state.conn <= C_PAUSED_SYNC_T)
  2298. dp_flags |= DP_MAY_SET_IN_SYNC;
  2299. p.dp_flags = cpu_to_be32(dp_flags);
  2300. set_bit(UNPLUG_REMOTE, &mdev->flags);
  2301. ok = (sizeof(p) ==
  2302. drbd_send(mdev, mdev->data.socket, &p, sizeof(p), dgs ? MSG_MORE : 0));
  2303. if (ok && dgs) {
  2304. dgb = mdev->int_dig_out;
  2305. drbd_csum_bio(mdev, mdev->integrity_w_tfm, req->master_bio, dgb);
  2306. ok = dgs == drbd_send(mdev, mdev->data.socket, dgb, dgs, 0);
  2307. }
  2308. if (ok) {
  2309. /* For protocol A, we have to memcpy the payload into
  2310. * socket buffers, as we may complete right away
  2311. * as soon as we handed it over to tcp, at which point the data
  2312. * pages may become invalid.
  2313. *
  2314. * For data-integrity enabled, we copy it as well, so we can be
  2315. * sure that even if the bio pages may still be modified, it
  2316. * won't change the data on the wire, thus if the digest checks
  2317. * out ok after sending on this side, but does not fit on the
  2318. * receiving side, we sure have detected corruption elsewhere.
  2319. */
  2320. if (mdev->net_conf->wire_protocol == DRBD_PROT_A || dgs)
  2321. ok = _drbd_send_bio(mdev, req->master_bio);
  2322. else
  2323. ok = _drbd_send_zc_bio(mdev, req->master_bio);
  2324. /* double check digest, sometimes buffers have been modified in flight. */
  2325. if (dgs > 0 && dgs <= 64) {
  2326. /* 64 byte, 512 bit, is the largest digest size
  2327. * currently supported in kernel crypto. */
  2328. unsigned char digest[64];
  2329. drbd_csum_bio(mdev, mdev->integrity_w_tfm, req->master_bio, digest);
  2330. if (memcmp(mdev->int_dig_out, digest, dgs)) {
  2331. dev_warn(DEV,
  2332. "Digest mismatch, buffer modified by upper layers during write: %llus +%u\n",
  2333. (unsigned long long)req->sector, req->size);
  2334. }
  2335. } /* else if (dgs > 64) {
  2336. ... Be noisy about digest too large ...
  2337. } */
  2338. }
  2339. drbd_put_data_sock(mdev);
  2340. return ok;
  2341. }
  2342. /* answer packet, used to send data back for read requests:
  2343. * Peer -> (diskless) R_PRIMARY (P_DATA_REPLY)
  2344. * C_SYNC_SOURCE -> C_SYNC_TARGET (P_RS_DATA_REPLY)
  2345. */
  2346. int drbd_send_block(struct drbd_conf *mdev, enum drbd_packets cmd,
  2347. struct drbd_epoch_entry *e)
  2348. {
  2349. int ok;
  2350. struct p_data p;
  2351. void *dgb;
  2352. int dgs;
  2353. dgs = (mdev->agreed_pro_version >= 87 && mdev->integrity_w_tfm) ?
  2354. crypto_hash_digestsize(mdev->integrity_w_tfm) : 0;
  2355. if (e->size <= DRBD_MAX_SIZE_H80_PACKET) {
  2356. p.head.h80.magic = BE_DRBD_MAGIC;
  2357. p.head.h80.command = cpu_to_be16(cmd);
  2358. p.head.h80.length =
  2359. cpu_to_be16(sizeof(p) - sizeof(struct p_header80) + dgs + e->size);
  2360. } else {
  2361. p.head.h95.magic = BE_DRBD_MAGIC_BIG;
  2362. p.head.h95.command = cpu_to_be16(cmd);
  2363. p.head.h95.length =
  2364. cpu_to_be32(sizeof(p) - sizeof(struct p_header80) + dgs + e->size);
  2365. }
  2366. p.sector = cpu_to_be64(e->sector);
  2367. p.block_id = e->block_id;
  2368. /* p.seq_num = 0; No sequence numbers here.. */
  2369. /* Only called by our kernel thread.
  2370. * This one may be interrupted by DRBD_SIG and/or DRBD_SIGKILL
  2371. * in response to admin command or module unload.
  2372. */
  2373. if (!drbd_get_data_sock(mdev))
  2374. return 0;
  2375. ok = sizeof(p) == drbd_send(mdev, mdev->data.socket, &p, sizeof(p), dgs ? MSG_MORE : 0);
  2376. if (ok && dgs) {
  2377. dgb = mdev->int_dig_out;
  2378. drbd_csum_ee(mdev, mdev->integrity_w_tfm, e, dgb);
  2379. ok = dgs == drbd_send(mdev, mdev->data.socket, dgb, dgs, 0);
  2380. }
  2381. if (ok)
  2382. ok = _drbd_send_zc_ee(mdev, e);
  2383. drbd_put_data_sock(mdev);
  2384. return ok;
  2385. }
  2386. int drbd_send_oos(struct drbd_conf *mdev, struct drbd_request *req)
  2387. {
  2388. struct p_block_desc p;
  2389. p.sector = cpu_to_be64(req->sector);
  2390. p.blksize = cpu_to_be32(req->size);
  2391. return drbd_send_cmd(mdev, USE_DATA_SOCKET, P_OUT_OF_SYNC, &p.head, sizeof(p));
  2392. }
  2393. /*
  2394. drbd_send distinguishes two cases:
  2395. Packets sent via the data socket "sock"
  2396. and packets sent via the meta data socket "msock"
  2397. sock msock
  2398. -----------------+-------------------------+------------------------------
  2399. timeout conf.timeout / 2 conf.timeout / 2
  2400. timeout action send a ping via msock Abort communication
  2401. and close all sockets
  2402. */
  2403. /*
  2404. * you must have down()ed the appropriate [m]sock_mutex elsewhere!
  2405. */
  2406. int drbd_send(struct drbd_conf *mdev, struct socket *sock,
  2407. void *buf, size_t size, unsigned msg_flags)
  2408. {
  2409. struct kvec iov;
  2410. struct msghdr msg;
  2411. int rv, sent = 0;
  2412. if (!sock)
  2413. return -1000;
  2414. /* THINK if (signal_pending) return ... ? */
  2415. iov.iov_base = buf;
  2416. iov.iov_len = size;
  2417. msg.msg_name = NULL;
  2418. msg.msg_namelen = 0;
  2419. msg.msg_control = NULL;
  2420. msg.msg_controllen = 0;
  2421. msg.msg_flags = msg_flags | MSG_NOSIGNAL;
  2422. if (sock == mdev->data.socket) {
  2423. mdev->ko_count = mdev->net_conf->ko_count;
  2424. drbd_update_congested(mdev);
  2425. }
  2426. do {
  2427. /* STRANGE
  2428. * tcp_sendmsg does _not_ use its size parameter at all ?
  2429. *
  2430. * -EAGAIN on timeout, -EINTR on signal.
  2431. */
  2432. /* THINK
  2433. * do we need to block DRBD_SIG if sock == &meta.socket ??
  2434. * otherwise wake_asender() might interrupt some send_*Ack !
  2435. */
  2436. rv = kernel_sendmsg(sock, &msg, &iov, 1, size);
  2437. if (rv == -EAGAIN) {
  2438. if (we_should_drop_the_connection(mdev, sock))
  2439. break;
  2440. else
  2441. continue;
  2442. }
  2443. D_ASSERT(rv != 0);
  2444. if (rv == -EINTR) {
  2445. flush_signals(current);
  2446. rv = 0;
  2447. }
  2448. if (rv < 0)
  2449. break;
  2450. sent += rv;
  2451. iov.iov_base += rv;
  2452. iov.iov_len -= rv;
  2453. } while (sent < size);
  2454. if (sock == mdev->data.socket)
  2455. clear_bit(NET_CONGESTED, &mdev->flags);
  2456. if (rv <= 0) {
  2457. if (rv != -EAGAIN) {
  2458. dev_err(DEV, "%s_sendmsg returned %d\n",
  2459. sock == mdev->meta.socket ? "msock" : "sock",
  2460. rv);
  2461. drbd_force_state(mdev, NS(conn, C_BROKEN_PIPE));
  2462. } else
  2463. drbd_force_state(mdev, NS(conn, C_TIMEOUT));
  2464. }
  2465. return sent;
  2466. }
  2467. static int drbd_open(struct block_device *bdev, fmode_t mode)
  2468. {
  2469. struct drbd_conf *mdev = bdev->bd_disk->private_data;
  2470. unsigned long flags;
  2471. int rv = 0;
  2472. mutex_lock(&drbd_main_mutex);
  2473. spin_lock_irqsave(&mdev->req_lock, flags);
  2474. /* to have a stable mdev->state.role
  2475. * and no race with updating open_cnt */
  2476. if (mdev->state.role != R_PRIMARY) {
  2477. if (mode & FMODE_WRITE)
  2478. rv = -EROFS;
  2479. else if (!allow_oos)
  2480. rv = -EMEDIUMTYPE;
  2481. }
  2482. if (!rv)
  2483. mdev->open_cnt++;
  2484. spin_unlock_irqrestore(&mdev->req_lock, flags);
  2485. mutex_unlock(&drbd_main_mutex);
  2486. return rv;
  2487. }
  2488. static int drbd_release(struct gendisk *gd, fmode_t mode)
  2489. {
  2490. struct drbd_conf *mdev = gd->private_data;
  2491. mutex_lock(&drbd_main_mutex);
  2492. mdev->open_cnt--;
  2493. mutex_unlock(&drbd_main_mutex);
  2494. return 0;
  2495. }
  2496. static void drbd_set_defaults(struct drbd_conf *mdev)
  2497. {
  2498. /* This way we get a compile error when sync_conf grows,
  2499. and we forgot to initialize it here */
  2500. mdev->sync_conf = (struct syncer_conf) {
  2501. /* .rate = */ DRBD_RATE_DEF,
  2502. /* .after = */ DRBD_AFTER_DEF,
  2503. /* .al_extents = */ DRBD_AL_EXTENTS_DEF,
  2504. /* .verify_alg = */ {}, 0,
  2505. /* .cpu_mask = */ {}, 0,
  2506. /* .csums_alg = */ {}, 0,
  2507. /* .use_rle = */ 0,
  2508. /* .on_no_data = */ DRBD_ON_NO_DATA_DEF,
  2509. /* .c_plan_ahead = */ DRBD_C_PLAN_AHEAD_DEF,
  2510. /* .c_delay_target = */ DRBD_C_DELAY_TARGET_DEF,
  2511. /* .c_fill_target = */ DRBD_C_FILL_TARGET_DEF,
  2512. /* .c_max_rate = */ DRBD_C_MAX_RATE_DEF,
  2513. /* .c_min_rate = */ DRBD_C_MIN_RATE_DEF
  2514. };
  2515. /* Have to use that way, because the layout differs between
  2516. big endian and little endian */
  2517. mdev->state = (union drbd_state) {
  2518. { .role = R_SECONDARY,
  2519. .peer = R_UNKNOWN,
  2520. .conn = C_STANDALONE,
  2521. .disk = D_DISKLESS,
  2522. .pdsk = D_UNKNOWN,
  2523. .susp = 0,
  2524. .susp_nod = 0,
  2525. .susp_fen = 0
  2526. } };
  2527. }
  2528. void drbd_init_set_defaults(struct drbd_conf *mdev)
  2529. {
  2530. /* the memset(,0,) did most of this.
  2531. * note: only assignments, no allocation in here */
  2532. drbd_set_defaults(mdev);
  2533. atomic_set(&mdev->ap_bio_cnt, 0);
  2534. atomic_set(&mdev->ap_pending_cnt, 0);
  2535. atomic_set(&mdev->rs_pending_cnt, 0);
  2536. atomic_set(&mdev->unacked_cnt, 0);
  2537. atomic_set(&mdev->local_cnt, 0);
  2538. atomic_set(&mdev->net_cnt, 0);
  2539. atomic_set(&mdev->packet_seq, 0);
  2540. atomic_set(&mdev->pp_in_use, 0);
  2541. atomic_set(&mdev->pp_in_use_by_net, 0);
  2542. atomic_set(&mdev->rs_sect_in, 0);
  2543. atomic_set(&mdev->rs_sect_ev, 0);
  2544. atomic_set(&mdev->ap_in_flight, 0);
  2545. mutex_init(&mdev->md_io_mutex);
  2546. mutex_init(&mdev->data.mutex);
  2547. mutex_init(&mdev->meta.mutex);
  2548. sema_init(&mdev->data.work.s, 0);
  2549. sema_init(&mdev->meta.work.s, 0);
  2550. mutex_init(&mdev->state_mutex);
  2551. spin_lock_init(&mdev->data.work.q_lock);
  2552. spin_lock_init(&mdev->meta.work.q_lock);
  2553. spin_lock_init(&mdev->al_lock);
  2554. spin_lock_init(&mdev->req_lock);
  2555. spin_lock_init(&mdev->peer_seq_lock);
  2556. spin_lock_init(&mdev->epoch_lock);
  2557. INIT_LIST_HEAD(&mdev->active_ee);
  2558. INIT_LIST_HEAD(&mdev->sync_ee);
  2559. INIT_LIST_HEAD(&mdev->done_ee);
  2560. INIT_LIST_HEAD(&mdev->read_ee);
  2561. INIT_LIST_HEAD(&mdev->net_ee);
  2562. INIT_LIST_HEAD(&mdev->resync_reads);
  2563. INIT_LIST_HEAD(&mdev->data.work.q);
  2564. INIT_LIST_HEAD(&mdev->meta.work.q);
  2565. INIT_LIST_HEAD(&mdev->resync_work.list);
  2566. INIT_LIST_HEAD(&mdev->unplug_work.list);
  2567. INIT_LIST_HEAD(&mdev->go_diskless.list);
  2568. INIT_LIST_HEAD(&mdev->md_sync_work.list);
  2569. INIT_LIST_HEAD(&mdev->start_resync_work.list);
  2570. INIT_LIST_HEAD(&mdev->bm_io_work.w.list);
  2571. mdev->resync_work.cb = w_resync_timer;
  2572. mdev->unplug_work.cb = w_send_write_hint;
  2573. mdev->go_diskless.cb = w_go_diskless;
  2574. mdev->md_sync_work.cb = w_md_sync;
  2575. mdev->bm_io_work.w.cb = w_bitmap_io;
  2576. mdev->start_resync_work.cb = w_start_resync;
  2577. init_timer(&mdev->resync_timer);
  2578. init_timer(&mdev->md_sync_timer);
  2579. init_timer(&mdev->start_resync_timer);
  2580. init_timer(&mdev->request_timer);
  2581. mdev->resync_timer.function = resync_timer_fn;
  2582. mdev->resync_timer.data = (unsigned long) mdev;
  2583. mdev->md_sync_timer.function = md_sync_timer_fn;
  2584. mdev->md_sync_timer.data = (unsigned long) mdev;
  2585. mdev->start_resync_timer.function = start_resync_timer_fn;
  2586. mdev->start_resync_timer.data = (unsigned long) mdev;
  2587. mdev->request_timer.function = request_timer_fn;
  2588. mdev->request_timer.data = (unsigned long) mdev;
  2589. init_waitqueue_head(&mdev->misc_wait);
  2590. init_waitqueue_head(&mdev->state_wait);
  2591. init_waitqueue_head(&mdev->net_cnt_wait);
  2592. init_waitqueue_head(&mdev->ee_wait);
  2593. init_waitqueue_head(&mdev->al_wait);
  2594. init_waitqueue_head(&mdev->seq_wait);
  2595. drbd_thread_init(mdev, &mdev->receiver, drbdd_init);
  2596. drbd_thread_init(mdev, &mdev->worker, drbd_worker);
  2597. drbd_thread_init(mdev, &mdev->asender, drbd_asender);
  2598. mdev->agreed_pro_version = PRO_VERSION_MAX;
  2599. mdev->write_ordering = WO_bdev_flush;
  2600. mdev->resync_wenr = LC_FREE;
  2601. mdev->peer_max_bio_size = DRBD_MAX_BIO_SIZE_SAFE;
  2602. mdev->local_max_bio_size = DRBD_MAX_BIO_SIZE_SAFE;
  2603. }
  2604. void drbd_mdev_cleanup(struct drbd_conf *mdev)
  2605. {
  2606. int i;
  2607. if (mdev->receiver.t_state != None)
  2608. dev_err(DEV, "ASSERT FAILED: receiver t_state == %d expected 0.\n",
  2609. mdev->receiver.t_state);
  2610. /* no need to lock it, I'm the only thread alive */
  2611. if (atomic_read(&mdev->current_epoch->epoch_size) != 0)
  2612. dev_err(DEV, "epoch_size:%d\n", atomic_read(&mdev->current_epoch->epoch_size));
  2613. mdev->al_writ_cnt =
  2614. mdev->bm_writ_cnt =
  2615. mdev->read_cnt =
  2616. mdev->recv_cnt =
  2617. mdev->send_cnt =
  2618. mdev->writ_cnt =
  2619. mdev->p_size =
  2620. mdev->rs_start =
  2621. mdev->rs_total =
  2622. mdev->rs_failed = 0;
  2623. mdev->rs_last_events = 0;
  2624. mdev->rs_last_sect_ev = 0;
  2625. for (i = 0; i < DRBD_SYNC_MARKS; i++) {
  2626. mdev->rs_mark_left[i] = 0;
  2627. mdev->rs_mark_time[i] = 0;
  2628. }
  2629. D_ASSERT(mdev->net_conf == NULL);
  2630. drbd_set_my_capacity(mdev, 0);
  2631. if (mdev->bitmap) {
  2632. /* maybe never allocated. */
  2633. drbd_bm_resize(mdev, 0, 1);
  2634. drbd_bm_cleanup(mdev);
  2635. }
  2636. drbd_free_resources(mdev);
  2637. clear_bit(AL_SUSPENDED, &mdev->flags);
  2638. /*
  2639. * currently we drbd_init_ee only on module load, so
  2640. * we may do drbd_release_ee only on module unload!
  2641. */
  2642. D_ASSERT(list_empty(&mdev->active_ee));
  2643. D_ASSERT(list_empty(&mdev->sync_ee));
  2644. D_ASSERT(list_empty(&mdev->done_ee));
  2645. D_ASSERT(list_empty(&mdev->read_ee));
  2646. D_ASSERT(list_empty(&mdev->net_ee));
  2647. D_ASSERT(list_empty(&mdev->resync_reads));
  2648. D_ASSERT(list_empty(&mdev->data.work.q));
  2649. D_ASSERT(list_empty(&mdev->meta.work.q));
  2650. D_ASSERT(list_empty(&mdev->resync_work.list));
  2651. D_ASSERT(list_empty(&mdev->unplug_work.list));
  2652. D_ASSERT(list_empty(&mdev->go_diskless.list));
  2653. drbd_set_defaults(mdev);
  2654. }
  2655. static void drbd_destroy_mempools(void)
  2656. {
  2657. struct page *page;
  2658. while (drbd_pp_pool) {
  2659. page = drbd_pp_pool;
  2660. drbd_pp_pool = (struct page *)page_private(page);
  2661. __free_page(page);
  2662. drbd_pp_vacant--;
  2663. }
  2664. /* D_ASSERT(atomic_read(&drbd_pp_vacant)==0); */
  2665. if (drbd_ee_mempool)
  2666. mempool_destroy(drbd_ee_mempool);
  2667. if (drbd_request_mempool)
  2668. mempool_destroy(drbd_request_mempool);
  2669. if (drbd_ee_cache)
  2670. kmem_cache_destroy(drbd_ee_cache);
  2671. if (drbd_request_cache)
  2672. kmem_cache_destroy(drbd_request_cache);
  2673. if (drbd_bm_ext_cache)
  2674. kmem_cache_destroy(drbd_bm_ext_cache);
  2675. if (drbd_al_ext_cache)
  2676. kmem_cache_destroy(drbd_al_ext_cache);
  2677. drbd_ee_mempool = NULL;
  2678. drbd_request_mempool = NULL;
  2679. drbd_ee_cache = NULL;
  2680. drbd_request_cache = NULL;
  2681. drbd_bm_ext_cache = NULL;
  2682. drbd_al_ext_cache = NULL;
  2683. return;
  2684. }
  2685. static int drbd_create_mempools(void)
  2686. {
  2687. struct page *page;
  2688. const int number = (DRBD_MAX_BIO_SIZE/PAGE_SIZE) * minor_count;
  2689. int i;
  2690. /* prepare our caches and mempools */
  2691. drbd_request_mempool = NULL;
  2692. drbd_ee_cache = NULL;
  2693. drbd_request_cache = NULL;
  2694. drbd_bm_ext_cache = NULL;
  2695. drbd_al_ext_cache = NULL;
  2696. drbd_pp_pool = NULL;
  2697. /* caches */
  2698. drbd_request_cache = kmem_cache_create(
  2699. "drbd_req", sizeof(struct drbd_request), 0, 0, NULL);
  2700. if (drbd_request_cache == NULL)
  2701. goto Enomem;
  2702. drbd_ee_cache = kmem_cache_create(
  2703. "drbd_ee", sizeof(struct drbd_epoch_entry), 0, 0, NULL);
  2704. if (drbd_ee_cache == NULL)
  2705. goto Enomem;
  2706. drbd_bm_ext_cache = kmem_cache_create(
  2707. "drbd_bm", sizeof(struct bm_extent), 0, 0, NULL);
  2708. if (drbd_bm_ext_cache == NULL)
  2709. goto Enomem;
  2710. drbd_al_ext_cache = kmem_cache_create(
  2711. "drbd_al", sizeof(struct lc_element), 0, 0, NULL);
  2712. if (drbd_al_ext_cache == NULL)
  2713. goto Enomem;
  2714. /* mempools */
  2715. drbd_request_mempool = mempool_create(number,
  2716. mempool_alloc_slab, mempool_free_slab, drbd_request_cache);
  2717. if (drbd_request_mempool == NULL)
  2718. goto Enomem;
  2719. drbd_ee_mempool = mempool_create(number,
  2720. mempool_alloc_slab, mempool_free_slab, drbd_ee_cache);
  2721. if (drbd_ee_mempool == NULL)
  2722. goto Enomem;
  2723. /* drbd's page pool */
  2724. spin_lock_init(&drbd_pp_lock);
  2725. for (i = 0; i < number; i++) {
  2726. page = alloc_page(GFP_HIGHUSER);
  2727. if (!page)
  2728. goto Enomem;
  2729. set_page_private(page, (unsigned long)drbd_pp_pool);
  2730. drbd_pp_pool = page;
  2731. }
  2732. drbd_pp_vacant = number;
  2733. return 0;
  2734. Enomem:
  2735. drbd_destroy_mempools(); /* in case we allocated some */
  2736. return -ENOMEM;
  2737. }
  2738. static int drbd_notify_sys(struct notifier_block *this, unsigned long code,
  2739. void *unused)
  2740. {
  2741. /* just so we have it. you never know what interesting things we
  2742. * might want to do here some day...
  2743. */
  2744. return NOTIFY_DONE;
  2745. }
  2746. static struct notifier_block drbd_notifier = {
  2747. .notifier_call = drbd_notify_sys,
  2748. };
  2749. static void drbd_release_ee_lists(struct drbd_conf *mdev)
  2750. {
  2751. int rr;
  2752. rr = drbd_release_ee(mdev, &mdev->active_ee);
  2753. if (rr)
  2754. dev_err(DEV, "%d EEs in active list found!\n", rr);
  2755. rr = drbd_release_ee(mdev, &mdev->sync_ee);
  2756. if (rr)
  2757. dev_err(DEV, "%d EEs in sync list found!\n", rr);
  2758. rr = drbd_release_ee(mdev, &mdev->read_ee);
  2759. if (rr)
  2760. dev_err(DEV, "%d EEs in read list found!\n", rr);
  2761. rr = drbd_release_ee(mdev, &mdev->done_ee);
  2762. if (rr)
  2763. dev_err(DEV, "%d EEs in done list found!\n", rr);
  2764. rr = drbd_release_ee(mdev, &mdev->net_ee);
  2765. if (rr)
  2766. dev_err(DEV, "%d EEs in net list found!\n", rr);
  2767. }
  2768. /* caution. no locking.
  2769. * currently only used from module cleanup code. */
  2770. static void drbd_delete_device(unsigned int minor)
  2771. {
  2772. struct drbd_conf *mdev = minor_to_mdev(minor);
  2773. if (!mdev)
  2774. return;
  2775. /* paranoia asserts */
  2776. if (mdev->open_cnt != 0)
  2777. dev_err(DEV, "open_cnt = %d in %s:%u", mdev->open_cnt,
  2778. __FILE__ , __LINE__);
  2779. ERR_IF (!list_empty(&mdev->data.work.q)) {
  2780. struct list_head *lp;
  2781. list_for_each(lp, &mdev->data.work.q) {
  2782. dev_err(DEV, "lp = %p\n", lp);
  2783. }
  2784. };
  2785. /* end paranoia asserts */
  2786. del_gendisk(mdev->vdisk);
  2787. /* cleanup stuff that may have been allocated during
  2788. * device (re-)configuration or state changes */
  2789. if (mdev->this_bdev)
  2790. bdput(mdev->this_bdev);
  2791. drbd_free_resources(mdev);
  2792. drbd_release_ee_lists(mdev);
  2793. /* should be freed on disconnect? */
  2794. kfree(mdev->ee_hash);
  2795. /*
  2796. mdev->ee_hash_s = 0;
  2797. mdev->ee_hash = NULL;
  2798. */
  2799. lc_destroy(mdev->act_log);
  2800. lc_destroy(mdev->resync);
  2801. kfree(mdev->p_uuid);
  2802. /* mdev->p_uuid = NULL; */
  2803. kfree(mdev->int_dig_out);
  2804. kfree(mdev->int_dig_in);
  2805. kfree(mdev->int_dig_vv);
  2806. /* cleanup the rest that has been
  2807. * allocated from drbd_new_device
  2808. * and actually free the mdev itself */
  2809. drbd_free_mdev(mdev);
  2810. }
  2811. static void drbd_cleanup(void)
  2812. {
  2813. unsigned int i;
  2814. unregister_reboot_notifier(&drbd_notifier);
  2815. /* first remove proc,
  2816. * drbdsetup uses it's presence to detect
  2817. * whether DRBD is loaded.
  2818. * If we would get stuck in proc removal,
  2819. * but have netlink already deregistered,
  2820. * some drbdsetup commands may wait forever
  2821. * for an answer.
  2822. */
  2823. if (drbd_proc)
  2824. remove_proc_entry("drbd", NULL);
  2825. drbd_nl_cleanup();
  2826. if (minor_table) {
  2827. i = minor_count;
  2828. while (i--)
  2829. drbd_delete_device(i);
  2830. drbd_destroy_mempools();
  2831. }
  2832. kfree(minor_table);
  2833. unregister_blkdev(DRBD_MAJOR, "drbd");
  2834. printk(KERN_INFO "drbd: module cleanup done.\n");
  2835. }
  2836. /**
  2837. * drbd_congested() - Callback for pdflush
  2838. * @congested_data: User data
  2839. * @bdi_bits: Bits pdflush is currently interested in
  2840. *
  2841. * Returns 1<<BDI_async_congested and/or 1<<BDI_sync_congested if we are congested.
  2842. */
  2843. static int drbd_congested(void *congested_data, int bdi_bits)
  2844. {
  2845. struct drbd_conf *mdev = congested_data;
  2846. struct request_queue *q;
  2847. char reason = '-';
  2848. int r = 0;
  2849. if (!may_inc_ap_bio(mdev)) {
  2850. /* DRBD has frozen IO */
  2851. r = bdi_bits;
  2852. reason = 'd';
  2853. goto out;
  2854. }
  2855. if (get_ldev(mdev)) {
  2856. q = bdev_get_queue(mdev->ldev->backing_bdev);
  2857. r = bdi_congested(&q->backing_dev_info, bdi_bits);
  2858. put_ldev(mdev);
  2859. if (r)
  2860. reason = 'b';
  2861. }
  2862. if (bdi_bits & (1 << BDI_async_congested) && test_bit(NET_CONGESTED, &mdev->flags)) {
  2863. r |= (1 << BDI_async_congested);
  2864. reason = reason == 'b' ? 'a' : 'n';
  2865. }
  2866. out:
  2867. mdev->congestion_reason = reason;
  2868. return r;
  2869. }
  2870. struct drbd_conf *drbd_new_device(unsigned int minor)
  2871. {
  2872. struct drbd_conf *mdev;
  2873. struct gendisk *disk;
  2874. struct request_queue *q;
  2875. /* GFP_KERNEL, we are outside of all write-out paths */
  2876. mdev = kzalloc(sizeof(struct drbd_conf), GFP_KERNEL);
  2877. if (!mdev)
  2878. return NULL;
  2879. if (!zalloc_cpumask_var(&mdev->cpu_mask, GFP_KERNEL))
  2880. goto out_no_cpumask;
  2881. mdev->minor = minor;
  2882. drbd_init_set_defaults(mdev);
  2883. q = blk_alloc_queue(GFP_KERNEL);
  2884. if (!q)
  2885. goto out_no_q;
  2886. mdev->rq_queue = q;
  2887. q->queuedata = mdev;
  2888. disk = alloc_disk(1);
  2889. if (!disk)
  2890. goto out_no_disk;
  2891. mdev->vdisk = disk;
  2892. set_disk_ro(disk, true);
  2893. disk->queue = q;
  2894. disk->major = DRBD_MAJOR;
  2895. disk->first_minor = minor;
  2896. disk->fops = &drbd_ops;
  2897. sprintf(disk->disk_name, "drbd%d", minor);
  2898. disk->private_data = mdev;
  2899. mdev->this_bdev = bdget(MKDEV(DRBD_MAJOR, minor));
  2900. /* we have no partitions. we contain only ourselves. */
  2901. mdev->this_bdev->bd_contains = mdev->this_bdev;
  2902. q->backing_dev_info.congested_fn = drbd_congested;
  2903. q->backing_dev_info.congested_data = mdev;
  2904. blk_queue_make_request(q, drbd_make_request);
  2905. /* Setting the max_hw_sectors to an odd value of 8kibyte here
  2906. This triggers a max_bio_size message upon first attach or connect */
  2907. blk_queue_max_hw_sectors(q, DRBD_MAX_BIO_SIZE_SAFE >> 8);
  2908. blk_queue_bounce_limit(q, BLK_BOUNCE_ANY);
  2909. blk_queue_merge_bvec(q, drbd_merge_bvec);
  2910. q->queue_lock = &mdev->req_lock;
  2911. mdev->md_io_page = alloc_page(GFP_KERNEL);
  2912. if (!mdev->md_io_page)
  2913. goto out_no_io_page;
  2914. if (drbd_bm_init(mdev))
  2915. goto out_no_bitmap;
  2916. /* no need to lock access, we are still initializing this minor device. */
  2917. if (!tl_init(mdev))
  2918. goto out_no_tl;
  2919. mdev->app_reads_hash = kzalloc(APP_R_HSIZE*sizeof(void *), GFP_KERNEL);
  2920. if (!mdev->app_reads_hash)
  2921. goto out_no_app_reads;
  2922. mdev->current_epoch = kzalloc(sizeof(struct drbd_epoch), GFP_KERNEL);
  2923. if (!mdev->current_epoch)
  2924. goto out_no_epoch;
  2925. INIT_LIST_HEAD(&mdev->current_epoch->list);
  2926. mdev->epochs = 1;
  2927. return mdev;
  2928. /* out_whatever_else:
  2929. kfree(mdev->current_epoch); */
  2930. out_no_epoch:
  2931. kfree(mdev->app_reads_hash);
  2932. out_no_app_reads:
  2933. tl_cleanup(mdev);
  2934. out_no_tl:
  2935. drbd_bm_cleanup(mdev);
  2936. out_no_bitmap:
  2937. __free_page(mdev->md_io_page);
  2938. out_no_io_page:
  2939. put_disk(disk);
  2940. out_no_disk:
  2941. blk_cleanup_queue(q);
  2942. out_no_q:
  2943. free_cpumask_var(mdev->cpu_mask);
  2944. out_no_cpumask:
  2945. kfree(mdev);
  2946. return NULL;
  2947. }
  2948. /* counterpart of drbd_new_device.
  2949. * last part of drbd_delete_device. */
  2950. void drbd_free_mdev(struct drbd_conf *mdev)
  2951. {
  2952. kfree(mdev->current_epoch);
  2953. kfree(mdev->app_reads_hash);
  2954. tl_cleanup(mdev);
  2955. if (mdev->bitmap) /* should no longer be there. */
  2956. drbd_bm_cleanup(mdev);
  2957. __free_page(mdev->md_io_page);
  2958. put_disk(mdev->vdisk);
  2959. blk_cleanup_queue(mdev->rq_queue);
  2960. free_cpumask_var(mdev->cpu_mask);
  2961. drbd_free_tl_hash(mdev);
  2962. kfree(mdev);
  2963. }
  2964. int __init drbd_init(void)
  2965. {
  2966. int err;
  2967. if (sizeof(struct p_handshake) != 80) {
  2968. printk(KERN_ERR
  2969. "drbd: never change the size or layout "
  2970. "of the HandShake packet.\n");
  2971. return -EINVAL;
  2972. }
  2973. if (minor_count < DRBD_MINOR_COUNT_MIN || minor_count > DRBD_MINOR_COUNT_MAX) {
  2974. printk(KERN_ERR
  2975. "drbd: invalid minor_count (%d)\n", minor_count);
  2976. #ifdef MODULE
  2977. return -EINVAL;
  2978. #else
  2979. minor_count = 8;
  2980. #endif
  2981. }
  2982. err = drbd_nl_init();
  2983. if (err)
  2984. return err;
  2985. err = register_blkdev(DRBD_MAJOR, "drbd");
  2986. if (err) {
  2987. printk(KERN_ERR
  2988. "drbd: unable to register block device major %d\n",
  2989. DRBD_MAJOR);
  2990. return err;
  2991. }
  2992. register_reboot_notifier(&drbd_notifier);
  2993. /*
  2994. * allocate all necessary structs
  2995. */
  2996. err = -ENOMEM;
  2997. init_waitqueue_head(&drbd_pp_wait);
  2998. drbd_proc = NULL; /* play safe for drbd_cleanup */
  2999. minor_table = kzalloc(sizeof(struct drbd_conf *)*minor_count,
  3000. GFP_KERNEL);
  3001. if (!minor_table)
  3002. goto Enomem;
  3003. err = drbd_create_mempools();
  3004. if (err)
  3005. goto Enomem;
  3006. drbd_proc = proc_create_data("drbd", S_IFREG | S_IRUGO , NULL, &drbd_proc_fops, NULL);
  3007. if (!drbd_proc) {
  3008. printk(KERN_ERR "drbd: unable to register proc file\n");
  3009. goto Enomem;
  3010. }
  3011. rwlock_init(&global_state_lock);
  3012. printk(KERN_INFO "drbd: initialized. "
  3013. "Version: " REL_VERSION " (api:%d/proto:%d-%d)\n",
  3014. API_VERSION, PRO_VERSION_MIN, PRO_VERSION_MAX);
  3015. printk(KERN_INFO "drbd: %s\n", drbd_buildtag());
  3016. printk(KERN_INFO "drbd: registered as block device major %d\n",
  3017. DRBD_MAJOR);
  3018. printk(KERN_INFO "drbd: minor_table @ 0x%p\n", minor_table);
  3019. return 0; /* Success! */
  3020. Enomem:
  3021. drbd_cleanup();
  3022. if (err == -ENOMEM)
  3023. /* currently always the case */
  3024. printk(KERN_ERR "drbd: ran out of memory\n");
  3025. else
  3026. printk(KERN_ERR "drbd: initialization failure\n");
  3027. return err;
  3028. }
  3029. void drbd_free_bc(struct drbd_backing_dev *ldev)
  3030. {
  3031. if (ldev == NULL)
  3032. return;
  3033. blkdev_put(ldev->backing_bdev, FMODE_READ | FMODE_WRITE | FMODE_EXCL);
  3034. blkdev_put(ldev->md_bdev, FMODE_READ | FMODE_WRITE | FMODE_EXCL);
  3035. kfree(ldev);
  3036. }
  3037. void drbd_free_sock(struct drbd_conf *mdev)
  3038. {
  3039. if (mdev->data.socket) {
  3040. mutex_lock(&mdev->data.mutex);
  3041. kernel_sock_shutdown(mdev->data.socket, SHUT_RDWR);
  3042. sock_release(mdev->data.socket);
  3043. mdev->data.socket = NULL;
  3044. mutex_unlock(&mdev->data.mutex);
  3045. }
  3046. if (mdev->meta.socket) {
  3047. mutex_lock(&mdev->meta.mutex);
  3048. kernel_sock_shutdown(mdev->meta.socket, SHUT_RDWR);
  3049. sock_release(mdev->meta.socket);
  3050. mdev->meta.socket = NULL;
  3051. mutex_unlock(&mdev->meta.mutex);
  3052. }
  3053. }
  3054. void drbd_free_resources(struct drbd_conf *mdev)
  3055. {
  3056. crypto_free_hash(mdev->csums_tfm);
  3057. mdev->csums_tfm = NULL;
  3058. crypto_free_hash(mdev->verify_tfm);
  3059. mdev->verify_tfm = NULL;
  3060. crypto_free_hash(mdev->cram_hmac_tfm);
  3061. mdev->cram_hmac_tfm = NULL;
  3062. crypto_free_hash(mdev->integrity_w_tfm);
  3063. mdev->integrity_w_tfm = NULL;
  3064. crypto_free_hash(mdev->integrity_r_tfm);
  3065. mdev->integrity_r_tfm = NULL;
  3066. drbd_free_sock(mdev);
  3067. __no_warn(local,
  3068. drbd_free_bc(mdev->ldev);
  3069. mdev->ldev = NULL;);
  3070. }
  3071. /* meta data management */
  3072. struct meta_data_on_disk {
  3073. u64 la_size; /* last agreed size. */
  3074. u64 uuid[UI_SIZE]; /* UUIDs. */
  3075. u64 device_uuid;
  3076. u64 reserved_u64_1;
  3077. u32 flags; /* MDF */
  3078. u32 magic;
  3079. u32 md_size_sect;
  3080. u32 al_offset; /* offset to this block */
  3081. u32 al_nr_extents; /* important for restoring the AL */
  3082. /* `-- act_log->nr_elements <-- sync_conf.al_extents */
  3083. u32 bm_offset; /* offset to the bitmap, from here */
  3084. u32 bm_bytes_per_bit; /* BM_BLOCK_SIZE */
  3085. u32 la_peer_max_bio_size; /* last peer max_bio_size */
  3086. u32 reserved_u32[3];
  3087. } __packed;
  3088. /**
  3089. * drbd_md_sync() - Writes the meta data super block if the MD_DIRTY flag bit is set
  3090. * @mdev: DRBD device.
  3091. */
  3092. void drbd_md_sync(struct drbd_conf *mdev)
  3093. {
  3094. struct meta_data_on_disk *buffer;
  3095. sector_t sector;
  3096. int i;
  3097. del_timer(&mdev->md_sync_timer);
  3098. /* timer may be rearmed by drbd_md_mark_dirty() now. */
  3099. if (!test_and_clear_bit(MD_DIRTY, &mdev->flags))
  3100. return;
  3101. /* We use here D_FAILED and not D_ATTACHING because we try to write
  3102. * metadata even if we detach due to a disk failure! */
  3103. if (!get_ldev_if_state(mdev, D_FAILED))
  3104. return;
  3105. mutex_lock(&mdev->md_io_mutex);
  3106. buffer = (struct meta_data_on_disk *)page_address(mdev->md_io_page);
  3107. memset(buffer, 0, 512);
  3108. buffer->la_size = cpu_to_be64(drbd_get_capacity(mdev->this_bdev));
  3109. for (i = UI_CURRENT; i < UI_SIZE; i++)
  3110. buffer->uuid[i] = cpu_to_be64(mdev->ldev->md.uuid[i]);
  3111. buffer->flags = cpu_to_be32(mdev->ldev->md.flags);
  3112. buffer->magic = cpu_to_be32(DRBD_MD_MAGIC);
  3113. buffer->md_size_sect = cpu_to_be32(mdev->ldev->md.md_size_sect);
  3114. buffer->al_offset = cpu_to_be32(mdev->ldev->md.al_offset);
  3115. buffer->al_nr_extents = cpu_to_be32(mdev->act_log->nr_elements);
  3116. buffer->bm_bytes_per_bit = cpu_to_be32(BM_BLOCK_SIZE);
  3117. buffer->device_uuid = cpu_to_be64(mdev->ldev->md.device_uuid);
  3118. buffer->bm_offset = cpu_to_be32(mdev->ldev->md.bm_offset);
  3119. buffer->la_peer_max_bio_size = cpu_to_be32(mdev->peer_max_bio_size);
  3120. D_ASSERT(drbd_md_ss__(mdev, mdev->ldev) == mdev->ldev->md.md_offset);
  3121. sector = mdev->ldev->md.md_offset;
  3122. if (!drbd_md_sync_page_io(mdev, mdev->ldev, sector, WRITE)) {
  3123. /* this was a try anyways ... */
  3124. dev_err(DEV, "meta data update failed!\n");
  3125. drbd_chk_io_error(mdev, 1, true);
  3126. }
  3127. /* Update mdev->ldev->md.la_size_sect,
  3128. * since we updated it on metadata. */
  3129. mdev->ldev->md.la_size_sect = drbd_get_capacity(mdev->this_bdev);
  3130. mutex_unlock(&mdev->md_io_mutex);
  3131. put_ldev(mdev);
  3132. }
  3133. /**
  3134. * drbd_md_read() - Reads in the meta data super block
  3135. * @mdev: DRBD device.
  3136. * @bdev: Device from which the meta data should be read in.
  3137. *
  3138. * Return 0 (NO_ERROR) on success, and an enum drbd_ret_code in case
  3139. * something goes wrong. Currently only: ERR_IO_MD_DISK, ERR_MD_INVALID.
  3140. */
  3141. int drbd_md_read(struct drbd_conf *mdev, struct drbd_backing_dev *bdev)
  3142. {
  3143. struct meta_data_on_disk *buffer;
  3144. int i, rv = NO_ERROR;
  3145. if (!get_ldev_if_state(mdev, D_ATTACHING))
  3146. return ERR_IO_MD_DISK;
  3147. mutex_lock(&mdev->md_io_mutex);
  3148. buffer = (struct meta_data_on_disk *)page_address(mdev->md_io_page);
  3149. if (!drbd_md_sync_page_io(mdev, bdev, bdev->md.md_offset, READ)) {
  3150. /* NOTE: can't do normal error processing here as this is
  3151. called BEFORE disk is attached */
  3152. dev_err(DEV, "Error while reading metadata.\n");
  3153. rv = ERR_IO_MD_DISK;
  3154. goto err;
  3155. }
  3156. if (be32_to_cpu(buffer->magic) != DRBD_MD_MAGIC) {
  3157. dev_err(DEV, "Error while reading metadata, magic not found.\n");
  3158. rv = ERR_MD_INVALID;
  3159. goto err;
  3160. }
  3161. if (be32_to_cpu(buffer->al_offset) != bdev->md.al_offset) {
  3162. dev_err(DEV, "unexpected al_offset: %d (expected %d)\n",
  3163. be32_to_cpu(buffer->al_offset), bdev->md.al_offset);
  3164. rv = ERR_MD_INVALID;
  3165. goto err;
  3166. }
  3167. if (be32_to_cpu(buffer->bm_offset) != bdev->md.bm_offset) {
  3168. dev_err(DEV, "unexpected bm_offset: %d (expected %d)\n",
  3169. be32_to_cpu(buffer->bm_offset), bdev->md.bm_offset);
  3170. rv = ERR_MD_INVALID;
  3171. goto err;
  3172. }
  3173. if (be32_to_cpu(buffer->md_size_sect) != bdev->md.md_size_sect) {
  3174. dev_err(DEV, "unexpected md_size: %u (expected %u)\n",
  3175. be32_to_cpu(buffer->md_size_sect), bdev->md.md_size_sect);
  3176. rv = ERR_MD_INVALID;
  3177. goto err;
  3178. }
  3179. if (be32_to_cpu(buffer->bm_bytes_per_bit) != BM_BLOCK_SIZE) {
  3180. dev_err(DEV, "unexpected bm_bytes_per_bit: %u (expected %u)\n",
  3181. be32_to_cpu(buffer->bm_bytes_per_bit), BM_BLOCK_SIZE);
  3182. rv = ERR_MD_INVALID;
  3183. goto err;
  3184. }
  3185. bdev->md.la_size_sect = be64_to_cpu(buffer->la_size);
  3186. for (i = UI_CURRENT; i < UI_SIZE; i++)
  3187. bdev->md.uuid[i] = be64_to_cpu(buffer->uuid[i]);
  3188. bdev->md.flags = be32_to_cpu(buffer->flags);
  3189. mdev->sync_conf.al_extents = be32_to_cpu(buffer->al_nr_extents);
  3190. bdev->md.device_uuid = be64_to_cpu(buffer->device_uuid);
  3191. spin_lock_irq(&mdev->req_lock);
  3192. if (mdev->state.conn < C_CONNECTED) {
  3193. int peer;
  3194. peer = be32_to_cpu(buffer->la_peer_max_bio_size);
  3195. peer = max_t(int, peer, DRBD_MAX_BIO_SIZE_SAFE);
  3196. mdev->peer_max_bio_size = peer;
  3197. }
  3198. spin_unlock_irq(&mdev->req_lock);
  3199. if (mdev->sync_conf.al_extents < 7)
  3200. mdev->sync_conf.al_extents = 127;
  3201. err:
  3202. mutex_unlock(&mdev->md_io_mutex);
  3203. put_ldev(mdev);
  3204. return rv;
  3205. }
  3206. /**
  3207. * drbd_md_mark_dirty() - Mark meta data super block as dirty
  3208. * @mdev: DRBD device.
  3209. *
  3210. * Call this function if you change anything that should be written to
  3211. * the meta-data super block. This function sets MD_DIRTY, and starts a
  3212. * timer that ensures that within five seconds you have to call drbd_md_sync().
  3213. */
  3214. #ifdef DEBUG
  3215. void drbd_md_mark_dirty_(struct drbd_conf *mdev, unsigned int line, const char *func)
  3216. {
  3217. if (!test_and_set_bit(MD_DIRTY, &mdev->flags)) {
  3218. mod_timer(&mdev->md_sync_timer, jiffies + HZ);
  3219. mdev->last_md_mark_dirty.line = line;
  3220. mdev->last_md_mark_dirty.func = func;
  3221. }
  3222. }
  3223. #else
  3224. void drbd_md_mark_dirty(struct drbd_conf *mdev)
  3225. {
  3226. if (!test_and_set_bit(MD_DIRTY, &mdev->flags))
  3227. mod_timer(&mdev->md_sync_timer, jiffies + 5*HZ);
  3228. }
  3229. #endif
  3230. static void drbd_uuid_move_history(struct drbd_conf *mdev) __must_hold(local)
  3231. {
  3232. int i;
  3233. for (i = UI_HISTORY_START; i < UI_HISTORY_END; i++)
  3234. mdev->ldev->md.uuid[i+1] = mdev->ldev->md.uuid[i];
  3235. }
  3236. void _drbd_uuid_set(struct drbd_conf *mdev, int idx, u64 val) __must_hold(local)
  3237. {
  3238. if (idx == UI_CURRENT) {
  3239. if (mdev->state.role == R_PRIMARY)
  3240. val |= 1;
  3241. else
  3242. val &= ~((u64)1);
  3243. drbd_set_ed_uuid(mdev, val);
  3244. }
  3245. mdev->ldev->md.uuid[idx] = val;
  3246. drbd_md_mark_dirty(mdev);
  3247. }
  3248. void drbd_uuid_set(struct drbd_conf *mdev, int idx, u64 val) __must_hold(local)
  3249. {
  3250. if (mdev->ldev->md.uuid[idx]) {
  3251. drbd_uuid_move_history(mdev);
  3252. mdev->ldev->md.uuid[UI_HISTORY_START] = mdev->ldev->md.uuid[idx];
  3253. }
  3254. _drbd_uuid_set(mdev, idx, val);
  3255. }
  3256. /**
  3257. * drbd_uuid_new_current() - Creates a new current UUID
  3258. * @mdev: DRBD device.
  3259. *
  3260. * Creates a new current UUID, and rotates the old current UUID into
  3261. * the bitmap slot. Causes an incremental resync upon next connect.
  3262. */
  3263. void drbd_uuid_new_current(struct drbd_conf *mdev) __must_hold(local)
  3264. {
  3265. u64 val;
  3266. unsigned long long bm_uuid = mdev->ldev->md.uuid[UI_BITMAP];
  3267. if (bm_uuid)
  3268. dev_warn(DEV, "bm UUID was already set: %llX\n", bm_uuid);
  3269. mdev->ldev->md.uuid[UI_BITMAP] = mdev->ldev->md.uuid[UI_CURRENT];
  3270. get_random_bytes(&val, sizeof(u64));
  3271. _drbd_uuid_set(mdev, UI_CURRENT, val);
  3272. drbd_print_uuids(mdev, "new current UUID");
  3273. /* get it to stable storage _now_ */
  3274. drbd_md_sync(mdev);
  3275. }
  3276. void drbd_uuid_set_bm(struct drbd_conf *mdev, u64 val) __must_hold(local)
  3277. {
  3278. if (mdev->ldev->md.uuid[UI_BITMAP] == 0 && val == 0)
  3279. return;
  3280. if (val == 0) {
  3281. drbd_uuid_move_history(mdev);
  3282. mdev->ldev->md.uuid[UI_HISTORY_START] = mdev->ldev->md.uuid[UI_BITMAP];
  3283. mdev->ldev->md.uuid[UI_BITMAP] = 0;
  3284. } else {
  3285. unsigned long long bm_uuid = mdev->ldev->md.uuid[UI_BITMAP];
  3286. if (bm_uuid)
  3287. dev_warn(DEV, "bm UUID was already set: %llX\n", bm_uuid);
  3288. mdev->ldev->md.uuid[UI_BITMAP] = val & ~((u64)1);
  3289. }
  3290. drbd_md_mark_dirty(mdev);
  3291. }
  3292. /**
  3293. * drbd_bmio_set_n_write() - io_fn for drbd_queue_bitmap_io() or drbd_bitmap_io()
  3294. * @mdev: DRBD device.
  3295. *
  3296. * Sets all bits in the bitmap and writes the whole bitmap to stable storage.
  3297. */
  3298. int drbd_bmio_set_n_write(struct drbd_conf *mdev)
  3299. {
  3300. int rv = -EIO;
  3301. if (get_ldev_if_state(mdev, D_ATTACHING)) {
  3302. drbd_md_set_flag(mdev, MDF_FULL_SYNC);
  3303. drbd_md_sync(mdev);
  3304. drbd_bm_set_all(mdev);
  3305. rv = drbd_bm_write(mdev);
  3306. if (!rv) {
  3307. drbd_md_clear_flag(mdev, MDF_FULL_SYNC);
  3308. drbd_md_sync(mdev);
  3309. }
  3310. put_ldev(mdev);
  3311. }
  3312. return rv;
  3313. }
  3314. /**
  3315. * drbd_bmio_clear_n_write() - io_fn for drbd_queue_bitmap_io() or drbd_bitmap_io()
  3316. * @mdev: DRBD device.
  3317. *
  3318. * Clears all bits in the bitmap and writes the whole bitmap to stable storage.
  3319. */
  3320. int drbd_bmio_clear_n_write(struct drbd_conf *mdev)
  3321. {
  3322. int rv = -EIO;
  3323. drbd_resume_al(mdev);
  3324. if (get_ldev_if_state(mdev, D_ATTACHING)) {
  3325. drbd_bm_clear_all(mdev);
  3326. rv = drbd_bm_write(mdev);
  3327. put_ldev(mdev);
  3328. }
  3329. return rv;
  3330. }
  3331. static int w_bitmap_io(struct drbd_conf *mdev, struct drbd_work *w, int unused)
  3332. {
  3333. struct bm_io_work *work = container_of(w, struct bm_io_work, w);
  3334. int rv = -EIO;
  3335. D_ASSERT(atomic_read(&mdev->ap_bio_cnt) == 0);
  3336. if (get_ldev(mdev)) {
  3337. drbd_bm_lock(mdev, work->why, work->flags);
  3338. rv = work->io_fn(mdev);
  3339. drbd_bm_unlock(mdev);
  3340. put_ldev(mdev);
  3341. }
  3342. clear_bit(BITMAP_IO, &mdev->flags);
  3343. smp_mb__after_clear_bit();
  3344. wake_up(&mdev->misc_wait);
  3345. if (work->done)
  3346. work->done(mdev, rv);
  3347. clear_bit(BITMAP_IO_QUEUED, &mdev->flags);
  3348. work->why = NULL;
  3349. work->flags = 0;
  3350. return 1;
  3351. }
  3352. void drbd_ldev_destroy(struct drbd_conf *mdev)
  3353. {
  3354. lc_destroy(mdev->resync);
  3355. mdev->resync = NULL;
  3356. lc_destroy(mdev->act_log);
  3357. mdev->act_log = NULL;
  3358. __no_warn(local,
  3359. drbd_free_bc(mdev->ldev);
  3360. mdev->ldev = NULL;);
  3361. if (mdev->md_io_tmpp) {
  3362. __free_page(mdev->md_io_tmpp);
  3363. mdev->md_io_tmpp = NULL;
  3364. }
  3365. clear_bit(GO_DISKLESS, &mdev->flags);
  3366. }
  3367. static int w_go_diskless(struct drbd_conf *mdev, struct drbd_work *w, int unused)
  3368. {
  3369. D_ASSERT(mdev->state.disk == D_FAILED);
  3370. /* we cannot assert local_cnt == 0 here, as get_ldev_if_state will
  3371. * inc/dec it frequently. Once we are D_DISKLESS, no one will touch
  3372. * the protected members anymore, though, so once put_ldev reaches zero
  3373. * again, it will be safe to free them. */
  3374. drbd_force_state(mdev, NS(disk, D_DISKLESS));
  3375. return 1;
  3376. }
  3377. void drbd_go_diskless(struct drbd_conf *mdev)
  3378. {
  3379. D_ASSERT(mdev->state.disk == D_FAILED);
  3380. if (!test_and_set_bit(GO_DISKLESS, &mdev->flags))
  3381. drbd_queue_work(&mdev->data.work, &mdev->go_diskless);
  3382. }
  3383. /**
  3384. * drbd_queue_bitmap_io() - Queues an IO operation on the whole bitmap
  3385. * @mdev: DRBD device.
  3386. * @io_fn: IO callback to be called when bitmap IO is possible
  3387. * @done: callback to be called after the bitmap IO was performed
  3388. * @why: Descriptive text of the reason for doing the IO
  3389. *
  3390. * While IO on the bitmap happens we freeze application IO thus we ensure
  3391. * that drbd_set_out_of_sync() can not be called. This function MAY ONLY be
  3392. * called from worker context. It MUST NOT be used while a previous such
  3393. * work is still pending!
  3394. */
  3395. void drbd_queue_bitmap_io(struct drbd_conf *mdev,
  3396. int (*io_fn)(struct drbd_conf *),
  3397. void (*done)(struct drbd_conf *, int),
  3398. char *why, enum bm_flag flags)
  3399. {
  3400. D_ASSERT(current == mdev->worker.task);
  3401. D_ASSERT(!test_bit(BITMAP_IO_QUEUED, &mdev->flags));
  3402. D_ASSERT(!test_bit(BITMAP_IO, &mdev->flags));
  3403. D_ASSERT(list_empty(&mdev->bm_io_work.w.list));
  3404. if (mdev->bm_io_work.why)
  3405. dev_err(DEV, "FIXME going to queue '%s' but '%s' still pending?\n",
  3406. why, mdev->bm_io_work.why);
  3407. mdev->bm_io_work.io_fn = io_fn;
  3408. mdev->bm_io_work.done = done;
  3409. mdev->bm_io_work.why = why;
  3410. mdev->bm_io_work.flags = flags;
  3411. spin_lock_irq(&mdev->req_lock);
  3412. set_bit(BITMAP_IO, &mdev->flags);
  3413. if (atomic_read(&mdev->ap_bio_cnt) == 0) {
  3414. if (!test_and_set_bit(BITMAP_IO_QUEUED, &mdev->flags))
  3415. drbd_queue_work(&mdev->data.work, &mdev->bm_io_work.w);
  3416. }
  3417. spin_unlock_irq(&mdev->req_lock);
  3418. }
  3419. /**
  3420. * drbd_bitmap_io() - Does an IO operation on the whole bitmap
  3421. * @mdev: DRBD device.
  3422. * @io_fn: IO callback to be called when bitmap IO is possible
  3423. * @why: Descriptive text of the reason for doing the IO
  3424. *
  3425. * freezes application IO while that the actual IO operations runs. This
  3426. * functions MAY NOT be called from worker context.
  3427. */
  3428. int drbd_bitmap_io(struct drbd_conf *mdev, int (*io_fn)(struct drbd_conf *),
  3429. char *why, enum bm_flag flags)
  3430. {
  3431. int rv;
  3432. D_ASSERT(current != mdev->worker.task);
  3433. if ((flags & BM_LOCKED_SET_ALLOWED) == 0)
  3434. drbd_suspend_io(mdev);
  3435. drbd_bm_lock(mdev, why, flags);
  3436. rv = io_fn(mdev);
  3437. drbd_bm_unlock(mdev);
  3438. if ((flags & BM_LOCKED_SET_ALLOWED) == 0)
  3439. drbd_resume_io(mdev);
  3440. return rv;
  3441. }
  3442. void drbd_md_set_flag(struct drbd_conf *mdev, int flag) __must_hold(local)
  3443. {
  3444. if ((mdev->ldev->md.flags & flag) != flag) {
  3445. drbd_md_mark_dirty(mdev);
  3446. mdev->ldev->md.flags |= flag;
  3447. }
  3448. }
  3449. void drbd_md_clear_flag(struct drbd_conf *mdev, int flag) __must_hold(local)
  3450. {
  3451. if ((mdev->ldev->md.flags & flag) != 0) {
  3452. drbd_md_mark_dirty(mdev);
  3453. mdev->ldev->md.flags &= ~flag;
  3454. }
  3455. }
  3456. int drbd_md_test_flag(struct drbd_backing_dev *bdev, int flag)
  3457. {
  3458. return (bdev->md.flags & flag) != 0;
  3459. }
  3460. static void md_sync_timer_fn(unsigned long data)
  3461. {
  3462. struct drbd_conf *mdev = (struct drbd_conf *) data;
  3463. drbd_queue_work_front(&mdev->data.work, &mdev->md_sync_work);
  3464. }
  3465. static int w_md_sync(struct drbd_conf *mdev, struct drbd_work *w, int unused)
  3466. {
  3467. dev_warn(DEV, "md_sync_timer expired! Worker calls drbd_md_sync().\n");
  3468. #ifdef DEBUG
  3469. dev_warn(DEV, "last md_mark_dirty: %s:%u\n",
  3470. mdev->last_md_mark_dirty.func, mdev->last_md_mark_dirty.line);
  3471. #endif
  3472. drbd_md_sync(mdev);
  3473. return 1;
  3474. }
  3475. #ifdef CONFIG_DRBD_FAULT_INJECTION
  3476. /* Fault insertion support including random number generator shamelessly
  3477. * stolen from kernel/rcutorture.c */
  3478. struct fault_random_state {
  3479. unsigned long state;
  3480. unsigned long count;
  3481. };
  3482. #define FAULT_RANDOM_MULT 39916801 /* prime */
  3483. #define FAULT_RANDOM_ADD 479001701 /* prime */
  3484. #define FAULT_RANDOM_REFRESH 10000
  3485. /*
  3486. * Crude but fast random-number generator. Uses a linear congruential
  3487. * generator, with occasional help from get_random_bytes().
  3488. */
  3489. static unsigned long
  3490. _drbd_fault_random(struct fault_random_state *rsp)
  3491. {
  3492. long refresh;
  3493. if (!rsp->count--) {
  3494. get_random_bytes(&refresh, sizeof(refresh));
  3495. rsp->state += refresh;
  3496. rsp->count = FAULT_RANDOM_REFRESH;
  3497. }
  3498. rsp->state = rsp->state * FAULT_RANDOM_MULT + FAULT_RANDOM_ADD;
  3499. return swahw32(rsp->state);
  3500. }
  3501. static char *
  3502. _drbd_fault_str(unsigned int type) {
  3503. static char *_faults[] = {
  3504. [DRBD_FAULT_MD_WR] = "Meta-data write",
  3505. [DRBD_FAULT_MD_RD] = "Meta-data read",
  3506. [DRBD_FAULT_RS_WR] = "Resync write",
  3507. [DRBD_FAULT_RS_RD] = "Resync read",
  3508. [DRBD_FAULT_DT_WR] = "Data write",
  3509. [DRBD_FAULT_DT_RD] = "Data read",
  3510. [DRBD_FAULT_DT_RA] = "Data read ahead",
  3511. [DRBD_FAULT_BM_ALLOC] = "BM allocation",
  3512. [DRBD_FAULT_AL_EE] = "EE allocation",
  3513. [DRBD_FAULT_RECEIVE] = "receive data corruption",
  3514. };
  3515. return (type < DRBD_FAULT_MAX) ? _faults[type] : "**Unknown**";
  3516. }
  3517. unsigned int
  3518. _drbd_insert_fault(struct drbd_conf *mdev, unsigned int type)
  3519. {
  3520. static struct fault_random_state rrs = {0, 0};
  3521. unsigned int ret = (
  3522. (fault_devs == 0 ||
  3523. ((1 << mdev_to_minor(mdev)) & fault_devs) != 0) &&
  3524. (((_drbd_fault_random(&rrs) % 100) + 1) <= fault_rate));
  3525. if (ret) {
  3526. fault_count++;
  3527. if (__ratelimit(&drbd_ratelimit_state))
  3528. dev_warn(DEV, "***Simulating %s failure\n",
  3529. _drbd_fault_str(type));
  3530. }
  3531. return ret;
  3532. }
  3533. #endif
  3534. const char *drbd_buildtag(void)
  3535. {
  3536. /* DRBD built from external sources has here a reference to the
  3537. git hash of the source code. */
  3538. static char buildtag[38] = "\0uilt-in";
  3539. if (buildtag[0] == 0) {
  3540. #ifdef CONFIG_MODULES
  3541. if (THIS_MODULE != NULL)
  3542. sprintf(buildtag, "srcversion: %-24s", THIS_MODULE->srcversion);
  3543. else
  3544. #endif
  3545. buildtag[0] = 'b';
  3546. }
  3547. return buildtag;
  3548. }
  3549. module_init(drbd_init)
  3550. module_exit(drbd_cleanup)
  3551. EXPORT_SYMBOL(drbd_conn_str);
  3552. EXPORT_SYMBOL(drbd_role_str);
  3553. EXPORT_SYMBOL(drbd_disk_str);
  3554. EXPORT_SYMBOL(drbd_set_st_err_str);