rbd.c 136 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330
  1. /*
  2. rbd.c -- Export ceph rados objects as a Linux block device
  3. based on drivers/block/osdblk.c:
  4. Copyright 2009 Red Hat, Inc.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; see the file COPYING. If not, write to
  14. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  15. For usage instructions, please refer to:
  16. Documentation/ABI/testing/sysfs-bus-rbd
  17. */
  18. #include <linux/ceph/libceph.h>
  19. #include <linux/ceph/osd_client.h>
  20. #include <linux/ceph/mon_client.h>
  21. #include <linux/ceph/decode.h>
  22. #include <linux/parser.h>
  23. #include <linux/bsearch.h>
  24. #include <linux/kernel.h>
  25. #include <linux/device.h>
  26. #include <linux/module.h>
  27. #include <linux/fs.h>
  28. #include <linux/blkdev.h>
  29. #include <linux/slab.h>
  30. #include "rbd_types.h"
  31. #define RBD_DEBUG /* Activate rbd_assert() calls */
  32. /*
  33. * The basic unit of block I/O is a sector. It is interpreted in a
  34. * number of contexts in Linux (blk, bio, genhd), but the default is
  35. * universally 512 bytes. These symbols are just slightly more
  36. * meaningful than the bare numbers they represent.
  37. */
  38. #define SECTOR_SHIFT 9
  39. #define SECTOR_SIZE (1ULL << SECTOR_SHIFT)
  40. /*
  41. * Increment the given counter and return its updated value.
  42. * If the counter is already 0 it will not be incremented.
  43. * If the counter is already at its maximum value returns
  44. * -EINVAL without updating it.
  45. */
  46. static int atomic_inc_return_safe(atomic_t *v)
  47. {
  48. unsigned int counter;
  49. counter = (unsigned int)__atomic_add_unless(v, 1, 0);
  50. if (counter <= (unsigned int)INT_MAX)
  51. return (int)counter;
  52. atomic_dec(v);
  53. return -EINVAL;
  54. }
  55. /* Decrement the counter. Return the resulting value, or -EINVAL */
  56. static int atomic_dec_return_safe(atomic_t *v)
  57. {
  58. int counter;
  59. counter = atomic_dec_return(v);
  60. if (counter >= 0)
  61. return counter;
  62. atomic_inc(v);
  63. return -EINVAL;
  64. }
  65. #define RBD_DRV_NAME "rbd"
  66. #define RBD_DRV_NAME_LONG "rbd (rados block device)"
  67. #define RBD_MINORS_PER_MAJOR 256 /* max minors per blkdev */
  68. #define RBD_SNAP_DEV_NAME_PREFIX "snap_"
  69. #define RBD_MAX_SNAP_NAME_LEN \
  70. (NAME_MAX - (sizeof (RBD_SNAP_DEV_NAME_PREFIX) - 1))
  71. #define RBD_MAX_SNAP_COUNT 510 /* allows max snapc to fit in 4KB */
  72. #define RBD_SNAP_HEAD_NAME "-"
  73. #define BAD_SNAP_INDEX U32_MAX /* invalid index into snap array */
  74. /* This allows a single page to hold an image name sent by OSD */
  75. #define RBD_IMAGE_NAME_LEN_MAX (PAGE_SIZE - sizeof (__le32) - 1)
  76. #define RBD_IMAGE_ID_LEN_MAX 64
  77. #define RBD_OBJ_PREFIX_LEN_MAX 64
  78. /* Feature bits */
  79. #define RBD_FEATURE_LAYERING (1<<0)
  80. #define RBD_FEATURE_STRIPINGV2 (1<<1)
  81. #define RBD_FEATURES_ALL \
  82. (RBD_FEATURE_LAYERING | RBD_FEATURE_STRIPINGV2)
  83. /* Features supported by this (client software) implementation. */
  84. #define RBD_FEATURES_SUPPORTED (RBD_FEATURES_ALL)
  85. /*
  86. * An RBD device name will be "rbd#", where the "rbd" comes from
  87. * RBD_DRV_NAME above, and # is a unique integer identifier.
  88. * MAX_INT_FORMAT_WIDTH is used in ensuring DEV_NAME_LEN is big
  89. * enough to hold all possible device names.
  90. */
  91. #define DEV_NAME_LEN 32
  92. #define MAX_INT_FORMAT_WIDTH ((5 * sizeof (int)) / 2 + 1)
  93. /*
  94. * block device image metadata (in-memory version)
  95. */
  96. struct rbd_image_header {
  97. /* These six fields never change for a given rbd image */
  98. char *object_prefix;
  99. __u8 obj_order;
  100. __u8 crypt_type;
  101. __u8 comp_type;
  102. u64 stripe_unit;
  103. u64 stripe_count;
  104. u64 features; /* Might be changeable someday? */
  105. /* The remaining fields need to be updated occasionally */
  106. u64 image_size;
  107. struct ceph_snap_context *snapc;
  108. char *snap_names; /* format 1 only */
  109. u64 *snap_sizes; /* format 1 only */
  110. };
  111. /*
  112. * An rbd image specification.
  113. *
  114. * The tuple (pool_id, image_id, snap_id) is sufficient to uniquely
  115. * identify an image. Each rbd_dev structure includes a pointer to
  116. * an rbd_spec structure that encapsulates this identity.
  117. *
  118. * Each of the id's in an rbd_spec has an associated name. For a
  119. * user-mapped image, the names are supplied and the id's associated
  120. * with them are looked up. For a layered image, a parent image is
  121. * defined by the tuple, and the names are looked up.
  122. *
  123. * An rbd_dev structure contains a parent_spec pointer which is
  124. * non-null if the image it represents is a child in a layered
  125. * image. This pointer will refer to the rbd_spec structure used
  126. * by the parent rbd_dev for its own identity (i.e., the structure
  127. * is shared between the parent and child).
  128. *
  129. * Since these structures are populated once, during the discovery
  130. * phase of image construction, they are effectively immutable so
  131. * we make no effort to synchronize access to them.
  132. *
  133. * Note that code herein does not assume the image name is known (it
  134. * could be a null pointer).
  135. */
  136. struct rbd_spec {
  137. u64 pool_id;
  138. const char *pool_name;
  139. const char *image_id;
  140. const char *image_name;
  141. u64 snap_id;
  142. const char *snap_name;
  143. struct kref kref;
  144. };
  145. /*
  146. * an instance of the client. multiple devices may share an rbd client.
  147. */
  148. struct rbd_client {
  149. struct ceph_client *client;
  150. struct kref kref;
  151. struct list_head node;
  152. };
  153. struct rbd_img_request;
  154. typedef void (*rbd_img_callback_t)(struct rbd_img_request *);
  155. #define BAD_WHICH U32_MAX /* Good which or bad which, which? */
  156. struct rbd_obj_request;
  157. typedef void (*rbd_obj_callback_t)(struct rbd_obj_request *);
  158. enum obj_request_type {
  159. OBJ_REQUEST_NODATA, OBJ_REQUEST_BIO, OBJ_REQUEST_PAGES
  160. };
  161. enum obj_req_flags {
  162. OBJ_REQ_DONE, /* completion flag: not done = 0, done = 1 */
  163. OBJ_REQ_IMG_DATA, /* object usage: standalone = 0, image = 1 */
  164. OBJ_REQ_KNOWN, /* EXISTS flag valid: no = 0, yes = 1 */
  165. OBJ_REQ_EXISTS, /* target exists: no = 0, yes = 1 */
  166. };
  167. struct rbd_obj_request {
  168. const char *object_name;
  169. u64 offset; /* object start byte */
  170. u64 length; /* bytes from offset */
  171. unsigned long flags;
  172. /*
  173. * An object request associated with an image will have its
  174. * img_data flag set; a standalone object request will not.
  175. *
  176. * A standalone object request will have which == BAD_WHICH
  177. * and a null obj_request pointer.
  178. *
  179. * An object request initiated in support of a layered image
  180. * object (to check for its existence before a write) will
  181. * have which == BAD_WHICH and a non-null obj_request pointer.
  182. *
  183. * Finally, an object request for rbd image data will have
  184. * which != BAD_WHICH, and will have a non-null img_request
  185. * pointer. The value of which will be in the range
  186. * 0..(img_request->obj_request_count-1).
  187. */
  188. union {
  189. struct rbd_obj_request *obj_request; /* STAT op */
  190. struct {
  191. struct rbd_img_request *img_request;
  192. u64 img_offset;
  193. /* links for img_request->obj_requests list */
  194. struct list_head links;
  195. };
  196. };
  197. u32 which; /* posn image request list */
  198. enum obj_request_type type;
  199. union {
  200. struct bio *bio_list;
  201. struct {
  202. struct page **pages;
  203. u32 page_count;
  204. };
  205. };
  206. struct page **copyup_pages;
  207. u32 copyup_page_count;
  208. struct ceph_osd_request *osd_req;
  209. u64 xferred; /* bytes transferred */
  210. int result;
  211. rbd_obj_callback_t callback;
  212. struct completion completion;
  213. struct kref kref;
  214. };
  215. enum img_req_flags {
  216. IMG_REQ_WRITE, /* I/O direction: read = 0, write = 1 */
  217. IMG_REQ_CHILD, /* initiator: block = 0, child image = 1 */
  218. IMG_REQ_LAYERED, /* ENOENT handling: normal = 0, layered = 1 */
  219. };
  220. struct rbd_img_request {
  221. struct rbd_device *rbd_dev;
  222. u64 offset; /* starting image byte offset */
  223. u64 length; /* byte count from offset */
  224. unsigned long flags;
  225. union {
  226. u64 snap_id; /* for reads */
  227. struct ceph_snap_context *snapc; /* for writes */
  228. };
  229. union {
  230. struct request *rq; /* block request */
  231. struct rbd_obj_request *obj_request; /* obj req initiator */
  232. };
  233. struct page **copyup_pages;
  234. u32 copyup_page_count;
  235. spinlock_t completion_lock;/* protects next_completion */
  236. u32 next_completion;
  237. rbd_img_callback_t callback;
  238. u64 xferred;/* aggregate bytes transferred */
  239. int result; /* first nonzero obj_request result */
  240. u32 obj_request_count;
  241. struct list_head obj_requests; /* rbd_obj_request structs */
  242. struct kref kref;
  243. };
  244. #define for_each_obj_request(ireq, oreq) \
  245. list_for_each_entry(oreq, &(ireq)->obj_requests, links)
  246. #define for_each_obj_request_from(ireq, oreq) \
  247. list_for_each_entry_from(oreq, &(ireq)->obj_requests, links)
  248. #define for_each_obj_request_safe(ireq, oreq, n) \
  249. list_for_each_entry_safe_reverse(oreq, n, &(ireq)->obj_requests, links)
  250. struct rbd_mapping {
  251. u64 size;
  252. u64 features;
  253. bool read_only;
  254. };
  255. /*
  256. * a single device
  257. */
  258. struct rbd_device {
  259. int dev_id; /* blkdev unique id */
  260. int major; /* blkdev assigned major */
  261. struct gendisk *disk; /* blkdev's gendisk and rq */
  262. u32 image_format; /* Either 1 or 2 */
  263. struct rbd_client *rbd_client;
  264. char name[DEV_NAME_LEN]; /* blkdev name, e.g. rbd3 */
  265. spinlock_t lock; /* queue, flags, open_count */
  266. struct rbd_image_header header;
  267. unsigned long flags; /* possibly lock protected */
  268. struct rbd_spec *spec;
  269. char *header_name;
  270. struct ceph_file_layout layout;
  271. struct ceph_osd_event *watch_event;
  272. struct rbd_obj_request *watch_request;
  273. struct rbd_spec *parent_spec;
  274. u64 parent_overlap;
  275. atomic_t parent_ref;
  276. struct rbd_device *parent;
  277. /* protects updating the header */
  278. struct rw_semaphore header_rwsem;
  279. struct rbd_mapping mapping;
  280. struct list_head node;
  281. /* sysfs related */
  282. struct device dev;
  283. unsigned long open_count; /* protected by lock */
  284. };
  285. /*
  286. * Flag bits for rbd_dev->flags. If atomicity is required,
  287. * rbd_dev->lock is used to protect access.
  288. *
  289. * Currently, only the "removing" flag (which is coupled with the
  290. * "open_count" field) requires atomic access.
  291. */
  292. enum rbd_dev_flags {
  293. RBD_DEV_FLAG_EXISTS, /* mapped snapshot has not been deleted */
  294. RBD_DEV_FLAG_REMOVING, /* this mapping is being removed */
  295. };
  296. static DEFINE_MUTEX(client_mutex); /* Serialize client creation */
  297. static LIST_HEAD(rbd_dev_list); /* devices */
  298. static DEFINE_SPINLOCK(rbd_dev_list_lock);
  299. static LIST_HEAD(rbd_client_list); /* clients */
  300. static DEFINE_SPINLOCK(rbd_client_list_lock);
  301. /* Slab caches for frequently-allocated structures */
  302. static struct kmem_cache *rbd_img_request_cache;
  303. static struct kmem_cache *rbd_obj_request_cache;
  304. static struct kmem_cache *rbd_segment_name_cache;
  305. static int rbd_img_request_submit(struct rbd_img_request *img_request);
  306. static void rbd_dev_device_release(struct device *dev);
  307. static ssize_t rbd_add(struct bus_type *bus, const char *buf,
  308. size_t count);
  309. static ssize_t rbd_remove(struct bus_type *bus, const char *buf,
  310. size_t count);
  311. static int rbd_dev_image_probe(struct rbd_device *rbd_dev, bool mapping);
  312. static void rbd_spec_put(struct rbd_spec *spec);
  313. static BUS_ATTR(add, S_IWUSR, NULL, rbd_add);
  314. static BUS_ATTR(remove, S_IWUSR, NULL, rbd_remove);
  315. static struct attribute *rbd_bus_attrs[] = {
  316. &bus_attr_add.attr,
  317. &bus_attr_remove.attr,
  318. NULL,
  319. };
  320. ATTRIBUTE_GROUPS(rbd_bus);
  321. static struct bus_type rbd_bus_type = {
  322. .name = "rbd",
  323. .bus_groups = rbd_bus_groups,
  324. };
  325. static void rbd_root_dev_release(struct device *dev)
  326. {
  327. }
  328. static struct device rbd_root_dev = {
  329. .init_name = "rbd",
  330. .release = rbd_root_dev_release,
  331. };
  332. static __printf(2, 3)
  333. void rbd_warn(struct rbd_device *rbd_dev, const char *fmt, ...)
  334. {
  335. struct va_format vaf;
  336. va_list args;
  337. va_start(args, fmt);
  338. vaf.fmt = fmt;
  339. vaf.va = &args;
  340. if (!rbd_dev)
  341. printk(KERN_WARNING "%s: %pV\n", RBD_DRV_NAME, &vaf);
  342. else if (rbd_dev->disk)
  343. printk(KERN_WARNING "%s: %s: %pV\n",
  344. RBD_DRV_NAME, rbd_dev->disk->disk_name, &vaf);
  345. else if (rbd_dev->spec && rbd_dev->spec->image_name)
  346. printk(KERN_WARNING "%s: image %s: %pV\n",
  347. RBD_DRV_NAME, rbd_dev->spec->image_name, &vaf);
  348. else if (rbd_dev->spec && rbd_dev->spec->image_id)
  349. printk(KERN_WARNING "%s: id %s: %pV\n",
  350. RBD_DRV_NAME, rbd_dev->spec->image_id, &vaf);
  351. else /* punt */
  352. printk(KERN_WARNING "%s: rbd_dev %p: %pV\n",
  353. RBD_DRV_NAME, rbd_dev, &vaf);
  354. va_end(args);
  355. }
  356. #ifdef RBD_DEBUG
  357. #define rbd_assert(expr) \
  358. if (unlikely(!(expr))) { \
  359. printk(KERN_ERR "\nAssertion failure in %s() " \
  360. "at line %d:\n\n" \
  361. "\trbd_assert(%s);\n\n", \
  362. __func__, __LINE__, #expr); \
  363. BUG(); \
  364. }
  365. #else /* !RBD_DEBUG */
  366. # define rbd_assert(expr) ((void) 0)
  367. #endif /* !RBD_DEBUG */
  368. static int rbd_img_obj_request_submit(struct rbd_obj_request *obj_request);
  369. static void rbd_img_parent_read(struct rbd_obj_request *obj_request);
  370. static void rbd_dev_remove_parent(struct rbd_device *rbd_dev);
  371. static int rbd_dev_refresh(struct rbd_device *rbd_dev);
  372. static int rbd_dev_v2_header_onetime(struct rbd_device *rbd_dev);
  373. static int rbd_dev_v2_header_info(struct rbd_device *rbd_dev);
  374. static const char *rbd_dev_v2_snap_name(struct rbd_device *rbd_dev,
  375. u64 snap_id);
  376. static int _rbd_dev_v2_snap_size(struct rbd_device *rbd_dev, u64 snap_id,
  377. u8 *order, u64 *snap_size);
  378. static int _rbd_dev_v2_snap_features(struct rbd_device *rbd_dev, u64 snap_id,
  379. u64 *snap_features);
  380. static u64 rbd_snap_id_by_name(struct rbd_device *rbd_dev, const char *name);
  381. static int rbd_open(struct block_device *bdev, fmode_t mode)
  382. {
  383. struct rbd_device *rbd_dev = bdev->bd_disk->private_data;
  384. bool removing = false;
  385. if ((mode & FMODE_WRITE) && rbd_dev->mapping.read_only)
  386. return -EROFS;
  387. spin_lock_irq(&rbd_dev->lock);
  388. if (test_bit(RBD_DEV_FLAG_REMOVING, &rbd_dev->flags))
  389. removing = true;
  390. else
  391. rbd_dev->open_count++;
  392. spin_unlock_irq(&rbd_dev->lock);
  393. if (removing)
  394. return -ENOENT;
  395. (void) get_device(&rbd_dev->dev);
  396. set_device_ro(bdev, rbd_dev->mapping.read_only);
  397. return 0;
  398. }
  399. static void rbd_release(struct gendisk *disk, fmode_t mode)
  400. {
  401. struct rbd_device *rbd_dev = disk->private_data;
  402. unsigned long open_count_before;
  403. spin_lock_irq(&rbd_dev->lock);
  404. open_count_before = rbd_dev->open_count--;
  405. spin_unlock_irq(&rbd_dev->lock);
  406. rbd_assert(open_count_before > 0);
  407. put_device(&rbd_dev->dev);
  408. }
  409. static const struct block_device_operations rbd_bd_ops = {
  410. .owner = THIS_MODULE,
  411. .open = rbd_open,
  412. .release = rbd_release,
  413. };
  414. /*
  415. * Initialize an rbd client instance. Success or not, this function
  416. * consumes ceph_opts. Caller holds client_mutex.
  417. */
  418. static struct rbd_client *rbd_client_create(struct ceph_options *ceph_opts)
  419. {
  420. struct rbd_client *rbdc;
  421. int ret = -ENOMEM;
  422. dout("%s:\n", __func__);
  423. rbdc = kmalloc(sizeof(struct rbd_client), GFP_KERNEL);
  424. if (!rbdc)
  425. goto out_opt;
  426. kref_init(&rbdc->kref);
  427. INIT_LIST_HEAD(&rbdc->node);
  428. rbdc->client = ceph_create_client(ceph_opts, rbdc, 0, 0);
  429. if (IS_ERR(rbdc->client))
  430. goto out_rbdc;
  431. ceph_opts = NULL; /* Now rbdc->client is responsible for ceph_opts */
  432. ret = ceph_open_session(rbdc->client);
  433. if (ret < 0)
  434. goto out_client;
  435. spin_lock(&rbd_client_list_lock);
  436. list_add_tail(&rbdc->node, &rbd_client_list);
  437. spin_unlock(&rbd_client_list_lock);
  438. dout("%s: rbdc %p\n", __func__, rbdc);
  439. return rbdc;
  440. out_client:
  441. ceph_destroy_client(rbdc->client);
  442. out_rbdc:
  443. kfree(rbdc);
  444. out_opt:
  445. if (ceph_opts)
  446. ceph_destroy_options(ceph_opts);
  447. dout("%s: error %d\n", __func__, ret);
  448. return ERR_PTR(ret);
  449. }
  450. static struct rbd_client *__rbd_get_client(struct rbd_client *rbdc)
  451. {
  452. kref_get(&rbdc->kref);
  453. return rbdc;
  454. }
  455. /*
  456. * Find a ceph client with specific addr and configuration. If
  457. * found, bump its reference count.
  458. */
  459. static struct rbd_client *rbd_client_find(struct ceph_options *ceph_opts)
  460. {
  461. struct rbd_client *client_node;
  462. bool found = false;
  463. if (ceph_opts->flags & CEPH_OPT_NOSHARE)
  464. return NULL;
  465. spin_lock(&rbd_client_list_lock);
  466. list_for_each_entry(client_node, &rbd_client_list, node) {
  467. if (!ceph_compare_options(ceph_opts, client_node->client)) {
  468. __rbd_get_client(client_node);
  469. found = true;
  470. break;
  471. }
  472. }
  473. spin_unlock(&rbd_client_list_lock);
  474. return found ? client_node : NULL;
  475. }
  476. /*
  477. * mount options
  478. */
  479. enum {
  480. Opt_last_int,
  481. /* int args above */
  482. Opt_last_string,
  483. /* string args above */
  484. Opt_read_only,
  485. Opt_read_write,
  486. /* Boolean args above */
  487. Opt_last_bool,
  488. };
  489. static match_table_t rbd_opts_tokens = {
  490. /* int args above */
  491. /* string args above */
  492. {Opt_read_only, "read_only"},
  493. {Opt_read_only, "ro"}, /* Alternate spelling */
  494. {Opt_read_write, "read_write"},
  495. {Opt_read_write, "rw"}, /* Alternate spelling */
  496. /* Boolean args above */
  497. {-1, NULL}
  498. };
  499. struct rbd_options {
  500. bool read_only;
  501. };
  502. #define RBD_READ_ONLY_DEFAULT false
  503. static int parse_rbd_opts_token(char *c, void *private)
  504. {
  505. struct rbd_options *rbd_opts = private;
  506. substring_t argstr[MAX_OPT_ARGS];
  507. int token, intval, ret;
  508. token = match_token(c, rbd_opts_tokens, argstr);
  509. if (token < 0)
  510. return -EINVAL;
  511. if (token < Opt_last_int) {
  512. ret = match_int(&argstr[0], &intval);
  513. if (ret < 0) {
  514. pr_err("bad mount option arg (not int) "
  515. "at '%s'\n", c);
  516. return ret;
  517. }
  518. dout("got int token %d val %d\n", token, intval);
  519. } else if (token > Opt_last_int && token < Opt_last_string) {
  520. dout("got string token %d val %s\n", token,
  521. argstr[0].from);
  522. } else if (token > Opt_last_string && token < Opt_last_bool) {
  523. dout("got Boolean token %d\n", token);
  524. } else {
  525. dout("got token %d\n", token);
  526. }
  527. switch (token) {
  528. case Opt_read_only:
  529. rbd_opts->read_only = true;
  530. break;
  531. case Opt_read_write:
  532. rbd_opts->read_only = false;
  533. break;
  534. default:
  535. rbd_assert(false);
  536. break;
  537. }
  538. return 0;
  539. }
  540. /*
  541. * Get a ceph client with specific addr and configuration, if one does
  542. * not exist create it. Either way, ceph_opts is consumed by this
  543. * function.
  544. */
  545. static struct rbd_client *rbd_get_client(struct ceph_options *ceph_opts)
  546. {
  547. struct rbd_client *rbdc;
  548. mutex_lock_nested(&client_mutex, SINGLE_DEPTH_NESTING);
  549. rbdc = rbd_client_find(ceph_opts);
  550. if (rbdc) /* using an existing client */
  551. ceph_destroy_options(ceph_opts);
  552. else
  553. rbdc = rbd_client_create(ceph_opts);
  554. mutex_unlock(&client_mutex);
  555. return rbdc;
  556. }
  557. /*
  558. * Destroy ceph client
  559. *
  560. * Caller must hold rbd_client_list_lock.
  561. */
  562. static void rbd_client_release(struct kref *kref)
  563. {
  564. struct rbd_client *rbdc = container_of(kref, struct rbd_client, kref);
  565. dout("%s: rbdc %p\n", __func__, rbdc);
  566. spin_lock(&rbd_client_list_lock);
  567. list_del(&rbdc->node);
  568. spin_unlock(&rbd_client_list_lock);
  569. ceph_destroy_client(rbdc->client);
  570. kfree(rbdc);
  571. }
  572. /*
  573. * Drop reference to ceph client node. If it's not referenced anymore, release
  574. * it.
  575. */
  576. static void rbd_put_client(struct rbd_client *rbdc)
  577. {
  578. if (rbdc)
  579. kref_put(&rbdc->kref, rbd_client_release);
  580. }
  581. static bool rbd_image_format_valid(u32 image_format)
  582. {
  583. return image_format == 1 || image_format == 2;
  584. }
  585. static bool rbd_dev_ondisk_valid(struct rbd_image_header_ondisk *ondisk)
  586. {
  587. size_t size;
  588. u32 snap_count;
  589. /* The header has to start with the magic rbd header text */
  590. if (memcmp(&ondisk->text, RBD_HEADER_TEXT, sizeof (RBD_HEADER_TEXT)))
  591. return false;
  592. /* The bio layer requires at least sector-sized I/O */
  593. if (ondisk->options.order < SECTOR_SHIFT)
  594. return false;
  595. /* If we use u64 in a few spots we may be able to loosen this */
  596. if (ondisk->options.order > 8 * sizeof (int) - 1)
  597. return false;
  598. /*
  599. * The size of a snapshot header has to fit in a size_t, and
  600. * that limits the number of snapshots.
  601. */
  602. snap_count = le32_to_cpu(ondisk->snap_count);
  603. size = SIZE_MAX - sizeof (struct ceph_snap_context);
  604. if (snap_count > size / sizeof (__le64))
  605. return false;
  606. /*
  607. * Not only that, but the size of the entire the snapshot
  608. * header must also be representable in a size_t.
  609. */
  610. size -= snap_count * sizeof (__le64);
  611. if ((u64) size < le64_to_cpu(ondisk->snap_names_len))
  612. return false;
  613. return true;
  614. }
  615. /*
  616. * Fill an rbd image header with information from the given format 1
  617. * on-disk header.
  618. */
  619. static int rbd_header_from_disk(struct rbd_device *rbd_dev,
  620. struct rbd_image_header_ondisk *ondisk)
  621. {
  622. struct rbd_image_header *header = &rbd_dev->header;
  623. bool first_time = header->object_prefix == NULL;
  624. struct ceph_snap_context *snapc;
  625. char *object_prefix = NULL;
  626. char *snap_names = NULL;
  627. u64 *snap_sizes = NULL;
  628. u32 snap_count;
  629. size_t size;
  630. int ret = -ENOMEM;
  631. u32 i;
  632. /* Allocate this now to avoid having to handle failure below */
  633. if (first_time) {
  634. size_t len;
  635. len = strnlen(ondisk->object_prefix,
  636. sizeof (ondisk->object_prefix));
  637. object_prefix = kmalloc(len + 1, GFP_KERNEL);
  638. if (!object_prefix)
  639. return -ENOMEM;
  640. memcpy(object_prefix, ondisk->object_prefix, len);
  641. object_prefix[len] = '\0';
  642. }
  643. /* Allocate the snapshot context and fill it in */
  644. snap_count = le32_to_cpu(ondisk->snap_count);
  645. snapc = ceph_create_snap_context(snap_count, GFP_KERNEL);
  646. if (!snapc)
  647. goto out_err;
  648. snapc->seq = le64_to_cpu(ondisk->snap_seq);
  649. if (snap_count) {
  650. struct rbd_image_snap_ondisk *snaps;
  651. u64 snap_names_len = le64_to_cpu(ondisk->snap_names_len);
  652. /* We'll keep a copy of the snapshot names... */
  653. if (snap_names_len > (u64)SIZE_MAX)
  654. goto out_2big;
  655. snap_names = kmalloc(snap_names_len, GFP_KERNEL);
  656. if (!snap_names)
  657. goto out_err;
  658. /* ...as well as the array of their sizes. */
  659. size = snap_count * sizeof (*header->snap_sizes);
  660. snap_sizes = kmalloc(size, GFP_KERNEL);
  661. if (!snap_sizes)
  662. goto out_err;
  663. /*
  664. * Copy the names, and fill in each snapshot's id
  665. * and size.
  666. *
  667. * Note that rbd_dev_v1_header_info() guarantees the
  668. * ondisk buffer we're working with has
  669. * snap_names_len bytes beyond the end of the
  670. * snapshot id array, this memcpy() is safe.
  671. */
  672. memcpy(snap_names, &ondisk->snaps[snap_count], snap_names_len);
  673. snaps = ondisk->snaps;
  674. for (i = 0; i < snap_count; i++) {
  675. snapc->snaps[i] = le64_to_cpu(snaps[i].id);
  676. snap_sizes[i] = le64_to_cpu(snaps[i].image_size);
  677. }
  678. }
  679. /* We won't fail any more, fill in the header */
  680. if (first_time) {
  681. header->object_prefix = object_prefix;
  682. header->obj_order = ondisk->options.order;
  683. header->crypt_type = ondisk->options.crypt_type;
  684. header->comp_type = ondisk->options.comp_type;
  685. /* The rest aren't used for format 1 images */
  686. header->stripe_unit = 0;
  687. header->stripe_count = 0;
  688. header->features = 0;
  689. } else {
  690. ceph_put_snap_context(header->snapc);
  691. kfree(header->snap_names);
  692. kfree(header->snap_sizes);
  693. }
  694. /* The remaining fields always get updated (when we refresh) */
  695. header->image_size = le64_to_cpu(ondisk->image_size);
  696. header->snapc = snapc;
  697. header->snap_names = snap_names;
  698. header->snap_sizes = snap_sizes;
  699. /* Make sure mapping size is consistent with header info */
  700. if (rbd_dev->spec->snap_id == CEPH_NOSNAP || first_time)
  701. if (rbd_dev->mapping.size != header->image_size)
  702. rbd_dev->mapping.size = header->image_size;
  703. return 0;
  704. out_2big:
  705. ret = -EIO;
  706. out_err:
  707. kfree(snap_sizes);
  708. kfree(snap_names);
  709. ceph_put_snap_context(snapc);
  710. kfree(object_prefix);
  711. return ret;
  712. }
  713. static const char *_rbd_dev_v1_snap_name(struct rbd_device *rbd_dev, u32 which)
  714. {
  715. const char *snap_name;
  716. rbd_assert(which < rbd_dev->header.snapc->num_snaps);
  717. /* Skip over names until we find the one we are looking for */
  718. snap_name = rbd_dev->header.snap_names;
  719. while (which--)
  720. snap_name += strlen(snap_name) + 1;
  721. return kstrdup(snap_name, GFP_KERNEL);
  722. }
  723. /*
  724. * Snapshot id comparison function for use with qsort()/bsearch().
  725. * Note that result is for snapshots in *descending* order.
  726. */
  727. static int snapid_compare_reverse(const void *s1, const void *s2)
  728. {
  729. u64 snap_id1 = *(u64 *)s1;
  730. u64 snap_id2 = *(u64 *)s2;
  731. if (snap_id1 < snap_id2)
  732. return 1;
  733. return snap_id1 == snap_id2 ? 0 : -1;
  734. }
  735. /*
  736. * Search a snapshot context to see if the given snapshot id is
  737. * present.
  738. *
  739. * Returns the position of the snapshot id in the array if it's found,
  740. * or BAD_SNAP_INDEX otherwise.
  741. *
  742. * Note: The snapshot array is in kept sorted (by the osd) in
  743. * reverse order, highest snapshot id first.
  744. */
  745. static u32 rbd_dev_snap_index(struct rbd_device *rbd_dev, u64 snap_id)
  746. {
  747. struct ceph_snap_context *snapc = rbd_dev->header.snapc;
  748. u64 *found;
  749. found = bsearch(&snap_id, &snapc->snaps, snapc->num_snaps,
  750. sizeof (snap_id), snapid_compare_reverse);
  751. return found ? (u32)(found - &snapc->snaps[0]) : BAD_SNAP_INDEX;
  752. }
  753. static const char *rbd_dev_v1_snap_name(struct rbd_device *rbd_dev,
  754. u64 snap_id)
  755. {
  756. u32 which;
  757. const char *snap_name;
  758. which = rbd_dev_snap_index(rbd_dev, snap_id);
  759. if (which == BAD_SNAP_INDEX)
  760. return ERR_PTR(-ENOENT);
  761. snap_name = _rbd_dev_v1_snap_name(rbd_dev, which);
  762. return snap_name ? snap_name : ERR_PTR(-ENOMEM);
  763. }
  764. static const char *rbd_snap_name(struct rbd_device *rbd_dev, u64 snap_id)
  765. {
  766. if (snap_id == CEPH_NOSNAP)
  767. return RBD_SNAP_HEAD_NAME;
  768. rbd_assert(rbd_image_format_valid(rbd_dev->image_format));
  769. if (rbd_dev->image_format == 1)
  770. return rbd_dev_v1_snap_name(rbd_dev, snap_id);
  771. return rbd_dev_v2_snap_name(rbd_dev, snap_id);
  772. }
  773. static int rbd_snap_size(struct rbd_device *rbd_dev, u64 snap_id,
  774. u64 *snap_size)
  775. {
  776. rbd_assert(rbd_image_format_valid(rbd_dev->image_format));
  777. if (snap_id == CEPH_NOSNAP) {
  778. *snap_size = rbd_dev->header.image_size;
  779. } else if (rbd_dev->image_format == 1) {
  780. u32 which;
  781. which = rbd_dev_snap_index(rbd_dev, snap_id);
  782. if (which == BAD_SNAP_INDEX)
  783. return -ENOENT;
  784. *snap_size = rbd_dev->header.snap_sizes[which];
  785. } else {
  786. u64 size = 0;
  787. int ret;
  788. ret = _rbd_dev_v2_snap_size(rbd_dev, snap_id, NULL, &size);
  789. if (ret)
  790. return ret;
  791. *snap_size = size;
  792. }
  793. return 0;
  794. }
  795. static int rbd_snap_features(struct rbd_device *rbd_dev, u64 snap_id,
  796. u64 *snap_features)
  797. {
  798. rbd_assert(rbd_image_format_valid(rbd_dev->image_format));
  799. if (snap_id == CEPH_NOSNAP) {
  800. *snap_features = rbd_dev->header.features;
  801. } else if (rbd_dev->image_format == 1) {
  802. *snap_features = 0; /* No features for format 1 */
  803. } else {
  804. u64 features = 0;
  805. int ret;
  806. ret = _rbd_dev_v2_snap_features(rbd_dev, snap_id, &features);
  807. if (ret)
  808. return ret;
  809. *snap_features = features;
  810. }
  811. return 0;
  812. }
  813. static int rbd_dev_mapping_set(struct rbd_device *rbd_dev)
  814. {
  815. u64 snap_id = rbd_dev->spec->snap_id;
  816. u64 size = 0;
  817. u64 features = 0;
  818. int ret;
  819. ret = rbd_snap_size(rbd_dev, snap_id, &size);
  820. if (ret)
  821. return ret;
  822. ret = rbd_snap_features(rbd_dev, snap_id, &features);
  823. if (ret)
  824. return ret;
  825. rbd_dev->mapping.size = size;
  826. rbd_dev->mapping.features = features;
  827. return 0;
  828. }
  829. static void rbd_dev_mapping_clear(struct rbd_device *rbd_dev)
  830. {
  831. rbd_dev->mapping.size = 0;
  832. rbd_dev->mapping.features = 0;
  833. }
  834. static const char *rbd_segment_name(struct rbd_device *rbd_dev, u64 offset)
  835. {
  836. char *name;
  837. u64 segment;
  838. int ret;
  839. char *name_format;
  840. name = kmem_cache_alloc(rbd_segment_name_cache, GFP_NOIO);
  841. if (!name)
  842. return NULL;
  843. segment = offset >> rbd_dev->header.obj_order;
  844. name_format = "%s.%012llx";
  845. if (rbd_dev->image_format == 2)
  846. name_format = "%s.%016llx";
  847. ret = snprintf(name, MAX_OBJ_NAME_SIZE + 1, name_format,
  848. rbd_dev->header.object_prefix, segment);
  849. if (ret < 0 || ret > MAX_OBJ_NAME_SIZE) {
  850. pr_err("error formatting segment name for #%llu (%d)\n",
  851. segment, ret);
  852. kfree(name);
  853. name = NULL;
  854. }
  855. return name;
  856. }
  857. static void rbd_segment_name_free(const char *name)
  858. {
  859. /* The explicit cast here is needed to drop the const qualifier */
  860. kmem_cache_free(rbd_segment_name_cache, (void *)name);
  861. }
  862. static u64 rbd_segment_offset(struct rbd_device *rbd_dev, u64 offset)
  863. {
  864. u64 segment_size = (u64) 1 << rbd_dev->header.obj_order;
  865. return offset & (segment_size - 1);
  866. }
  867. static u64 rbd_segment_length(struct rbd_device *rbd_dev,
  868. u64 offset, u64 length)
  869. {
  870. u64 segment_size = (u64) 1 << rbd_dev->header.obj_order;
  871. offset &= segment_size - 1;
  872. rbd_assert(length <= U64_MAX - offset);
  873. if (offset + length > segment_size)
  874. length = segment_size - offset;
  875. return length;
  876. }
  877. /*
  878. * returns the size of an object in the image
  879. */
  880. static u64 rbd_obj_bytes(struct rbd_image_header *header)
  881. {
  882. return 1 << header->obj_order;
  883. }
  884. /*
  885. * bio helpers
  886. */
  887. static void bio_chain_put(struct bio *chain)
  888. {
  889. struct bio *tmp;
  890. while (chain) {
  891. tmp = chain;
  892. chain = chain->bi_next;
  893. bio_put(tmp);
  894. }
  895. }
  896. /*
  897. * zeros a bio chain, starting at specific offset
  898. */
  899. static void zero_bio_chain(struct bio *chain, int start_ofs)
  900. {
  901. struct bio_vec *bv;
  902. unsigned long flags;
  903. void *buf;
  904. int i;
  905. int pos = 0;
  906. while (chain) {
  907. bio_for_each_segment(bv, chain, i) {
  908. if (pos + bv->bv_len > start_ofs) {
  909. int remainder = max(start_ofs - pos, 0);
  910. buf = bvec_kmap_irq(bv, &flags);
  911. memset(buf + remainder, 0,
  912. bv->bv_len - remainder);
  913. flush_dcache_page(bv->bv_page);
  914. bvec_kunmap_irq(buf, &flags);
  915. }
  916. pos += bv->bv_len;
  917. }
  918. chain = chain->bi_next;
  919. }
  920. }
  921. /*
  922. * similar to zero_bio_chain(), zeros data defined by a page array,
  923. * starting at the given byte offset from the start of the array and
  924. * continuing up to the given end offset. The pages array is
  925. * assumed to be big enough to hold all bytes up to the end.
  926. */
  927. static void zero_pages(struct page **pages, u64 offset, u64 end)
  928. {
  929. struct page **page = &pages[offset >> PAGE_SHIFT];
  930. rbd_assert(end > offset);
  931. rbd_assert(end - offset <= (u64)SIZE_MAX);
  932. while (offset < end) {
  933. size_t page_offset;
  934. size_t length;
  935. unsigned long flags;
  936. void *kaddr;
  937. page_offset = offset & ~PAGE_MASK;
  938. length = min_t(size_t, PAGE_SIZE - page_offset, end - offset);
  939. local_irq_save(flags);
  940. kaddr = kmap_atomic(*page);
  941. memset(kaddr + page_offset, 0, length);
  942. flush_dcache_page(*page);
  943. kunmap_atomic(kaddr);
  944. local_irq_restore(flags);
  945. offset += length;
  946. page++;
  947. }
  948. }
  949. /*
  950. * Clone a portion of a bio, starting at the given byte offset
  951. * and continuing for the number of bytes indicated.
  952. */
  953. static struct bio *bio_clone_range(struct bio *bio_src,
  954. unsigned int offset,
  955. unsigned int len,
  956. gfp_t gfpmask)
  957. {
  958. struct bio_vec *bv;
  959. unsigned int resid;
  960. unsigned short idx;
  961. unsigned int voff;
  962. unsigned short end_idx;
  963. unsigned short vcnt;
  964. struct bio *bio;
  965. /* Handle the easy case for the caller */
  966. if (!offset && len == bio_src->bi_size)
  967. return bio_clone(bio_src, gfpmask);
  968. if (WARN_ON_ONCE(!len))
  969. return NULL;
  970. if (WARN_ON_ONCE(len > bio_src->bi_size))
  971. return NULL;
  972. if (WARN_ON_ONCE(offset > bio_src->bi_size - len))
  973. return NULL;
  974. /* Find first affected segment... */
  975. resid = offset;
  976. bio_for_each_segment(bv, bio_src, idx) {
  977. if (resid < bv->bv_len)
  978. break;
  979. resid -= bv->bv_len;
  980. }
  981. voff = resid;
  982. /* ...and the last affected segment */
  983. resid += len;
  984. __bio_for_each_segment(bv, bio_src, end_idx, idx) {
  985. if (resid <= bv->bv_len)
  986. break;
  987. resid -= bv->bv_len;
  988. }
  989. vcnt = end_idx - idx + 1;
  990. /* Build the clone */
  991. bio = bio_alloc(gfpmask, (unsigned int) vcnt);
  992. if (!bio)
  993. return NULL; /* ENOMEM */
  994. bio->bi_bdev = bio_src->bi_bdev;
  995. bio->bi_sector = bio_src->bi_sector + (offset >> SECTOR_SHIFT);
  996. bio->bi_rw = bio_src->bi_rw;
  997. bio->bi_flags |= 1 << BIO_CLONED;
  998. /*
  999. * Copy over our part of the bio_vec, then update the first
  1000. * and last (or only) entries.
  1001. */
  1002. memcpy(&bio->bi_io_vec[0], &bio_src->bi_io_vec[idx],
  1003. vcnt * sizeof (struct bio_vec));
  1004. bio->bi_io_vec[0].bv_offset += voff;
  1005. if (vcnt > 1) {
  1006. bio->bi_io_vec[0].bv_len -= voff;
  1007. bio->bi_io_vec[vcnt - 1].bv_len = resid;
  1008. } else {
  1009. bio->bi_io_vec[0].bv_len = len;
  1010. }
  1011. bio->bi_vcnt = vcnt;
  1012. bio->bi_size = len;
  1013. bio->bi_idx = 0;
  1014. return bio;
  1015. }
  1016. /*
  1017. * Clone a portion of a bio chain, starting at the given byte offset
  1018. * into the first bio in the source chain and continuing for the
  1019. * number of bytes indicated. The result is another bio chain of
  1020. * exactly the given length, or a null pointer on error.
  1021. *
  1022. * The bio_src and offset parameters are both in-out. On entry they
  1023. * refer to the first source bio and the offset into that bio where
  1024. * the start of data to be cloned is located.
  1025. *
  1026. * On return, bio_src is updated to refer to the bio in the source
  1027. * chain that contains first un-cloned byte, and *offset will
  1028. * contain the offset of that byte within that bio.
  1029. */
  1030. static struct bio *bio_chain_clone_range(struct bio **bio_src,
  1031. unsigned int *offset,
  1032. unsigned int len,
  1033. gfp_t gfpmask)
  1034. {
  1035. struct bio *bi = *bio_src;
  1036. unsigned int off = *offset;
  1037. struct bio *chain = NULL;
  1038. struct bio **end;
  1039. /* Build up a chain of clone bios up to the limit */
  1040. if (!bi || off >= bi->bi_size || !len)
  1041. return NULL; /* Nothing to clone */
  1042. end = &chain;
  1043. while (len) {
  1044. unsigned int bi_size;
  1045. struct bio *bio;
  1046. if (!bi) {
  1047. rbd_warn(NULL, "bio_chain exhausted with %u left", len);
  1048. goto out_err; /* EINVAL; ran out of bio's */
  1049. }
  1050. bi_size = min_t(unsigned int, bi->bi_size - off, len);
  1051. bio = bio_clone_range(bi, off, bi_size, gfpmask);
  1052. if (!bio)
  1053. goto out_err; /* ENOMEM */
  1054. *end = bio;
  1055. end = &bio->bi_next;
  1056. off += bi_size;
  1057. if (off == bi->bi_size) {
  1058. bi = bi->bi_next;
  1059. off = 0;
  1060. }
  1061. len -= bi_size;
  1062. }
  1063. *bio_src = bi;
  1064. *offset = off;
  1065. return chain;
  1066. out_err:
  1067. bio_chain_put(chain);
  1068. return NULL;
  1069. }
  1070. /*
  1071. * The default/initial value for all object request flags is 0. For
  1072. * each flag, once its value is set to 1 it is never reset to 0
  1073. * again.
  1074. */
  1075. static void obj_request_img_data_set(struct rbd_obj_request *obj_request)
  1076. {
  1077. if (test_and_set_bit(OBJ_REQ_IMG_DATA, &obj_request->flags)) {
  1078. struct rbd_device *rbd_dev;
  1079. rbd_dev = obj_request->img_request->rbd_dev;
  1080. rbd_warn(rbd_dev, "obj_request %p already marked img_data\n",
  1081. obj_request);
  1082. }
  1083. }
  1084. static bool obj_request_img_data_test(struct rbd_obj_request *obj_request)
  1085. {
  1086. smp_mb();
  1087. return test_bit(OBJ_REQ_IMG_DATA, &obj_request->flags) != 0;
  1088. }
  1089. static void obj_request_done_set(struct rbd_obj_request *obj_request)
  1090. {
  1091. if (test_and_set_bit(OBJ_REQ_DONE, &obj_request->flags)) {
  1092. struct rbd_device *rbd_dev = NULL;
  1093. if (obj_request_img_data_test(obj_request))
  1094. rbd_dev = obj_request->img_request->rbd_dev;
  1095. rbd_warn(rbd_dev, "obj_request %p already marked done\n",
  1096. obj_request);
  1097. }
  1098. }
  1099. static bool obj_request_done_test(struct rbd_obj_request *obj_request)
  1100. {
  1101. smp_mb();
  1102. return test_bit(OBJ_REQ_DONE, &obj_request->flags) != 0;
  1103. }
  1104. /*
  1105. * This sets the KNOWN flag after (possibly) setting the EXISTS
  1106. * flag. The latter is set based on the "exists" value provided.
  1107. *
  1108. * Note that for our purposes once an object exists it never goes
  1109. * away again. It's possible that the response from two existence
  1110. * checks are separated by the creation of the target object, and
  1111. * the first ("doesn't exist") response arrives *after* the second
  1112. * ("does exist"). In that case we ignore the second one.
  1113. */
  1114. static void obj_request_existence_set(struct rbd_obj_request *obj_request,
  1115. bool exists)
  1116. {
  1117. if (exists)
  1118. set_bit(OBJ_REQ_EXISTS, &obj_request->flags);
  1119. set_bit(OBJ_REQ_KNOWN, &obj_request->flags);
  1120. smp_mb();
  1121. }
  1122. static bool obj_request_known_test(struct rbd_obj_request *obj_request)
  1123. {
  1124. smp_mb();
  1125. return test_bit(OBJ_REQ_KNOWN, &obj_request->flags) != 0;
  1126. }
  1127. static bool obj_request_exists_test(struct rbd_obj_request *obj_request)
  1128. {
  1129. smp_mb();
  1130. return test_bit(OBJ_REQ_EXISTS, &obj_request->flags) != 0;
  1131. }
  1132. static void rbd_obj_request_get(struct rbd_obj_request *obj_request)
  1133. {
  1134. dout("%s: obj %p (was %d)\n", __func__, obj_request,
  1135. atomic_read(&obj_request->kref.refcount));
  1136. kref_get(&obj_request->kref);
  1137. }
  1138. static void rbd_obj_request_destroy(struct kref *kref);
  1139. static void rbd_obj_request_put(struct rbd_obj_request *obj_request)
  1140. {
  1141. rbd_assert(obj_request != NULL);
  1142. dout("%s: obj %p (was %d)\n", __func__, obj_request,
  1143. atomic_read(&obj_request->kref.refcount));
  1144. kref_put(&obj_request->kref, rbd_obj_request_destroy);
  1145. }
  1146. static bool img_request_child_test(struct rbd_img_request *img_request);
  1147. static void rbd_parent_request_destroy(struct kref *kref);
  1148. static void rbd_img_request_destroy(struct kref *kref);
  1149. static void rbd_img_request_put(struct rbd_img_request *img_request)
  1150. {
  1151. rbd_assert(img_request != NULL);
  1152. dout("%s: img %p (was %d)\n", __func__, img_request,
  1153. atomic_read(&img_request->kref.refcount));
  1154. if (img_request_child_test(img_request))
  1155. kref_put(&img_request->kref, rbd_parent_request_destroy);
  1156. else
  1157. kref_put(&img_request->kref, rbd_img_request_destroy);
  1158. }
  1159. static inline void rbd_img_obj_request_add(struct rbd_img_request *img_request,
  1160. struct rbd_obj_request *obj_request)
  1161. {
  1162. rbd_assert(obj_request->img_request == NULL);
  1163. /* Image request now owns object's original reference */
  1164. obj_request->img_request = img_request;
  1165. obj_request->which = img_request->obj_request_count;
  1166. rbd_assert(!obj_request_img_data_test(obj_request));
  1167. obj_request_img_data_set(obj_request);
  1168. rbd_assert(obj_request->which != BAD_WHICH);
  1169. img_request->obj_request_count++;
  1170. list_add_tail(&obj_request->links, &img_request->obj_requests);
  1171. dout("%s: img %p obj %p w=%u\n", __func__, img_request, obj_request,
  1172. obj_request->which);
  1173. }
  1174. static inline void rbd_img_obj_request_del(struct rbd_img_request *img_request,
  1175. struct rbd_obj_request *obj_request)
  1176. {
  1177. rbd_assert(obj_request->which != BAD_WHICH);
  1178. dout("%s: img %p obj %p w=%u\n", __func__, img_request, obj_request,
  1179. obj_request->which);
  1180. list_del(&obj_request->links);
  1181. rbd_assert(img_request->obj_request_count > 0);
  1182. img_request->obj_request_count--;
  1183. rbd_assert(obj_request->which == img_request->obj_request_count);
  1184. obj_request->which = BAD_WHICH;
  1185. rbd_assert(obj_request_img_data_test(obj_request));
  1186. rbd_assert(obj_request->img_request == img_request);
  1187. obj_request->img_request = NULL;
  1188. obj_request->callback = NULL;
  1189. rbd_obj_request_put(obj_request);
  1190. }
  1191. static bool obj_request_type_valid(enum obj_request_type type)
  1192. {
  1193. switch (type) {
  1194. case OBJ_REQUEST_NODATA:
  1195. case OBJ_REQUEST_BIO:
  1196. case OBJ_REQUEST_PAGES:
  1197. return true;
  1198. default:
  1199. return false;
  1200. }
  1201. }
  1202. static int rbd_obj_request_submit(struct ceph_osd_client *osdc,
  1203. struct rbd_obj_request *obj_request)
  1204. {
  1205. dout("%s: osdc %p obj %p\n", __func__, osdc, obj_request);
  1206. return ceph_osdc_start_request(osdc, obj_request->osd_req, false);
  1207. }
  1208. static void rbd_img_request_complete(struct rbd_img_request *img_request)
  1209. {
  1210. dout("%s: img %p\n", __func__, img_request);
  1211. /*
  1212. * If no error occurred, compute the aggregate transfer
  1213. * count for the image request. We could instead use
  1214. * atomic64_cmpxchg() to update it as each object request
  1215. * completes; not clear which way is better off hand.
  1216. */
  1217. if (!img_request->result) {
  1218. struct rbd_obj_request *obj_request;
  1219. u64 xferred = 0;
  1220. for_each_obj_request(img_request, obj_request)
  1221. xferred += obj_request->xferred;
  1222. img_request->xferred = xferred;
  1223. }
  1224. if (img_request->callback)
  1225. img_request->callback(img_request);
  1226. else
  1227. rbd_img_request_put(img_request);
  1228. }
  1229. /* Caller is responsible for rbd_obj_request_destroy(obj_request) */
  1230. static int rbd_obj_request_wait(struct rbd_obj_request *obj_request)
  1231. {
  1232. dout("%s: obj %p\n", __func__, obj_request);
  1233. return wait_for_completion_interruptible(&obj_request->completion);
  1234. }
  1235. /*
  1236. * The default/initial value for all image request flags is 0. Each
  1237. * is conditionally set to 1 at image request initialization time
  1238. * and currently never change thereafter.
  1239. */
  1240. static void img_request_write_set(struct rbd_img_request *img_request)
  1241. {
  1242. set_bit(IMG_REQ_WRITE, &img_request->flags);
  1243. smp_mb();
  1244. }
  1245. static bool img_request_write_test(struct rbd_img_request *img_request)
  1246. {
  1247. smp_mb();
  1248. return test_bit(IMG_REQ_WRITE, &img_request->flags) != 0;
  1249. }
  1250. static void img_request_child_set(struct rbd_img_request *img_request)
  1251. {
  1252. set_bit(IMG_REQ_CHILD, &img_request->flags);
  1253. smp_mb();
  1254. }
  1255. static void img_request_child_clear(struct rbd_img_request *img_request)
  1256. {
  1257. clear_bit(IMG_REQ_CHILD, &img_request->flags);
  1258. smp_mb();
  1259. }
  1260. static bool img_request_child_test(struct rbd_img_request *img_request)
  1261. {
  1262. smp_mb();
  1263. return test_bit(IMG_REQ_CHILD, &img_request->flags) != 0;
  1264. }
  1265. static void img_request_layered_set(struct rbd_img_request *img_request)
  1266. {
  1267. set_bit(IMG_REQ_LAYERED, &img_request->flags);
  1268. smp_mb();
  1269. }
  1270. static void img_request_layered_clear(struct rbd_img_request *img_request)
  1271. {
  1272. clear_bit(IMG_REQ_LAYERED, &img_request->flags);
  1273. smp_mb();
  1274. }
  1275. static bool img_request_layered_test(struct rbd_img_request *img_request)
  1276. {
  1277. smp_mb();
  1278. return test_bit(IMG_REQ_LAYERED, &img_request->flags) != 0;
  1279. }
  1280. static void
  1281. rbd_img_obj_request_read_callback(struct rbd_obj_request *obj_request)
  1282. {
  1283. u64 xferred = obj_request->xferred;
  1284. u64 length = obj_request->length;
  1285. dout("%s: obj %p img %p result %d %llu/%llu\n", __func__,
  1286. obj_request, obj_request->img_request, obj_request->result,
  1287. xferred, length);
  1288. /*
  1289. * ENOENT means a hole in the image. We zero-fill the entire
  1290. * length of the request. A short read also implies zero-fill
  1291. * to the end of the request. An error requires the whole
  1292. * length of the request to be reported finished with an error
  1293. * to the block layer. In each case we update the xferred
  1294. * count to indicate the whole request was satisfied.
  1295. */
  1296. rbd_assert(obj_request->type != OBJ_REQUEST_NODATA);
  1297. if (obj_request->result == -ENOENT) {
  1298. if (obj_request->type == OBJ_REQUEST_BIO)
  1299. zero_bio_chain(obj_request->bio_list, 0);
  1300. else
  1301. zero_pages(obj_request->pages, 0, length);
  1302. obj_request->result = 0;
  1303. } else if (xferred < length && !obj_request->result) {
  1304. if (obj_request->type == OBJ_REQUEST_BIO)
  1305. zero_bio_chain(obj_request->bio_list, xferred);
  1306. else
  1307. zero_pages(obj_request->pages, xferred, length);
  1308. }
  1309. obj_request->xferred = length;
  1310. obj_request_done_set(obj_request);
  1311. }
  1312. static void rbd_obj_request_complete(struct rbd_obj_request *obj_request)
  1313. {
  1314. dout("%s: obj %p cb %p\n", __func__, obj_request,
  1315. obj_request->callback);
  1316. if (obj_request->callback)
  1317. obj_request->callback(obj_request);
  1318. else
  1319. complete_all(&obj_request->completion);
  1320. }
  1321. static void rbd_osd_trivial_callback(struct rbd_obj_request *obj_request)
  1322. {
  1323. dout("%s: obj %p\n", __func__, obj_request);
  1324. obj_request_done_set(obj_request);
  1325. }
  1326. static void rbd_osd_read_callback(struct rbd_obj_request *obj_request)
  1327. {
  1328. struct rbd_img_request *img_request = NULL;
  1329. struct rbd_device *rbd_dev = NULL;
  1330. bool layered = false;
  1331. if (obj_request_img_data_test(obj_request)) {
  1332. img_request = obj_request->img_request;
  1333. layered = img_request && img_request_layered_test(img_request);
  1334. rbd_dev = img_request->rbd_dev;
  1335. }
  1336. dout("%s: obj %p img %p result %d %llu/%llu\n", __func__,
  1337. obj_request, img_request, obj_request->result,
  1338. obj_request->xferred, obj_request->length);
  1339. if (layered && obj_request->result == -ENOENT &&
  1340. obj_request->img_offset < rbd_dev->parent_overlap)
  1341. rbd_img_parent_read(obj_request);
  1342. else if (img_request)
  1343. rbd_img_obj_request_read_callback(obj_request);
  1344. else
  1345. obj_request_done_set(obj_request);
  1346. }
  1347. static void rbd_osd_write_callback(struct rbd_obj_request *obj_request)
  1348. {
  1349. dout("%s: obj %p result %d %llu\n", __func__, obj_request,
  1350. obj_request->result, obj_request->length);
  1351. /*
  1352. * There is no such thing as a successful short write. Set
  1353. * it to our originally-requested length.
  1354. */
  1355. obj_request->xferred = obj_request->length;
  1356. obj_request_done_set(obj_request);
  1357. }
  1358. /*
  1359. * For a simple stat call there's nothing to do. We'll do more if
  1360. * this is part of a write sequence for a layered image.
  1361. */
  1362. static void rbd_osd_stat_callback(struct rbd_obj_request *obj_request)
  1363. {
  1364. dout("%s: obj %p\n", __func__, obj_request);
  1365. obj_request_done_set(obj_request);
  1366. }
  1367. static void rbd_osd_req_callback(struct ceph_osd_request *osd_req,
  1368. struct ceph_msg *msg)
  1369. {
  1370. struct rbd_obj_request *obj_request = osd_req->r_priv;
  1371. u16 opcode;
  1372. dout("%s: osd_req %p msg %p\n", __func__, osd_req, msg);
  1373. rbd_assert(osd_req == obj_request->osd_req);
  1374. if (obj_request_img_data_test(obj_request)) {
  1375. rbd_assert(obj_request->img_request);
  1376. rbd_assert(obj_request->which != BAD_WHICH);
  1377. } else {
  1378. rbd_assert(obj_request->which == BAD_WHICH);
  1379. }
  1380. if (osd_req->r_result < 0)
  1381. obj_request->result = osd_req->r_result;
  1382. BUG_ON(osd_req->r_num_ops > 2);
  1383. /*
  1384. * We support a 64-bit length, but ultimately it has to be
  1385. * passed to blk_end_request(), which takes an unsigned int.
  1386. */
  1387. obj_request->xferred = osd_req->r_reply_op_len[0];
  1388. rbd_assert(obj_request->xferred < (u64)UINT_MAX);
  1389. opcode = osd_req->r_ops[0].op;
  1390. switch (opcode) {
  1391. case CEPH_OSD_OP_READ:
  1392. rbd_osd_read_callback(obj_request);
  1393. break;
  1394. case CEPH_OSD_OP_WRITE:
  1395. rbd_osd_write_callback(obj_request);
  1396. break;
  1397. case CEPH_OSD_OP_STAT:
  1398. rbd_osd_stat_callback(obj_request);
  1399. break;
  1400. case CEPH_OSD_OP_CALL:
  1401. case CEPH_OSD_OP_NOTIFY_ACK:
  1402. case CEPH_OSD_OP_WATCH:
  1403. rbd_osd_trivial_callback(obj_request);
  1404. break;
  1405. default:
  1406. rbd_warn(NULL, "%s: unsupported op %hu\n",
  1407. obj_request->object_name, (unsigned short) opcode);
  1408. break;
  1409. }
  1410. if (obj_request_done_test(obj_request))
  1411. rbd_obj_request_complete(obj_request);
  1412. }
  1413. static void rbd_osd_req_format_read(struct rbd_obj_request *obj_request)
  1414. {
  1415. struct rbd_img_request *img_request = obj_request->img_request;
  1416. struct ceph_osd_request *osd_req = obj_request->osd_req;
  1417. u64 snap_id;
  1418. rbd_assert(osd_req != NULL);
  1419. snap_id = img_request ? img_request->snap_id : CEPH_NOSNAP;
  1420. ceph_osdc_build_request(osd_req, obj_request->offset,
  1421. NULL, snap_id, NULL);
  1422. }
  1423. static void rbd_osd_req_format_write(struct rbd_obj_request *obj_request)
  1424. {
  1425. struct rbd_img_request *img_request = obj_request->img_request;
  1426. struct ceph_osd_request *osd_req = obj_request->osd_req;
  1427. struct ceph_snap_context *snapc;
  1428. struct timespec mtime = CURRENT_TIME;
  1429. rbd_assert(osd_req != NULL);
  1430. snapc = img_request ? img_request->snapc : NULL;
  1431. ceph_osdc_build_request(osd_req, obj_request->offset,
  1432. snapc, CEPH_NOSNAP, &mtime);
  1433. }
  1434. static struct ceph_osd_request *rbd_osd_req_create(
  1435. struct rbd_device *rbd_dev,
  1436. bool write_request,
  1437. struct rbd_obj_request *obj_request)
  1438. {
  1439. struct ceph_snap_context *snapc = NULL;
  1440. struct ceph_osd_client *osdc;
  1441. struct ceph_osd_request *osd_req;
  1442. if (obj_request_img_data_test(obj_request)) {
  1443. struct rbd_img_request *img_request = obj_request->img_request;
  1444. rbd_assert(write_request ==
  1445. img_request_write_test(img_request));
  1446. if (write_request)
  1447. snapc = img_request->snapc;
  1448. }
  1449. /* Allocate and initialize the request, for the single op */
  1450. osdc = &rbd_dev->rbd_client->client->osdc;
  1451. osd_req = ceph_osdc_alloc_request(osdc, snapc, 1, false, GFP_ATOMIC);
  1452. if (!osd_req)
  1453. return NULL; /* ENOMEM */
  1454. if (write_request)
  1455. osd_req->r_flags = CEPH_OSD_FLAG_WRITE | CEPH_OSD_FLAG_ONDISK;
  1456. else
  1457. osd_req->r_flags = CEPH_OSD_FLAG_READ;
  1458. osd_req->r_callback = rbd_osd_req_callback;
  1459. osd_req->r_priv = obj_request;
  1460. osd_req->r_oid_len = strlen(obj_request->object_name);
  1461. rbd_assert(osd_req->r_oid_len < sizeof (osd_req->r_oid));
  1462. memcpy(osd_req->r_oid, obj_request->object_name, osd_req->r_oid_len);
  1463. osd_req->r_file_layout = rbd_dev->layout; /* struct */
  1464. return osd_req;
  1465. }
  1466. /*
  1467. * Create a copyup osd request based on the information in the
  1468. * object request supplied. A copyup request has two osd ops,
  1469. * a copyup method call, and a "normal" write request.
  1470. */
  1471. static struct ceph_osd_request *
  1472. rbd_osd_req_create_copyup(struct rbd_obj_request *obj_request)
  1473. {
  1474. struct rbd_img_request *img_request;
  1475. struct ceph_snap_context *snapc;
  1476. struct rbd_device *rbd_dev;
  1477. struct ceph_osd_client *osdc;
  1478. struct ceph_osd_request *osd_req;
  1479. rbd_assert(obj_request_img_data_test(obj_request));
  1480. img_request = obj_request->img_request;
  1481. rbd_assert(img_request);
  1482. rbd_assert(img_request_write_test(img_request));
  1483. /* Allocate and initialize the request, for the two ops */
  1484. snapc = img_request->snapc;
  1485. rbd_dev = img_request->rbd_dev;
  1486. osdc = &rbd_dev->rbd_client->client->osdc;
  1487. osd_req = ceph_osdc_alloc_request(osdc, snapc, 2, false, GFP_ATOMIC);
  1488. if (!osd_req)
  1489. return NULL; /* ENOMEM */
  1490. osd_req->r_flags = CEPH_OSD_FLAG_WRITE | CEPH_OSD_FLAG_ONDISK;
  1491. osd_req->r_callback = rbd_osd_req_callback;
  1492. osd_req->r_priv = obj_request;
  1493. osd_req->r_oid_len = strlen(obj_request->object_name);
  1494. rbd_assert(osd_req->r_oid_len < sizeof (osd_req->r_oid));
  1495. memcpy(osd_req->r_oid, obj_request->object_name, osd_req->r_oid_len);
  1496. osd_req->r_file_layout = rbd_dev->layout; /* struct */
  1497. return osd_req;
  1498. }
  1499. static void rbd_osd_req_destroy(struct ceph_osd_request *osd_req)
  1500. {
  1501. ceph_osdc_put_request(osd_req);
  1502. }
  1503. /* object_name is assumed to be a non-null pointer and NUL-terminated */
  1504. static struct rbd_obj_request *rbd_obj_request_create(const char *object_name,
  1505. u64 offset, u64 length,
  1506. enum obj_request_type type)
  1507. {
  1508. struct rbd_obj_request *obj_request;
  1509. size_t size;
  1510. char *name;
  1511. rbd_assert(obj_request_type_valid(type));
  1512. size = strlen(object_name) + 1;
  1513. name = kmalloc(size, GFP_KERNEL);
  1514. if (!name)
  1515. return NULL;
  1516. obj_request = kmem_cache_zalloc(rbd_obj_request_cache, GFP_KERNEL);
  1517. if (!obj_request) {
  1518. kfree(name);
  1519. return NULL;
  1520. }
  1521. obj_request->object_name = memcpy(name, object_name, size);
  1522. obj_request->offset = offset;
  1523. obj_request->length = length;
  1524. obj_request->flags = 0;
  1525. obj_request->which = BAD_WHICH;
  1526. obj_request->type = type;
  1527. INIT_LIST_HEAD(&obj_request->links);
  1528. init_completion(&obj_request->completion);
  1529. kref_init(&obj_request->kref);
  1530. dout("%s: \"%s\" %llu/%llu %d -> obj %p\n", __func__, object_name,
  1531. offset, length, (int)type, obj_request);
  1532. return obj_request;
  1533. }
  1534. static void rbd_obj_request_destroy(struct kref *kref)
  1535. {
  1536. struct rbd_obj_request *obj_request;
  1537. obj_request = container_of(kref, struct rbd_obj_request, kref);
  1538. dout("%s: obj %p\n", __func__, obj_request);
  1539. rbd_assert(obj_request->img_request == NULL);
  1540. rbd_assert(obj_request->which == BAD_WHICH);
  1541. if (obj_request->osd_req)
  1542. rbd_osd_req_destroy(obj_request->osd_req);
  1543. rbd_assert(obj_request_type_valid(obj_request->type));
  1544. switch (obj_request->type) {
  1545. case OBJ_REQUEST_NODATA:
  1546. break; /* Nothing to do */
  1547. case OBJ_REQUEST_BIO:
  1548. if (obj_request->bio_list)
  1549. bio_chain_put(obj_request->bio_list);
  1550. break;
  1551. case OBJ_REQUEST_PAGES:
  1552. if (obj_request->pages)
  1553. ceph_release_page_vector(obj_request->pages,
  1554. obj_request->page_count);
  1555. break;
  1556. }
  1557. kfree(obj_request->object_name);
  1558. obj_request->object_name = NULL;
  1559. kmem_cache_free(rbd_obj_request_cache, obj_request);
  1560. }
  1561. /* It's OK to call this for a device with no parent */
  1562. static void rbd_spec_put(struct rbd_spec *spec);
  1563. static void rbd_dev_unparent(struct rbd_device *rbd_dev)
  1564. {
  1565. rbd_dev_remove_parent(rbd_dev);
  1566. rbd_spec_put(rbd_dev->parent_spec);
  1567. rbd_dev->parent_spec = NULL;
  1568. rbd_dev->parent_overlap = 0;
  1569. }
  1570. /*
  1571. * Parent image reference counting is used to determine when an
  1572. * image's parent fields can be safely torn down--after there are no
  1573. * more in-flight requests to the parent image. When the last
  1574. * reference is dropped, cleaning them up is safe.
  1575. */
  1576. static void rbd_dev_parent_put(struct rbd_device *rbd_dev)
  1577. {
  1578. int counter;
  1579. if (!rbd_dev->parent_spec)
  1580. return;
  1581. counter = atomic_dec_return_safe(&rbd_dev->parent_ref);
  1582. if (counter > 0)
  1583. return;
  1584. /* Last reference; clean up parent data structures */
  1585. if (!counter)
  1586. rbd_dev_unparent(rbd_dev);
  1587. else
  1588. rbd_warn(rbd_dev, "parent reference underflow\n");
  1589. }
  1590. /*
  1591. * If an image has a non-zero parent overlap, get a reference to its
  1592. * parent.
  1593. *
  1594. * We must get the reference before checking for the overlap to
  1595. * coordinate properly with zeroing the parent overlap in
  1596. * rbd_dev_v2_parent_info() when an image gets flattened. We
  1597. * drop it again if there is no overlap.
  1598. *
  1599. * Returns true if the rbd device has a parent with a non-zero
  1600. * overlap and a reference for it was successfully taken, or
  1601. * false otherwise.
  1602. */
  1603. static bool rbd_dev_parent_get(struct rbd_device *rbd_dev)
  1604. {
  1605. int counter;
  1606. if (!rbd_dev->parent_spec)
  1607. return false;
  1608. counter = atomic_inc_return_safe(&rbd_dev->parent_ref);
  1609. if (counter > 0 && rbd_dev->parent_overlap)
  1610. return true;
  1611. /* Image was flattened, but parent is not yet torn down */
  1612. if (counter < 0)
  1613. rbd_warn(rbd_dev, "parent reference overflow\n");
  1614. return false;
  1615. }
  1616. /*
  1617. * Caller is responsible for filling in the list of object requests
  1618. * that comprises the image request, and the Linux request pointer
  1619. * (if there is one).
  1620. */
  1621. static struct rbd_img_request *rbd_img_request_create(
  1622. struct rbd_device *rbd_dev,
  1623. u64 offset, u64 length,
  1624. bool write_request)
  1625. {
  1626. struct rbd_img_request *img_request;
  1627. img_request = kmem_cache_alloc(rbd_img_request_cache, GFP_ATOMIC);
  1628. if (!img_request)
  1629. return NULL;
  1630. if (write_request) {
  1631. down_read(&rbd_dev->header_rwsem);
  1632. ceph_get_snap_context(rbd_dev->header.snapc);
  1633. up_read(&rbd_dev->header_rwsem);
  1634. }
  1635. img_request->rq = NULL;
  1636. img_request->rbd_dev = rbd_dev;
  1637. img_request->offset = offset;
  1638. img_request->length = length;
  1639. img_request->flags = 0;
  1640. if (write_request) {
  1641. img_request_write_set(img_request);
  1642. img_request->snapc = rbd_dev->header.snapc;
  1643. } else {
  1644. img_request->snap_id = rbd_dev->spec->snap_id;
  1645. }
  1646. if (rbd_dev_parent_get(rbd_dev))
  1647. img_request_layered_set(img_request);
  1648. spin_lock_init(&img_request->completion_lock);
  1649. img_request->next_completion = 0;
  1650. img_request->callback = NULL;
  1651. img_request->result = 0;
  1652. img_request->obj_request_count = 0;
  1653. INIT_LIST_HEAD(&img_request->obj_requests);
  1654. kref_init(&img_request->kref);
  1655. dout("%s: rbd_dev %p %s %llu/%llu -> img %p\n", __func__, rbd_dev,
  1656. write_request ? "write" : "read", offset, length,
  1657. img_request);
  1658. return img_request;
  1659. }
  1660. static void rbd_img_request_destroy(struct kref *kref)
  1661. {
  1662. struct rbd_img_request *img_request;
  1663. struct rbd_obj_request *obj_request;
  1664. struct rbd_obj_request *next_obj_request;
  1665. img_request = container_of(kref, struct rbd_img_request, kref);
  1666. dout("%s: img %p\n", __func__, img_request);
  1667. for_each_obj_request_safe(img_request, obj_request, next_obj_request)
  1668. rbd_img_obj_request_del(img_request, obj_request);
  1669. rbd_assert(img_request->obj_request_count == 0);
  1670. if (img_request_layered_test(img_request)) {
  1671. img_request_layered_clear(img_request);
  1672. rbd_dev_parent_put(img_request->rbd_dev);
  1673. }
  1674. if (img_request_write_test(img_request))
  1675. ceph_put_snap_context(img_request->snapc);
  1676. kmem_cache_free(rbd_img_request_cache, img_request);
  1677. }
  1678. static struct rbd_img_request *rbd_parent_request_create(
  1679. struct rbd_obj_request *obj_request,
  1680. u64 img_offset, u64 length)
  1681. {
  1682. struct rbd_img_request *parent_request;
  1683. struct rbd_device *rbd_dev;
  1684. rbd_assert(obj_request->img_request);
  1685. rbd_dev = obj_request->img_request->rbd_dev;
  1686. parent_request = rbd_img_request_create(rbd_dev->parent,
  1687. img_offset, length, false);
  1688. if (!parent_request)
  1689. return NULL;
  1690. img_request_child_set(parent_request);
  1691. rbd_obj_request_get(obj_request);
  1692. parent_request->obj_request = obj_request;
  1693. return parent_request;
  1694. }
  1695. static void rbd_parent_request_destroy(struct kref *kref)
  1696. {
  1697. struct rbd_img_request *parent_request;
  1698. struct rbd_obj_request *orig_request;
  1699. parent_request = container_of(kref, struct rbd_img_request, kref);
  1700. orig_request = parent_request->obj_request;
  1701. parent_request->obj_request = NULL;
  1702. rbd_obj_request_put(orig_request);
  1703. img_request_child_clear(parent_request);
  1704. rbd_img_request_destroy(kref);
  1705. }
  1706. static bool rbd_img_obj_end_request(struct rbd_obj_request *obj_request)
  1707. {
  1708. struct rbd_img_request *img_request;
  1709. unsigned int xferred;
  1710. int result;
  1711. bool more;
  1712. rbd_assert(obj_request_img_data_test(obj_request));
  1713. img_request = obj_request->img_request;
  1714. rbd_assert(obj_request->xferred <= (u64)UINT_MAX);
  1715. xferred = (unsigned int)obj_request->xferred;
  1716. result = obj_request->result;
  1717. if (result) {
  1718. struct rbd_device *rbd_dev = img_request->rbd_dev;
  1719. rbd_warn(rbd_dev, "%s %llx at %llx (%llx)\n",
  1720. img_request_write_test(img_request) ? "write" : "read",
  1721. obj_request->length, obj_request->img_offset,
  1722. obj_request->offset);
  1723. rbd_warn(rbd_dev, " result %d xferred %x\n",
  1724. result, xferred);
  1725. if (!img_request->result)
  1726. img_request->result = result;
  1727. }
  1728. /* Image object requests don't own their page array */
  1729. if (obj_request->type == OBJ_REQUEST_PAGES) {
  1730. obj_request->pages = NULL;
  1731. obj_request->page_count = 0;
  1732. }
  1733. if (img_request_child_test(img_request)) {
  1734. rbd_assert(img_request->obj_request != NULL);
  1735. more = obj_request->which < img_request->obj_request_count - 1;
  1736. } else {
  1737. rbd_assert(img_request->rq != NULL);
  1738. more = blk_end_request(img_request->rq, result, xferred);
  1739. }
  1740. return more;
  1741. }
  1742. static void rbd_img_obj_callback(struct rbd_obj_request *obj_request)
  1743. {
  1744. struct rbd_img_request *img_request;
  1745. u32 which = obj_request->which;
  1746. bool more = true;
  1747. rbd_assert(obj_request_img_data_test(obj_request));
  1748. img_request = obj_request->img_request;
  1749. dout("%s: img %p obj %p\n", __func__, img_request, obj_request);
  1750. rbd_assert(img_request != NULL);
  1751. rbd_assert(img_request->obj_request_count > 0);
  1752. rbd_assert(which != BAD_WHICH);
  1753. rbd_assert(which < img_request->obj_request_count);
  1754. rbd_assert(which >= img_request->next_completion);
  1755. spin_lock_irq(&img_request->completion_lock);
  1756. if (which != img_request->next_completion)
  1757. goto out;
  1758. for_each_obj_request_from(img_request, obj_request) {
  1759. rbd_assert(more);
  1760. rbd_assert(which < img_request->obj_request_count);
  1761. if (!obj_request_done_test(obj_request))
  1762. break;
  1763. more = rbd_img_obj_end_request(obj_request);
  1764. which++;
  1765. }
  1766. rbd_assert(more ^ (which == img_request->obj_request_count));
  1767. img_request->next_completion = which;
  1768. out:
  1769. spin_unlock_irq(&img_request->completion_lock);
  1770. if (!more)
  1771. rbd_img_request_complete(img_request);
  1772. }
  1773. /*
  1774. * Split up an image request into one or more object requests, each
  1775. * to a different object. The "type" parameter indicates whether
  1776. * "data_desc" is the pointer to the head of a list of bio
  1777. * structures, or the base of a page array. In either case this
  1778. * function assumes data_desc describes memory sufficient to hold
  1779. * all data described by the image request.
  1780. */
  1781. static int rbd_img_request_fill(struct rbd_img_request *img_request,
  1782. enum obj_request_type type,
  1783. void *data_desc)
  1784. {
  1785. struct rbd_device *rbd_dev = img_request->rbd_dev;
  1786. struct rbd_obj_request *obj_request = NULL;
  1787. struct rbd_obj_request *next_obj_request;
  1788. bool write_request = img_request_write_test(img_request);
  1789. struct bio *bio_list = NULL;
  1790. unsigned int bio_offset = 0;
  1791. struct page **pages = NULL;
  1792. u64 img_offset;
  1793. u64 resid;
  1794. u16 opcode;
  1795. dout("%s: img %p type %d data_desc %p\n", __func__, img_request,
  1796. (int)type, data_desc);
  1797. opcode = write_request ? CEPH_OSD_OP_WRITE : CEPH_OSD_OP_READ;
  1798. img_offset = img_request->offset;
  1799. resid = img_request->length;
  1800. rbd_assert(resid > 0);
  1801. if (type == OBJ_REQUEST_BIO) {
  1802. bio_list = data_desc;
  1803. rbd_assert(img_offset == bio_list->bi_sector << SECTOR_SHIFT);
  1804. } else {
  1805. rbd_assert(type == OBJ_REQUEST_PAGES);
  1806. pages = data_desc;
  1807. }
  1808. while (resid) {
  1809. struct ceph_osd_request *osd_req;
  1810. const char *object_name;
  1811. u64 offset;
  1812. u64 length;
  1813. object_name = rbd_segment_name(rbd_dev, img_offset);
  1814. if (!object_name)
  1815. goto out_unwind;
  1816. offset = rbd_segment_offset(rbd_dev, img_offset);
  1817. length = rbd_segment_length(rbd_dev, img_offset, resid);
  1818. obj_request = rbd_obj_request_create(object_name,
  1819. offset, length, type);
  1820. /* object request has its own copy of the object name */
  1821. rbd_segment_name_free(object_name);
  1822. if (!obj_request)
  1823. goto out_unwind;
  1824. /*
  1825. * set obj_request->img_request before creating the
  1826. * osd_request so that it gets the right snapc
  1827. */
  1828. rbd_img_obj_request_add(img_request, obj_request);
  1829. if (type == OBJ_REQUEST_BIO) {
  1830. unsigned int clone_size;
  1831. rbd_assert(length <= (u64)UINT_MAX);
  1832. clone_size = (unsigned int)length;
  1833. obj_request->bio_list =
  1834. bio_chain_clone_range(&bio_list,
  1835. &bio_offset,
  1836. clone_size,
  1837. GFP_ATOMIC);
  1838. if (!obj_request->bio_list)
  1839. goto out_partial;
  1840. } else {
  1841. unsigned int page_count;
  1842. obj_request->pages = pages;
  1843. page_count = (u32)calc_pages_for(offset, length);
  1844. obj_request->page_count = page_count;
  1845. if ((offset + length) & ~PAGE_MASK)
  1846. page_count--; /* more on last page */
  1847. pages += page_count;
  1848. }
  1849. osd_req = rbd_osd_req_create(rbd_dev, write_request,
  1850. obj_request);
  1851. if (!osd_req)
  1852. goto out_partial;
  1853. obj_request->osd_req = osd_req;
  1854. obj_request->callback = rbd_img_obj_callback;
  1855. osd_req_op_extent_init(osd_req, 0, opcode, offset, length,
  1856. 0, 0);
  1857. if (type == OBJ_REQUEST_BIO)
  1858. osd_req_op_extent_osd_data_bio(osd_req, 0,
  1859. obj_request->bio_list, length);
  1860. else
  1861. osd_req_op_extent_osd_data_pages(osd_req, 0,
  1862. obj_request->pages, length,
  1863. offset & ~PAGE_MASK, false, false);
  1864. if (write_request)
  1865. rbd_osd_req_format_write(obj_request);
  1866. else
  1867. rbd_osd_req_format_read(obj_request);
  1868. obj_request->img_offset = img_offset;
  1869. img_offset += length;
  1870. resid -= length;
  1871. }
  1872. return 0;
  1873. out_partial:
  1874. rbd_obj_request_put(obj_request);
  1875. out_unwind:
  1876. for_each_obj_request_safe(img_request, obj_request, next_obj_request)
  1877. rbd_obj_request_put(obj_request);
  1878. return -ENOMEM;
  1879. }
  1880. static void
  1881. rbd_img_obj_copyup_callback(struct rbd_obj_request *obj_request)
  1882. {
  1883. struct rbd_img_request *img_request;
  1884. struct rbd_device *rbd_dev;
  1885. struct page **pages;
  1886. u32 page_count;
  1887. rbd_assert(obj_request->type == OBJ_REQUEST_BIO);
  1888. rbd_assert(obj_request_img_data_test(obj_request));
  1889. img_request = obj_request->img_request;
  1890. rbd_assert(img_request);
  1891. rbd_dev = img_request->rbd_dev;
  1892. rbd_assert(rbd_dev);
  1893. pages = obj_request->copyup_pages;
  1894. rbd_assert(pages != NULL);
  1895. obj_request->copyup_pages = NULL;
  1896. page_count = obj_request->copyup_page_count;
  1897. rbd_assert(page_count);
  1898. obj_request->copyup_page_count = 0;
  1899. ceph_release_page_vector(pages, page_count);
  1900. /*
  1901. * We want the transfer count to reflect the size of the
  1902. * original write request. There is no such thing as a
  1903. * successful short write, so if the request was successful
  1904. * we can just set it to the originally-requested length.
  1905. */
  1906. if (!obj_request->result)
  1907. obj_request->xferred = obj_request->length;
  1908. /* Finish up with the normal image object callback */
  1909. rbd_img_obj_callback(obj_request);
  1910. }
  1911. static void
  1912. rbd_img_obj_parent_read_full_callback(struct rbd_img_request *img_request)
  1913. {
  1914. struct rbd_obj_request *orig_request;
  1915. struct ceph_osd_request *osd_req;
  1916. struct ceph_osd_client *osdc;
  1917. struct rbd_device *rbd_dev;
  1918. struct page **pages;
  1919. u32 page_count;
  1920. int img_result;
  1921. u64 parent_length;
  1922. u64 offset;
  1923. u64 length;
  1924. rbd_assert(img_request_child_test(img_request));
  1925. /* First get what we need from the image request */
  1926. pages = img_request->copyup_pages;
  1927. rbd_assert(pages != NULL);
  1928. img_request->copyup_pages = NULL;
  1929. page_count = img_request->copyup_page_count;
  1930. rbd_assert(page_count);
  1931. img_request->copyup_page_count = 0;
  1932. orig_request = img_request->obj_request;
  1933. rbd_assert(orig_request != NULL);
  1934. rbd_assert(obj_request_type_valid(orig_request->type));
  1935. img_result = img_request->result;
  1936. parent_length = img_request->length;
  1937. rbd_assert(parent_length == img_request->xferred);
  1938. rbd_img_request_put(img_request);
  1939. rbd_assert(orig_request->img_request);
  1940. rbd_dev = orig_request->img_request->rbd_dev;
  1941. rbd_assert(rbd_dev);
  1942. /*
  1943. * If the overlap has become 0 (most likely because the
  1944. * image has been flattened) we need to free the pages
  1945. * and re-submit the original write request.
  1946. */
  1947. if (!rbd_dev->parent_overlap) {
  1948. struct ceph_osd_client *osdc;
  1949. ceph_release_page_vector(pages, page_count);
  1950. osdc = &rbd_dev->rbd_client->client->osdc;
  1951. img_result = rbd_obj_request_submit(osdc, orig_request);
  1952. if (!img_result)
  1953. return;
  1954. }
  1955. if (img_result)
  1956. goto out_err;
  1957. /*
  1958. * The original osd request is of no use to use any more.
  1959. * We need a new one that can hold the two ops in a copyup
  1960. * request. Allocate the new copyup osd request for the
  1961. * original request, and release the old one.
  1962. */
  1963. img_result = -ENOMEM;
  1964. osd_req = rbd_osd_req_create_copyup(orig_request);
  1965. if (!osd_req)
  1966. goto out_err;
  1967. rbd_osd_req_destroy(orig_request->osd_req);
  1968. orig_request->osd_req = osd_req;
  1969. orig_request->copyup_pages = pages;
  1970. orig_request->copyup_page_count = page_count;
  1971. /* Initialize the copyup op */
  1972. osd_req_op_cls_init(osd_req, 0, CEPH_OSD_OP_CALL, "rbd", "copyup");
  1973. osd_req_op_cls_request_data_pages(osd_req, 0, pages, parent_length, 0,
  1974. false, false);
  1975. /* Then the original write request op */
  1976. offset = orig_request->offset;
  1977. length = orig_request->length;
  1978. osd_req_op_extent_init(osd_req, 1, CEPH_OSD_OP_WRITE,
  1979. offset, length, 0, 0);
  1980. if (orig_request->type == OBJ_REQUEST_BIO)
  1981. osd_req_op_extent_osd_data_bio(osd_req, 1,
  1982. orig_request->bio_list, length);
  1983. else
  1984. osd_req_op_extent_osd_data_pages(osd_req, 1,
  1985. orig_request->pages, length,
  1986. offset & ~PAGE_MASK, false, false);
  1987. rbd_osd_req_format_write(orig_request);
  1988. /* All set, send it off. */
  1989. orig_request->callback = rbd_img_obj_copyup_callback;
  1990. osdc = &rbd_dev->rbd_client->client->osdc;
  1991. img_result = rbd_obj_request_submit(osdc, orig_request);
  1992. if (!img_result)
  1993. return;
  1994. out_err:
  1995. /* Record the error code and complete the request */
  1996. orig_request->result = img_result;
  1997. orig_request->xferred = 0;
  1998. obj_request_done_set(orig_request);
  1999. rbd_obj_request_complete(orig_request);
  2000. }
  2001. /*
  2002. * Read from the parent image the range of data that covers the
  2003. * entire target of the given object request. This is used for
  2004. * satisfying a layered image write request when the target of an
  2005. * object request from the image request does not exist.
  2006. *
  2007. * A page array big enough to hold the returned data is allocated
  2008. * and supplied to rbd_img_request_fill() as the "data descriptor."
  2009. * When the read completes, this page array will be transferred to
  2010. * the original object request for the copyup operation.
  2011. *
  2012. * If an error occurs, record it as the result of the original
  2013. * object request and mark it done so it gets completed.
  2014. */
  2015. static int rbd_img_obj_parent_read_full(struct rbd_obj_request *obj_request)
  2016. {
  2017. struct rbd_img_request *img_request = NULL;
  2018. struct rbd_img_request *parent_request = NULL;
  2019. struct rbd_device *rbd_dev;
  2020. u64 img_offset;
  2021. u64 length;
  2022. struct page **pages = NULL;
  2023. u32 page_count;
  2024. int result;
  2025. rbd_assert(obj_request_img_data_test(obj_request));
  2026. rbd_assert(obj_request_type_valid(obj_request->type));
  2027. img_request = obj_request->img_request;
  2028. rbd_assert(img_request != NULL);
  2029. rbd_dev = img_request->rbd_dev;
  2030. rbd_assert(rbd_dev->parent != NULL);
  2031. /*
  2032. * Determine the byte range covered by the object in the
  2033. * child image to which the original request was to be sent.
  2034. */
  2035. img_offset = obj_request->img_offset - obj_request->offset;
  2036. length = (u64)1 << rbd_dev->header.obj_order;
  2037. /*
  2038. * There is no defined parent data beyond the parent
  2039. * overlap, so limit what we read at that boundary if
  2040. * necessary.
  2041. */
  2042. if (img_offset + length > rbd_dev->parent_overlap) {
  2043. rbd_assert(img_offset < rbd_dev->parent_overlap);
  2044. length = rbd_dev->parent_overlap - img_offset;
  2045. }
  2046. /*
  2047. * Allocate a page array big enough to receive the data read
  2048. * from the parent.
  2049. */
  2050. page_count = (u32)calc_pages_for(0, length);
  2051. pages = ceph_alloc_page_vector(page_count, GFP_KERNEL);
  2052. if (IS_ERR(pages)) {
  2053. result = PTR_ERR(pages);
  2054. pages = NULL;
  2055. goto out_err;
  2056. }
  2057. result = -ENOMEM;
  2058. parent_request = rbd_parent_request_create(obj_request,
  2059. img_offset, length);
  2060. if (!parent_request)
  2061. goto out_err;
  2062. result = rbd_img_request_fill(parent_request, OBJ_REQUEST_PAGES, pages);
  2063. if (result)
  2064. goto out_err;
  2065. parent_request->copyup_pages = pages;
  2066. parent_request->copyup_page_count = page_count;
  2067. parent_request->callback = rbd_img_obj_parent_read_full_callback;
  2068. result = rbd_img_request_submit(parent_request);
  2069. if (!result)
  2070. return 0;
  2071. parent_request->copyup_pages = NULL;
  2072. parent_request->copyup_page_count = 0;
  2073. parent_request->obj_request = NULL;
  2074. rbd_obj_request_put(obj_request);
  2075. out_err:
  2076. if (pages)
  2077. ceph_release_page_vector(pages, page_count);
  2078. if (parent_request)
  2079. rbd_img_request_put(parent_request);
  2080. obj_request->result = result;
  2081. obj_request->xferred = 0;
  2082. obj_request_done_set(obj_request);
  2083. return result;
  2084. }
  2085. static void rbd_img_obj_exists_callback(struct rbd_obj_request *obj_request)
  2086. {
  2087. struct rbd_obj_request *orig_request;
  2088. struct rbd_device *rbd_dev;
  2089. int result;
  2090. rbd_assert(!obj_request_img_data_test(obj_request));
  2091. /*
  2092. * All we need from the object request is the original
  2093. * request and the result of the STAT op. Grab those, then
  2094. * we're done with the request.
  2095. */
  2096. orig_request = obj_request->obj_request;
  2097. obj_request->obj_request = NULL;
  2098. rbd_obj_request_put(orig_request);
  2099. rbd_assert(orig_request);
  2100. rbd_assert(orig_request->img_request);
  2101. result = obj_request->result;
  2102. obj_request->result = 0;
  2103. dout("%s: obj %p for obj %p result %d %llu/%llu\n", __func__,
  2104. obj_request, orig_request, result,
  2105. obj_request->xferred, obj_request->length);
  2106. rbd_obj_request_put(obj_request);
  2107. /*
  2108. * If the overlap has become 0 (most likely because the
  2109. * image has been flattened) we need to free the pages
  2110. * and re-submit the original write request.
  2111. */
  2112. rbd_dev = orig_request->img_request->rbd_dev;
  2113. if (!rbd_dev->parent_overlap) {
  2114. struct ceph_osd_client *osdc;
  2115. osdc = &rbd_dev->rbd_client->client->osdc;
  2116. result = rbd_obj_request_submit(osdc, orig_request);
  2117. if (!result)
  2118. return;
  2119. }
  2120. /*
  2121. * Our only purpose here is to determine whether the object
  2122. * exists, and we don't want to treat the non-existence as
  2123. * an error. If something else comes back, transfer the
  2124. * error to the original request and complete it now.
  2125. */
  2126. if (!result) {
  2127. obj_request_existence_set(orig_request, true);
  2128. } else if (result == -ENOENT) {
  2129. obj_request_existence_set(orig_request, false);
  2130. } else if (result) {
  2131. orig_request->result = result;
  2132. goto out;
  2133. }
  2134. /*
  2135. * Resubmit the original request now that we have recorded
  2136. * whether the target object exists.
  2137. */
  2138. orig_request->result = rbd_img_obj_request_submit(orig_request);
  2139. out:
  2140. if (orig_request->result)
  2141. rbd_obj_request_complete(orig_request);
  2142. }
  2143. static int rbd_img_obj_exists_submit(struct rbd_obj_request *obj_request)
  2144. {
  2145. struct rbd_obj_request *stat_request;
  2146. struct rbd_device *rbd_dev;
  2147. struct ceph_osd_client *osdc;
  2148. struct page **pages = NULL;
  2149. u32 page_count;
  2150. size_t size;
  2151. int ret;
  2152. /*
  2153. * The response data for a STAT call consists of:
  2154. * le64 length;
  2155. * struct {
  2156. * le32 tv_sec;
  2157. * le32 tv_nsec;
  2158. * } mtime;
  2159. */
  2160. size = sizeof (__le64) + sizeof (__le32) + sizeof (__le32);
  2161. page_count = (u32)calc_pages_for(0, size);
  2162. pages = ceph_alloc_page_vector(page_count, GFP_KERNEL);
  2163. if (IS_ERR(pages))
  2164. return PTR_ERR(pages);
  2165. ret = -ENOMEM;
  2166. stat_request = rbd_obj_request_create(obj_request->object_name, 0, 0,
  2167. OBJ_REQUEST_PAGES);
  2168. if (!stat_request)
  2169. goto out;
  2170. rbd_obj_request_get(obj_request);
  2171. stat_request->obj_request = obj_request;
  2172. stat_request->pages = pages;
  2173. stat_request->page_count = page_count;
  2174. rbd_assert(obj_request->img_request);
  2175. rbd_dev = obj_request->img_request->rbd_dev;
  2176. stat_request->osd_req = rbd_osd_req_create(rbd_dev, false,
  2177. stat_request);
  2178. if (!stat_request->osd_req)
  2179. goto out;
  2180. stat_request->callback = rbd_img_obj_exists_callback;
  2181. osd_req_op_init(stat_request->osd_req, 0, CEPH_OSD_OP_STAT);
  2182. osd_req_op_raw_data_in_pages(stat_request->osd_req, 0, pages, size, 0,
  2183. false, false);
  2184. rbd_osd_req_format_read(stat_request);
  2185. osdc = &rbd_dev->rbd_client->client->osdc;
  2186. ret = rbd_obj_request_submit(osdc, stat_request);
  2187. out:
  2188. if (ret)
  2189. rbd_obj_request_put(obj_request);
  2190. return ret;
  2191. }
  2192. static int rbd_img_obj_request_submit(struct rbd_obj_request *obj_request)
  2193. {
  2194. struct rbd_img_request *img_request;
  2195. struct rbd_device *rbd_dev;
  2196. bool known;
  2197. rbd_assert(obj_request_img_data_test(obj_request));
  2198. img_request = obj_request->img_request;
  2199. rbd_assert(img_request);
  2200. rbd_dev = img_request->rbd_dev;
  2201. /*
  2202. * Only writes to layered images need special handling.
  2203. * Reads and non-layered writes are simple object requests.
  2204. * Layered writes that start beyond the end of the overlap
  2205. * with the parent have no parent data, so they too are
  2206. * simple object requests. Finally, if the target object is
  2207. * known to already exist, its parent data has already been
  2208. * copied, so a write to the object can also be handled as a
  2209. * simple object request.
  2210. */
  2211. if (!img_request_write_test(img_request) ||
  2212. !img_request_layered_test(img_request) ||
  2213. rbd_dev->parent_overlap <= obj_request->img_offset ||
  2214. ((known = obj_request_known_test(obj_request)) &&
  2215. obj_request_exists_test(obj_request))) {
  2216. struct rbd_device *rbd_dev;
  2217. struct ceph_osd_client *osdc;
  2218. rbd_dev = obj_request->img_request->rbd_dev;
  2219. osdc = &rbd_dev->rbd_client->client->osdc;
  2220. return rbd_obj_request_submit(osdc, obj_request);
  2221. }
  2222. /*
  2223. * It's a layered write. The target object might exist but
  2224. * we may not know that yet. If we know it doesn't exist,
  2225. * start by reading the data for the full target object from
  2226. * the parent so we can use it for a copyup to the target.
  2227. */
  2228. if (known)
  2229. return rbd_img_obj_parent_read_full(obj_request);
  2230. /* We don't know whether the target exists. Go find out. */
  2231. return rbd_img_obj_exists_submit(obj_request);
  2232. }
  2233. static int rbd_img_request_submit(struct rbd_img_request *img_request)
  2234. {
  2235. struct rbd_obj_request *obj_request;
  2236. struct rbd_obj_request *next_obj_request;
  2237. dout("%s: img %p\n", __func__, img_request);
  2238. for_each_obj_request_safe(img_request, obj_request, next_obj_request) {
  2239. int ret;
  2240. ret = rbd_img_obj_request_submit(obj_request);
  2241. if (ret)
  2242. return ret;
  2243. }
  2244. return 0;
  2245. }
  2246. static void rbd_img_parent_read_callback(struct rbd_img_request *img_request)
  2247. {
  2248. struct rbd_obj_request *obj_request;
  2249. struct rbd_device *rbd_dev;
  2250. u64 obj_end;
  2251. u64 img_xferred;
  2252. int img_result;
  2253. rbd_assert(img_request_child_test(img_request));
  2254. /* First get what we need from the image request and release it */
  2255. obj_request = img_request->obj_request;
  2256. img_xferred = img_request->xferred;
  2257. img_result = img_request->result;
  2258. rbd_img_request_put(img_request);
  2259. /*
  2260. * If the overlap has become 0 (most likely because the
  2261. * image has been flattened) we need to re-submit the
  2262. * original request.
  2263. */
  2264. rbd_assert(obj_request);
  2265. rbd_assert(obj_request->img_request);
  2266. rbd_dev = obj_request->img_request->rbd_dev;
  2267. if (!rbd_dev->parent_overlap) {
  2268. struct ceph_osd_client *osdc;
  2269. osdc = &rbd_dev->rbd_client->client->osdc;
  2270. img_result = rbd_obj_request_submit(osdc, obj_request);
  2271. if (!img_result)
  2272. return;
  2273. }
  2274. obj_request->result = img_result;
  2275. if (obj_request->result)
  2276. goto out;
  2277. /*
  2278. * We need to zero anything beyond the parent overlap
  2279. * boundary. Since rbd_img_obj_request_read_callback()
  2280. * will zero anything beyond the end of a short read, an
  2281. * easy way to do this is to pretend the data from the
  2282. * parent came up short--ending at the overlap boundary.
  2283. */
  2284. rbd_assert(obj_request->img_offset < U64_MAX - obj_request->length);
  2285. obj_end = obj_request->img_offset + obj_request->length;
  2286. if (obj_end > rbd_dev->parent_overlap) {
  2287. u64 xferred = 0;
  2288. if (obj_request->img_offset < rbd_dev->parent_overlap)
  2289. xferred = rbd_dev->parent_overlap -
  2290. obj_request->img_offset;
  2291. obj_request->xferred = min(img_xferred, xferred);
  2292. } else {
  2293. obj_request->xferred = img_xferred;
  2294. }
  2295. out:
  2296. rbd_img_obj_request_read_callback(obj_request);
  2297. rbd_obj_request_complete(obj_request);
  2298. }
  2299. static void rbd_img_parent_read(struct rbd_obj_request *obj_request)
  2300. {
  2301. struct rbd_img_request *img_request;
  2302. int result;
  2303. rbd_assert(obj_request_img_data_test(obj_request));
  2304. rbd_assert(obj_request->img_request != NULL);
  2305. rbd_assert(obj_request->result == (s32) -ENOENT);
  2306. rbd_assert(obj_request_type_valid(obj_request->type));
  2307. /* rbd_read_finish(obj_request, obj_request->length); */
  2308. img_request = rbd_parent_request_create(obj_request,
  2309. obj_request->img_offset,
  2310. obj_request->length);
  2311. result = -ENOMEM;
  2312. if (!img_request)
  2313. goto out_err;
  2314. if (obj_request->type == OBJ_REQUEST_BIO)
  2315. result = rbd_img_request_fill(img_request, OBJ_REQUEST_BIO,
  2316. obj_request->bio_list);
  2317. else
  2318. result = rbd_img_request_fill(img_request, OBJ_REQUEST_PAGES,
  2319. obj_request->pages);
  2320. if (result)
  2321. goto out_err;
  2322. img_request->callback = rbd_img_parent_read_callback;
  2323. result = rbd_img_request_submit(img_request);
  2324. if (result)
  2325. goto out_err;
  2326. return;
  2327. out_err:
  2328. if (img_request)
  2329. rbd_img_request_put(img_request);
  2330. obj_request->result = result;
  2331. obj_request->xferred = 0;
  2332. obj_request_done_set(obj_request);
  2333. }
  2334. static int rbd_obj_notify_ack_sync(struct rbd_device *rbd_dev, u64 notify_id)
  2335. {
  2336. struct rbd_obj_request *obj_request;
  2337. struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
  2338. int ret;
  2339. obj_request = rbd_obj_request_create(rbd_dev->header_name, 0, 0,
  2340. OBJ_REQUEST_NODATA);
  2341. if (!obj_request)
  2342. return -ENOMEM;
  2343. ret = -ENOMEM;
  2344. obj_request->osd_req = rbd_osd_req_create(rbd_dev, false, obj_request);
  2345. if (!obj_request->osd_req)
  2346. goto out;
  2347. osd_req_op_watch_init(obj_request->osd_req, 0, CEPH_OSD_OP_NOTIFY_ACK,
  2348. notify_id, 0, 0);
  2349. rbd_osd_req_format_read(obj_request);
  2350. ret = rbd_obj_request_submit(osdc, obj_request);
  2351. if (ret)
  2352. goto out;
  2353. ret = rbd_obj_request_wait(obj_request);
  2354. out:
  2355. rbd_obj_request_put(obj_request);
  2356. return ret;
  2357. }
  2358. static void rbd_watch_cb(u64 ver, u64 notify_id, u8 opcode, void *data)
  2359. {
  2360. struct rbd_device *rbd_dev = (struct rbd_device *)data;
  2361. int ret;
  2362. if (!rbd_dev)
  2363. return;
  2364. dout("%s: \"%s\" notify_id %llu opcode %u\n", __func__,
  2365. rbd_dev->header_name, (unsigned long long)notify_id,
  2366. (unsigned int)opcode);
  2367. ret = rbd_dev_refresh(rbd_dev);
  2368. if (ret)
  2369. rbd_warn(rbd_dev, "header refresh error (%d)\n", ret);
  2370. rbd_obj_notify_ack_sync(rbd_dev, notify_id);
  2371. }
  2372. /*
  2373. * Request sync osd watch/unwatch. The value of "start" determines
  2374. * whether a watch request is being initiated or torn down.
  2375. */
  2376. static int rbd_dev_header_watch_sync(struct rbd_device *rbd_dev, bool start)
  2377. {
  2378. struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
  2379. struct rbd_obj_request *obj_request;
  2380. int ret;
  2381. rbd_assert(start ^ !!rbd_dev->watch_event);
  2382. rbd_assert(start ^ !!rbd_dev->watch_request);
  2383. if (start) {
  2384. ret = ceph_osdc_create_event(osdc, rbd_watch_cb, rbd_dev,
  2385. &rbd_dev->watch_event);
  2386. if (ret < 0)
  2387. return ret;
  2388. rbd_assert(rbd_dev->watch_event != NULL);
  2389. }
  2390. ret = -ENOMEM;
  2391. obj_request = rbd_obj_request_create(rbd_dev->header_name, 0, 0,
  2392. OBJ_REQUEST_NODATA);
  2393. if (!obj_request)
  2394. goto out_cancel;
  2395. obj_request->osd_req = rbd_osd_req_create(rbd_dev, true, obj_request);
  2396. if (!obj_request->osd_req)
  2397. goto out_cancel;
  2398. if (start)
  2399. ceph_osdc_set_request_linger(osdc, obj_request->osd_req);
  2400. else
  2401. ceph_osdc_unregister_linger_request(osdc,
  2402. rbd_dev->watch_request->osd_req);
  2403. osd_req_op_watch_init(obj_request->osd_req, 0, CEPH_OSD_OP_WATCH,
  2404. rbd_dev->watch_event->cookie, 0, start ? 1 : 0);
  2405. rbd_osd_req_format_write(obj_request);
  2406. ret = rbd_obj_request_submit(osdc, obj_request);
  2407. if (ret)
  2408. goto out_cancel;
  2409. ret = rbd_obj_request_wait(obj_request);
  2410. if (ret)
  2411. goto out_cancel;
  2412. ret = obj_request->result;
  2413. if (ret)
  2414. goto out_cancel;
  2415. /*
  2416. * A watch request is set to linger, so the underlying osd
  2417. * request won't go away until we unregister it. We retain
  2418. * a pointer to the object request during that time (in
  2419. * rbd_dev->watch_request), so we'll keep a reference to
  2420. * it. We'll drop that reference (below) after we've
  2421. * unregistered it.
  2422. */
  2423. if (start) {
  2424. rbd_dev->watch_request = obj_request;
  2425. return 0;
  2426. }
  2427. /* We have successfully torn down the watch request */
  2428. rbd_obj_request_put(rbd_dev->watch_request);
  2429. rbd_dev->watch_request = NULL;
  2430. out_cancel:
  2431. /* Cancel the event if we're tearing down, or on error */
  2432. ceph_osdc_cancel_event(rbd_dev->watch_event);
  2433. rbd_dev->watch_event = NULL;
  2434. if (obj_request)
  2435. rbd_obj_request_put(obj_request);
  2436. return ret;
  2437. }
  2438. /*
  2439. * Synchronous osd object method call. Returns the number of bytes
  2440. * returned in the outbound buffer, or a negative error code.
  2441. */
  2442. static int rbd_obj_method_sync(struct rbd_device *rbd_dev,
  2443. const char *object_name,
  2444. const char *class_name,
  2445. const char *method_name,
  2446. const void *outbound,
  2447. size_t outbound_size,
  2448. void *inbound,
  2449. size_t inbound_size)
  2450. {
  2451. struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
  2452. struct rbd_obj_request *obj_request;
  2453. struct page **pages;
  2454. u32 page_count;
  2455. int ret;
  2456. /*
  2457. * Method calls are ultimately read operations. The result
  2458. * should placed into the inbound buffer provided. They
  2459. * also supply outbound data--parameters for the object
  2460. * method. Currently if this is present it will be a
  2461. * snapshot id.
  2462. */
  2463. page_count = (u32)calc_pages_for(0, inbound_size);
  2464. pages = ceph_alloc_page_vector(page_count, GFP_KERNEL);
  2465. if (IS_ERR(pages))
  2466. return PTR_ERR(pages);
  2467. ret = -ENOMEM;
  2468. obj_request = rbd_obj_request_create(object_name, 0, inbound_size,
  2469. OBJ_REQUEST_PAGES);
  2470. if (!obj_request)
  2471. goto out;
  2472. obj_request->pages = pages;
  2473. obj_request->page_count = page_count;
  2474. obj_request->osd_req = rbd_osd_req_create(rbd_dev, false, obj_request);
  2475. if (!obj_request->osd_req)
  2476. goto out;
  2477. osd_req_op_cls_init(obj_request->osd_req, 0, CEPH_OSD_OP_CALL,
  2478. class_name, method_name);
  2479. if (outbound_size) {
  2480. struct ceph_pagelist *pagelist;
  2481. pagelist = kmalloc(sizeof (*pagelist), GFP_NOFS);
  2482. if (!pagelist)
  2483. goto out;
  2484. ceph_pagelist_init(pagelist);
  2485. ceph_pagelist_append(pagelist, outbound, outbound_size);
  2486. osd_req_op_cls_request_data_pagelist(obj_request->osd_req, 0,
  2487. pagelist);
  2488. }
  2489. osd_req_op_cls_response_data_pages(obj_request->osd_req, 0,
  2490. obj_request->pages, inbound_size,
  2491. 0, false, false);
  2492. rbd_osd_req_format_read(obj_request);
  2493. ret = rbd_obj_request_submit(osdc, obj_request);
  2494. if (ret)
  2495. goto out;
  2496. ret = rbd_obj_request_wait(obj_request);
  2497. if (ret)
  2498. goto out;
  2499. ret = obj_request->result;
  2500. if (ret < 0)
  2501. goto out;
  2502. rbd_assert(obj_request->xferred < (u64)INT_MAX);
  2503. ret = (int)obj_request->xferred;
  2504. ceph_copy_from_page_vector(pages, inbound, 0, obj_request->xferred);
  2505. out:
  2506. if (obj_request)
  2507. rbd_obj_request_put(obj_request);
  2508. else
  2509. ceph_release_page_vector(pages, page_count);
  2510. return ret;
  2511. }
  2512. static void rbd_request_fn(struct request_queue *q)
  2513. __releases(q->queue_lock) __acquires(q->queue_lock)
  2514. {
  2515. struct rbd_device *rbd_dev = q->queuedata;
  2516. bool read_only = rbd_dev->mapping.read_only;
  2517. struct request *rq;
  2518. int result;
  2519. while ((rq = blk_fetch_request(q))) {
  2520. bool write_request = rq_data_dir(rq) == WRITE;
  2521. struct rbd_img_request *img_request;
  2522. u64 offset;
  2523. u64 length;
  2524. /* Ignore any non-FS requests that filter through. */
  2525. if (rq->cmd_type != REQ_TYPE_FS) {
  2526. dout("%s: non-fs request type %d\n", __func__,
  2527. (int) rq->cmd_type);
  2528. __blk_end_request_all(rq, 0);
  2529. continue;
  2530. }
  2531. /* Ignore/skip any zero-length requests */
  2532. offset = (u64) blk_rq_pos(rq) << SECTOR_SHIFT;
  2533. length = (u64) blk_rq_bytes(rq);
  2534. if (!length) {
  2535. dout("%s: zero-length request\n", __func__);
  2536. __blk_end_request_all(rq, 0);
  2537. continue;
  2538. }
  2539. spin_unlock_irq(q->queue_lock);
  2540. /* Disallow writes to a read-only device */
  2541. if (write_request) {
  2542. result = -EROFS;
  2543. if (read_only)
  2544. goto end_request;
  2545. rbd_assert(rbd_dev->spec->snap_id == CEPH_NOSNAP);
  2546. }
  2547. /*
  2548. * Quit early if the mapped snapshot no longer
  2549. * exists. It's still possible the snapshot will
  2550. * have disappeared by the time our request arrives
  2551. * at the osd, but there's no sense in sending it if
  2552. * we already know.
  2553. */
  2554. if (!test_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags)) {
  2555. dout("request for non-existent snapshot");
  2556. rbd_assert(rbd_dev->spec->snap_id != CEPH_NOSNAP);
  2557. result = -ENXIO;
  2558. goto end_request;
  2559. }
  2560. result = -EINVAL;
  2561. if (offset && length > U64_MAX - offset + 1) {
  2562. rbd_warn(rbd_dev, "bad request range (%llu~%llu)\n",
  2563. offset, length);
  2564. goto end_request; /* Shouldn't happen */
  2565. }
  2566. result = -EIO;
  2567. if (offset + length > rbd_dev->mapping.size) {
  2568. rbd_warn(rbd_dev, "beyond EOD (%llu~%llu > %llu)\n",
  2569. offset, length, rbd_dev->mapping.size);
  2570. goto end_request;
  2571. }
  2572. result = -ENOMEM;
  2573. img_request = rbd_img_request_create(rbd_dev, offset, length,
  2574. write_request);
  2575. if (!img_request)
  2576. goto end_request;
  2577. img_request->rq = rq;
  2578. result = rbd_img_request_fill(img_request, OBJ_REQUEST_BIO,
  2579. rq->bio);
  2580. if (!result)
  2581. result = rbd_img_request_submit(img_request);
  2582. if (result)
  2583. rbd_img_request_put(img_request);
  2584. end_request:
  2585. spin_lock_irq(q->queue_lock);
  2586. if (result < 0) {
  2587. rbd_warn(rbd_dev, "%s %llx at %llx result %d\n",
  2588. write_request ? "write" : "read",
  2589. length, offset, result);
  2590. __blk_end_request_all(rq, result);
  2591. }
  2592. }
  2593. }
  2594. /*
  2595. * a queue callback. Makes sure that we don't create a bio that spans across
  2596. * multiple osd objects. One exception would be with a single page bios,
  2597. * which we handle later at bio_chain_clone_range()
  2598. */
  2599. static int rbd_merge_bvec(struct request_queue *q, struct bvec_merge_data *bmd,
  2600. struct bio_vec *bvec)
  2601. {
  2602. struct rbd_device *rbd_dev = q->queuedata;
  2603. sector_t sector_offset;
  2604. sector_t sectors_per_obj;
  2605. sector_t obj_sector_offset;
  2606. int ret;
  2607. /*
  2608. * Find how far into its rbd object the partition-relative
  2609. * bio start sector is to offset relative to the enclosing
  2610. * device.
  2611. */
  2612. sector_offset = get_start_sect(bmd->bi_bdev) + bmd->bi_sector;
  2613. sectors_per_obj = 1 << (rbd_dev->header.obj_order - SECTOR_SHIFT);
  2614. obj_sector_offset = sector_offset & (sectors_per_obj - 1);
  2615. /*
  2616. * Compute the number of bytes from that offset to the end
  2617. * of the object. Account for what's already used by the bio.
  2618. */
  2619. ret = (int) (sectors_per_obj - obj_sector_offset) << SECTOR_SHIFT;
  2620. if (ret > bmd->bi_size)
  2621. ret -= bmd->bi_size;
  2622. else
  2623. ret = 0;
  2624. /*
  2625. * Don't send back more than was asked for. And if the bio
  2626. * was empty, let the whole thing through because: "Note
  2627. * that a block device *must* allow a single page to be
  2628. * added to an empty bio."
  2629. */
  2630. rbd_assert(bvec->bv_len <= PAGE_SIZE);
  2631. if (ret > (int) bvec->bv_len || !bmd->bi_size)
  2632. ret = (int) bvec->bv_len;
  2633. return ret;
  2634. }
  2635. static void rbd_free_disk(struct rbd_device *rbd_dev)
  2636. {
  2637. struct gendisk *disk = rbd_dev->disk;
  2638. if (!disk)
  2639. return;
  2640. rbd_dev->disk = NULL;
  2641. if (disk->flags & GENHD_FL_UP) {
  2642. del_gendisk(disk);
  2643. if (disk->queue)
  2644. blk_cleanup_queue(disk->queue);
  2645. }
  2646. put_disk(disk);
  2647. }
  2648. static int rbd_obj_read_sync(struct rbd_device *rbd_dev,
  2649. const char *object_name,
  2650. u64 offset, u64 length, void *buf)
  2651. {
  2652. struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
  2653. struct rbd_obj_request *obj_request;
  2654. struct page **pages = NULL;
  2655. u32 page_count;
  2656. size_t size;
  2657. int ret;
  2658. page_count = (u32) calc_pages_for(offset, length);
  2659. pages = ceph_alloc_page_vector(page_count, GFP_KERNEL);
  2660. if (IS_ERR(pages))
  2661. ret = PTR_ERR(pages);
  2662. ret = -ENOMEM;
  2663. obj_request = rbd_obj_request_create(object_name, offset, length,
  2664. OBJ_REQUEST_PAGES);
  2665. if (!obj_request)
  2666. goto out;
  2667. obj_request->pages = pages;
  2668. obj_request->page_count = page_count;
  2669. obj_request->osd_req = rbd_osd_req_create(rbd_dev, false, obj_request);
  2670. if (!obj_request->osd_req)
  2671. goto out;
  2672. osd_req_op_extent_init(obj_request->osd_req, 0, CEPH_OSD_OP_READ,
  2673. offset, length, 0, 0);
  2674. osd_req_op_extent_osd_data_pages(obj_request->osd_req, 0,
  2675. obj_request->pages,
  2676. obj_request->length,
  2677. obj_request->offset & ~PAGE_MASK,
  2678. false, false);
  2679. rbd_osd_req_format_read(obj_request);
  2680. ret = rbd_obj_request_submit(osdc, obj_request);
  2681. if (ret)
  2682. goto out;
  2683. ret = rbd_obj_request_wait(obj_request);
  2684. if (ret)
  2685. goto out;
  2686. ret = obj_request->result;
  2687. if (ret < 0)
  2688. goto out;
  2689. rbd_assert(obj_request->xferred <= (u64) SIZE_MAX);
  2690. size = (size_t) obj_request->xferred;
  2691. ceph_copy_from_page_vector(pages, buf, 0, size);
  2692. rbd_assert(size <= (size_t)INT_MAX);
  2693. ret = (int)size;
  2694. out:
  2695. if (obj_request)
  2696. rbd_obj_request_put(obj_request);
  2697. else
  2698. ceph_release_page_vector(pages, page_count);
  2699. return ret;
  2700. }
  2701. /*
  2702. * Read the complete header for the given rbd device. On successful
  2703. * return, the rbd_dev->header field will contain up-to-date
  2704. * information about the image.
  2705. */
  2706. static int rbd_dev_v1_header_info(struct rbd_device *rbd_dev)
  2707. {
  2708. struct rbd_image_header_ondisk *ondisk = NULL;
  2709. u32 snap_count = 0;
  2710. u64 names_size = 0;
  2711. u32 want_count;
  2712. int ret;
  2713. /*
  2714. * The complete header will include an array of its 64-bit
  2715. * snapshot ids, followed by the names of those snapshots as
  2716. * a contiguous block of NUL-terminated strings. Note that
  2717. * the number of snapshots could change by the time we read
  2718. * it in, in which case we re-read it.
  2719. */
  2720. do {
  2721. size_t size;
  2722. kfree(ondisk);
  2723. size = sizeof (*ondisk);
  2724. size += snap_count * sizeof (struct rbd_image_snap_ondisk);
  2725. size += names_size;
  2726. ondisk = kmalloc(size, GFP_KERNEL);
  2727. if (!ondisk)
  2728. return -ENOMEM;
  2729. ret = rbd_obj_read_sync(rbd_dev, rbd_dev->header_name,
  2730. 0, size, ondisk);
  2731. if (ret < 0)
  2732. goto out;
  2733. if ((size_t)ret < size) {
  2734. ret = -ENXIO;
  2735. rbd_warn(rbd_dev, "short header read (want %zd got %d)",
  2736. size, ret);
  2737. goto out;
  2738. }
  2739. if (!rbd_dev_ondisk_valid(ondisk)) {
  2740. ret = -ENXIO;
  2741. rbd_warn(rbd_dev, "invalid header");
  2742. goto out;
  2743. }
  2744. names_size = le64_to_cpu(ondisk->snap_names_len);
  2745. want_count = snap_count;
  2746. snap_count = le32_to_cpu(ondisk->snap_count);
  2747. } while (snap_count != want_count);
  2748. ret = rbd_header_from_disk(rbd_dev, ondisk);
  2749. out:
  2750. kfree(ondisk);
  2751. return ret;
  2752. }
  2753. /*
  2754. * Clear the rbd device's EXISTS flag if the snapshot it's mapped to
  2755. * has disappeared from the (just updated) snapshot context.
  2756. */
  2757. static void rbd_exists_validate(struct rbd_device *rbd_dev)
  2758. {
  2759. u64 snap_id;
  2760. if (!test_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags))
  2761. return;
  2762. snap_id = rbd_dev->spec->snap_id;
  2763. if (snap_id == CEPH_NOSNAP)
  2764. return;
  2765. if (rbd_dev_snap_index(rbd_dev, snap_id) == BAD_SNAP_INDEX)
  2766. clear_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags);
  2767. }
  2768. static void rbd_dev_update_size(struct rbd_device *rbd_dev)
  2769. {
  2770. sector_t size;
  2771. bool removing;
  2772. /*
  2773. * Don't hold the lock while doing disk operations,
  2774. * or lock ordering will conflict with the bdev mutex via:
  2775. * rbd_add() -> blkdev_get() -> rbd_open()
  2776. */
  2777. spin_lock_irq(&rbd_dev->lock);
  2778. removing = test_bit(RBD_DEV_FLAG_REMOVING, &rbd_dev->flags);
  2779. spin_unlock_irq(&rbd_dev->lock);
  2780. /*
  2781. * If the device is being removed, rbd_dev->disk has
  2782. * been destroyed, so don't try to update its size
  2783. */
  2784. if (!removing) {
  2785. size = (sector_t)rbd_dev->mapping.size / SECTOR_SIZE;
  2786. dout("setting size to %llu sectors", (unsigned long long)size);
  2787. set_capacity(rbd_dev->disk, size);
  2788. revalidate_disk(rbd_dev->disk);
  2789. }
  2790. }
  2791. static int rbd_dev_refresh(struct rbd_device *rbd_dev)
  2792. {
  2793. u64 mapping_size;
  2794. int ret;
  2795. rbd_assert(rbd_image_format_valid(rbd_dev->image_format));
  2796. down_write(&rbd_dev->header_rwsem);
  2797. mapping_size = rbd_dev->mapping.size;
  2798. if (rbd_dev->image_format == 1)
  2799. ret = rbd_dev_v1_header_info(rbd_dev);
  2800. else
  2801. ret = rbd_dev_v2_header_info(rbd_dev);
  2802. /* If it's a mapped snapshot, validate its EXISTS flag */
  2803. rbd_exists_validate(rbd_dev);
  2804. up_write(&rbd_dev->header_rwsem);
  2805. if (mapping_size != rbd_dev->mapping.size) {
  2806. rbd_dev_update_size(rbd_dev);
  2807. }
  2808. return ret;
  2809. }
  2810. static int rbd_init_disk(struct rbd_device *rbd_dev)
  2811. {
  2812. struct gendisk *disk;
  2813. struct request_queue *q;
  2814. u64 segment_size;
  2815. /* create gendisk info */
  2816. disk = alloc_disk(RBD_MINORS_PER_MAJOR);
  2817. if (!disk)
  2818. return -ENOMEM;
  2819. snprintf(disk->disk_name, sizeof(disk->disk_name), RBD_DRV_NAME "%d",
  2820. rbd_dev->dev_id);
  2821. disk->major = rbd_dev->major;
  2822. disk->first_minor = 0;
  2823. disk->fops = &rbd_bd_ops;
  2824. disk->private_data = rbd_dev;
  2825. q = blk_init_queue(rbd_request_fn, &rbd_dev->lock);
  2826. if (!q)
  2827. goto out_disk;
  2828. /* We use the default size, but let's be explicit about it. */
  2829. blk_queue_physical_block_size(q, SECTOR_SIZE);
  2830. /* set io sizes to object size */
  2831. segment_size = rbd_obj_bytes(&rbd_dev->header);
  2832. blk_queue_max_hw_sectors(q, segment_size / SECTOR_SIZE);
  2833. blk_queue_max_segment_size(q, segment_size);
  2834. blk_queue_io_min(q, segment_size);
  2835. blk_queue_io_opt(q, segment_size);
  2836. blk_queue_merge_bvec(q, rbd_merge_bvec);
  2837. disk->queue = q;
  2838. q->queuedata = rbd_dev;
  2839. rbd_dev->disk = disk;
  2840. return 0;
  2841. out_disk:
  2842. put_disk(disk);
  2843. return -ENOMEM;
  2844. }
  2845. /*
  2846. sysfs
  2847. */
  2848. static struct rbd_device *dev_to_rbd_dev(struct device *dev)
  2849. {
  2850. return container_of(dev, struct rbd_device, dev);
  2851. }
  2852. static ssize_t rbd_size_show(struct device *dev,
  2853. struct device_attribute *attr, char *buf)
  2854. {
  2855. struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
  2856. return sprintf(buf, "%llu\n",
  2857. (unsigned long long)rbd_dev->mapping.size);
  2858. }
  2859. /*
  2860. * Note this shows the features for whatever's mapped, which is not
  2861. * necessarily the base image.
  2862. */
  2863. static ssize_t rbd_features_show(struct device *dev,
  2864. struct device_attribute *attr, char *buf)
  2865. {
  2866. struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
  2867. return sprintf(buf, "0x%016llx\n",
  2868. (unsigned long long)rbd_dev->mapping.features);
  2869. }
  2870. static ssize_t rbd_major_show(struct device *dev,
  2871. struct device_attribute *attr, char *buf)
  2872. {
  2873. struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
  2874. if (rbd_dev->major)
  2875. return sprintf(buf, "%d\n", rbd_dev->major);
  2876. return sprintf(buf, "(none)\n");
  2877. }
  2878. static ssize_t rbd_client_id_show(struct device *dev,
  2879. struct device_attribute *attr, char *buf)
  2880. {
  2881. struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
  2882. return sprintf(buf, "client%lld\n",
  2883. ceph_client_id(rbd_dev->rbd_client->client));
  2884. }
  2885. static ssize_t rbd_pool_show(struct device *dev,
  2886. struct device_attribute *attr, char *buf)
  2887. {
  2888. struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
  2889. return sprintf(buf, "%s\n", rbd_dev->spec->pool_name);
  2890. }
  2891. static ssize_t rbd_pool_id_show(struct device *dev,
  2892. struct device_attribute *attr, char *buf)
  2893. {
  2894. struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
  2895. return sprintf(buf, "%llu\n",
  2896. (unsigned long long) rbd_dev->spec->pool_id);
  2897. }
  2898. static ssize_t rbd_name_show(struct device *dev,
  2899. struct device_attribute *attr, char *buf)
  2900. {
  2901. struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
  2902. if (rbd_dev->spec->image_name)
  2903. return sprintf(buf, "%s\n", rbd_dev->spec->image_name);
  2904. return sprintf(buf, "(unknown)\n");
  2905. }
  2906. static ssize_t rbd_image_id_show(struct device *dev,
  2907. struct device_attribute *attr, char *buf)
  2908. {
  2909. struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
  2910. return sprintf(buf, "%s\n", rbd_dev->spec->image_id);
  2911. }
  2912. /*
  2913. * Shows the name of the currently-mapped snapshot (or
  2914. * RBD_SNAP_HEAD_NAME for the base image).
  2915. */
  2916. static ssize_t rbd_snap_show(struct device *dev,
  2917. struct device_attribute *attr,
  2918. char *buf)
  2919. {
  2920. struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
  2921. return sprintf(buf, "%s\n", rbd_dev->spec->snap_name);
  2922. }
  2923. /*
  2924. * For an rbd v2 image, shows the pool id, image id, and snapshot id
  2925. * for the parent image. If there is no parent, simply shows
  2926. * "(no parent image)".
  2927. */
  2928. static ssize_t rbd_parent_show(struct device *dev,
  2929. struct device_attribute *attr,
  2930. char *buf)
  2931. {
  2932. struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
  2933. struct rbd_spec *spec = rbd_dev->parent_spec;
  2934. int count;
  2935. char *bufp = buf;
  2936. if (!spec)
  2937. return sprintf(buf, "(no parent image)\n");
  2938. count = sprintf(bufp, "pool_id %llu\npool_name %s\n",
  2939. (unsigned long long) spec->pool_id, spec->pool_name);
  2940. if (count < 0)
  2941. return count;
  2942. bufp += count;
  2943. count = sprintf(bufp, "image_id %s\nimage_name %s\n", spec->image_id,
  2944. spec->image_name ? spec->image_name : "(unknown)");
  2945. if (count < 0)
  2946. return count;
  2947. bufp += count;
  2948. count = sprintf(bufp, "snap_id %llu\nsnap_name %s\n",
  2949. (unsigned long long) spec->snap_id, spec->snap_name);
  2950. if (count < 0)
  2951. return count;
  2952. bufp += count;
  2953. count = sprintf(bufp, "overlap %llu\n", rbd_dev->parent_overlap);
  2954. if (count < 0)
  2955. return count;
  2956. bufp += count;
  2957. return (ssize_t) (bufp - buf);
  2958. }
  2959. static ssize_t rbd_image_refresh(struct device *dev,
  2960. struct device_attribute *attr,
  2961. const char *buf,
  2962. size_t size)
  2963. {
  2964. struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
  2965. int ret;
  2966. ret = rbd_dev_refresh(rbd_dev);
  2967. if (ret)
  2968. rbd_warn(rbd_dev, ": manual header refresh error (%d)\n", ret);
  2969. return ret < 0 ? ret : size;
  2970. }
  2971. static DEVICE_ATTR(size, S_IRUGO, rbd_size_show, NULL);
  2972. static DEVICE_ATTR(features, S_IRUGO, rbd_features_show, NULL);
  2973. static DEVICE_ATTR(major, S_IRUGO, rbd_major_show, NULL);
  2974. static DEVICE_ATTR(client_id, S_IRUGO, rbd_client_id_show, NULL);
  2975. static DEVICE_ATTR(pool, S_IRUGO, rbd_pool_show, NULL);
  2976. static DEVICE_ATTR(pool_id, S_IRUGO, rbd_pool_id_show, NULL);
  2977. static DEVICE_ATTR(name, S_IRUGO, rbd_name_show, NULL);
  2978. static DEVICE_ATTR(image_id, S_IRUGO, rbd_image_id_show, NULL);
  2979. static DEVICE_ATTR(refresh, S_IWUSR, NULL, rbd_image_refresh);
  2980. static DEVICE_ATTR(current_snap, S_IRUGO, rbd_snap_show, NULL);
  2981. static DEVICE_ATTR(parent, S_IRUGO, rbd_parent_show, NULL);
  2982. static struct attribute *rbd_attrs[] = {
  2983. &dev_attr_size.attr,
  2984. &dev_attr_features.attr,
  2985. &dev_attr_major.attr,
  2986. &dev_attr_client_id.attr,
  2987. &dev_attr_pool.attr,
  2988. &dev_attr_pool_id.attr,
  2989. &dev_attr_name.attr,
  2990. &dev_attr_image_id.attr,
  2991. &dev_attr_current_snap.attr,
  2992. &dev_attr_parent.attr,
  2993. &dev_attr_refresh.attr,
  2994. NULL
  2995. };
  2996. static struct attribute_group rbd_attr_group = {
  2997. .attrs = rbd_attrs,
  2998. };
  2999. static const struct attribute_group *rbd_attr_groups[] = {
  3000. &rbd_attr_group,
  3001. NULL
  3002. };
  3003. static void rbd_sysfs_dev_release(struct device *dev)
  3004. {
  3005. }
  3006. static struct device_type rbd_device_type = {
  3007. .name = "rbd",
  3008. .groups = rbd_attr_groups,
  3009. .release = rbd_sysfs_dev_release,
  3010. };
  3011. static struct rbd_spec *rbd_spec_get(struct rbd_spec *spec)
  3012. {
  3013. kref_get(&spec->kref);
  3014. return spec;
  3015. }
  3016. static void rbd_spec_free(struct kref *kref);
  3017. static void rbd_spec_put(struct rbd_spec *spec)
  3018. {
  3019. if (spec)
  3020. kref_put(&spec->kref, rbd_spec_free);
  3021. }
  3022. static struct rbd_spec *rbd_spec_alloc(void)
  3023. {
  3024. struct rbd_spec *spec;
  3025. spec = kzalloc(sizeof (*spec), GFP_KERNEL);
  3026. if (!spec)
  3027. return NULL;
  3028. kref_init(&spec->kref);
  3029. return spec;
  3030. }
  3031. static void rbd_spec_free(struct kref *kref)
  3032. {
  3033. struct rbd_spec *spec = container_of(kref, struct rbd_spec, kref);
  3034. kfree(spec->pool_name);
  3035. kfree(spec->image_id);
  3036. kfree(spec->image_name);
  3037. kfree(spec->snap_name);
  3038. kfree(spec);
  3039. }
  3040. static struct rbd_device *rbd_dev_create(struct rbd_client *rbdc,
  3041. struct rbd_spec *spec)
  3042. {
  3043. struct rbd_device *rbd_dev;
  3044. rbd_dev = kzalloc(sizeof (*rbd_dev), GFP_KERNEL);
  3045. if (!rbd_dev)
  3046. return NULL;
  3047. spin_lock_init(&rbd_dev->lock);
  3048. rbd_dev->flags = 0;
  3049. atomic_set(&rbd_dev->parent_ref, 0);
  3050. INIT_LIST_HEAD(&rbd_dev->node);
  3051. init_rwsem(&rbd_dev->header_rwsem);
  3052. rbd_dev->spec = spec;
  3053. rbd_dev->rbd_client = rbdc;
  3054. /* Initialize the layout used for all rbd requests */
  3055. rbd_dev->layout.fl_stripe_unit = cpu_to_le32(1 << RBD_MAX_OBJ_ORDER);
  3056. rbd_dev->layout.fl_stripe_count = cpu_to_le32(1);
  3057. rbd_dev->layout.fl_object_size = cpu_to_le32(1 << RBD_MAX_OBJ_ORDER);
  3058. rbd_dev->layout.fl_pg_pool = cpu_to_le32((u32) spec->pool_id);
  3059. return rbd_dev;
  3060. }
  3061. static void rbd_dev_destroy(struct rbd_device *rbd_dev)
  3062. {
  3063. rbd_put_client(rbd_dev->rbd_client);
  3064. rbd_spec_put(rbd_dev->spec);
  3065. kfree(rbd_dev);
  3066. }
  3067. /*
  3068. * Get the size and object order for an image snapshot, or if
  3069. * snap_id is CEPH_NOSNAP, gets this information for the base
  3070. * image.
  3071. */
  3072. static int _rbd_dev_v2_snap_size(struct rbd_device *rbd_dev, u64 snap_id,
  3073. u8 *order, u64 *snap_size)
  3074. {
  3075. __le64 snapid = cpu_to_le64(snap_id);
  3076. int ret;
  3077. struct {
  3078. u8 order;
  3079. __le64 size;
  3080. } __attribute__ ((packed)) size_buf = { 0 };
  3081. ret = rbd_obj_method_sync(rbd_dev, rbd_dev->header_name,
  3082. "rbd", "get_size",
  3083. &snapid, sizeof (snapid),
  3084. &size_buf, sizeof (size_buf));
  3085. dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
  3086. if (ret < 0)
  3087. return ret;
  3088. if (ret < sizeof (size_buf))
  3089. return -ERANGE;
  3090. if (order) {
  3091. *order = size_buf.order;
  3092. dout(" order %u", (unsigned int)*order);
  3093. }
  3094. *snap_size = le64_to_cpu(size_buf.size);
  3095. dout(" snap_id 0x%016llx snap_size = %llu\n",
  3096. (unsigned long long)snap_id,
  3097. (unsigned long long)*snap_size);
  3098. return 0;
  3099. }
  3100. static int rbd_dev_v2_image_size(struct rbd_device *rbd_dev)
  3101. {
  3102. return _rbd_dev_v2_snap_size(rbd_dev, CEPH_NOSNAP,
  3103. &rbd_dev->header.obj_order,
  3104. &rbd_dev->header.image_size);
  3105. }
  3106. static int rbd_dev_v2_object_prefix(struct rbd_device *rbd_dev)
  3107. {
  3108. void *reply_buf;
  3109. int ret;
  3110. void *p;
  3111. reply_buf = kzalloc(RBD_OBJ_PREFIX_LEN_MAX, GFP_KERNEL);
  3112. if (!reply_buf)
  3113. return -ENOMEM;
  3114. ret = rbd_obj_method_sync(rbd_dev, rbd_dev->header_name,
  3115. "rbd", "get_object_prefix", NULL, 0,
  3116. reply_buf, RBD_OBJ_PREFIX_LEN_MAX);
  3117. dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
  3118. if (ret < 0)
  3119. goto out;
  3120. p = reply_buf;
  3121. rbd_dev->header.object_prefix = ceph_extract_encoded_string(&p,
  3122. p + ret, NULL, GFP_NOIO);
  3123. ret = 0;
  3124. if (IS_ERR(rbd_dev->header.object_prefix)) {
  3125. ret = PTR_ERR(rbd_dev->header.object_prefix);
  3126. rbd_dev->header.object_prefix = NULL;
  3127. } else {
  3128. dout(" object_prefix = %s\n", rbd_dev->header.object_prefix);
  3129. }
  3130. out:
  3131. kfree(reply_buf);
  3132. return ret;
  3133. }
  3134. static int _rbd_dev_v2_snap_features(struct rbd_device *rbd_dev, u64 snap_id,
  3135. u64 *snap_features)
  3136. {
  3137. __le64 snapid = cpu_to_le64(snap_id);
  3138. struct {
  3139. __le64 features;
  3140. __le64 incompat;
  3141. } __attribute__ ((packed)) features_buf = { 0 };
  3142. u64 incompat;
  3143. int ret;
  3144. ret = rbd_obj_method_sync(rbd_dev, rbd_dev->header_name,
  3145. "rbd", "get_features",
  3146. &snapid, sizeof (snapid),
  3147. &features_buf, sizeof (features_buf));
  3148. dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
  3149. if (ret < 0)
  3150. return ret;
  3151. if (ret < sizeof (features_buf))
  3152. return -ERANGE;
  3153. incompat = le64_to_cpu(features_buf.incompat);
  3154. if (incompat & ~RBD_FEATURES_SUPPORTED)
  3155. return -ENXIO;
  3156. *snap_features = le64_to_cpu(features_buf.features);
  3157. dout(" snap_id 0x%016llx features = 0x%016llx incompat = 0x%016llx\n",
  3158. (unsigned long long)snap_id,
  3159. (unsigned long long)*snap_features,
  3160. (unsigned long long)le64_to_cpu(features_buf.incompat));
  3161. return 0;
  3162. }
  3163. static int rbd_dev_v2_features(struct rbd_device *rbd_dev)
  3164. {
  3165. return _rbd_dev_v2_snap_features(rbd_dev, CEPH_NOSNAP,
  3166. &rbd_dev->header.features);
  3167. }
  3168. static int rbd_dev_v2_parent_info(struct rbd_device *rbd_dev)
  3169. {
  3170. struct rbd_spec *parent_spec;
  3171. size_t size;
  3172. void *reply_buf = NULL;
  3173. __le64 snapid;
  3174. void *p;
  3175. void *end;
  3176. u64 pool_id;
  3177. char *image_id;
  3178. u64 snap_id;
  3179. u64 overlap;
  3180. int ret;
  3181. parent_spec = rbd_spec_alloc();
  3182. if (!parent_spec)
  3183. return -ENOMEM;
  3184. size = sizeof (__le64) + /* pool_id */
  3185. sizeof (__le32) + RBD_IMAGE_ID_LEN_MAX + /* image_id */
  3186. sizeof (__le64) + /* snap_id */
  3187. sizeof (__le64); /* overlap */
  3188. reply_buf = kmalloc(size, GFP_KERNEL);
  3189. if (!reply_buf) {
  3190. ret = -ENOMEM;
  3191. goto out_err;
  3192. }
  3193. snapid = cpu_to_le64(CEPH_NOSNAP);
  3194. ret = rbd_obj_method_sync(rbd_dev, rbd_dev->header_name,
  3195. "rbd", "get_parent",
  3196. &snapid, sizeof (snapid),
  3197. reply_buf, size);
  3198. dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
  3199. if (ret < 0)
  3200. goto out_err;
  3201. p = reply_buf;
  3202. end = reply_buf + ret;
  3203. ret = -ERANGE;
  3204. ceph_decode_64_safe(&p, end, pool_id, out_err);
  3205. if (pool_id == CEPH_NOPOOL) {
  3206. /*
  3207. * Either the parent never existed, or we have
  3208. * record of it but the image got flattened so it no
  3209. * longer has a parent. When the parent of a
  3210. * layered image disappears we immediately set the
  3211. * overlap to 0. The effect of this is that all new
  3212. * requests will be treated as if the image had no
  3213. * parent.
  3214. */
  3215. if (rbd_dev->parent_overlap) {
  3216. rbd_dev->parent_overlap = 0;
  3217. smp_mb();
  3218. rbd_dev_parent_put(rbd_dev);
  3219. pr_info("%s: clone image has been flattened\n",
  3220. rbd_dev->disk->disk_name);
  3221. }
  3222. goto out; /* No parent? No problem. */
  3223. }
  3224. /* The ceph file layout needs to fit pool id in 32 bits */
  3225. ret = -EIO;
  3226. if (pool_id > (u64)U32_MAX) {
  3227. rbd_warn(NULL, "parent pool id too large (%llu > %u)\n",
  3228. (unsigned long long)pool_id, U32_MAX);
  3229. goto out_err;
  3230. }
  3231. image_id = ceph_extract_encoded_string(&p, end, NULL, GFP_KERNEL);
  3232. if (IS_ERR(image_id)) {
  3233. ret = PTR_ERR(image_id);
  3234. goto out_err;
  3235. }
  3236. ceph_decode_64_safe(&p, end, snap_id, out_err);
  3237. ceph_decode_64_safe(&p, end, overlap, out_err);
  3238. /*
  3239. * The parent won't change (except when the clone is
  3240. * flattened, already handled that). So we only need to
  3241. * record the parent spec we have not already done so.
  3242. */
  3243. if (!rbd_dev->parent_spec) {
  3244. parent_spec->pool_id = pool_id;
  3245. parent_spec->image_id = image_id;
  3246. parent_spec->snap_id = snap_id;
  3247. rbd_dev->parent_spec = parent_spec;
  3248. parent_spec = NULL; /* rbd_dev now owns this */
  3249. }
  3250. /*
  3251. * We always update the parent overlap. If it's zero we
  3252. * treat it specially.
  3253. */
  3254. rbd_dev->parent_overlap = overlap;
  3255. smp_mb();
  3256. if (!overlap) {
  3257. /* A null parent_spec indicates it's the initial probe */
  3258. if (parent_spec) {
  3259. /*
  3260. * The overlap has become zero, so the clone
  3261. * must have been resized down to 0 at some
  3262. * point. Treat this the same as a flatten.
  3263. */
  3264. rbd_dev_parent_put(rbd_dev);
  3265. pr_info("%s: clone image now standalone\n",
  3266. rbd_dev->disk->disk_name);
  3267. } else {
  3268. /*
  3269. * For the initial probe, if we find the
  3270. * overlap is zero we just pretend there was
  3271. * no parent image.
  3272. */
  3273. rbd_warn(rbd_dev, "ignoring parent of "
  3274. "clone with overlap 0\n");
  3275. }
  3276. }
  3277. out:
  3278. ret = 0;
  3279. out_err:
  3280. kfree(reply_buf);
  3281. rbd_spec_put(parent_spec);
  3282. return ret;
  3283. }
  3284. static int rbd_dev_v2_striping_info(struct rbd_device *rbd_dev)
  3285. {
  3286. struct {
  3287. __le64 stripe_unit;
  3288. __le64 stripe_count;
  3289. } __attribute__ ((packed)) striping_info_buf = { 0 };
  3290. size_t size = sizeof (striping_info_buf);
  3291. void *p;
  3292. u64 obj_size;
  3293. u64 stripe_unit;
  3294. u64 stripe_count;
  3295. int ret;
  3296. ret = rbd_obj_method_sync(rbd_dev, rbd_dev->header_name,
  3297. "rbd", "get_stripe_unit_count", NULL, 0,
  3298. (char *)&striping_info_buf, size);
  3299. dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
  3300. if (ret < 0)
  3301. return ret;
  3302. if (ret < size)
  3303. return -ERANGE;
  3304. /*
  3305. * We don't actually support the "fancy striping" feature
  3306. * (STRIPINGV2) yet, but if the striping sizes are the
  3307. * defaults the behavior is the same as before. So find
  3308. * out, and only fail if the image has non-default values.
  3309. */
  3310. ret = -EINVAL;
  3311. obj_size = (u64)1 << rbd_dev->header.obj_order;
  3312. p = &striping_info_buf;
  3313. stripe_unit = ceph_decode_64(&p);
  3314. if (stripe_unit != obj_size) {
  3315. rbd_warn(rbd_dev, "unsupported stripe unit "
  3316. "(got %llu want %llu)",
  3317. stripe_unit, obj_size);
  3318. return -EINVAL;
  3319. }
  3320. stripe_count = ceph_decode_64(&p);
  3321. if (stripe_count != 1) {
  3322. rbd_warn(rbd_dev, "unsupported stripe count "
  3323. "(got %llu want 1)", stripe_count);
  3324. return -EINVAL;
  3325. }
  3326. rbd_dev->header.stripe_unit = stripe_unit;
  3327. rbd_dev->header.stripe_count = stripe_count;
  3328. return 0;
  3329. }
  3330. static char *rbd_dev_image_name(struct rbd_device *rbd_dev)
  3331. {
  3332. size_t image_id_size;
  3333. char *image_id;
  3334. void *p;
  3335. void *end;
  3336. size_t size;
  3337. void *reply_buf = NULL;
  3338. size_t len = 0;
  3339. char *image_name = NULL;
  3340. int ret;
  3341. rbd_assert(!rbd_dev->spec->image_name);
  3342. len = strlen(rbd_dev->spec->image_id);
  3343. image_id_size = sizeof (__le32) + len;
  3344. image_id = kmalloc(image_id_size, GFP_KERNEL);
  3345. if (!image_id)
  3346. return NULL;
  3347. p = image_id;
  3348. end = image_id + image_id_size;
  3349. ceph_encode_string(&p, end, rbd_dev->spec->image_id, (u32)len);
  3350. size = sizeof (__le32) + RBD_IMAGE_NAME_LEN_MAX;
  3351. reply_buf = kmalloc(size, GFP_KERNEL);
  3352. if (!reply_buf)
  3353. goto out;
  3354. ret = rbd_obj_method_sync(rbd_dev, RBD_DIRECTORY,
  3355. "rbd", "dir_get_name",
  3356. image_id, image_id_size,
  3357. reply_buf, size);
  3358. if (ret < 0)
  3359. goto out;
  3360. p = reply_buf;
  3361. end = reply_buf + ret;
  3362. image_name = ceph_extract_encoded_string(&p, end, &len, GFP_KERNEL);
  3363. if (IS_ERR(image_name))
  3364. image_name = NULL;
  3365. else
  3366. dout("%s: name is %s len is %zd\n", __func__, image_name, len);
  3367. out:
  3368. kfree(reply_buf);
  3369. kfree(image_id);
  3370. return image_name;
  3371. }
  3372. static u64 rbd_v1_snap_id_by_name(struct rbd_device *rbd_dev, const char *name)
  3373. {
  3374. struct ceph_snap_context *snapc = rbd_dev->header.snapc;
  3375. const char *snap_name;
  3376. u32 which = 0;
  3377. /* Skip over names until we find the one we are looking for */
  3378. snap_name = rbd_dev->header.snap_names;
  3379. while (which < snapc->num_snaps) {
  3380. if (!strcmp(name, snap_name))
  3381. return snapc->snaps[which];
  3382. snap_name += strlen(snap_name) + 1;
  3383. which++;
  3384. }
  3385. return CEPH_NOSNAP;
  3386. }
  3387. static u64 rbd_v2_snap_id_by_name(struct rbd_device *rbd_dev, const char *name)
  3388. {
  3389. struct ceph_snap_context *snapc = rbd_dev->header.snapc;
  3390. u32 which;
  3391. bool found = false;
  3392. u64 snap_id;
  3393. for (which = 0; !found && which < snapc->num_snaps; which++) {
  3394. const char *snap_name;
  3395. snap_id = snapc->snaps[which];
  3396. snap_name = rbd_dev_v2_snap_name(rbd_dev, snap_id);
  3397. if (IS_ERR(snap_name)) {
  3398. /* ignore no-longer existing snapshots */
  3399. if (PTR_ERR(snap_name) == -ENOENT)
  3400. continue;
  3401. else
  3402. break;
  3403. }
  3404. found = !strcmp(name, snap_name);
  3405. kfree(snap_name);
  3406. }
  3407. return found ? snap_id : CEPH_NOSNAP;
  3408. }
  3409. /*
  3410. * Assumes name is never RBD_SNAP_HEAD_NAME; returns CEPH_NOSNAP if
  3411. * no snapshot by that name is found, or if an error occurs.
  3412. */
  3413. static u64 rbd_snap_id_by_name(struct rbd_device *rbd_dev, const char *name)
  3414. {
  3415. if (rbd_dev->image_format == 1)
  3416. return rbd_v1_snap_id_by_name(rbd_dev, name);
  3417. return rbd_v2_snap_id_by_name(rbd_dev, name);
  3418. }
  3419. /*
  3420. * When an rbd image has a parent image, it is identified by the
  3421. * pool, image, and snapshot ids (not names). This function fills
  3422. * in the names for those ids. (It's OK if we can't figure out the
  3423. * name for an image id, but the pool and snapshot ids should always
  3424. * exist and have names.) All names in an rbd spec are dynamically
  3425. * allocated.
  3426. *
  3427. * When an image being mapped (not a parent) is probed, we have the
  3428. * pool name and pool id, image name and image id, and the snapshot
  3429. * name. The only thing we're missing is the snapshot id.
  3430. */
  3431. static int rbd_dev_spec_update(struct rbd_device *rbd_dev)
  3432. {
  3433. struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
  3434. struct rbd_spec *spec = rbd_dev->spec;
  3435. const char *pool_name;
  3436. const char *image_name;
  3437. const char *snap_name;
  3438. int ret;
  3439. /*
  3440. * An image being mapped will have the pool name (etc.), but
  3441. * we need to look up the snapshot id.
  3442. */
  3443. if (spec->pool_name) {
  3444. if (strcmp(spec->snap_name, RBD_SNAP_HEAD_NAME)) {
  3445. u64 snap_id;
  3446. snap_id = rbd_snap_id_by_name(rbd_dev, spec->snap_name);
  3447. if (snap_id == CEPH_NOSNAP)
  3448. return -ENOENT;
  3449. spec->snap_id = snap_id;
  3450. } else {
  3451. spec->snap_id = CEPH_NOSNAP;
  3452. }
  3453. return 0;
  3454. }
  3455. /* Get the pool name; we have to make our own copy of this */
  3456. pool_name = ceph_pg_pool_name_by_id(osdc->osdmap, spec->pool_id);
  3457. if (!pool_name) {
  3458. rbd_warn(rbd_dev, "no pool with id %llu", spec->pool_id);
  3459. return -EIO;
  3460. }
  3461. pool_name = kstrdup(pool_name, GFP_KERNEL);
  3462. if (!pool_name)
  3463. return -ENOMEM;
  3464. /* Fetch the image name; tolerate failure here */
  3465. image_name = rbd_dev_image_name(rbd_dev);
  3466. if (!image_name)
  3467. rbd_warn(rbd_dev, "unable to get image name");
  3468. /* Look up the snapshot name, and make a copy */
  3469. snap_name = rbd_snap_name(rbd_dev, spec->snap_id);
  3470. if (IS_ERR(snap_name)) {
  3471. ret = PTR_ERR(snap_name);
  3472. goto out_err;
  3473. }
  3474. spec->pool_name = pool_name;
  3475. spec->image_name = image_name;
  3476. spec->snap_name = snap_name;
  3477. return 0;
  3478. out_err:
  3479. kfree(image_name);
  3480. kfree(pool_name);
  3481. return ret;
  3482. }
  3483. static int rbd_dev_v2_snap_context(struct rbd_device *rbd_dev)
  3484. {
  3485. size_t size;
  3486. int ret;
  3487. void *reply_buf;
  3488. void *p;
  3489. void *end;
  3490. u64 seq;
  3491. u32 snap_count;
  3492. struct ceph_snap_context *snapc;
  3493. u32 i;
  3494. /*
  3495. * We'll need room for the seq value (maximum snapshot id),
  3496. * snapshot count, and array of that many snapshot ids.
  3497. * For now we have a fixed upper limit on the number we're
  3498. * prepared to receive.
  3499. */
  3500. size = sizeof (__le64) + sizeof (__le32) +
  3501. RBD_MAX_SNAP_COUNT * sizeof (__le64);
  3502. reply_buf = kzalloc(size, GFP_KERNEL);
  3503. if (!reply_buf)
  3504. return -ENOMEM;
  3505. ret = rbd_obj_method_sync(rbd_dev, rbd_dev->header_name,
  3506. "rbd", "get_snapcontext", NULL, 0,
  3507. reply_buf, size);
  3508. dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
  3509. if (ret < 0)
  3510. goto out;
  3511. p = reply_buf;
  3512. end = reply_buf + ret;
  3513. ret = -ERANGE;
  3514. ceph_decode_64_safe(&p, end, seq, out);
  3515. ceph_decode_32_safe(&p, end, snap_count, out);
  3516. /*
  3517. * Make sure the reported number of snapshot ids wouldn't go
  3518. * beyond the end of our buffer. But before checking that,
  3519. * make sure the computed size of the snapshot context we
  3520. * allocate is representable in a size_t.
  3521. */
  3522. if (snap_count > (SIZE_MAX - sizeof (struct ceph_snap_context))
  3523. / sizeof (u64)) {
  3524. ret = -EINVAL;
  3525. goto out;
  3526. }
  3527. if (!ceph_has_room(&p, end, snap_count * sizeof (__le64)))
  3528. goto out;
  3529. ret = 0;
  3530. snapc = ceph_create_snap_context(snap_count, GFP_KERNEL);
  3531. if (!snapc) {
  3532. ret = -ENOMEM;
  3533. goto out;
  3534. }
  3535. snapc->seq = seq;
  3536. for (i = 0; i < snap_count; i++)
  3537. snapc->snaps[i] = ceph_decode_64(&p);
  3538. ceph_put_snap_context(rbd_dev->header.snapc);
  3539. rbd_dev->header.snapc = snapc;
  3540. dout(" snap context seq = %llu, snap_count = %u\n",
  3541. (unsigned long long)seq, (unsigned int)snap_count);
  3542. out:
  3543. kfree(reply_buf);
  3544. return ret;
  3545. }
  3546. static const char *rbd_dev_v2_snap_name(struct rbd_device *rbd_dev,
  3547. u64 snap_id)
  3548. {
  3549. size_t size;
  3550. void *reply_buf;
  3551. __le64 snapid;
  3552. int ret;
  3553. void *p;
  3554. void *end;
  3555. char *snap_name;
  3556. size = sizeof (__le32) + RBD_MAX_SNAP_NAME_LEN;
  3557. reply_buf = kmalloc(size, GFP_KERNEL);
  3558. if (!reply_buf)
  3559. return ERR_PTR(-ENOMEM);
  3560. snapid = cpu_to_le64(snap_id);
  3561. ret = rbd_obj_method_sync(rbd_dev, rbd_dev->header_name,
  3562. "rbd", "get_snapshot_name",
  3563. &snapid, sizeof (snapid),
  3564. reply_buf, size);
  3565. dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
  3566. if (ret < 0) {
  3567. snap_name = ERR_PTR(ret);
  3568. goto out;
  3569. }
  3570. p = reply_buf;
  3571. end = reply_buf + ret;
  3572. snap_name = ceph_extract_encoded_string(&p, end, NULL, GFP_KERNEL);
  3573. if (IS_ERR(snap_name))
  3574. goto out;
  3575. dout(" snap_id 0x%016llx snap_name = %s\n",
  3576. (unsigned long long)snap_id, snap_name);
  3577. out:
  3578. kfree(reply_buf);
  3579. return snap_name;
  3580. }
  3581. static int rbd_dev_v2_header_info(struct rbd_device *rbd_dev)
  3582. {
  3583. bool first_time = rbd_dev->header.object_prefix == NULL;
  3584. int ret;
  3585. ret = rbd_dev_v2_image_size(rbd_dev);
  3586. if (ret)
  3587. return ret;
  3588. if (first_time) {
  3589. ret = rbd_dev_v2_header_onetime(rbd_dev);
  3590. if (ret)
  3591. return ret;
  3592. }
  3593. /*
  3594. * If the image supports layering, get the parent info. We
  3595. * need to probe the first time regardless. Thereafter we
  3596. * only need to if there's a parent, to see if it has
  3597. * disappeared due to the mapped image getting flattened.
  3598. */
  3599. if (rbd_dev->header.features & RBD_FEATURE_LAYERING &&
  3600. (first_time || rbd_dev->parent_spec)) {
  3601. bool warn;
  3602. ret = rbd_dev_v2_parent_info(rbd_dev);
  3603. if (ret)
  3604. return ret;
  3605. /*
  3606. * Print a warning if this is the initial probe and
  3607. * the image has a parent. Don't print it if the
  3608. * image now being probed is itself a parent. We
  3609. * can tell at this point because we won't know its
  3610. * pool name yet (just its pool id).
  3611. */
  3612. warn = rbd_dev->parent_spec && rbd_dev->spec->pool_name;
  3613. if (first_time && warn)
  3614. rbd_warn(rbd_dev, "WARNING: kernel layering "
  3615. "is EXPERIMENTAL!");
  3616. }
  3617. if (rbd_dev->spec->snap_id == CEPH_NOSNAP)
  3618. if (rbd_dev->mapping.size != rbd_dev->header.image_size)
  3619. rbd_dev->mapping.size = rbd_dev->header.image_size;
  3620. ret = rbd_dev_v2_snap_context(rbd_dev);
  3621. dout("rbd_dev_v2_snap_context returned %d\n", ret);
  3622. return ret;
  3623. }
  3624. static int rbd_bus_add_dev(struct rbd_device *rbd_dev)
  3625. {
  3626. struct device *dev;
  3627. int ret;
  3628. dev = &rbd_dev->dev;
  3629. dev->bus = &rbd_bus_type;
  3630. dev->type = &rbd_device_type;
  3631. dev->parent = &rbd_root_dev;
  3632. dev->release = rbd_dev_device_release;
  3633. dev_set_name(dev, "%d", rbd_dev->dev_id);
  3634. ret = device_register(dev);
  3635. return ret;
  3636. }
  3637. static void rbd_bus_del_dev(struct rbd_device *rbd_dev)
  3638. {
  3639. device_unregister(&rbd_dev->dev);
  3640. }
  3641. static atomic64_t rbd_dev_id_max = ATOMIC64_INIT(0);
  3642. /*
  3643. * Get a unique rbd identifier for the given new rbd_dev, and add
  3644. * the rbd_dev to the global list. The minimum rbd id is 1.
  3645. */
  3646. static void rbd_dev_id_get(struct rbd_device *rbd_dev)
  3647. {
  3648. rbd_dev->dev_id = atomic64_inc_return(&rbd_dev_id_max);
  3649. spin_lock(&rbd_dev_list_lock);
  3650. list_add_tail(&rbd_dev->node, &rbd_dev_list);
  3651. spin_unlock(&rbd_dev_list_lock);
  3652. dout("rbd_dev %p given dev id %llu\n", rbd_dev,
  3653. (unsigned long long) rbd_dev->dev_id);
  3654. }
  3655. /*
  3656. * Remove an rbd_dev from the global list, and record that its
  3657. * identifier is no longer in use.
  3658. */
  3659. static void rbd_dev_id_put(struct rbd_device *rbd_dev)
  3660. {
  3661. struct list_head *tmp;
  3662. int rbd_id = rbd_dev->dev_id;
  3663. int max_id;
  3664. rbd_assert(rbd_id > 0);
  3665. dout("rbd_dev %p released dev id %llu\n", rbd_dev,
  3666. (unsigned long long) rbd_dev->dev_id);
  3667. spin_lock(&rbd_dev_list_lock);
  3668. list_del_init(&rbd_dev->node);
  3669. /*
  3670. * If the id being "put" is not the current maximum, there
  3671. * is nothing special we need to do.
  3672. */
  3673. if (rbd_id != atomic64_read(&rbd_dev_id_max)) {
  3674. spin_unlock(&rbd_dev_list_lock);
  3675. return;
  3676. }
  3677. /*
  3678. * We need to update the current maximum id. Search the
  3679. * list to find out what it is. We're more likely to find
  3680. * the maximum at the end, so search the list backward.
  3681. */
  3682. max_id = 0;
  3683. list_for_each_prev(tmp, &rbd_dev_list) {
  3684. struct rbd_device *rbd_dev;
  3685. rbd_dev = list_entry(tmp, struct rbd_device, node);
  3686. if (rbd_dev->dev_id > max_id)
  3687. max_id = rbd_dev->dev_id;
  3688. }
  3689. spin_unlock(&rbd_dev_list_lock);
  3690. /*
  3691. * The max id could have been updated by rbd_dev_id_get(), in
  3692. * which case it now accurately reflects the new maximum.
  3693. * Be careful not to overwrite the maximum value in that
  3694. * case.
  3695. */
  3696. atomic64_cmpxchg(&rbd_dev_id_max, rbd_id, max_id);
  3697. dout(" max dev id has been reset\n");
  3698. }
  3699. /*
  3700. * Skips over white space at *buf, and updates *buf to point to the
  3701. * first found non-space character (if any). Returns the length of
  3702. * the token (string of non-white space characters) found. Note
  3703. * that *buf must be terminated with '\0'.
  3704. */
  3705. static inline size_t next_token(const char **buf)
  3706. {
  3707. /*
  3708. * These are the characters that produce nonzero for
  3709. * isspace() in the "C" and "POSIX" locales.
  3710. */
  3711. const char *spaces = " \f\n\r\t\v";
  3712. *buf += strspn(*buf, spaces); /* Find start of token */
  3713. return strcspn(*buf, spaces); /* Return token length */
  3714. }
  3715. /*
  3716. * Finds the next token in *buf, and if the provided token buffer is
  3717. * big enough, copies the found token into it. The result, if
  3718. * copied, is guaranteed to be terminated with '\0'. Note that *buf
  3719. * must be terminated with '\0' on entry.
  3720. *
  3721. * Returns the length of the token found (not including the '\0').
  3722. * Return value will be 0 if no token is found, and it will be >=
  3723. * token_size if the token would not fit.
  3724. *
  3725. * The *buf pointer will be updated to point beyond the end of the
  3726. * found token. Note that this occurs even if the token buffer is
  3727. * too small to hold it.
  3728. */
  3729. static inline size_t copy_token(const char **buf,
  3730. char *token,
  3731. size_t token_size)
  3732. {
  3733. size_t len;
  3734. len = next_token(buf);
  3735. if (len < token_size) {
  3736. memcpy(token, *buf, len);
  3737. *(token + len) = '\0';
  3738. }
  3739. *buf += len;
  3740. return len;
  3741. }
  3742. /*
  3743. * Finds the next token in *buf, dynamically allocates a buffer big
  3744. * enough to hold a copy of it, and copies the token into the new
  3745. * buffer. The copy is guaranteed to be terminated with '\0'. Note
  3746. * that a duplicate buffer is created even for a zero-length token.
  3747. *
  3748. * Returns a pointer to the newly-allocated duplicate, or a null
  3749. * pointer if memory for the duplicate was not available. If
  3750. * the lenp argument is a non-null pointer, the length of the token
  3751. * (not including the '\0') is returned in *lenp.
  3752. *
  3753. * If successful, the *buf pointer will be updated to point beyond
  3754. * the end of the found token.
  3755. *
  3756. * Note: uses GFP_KERNEL for allocation.
  3757. */
  3758. static inline char *dup_token(const char **buf, size_t *lenp)
  3759. {
  3760. char *dup;
  3761. size_t len;
  3762. len = next_token(buf);
  3763. dup = kmemdup(*buf, len + 1, GFP_KERNEL);
  3764. if (!dup)
  3765. return NULL;
  3766. *(dup + len) = '\0';
  3767. *buf += len;
  3768. if (lenp)
  3769. *lenp = len;
  3770. return dup;
  3771. }
  3772. /*
  3773. * Parse the options provided for an "rbd add" (i.e., rbd image
  3774. * mapping) request. These arrive via a write to /sys/bus/rbd/add,
  3775. * and the data written is passed here via a NUL-terminated buffer.
  3776. * Returns 0 if successful or an error code otherwise.
  3777. *
  3778. * The information extracted from these options is recorded in
  3779. * the other parameters which return dynamically-allocated
  3780. * structures:
  3781. * ceph_opts
  3782. * The address of a pointer that will refer to a ceph options
  3783. * structure. Caller must release the returned pointer using
  3784. * ceph_destroy_options() when it is no longer needed.
  3785. * rbd_opts
  3786. * Address of an rbd options pointer. Fully initialized by
  3787. * this function; caller must release with kfree().
  3788. * spec
  3789. * Address of an rbd image specification pointer. Fully
  3790. * initialized by this function based on parsed options.
  3791. * Caller must release with rbd_spec_put().
  3792. *
  3793. * The options passed take this form:
  3794. * <mon_addrs> <options> <pool_name> <image_name> [<snap_id>]
  3795. * where:
  3796. * <mon_addrs>
  3797. * A comma-separated list of one or more monitor addresses.
  3798. * A monitor address is an ip address, optionally followed
  3799. * by a port number (separated by a colon).
  3800. * I.e.: ip1[:port1][,ip2[:port2]...]
  3801. * <options>
  3802. * A comma-separated list of ceph and/or rbd options.
  3803. * <pool_name>
  3804. * The name of the rados pool containing the rbd image.
  3805. * <image_name>
  3806. * The name of the image in that pool to map.
  3807. * <snap_id>
  3808. * An optional snapshot id. If provided, the mapping will
  3809. * present data from the image at the time that snapshot was
  3810. * created. The image head is used if no snapshot id is
  3811. * provided. Snapshot mappings are always read-only.
  3812. */
  3813. static int rbd_add_parse_args(const char *buf,
  3814. struct ceph_options **ceph_opts,
  3815. struct rbd_options **opts,
  3816. struct rbd_spec **rbd_spec)
  3817. {
  3818. size_t len;
  3819. char *options;
  3820. const char *mon_addrs;
  3821. char *snap_name;
  3822. size_t mon_addrs_size;
  3823. struct rbd_spec *spec = NULL;
  3824. struct rbd_options *rbd_opts = NULL;
  3825. struct ceph_options *copts;
  3826. int ret;
  3827. /* The first four tokens are required */
  3828. len = next_token(&buf);
  3829. if (!len) {
  3830. rbd_warn(NULL, "no monitor address(es) provided");
  3831. return -EINVAL;
  3832. }
  3833. mon_addrs = buf;
  3834. mon_addrs_size = len + 1;
  3835. buf += len;
  3836. ret = -EINVAL;
  3837. options = dup_token(&buf, NULL);
  3838. if (!options)
  3839. return -ENOMEM;
  3840. if (!*options) {
  3841. rbd_warn(NULL, "no options provided");
  3842. goto out_err;
  3843. }
  3844. spec = rbd_spec_alloc();
  3845. if (!spec)
  3846. goto out_mem;
  3847. spec->pool_name = dup_token(&buf, NULL);
  3848. if (!spec->pool_name)
  3849. goto out_mem;
  3850. if (!*spec->pool_name) {
  3851. rbd_warn(NULL, "no pool name provided");
  3852. goto out_err;
  3853. }
  3854. spec->image_name = dup_token(&buf, NULL);
  3855. if (!spec->image_name)
  3856. goto out_mem;
  3857. if (!*spec->image_name) {
  3858. rbd_warn(NULL, "no image name provided");
  3859. goto out_err;
  3860. }
  3861. /*
  3862. * Snapshot name is optional; default is to use "-"
  3863. * (indicating the head/no snapshot).
  3864. */
  3865. len = next_token(&buf);
  3866. if (!len) {
  3867. buf = RBD_SNAP_HEAD_NAME; /* No snapshot supplied */
  3868. len = sizeof (RBD_SNAP_HEAD_NAME) - 1;
  3869. } else if (len > RBD_MAX_SNAP_NAME_LEN) {
  3870. ret = -ENAMETOOLONG;
  3871. goto out_err;
  3872. }
  3873. snap_name = kmemdup(buf, len + 1, GFP_KERNEL);
  3874. if (!snap_name)
  3875. goto out_mem;
  3876. *(snap_name + len) = '\0';
  3877. spec->snap_name = snap_name;
  3878. /* Initialize all rbd options to the defaults */
  3879. rbd_opts = kzalloc(sizeof (*rbd_opts), GFP_KERNEL);
  3880. if (!rbd_opts)
  3881. goto out_mem;
  3882. rbd_opts->read_only = RBD_READ_ONLY_DEFAULT;
  3883. copts = ceph_parse_options(options, mon_addrs,
  3884. mon_addrs + mon_addrs_size - 1,
  3885. parse_rbd_opts_token, rbd_opts);
  3886. if (IS_ERR(copts)) {
  3887. ret = PTR_ERR(copts);
  3888. goto out_err;
  3889. }
  3890. kfree(options);
  3891. *ceph_opts = copts;
  3892. *opts = rbd_opts;
  3893. *rbd_spec = spec;
  3894. return 0;
  3895. out_mem:
  3896. ret = -ENOMEM;
  3897. out_err:
  3898. kfree(rbd_opts);
  3899. rbd_spec_put(spec);
  3900. kfree(options);
  3901. return ret;
  3902. }
  3903. /*
  3904. * An rbd format 2 image has a unique identifier, distinct from the
  3905. * name given to it by the user. Internally, that identifier is
  3906. * what's used to specify the names of objects related to the image.
  3907. *
  3908. * A special "rbd id" object is used to map an rbd image name to its
  3909. * id. If that object doesn't exist, then there is no v2 rbd image
  3910. * with the supplied name.
  3911. *
  3912. * This function will record the given rbd_dev's image_id field if
  3913. * it can be determined, and in that case will return 0. If any
  3914. * errors occur a negative errno will be returned and the rbd_dev's
  3915. * image_id field will be unchanged (and should be NULL).
  3916. */
  3917. static int rbd_dev_image_id(struct rbd_device *rbd_dev)
  3918. {
  3919. int ret;
  3920. size_t size;
  3921. char *object_name;
  3922. void *response;
  3923. char *image_id;
  3924. /*
  3925. * When probing a parent image, the image id is already
  3926. * known (and the image name likely is not). There's no
  3927. * need to fetch the image id again in this case. We
  3928. * do still need to set the image format though.
  3929. */
  3930. if (rbd_dev->spec->image_id) {
  3931. rbd_dev->image_format = *rbd_dev->spec->image_id ? 2 : 1;
  3932. return 0;
  3933. }
  3934. /*
  3935. * First, see if the format 2 image id file exists, and if
  3936. * so, get the image's persistent id from it.
  3937. */
  3938. size = sizeof (RBD_ID_PREFIX) + strlen(rbd_dev->spec->image_name);
  3939. object_name = kmalloc(size, GFP_NOIO);
  3940. if (!object_name)
  3941. return -ENOMEM;
  3942. sprintf(object_name, "%s%s", RBD_ID_PREFIX, rbd_dev->spec->image_name);
  3943. dout("rbd id object name is %s\n", object_name);
  3944. /* Response will be an encoded string, which includes a length */
  3945. size = sizeof (__le32) + RBD_IMAGE_ID_LEN_MAX;
  3946. response = kzalloc(size, GFP_NOIO);
  3947. if (!response) {
  3948. ret = -ENOMEM;
  3949. goto out;
  3950. }
  3951. /* If it doesn't exist we'll assume it's a format 1 image */
  3952. ret = rbd_obj_method_sync(rbd_dev, object_name,
  3953. "rbd", "get_id", NULL, 0,
  3954. response, RBD_IMAGE_ID_LEN_MAX);
  3955. dout("%s: rbd_obj_method_sync returned %d\n", __func__, ret);
  3956. if (ret == -ENOENT) {
  3957. image_id = kstrdup("", GFP_KERNEL);
  3958. ret = image_id ? 0 : -ENOMEM;
  3959. if (!ret)
  3960. rbd_dev->image_format = 1;
  3961. } else if (ret > sizeof (__le32)) {
  3962. void *p = response;
  3963. image_id = ceph_extract_encoded_string(&p, p + ret,
  3964. NULL, GFP_NOIO);
  3965. ret = IS_ERR(image_id) ? PTR_ERR(image_id) : 0;
  3966. if (!ret)
  3967. rbd_dev->image_format = 2;
  3968. } else {
  3969. ret = -EINVAL;
  3970. }
  3971. if (!ret) {
  3972. rbd_dev->spec->image_id = image_id;
  3973. dout("image_id is %s\n", image_id);
  3974. }
  3975. out:
  3976. kfree(response);
  3977. kfree(object_name);
  3978. return ret;
  3979. }
  3980. /*
  3981. * Undo whatever state changes are made by v1 or v2 header info
  3982. * call.
  3983. */
  3984. static void rbd_dev_unprobe(struct rbd_device *rbd_dev)
  3985. {
  3986. struct rbd_image_header *header;
  3987. /* Drop parent reference unless it's already been done (or none) */
  3988. if (rbd_dev->parent_overlap)
  3989. rbd_dev_parent_put(rbd_dev);
  3990. /* Free dynamic fields from the header, then zero it out */
  3991. header = &rbd_dev->header;
  3992. ceph_put_snap_context(header->snapc);
  3993. kfree(header->snap_sizes);
  3994. kfree(header->snap_names);
  3995. kfree(header->object_prefix);
  3996. memset(header, 0, sizeof (*header));
  3997. }
  3998. static int rbd_dev_v2_header_onetime(struct rbd_device *rbd_dev)
  3999. {
  4000. int ret;
  4001. ret = rbd_dev_v2_object_prefix(rbd_dev);
  4002. if (ret)
  4003. goto out_err;
  4004. /*
  4005. * Get the and check features for the image. Currently the
  4006. * features are assumed to never change.
  4007. */
  4008. ret = rbd_dev_v2_features(rbd_dev);
  4009. if (ret)
  4010. goto out_err;
  4011. /* If the image supports fancy striping, get its parameters */
  4012. if (rbd_dev->header.features & RBD_FEATURE_STRIPINGV2) {
  4013. ret = rbd_dev_v2_striping_info(rbd_dev);
  4014. if (ret < 0)
  4015. goto out_err;
  4016. }
  4017. /* No support for crypto and compression type format 2 images */
  4018. return 0;
  4019. out_err:
  4020. rbd_dev->header.features = 0;
  4021. kfree(rbd_dev->header.object_prefix);
  4022. rbd_dev->header.object_prefix = NULL;
  4023. return ret;
  4024. }
  4025. static int rbd_dev_probe_parent(struct rbd_device *rbd_dev)
  4026. {
  4027. struct rbd_device *parent = NULL;
  4028. struct rbd_spec *parent_spec;
  4029. struct rbd_client *rbdc;
  4030. int ret;
  4031. if (!rbd_dev->parent_spec)
  4032. return 0;
  4033. /*
  4034. * We need to pass a reference to the client and the parent
  4035. * spec when creating the parent rbd_dev. Images related by
  4036. * parent/child relationships always share both.
  4037. */
  4038. parent_spec = rbd_spec_get(rbd_dev->parent_spec);
  4039. rbdc = __rbd_get_client(rbd_dev->rbd_client);
  4040. ret = -ENOMEM;
  4041. parent = rbd_dev_create(rbdc, parent_spec);
  4042. if (!parent)
  4043. goto out_err;
  4044. ret = rbd_dev_image_probe(parent, false);
  4045. if (ret < 0)
  4046. goto out_err;
  4047. rbd_dev->parent = parent;
  4048. atomic_set(&rbd_dev->parent_ref, 1);
  4049. return 0;
  4050. out_err:
  4051. if (parent) {
  4052. rbd_dev_unparent(rbd_dev);
  4053. kfree(rbd_dev->header_name);
  4054. rbd_dev_destroy(parent);
  4055. } else {
  4056. rbd_put_client(rbdc);
  4057. rbd_spec_put(parent_spec);
  4058. }
  4059. return ret;
  4060. }
  4061. static int rbd_dev_device_setup(struct rbd_device *rbd_dev)
  4062. {
  4063. int ret;
  4064. /* generate unique id: find highest unique id, add one */
  4065. rbd_dev_id_get(rbd_dev);
  4066. /* Fill in the device name, now that we have its id. */
  4067. BUILD_BUG_ON(DEV_NAME_LEN
  4068. < sizeof (RBD_DRV_NAME) + MAX_INT_FORMAT_WIDTH);
  4069. sprintf(rbd_dev->name, "%s%d", RBD_DRV_NAME, rbd_dev->dev_id);
  4070. /* Get our block major device number. */
  4071. ret = register_blkdev(0, rbd_dev->name);
  4072. if (ret < 0)
  4073. goto err_out_id;
  4074. rbd_dev->major = ret;
  4075. /* Set up the blkdev mapping. */
  4076. ret = rbd_init_disk(rbd_dev);
  4077. if (ret)
  4078. goto err_out_blkdev;
  4079. ret = rbd_dev_mapping_set(rbd_dev);
  4080. if (ret)
  4081. goto err_out_disk;
  4082. set_capacity(rbd_dev->disk, rbd_dev->mapping.size / SECTOR_SIZE);
  4083. ret = rbd_bus_add_dev(rbd_dev);
  4084. if (ret)
  4085. goto err_out_mapping;
  4086. /* Everything's ready. Announce the disk to the world. */
  4087. set_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags);
  4088. add_disk(rbd_dev->disk);
  4089. pr_info("%s: added with size 0x%llx\n", rbd_dev->disk->disk_name,
  4090. (unsigned long long) rbd_dev->mapping.size);
  4091. return ret;
  4092. err_out_mapping:
  4093. rbd_dev_mapping_clear(rbd_dev);
  4094. err_out_disk:
  4095. rbd_free_disk(rbd_dev);
  4096. err_out_blkdev:
  4097. unregister_blkdev(rbd_dev->major, rbd_dev->name);
  4098. err_out_id:
  4099. rbd_dev_id_put(rbd_dev);
  4100. rbd_dev_mapping_clear(rbd_dev);
  4101. return ret;
  4102. }
  4103. static int rbd_dev_header_name(struct rbd_device *rbd_dev)
  4104. {
  4105. struct rbd_spec *spec = rbd_dev->spec;
  4106. size_t size;
  4107. /* Record the header object name for this rbd image. */
  4108. rbd_assert(rbd_image_format_valid(rbd_dev->image_format));
  4109. if (rbd_dev->image_format == 1)
  4110. size = strlen(spec->image_name) + sizeof (RBD_SUFFIX);
  4111. else
  4112. size = sizeof (RBD_HEADER_PREFIX) + strlen(spec->image_id);
  4113. rbd_dev->header_name = kmalloc(size, GFP_KERNEL);
  4114. if (!rbd_dev->header_name)
  4115. return -ENOMEM;
  4116. if (rbd_dev->image_format == 1)
  4117. sprintf(rbd_dev->header_name, "%s%s",
  4118. spec->image_name, RBD_SUFFIX);
  4119. else
  4120. sprintf(rbd_dev->header_name, "%s%s",
  4121. RBD_HEADER_PREFIX, spec->image_id);
  4122. return 0;
  4123. }
  4124. static void rbd_dev_image_release(struct rbd_device *rbd_dev)
  4125. {
  4126. rbd_dev_unprobe(rbd_dev);
  4127. kfree(rbd_dev->header_name);
  4128. rbd_dev->header_name = NULL;
  4129. rbd_dev->image_format = 0;
  4130. kfree(rbd_dev->spec->image_id);
  4131. rbd_dev->spec->image_id = NULL;
  4132. rbd_dev_destroy(rbd_dev);
  4133. }
  4134. /*
  4135. * Probe for the existence of the header object for the given rbd
  4136. * device. If this image is the one being mapped (i.e., not a
  4137. * parent), initiate a watch on its header object before using that
  4138. * object to get detailed information about the rbd image.
  4139. */
  4140. static int rbd_dev_image_probe(struct rbd_device *rbd_dev, bool mapping)
  4141. {
  4142. int ret;
  4143. int tmp;
  4144. /*
  4145. * Get the id from the image id object. Unless there's an
  4146. * error, rbd_dev->spec->image_id will be filled in with
  4147. * a dynamically-allocated string, and rbd_dev->image_format
  4148. * will be set to either 1 or 2.
  4149. */
  4150. ret = rbd_dev_image_id(rbd_dev);
  4151. if (ret)
  4152. return ret;
  4153. rbd_assert(rbd_dev->spec->image_id);
  4154. rbd_assert(rbd_image_format_valid(rbd_dev->image_format));
  4155. ret = rbd_dev_header_name(rbd_dev);
  4156. if (ret)
  4157. goto err_out_format;
  4158. if (mapping) {
  4159. ret = rbd_dev_header_watch_sync(rbd_dev, true);
  4160. if (ret)
  4161. goto out_header_name;
  4162. }
  4163. if (rbd_dev->image_format == 1)
  4164. ret = rbd_dev_v1_header_info(rbd_dev);
  4165. else
  4166. ret = rbd_dev_v2_header_info(rbd_dev);
  4167. if (ret)
  4168. goto err_out_watch;
  4169. ret = rbd_dev_spec_update(rbd_dev);
  4170. if (ret)
  4171. goto err_out_probe;
  4172. ret = rbd_dev_probe_parent(rbd_dev);
  4173. if (ret)
  4174. goto err_out_probe;
  4175. dout("discovered format %u image, header name is %s\n",
  4176. rbd_dev->image_format, rbd_dev->header_name);
  4177. return 0;
  4178. err_out_probe:
  4179. rbd_dev_unprobe(rbd_dev);
  4180. err_out_watch:
  4181. if (mapping) {
  4182. tmp = rbd_dev_header_watch_sync(rbd_dev, false);
  4183. if (tmp)
  4184. rbd_warn(rbd_dev, "unable to tear down "
  4185. "watch request (%d)\n", tmp);
  4186. }
  4187. out_header_name:
  4188. kfree(rbd_dev->header_name);
  4189. rbd_dev->header_name = NULL;
  4190. err_out_format:
  4191. rbd_dev->image_format = 0;
  4192. kfree(rbd_dev->spec->image_id);
  4193. rbd_dev->spec->image_id = NULL;
  4194. dout("probe failed, returning %d\n", ret);
  4195. return ret;
  4196. }
  4197. static ssize_t rbd_add(struct bus_type *bus,
  4198. const char *buf,
  4199. size_t count)
  4200. {
  4201. struct rbd_device *rbd_dev = NULL;
  4202. struct ceph_options *ceph_opts = NULL;
  4203. struct rbd_options *rbd_opts = NULL;
  4204. struct rbd_spec *spec = NULL;
  4205. struct rbd_client *rbdc;
  4206. struct ceph_osd_client *osdc;
  4207. bool read_only;
  4208. int rc = -ENOMEM;
  4209. if (!try_module_get(THIS_MODULE))
  4210. return -ENODEV;
  4211. /* parse add command */
  4212. rc = rbd_add_parse_args(buf, &ceph_opts, &rbd_opts, &spec);
  4213. if (rc < 0)
  4214. goto err_out_module;
  4215. read_only = rbd_opts->read_only;
  4216. kfree(rbd_opts);
  4217. rbd_opts = NULL; /* done with this */
  4218. rbdc = rbd_get_client(ceph_opts);
  4219. if (IS_ERR(rbdc)) {
  4220. rc = PTR_ERR(rbdc);
  4221. goto err_out_args;
  4222. }
  4223. /* pick the pool */
  4224. osdc = &rbdc->client->osdc;
  4225. rc = ceph_pg_poolid_by_name(osdc->osdmap, spec->pool_name);
  4226. if (rc < 0)
  4227. goto err_out_client;
  4228. spec->pool_id = (u64)rc;
  4229. /* The ceph file layout needs to fit pool id in 32 bits */
  4230. if (spec->pool_id > (u64)U32_MAX) {
  4231. rbd_warn(NULL, "pool id too large (%llu > %u)\n",
  4232. (unsigned long long)spec->pool_id, U32_MAX);
  4233. rc = -EIO;
  4234. goto err_out_client;
  4235. }
  4236. rbd_dev = rbd_dev_create(rbdc, spec);
  4237. if (!rbd_dev)
  4238. goto err_out_client;
  4239. rbdc = NULL; /* rbd_dev now owns this */
  4240. spec = NULL; /* rbd_dev now owns this */
  4241. rc = rbd_dev_image_probe(rbd_dev, true);
  4242. if (rc < 0)
  4243. goto err_out_rbd_dev;
  4244. /* If we are mapping a snapshot it must be marked read-only */
  4245. if (rbd_dev->spec->snap_id != CEPH_NOSNAP)
  4246. read_only = true;
  4247. rbd_dev->mapping.read_only = read_only;
  4248. rc = rbd_dev_device_setup(rbd_dev);
  4249. if (rc) {
  4250. rbd_dev_image_release(rbd_dev);
  4251. goto err_out_module;
  4252. }
  4253. return count;
  4254. err_out_rbd_dev:
  4255. rbd_dev_destroy(rbd_dev);
  4256. err_out_client:
  4257. rbd_put_client(rbdc);
  4258. err_out_args:
  4259. rbd_spec_put(spec);
  4260. err_out_module:
  4261. module_put(THIS_MODULE);
  4262. dout("Error adding device %s\n", buf);
  4263. return (ssize_t)rc;
  4264. }
  4265. static void rbd_dev_device_release(struct device *dev)
  4266. {
  4267. struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
  4268. rbd_free_disk(rbd_dev);
  4269. clear_bit(RBD_DEV_FLAG_EXISTS, &rbd_dev->flags);
  4270. rbd_dev_mapping_clear(rbd_dev);
  4271. unregister_blkdev(rbd_dev->major, rbd_dev->name);
  4272. rbd_dev->major = 0;
  4273. rbd_dev_id_put(rbd_dev);
  4274. rbd_dev_mapping_clear(rbd_dev);
  4275. }
  4276. static void rbd_dev_remove_parent(struct rbd_device *rbd_dev)
  4277. {
  4278. while (rbd_dev->parent) {
  4279. struct rbd_device *first = rbd_dev;
  4280. struct rbd_device *second = first->parent;
  4281. struct rbd_device *third;
  4282. /*
  4283. * Follow to the parent with no grandparent and
  4284. * remove it.
  4285. */
  4286. while (second && (third = second->parent)) {
  4287. first = second;
  4288. second = third;
  4289. }
  4290. rbd_assert(second);
  4291. rbd_dev_image_release(second);
  4292. first->parent = NULL;
  4293. first->parent_overlap = 0;
  4294. rbd_assert(first->parent_spec);
  4295. rbd_spec_put(first->parent_spec);
  4296. first->parent_spec = NULL;
  4297. }
  4298. }
  4299. static ssize_t rbd_remove(struct bus_type *bus,
  4300. const char *buf,
  4301. size_t count)
  4302. {
  4303. struct rbd_device *rbd_dev = NULL;
  4304. struct list_head *tmp;
  4305. int dev_id;
  4306. unsigned long ul;
  4307. bool already = false;
  4308. int ret;
  4309. ret = kstrtoul(buf, 10, &ul);
  4310. if (ret)
  4311. return ret;
  4312. /* convert to int; abort if we lost anything in the conversion */
  4313. dev_id = (int)ul;
  4314. if (dev_id != ul)
  4315. return -EINVAL;
  4316. ret = -ENOENT;
  4317. spin_lock(&rbd_dev_list_lock);
  4318. list_for_each(tmp, &rbd_dev_list) {
  4319. rbd_dev = list_entry(tmp, struct rbd_device, node);
  4320. if (rbd_dev->dev_id == dev_id) {
  4321. ret = 0;
  4322. break;
  4323. }
  4324. }
  4325. if (!ret) {
  4326. spin_lock_irq(&rbd_dev->lock);
  4327. if (rbd_dev->open_count)
  4328. ret = -EBUSY;
  4329. else
  4330. already = test_and_set_bit(RBD_DEV_FLAG_REMOVING,
  4331. &rbd_dev->flags);
  4332. spin_unlock_irq(&rbd_dev->lock);
  4333. }
  4334. spin_unlock(&rbd_dev_list_lock);
  4335. if (ret < 0 || already)
  4336. return ret;
  4337. ret = rbd_dev_header_watch_sync(rbd_dev, false);
  4338. if (ret)
  4339. rbd_warn(rbd_dev, "failed to cancel watch event (%d)\n", ret);
  4340. /*
  4341. * flush remaining watch callbacks - these must be complete
  4342. * before the osd_client is shutdown
  4343. */
  4344. dout("%s: flushing notifies", __func__);
  4345. ceph_osdc_flush_notifies(&rbd_dev->rbd_client->client->osdc);
  4346. /*
  4347. * Don't free anything from rbd_dev->disk until after all
  4348. * notifies are completely processed. Otherwise
  4349. * rbd_bus_del_dev() will race with rbd_watch_cb(), resulting
  4350. * in a potential use after free of rbd_dev->disk or rbd_dev.
  4351. */
  4352. rbd_bus_del_dev(rbd_dev);
  4353. rbd_dev_image_release(rbd_dev);
  4354. module_put(THIS_MODULE);
  4355. return count;
  4356. }
  4357. /*
  4358. * create control files in sysfs
  4359. * /sys/bus/rbd/...
  4360. */
  4361. static int rbd_sysfs_init(void)
  4362. {
  4363. int ret;
  4364. ret = device_register(&rbd_root_dev);
  4365. if (ret < 0)
  4366. return ret;
  4367. ret = bus_register(&rbd_bus_type);
  4368. if (ret < 0)
  4369. device_unregister(&rbd_root_dev);
  4370. return ret;
  4371. }
  4372. static void rbd_sysfs_cleanup(void)
  4373. {
  4374. bus_unregister(&rbd_bus_type);
  4375. device_unregister(&rbd_root_dev);
  4376. }
  4377. static int rbd_slab_init(void)
  4378. {
  4379. rbd_assert(!rbd_img_request_cache);
  4380. rbd_img_request_cache = kmem_cache_create("rbd_img_request",
  4381. sizeof (struct rbd_img_request),
  4382. __alignof__(struct rbd_img_request),
  4383. 0, NULL);
  4384. if (!rbd_img_request_cache)
  4385. return -ENOMEM;
  4386. rbd_assert(!rbd_obj_request_cache);
  4387. rbd_obj_request_cache = kmem_cache_create("rbd_obj_request",
  4388. sizeof (struct rbd_obj_request),
  4389. __alignof__(struct rbd_obj_request),
  4390. 0, NULL);
  4391. if (!rbd_obj_request_cache)
  4392. goto out_err;
  4393. rbd_assert(!rbd_segment_name_cache);
  4394. rbd_segment_name_cache = kmem_cache_create("rbd_segment_name",
  4395. MAX_OBJ_NAME_SIZE + 1, 1, 0, NULL);
  4396. if (rbd_segment_name_cache)
  4397. return 0;
  4398. out_err:
  4399. if (rbd_obj_request_cache) {
  4400. kmem_cache_destroy(rbd_obj_request_cache);
  4401. rbd_obj_request_cache = NULL;
  4402. }
  4403. kmem_cache_destroy(rbd_img_request_cache);
  4404. rbd_img_request_cache = NULL;
  4405. return -ENOMEM;
  4406. }
  4407. static void rbd_slab_exit(void)
  4408. {
  4409. rbd_assert(rbd_segment_name_cache);
  4410. kmem_cache_destroy(rbd_segment_name_cache);
  4411. rbd_segment_name_cache = NULL;
  4412. rbd_assert(rbd_obj_request_cache);
  4413. kmem_cache_destroy(rbd_obj_request_cache);
  4414. rbd_obj_request_cache = NULL;
  4415. rbd_assert(rbd_img_request_cache);
  4416. kmem_cache_destroy(rbd_img_request_cache);
  4417. rbd_img_request_cache = NULL;
  4418. }
  4419. static int __init rbd_init(void)
  4420. {
  4421. int rc;
  4422. if (!libceph_compatible(NULL)) {
  4423. rbd_warn(NULL, "libceph incompatibility (quitting)");
  4424. return -EINVAL;
  4425. }
  4426. rc = rbd_slab_init();
  4427. if (rc)
  4428. return rc;
  4429. rc = rbd_sysfs_init();
  4430. if (rc)
  4431. rbd_slab_exit();
  4432. else
  4433. pr_info("loaded " RBD_DRV_NAME_LONG "\n");
  4434. return rc;
  4435. }
  4436. static void __exit rbd_exit(void)
  4437. {
  4438. rbd_sysfs_cleanup();
  4439. rbd_slab_exit();
  4440. }
  4441. module_init(rbd_init);
  4442. module_exit(rbd_exit);
  4443. MODULE_AUTHOR("Alex Elder <elder@inktank.com>");
  4444. MODULE_AUTHOR("Sage Weil <sage@newdream.net>");
  4445. MODULE_AUTHOR("Yehuda Sadeh <yehuda@hq.newdream.net>");
  4446. MODULE_DESCRIPTION("rados block device");
  4447. /* following authorship retained from original osdblk.c */
  4448. MODULE_AUTHOR("Jeff Garzik <jeff@garzik.org>");
  4449. MODULE_LICENSE("GPL");