rbd.c 129 KB

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