rbd.c 130 KB

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