rbd.c 103 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229
  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. /* It might be useful to have these defined elsewhere */
  39. #define U8_MAX ((u8) (~0U))
  40. #define U16_MAX ((u16) (~0U))
  41. #define U32_MAX ((u32) (~0U))
  42. #define U64_MAX ((u64) (~0ULL))
  43. #define RBD_DRV_NAME "rbd"
  44. #define RBD_DRV_NAME_LONG "rbd (rados block device)"
  45. #define RBD_MINORS_PER_MAJOR 256 /* max minors per blkdev */
  46. #define RBD_SNAP_DEV_NAME_PREFIX "snap_"
  47. #define RBD_MAX_SNAP_NAME_LEN \
  48. (NAME_MAX - (sizeof (RBD_SNAP_DEV_NAME_PREFIX) - 1))
  49. #define RBD_MAX_SNAP_COUNT 510 /* allows max snapc to fit in 4KB */
  50. #define RBD_SNAP_HEAD_NAME "-"
  51. /* This allows a single page to hold an image name sent by OSD */
  52. #define RBD_IMAGE_NAME_LEN_MAX (PAGE_SIZE - sizeof (__le32) - 1)
  53. #define RBD_IMAGE_ID_LEN_MAX 64
  54. #define RBD_OBJ_PREFIX_LEN_MAX 64
  55. /* Feature bits */
  56. #define RBD_FEATURE_LAYERING 1
  57. /* Features supported by this (client software) implementation. */
  58. #define RBD_FEATURES_ALL (0)
  59. /*
  60. * An RBD device name will be "rbd#", where the "rbd" comes from
  61. * RBD_DRV_NAME above, and # is a unique integer identifier.
  62. * MAX_INT_FORMAT_WIDTH is used in ensuring DEV_NAME_LEN is big
  63. * enough to hold all possible device names.
  64. */
  65. #define DEV_NAME_LEN 32
  66. #define MAX_INT_FORMAT_WIDTH ((5 * sizeof (int)) / 2 + 1)
  67. /*
  68. * block device image metadata (in-memory version)
  69. */
  70. struct rbd_image_header {
  71. /* These four fields never change for a given rbd image */
  72. char *object_prefix;
  73. u64 features;
  74. __u8 obj_order;
  75. __u8 crypt_type;
  76. __u8 comp_type;
  77. /* The remaining fields need to be updated occasionally */
  78. u64 image_size;
  79. struct ceph_snap_context *snapc;
  80. char *snap_names;
  81. u64 *snap_sizes;
  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. char *pool_name;
  112. char *image_id;
  113. char *image_name;
  114. u64 snap_id;
  115. 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. struct rbd_obj_request {
  135. const char *object_name;
  136. u64 offset; /* object start byte */
  137. u64 length; /* bytes from offset */
  138. struct rbd_img_request *img_request;
  139. struct list_head links; /* img_request->obj_requests */
  140. u32 which; /* posn image request list */
  141. enum obj_request_type type;
  142. union {
  143. struct bio *bio_list;
  144. struct {
  145. struct page **pages;
  146. u32 page_count;
  147. };
  148. };
  149. struct ceph_osd_request *osd_req;
  150. u64 xferred; /* bytes transferred */
  151. u64 version;
  152. s32 result;
  153. atomic_t done;
  154. rbd_obj_callback_t callback;
  155. struct completion completion;
  156. struct kref kref;
  157. };
  158. struct rbd_img_request {
  159. struct request *rq;
  160. struct rbd_device *rbd_dev;
  161. u64 offset; /* starting image byte offset */
  162. u64 length; /* byte count from offset */
  163. bool write_request; /* false for read */
  164. union {
  165. struct ceph_snap_context *snapc; /* for writes */
  166. u64 snap_id; /* for reads */
  167. };
  168. spinlock_t completion_lock;/* protects next_completion */
  169. u32 next_completion;
  170. rbd_img_callback_t callback;
  171. u32 obj_request_count;
  172. struct list_head obj_requests; /* rbd_obj_request structs */
  173. struct kref kref;
  174. };
  175. #define for_each_obj_request(ireq, oreq) \
  176. list_for_each_entry(oreq, &ireq->obj_requests, links)
  177. #define for_each_obj_request_from(ireq, oreq) \
  178. list_for_each_entry_from(oreq, &ireq->obj_requests, links)
  179. #define for_each_obj_request_safe(ireq, oreq, n) \
  180. list_for_each_entry_safe_reverse(oreq, n, &ireq->obj_requests, links)
  181. struct rbd_snap {
  182. struct device dev;
  183. const char *name;
  184. u64 size;
  185. struct list_head node;
  186. u64 id;
  187. u64 features;
  188. };
  189. struct rbd_mapping {
  190. u64 size;
  191. u64 features;
  192. bool read_only;
  193. };
  194. /*
  195. * a single device
  196. */
  197. struct rbd_device {
  198. int dev_id; /* blkdev unique id */
  199. int major; /* blkdev assigned major */
  200. struct gendisk *disk; /* blkdev's gendisk and rq */
  201. u32 image_format; /* Either 1 or 2 */
  202. struct rbd_client *rbd_client;
  203. char name[DEV_NAME_LEN]; /* blkdev name, e.g. rbd3 */
  204. spinlock_t lock; /* queue lock */
  205. struct rbd_image_header header;
  206. atomic_t exists;
  207. struct rbd_spec *spec;
  208. char *header_name;
  209. struct ceph_file_layout layout;
  210. struct ceph_osd_event *watch_event;
  211. struct ceph_osd_request *watch_request;
  212. struct rbd_spec *parent_spec;
  213. u64 parent_overlap;
  214. /* protects updating the header */
  215. struct rw_semaphore header_rwsem;
  216. struct rbd_mapping mapping;
  217. struct list_head node;
  218. /* list of snapshots */
  219. struct list_head snaps;
  220. /* sysfs related */
  221. struct device dev;
  222. unsigned long open_count;
  223. };
  224. static DEFINE_MUTEX(ctl_mutex); /* Serialize open/close/setup/teardown */
  225. static LIST_HEAD(rbd_dev_list); /* devices */
  226. static DEFINE_SPINLOCK(rbd_dev_list_lock);
  227. static LIST_HEAD(rbd_client_list); /* clients */
  228. static DEFINE_SPINLOCK(rbd_client_list_lock);
  229. static int rbd_dev_snaps_update(struct rbd_device *rbd_dev);
  230. static int rbd_dev_snaps_register(struct rbd_device *rbd_dev);
  231. static void rbd_dev_release(struct device *dev);
  232. static void rbd_remove_snap_dev(struct rbd_snap *snap);
  233. static ssize_t rbd_add(struct bus_type *bus, const char *buf,
  234. size_t count);
  235. static ssize_t rbd_remove(struct bus_type *bus, const char *buf,
  236. size_t count);
  237. static struct bus_attribute rbd_bus_attrs[] = {
  238. __ATTR(add, S_IWUSR, NULL, rbd_add),
  239. __ATTR(remove, S_IWUSR, NULL, rbd_remove),
  240. __ATTR_NULL
  241. };
  242. static struct bus_type rbd_bus_type = {
  243. .name = "rbd",
  244. .bus_attrs = rbd_bus_attrs,
  245. };
  246. static void rbd_root_dev_release(struct device *dev)
  247. {
  248. }
  249. static struct device rbd_root_dev = {
  250. .init_name = "rbd",
  251. .release = rbd_root_dev_release,
  252. };
  253. static __printf(2, 3)
  254. void rbd_warn(struct rbd_device *rbd_dev, const char *fmt, ...)
  255. {
  256. struct va_format vaf;
  257. va_list args;
  258. va_start(args, fmt);
  259. vaf.fmt = fmt;
  260. vaf.va = &args;
  261. if (!rbd_dev)
  262. printk(KERN_WARNING "%s: %pV\n", RBD_DRV_NAME, &vaf);
  263. else if (rbd_dev->disk)
  264. printk(KERN_WARNING "%s: %s: %pV\n",
  265. RBD_DRV_NAME, rbd_dev->disk->disk_name, &vaf);
  266. else if (rbd_dev->spec && rbd_dev->spec->image_name)
  267. printk(KERN_WARNING "%s: image %s: %pV\n",
  268. RBD_DRV_NAME, rbd_dev->spec->image_name, &vaf);
  269. else if (rbd_dev->spec && rbd_dev->spec->image_id)
  270. printk(KERN_WARNING "%s: id %s: %pV\n",
  271. RBD_DRV_NAME, rbd_dev->spec->image_id, &vaf);
  272. else /* punt */
  273. printk(KERN_WARNING "%s: rbd_dev %p: %pV\n",
  274. RBD_DRV_NAME, rbd_dev, &vaf);
  275. va_end(args);
  276. }
  277. #ifdef RBD_DEBUG
  278. #define rbd_assert(expr) \
  279. if (unlikely(!(expr))) { \
  280. printk(KERN_ERR "\nAssertion failure in %s() " \
  281. "at line %d:\n\n" \
  282. "\trbd_assert(%s);\n\n", \
  283. __func__, __LINE__, #expr); \
  284. BUG(); \
  285. }
  286. #else /* !RBD_DEBUG */
  287. # define rbd_assert(expr) ((void) 0)
  288. #endif /* !RBD_DEBUG */
  289. static int rbd_dev_refresh(struct rbd_device *rbd_dev, u64 *hver);
  290. static int rbd_dev_v2_refresh(struct rbd_device *rbd_dev, u64 *hver);
  291. static int rbd_open(struct block_device *bdev, fmode_t mode)
  292. {
  293. struct rbd_device *rbd_dev = bdev->bd_disk->private_data;
  294. if ((mode & FMODE_WRITE) && rbd_dev->mapping.read_only)
  295. return -EROFS;
  296. mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
  297. (void) get_device(&rbd_dev->dev);
  298. set_device_ro(bdev, rbd_dev->mapping.read_only);
  299. rbd_dev->open_count++;
  300. mutex_unlock(&ctl_mutex);
  301. return 0;
  302. }
  303. static int rbd_release(struct gendisk *disk, fmode_t mode)
  304. {
  305. struct rbd_device *rbd_dev = disk->private_data;
  306. mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
  307. rbd_assert(rbd_dev->open_count > 0);
  308. rbd_dev->open_count--;
  309. put_device(&rbd_dev->dev);
  310. mutex_unlock(&ctl_mutex);
  311. return 0;
  312. }
  313. static const struct block_device_operations rbd_bd_ops = {
  314. .owner = THIS_MODULE,
  315. .open = rbd_open,
  316. .release = rbd_release,
  317. };
  318. /*
  319. * Initialize an rbd client instance.
  320. * We own *ceph_opts.
  321. */
  322. static struct rbd_client *rbd_client_create(struct ceph_options *ceph_opts)
  323. {
  324. struct rbd_client *rbdc;
  325. int ret = -ENOMEM;
  326. dout("rbd_client_create\n");
  327. rbdc = kmalloc(sizeof(struct rbd_client), GFP_KERNEL);
  328. if (!rbdc)
  329. goto out_opt;
  330. kref_init(&rbdc->kref);
  331. INIT_LIST_HEAD(&rbdc->node);
  332. mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
  333. rbdc->client = ceph_create_client(ceph_opts, rbdc, 0, 0);
  334. if (IS_ERR(rbdc->client))
  335. goto out_mutex;
  336. ceph_opts = NULL; /* Now rbdc->client is responsible for ceph_opts */
  337. ret = ceph_open_session(rbdc->client);
  338. if (ret < 0)
  339. goto out_err;
  340. spin_lock(&rbd_client_list_lock);
  341. list_add_tail(&rbdc->node, &rbd_client_list);
  342. spin_unlock(&rbd_client_list_lock);
  343. mutex_unlock(&ctl_mutex);
  344. dout("rbd_client_create created %p\n", rbdc);
  345. return rbdc;
  346. out_err:
  347. ceph_destroy_client(rbdc->client);
  348. out_mutex:
  349. mutex_unlock(&ctl_mutex);
  350. kfree(rbdc);
  351. out_opt:
  352. if (ceph_opts)
  353. ceph_destroy_options(ceph_opts);
  354. return ERR_PTR(ret);
  355. }
  356. /*
  357. * Find a ceph client with specific addr and configuration. If
  358. * found, bump its reference count.
  359. */
  360. static struct rbd_client *rbd_client_find(struct ceph_options *ceph_opts)
  361. {
  362. struct rbd_client *client_node;
  363. bool found = false;
  364. if (ceph_opts->flags & CEPH_OPT_NOSHARE)
  365. return NULL;
  366. spin_lock(&rbd_client_list_lock);
  367. list_for_each_entry(client_node, &rbd_client_list, node) {
  368. if (!ceph_compare_options(ceph_opts, client_node->client)) {
  369. kref_get(&client_node->kref);
  370. found = true;
  371. break;
  372. }
  373. }
  374. spin_unlock(&rbd_client_list_lock);
  375. return found ? client_node : NULL;
  376. }
  377. /*
  378. * mount options
  379. */
  380. enum {
  381. Opt_last_int,
  382. /* int args above */
  383. Opt_last_string,
  384. /* string args above */
  385. Opt_read_only,
  386. Opt_read_write,
  387. /* Boolean args above */
  388. Opt_last_bool,
  389. };
  390. static match_table_t rbd_opts_tokens = {
  391. /* int args above */
  392. /* string args above */
  393. {Opt_read_only, "read_only"},
  394. {Opt_read_only, "ro"}, /* Alternate spelling */
  395. {Opt_read_write, "read_write"},
  396. {Opt_read_write, "rw"}, /* Alternate spelling */
  397. /* Boolean args above */
  398. {-1, NULL}
  399. };
  400. struct rbd_options {
  401. bool read_only;
  402. };
  403. #define RBD_READ_ONLY_DEFAULT false
  404. static int parse_rbd_opts_token(char *c, void *private)
  405. {
  406. struct rbd_options *rbd_opts = private;
  407. substring_t argstr[MAX_OPT_ARGS];
  408. int token, intval, ret;
  409. token = match_token(c, rbd_opts_tokens, argstr);
  410. if (token < 0)
  411. return -EINVAL;
  412. if (token < Opt_last_int) {
  413. ret = match_int(&argstr[0], &intval);
  414. if (ret < 0) {
  415. pr_err("bad mount option arg (not int) "
  416. "at '%s'\n", c);
  417. return ret;
  418. }
  419. dout("got int token %d val %d\n", token, intval);
  420. } else if (token > Opt_last_int && token < Opt_last_string) {
  421. dout("got string token %d val %s\n", token,
  422. argstr[0].from);
  423. } else if (token > Opt_last_string && token < Opt_last_bool) {
  424. dout("got Boolean token %d\n", token);
  425. } else {
  426. dout("got token %d\n", token);
  427. }
  428. switch (token) {
  429. case Opt_read_only:
  430. rbd_opts->read_only = true;
  431. break;
  432. case Opt_read_write:
  433. rbd_opts->read_only = false;
  434. break;
  435. default:
  436. rbd_assert(false);
  437. break;
  438. }
  439. return 0;
  440. }
  441. /*
  442. * Get a ceph client with specific addr and configuration, if one does
  443. * not exist create it.
  444. */
  445. static struct rbd_client *rbd_get_client(struct ceph_options *ceph_opts)
  446. {
  447. struct rbd_client *rbdc;
  448. rbdc = rbd_client_find(ceph_opts);
  449. if (rbdc) /* using an existing client */
  450. ceph_destroy_options(ceph_opts);
  451. else
  452. rbdc = rbd_client_create(ceph_opts);
  453. return rbdc;
  454. }
  455. /*
  456. * Destroy ceph client
  457. *
  458. * Caller must hold rbd_client_list_lock.
  459. */
  460. static void rbd_client_release(struct kref *kref)
  461. {
  462. struct rbd_client *rbdc = container_of(kref, struct rbd_client, kref);
  463. dout("rbd_release_client %p\n", rbdc);
  464. spin_lock(&rbd_client_list_lock);
  465. list_del(&rbdc->node);
  466. spin_unlock(&rbd_client_list_lock);
  467. ceph_destroy_client(rbdc->client);
  468. kfree(rbdc);
  469. }
  470. /*
  471. * Drop reference to ceph client node. If it's not referenced anymore, release
  472. * it.
  473. */
  474. static void rbd_put_client(struct rbd_client *rbdc)
  475. {
  476. if (rbdc)
  477. kref_put(&rbdc->kref, rbd_client_release);
  478. }
  479. static bool rbd_image_format_valid(u32 image_format)
  480. {
  481. return image_format == 1 || image_format == 2;
  482. }
  483. static bool rbd_dev_ondisk_valid(struct rbd_image_header_ondisk *ondisk)
  484. {
  485. size_t size;
  486. u32 snap_count;
  487. /* The header has to start with the magic rbd header text */
  488. if (memcmp(&ondisk->text, RBD_HEADER_TEXT, sizeof (RBD_HEADER_TEXT)))
  489. return false;
  490. /* The bio layer requires at least sector-sized I/O */
  491. if (ondisk->options.order < SECTOR_SHIFT)
  492. return false;
  493. /* If we use u64 in a few spots we may be able to loosen this */
  494. if (ondisk->options.order > 8 * sizeof (int) - 1)
  495. return false;
  496. /*
  497. * The size of a snapshot header has to fit in a size_t, and
  498. * that limits the number of snapshots.
  499. */
  500. snap_count = le32_to_cpu(ondisk->snap_count);
  501. size = SIZE_MAX - sizeof (struct ceph_snap_context);
  502. if (snap_count > size / sizeof (__le64))
  503. return false;
  504. /*
  505. * Not only that, but the size of the entire the snapshot
  506. * header must also be representable in a size_t.
  507. */
  508. size -= snap_count * sizeof (__le64);
  509. if ((u64) size < le64_to_cpu(ondisk->snap_names_len))
  510. return false;
  511. return true;
  512. }
  513. /*
  514. * Create a new header structure, translate header format from the on-disk
  515. * header.
  516. */
  517. static int rbd_header_from_disk(struct rbd_image_header *header,
  518. struct rbd_image_header_ondisk *ondisk)
  519. {
  520. u32 snap_count;
  521. size_t len;
  522. size_t size;
  523. u32 i;
  524. memset(header, 0, sizeof (*header));
  525. snap_count = le32_to_cpu(ondisk->snap_count);
  526. len = strnlen(ondisk->object_prefix, sizeof (ondisk->object_prefix));
  527. header->object_prefix = kmalloc(len + 1, GFP_KERNEL);
  528. if (!header->object_prefix)
  529. return -ENOMEM;
  530. memcpy(header->object_prefix, ondisk->object_prefix, len);
  531. header->object_prefix[len] = '\0';
  532. if (snap_count) {
  533. u64 snap_names_len = le64_to_cpu(ondisk->snap_names_len);
  534. /* Save a copy of the snapshot names */
  535. if (snap_names_len > (u64) SIZE_MAX)
  536. return -EIO;
  537. header->snap_names = kmalloc(snap_names_len, GFP_KERNEL);
  538. if (!header->snap_names)
  539. goto out_err;
  540. /*
  541. * Note that rbd_dev_v1_header_read() guarantees
  542. * the ondisk buffer we're working with has
  543. * snap_names_len bytes beyond the end of the
  544. * snapshot id array, this memcpy() is safe.
  545. */
  546. memcpy(header->snap_names, &ondisk->snaps[snap_count],
  547. snap_names_len);
  548. /* Record each snapshot's size */
  549. size = snap_count * sizeof (*header->snap_sizes);
  550. header->snap_sizes = kmalloc(size, GFP_KERNEL);
  551. if (!header->snap_sizes)
  552. goto out_err;
  553. for (i = 0; i < snap_count; i++)
  554. header->snap_sizes[i] =
  555. le64_to_cpu(ondisk->snaps[i].image_size);
  556. } else {
  557. WARN_ON(ondisk->snap_names_len);
  558. header->snap_names = NULL;
  559. header->snap_sizes = NULL;
  560. }
  561. header->features = 0; /* No features support in v1 images */
  562. header->obj_order = ondisk->options.order;
  563. header->crypt_type = ondisk->options.crypt_type;
  564. header->comp_type = ondisk->options.comp_type;
  565. /* Allocate and fill in the snapshot context */
  566. header->image_size = le64_to_cpu(ondisk->image_size);
  567. size = sizeof (struct ceph_snap_context);
  568. size += snap_count * sizeof (header->snapc->snaps[0]);
  569. header->snapc = kzalloc(size, GFP_KERNEL);
  570. if (!header->snapc)
  571. goto out_err;
  572. atomic_set(&header->snapc->nref, 1);
  573. header->snapc->seq = le64_to_cpu(ondisk->snap_seq);
  574. header->snapc->num_snaps = snap_count;
  575. for (i = 0; i < snap_count; i++)
  576. header->snapc->snaps[i] =
  577. le64_to_cpu(ondisk->snaps[i].id);
  578. return 0;
  579. out_err:
  580. kfree(header->snap_sizes);
  581. header->snap_sizes = NULL;
  582. kfree(header->snap_names);
  583. header->snap_names = NULL;
  584. kfree(header->object_prefix);
  585. header->object_prefix = NULL;
  586. return -ENOMEM;
  587. }
  588. static const char *rbd_snap_name(struct rbd_device *rbd_dev, u64 snap_id)
  589. {
  590. struct rbd_snap *snap;
  591. if (snap_id == CEPH_NOSNAP)
  592. return RBD_SNAP_HEAD_NAME;
  593. list_for_each_entry(snap, &rbd_dev->snaps, node)
  594. if (snap_id == snap->id)
  595. return snap->name;
  596. return NULL;
  597. }
  598. static int snap_by_name(struct rbd_device *rbd_dev, const char *snap_name)
  599. {
  600. struct rbd_snap *snap;
  601. list_for_each_entry(snap, &rbd_dev->snaps, node) {
  602. if (!strcmp(snap_name, snap->name)) {
  603. rbd_dev->spec->snap_id = snap->id;
  604. rbd_dev->mapping.size = snap->size;
  605. rbd_dev->mapping.features = snap->features;
  606. return 0;
  607. }
  608. }
  609. return -ENOENT;
  610. }
  611. static int rbd_dev_set_mapping(struct rbd_device *rbd_dev)
  612. {
  613. int ret;
  614. if (!memcmp(rbd_dev->spec->snap_name, RBD_SNAP_HEAD_NAME,
  615. sizeof (RBD_SNAP_HEAD_NAME))) {
  616. rbd_dev->spec->snap_id = CEPH_NOSNAP;
  617. rbd_dev->mapping.size = rbd_dev->header.image_size;
  618. rbd_dev->mapping.features = rbd_dev->header.features;
  619. ret = 0;
  620. } else {
  621. ret = snap_by_name(rbd_dev, rbd_dev->spec->snap_name);
  622. if (ret < 0)
  623. goto done;
  624. rbd_dev->mapping.read_only = true;
  625. }
  626. atomic_set(&rbd_dev->exists, 1);
  627. done:
  628. return ret;
  629. }
  630. static void rbd_header_free(struct rbd_image_header *header)
  631. {
  632. kfree(header->object_prefix);
  633. header->object_prefix = NULL;
  634. kfree(header->snap_sizes);
  635. header->snap_sizes = NULL;
  636. kfree(header->snap_names);
  637. header->snap_names = NULL;
  638. ceph_put_snap_context(header->snapc);
  639. header->snapc = NULL;
  640. }
  641. static const char *rbd_segment_name(struct rbd_device *rbd_dev, u64 offset)
  642. {
  643. char *name;
  644. u64 segment;
  645. int ret;
  646. name = kmalloc(MAX_OBJ_NAME_SIZE + 1, GFP_NOIO);
  647. if (!name)
  648. return NULL;
  649. segment = offset >> rbd_dev->header.obj_order;
  650. ret = snprintf(name, MAX_OBJ_NAME_SIZE + 1, "%s.%012llx",
  651. rbd_dev->header.object_prefix, segment);
  652. if (ret < 0 || ret > MAX_OBJ_NAME_SIZE) {
  653. pr_err("error formatting segment name for #%llu (%d)\n",
  654. segment, ret);
  655. kfree(name);
  656. name = NULL;
  657. }
  658. return name;
  659. }
  660. static u64 rbd_segment_offset(struct rbd_device *rbd_dev, u64 offset)
  661. {
  662. u64 segment_size = (u64) 1 << rbd_dev->header.obj_order;
  663. return offset & (segment_size - 1);
  664. }
  665. static u64 rbd_segment_length(struct rbd_device *rbd_dev,
  666. u64 offset, u64 length)
  667. {
  668. u64 segment_size = (u64) 1 << rbd_dev->header.obj_order;
  669. offset &= segment_size - 1;
  670. rbd_assert(length <= U64_MAX - offset);
  671. if (offset + length > segment_size)
  672. length = segment_size - offset;
  673. return length;
  674. }
  675. /*
  676. * returns the size of an object in the image
  677. */
  678. static u64 rbd_obj_bytes(struct rbd_image_header *header)
  679. {
  680. return 1 << header->obj_order;
  681. }
  682. /*
  683. * bio helpers
  684. */
  685. static void bio_chain_put(struct bio *chain)
  686. {
  687. struct bio *tmp;
  688. while (chain) {
  689. tmp = chain;
  690. chain = chain->bi_next;
  691. bio_put(tmp);
  692. }
  693. }
  694. /*
  695. * zeros a bio chain, starting at specific offset
  696. */
  697. static void zero_bio_chain(struct bio *chain, int start_ofs)
  698. {
  699. struct bio_vec *bv;
  700. unsigned long flags;
  701. void *buf;
  702. int i;
  703. int pos = 0;
  704. while (chain) {
  705. bio_for_each_segment(bv, chain, i) {
  706. if (pos + bv->bv_len > start_ofs) {
  707. int remainder = max(start_ofs - pos, 0);
  708. buf = bvec_kmap_irq(bv, &flags);
  709. memset(buf + remainder, 0,
  710. bv->bv_len - remainder);
  711. bvec_kunmap_irq(buf, &flags);
  712. }
  713. pos += bv->bv_len;
  714. }
  715. chain = chain->bi_next;
  716. }
  717. }
  718. /*
  719. * Clone a portion of a bio, starting at the given byte offset
  720. * and continuing for the number of bytes indicated.
  721. */
  722. static struct bio *bio_clone_range(struct bio *bio_src,
  723. unsigned int offset,
  724. unsigned int len,
  725. gfp_t gfpmask)
  726. {
  727. struct bio_vec *bv;
  728. unsigned int resid;
  729. unsigned short idx;
  730. unsigned int voff;
  731. unsigned short end_idx;
  732. unsigned short vcnt;
  733. struct bio *bio;
  734. /* Handle the easy case for the caller */
  735. if (!offset && len == bio_src->bi_size)
  736. return bio_clone(bio_src, gfpmask);
  737. if (WARN_ON_ONCE(!len))
  738. return NULL;
  739. if (WARN_ON_ONCE(len > bio_src->bi_size))
  740. return NULL;
  741. if (WARN_ON_ONCE(offset > bio_src->bi_size - len))
  742. return NULL;
  743. /* Find first affected segment... */
  744. resid = offset;
  745. __bio_for_each_segment(bv, bio_src, idx, 0) {
  746. if (resid < bv->bv_len)
  747. break;
  748. resid -= bv->bv_len;
  749. }
  750. voff = resid;
  751. /* ...and the last affected segment */
  752. resid += len;
  753. __bio_for_each_segment(bv, bio_src, end_idx, idx) {
  754. if (resid <= bv->bv_len)
  755. break;
  756. resid -= bv->bv_len;
  757. }
  758. vcnt = end_idx - idx + 1;
  759. /* Build the clone */
  760. bio = bio_alloc(gfpmask, (unsigned int) vcnt);
  761. if (!bio)
  762. return NULL; /* ENOMEM */
  763. bio->bi_bdev = bio_src->bi_bdev;
  764. bio->bi_sector = bio_src->bi_sector + (offset >> SECTOR_SHIFT);
  765. bio->bi_rw = bio_src->bi_rw;
  766. bio->bi_flags |= 1 << BIO_CLONED;
  767. /*
  768. * Copy over our part of the bio_vec, then update the first
  769. * and last (or only) entries.
  770. */
  771. memcpy(&bio->bi_io_vec[0], &bio_src->bi_io_vec[idx],
  772. vcnt * sizeof (struct bio_vec));
  773. bio->bi_io_vec[0].bv_offset += voff;
  774. if (vcnt > 1) {
  775. bio->bi_io_vec[0].bv_len -= voff;
  776. bio->bi_io_vec[vcnt - 1].bv_len = resid;
  777. } else {
  778. bio->bi_io_vec[0].bv_len = len;
  779. }
  780. bio->bi_vcnt = vcnt;
  781. bio->bi_size = len;
  782. bio->bi_idx = 0;
  783. return bio;
  784. }
  785. /*
  786. * Clone a portion of a bio chain, starting at the given byte offset
  787. * into the first bio in the source chain and continuing for the
  788. * number of bytes indicated. The result is another bio chain of
  789. * exactly the given length, or a null pointer on error.
  790. *
  791. * The bio_src and offset parameters are both in-out. On entry they
  792. * refer to the first source bio and the offset into that bio where
  793. * the start of data to be cloned is located.
  794. *
  795. * On return, bio_src is updated to refer to the bio in the source
  796. * chain that contains first un-cloned byte, and *offset will
  797. * contain the offset of that byte within that bio.
  798. */
  799. static struct bio *bio_chain_clone_range(struct bio **bio_src,
  800. unsigned int *offset,
  801. unsigned int len,
  802. gfp_t gfpmask)
  803. {
  804. struct bio *bi = *bio_src;
  805. unsigned int off = *offset;
  806. struct bio *chain = NULL;
  807. struct bio **end;
  808. /* Build up a chain of clone bios up to the limit */
  809. if (!bi || off >= bi->bi_size || !len)
  810. return NULL; /* Nothing to clone */
  811. end = &chain;
  812. while (len) {
  813. unsigned int bi_size;
  814. struct bio *bio;
  815. if (!bi) {
  816. rbd_warn(NULL, "bio_chain exhausted with %u left", len);
  817. goto out_err; /* EINVAL; ran out of bio's */
  818. }
  819. bi_size = min_t(unsigned int, bi->bi_size - off, len);
  820. bio = bio_clone_range(bi, off, bi_size, gfpmask);
  821. if (!bio)
  822. goto out_err; /* ENOMEM */
  823. *end = bio;
  824. end = &bio->bi_next;
  825. off += bi_size;
  826. if (off == bi->bi_size) {
  827. bi = bi->bi_next;
  828. off = 0;
  829. }
  830. len -= bi_size;
  831. }
  832. *bio_src = bi;
  833. *offset = off;
  834. return chain;
  835. out_err:
  836. bio_chain_put(chain);
  837. return NULL;
  838. }
  839. static void rbd_obj_request_get(struct rbd_obj_request *obj_request)
  840. {
  841. kref_get(&obj_request->kref);
  842. }
  843. static void rbd_obj_request_destroy(struct kref *kref);
  844. static void rbd_obj_request_put(struct rbd_obj_request *obj_request)
  845. {
  846. rbd_assert(obj_request != NULL);
  847. kref_put(&obj_request->kref, rbd_obj_request_destroy);
  848. }
  849. static void rbd_img_request_get(struct rbd_img_request *img_request)
  850. {
  851. kref_get(&img_request->kref);
  852. }
  853. static void rbd_img_request_destroy(struct kref *kref);
  854. static void rbd_img_request_put(struct rbd_img_request *img_request)
  855. {
  856. rbd_assert(img_request != NULL);
  857. kref_put(&img_request->kref, rbd_img_request_destroy);
  858. }
  859. static inline void rbd_img_obj_request_add(struct rbd_img_request *img_request,
  860. struct rbd_obj_request *obj_request)
  861. {
  862. rbd_obj_request_get(obj_request);
  863. obj_request->img_request = img_request;
  864. list_add_tail(&obj_request->links, &img_request->obj_requests);
  865. obj_request->which = img_request->obj_request_count++;
  866. rbd_assert(obj_request->which != BAD_WHICH);
  867. }
  868. static inline void rbd_img_obj_request_del(struct rbd_img_request *img_request,
  869. struct rbd_obj_request *obj_request)
  870. {
  871. rbd_assert(obj_request->which != BAD_WHICH);
  872. obj_request->which = BAD_WHICH;
  873. list_del(&obj_request->links);
  874. rbd_assert(obj_request->img_request == img_request);
  875. obj_request->callback = NULL;
  876. obj_request->img_request = NULL;
  877. rbd_obj_request_put(obj_request);
  878. }
  879. static bool obj_request_type_valid(enum obj_request_type type)
  880. {
  881. switch (type) {
  882. case OBJ_REQUEST_NODATA:
  883. case OBJ_REQUEST_BIO:
  884. case OBJ_REQUEST_PAGES:
  885. return true;
  886. default:
  887. return false;
  888. }
  889. }
  890. struct ceph_osd_req_op *rbd_osd_req_op_create(u16 opcode, ...)
  891. {
  892. struct ceph_osd_req_op *op;
  893. va_list args;
  894. size_t size;
  895. op = kzalloc(sizeof (*op), GFP_NOIO);
  896. if (!op)
  897. return NULL;
  898. op->op = opcode;
  899. va_start(args, opcode);
  900. switch (opcode) {
  901. case CEPH_OSD_OP_READ:
  902. case CEPH_OSD_OP_WRITE:
  903. /* rbd_osd_req_op_create(READ, offset, length) */
  904. /* rbd_osd_req_op_create(WRITE, offset, length) */
  905. op->extent.offset = va_arg(args, u64);
  906. op->extent.length = va_arg(args, u64);
  907. if (opcode == CEPH_OSD_OP_WRITE)
  908. op->payload_len = op->extent.length;
  909. break;
  910. case CEPH_OSD_OP_CALL:
  911. /* rbd_osd_req_op_create(CALL, class, method, data, datalen) */
  912. op->cls.class_name = va_arg(args, char *);
  913. size = strlen(op->cls.class_name);
  914. rbd_assert(size <= (size_t) U8_MAX);
  915. op->cls.class_len = size;
  916. op->payload_len = size;
  917. op->cls.method_name = va_arg(args, char *);
  918. size = strlen(op->cls.method_name);
  919. rbd_assert(size <= (size_t) U8_MAX);
  920. op->cls.method_len = size;
  921. op->payload_len += size;
  922. op->cls.argc = 0;
  923. op->cls.indata = va_arg(args, void *);
  924. size = va_arg(args, size_t);
  925. rbd_assert(size <= (size_t) U32_MAX);
  926. op->cls.indata_len = (u32) size;
  927. op->payload_len += size;
  928. break;
  929. case CEPH_OSD_OP_NOTIFY_ACK:
  930. case CEPH_OSD_OP_WATCH:
  931. /* rbd_osd_req_op_create(NOTIFY_ACK, cookie, version) */
  932. /* rbd_osd_req_op_create(WATCH, cookie, version, flag) */
  933. op->watch.cookie = va_arg(args, u64);
  934. op->watch.ver = va_arg(args, u64);
  935. op->watch.ver = cpu_to_le64(op->watch.ver);
  936. if (opcode == CEPH_OSD_OP_WATCH && va_arg(args, int))
  937. op->watch.flag = (u8) 1;
  938. break;
  939. default:
  940. rbd_warn(NULL, "unsupported opcode %hu\n", opcode);
  941. kfree(op);
  942. op = NULL;
  943. break;
  944. }
  945. va_end(args);
  946. return op;
  947. }
  948. static void rbd_osd_req_op_destroy(struct ceph_osd_req_op *op)
  949. {
  950. kfree(op);
  951. }
  952. /*
  953. * Send ceph osd request
  954. */
  955. static int rbd_do_request(struct request *rq,
  956. struct rbd_device *rbd_dev,
  957. struct ceph_snap_context *snapc,
  958. u64 snapid,
  959. const char *object_name, u64 ofs, u64 len,
  960. struct bio *bio,
  961. struct page **pages,
  962. int num_pages,
  963. int flags,
  964. struct ceph_osd_req_op *op,
  965. void (*rbd_cb)(struct ceph_osd_request *,
  966. struct ceph_msg *),
  967. u64 *ver)
  968. {
  969. struct ceph_osd_client *osdc;
  970. struct ceph_osd_request *osd_req;
  971. struct timespec mtime = CURRENT_TIME;
  972. int ret;
  973. dout("rbd_do_request object_name=%s ofs=%llu len=%llu\n",
  974. object_name, (unsigned long long) ofs,
  975. (unsigned long long) len);
  976. osdc = &rbd_dev->rbd_client->client->osdc;
  977. osd_req = ceph_osdc_alloc_request(osdc, snapc, 1, false, GFP_NOIO);
  978. if (!osd_req)
  979. return -ENOMEM;
  980. osd_req->r_flags = flags;
  981. osd_req->r_pages = pages;
  982. if (bio) {
  983. osd_req->r_bio = bio;
  984. bio_get(osd_req->r_bio);
  985. }
  986. osd_req->r_callback = rbd_cb;
  987. osd_req->r_priv = NULL;
  988. strncpy(osd_req->r_oid, object_name, sizeof(osd_req->r_oid));
  989. osd_req->r_oid_len = strlen(osd_req->r_oid);
  990. osd_req->r_file_layout = rbd_dev->layout; /* struct */
  991. osd_req->r_num_pages = calc_pages_for(ofs, len);
  992. osd_req->r_page_alignment = ofs & ~PAGE_MASK;
  993. ceph_osdc_build_request(osd_req, ofs, len, 1, op,
  994. snapc, snapid, &mtime);
  995. if (op->op == CEPH_OSD_OP_WATCH && op->watch.flag) {
  996. ceph_osdc_set_request_linger(osdc, osd_req);
  997. rbd_dev->watch_request = osd_req;
  998. }
  999. ret = ceph_osdc_start_request(osdc, osd_req, false);
  1000. if (ret < 0)
  1001. goto done_err;
  1002. if (!rbd_cb) {
  1003. u64 version;
  1004. ret = ceph_osdc_wait_request(osdc, osd_req);
  1005. version = le64_to_cpu(osd_req->r_reassert_version.version);
  1006. if (ver)
  1007. *ver = version;
  1008. dout("reassert_ver=%llu\n", (unsigned long long) version);
  1009. ceph_osdc_put_request(osd_req);
  1010. }
  1011. return ret;
  1012. done_err:
  1013. if (bio)
  1014. bio_chain_put(osd_req->r_bio);
  1015. ceph_osdc_put_request(osd_req);
  1016. return ret;
  1017. }
  1018. static void rbd_simple_req_cb(struct ceph_osd_request *osd_req,
  1019. struct ceph_msg *msg)
  1020. {
  1021. ceph_osdc_put_request(osd_req);
  1022. }
  1023. /*
  1024. * Do a synchronous ceph osd operation
  1025. */
  1026. static int rbd_req_sync_op(struct rbd_device *rbd_dev,
  1027. int flags,
  1028. struct ceph_osd_req_op *op,
  1029. const char *object_name,
  1030. u64 ofs, u64 inbound_size,
  1031. char *inbound,
  1032. u64 *ver)
  1033. {
  1034. int ret;
  1035. struct page **pages;
  1036. int num_pages;
  1037. rbd_assert(op != NULL);
  1038. num_pages = calc_pages_for(ofs, inbound_size);
  1039. pages = ceph_alloc_page_vector(num_pages, GFP_KERNEL);
  1040. if (IS_ERR(pages))
  1041. return PTR_ERR(pages);
  1042. ret = rbd_do_request(NULL, rbd_dev, NULL, CEPH_NOSNAP,
  1043. object_name, ofs, inbound_size, NULL,
  1044. pages, num_pages,
  1045. flags,
  1046. op,
  1047. NULL,
  1048. ver);
  1049. if (ret < 0)
  1050. goto done;
  1051. if ((flags & CEPH_OSD_FLAG_READ) && inbound)
  1052. ret = ceph_copy_from_page_vector(pages, inbound, ofs, ret);
  1053. done:
  1054. ceph_release_page_vector(pages, num_pages);
  1055. return ret;
  1056. }
  1057. static int rbd_obj_request_submit(struct ceph_osd_client *osdc,
  1058. struct rbd_obj_request *obj_request)
  1059. {
  1060. return ceph_osdc_start_request(osdc, obj_request->osd_req, false);
  1061. }
  1062. static void rbd_img_request_complete(struct rbd_img_request *img_request)
  1063. {
  1064. if (img_request->callback)
  1065. img_request->callback(img_request);
  1066. else
  1067. rbd_img_request_put(img_request);
  1068. }
  1069. /* Caller is responsible for rbd_obj_request_destroy(obj_request) */
  1070. static int rbd_obj_request_wait(struct rbd_obj_request *obj_request)
  1071. {
  1072. return wait_for_completion_interruptible(&obj_request->completion);
  1073. }
  1074. static void rbd_osd_trivial_callback(struct rbd_obj_request *obj_request,
  1075. struct ceph_osd_op *op)
  1076. {
  1077. atomic_set(&obj_request->done, 1);
  1078. }
  1079. static void rbd_obj_request_complete(struct rbd_obj_request *obj_request)
  1080. {
  1081. if (obj_request->callback)
  1082. obj_request->callback(obj_request);
  1083. else
  1084. complete_all(&obj_request->completion);
  1085. }
  1086. /*
  1087. * Request sync osd watch
  1088. */
  1089. static int rbd_req_sync_notify_ack(struct rbd_device *rbd_dev,
  1090. u64 ver,
  1091. u64 notify_id)
  1092. {
  1093. struct ceph_osd_req_op *op;
  1094. int ret;
  1095. op = rbd_osd_req_op_create(CEPH_OSD_OP_NOTIFY_ACK, notify_id, ver);
  1096. if (!op)
  1097. return -ENOMEM;
  1098. ret = rbd_do_request(NULL, rbd_dev, NULL, CEPH_NOSNAP,
  1099. rbd_dev->header_name, 0, 0, NULL,
  1100. NULL, 0,
  1101. CEPH_OSD_FLAG_READ,
  1102. op,
  1103. rbd_simple_req_cb, NULL);
  1104. rbd_osd_req_op_destroy(op);
  1105. return ret;
  1106. }
  1107. /*
  1108. * Synchronous osd object method call
  1109. */
  1110. static int rbd_req_sync_exec(struct rbd_device *rbd_dev,
  1111. const char *object_name,
  1112. const char *class_name,
  1113. const char *method_name,
  1114. const char *outbound,
  1115. size_t outbound_size,
  1116. char *inbound,
  1117. size_t inbound_size,
  1118. u64 *ver)
  1119. {
  1120. struct ceph_osd_req_op *op;
  1121. int ret;
  1122. /*
  1123. * Any input parameters required by the method we're calling
  1124. * will be sent along with the class and method names as
  1125. * part of the message payload. That data and its size are
  1126. * supplied via the indata and indata_len fields (named from
  1127. * the perspective of the server side) in the OSD request
  1128. * operation.
  1129. */
  1130. op = rbd_osd_req_op_create(CEPH_OSD_OP_CALL, class_name,
  1131. method_name, outbound, outbound_size);
  1132. if (!op)
  1133. return -ENOMEM;
  1134. ret = rbd_req_sync_op(rbd_dev, CEPH_OSD_FLAG_READ, op,
  1135. object_name, 0, inbound_size, inbound,
  1136. ver);
  1137. rbd_osd_req_op_destroy(op);
  1138. dout("cls_exec returned %d\n", ret);
  1139. return ret;
  1140. }
  1141. static void rbd_osd_read_callback(struct rbd_obj_request *obj_request,
  1142. struct ceph_osd_op *op)
  1143. {
  1144. u64 xferred;
  1145. /*
  1146. * We support a 64-bit length, but ultimately it has to be
  1147. * passed to blk_end_request(), which takes an unsigned int.
  1148. */
  1149. xferred = le64_to_cpu(op->extent.length);
  1150. rbd_assert(xferred < (u64) UINT_MAX);
  1151. if (obj_request->result == (s32) -ENOENT) {
  1152. zero_bio_chain(obj_request->bio_list, 0);
  1153. obj_request->result = 0;
  1154. } else if (xferred < obj_request->length && !obj_request->result) {
  1155. zero_bio_chain(obj_request->bio_list, xferred);
  1156. xferred = obj_request->length;
  1157. }
  1158. obj_request->xferred = xferred;
  1159. atomic_set(&obj_request->done, 1);
  1160. }
  1161. static void rbd_osd_write_callback(struct rbd_obj_request *obj_request,
  1162. struct ceph_osd_op *op)
  1163. {
  1164. obj_request->xferred = le64_to_cpu(op->extent.length);
  1165. atomic_set(&obj_request->done, 1);
  1166. }
  1167. static void rbd_osd_req_callback(struct ceph_osd_request *osd_req,
  1168. struct ceph_msg *msg)
  1169. {
  1170. struct rbd_obj_request *obj_request = osd_req->r_priv;
  1171. struct ceph_osd_reply_head *reply_head;
  1172. struct ceph_osd_op *op;
  1173. u32 num_ops;
  1174. u16 opcode;
  1175. rbd_assert(osd_req == obj_request->osd_req);
  1176. rbd_assert(!!obj_request->img_request ^
  1177. (obj_request->which == BAD_WHICH));
  1178. obj_request->xferred = le32_to_cpu(msg->hdr.data_len);
  1179. reply_head = msg->front.iov_base;
  1180. obj_request->result = (s32) le32_to_cpu(reply_head->result);
  1181. obj_request->version = le64_to_cpu(osd_req->r_reassert_version.version);
  1182. num_ops = le32_to_cpu(reply_head->num_ops);
  1183. WARN_ON(num_ops != 1); /* For now */
  1184. op = &reply_head->ops[0];
  1185. opcode = le16_to_cpu(op->op);
  1186. switch (opcode) {
  1187. case CEPH_OSD_OP_READ:
  1188. rbd_osd_read_callback(obj_request, op);
  1189. break;
  1190. case CEPH_OSD_OP_WRITE:
  1191. rbd_osd_write_callback(obj_request, op);
  1192. break;
  1193. case CEPH_OSD_OP_NOTIFY_ACK:
  1194. case CEPH_OSD_OP_WATCH:
  1195. rbd_osd_trivial_callback(obj_request, op);
  1196. break;
  1197. default:
  1198. rbd_warn(NULL, "%s: unsupported op %hu\n",
  1199. obj_request->object_name, (unsigned short) opcode);
  1200. break;
  1201. }
  1202. if (atomic_read(&obj_request->done))
  1203. rbd_obj_request_complete(obj_request);
  1204. }
  1205. static struct ceph_osd_request *rbd_osd_req_create(
  1206. struct rbd_device *rbd_dev,
  1207. bool write_request,
  1208. struct rbd_obj_request *obj_request,
  1209. struct ceph_osd_req_op *op)
  1210. {
  1211. struct rbd_img_request *img_request = obj_request->img_request;
  1212. struct ceph_snap_context *snapc = NULL;
  1213. struct ceph_osd_client *osdc;
  1214. struct ceph_osd_request *osd_req;
  1215. struct timespec now;
  1216. struct timespec *mtime;
  1217. u64 snap_id = CEPH_NOSNAP;
  1218. u64 offset = obj_request->offset;
  1219. u64 length = obj_request->length;
  1220. if (img_request) {
  1221. rbd_assert(img_request->write_request == write_request);
  1222. if (img_request->write_request)
  1223. snapc = img_request->snapc;
  1224. else
  1225. snap_id = img_request->snap_id;
  1226. }
  1227. /* Allocate and initialize the request, for the single op */
  1228. osdc = &rbd_dev->rbd_client->client->osdc;
  1229. osd_req = ceph_osdc_alloc_request(osdc, snapc, 1, false, GFP_ATOMIC);
  1230. if (!osd_req)
  1231. return NULL; /* ENOMEM */
  1232. rbd_assert(obj_request_type_valid(obj_request->type));
  1233. switch (obj_request->type) {
  1234. case OBJ_REQUEST_NODATA:
  1235. break; /* Nothing to do */
  1236. case OBJ_REQUEST_BIO:
  1237. rbd_assert(obj_request->bio_list != NULL);
  1238. osd_req->r_bio = obj_request->bio_list;
  1239. bio_get(osd_req->r_bio);
  1240. /* osd client requires "num pages" even for bio */
  1241. osd_req->r_num_pages = calc_pages_for(offset, length);
  1242. break;
  1243. case OBJ_REQUEST_PAGES:
  1244. osd_req->r_pages = obj_request->pages;
  1245. osd_req->r_num_pages = obj_request->page_count;
  1246. osd_req->r_page_alignment = offset & ~PAGE_MASK;
  1247. break;
  1248. }
  1249. if (write_request) {
  1250. osd_req->r_flags = CEPH_OSD_FLAG_WRITE | CEPH_OSD_FLAG_ONDISK;
  1251. now = CURRENT_TIME;
  1252. mtime = &now;
  1253. } else {
  1254. osd_req->r_flags = CEPH_OSD_FLAG_READ;
  1255. mtime = NULL; /* not needed for reads */
  1256. offset = 0; /* These are not used... */
  1257. length = 0; /* ...for osd read requests */
  1258. }
  1259. osd_req->r_callback = rbd_osd_req_callback;
  1260. osd_req->r_priv = obj_request;
  1261. osd_req->r_oid_len = strlen(obj_request->object_name);
  1262. rbd_assert(osd_req->r_oid_len < sizeof (osd_req->r_oid));
  1263. memcpy(osd_req->r_oid, obj_request->object_name, osd_req->r_oid_len);
  1264. osd_req->r_file_layout = rbd_dev->layout; /* struct */
  1265. /* osd_req will get its own reference to snapc (if non-null) */
  1266. ceph_osdc_build_request(osd_req, offset, length, 1, op,
  1267. snapc, snap_id, mtime);
  1268. return osd_req;
  1269. }
  1270. static void rbd_osd_req_destroy(struct ceph_osd_request *osd_req)
  1271. {
  1272. ceph_osdc_put_request(osd_req);
  1273. }
  1274. /* object_name is assumed to be a non-null pointer and NUL-terminated */
  1275. static struct rbd_obj_request *rbd_obj_request_create(const char *object_name,
  1276. u64 offset, u64 length,
  1277. enum obj_request_type type)
  1278. {
  1279. struct rbd_obj_request *obj_request;
  1280. size_t size;
  1281. char *name;
  1282. rbd_assert(obj_request_type_valid(type));
  1283. size = strlen(object_name) + 1;
  1284. obj_request = kzalloc(sizeof (*obj_request) + size, GFP_KERNEL);
  1285. if (!obj_request)
  1286. return NULL;
  1287. name = (char *)(obj_request + 1);
  1288. obj_request->object_name = memcpy(name, object_name, size);
  1289. obj_request->offset = offset;
  1290. obj_request->length = length;
  1291. obj_request->which = BAD_WHICH;
  1292. obj_request->type = type;
  1293. INIT_LIST_HEAD(&obj_request->links);
  1294. atomic_set(&obj_request->done, 0);
  1295. init_completion(&obj_request->completion);
  1296. kref_init(&obj_request->kref);
  1297. return obj_request;
  1298. }
  1299. static void rbd_obj_request_destroy(struct kref *kref)
  1300. {
  1301. struct rbd_obj_request *obj_request;
  1302. obj_request = container_of(kref, struct rbd_obj_request, kref);
  1303. rbd_assert(obj_request->img_request == NULL);
  1304. rbd_assert(obj_request->which == BAD_WHICH);
  1305. if (obj_request->osd_req)
  1306. rbd_osd_req_destroy(obj_request->osd_req);
  1307. rbd_assert(obj_request_type_valid(obj_request->type));
  1308. switch (obj_request->type) {
  1309. case OBJ_REQUEST_NODATA:
  1310. break; /* Nothing to do */
  1311. case OBJ_REQUEST_BIO:
  1312. if (obj_request->bio_list)
  1313. bio_chain_put(obj_request->bio_list);
  1314. break;
  1315. case OBJ_REQUEST_PAGES:
  1316. if (obj_request->pages)
  1317. ceph_release_page_vector(obj_request->pages,
  1318. obj_request->page_count);
  1319. break;
  1320. }
  1321. kfree(obj_request);
  1322. }
  1323. /*
  1324. * Caller is responsible for filling in the list of object requests
  1325. * that comprises the image request, and the Linux request pointer
  1326. * (if there is one).
  1327. */
  1328. struct rbd_img_request *rbd_img_request_create(struct rbd_device *rbd_dev,
  1329. u64 offset, u64 length,
  1330. bool write_request)
  1331. {
  1332. struct rbd_img_request *img_request;
  1333. struct ceph_snap_context *snapc = NULL;
  1334. img_request = kmalloc(sizeof (*img_request), GFP_ATOMIC);
  1335. if (!img_request)
  1336. return NULL;
  1337. if (write_request) {
  1338. down_read(&rbd_dev->header_rwsem);
  1339. snapc = ceph_get_snap_context(rbd_dev->header.snapc);
  1340. up_read(&rbd_dev->header_rwsem);
  1341. if (WARN_ON(!snapc)) {
  1342. kfree(img_request);
  1343. return NULL; /* Shouldn't happen */
  1344. }
  1345. }
  1346. img_request->rq = NULL;
  1347. img_request->rbd_dev = rbd_dev;
  1348. img_request->offset = offset;
  1349. img_request->length = length;
  1350. img_request->write_request = write_request;
  1351. if (write_request)
  1352. img_request->snapc = snapc;
  1353. else
  1354. img_request->snap_id = rbd_dev->spec->snap_id;
  1355. spin_lock_init(&img_request->completion_lock);
  1356. img_request->next_completion = 0;
  1357. img_request->callback = NULL;
  1358. img_request->obj_request_count = 0;
  1359. INIT_LIST_HEAD(&img_request->obj_requests);
  1360. kref_init(&img_request->kref);
  1361. rbd_img_request_get(img_request); /* Avoid a warning */
  1362. rbd_img_request_put(img_request); /* TEMPORARY */
  1363. return img_request;
  1364. }
  1365. static void rbd_img_request_destroy(struct kref *kref)
  1366. {
  1367. struct rbd_img_request *img_request;
  1368. struct rbd_obj_request *obj_request;
  1369. struct rbd_obj_request *next_obj_request;
  1370. img_request = container_of(kref, struct rbd_img_request, kref);
  1371. for_each_obj_request_safe(img_request, obj_request, next_obj_request)
  1372. rbd_img_obj_request_del(img_request, obj_request);
  1373. if (img_request->write_request)
  1374. ceph_put_snap_context(img_request->snapc);
  1375. kfree(img_request);
  1376. }
  1377. static int rbd_img_request_fill_bio(struct rbd_img_request *img_request,
  1378. struct bio *bio_list)
  1379. {
  1380. struct rbd_device *rbd_dev = img_request->rbd_dev;
  1381. struct rbd_obj_request *obj_request = NULL;
  1382. struct rbd_obj_request *next_obj_request;
  1383. unsigned int bio_offset;
  1384. u64 image_offset;
  1385. u64 resid;
  1386. u16 opcode;
  1387. opcode = img_request->write_request ? CEPH_OSD_OP_WRITE
  1388. : CEPH_OSD_OP_READ;
  1389. bio_offset = 0;
  1390. image_offset = img_request->offset;
  1391. rbd_assert(image_offset == bio_list->bi_sector << SECTOR_SHIFT);
  1392. resid = img_request->length;
  1393. while (resid) {
  1394. const char *object_name;
  1395. unsigned int clone_size;
  1396. struct ceph_osd_req_op *op;
  1397. u64 offset;
  1398. u64 length;
  1399. object_name = rbd_segment_name(rbd_dev, image_offset);
  1400. if (!object_name)
  1401. goto out_unwind;
  1402. offset = rbd_segment_offset(rbd_dev, image_offset);
  1403. length = rbd_segment_length(rbd_dev, image_offset, resid);
  1404. obj_request = rbd_obj_request_create(object_name,
  1405. offset, length,
  1406. OBJ_REQUEST_BIO);
  1407. kfree(object_name); /* object request has its own copy */
  1408. if (!obj_request)
  1409. goto out_unwind;
  1410. rbd_assert(length <= (u64) UINT_MAX);
  1411. clone_size = (unsigned int) length;
  1412. obj_request->bio_list = bio_chain_clone_range(&bio_list,
  1413. &bio_offset, clone_size,
  1414. GFP_ATOMIC);
  1415. if (!obj_request->bio_list)
  1416. goto out_partial;
  1417. /*
  1418. * Build up the op to use in building the osd
  1419. * request. Note that the contents of the op are
  1420. * copied by rbd_osd_req_create().
  1421. */
  1422. op = rbd_osd_req_op_create(opcode, offset, length);
  1423. if (!op)
  1424. goto out_partial;
  1425. obj_request->osd_req = rbd_osd_req_create(rbd_dev,
  1426. img_request->write_request,
  1427. obj_request, op);
  1428. rbd_osd_req_op_destroy(op);
  1429. if (!obj_request->osd_req)
  1430. goto out_partial;
  1431. /* status and version are initially zero-filled */
  1432. rbd_img_obj_request_add(img_request, obj_request);
  1433. image_offset += length;
  1434. resid -= length;
  1435. }
  1436. return 0;
  1437. out_partial:
  1438. rbd_obj_request_put(obj_request);
  1439. out_unwind:
  1440. for_each_obj_request_safe(img_request, obj_request, next_obj_request)
  1441. rbd_obj_request_put(obj_request);
  1442. return -ENOMEM;
  1443. }
  1444. static void rbd_img_obj_callback(struct rbd_obj_request *obj_request)
  1445. {
  1446. struct rbd_img_request *img_request;
  1447. u32 which = obj_request->which;
  1448. bool more = true;
  1449. img_request = obj_request->img_request;
  1450. rbd_assert(img_request != NULL);
  1451. rbd_assert(img_request->rq != NULL);
  1452. rbd_assert(which != BAD_WHICH);
  1453. rbd_assert(which < img_request->obj_request_count);
  1454. rbd_assert(which >= img_request->next_completion);
  1455. spin_lock_irq(&img_request->completion_lock);
  1456. if (which != img_request->next_completion)
  1457. goto out;
  1458. for_each_obj_request_from(img_request, obj_request) {
  1459. unsigned int xferred;
  1460. int result;
  1461. rbd_assert(more);
  1462. rbd_assert(which < img_request->obj_request_count);
  1463. if (!atomic_read(&obj_request->done))
  1464. break;
  1465. rbd_assert(obj_request->xferred <= (u64) UINT_MAX);
  1466. xferred = (unsigned int) obj_request->xferred;
  1467. result = (int) obj_request->result;
  1468. if (result)
  1469. rbd_warn(NULL, "obj_request %s result %d xferred %u\n",
  1470. img_request->write_request ? "write" : "read",
  1471. result, xferred);
  1472. more = blk_end_request(img_request->rq, result, xferred);
  1473. which++;
  1474. }
  1475. rbd_assert(more ^ (which == img_request->obj_request_count));
  1476. img_request->next_completion = which;
  1477. out:
  1478. spin_unlock_irq(&img_request->completion_lock);
  1479. if (!more)
  1480. rbd_img_request_complete(img_request);
  1481. }
  1482. static int rbd_img_request_submit(struct rbd_img_request *img_request)
  1483. {
  1484. struct rbd_device *rbd_dev = img_request->rbd_dev;
  1485. struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
  1486. struct rbd_obj_request *obj_request;
  1487. for_each_obj_request(img_request, obj_request) {
  1488. int ret;
  1489. obj_request->callback = rbd_img_obj_callback;
  1490. ret = rbd_obj_request_submit(osdc, obj_request);
  1491. if (ret)
  1492. return ret;
  1493. /*
  1494. * The image request has its own reference to each
  1495. * of its object requests, so we can safely drop the
  1496. * initial one here.
  1497. */
  1498. rbd_obj_request_put(obj_request);
  1499. }
  1500. return 0;
  1501. }
  1502. static int rbd_obj_notify_ack_sync(struct rbd_device *rbd_dev,
  1503. u64 ver, u64 notify_id)
  1504. {
  1505. struct rbd_obj_request *obj_request;
  1506. struct ceph_osd_req_op *op;
  1507. struct ceph_osd_client *osdc;
  1508. int ret;
  1509. obj_request = rbd_obj_request_create(rbd_dev->header_name, 0, 0,
  1510. OBJ_REQUEST_NODATA);
  1511. if (!obj_request)
  1512. return -ENOMEM;
  1513. ret = -ENOMEM;
  1514. op = rbd_osd_req_op_create(CEPH_OSD_OP_NOTIFY_ACK, notify_id, ver);
  1515. if (!op)
  1516. goto out;
  1517. obj_request->osd_req = rbd_osd_req_create(rbd_dev, false,
  1518. obj_request, op);
  1519. rbd_osd_req_op_destroy(op);
  1520. if (!obj_request->osd_req)
  1521. goto out;
  1522. osdc = &rbd_dev->rbd_client->client->osdc;
  1523. ret = rbd_obj_request_submit(osdc, obj_request);
  1524. if (!ret)
  1525. ret = rbd_obj_request_wait(obj_request);
  1526. out:
  1527. rbd_obj_request_put(obj_request);
  1528. return ret;
  1529. }
  1530. static void rbd_watch_cb(u64 ver, u64 notify_id, u8 opcode, void *data)
  1531. {
  1532. struct rbd_device *rbd_dev = (struct rbd_device *)data;
  1533. u64 hver;
  1534. int rc;
  1535. if (!rbd_dev)
  1536. return;
  1537. dout("rbd_watch_cb %s notify_id=%llu opcode=%u\n",
  1538. rbd_dev->header_name, (unsigned long long) notify_id,
  1539. (unsigned int) opcode);
  1540. rc = rbd_dev_refresh(rbd_dev, &hver);
  1541. if (rc)
  1542. rbd_warn(rbd_dev, "got notification but failed to "
  1543. " update snaps: %d\n", rc);
  1544. (void) rbd_req_sync_notify_ack; /* avoid a warning */
  1545. rbd_obj_notify_ack_sync(rbd_dev, hver, notify_id);
  1546. }
  1547. /*
  1548. * Request sync osd watch/unwatch. The value of "start" determines
  1549. * whether a watch request is being initiated or torn down.
  1550. */
  1551. static int rbd_dev_header_watch_sync(struct rbd_device *rbd_dev, int start)
  1552. {
  1553. struct ceph_osd_client *osdc = &rbd_dev->rbd_client->client->osdc;
  1554. struct rbd_obj_request *obj_request;
  1555. struct ceph_osd_req_op *op;
  1556. int ret;
  1557. rbd_assert(start ^ !!rbd_dev->watch_event);
  1558. rbd_assert(start ^ !!rbd_dev->watch_request);
  1559. if (start) {
  1560. ret = ceph_osdc_create_event(osdc, rbd_watch_cb, 0, rbd_dev,
  1561. &rbd_dev->watch_event);
  1562. if (ret < 0)
  1563. return ret;
  1564. }
  1565. ret = -ENOMEM;
  1566. obj_request = rbd_obj_request_create(rbd_dev->header_name, 0, 0,
  1567. OBJ_REQUEST_NODATA);
  1568. if (!obj_request)
  1569. goto out_cancel;
  1570. op = rbd_osd_req_op_create(CEPH_OSD_OP_WATCH,
  1571. rbd_dev->watch_event->cookie,
  1572. rbd_dev->header.obj_version, start);
  1573. if (!op)
  1574. goto out_cancel;
  1575. obj_request->osd_req = rbd_osd_req_create(rbd_dev, true,
  1576. obj_request, op);
  1577. rbd_osd_req_op_destroy(op);
  1578. if (!obj_request->osd_req)
  1579. goto out_cancel;
  1580. if (start) {
  1581. rbd_dev->watch_request = obj_request->osd_req;
  1582. ceph_osdc_set_request_linger(osdc, rbd_dev->watch_request);
  1583. }
  1584. ret = rbd_obj_request_submit(osdc, obj_request);
  1585. if (ret)
  1586. goto out_cancel;
  1587. ret = rbd_obj_request_wait(obj_request);
  1588. if (ret)
  1589. goto out_cancel;
  1590. ret = obj_request->result;
  1591. if (ret)
  1592. goto out_cancel;
  1593. if (start)
  1594. goto done; /* Done if setting up the watch request */
  1595. out_cancel:
  1596. /* Cancel the event if we're tearing down, or on error */
  1597. ceph_osdc_cancel_event(rbd_dev->watch_event);
  1598. rbd_dev->watch_event = NULL;
  1599. done:
  1600. if (obj_request)
  1601. rbd_obj_request_put(obj_request);
  1602. return ret;
  1603. }
  1604. static void rbd_request_fn(struct request_queue *q)
  1605. {
  1606. struct rbd_device *rbd_dev = q->queuedata;
  1607. bool read_only = rbd_dev->mapping.read_only;
  1608. struct request *rq;
  1609. int result;
  1610. while ((rq = blk_fetch_request(q))) {
  1611. bool write_request = rq_data_dir(rq) == WRITE;
  1612. struct rbd_img_request *img_request;
  1613. u64 offset;
  1614. u64 length;
  1615. /* Ignore any non-FS requests that filter through. */
  1616. if (rq->cmd_type != REQ_TYPE_FS) {
  1617. __blk_end_request_all(rq, 0);
  1618. continue;
  1619. }
  1620. spin_unlock_irq(q->queue_lock);
  1621. /* Disallow writes to a read-only device */
  1622. if (write_request) {
  1623. result = -EROFS;
  1624. if (read_only)
  1625. goto end_request;
  1626. rbd_assert(rbd_dev->spec->snap_id == CEPH_NOSNAP);
  1627. }
  1628. /* Quit early if the snapshot has disappeared */
  1629. if (!atomic_read(&rbd_dev->exists)) {
  1630. dout("request for non-existent snapshot");
  1631. rbd_assert(rbd_dev->spec->snap_id != CEPH_NOSNAP);
  1632. result = -ENXIO;
  1633. goto end_request;
  1634. }
  1635. offset = (u64) blk_rq_pos(rq) << SECTOR_SHIFT;
  1636. length = (u64) blk_rq_bytes(rq);
  1637. result = -EINVAL;
  1638. if (WARN_ON(offset && length > U64_MAX - offset + 1))
  1639. goto end_request; /* Shouldn't happen */
  1640. result = -ENOMEM;
  1641. img_request = rbd_img_request_create(rbd_dev, offset, length,
  1642. write_request);
  1643. if (!img_request)
  1644. goto end_request;
  1645. img_request->rq = rq;
  1646. result = rbd_img_request_fill_bio(img_request, rq->bio);
  1647. if (!result)
  1648. result = rbd_img_request_submit(img_request);
  1649. if (result)
  1650. rbd_img_request_put(img_request);
  1651. end_request:
  1652. spin_lock_irq(q->queue_lock);
  1653. if (result < 0) {
  1654. rbd_warn(rbd_dev, "obj_request %s result %d\n",
  1655. write_request ? "write" : "read", result);
  1656. __blk_end_request_all(rq, result);
  1657. }
  1658. }
  1659. }
  1660. /*
  1661. * a queue callback. Makes sure that we don't create a bio that spans across
  1662. * multiple osd objects. One exception would be with a single page bios,
  1663. * which we handle later at bio_chain_clone_range()
  1664. */
  1665. static int rbd_merge_bvec(struct request_queue *q, struct bvec_merge_data *bmd,
  1666. struct bio_vec *bvec)
  1667. {
  1668. struct rbd_device *rbd_dev = q->queuedata;
  1669. sector_t sector_offset;
  1670. sector_t sectors_per_obj;
  1671. sector_t obj_sector_offset;
  1672. int ret;
  1673. /*
  1674. * Find how far into its rbd object the partition-relative
  1675. * bio start sector is to offset relative to the enclosing
  1676. * device.
  1677. */
  1678. sector_offset = get_start_sect(bmd->bi_bdev) + bmd->bi_sector;
  1679. sectors_per_obj = 1 << (rbd_dev->header.obj_order - SECTOR_SHIFT);
  1680. obj_sector_offset = sector_offset & (sectors_per_obj - 1);
  1681. /*
  1682. * Compute the number of bytes from that offset to the end
  1683. * of the object. Account for what's already used by the bio.
  1684. */
  1685. ret = (int) (sectors_per_obj - obj_sector_offset) << SECTOR_SHIFT;
  1686. if (ret > bmd->bi_size)
  1687. ret -= bmd->bi_size;
  1688. else
  1689. ret = 0;
  1690. /*
  1691. * Don't send back more than was asked for. And if the bio
  1692. * was empty, let the whole thing through because: "Note
  1693. * that a block device *must* allow a single page to be
  1694. * added to an empty bio."
  1695. */
  1696. rbd_assert(bvec->bv_len <= PAGE_SIZE);
  1697. if (ret > (int) bvec->bv_len || !bmd->bi_size)
  1698. ret = (int) bvec->bv_len;
  1699. return ret;
  1700. }
  1701. static void rbd_free_disk(struct rbd_device *rbd_dev)
  1702. {
  1703. struct gendisk *disk = rbd_dev->disk;
  1704. if (!disk)
  1705. return;
  1706. if (disk->flags & GENHD_FL_UP)
  1707. del_gendisk(disk);
  1708. if (disk->queue)
  1709. blk_cleanup_queue(disk->queue);
  1710. put_disk(disk);
  1711. }
  1712. static int rbd_obj_read_sync(struct rbd_device *rbd_dev,
  1713. const char *object_name,
  1714. u64 offset, u64 length,
  1715. char *buf, u64 *version)
  1716. {
  1717. struct ceph_osd_req_op *op;
  1718. struct rbd_obj_request *obj_request;
  1719. struct ceph_osd_client *osdc;
  1720. struct page **pages = NULL;
  1721. u32 page_count;
  1722. int ret;
  1723. page_count = (u32) calc_pages_for(offset, length);
  1724. pages = ceph_alloc_page_vector(page_count, GFP_KERNEL);
  1725. if (IS_ERR(pages))
  1726. ret = PTR_ERR(pages);
  1727. ret = -ENOMEM;
  1728. obj_request = rbd_obj_request_create(object_name, offset, length,
  1729. OBJ_REQUEST_PAGES);
  1730. if (!obj_request)
  1731. goto out;
  1732. obj_request->pages = pages;
  1733. obj_request->page_count = page_count;
  1734. op = rbd_osd_req_op_create(CEPH_OSD_OP_READ, offset, length);
  1735. if (!op)
  1736. goto out;
  1737. obj_request->osd_req = rbd_osd_req_create(rbd_dev, false,
  1738. obj_request, op);
  1739. rbd_osd_req_op_destroy(op);
  1740. if (!obj_request->osd_req)
  1741. goto out;
  1742. osdc = &rbd_dev->rbd_client->client->osdc;
  1743. ret = rbd_obj_request_submit(osdc, obj_request);
  1744. if (ret)
  1745. goto out;
  1746. ret = rbd_obj_request_wait(obj_request);
  1747. if (ret)
  1748. goto out;
  1749. ret = obj_request->result;
  1750. if (ret < 0)
  1751. goto out;
  1752. ret = ceph_copy_from_page_vector(pages, buf, 0, obj_request->xferred);
  1753. if (version)
  1754. *version = obj_request->version;
  1755. out:
  1756. if (obj_request)
  1757. rbd_obj_request_put(obj_request);
  1758. else
  1759. ceph_release_page_vector(pages, page_count);
  1760. return ret;
  1761. }
  1762. /*
  1763. * Read the complete header for the given rbd device.
  1764. *
  1765. * Returns a pointer to a dynamically-allocated buffer containing
  1766. * the complete and validated header. Caller can pass the address
  1767. * of a variable that will be filled in with the version of the
  1768. * header object at the time it was read.
  1769. *
  1770. * Returns a pointer-coded errno if a failure occurs.
  1771. */
  1772. static struct rbd_image_header_ondisk *
  1773. rbd_dev_v1_header_read(struct rbd_device *rbd_dev, u64 *version)
  1774. {
  1775. struct rbd_image_header_ondisk *ondisk = NULL;
  1776. u32 snap_count = 0;
  1777. u64 names_size = 0;
  1778. u32 want_count;
  1779. int ret;
  1780. /*
  1781. * The complete header will include an array of its 64-bit
  1782. * snapshot ids, followed by the names of those snapshots as
  1783. * a contiguous block of NUL-terminated strings. Note that
  1784. * the number of snapshots could change by the time we read
  1785. * it in, in which case we re-read it.
  1786. */
  1787. do {
  1788. size_t size;
  1789. kfree(ondisk);
  1790. size = sizeof (*ondisk);
  1791. size += snap_count * sizeof (struct rbd_image_snap_ondisk);
  1792. size += names_size;
  1793. ondisk = kmalloc(size, GFP_KERNEL);
  1794. if (!ondisk)
  1795. return ERR_PTR(-ENOMEM);
  1796. ret = rbd_obj_read_sync(rbd_dev, rbd_dev->header_name,
  1797. 0, size,
  1798. (char *) ondisk, version);
  1799. if (ret < 0)
  1800. goto out_err;
  1801. if (WARN_ON((size_t) ret < size)) {
  1802. ret = -ENXIO;
  1803. rbd_warn(rbd_dev, "short header read (want %zd got %d)",
  1804. size, ret);
  1805. goto out_err;
  1806. }
  1807. if (!rbd_dev_ondisk_valid(ondisk)) {
  1808. ret = -ENXIO;
  1809. rbd_warn(rbd_dev, "invalid header");
  1810. goto out_err;
  1811. }
  1812. names_size = le64_to_cpu(ondisk->snap_names_len);
  1813. want_count = snap_count;
  1814. snap_count = le32_to_cpu(ondisk->snap_count);
  1815. } while (snap_count != want_count);
  1816. return ondisk;
  1817. out_err:
  1818. kfree(ondisk);
  1819. return ERR_PTR(ret);
  1820. }
  1821. /*
  1822. * reload the ondisk the header
  1823. */
  1824. static int rbd_read_header(struct rbd_device *rbd_dev,
  1825. struct rbd_image_header *header)
  1826. {
  1827. struct rbd_image_header_ondisk *ondisk;
  1828. u64 ver = 0;
  1829. int ret;
  1830. ondisk = rbd_dev_v1_header_read(rbd_dev, &ver);
  1831. if (IS_ERR(ondisk))
  1832. return PTR_ERR(ondisk);
  1833. ret = rbd_header_from_disk(header, ondisk);
  1834. if (ret >= 0)
  1835. header->obj_version = ver;
  1836. kfree(ondisk);
  1837. return ret;
  1838. }
  1839. static void rbd_remove_all_snaps(struct rbd_device *rbd_dev)
  1840. {
  1841. struct rbd_snap *snap;
  1842. struct rbd_snap *next;
  1843. list_for_each_entry_safe(snap, next, &rbd_dev->snaps, node)
  1844. rbd_remove_snap_dev(snap);
  1845. }
  1846. static void rbd_update_mapping_size(struct rbd_device *rbd_dev)
  1847. {
  1848. sector_t size;
  1849. if (rbd_dev->spec->snap_id != CEPH_NOSNAP)
  1850. return;
  1851. size = (sector_t) rbd_dev->header.image_size / SECTOR_SIZE;
  1852. dout("setting size to %llu sectors", (unsigned long long) size);
  1853. rbd_dev->mapping.size = (u64) size;
  1854. set_capacity(rbd_dev->disk, size);
  1855. }
  1856. /*
  1857. * only read the first part of the ondisk header, without the snaps info
  1858. */
  1859. static int rbd_dev_v1_refresh(struct rbd_device *rbd_dev, u64 *hver)
  1860. {
  1861. int ret;
  1862. struct rbd_image_header h;
  1863. ret = rbd_read_header(rbd_dev, &h);
  1864. if (ret < 0)
  1865. return ret;
  1866. down_write(&rbd_dev->header_rwsem);
  1867. /* Update image size, and check for resize of mapped image */
  1868. rbd_dev->header.image_size = h.image_size;
  1869. rbd_update_mapping_size(rbd_dev);
  1870. /* rbd_dev->header.object_prefix shouldn't change */
  1871. kfree(rbd_dev->header.snap_sizes);
  1872. kfree(rbd_dev->header.snap_names);
  1873. /* osd requests may still refer to snapc */
  1874. ceph_put_snap_context(rbd_dev->header.snapc);
  1875. if (hver)
  1876. *hver = h.obj_version;
  1877. rbd_dev->header.obj_version = h.obj_version;
  1878. rbd_dev->header.image_size = h.image_size;
  1879. rbd_dev->header.snapc = h.snapc;
  1880. rbd_dev->header.snap_names = h.snap_names;
  1881. rbd_dev->header.snap_sizes = h.snap_sizes;
  1882. /* Free the extra copy of the object prefix */
  1883. WARN_ON(strcmp(rbd_dev->header.object_prefix, h.object_prefix));
  1884. kfree(h.object_prefix);
  1885. ret = rbd_dev_snaps_update(rbd_dev);
  1886. if (!ret)
  1887. ret = rbd_dev_snaps_register(rbd_dev);
  1888. up_write(&rbd_dev->header_rwsem);
  1889. return ret;
  1890. }
  1891. static int rbd_dev_refresh(struct rbd_device *rbd_dev, u64 *hver)
  1892. {
  1893. int ret;
  1894. rbd_assert(rbd_image_format_valid(rbd_dev->image_format));
  1895. mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
  1896. if (rbd_dev->image_format == 1)
  1897. ret = rbd_dev_v1_refresh(rbd_dev, hver);
  1898. else
  1899. ret = rbd_dev_v2_refresh(rbd_dev, hver);
  1900. mutex_unlock(&ctl_mutex);
  1901. return ret;
  1902. }
  1903. static int rbd_init_disk(struct rbd_device *rbd_dev)
  1904. {
  1905. struct gendisk *disk;
  1906. struct request_queue *q;
  1907. u64 segment_size;
  1908. /* create gendisk info */
  1909. disk = alloc_disk(RBD_MINORS_PER_MAJOR);
  1910. if (!disk)
  1911. return -ENOMEM;
  1912. snprintf(disk->disk_name, sizeof(disk->disk_name), RBD_DRV_NAME "%d",
  1913. rbd_dev->dev_id);
  1914. disk->major = rbd_dev->major;
  1915. disk->first_minor = 0;
  1916. disk->fops = &rbd_bd_ops;
  1917. disk->private_data = rbd_dev;
  1918. q = blk_init_queue(rbd_request_fn, &rbd_dev->lock);
  1919. if (!q)
  1920. goto out_disk;
  1921. /* We use the default size, but let's be explicit about it. */
  1922. blk_queue_physical_block_size(q, SECTOR_SIZE);
  1923. /* set io sizes to object size */
  1924. segment_size = rbd_obj_bytes(&rbd_dev->header);
  1925. blk_queue_max_hw_sectors(q, segment_size / SECTOR_SIZE);
  1926. blk_queue_max_segment_size(q, segment_size);
  1927. blk_queue_io_min(q, segment_size);
  1928. blk_queue_io_opt(q, segment_size);
  1929. blk_queue_merge_bvec(q, rbd_merge_bvec);
  1930. disk->queue = q;
  1931. q->queuedata = rbd_dev;
  1932. rbd_dev->disk = disk;
  1933. set_capacity(rbd_dev->disk, rbd_dev->mapping.size / SECTOR_SIZE);
  1934. return 0;
  1935. out_disk:
  1936. put_disk(disk);
  1937. return -ENOMEM;
  1938. }
  1939. /*
  1940. sysfs
  1941. */
  1942. static struct rbd_device *dev_to_rbd_dev(struct device *dev)
  1943. {
  1944. return container_of(dev, struct rbd_device, dev);
  1945. }
  1946. static ssize_t rbd_size_show(struct device *dev,
  1947. struct device_attribute *attr, char *buf)
  1948. {
  1949. struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
  1950. sector_t size;
  1951. down_read(&rbd_dev->header_rwsem);
  1952. size = get_capacity(rbd_dev->disk);
  1953. up_read(&rbd_dev->header_rwsem);
  1954. return sprintf(buf, "%llu\n", (unsigned long long) size * SECTOR_SIZE);
  1955. }
  1956. /*
  1957. * Note this shows the features for whatever's mapped, which is not
  1958. * necessarily the base image.
  1959. */
  1960. static ssize_t rbd_features_show(struct device *dev,
  1961. struct device_attribute *attr, char *buf)
  1962. {
  1963. struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
  1964. return sprintf(buf, "0x%016llx\n",
  1965. (unsigned long long) rbd_dev->mapping.features);
  1966. }
  1967. static ssize_t rbd_major_show(struct device *dev,
  1968. struct device_attribute *attr, char *buf)
  1969. {
  1970. struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
  1971. return sprintf(buf, "%d\n", rbd_dev->major);
  1972. }
  1973. static ssize_t rbd_client_id_show(struct device *dev,
  1974. struct device_attribute *attr, char *buf)
  1975. {
  1976. struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
  1977. return sprintf(buf, "client%lld\n",
  1978. ceph_client_id(rbd_dev->rbd_client->client));
  1979. }
  1980. static ssize_t rbd_pool_show(struct device *dev,
  1981. struct device_attribute *attr, char *buf)
  1982. {
  1983. struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
  1984. return sprintf(buf, "%s\n", rbd_dev->spec->pool_name);
  1985. }
  1986. static ssize_t rbd_pool_id_show(struct device *dev,
  1987. struct device_attribute *attr, char *buf)
  1988. {
  1989. struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
  1990. return sprintf(buf, "%llu\n",
  1991. (unsigned long long) rbd_dev->spec->pool_id);
  1992. }
  1993. static ssize_t rbd_name_show(struct device *dev,
  1994. struct device_attribute *attr, char *buf)
  1995. {
  1996. struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
  1997. if (rbd_dev->spec->image_name)
  1998. return sprintf(buf, "%s\n", rbd_dev->spec->image_name);
  1999. return sprintf(buf, "(unknown)\n");
  2000. }
  2001. static ssize_t rbd_image_id_show(struct device *dev,
  2002. struct device_attribute *attr, char *buf)
  2003. {
  2004. struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
  2005. return sprintf(buf, "%s\n", rbd_dev->spec->image_id);
  2006. }
  2007. /*
  2008. * Shows the name of the currently-mapped snapshot (or
  2009. * RBD_SNAP_HEAD_NAME for the base image).
  2010. */
  2011. static ssize_t rbd_snap_show(struct device *dev,
  2012. struct device_attribute *attr,
  2013. char *buf)
  2014. {
  2015. struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
  2016. return sprintf(buf, "%s\n", rbd_dev->spec->snap_name);
  2017. }
  2018. /*
  2019. * For an rbd v2 image, shows the pool id, image id, and snapshot id
  2020. * for the parent image. If there is no parent, simply shows
  2021. * "(no parent image)".
  2022. */
  2023. static ssize_t rbd_parent_show(struct device *dev,
  2024. struct device_attribute *attr,
  2025. char *buf)
  2026. {
  2027. struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
  2028. struct rbd_spec *spec = rbd_dev->parent_spec;
  2029. int count;
  2030. char *bufp = buf;
  2031. if (!spec)
  2032. return sprintf(buf, "(no parent image)\n");
  2033. count = sprintf(bufp, "pool_id %llu\npool_name %s\n",
  2034. (unsigned long long) spec->pool_id, spec->pool_name);
  2035. if (count < 0)
  2036. return count;
  2037. bufp += count;
  2038. count = sprintf(bufp, "image_id %s\nimage_name %s\n", spec->image_id,
  2039. spec->image_name ? spec->image_name : "(unknown)");
  2040. if (count < 0)
  2041. return count;
  2042. bufp += count;
  2043. count = sprintf(bufp, "snap_id %llu\nsnap_name %s\n",
  2044. (unsigned long long) spec->snap_id, spec->snap_name);
  2045. if (count < 0)
  2046. return count;
  2047. bufp += count;
  2048. count = sprintf(bufp, "overlap %llu\n", rbd_dev->parent_overlap);
  2049. if (count < 0)
  2050. return count;
  2051. bufp += count;
  2052. return (ssize_t) (bufp - buf);
  2053. }
  2054. static ssize_t rbd_image_refresh(struct device *dev,
  2055. struct device_attribute *attr,
  2056. const char *buf,
  2057. size_t size)
  2058. {
  2059. struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
  2060. int ret;
  2061. ret = rbd_dev_refresh(rbd_dev, NULL);
  2062. return ret < 0 ? ret : size;
  2063. }
  2064. static DEVICE_ATTR(size, S_IRUGO, rbd_size_show, NULL);
  2065. static DEVICE_ATTR(features, S_IRUGO, rbd_features_show, NULL);
  2066. static DEVICE_ATTR(major, S_IRUGO, rbd_major_show, NULL);
  2067. static DEVICE_ATTR(client_id, S_IRUGO, rbd_client_id_show, NULL);
  2068. static DEVICE_ATTR(pool, S_IRUGO, rbd_pool_show, NULL);
  2069. static DEVICE_ATTR(pool_id, S_IRUGO, rbd_pool_id_show, NULL);
  2070. static DEVICE_ATTR(name, S_IRUGO, rbd_name_show, NULL);
  2071. static DEVICE_ATTR(image_id, S_IRUGO, rbd_image_id_show, NULL);
  2072. static DEVICE_ATTR(refresh, S_IWUSR, NULL, rbd_image_refresh);
  2073. static DEVICE_ATTR(current_snap, S_IRUGO, rbd_snap_show, NULL);
  2074. static DEVICE_ATTR(parent, S_IRUGO, rbd_parent_show, NULL);
  2075. static struct attribute *rbd_attrs[] = {
  2076. &dev_attr_size.attr,
  2077. &dev_attr_features.attr,
  2078. &dev_attr_major.attr,
  2079. &dev_attr_client_id.attr,
  2080. &dev_attr_pool.attr,
  2081. &dev_attr_pool_id.attr,
  2082. &dev_attr_name.attr,
  2083. &dev_attr_image_id.attr,
  2084. &dev_attr_current_snap.attr,
  2085. &dev_attr_parent.attr,
  2086. &dev_attr_refresh.attr,
  2087. NULL
  2088. };
  2089. static struct attribute_group rbd_attr_group = {
  2090. .attrs = rbd_attrs,
  2091. };
  2092. static const struct attribute_group *rbd_attr_groups[] = {
  2093. &rbd_attr_group,
  2094. NULL
  2095. };
  2096. static void rbd_sysfs_dev_release(struct device *dev)
  2097. {
  2098. }
  2099. static struct device_type rbd_device_type = {
  2100. .name = "rbd",
  2101. .groups = rbd_attr_groups,
  2102. .release = rbd_sysfs_dev_release,
  2103. };
  2104. /*
  2105. sysfs - snapshots
  2106. */
  2107. static ssize_t rbd_snap_size_show(struct device *dev,
  2108. struct device_attribute *attr,
  2109. char *buf)
  2110. {
  2111. struct rbd_snap *snap = container_of(dev, struct rbd_snap, dev);
  2112. return sprintf(buf, "%llu\n", (unsigned long long)snap->size);
  2113. }
  2114. static ssize_t rbd_snap_id_show(struct device *dev,
  2115. struct device_attribute *attr,
  2116. char *buf)
  2117. {
  2118. struct rbd_snap *snap = container_of(dev, struct rbd_snap, dev);
  2119. return sprintf(buf, "%llu\n", (unsigned long long)snap->id);
  2120. }
  2121. static ssize_t rbd_snap_features_show(struct device *dev,
  2122. struct device_attribute *attr,
  2123. char *buf)
  2124. {
  2125. struct rbd_snap *snap = container_of(dev, struct rbd_snap, dev);
  2126. return sprintf(buf, "0x%016llx\n",
  2127. (unsigned long long) snap->features);
  2128. }
  2129. static DEVICE_ATTR(snap_size, S_IRUGO, rbd_snap_size_show, NULL);
  2130. static DEVICE_ATTR(snap_id, S_IRUGO, rbd_snap_id_show, NULL);
  2131. static DEVICE_ATTR(snap_features, S_IRUGO, rbd_snap_features_show, NULL);
  2132. static struct attribute *rbd_snap_attrs[] = {
  2133. &dev_attr_snap_size.attr,
  2134. &dev_attr_snap_id.attr,
  2135. &dev_attr_snap_features.attr,
  2136. NULL,
  2137. };
  2138. static struct attribute_group rbd_snap_attr_group = {
  2139. .attrs = rbd_snap_attrs,
  2140. };
  2141. static void rbd_snap_dev_release(struct device *dev)
  2142. {
  2143. struct rbd_snap *snap = container_of(dev, struct rbd_snap, dev);
  2144. kfree(snap->name);
  2145. kfree(snap);
  2146. }
  2147. static const struct attribute_group *rbd_snap_attr_groups[] = {
  2148. &rbd_snap_attr_group,
  2149. NULL
  2150. };
  2151. static struct device_type rbd_snap_device_type = {
  2152. .groups = rbd_snap_attr_groups,
  2153. .release = rbd_snap_dev_release,
  2154. };
  2155. static struct rbd_spec *rbd_spec_get(struct rbd_spec *spec)
  2156. {
  2157. kref_get(&spec->kref);
  2158. return spec;
  2159. }
  2160. static void rbd_spec_free(struct kref *kref);
  2161. static void rbd_spec_put(struct rbd_spec *spec)
  2162. {
  2163. if (spec)
  2164. kref_put(&spec->kref, rbd_spec_free);
  2165. }
  2166. static struct rbd_spec *rbd_spec_alloc(void)
  2167. {
  2168. struct rbd_spec *spec;
  2169. spec = kzalloc(sizeof (*spec), GFP_KERNEL);
  2170. if (!spec)
  2171. return NULL;
  2172. kref_init(&spec->kref);
  2173. rbd_spec_put(rbd_spec_get(spec)); /* TEMPORARY */
  2174. return spec;
  2175. }
  2176. static void rbd_spec_free(struct kref *kref)
  2177. {
  2178. struct rbd_spec *spec = container_of(kref, struct rbd_spec, kref);
  2179. kfree(spec->pool_name);
  2180. kfree(spec->image_id);
  2181. kfree(spec->image_name);
  2182. kfree(spec->snap_name);
  2183. kfree(spec);
  2184. }
  2185. struct rbd_device *rbd_dev_create(struct rbd_client *rbdc,
  2186. struct rbd_spec *spec)
  2187. {
  2188. struct rbd_device *rbd_dev;
  2189. rbd_dev = kzalloc(sizeof (*rbd_dev), GFP_KERNEL);
  2190. if (!rbd_dev)
  2191. return NULL;
  2192. spin_lock_init(&rbd_dev->lock);
  2193. atomic_set(&rbd_dev->exists, 0);
  2194. INIT_LIST_HEAD(&rbd_dev->node);
  2195. INIT_LIST_HEAD(&rbd_dev->snaps);
  2196. init_rwsem(&rbd_dev->header_rwsem);
  2197. rbd_dev->spec = spec;
  2198. rbd_dev->rbd_client = rbdc;
  2199. /* Initialize the layout used for all rbd requests */
  2200. rbd_dev->layout.fl_stripe_unit = cpu_to_le32(1 << RBD_MAX_OBJ_ORDER);
  2201. rbd_dev->layout.fl_stripe_count = cpu_to_le32(1);
  2202. rbd_dev->layout.fl_object_size = cpu_to_le32(1 << RBD_MAX_OBJ_ORDER);
  2203. rbd_dev->layout.fl_pg_pool = cpu_to_le32((u32) spec->pool_id);
  2204. return rbd_dev;
  2205. }
  2206. static void rbd_dev_destroy(struct rbd_device *rbd_dev)
  2207. {
  2208. rbd_spec_put(rbd_dev->parent_spec);
  2209. kfree(rbd_dev->header_name);
  2210. rbd_put_client(rbd_dev->rbd_client);
  2211. rbd_spec_put(rbd_dev->spec);
  2212. kfree(rbd_dev);
  2213. }
  2214. static bool rbd_snap_registered(struct rbd_snap *snap)
  2215. {
  2216. bool ret = snap->dev.type == &rbd_snap_device_type;
  2217. bool reg = device_is_registered(&snap->dev);
  2218. rbd_assert(!ret ^ reg);
  2219. return ret;
  2220. }
  2221. static void rbd_remove_snap_dev(struct rbd_snap *snap)
  2222. {
  2223. list_del(&snap->node);
  2224. if (device_is_registered(&snap->dev))
  2225. device_unregister(&snap->dev);
  2226. }
  2227. static int rbd_register_snap_dev(struct rbd_snap *snap,
  2228. struct device *parent)
  2229. {
  2230. struct device *dev = &snap->dev;
  2231. int ret;
  2232. dev->type = &rbd_snap_device_type;
  2233. dev->parent = parent;
  2234. dev->release = rbd_snap_dev_release;
  2235. dev_set_name(dev, "%s%s", RBD_SNAP_DEV_NAME_PREFIX, snap->name);
  2236. dout("%s: registering device for snapshot %s\n", __func__, snap->name);
  2237. ret = device_register(dev);
  2238. return ret;
  2239. }
  2240. static struct rbd_snap *__rbd_add_snap_dev(struct rbd_device *rbd_dev,
  2241. const char *snap_name,
  2242. u64 snap_id, u64 snap_size,
  2243. u64 snap_features)
  2244. {
  2245. struct rbd_snap *snap;
  2246. int ret;
  2247. snap = kzalloc(sizeof (*snap), GFP_KERNEL);
  2248. if (!snap)
  2249. return ERR_PTR(-ENOMEM);
  2250. ret = -ENOMEM;
  2251. snap->name = kstrdup(snap_name, GFP_KERNEL);
  2252. if (!snap->name)
  2253. goto err;
  2254. snap->id = snap_id;
  2255. snap->size = snap_size;
  2256. snap->features = snap_features;
  2257. return snap;
  2258. err:
  2259. kfree(snap->name);
  2260. kfree(snap);
  2261. return ERR_PTR(ret);
  2262. }
  2263. static char *rbd_dev_v1_snap_info(struct rbd_device *rbd_dev, u32 which,
  2264. u64 *snap_size, u64 *snap_features)
  2265. {
  2266. char *snap_name;
  2267. rbd_assert(which < rbd_dev->header.snapc->num_snaps);
  2268. *snap_size = rbd_dev->header.snap_sizes[which];
  2269. *snap_features = 0; /* No features for v1 */
  2270. /* Skip over names until we find the one we are looking for */
  2271. snap_name = rbd_dev->header.snap_names;
  2272. while (which--)
  2273. snap_name += strlen(snap_name) + 1;
  2274. return snap_name;
  2275. }
  2276. /*
  2277. * Get the size and object order for an image snapshot, or if
  2278. * snap_id is CEPH_NOSNAP, gets this information for the base
  2279. * image.
  2280. */
  2281. static int _rbd_dev_v2_snap_size(struct rbd_device *rbd_dev, u64 snap_id,
  2282. u8 *order, u64 *snap_size)
  2283. {
  2284. __le64 snapid = cpu_to_le64(snap_id);
  2285. int ret;
  2286. struct {
  2287. u8 order;
  2288. __le64 size;
  2289. } __attribute__ ((packed)) size_buf = { 0 };
  2290. ret = rbd_req_sync_exec(rbd_dev, rbd_dev->header_name,
  2291. "rbd", "get_size",
  2292. (char *) &snapid, sizeof (snapid),
  2293. (char *) &size_buf, sizeof (size_buf), NULL);
  2294. dout("%s: rbd_req_sync_exec returned %d\n", __func__, ret);
  2295. if (ret < 0)
  2296. return ret;
  2297. *order = size_buf.order;
  2298. *snap_size = le64_to_cpu(size_buf.size);
  2299. dout(" snap_id 0x%016llx order = %u, snap_size = %llu\n",
  2300. (unsigned long long) snap_id, (unsigned int) *order,
  2301. (unsigned long long) *snap_size);
  2302. return 0;
  2303. }
  2304. static int rbd_dev_v2_image_size(struct rbd_device *rbd_dev)
  2305. {
  2306. return _rbd_dev_v2_snap_size(rbd_dev, CEPH_NOSNAP,
  2307. &rbd_dev->header.obj_order,
  2308. &rbd_dev->header.image_size);
  2309. }
  2310. static int rbd_dev_v2_object_prefix(struct rbd_device *rbd_dev)
  2311. {
  2312. void *reply_buf;
  2313. int ret;
  2314. void *p;
  2315. reply_buf = kzalloc(RBD_OBJ_PREFIX_LEN_MAX, GFP_KERNEL);
  2316. if (!reply_buf)
  2317. return -ENOMEM;
  2318. ret = rbd_req_sync_exec(rbd_dev, rbd_dev->header_name,
  2319. "rbd", "get_object_prefix",
  2320. NULL, 0,
  2321. reply_buf, RBD_OBJ_PREFIX_LEN_MAX, NULL);
  2322. dout("%s: rbd_req_sync_exec returned %d\n", __func__, ret);
  2323. if (ret < 0)
  2324. goto out;
  2325. ret = 0; /* rbd_req_sync_exec() can return positive */
  2326. p = reply_buf;
  2327. rbd_dev->header.object_prefix = ceph_extract_encoded_string(&p,
  2328. p + RBD_OBJ_PREFIX_LEN_MAX,
  2329. NULL, GFP_NOIO);
  2330. if (IS_ERR(rbd_dev->header.object_prefix)) {
  2331. ret = PTR_ERR(rbd_dev->header.object_prefix);
  2332. rbd_dev->header.object_prefix = NULL;
  2333. } else {
  2334. dout(" object_prefix = %s\n", rbd_dev->header.object_prefix);
  2335. }
  2336. out:
  2337. kfree(reply_buf);
  2338. return ret;
  2339. }
  2340. static int _rbd_dev_v2_snap_features(struct rbd_device *rbd_dev, u64 snap_id,
  2341. u64 *snap_features)
  2342. {
  2343. __le64 snapid = cpu_to_le64(snap_id);
  2344. struct {
  2345. __le64 features;
  2346. __le64 incompat;
  2347. } features_buf = { 0 };
  2348. u64 incompat;
  2349. int ret;
  2350. ret = rbd_req_sync_exec(rbd_dev, rbd_dev->header_name,
  2351. "rbd", "get_features",
  2352. (char *) &snapid, sizeof (snapid),
  2353. (char *) &features_buf, sizeof (features_buf),
  2354. NULL);
  2355. dout("%s: rbd_req_sync_exec returned %d\n", __func__, ret);
  2356. if (ret < 0)
  2357. return ret;
  2358. incompat = le64_to_cpu(features_buf.incompat);
  2359. if (incompat & ~RBD_FEATURES_ALL)
  2360. return -ENXIO;
  2361. *snap_features = le64_to_cpu(features_buf.features);
  2362. dout(" snap_id 0x%016llx features = 0x%016llx incompat = 0x%016llx\n",
  2363. (unsigned long long) snap_id,
  2364. (unsigned long long) *snap_features,
  2365. (unsigned long long) le64_to_cpu(features_buf.incompat));
  2366. return 0;
  2367. }
  2368. static int rbd_dev_v2_features(struct rbd_device *rbd_dev)
  2369. {
  2370. return _rbd_dev_v2_snap_features(rbd_dev, CEPH_NOSNAP,
  2371. &rbd_dev->header.features);
  2372. }
  2373. static int rbd_dev_v2_parent_info(struct rbd_device *rbd_dev)
  2374. {
  2375. struct rbd_spec *parent_spec;
  2376. size_t size;
  2377. void *reply_buf = NULL;
  2378. __le64 snapid;
  2379. void *p;
  2380. void *end;
  2381. char *image_id;
  2382. u64 overlap;
  2383. int ret;
  2384. parent_spec = rbd_spec_alloc();
  2385. if (!parent_spec)
  2386. return -ENOMEM;
  2387. size = sizeof (__le64) + /* pool_id */
  2388. sizeof (__le32) + RBD_IMAGE_ID_LEN_MAX + /* image_id */
  2389. sizeof (__le64) + /* snap_id */
  2390. sizeof (__le64); /* overlap */
  2391. reply_buf = kmalloc(size, GFP_KERNEL);
  2392. if (!reply_buf) {
  2393. ret = -ENOMEM;
  2394. goto out_err;
  2395. }
  2396. snapid = cpu_to_le64(CEPH_NOSNAP);
  2397. ret = rbd_req_sync_exec(rbd_dev, rbd_dev->header_name,
  2398. "rbd", "get_parent",
  2399. (char *) &snapid, sizeof (snapid),
  2400. (char *) reply_buf, size, NULL);
  2401. dout("%s: rbd_req_sync_exec returned %d\n", __func__, ret);
  2402. if (ret < 0)
  2403. goto out_err;
  2404. ret = -ERANGE;
  2405. p = reply_buf;
  2406. end = (char *) reply_buf + size;
  2407. ceph_decode_64_safe(&p, end, parent_spec->pool_id, out_err);
  2408. if (parent_spec->pool_id == CEPH_NOPOOL)
  2409. goto out; /* No parent? No problem. */
  2410. /* The ceph file layout needs to fit pool id in 32 bits */
  2411. ret = -EIO;
  2412. if (WARN_ON(parent_spec->pool_id > (u64) U32_MAX))
  2413. goto out;
  2414. image_id = ceph_extract_encoded_string(&p, end, NULL, GFP_KERNEL);
  2415. if (IS_ERR(image_id)) {
  2416. ret = PTR_ERR(image_id);
  2417. goto out_err;
  2418. }
  2419. parent_spec->image_id = image_id;
  2420. ceph_decode_64_safe(&p, end, parent_spec->snap_id, out_err);
  2421. ceph_decode_64_safe(&p, end, overlap, out_err);
  2422. rbd_dev->parent_overlap = overlap;
  2423. rbd_dev->parent_spec = parent_spec;
  2424. parent_spec = NULL; /* rbd_dev now owns this */
  2425. out:
  2426. ret = 0;
  2427. out_err:
  2428. kfree(reply_buf);
  2429. rbd_spec_put(parent_spec);
  2430. return ret;
  2431. }
  2432. static char *rbd_dev_image_name(struct rbd_device *rbd_dev)
  2433. {
  2434. size_t image_id_size;
  2435. char *image_id;
  2436. void *p;
  2437. void *end;
  2438. size_t size;
  2439. void *reply_buf = NULL;
  2440. size_t len = 0;
  2441. char *image_name = NULL;
  2442. int ret;
  2443. rbd_assert(!rbd_dev->spec->image_name);
  2444. len = strlen(rbd_dev->spec->image_id);
  2445. image_id_size = sizeof (__le32) + len;
  2446. image_id = kmalloc(image_id_size, GFP_KERNEL);
  2447. if (!image_id)
  2448. return NULL;
  2449. p = image_id;
  2450. end = (char *) image_id + image_id_size;
  2451. ceph_encode_string(&p, end, rbd_dev->spec->image_id, (u32) len);
  2452. size = sizeof (__le32) + RBD_IMAGE_NAME_LEN_MAX;
  2453. reply_buf = kmalloc(size, GFP_KERNEL);
  2454. if (!reply_buf)
  2455. goto out;
  2456. ret = rbd_req_sync_exec(rbd_dev, RBD_DIRECTORY,
  2457. "rbd", "dir_get_name",
  2458. image_id, image_id_size,
  2459. (char *) reply_buf, size, NULL);
  2460. if (ret < 0)
  2461. goto out;
  2462. p = reply_buf;
  2463. end = (char *) reply_buf + size;
  2464. image_name = ceph_extract_encoded_string(&p, end, &len, GFP_KERNEL);
  2465. if (IS_ERR(image_name))
  2466. image_name = NULL;
  2467. else
  2468. dout("%s: name is %s len is %zd\n", __func__, image_name, len);
  2469. out:
  2470. kfree(reply_buf);
  2471. kfree(image_id);
  2472. return image_name;
  2473. }
  2474. /*
  2475. * When a parent image gets probed, we only have the pool, image,
  2476. * and snapshot ids but not the names of any of them. This call
  2477. * is made later to fill in those names. It has to be done after
  2478. * rbd_dev_snaps_update() has completed because some of the
  2479. * information (in particular, snapshot name) is not available
  2480. * until then.
  2481. */
  2482. static int rbd_dev_probe_update_spec(struct rbd_device *rbd_dev)
  2483. {
  2484. struct ceph_osd_client *osdc;
  2485. const char *name;
  2486. void *reply_buf = NULL;
  2487. int ret;
  2488. if (rbd_dev->spec->pool_name)
  2489. return 0; /* Already have the names */
  2490. /* Look up the pool name */
  2491. osdc = &rbd_dev->rbd_client->client->osdc;
  2492. name = ceph_pg_pool_name_by_id(osdc->osdmap, rbd_dev->spec->pool_id);
  2493. if (!name) {
  2494. rbd_warn(rbd_dev, "there is no pool with id %llu",
  2495. rbd_dev->spec->pool_id); /* Really a BUG() */
  2496. return -EIO;
  2497. }
  2498. rbd_dev->spec->pool_name = kstrdup(name, GFP_KERNEL);
  2499. if (!rbd_dev->spec->pool_name)
  2500. return -ENOMEM;
  2501. /* Fetch the image name; tolerate failure here */
  2502. name = rbd_dev_image_name(rbd_dev);
  2503. if (name)
  2504. rbd_dev->spec->image_name = (char *) name;
  2505. else
  2506. rbd_warn(rbd_dev, "unable to get image name");
  2507. /* Look up the snapshot name. */
  2508. name = rbd_snap_name(rbd_dev, rbd_dev->spec->snap_id);
  2509. if (!name) {
  2510. rbd_warn(rbd_dev, "no snapshot with id %llu",
  2511. rbd_dev->spec->snap_id); /* Really a BUG() */
  2512. ret = -EIO;
  2513. goto out_err;
  2514. }
  2515. rbd_dev->spec->snap_name = kstrdup(name, GFP_KERNEL);
  2516. if(!rbd_dev->spec->snap_name)
  2517. goto out_err;
  2518. return 0;
  2519. out_err:
  2520. kfree(reply_buf);
  2521. kfree(rbd_dev->spec->pool_name);
  2522. rbd_dev->spec->pool_name = NULL;
  2523. return ret;
  2524. }
  2525. static int rbd_dev_v2_snap_context(struct rbd_device *rbd_dev, u64 *ver)
  2526. {
  2527. size_t size;
  2528. int ret;
  2529. void *reply_buf;
  2530. void *p;
  2531. void *end;
  2532. u64 seq;
  2533. u32 snap_count;
  2534. struct ceph_snap_context *snapc;
  2535. u32 i;
  2536. /*
  2537. * We'll need room for the seq value (maximum snapshot id),
  2538. * snapshot count, and array of that many snapshot ids.
  2539. * For now we have a fixed upper limit on the number we're
  2540. * prepared to receive.
  2541. */
  2542. size = sizeof (__le64) + sizeof (__le32) +
  2543. RBD_MAX_SNAP_COUNT * sizeof (__le64);
  2544. reply_buf = kzalloc(size, GFP_KERNEL);
  2545. if (!reply_buf)
  2546. return -ENOMEM;
  2547. ret = rbd_req_sync_exec(rbd_dev, rbd_dev->header_name,
  2548. "rbd", "get_snapcontext",
  2549. NULL, 0,
  2550. reply_buf, size, ver);
  2551. dout("%s: rbd_req_sync_exec returned %d\n", __func__, ret);
  2552. if (ret < 0)
  2553. goto out;
  2554. ret = -ERANGE;
  2555. p = reply_buf;
  2556. end = (char *) reply_buf + size;
  2557. ceph_decode_64_safe(&p, end, seq, out);
  2558. ceph_decode_32_safe(&p, end, snap_count, out);
  2559. /*
  2560. * Make sure the reported number of snapshot ids wouldn't go
  2561. * beyond the end of our buffer. But before checking that,
  2562. * make sure the computed size of the snapshot context we
  2563. * allocate is representable in a size_t.
  2564. */
  2565. if (snap_count > (SIZE_MAX - sizeof (struct ceph_snap_context))
  2566. / sizeof (u64)) {
  2567. ret = -EINVAL;
  2568. goto out;
  2569. }
  2570. if (!ceph_has_room(&p, end, snap_count * sizeof (__le64)))
  2571. goto out;
  2572. size = sizeof (struct ceph_snap_context) +
  2573. snap_count * sizeof (snapc->snaps[0]);
  2574. snapc = kmalloc(size, GFP_KERNEL);
  2575. if (!snapc) {
  2576. ret = -ENOMEM;
  2577. goto out;
  2578. }
  2579. atomic_set(&snapc->nref, 1);
  2580. snapc->seq = seq;
  2581. snapc->num_snaps = snap_count;
  2582. for (i = 0; i < snap_count; i++)
  2583. snapc->snaps[i] = ceph_decode_64(&p);
  2584. rbd_dev->header.snapc = snapc;
  2585. dout(" snap context seq = %llu, snap_count = %u\n",
  2586. (unsigned long long) seq, (unsigned int) snap_count);
  2587. out:
  2588. kfree(reply_buf);
  2589. return 0;
  2590. }
  2591. static char *rbd_dev_v2_snap_name(struct rbd_device *rbd_dev, u32 which)
  2592. {
  2593. size_t size;
  2594. void *reply_buf;
  2595. __le64 snap_id;
  2596. int ret;
  2597. void *p;
  2598. void *end;
  2599. char *snap_name;
  2600. size = sizeof (__le32) + RBD_MAX_SNAP_NAME_LEN;
  2601. reply_buf = kmalloc(size, GFP_KERNEL);
  2602. if (!reply_buf)
  2603. return ERR_PTR(-ENOMEM);
  2604. snap_id = cpu_to_le64(rbd_dev->header.snapc->snaps[which]);
  2605. ret = rbd_req_sync_exec(rbd_dev, rbd_dev->header_name,
  2606. "rbd", "get_snapshot_name",
  2607. (char *) &snap_id, sizeof (snap_id),
  2608. reply_buf, size, NULL);
  2609. dout("%s: rbd_req_sync_exec returned %d\n", __func__, ret);
  2610. if (ret < 0)
  2611. goto out;
  2612. p = reply_buf;
  2613. end = (char *) reply_buf + size;
  2614. snap_name = ceph_extract_encoded_string(&p, end, NULL, GFP_KERNEL);
  2615. if (IS_ERR(snap_name)) {
  2616. ret = PTR_ERR(snap_name);
  2617. goto out;
  2618. } else {
  2619. dout(" snap_id 0x%016llx snap_name = %s\n",
  2620. (unsigned long long) le64_to_cpu(snap_id), snap_name);
  2621. }
  2622. kfree(reply_buf);
  2623. return snap_name;
  2624. out:
  2625. kfree(reply_buf);
  2626. return ERR_PTR(ret);
  2627. }
  2628. static char *rbd_dev_v2_snap_info(struct rbd_device *rbd_dev, u32 which,
  2629. u64 *snap_size, u64 *snap_features)
  2630. {
  2631. u64 snap_id;
  2632. u8 order;
  2633. int ret;
  2634. snap_id = rbd_dev->header.snapc->snaps[which];
  2635. ret = _rbd_dev_v2_snap_size(rbd_dev, snap_id, &order, snap_size);
  2636. if (ret)
  2637. return ERR_PTR(ret);
  2638. ret = _rbd_dev_v2_snap_features(rbd_dev, snap_id, snap_features);
  2639. if (ret)
  2640. return ERR_PTR(ret);
  2641. return rbd_dev_v2_snap_name(rbd_dev, which);
  2642. }
  2643. static char *rbd_dev_snap_info(struct rbd_device *rbd_dev, u32 which,
  2644. u64 *snap_size, u64 *snap_features)
  2645. {
  2646. if (rbd_dev->image_format == 1)
  2647. return rbd_dev_v1_snap_info(rbd_dev, which,
  2648. snap_size, snap_features);
  2649. if (rbd_dev->image_format == 2)
  2650. return rbd_dev_v2_snap_info(rbd_dev, which,
  2651. snap_size, snap_features);
  2652. return ERR_PTR(-EINVAL);
  2653. }
  2654. static int rbd_dev_v2_refresh(struct rbd_device *rbd_dev, u64 *hver)
  2655. {
  2656. int ret;
  2657. __u8 obj_order;
  2658. down_write(&rbd_dev->header_rwsem);
  2659. /* Grab old order first, to see if it changes */
  2660. obj_order = rbd_dev->header.obj_order,
  2661. ret = rbd_dev_v2_image_size(rbd_dev);
  2662. if (ret)
  2663. goto out;
  2664. if (rbd_dev->header.obj_order != obj_order) {
  2665. ret = -EIO;
  2666. goto out;
  2667. }
  2668. rbd_update_mapping_size(rbd_dev);
  2669. ret = rbd_dev_v2_snap_context(rbd_dev, hver);
  2670. dout("rbd_dev_v2_snap_context returned %d\n", ret);
  2671. if (ret)
  2672. goto out;
  2673. ret = rbd_dev_snaps_update(rbd_dev);
  2674. dout("rbd_dev_snaps_update returned %d\n", ret);
  2675. if (ret)
  2676. goto out;
  2677. ret = rbd_dev_snaps_register(rbd_dev);
  2678. dout("rbd_dev_snaps_register returned %d\n", ret);
  2679. out:
  2680. up_write(&rbd_dev->header_rwsem);
  2681. return ret;
  2682. }
  2683. /*
  2684. * Scan the rbd device's current snapshot list and compare it to the
  2685. * newly-received snapshot context. Remove any existing snapshots
  2686. * not present in the new snapshot context. Add a new snapshot for
  2687. * any snaphots in the snapshot context not in the current list.
  2688. * And verify there are no changes to snapshots we already know
  2689. * about.
  2690. *
  2691. * Assumes the snapshots in the snapshot context are sorted by
  2692. * snapshot id, highest id first. (Snapshots in the rbd_dev's list
  2693. * are also maintained in that order.)
  2694. */
  2695. static int rbd_dev_snaps_update(struct rbd_device *rbd_dev)
  2696. {
  2697. struct ceph_snap_context *snapc = rbd_dev->header.snapc;
  2698. const u32 snap_count = snapc->num_snaps;
  2699. struct list_head *head = &rbd_dev->snaps;
  2700. struct list_head *links = head->next;
  2701. u32 index = 0;
  2702. dout("%s: snap count is %u\n", __func__, (unsigned int) snap_count);
  2703. while (index < snap_count || links != head) {
  2704. u64 snap_id;
  2705. struct rbd_snap *snap;
  2706. char *snap_name;
  2707. u64 snap_size = 0;
  2708. u64 snap_features = 0;
  2709. snap_id = index < snap_count ? snapc->snaps[index]
  2710. : CEPH_NOSNAP;
  2711. snap = links != head ? list_entry(links, struct rbd_snap, node)
  2712. : NULL;
  2713. rbd_assert(!snap || snap->id != CEPH_NOSNAP);
  2714. if (snap_id == CEPH_NOSNAP || (snap && snap->id > snap_id)) {
  2715. struct list_head *next = links->next;
  2716. /* Existing snapshot not in the new snap context */
  2717. if (rbd_dev->spec->snap_id == snap->id)
  2718. atomic_set(&rbd_dev->exists, 0);
  2719. rbd_remove_snap_dev(snap);
  2720. dout("%ssnap id %llu has been removed\n",
  2721. rbd_dev->spec->snap_id == snap->id ?
  2722. "mapped " : "",
  2723. (unsigned long long) snap->id);
  2724. /* Done with this list entry; advance */
  2725. links = next;
  2726. continue;
  2727. }
  2728. snap_name = rbd_dev_snap_info(rbd_dev, index,
  2729. &snap_size, &snap_features);
  2730. if (IS_ERR(snap_name))
  2731. return PTR_ERR(snap_name);
  2732. dout("entry %u: snap_id = %llu\n", (unsigned int) snap_count,
  2733. (unsigned long long) snap_id);
  2734. if (!snap || (snap_id != CEPH_NOSNAP && snap->id < snap_id)) {
  2735. struct rbd_snap *new_snap;
  2736. /* We haven't seen this snapshot before */
  2737. new_snap = __rbd_add_snap_dev(rbd_dev, snap_name,
  2738. snap_id, snap_size, snap_features);
  2739. if (IS_ERR(new_snap)) {
  2740. int err = PTR_ERR(new_snap);
  2741. dout(" failed to add dev, error %d\n", err);
  2742. return err;
  2743. }
  2744. /* New goes before existing, or at end of list */
  2745. dout(" added dev%s\n", snap ? "" : " at end\n");
  2746. if (snap)
  2747. list_add_tail(&new_snap->node, &snap->node);
  2748. else
  2749. list_add_tail(&new_snap->node, head);
  2750. } else {
  2751. /* Already have this one */
  2752. dout(" already present\n");
  2753. rbd_assert(snap->size == snap_size);
  2754. rbd_assert(!strcmp(snap->name, snap_name));
  2755. rbd_assert(snap->features == snap_features);
  2756. /* Done with this list entry; advance */
  2757. links = links->next;
  2758. }
  2759. /* Advance to the next entry in the snapshot context */
  2760. index++;
  2761. }
  2762. dout("%s: done\n", __func__);
  2763. return 0;
  2764. }
  2765. /*
  2766. * Scan the list of snapshots and register the devices for any that
  2767. * have not already been registered.
  2768. */
  2769. static int rbd_dev_snaps_register(struct rbd_device *rbd_dev)
  2770. {
  2771. struct rbd_snap *snap;
  2772. int ret = 0;
  2773. dout("%s called\n", __func__);
  2774. if (WARN_ON(!device_is_registered(&rbd_dev->dev)))
  2775. return -EIO;
  2776. list_for_each_entry(snap, &rbd_dev->snaps, node) {
  2777. if (!rbd_snap_registered(snap)) {
  2778. ret = rbd_register_snap_dev(snap, &rbd_dev->dev);
  2779. if (ret < 0)
  2780. break;
  2781. }
  2782. }
  2783. dout("%s: returning %d\n", __func__, ret);
  2784. return ret;
  2785. }
  2786. static int rbd_bus_add_dev(struct rbd_device *rbd_dev)
  2787. {
  2788. struct device *dev;
  2789. int ret;
  2790. mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
  2791. dev = &rbd_dev->dev;
  2792. dev->bus = &rbd_bus_type;
  2793. dev->type = &rbd_device_type;
  2794. dev->parent = &rbd_root_dev;
  2795. dev->release = rbd_dev_release;
  2796. dev_set_name(dev, "%d", rbd_dev->dev_id);
  2797. ret = device_register(dev);
  2798. mutex_unlock(&ctl_mutex);
  2799. return ret;
  2800. }
  2801. static void rbd_bus_del_dev(struct rbd_device *rbd_dev)
  2802. {
  2803. device_unregister(&rbd_dev->dev);
  2804. }
  2805. static atomic64_t rbd_dev_id_max = ATOMIC64_INIT(0);
  2806. /*
  2807. * Get a unique rbd identifier for the given new rbd_dev, and add
  2808. * the rbd_dev to the global list. The minimum rbd id is 1.
  2809. */
  2810. static void rbd_dev_id_get(struct rbd_device *rbd_dev)
  2811. {
  2812. rbd_dev->dev_id = atomic64_inc_return(&rbd_dev_id_max);
  2813. spin_lock(&rbd_dev_list_lock);
  2814. list_add_tail(&rbd_dev->node, &rbd_dev_list);
  2815. spin_unlock(&rbd_dev_list_lock);
  2816. dout("rbd_dev %p given dev id %llu\n", rbd_dev,
  2817. (unsigned long long) rbd_dev->dev_id);
  2818. }
  2819. /*
  2820. * Remove an rbd_dev from the global list, and record that its
  2821. * identifier is no longer in use.
  2822. */
  2823. static void rbd_dev_id_put(struct rbd_device *rbd_dev)
  2824. {
  2825. struct list_head *tmp;
  2826. int rbd_id = rbd_dev->dev_id;
  2827. int max_id;
  2828. rbd_assert(rbd_id > 0);
  2829. dout("rbd_dev %p released dev id %llu\n", rbd_dev,
  2830. (unsigned long long) rbd_dev->dev_id);
  2831. spin_lock(&rbd_dev_list_lock);
  2832. list_del_init(&rbd_dev->node);
  2833. /*
  2834. * If the id being "put" is not the current maximum, there
  2835. * is nothing special we need to do.
  2836. */
  2837. if (rbd_id != atomic64_read(&rbd_dev_id_max)) {
  2838. spin_unlock(&rbd_dev_list_lock);
  2839. return;
  2840. }
  2841. /*
  2842. * We need to update the current maximum id. Search the
  2843. * list to find out what it is. We're more likely to find
  2844. * the maximum at the end, so search the list backward.
  2845. */
  2846. max_id = 0;
  2847. list_for_each_prev(tmp, &rbd_dev_list) {
  2848. struct rbd_device *rbd_dev;
  2849. rbd_dev = list_entry(tmp, struct rbd_device, node);
  2850. if (rbd_dev->dev_id > max_id)
  2851. max_id = rbd_dev->dev_id;
  2852. }
  2853. spin_unlock(&rbd_dev_list_lock);
  2854. /*
  2855. * The max id could have been updated by rbd_dev_id_get(), in
  2856. * which case it now accurately reflects the new maximum.
  2857. * Be careful not to overwrite the maximum value in that
  2858. * case.
  2859. */
  2860. atomic64_cmpxchg(&rbd_dev_id_max, rbd_id, max_id);
  2861. dout(" max dev id has been reset\n");
  2862. }
  2863. /*
  2864. * Skips over white space at *buf, and updates *buf to point to the
  2865. * first found non-space character (if any). Returns the length of
  2866. * the token (string of non-white space characters) found. Note
  2867. * that *buf must be terminated with '\0'.
  2868. */
  2869. static inline size_t next_token(const char **buf)
  2870. {
  2871. /*
  2872. * These are the characters that produce nonzero for
  2873. * isspace() in the "C" and "POSIX" locales.
  2874. */
  2875. const char *spaces = " \f\n\r\t\v";
  2876. *buf += strspn(*buf, spaces); /* Find start of token */
  2877. return strcspn(*buf, spaces); /* Return token length */
  2878. }
  2879. /*
  2880. * Finds the next token in *buf, and if the provided token buffer is
  2881. * big enough, copies the found token into it. The result, if
  2882. * copied, is guaranteed to be terminated with '\0'. Note that *buf
  2883. * must be terminated with '\0' on entry.
  2884. *
  2885. * Returns the length of the token found (not including the '\0').
  2886. * Return value will be 0 if no token is found, and it will be >=
  2887. * token_size if the token would not fit.
  2888. *
  2889. * The *buf pointer will be updated to point beyond the end of the
  2890. * found token. Note that this occurs even if the token buffer is
  2891. * too small to hold it.
  2892. */
  2893. static inline size_t copy_token(const char **buf,
  2894. char *token,
  2895. size_t token_size)
  2896. {
  2897. size_t len;
  2898. len = next_token(buf);
  2899. if (len < token_size) {
  2900. memcpy(token, *buf, len);
  2901. *(token + len) = '\0';
  2902. }
  2903. *buf += len;
  2904. return len;
  2905. }
  2906. /*
  2907. * Finds the next token in *buf, dynamically allocates a buffer big
  2908. * enough to hold a copy of it, and copies the token into the new
  2909. * buffer. The copy is guaranteed to be terminated with '\0'. Note
  2910. * that a duplicate buffer is created even for a zero-length token.
  2911. *
  2912. * Returns a pointer to the newly-allocated duplicate, or a null
  2913. * pointer if memory for the duplicate was not available. If
  2914. * the lenp argument is a non-null pointer, the length of the token
  2915. * (not including the '\0') is returned in *lenp.
  2916. *
  2917. * If successful, the *buf pointer will be updated to point beyond
  2918. * the end of the found token.
  2919. *
  2920. * Note: uses GFP_KERNEL for allocation.
  2921. */
  2922. static inline char *dup_token(const char **buf, size_t *lenp)
  2923. {
  2924. char *dup;
  2925. size_t len;
  2926. len = next_token(buf);
  2927. dup = kmemdup(*buf, len + 1, GFP_KERNEL);
  2928. if (!dup)
  2929. return NULL;
  2930. *(dup + len) = '\0';
  2931. *buf += len;
  2932. if (lenp)
  2933. *lenp = len;
  2934. return dup;
  2935. }
  2936. /*
  2937. * Parse the options provided for an "rbd add" (i.e., rbd image
  2938. * mapping) request. These arrive via a write to /sys/bus/rbd/add,
  2939. * and the data written is passed here via a NUL-terminated buffer.
  2940. * Returns 0 if successful or an error code otherwise.
  2941. *
  2942. * The information extracted from these options is recorded in
  2943. * the other parameters which return dynamically-allocated
  2944. * structures:
  2945. * ceph_opts
  2946. * The address of a pointer that will refer to a ceph options
  2947. * structure. Caller must release the returned pointer using
  2948. * ceph_destroy_options() when it is no longer needed.
  2949. * rbd_opts
  2950. * Address of an rbd options pointer. Fully initialized by
  2951. * this function; caller must release with kfree().
  2952. * spec
  2953. * Address of an rbd image specification pointer. Fully
  2954. * initialized by this function based on parsed options.
  2955. * Caller must release with rbd_spec_put().
  2956. *
  2957. * The options passed take this form:
  2958. * <mon_addrs> <options> <pool_name> <image_name> [<snap_id>]
  2959. * where:
  2960. * <mon_addrs>
  2961. * A comma-separated list of one or more monitor addresses.
  2962. * A monitor address is an ip address, optionally followed
  2963. * by a port number (separated by a colon).
  2964. * I.e.: ip1[:port1][,ip2[:port2]...]
  2965. * <options>
  2966. * A comma-separated list of ceph and/or rbd options.
  2967. * <pool_name>
  2968. * The name of the rados pool containing the rbd image.
  2969. * <image_name>
  2970. * The name of the image in that pool to map.
  2971. * <snap_id>
  2972. * An optional snapshot id. If provided, the mapping will
  2973. * present data from the image at the time that snapshot was
  2974. * created. The image head is used if no snapshot id is
  2975. * provided. Snapshot mappings are always read-only.
  2976. */
  2977. static int rbd_add_parse_args(const char *buf,
  2978. struct ceph_options **ceph_opts,
  2979. struct rbd_options **opts,
  2980. struct rbd_spec **rbd_spec)
  2981. {
  2982. size_t len;
  2983. char *options;
  2984. const char *mon_addrs;
  2985. size_t mon_addrs_size;
  2986. struct rbd_spec *spec = NULL;
  2987. struct rbd_options *rbd_opts = NULL;
  2988. struct ceph_options *copts;
  2989. int ret;
  2990. /* The first four tokens are required */
  2991. len = next_token(&buf);
  2992. if (!len) {
  2993. rbd_warn(NULL, "no monitor address(es) provided");
  2994. return -EINVAL;
  2995. }
  2996. mon_addrs = buf;
  2997. mon_addrs_size = len + 1;
  2998. buf += len;
  2999. ret = -EINVAL;
  3000. options = dup_token(&buf, NULL);
  3001. if (!options)
  3002. return -ENOMEM;
  3003. if (!*options) {
  3004. rbd_warn(NULL, "no options provided");
  3005. goto out_err;
  3006. }
  3007. spec = rbd_spec_alloc();
  3008. if (!spec)
  3009. goto out_mem;
  3010. spec->pool_name = dup_token(&buf, NULL);
  3011. if (!spec->pool_name)
  3012. goto out_mem;
  3013. if (!*spec->pool_name) {
  3014. rbd_warn(NULL, "no pool name provided");
  3015. goto out_err;
  3016. }
  3017. spec->image_name = dup_token(&buf, NULL);
  3018. if (!spec->image_name)
  3019. goto out_mem;
  3020. if (!*spec->image_name) {
  3021. rbd_warn(NULL, "no image name provided");
  3022. goto out_err;
  3023. }
  3024. /*
  3025. * Snapshot name is optional; default is to use "-"
  3026. * (indicating the head/no snapshot).
  3027. */
  3028. len = next_token(&buf);
  3029. if (!len) {
  3030. buf = RBD_SNAP_HEAD_NAME; /* No snapshot supplied */
  3031. len = sizeof (RBD_SNAP_HEAD_NAME) - 1;
  3032. } else if (len > RBD_MAX_SNAP_NAME_LEN) {
  3033. ret = -ENAMETOOLONG;
  3034. goto out_err;
  3035. }
  3036. spec->snap_name = kmemdup(buf, len + 1, GFP_KERNEL);
  3037. if (!spec->snap_name)
  3038. goto out_mem;
  3039. *(spec->snap_name + len) = '\0';
  3040. /* Initialize all rbd options to the defaults */
  3041. rbd_opts = kzalloc(sizeof (*rbd_opts), GFP_KERNEL);
  3042. if (!rbd_opts)
  3043. goto out_mem;
  3044. rbd_opts->read_only = RBD_READ_ONLY_DEFAULT;
  3045. copts = ceph_parse_options(options, mon_addrs,
  3046. mon_addrs + mon_addrs_size - 1,
  3047. parse_rbd_opts_token, rbd_opts);
  3048. if (IS_ERR(copts)) {
  3049. ret = PTR_ERR(copts);
  3050. goto out_err;
  3051. }
  3052. kfree(options);
  3053. *ceph_opts = copts;
  3054. *opts = rbd_opts;
  3055. *rbd_spec = spec;
  3056. return 0;
  3057. out_mem:
  3058. ret = -ENOMEM;
  3059. out_err:
  3060. kfree(rbd_opts);
  3061. rbd_spec_put(spec);
  3062. kfree(options);
  3063. return ret;
  3064. }
  3065. /*
  3066. * An rbd format 2 image has a unique identifier, distinct from the
  3067. * name given to it by the user. Internally, that identifier is
  3068. * what's used to specify the names of objects related to the image.
  3069. *
  3070. * A special "rbd id" object is used to map an rbd image name to its
  3071. * id. If that object doesn't exist, then there is no v2 rbd image
  3072. * with the supplied name.
  3073. *
  3074. * This function will record the given rbd_dev's image_id field if
  3075. * it can be determined, and in that case will return 0. If any
  3076. * errors occur a negative errno will be returned and the rbd_dev's
  3077. * image_id field will be unchanged (and should be NULL).
  3078. */
  3079. static int rbd_dev_image_id(struct rbd_device *rbd_dev)
  3080. {
  3081. int ret;
  3082. size_t size;
  3083. char *object_name;
  3084. void *response;
  3085. void *p;
  3086. /*
  3087. * When probing a parent image, the image id is already
  3088. * known (and the image name likely is not). There's no
  3089. * need to fetch the image id again in this case.
  3090. */
  3091. if (rbd_dev->spec->image_id)
  3092. return 0;
  3093. /*
  3094. * First, see if the format 2 image id file exists, and if
  3095. * so, get the image's persistent id from it.
  3096. */
  3097. size = sizeof (RBD_ID_PREFIX) + strlen(rbd_dev->spec->image_name);
  3098. object_name = kmalloc(size, GFP_NOIO);
  3099. if (!object_name)
  3100. return -ENOMEM;
  3101. sprintf(object_name, "%s%s", RBD_ID_PREFIX, rbd_dev->spec->image_name);
  3102. dout("rbd id object name is %s\n", object_name);
  3103. /* Response will be an encoded string, which includes a length */
  3104. size = sizeof (__le32) + RBD_IMAGE_ID_LEN_MAX;
  3105. response = kzalloc(size, GFP_NOIO);
  3106. if (!response) {
  3107. ret = -ENOMEM;
  3108. goto out;
  3109. }
  3110. ret = rbd_req_sync_exec(rbd_dev, object_name,
  3111. "rbd", "get_id",
  3112. NULL, 0,
  3113. response, RBD_IMAGE_ID_LEN_MAX, NULL);
  3114. dout("%s: rbd_req_sync_exec returned %d\n", __func__, ret);
  3115. if (ret < 0)
  3116. goto out;
  3117. ret = 0; /* rbd_req_sync_exec() can return positive */
  3118. p = response;
  3119. rbd_dev->spec->image_id = ceph_extract_encoded_string(&p,
  3120. p + RBD_IMAGE_ID_LEN_MAX,
  3121. NULL, GFP_NOIO);
  3122. if (IS_ERR(rbd_dev->spec->image_id)) {
  3123. ret = PTR_ERR(rbd_dev->spec->image_id);
  3124. rbd_dev->spec->image_id = NULL;
  3125. } else {
  3126. dout("image_id is %s\n", rbd_dev->spec->image_id);
  3127. }
  3128. out:
  3129. kfree(response);
  3130. kfree(object_name);
  3131. return ret;
  3132. }
  3133. static int rbd_dev_v1_probe(struct rbd_device *rbd_dev)
  3134. {
  3135. int ret;
  3136. size_t size;
  3137. /* Version 1 images have no id; empty string is used */
  3138. rbd_dev->spec->image_id = kstrdup("", GFP_KERNEL);
  3139. if (!rbd_dev->spec->image_id)
  3140. return -ENOMEM;
  3141. /* Record the header object name for this rbd image. */
  3142. size = strlen(rbd_dev->spec->image_name) + sizeof (RBD_SUFFIX);
  3143. rbd_dev->header_name = kmalloc(size, GFP_KERNEL);
  3144. if (!rbd_dev->header_name) {
  3145. ret = -ENOMEM;
  3146. goto out_err;
  3147. }
  3148. sprintf(rbd_dev->header_name, "%s%s",
  3149. rbd_dev->spec->image_name, RBD_SUFFIX);
  3150. /* Populate rbd image metadata */
  3151. ret = rbd_read_header(rbd_dev, &rbd_dev->header);
  3152. if (ret < 0)
  3153. goto out_err;
  3154. /* Version 1 images have no parent (no layering) */
  3155. rbd_dev->parent_spec = NULL;
  3156. rbd_dev->parent_overlap = 0;
  3157. rbd_dev->image_format = 1;
  3158. dout("discovered version 1 image, header name is %s\n",
  3159. rbd_dev->header_name);
  3160. return 0;
  3161. out_err:
  3162. kfree(rbd_dev->header_name);
  3163. rbd_dev->header_name = NULL;
  3164. kfree(rbd_dev->spec->image_id);
  3165. rbd_dev->spec->image_id = NULL;
  3166. return ret;
  3167. }
  3168. static int rbd_dev_v2_probe(struct rbd_device *rbd_dev)
  3169. {
  3170. size_t size;
  3171. int ret;
  3172. u64 ver = 0;
  3173. /*
  3174. * Image id was filled in by the caller. Record the header
  3175. * object name for this rbd image.
  3176. */
  3177. size = sizeof (RBD_HEADER_PREFIX) + strlen(rbd_dev->spec->image_id);
  3178. rbd_dev->header_name = kmalloc(size, GFP_KERNEL);
  3179. if (!rbd_dev->header_name)
  3180. return -ENOMEM;
  3181. sprintf(rbd_dev->header_name, "%s%s",
  3182. RBD_HEADER_PREFIX, rbd_dev->spec->image_id);
  3183. /* Get the size and object order for the image */
  3184. ret = rbd_dev_v2_image_size(rbd_dev);
  3185. if (ret < 0)
  3186. goto out_err;
  3187. /* Get the object prefix (a.k.a. block_name) for the image */
  3188. ret = rbd_dev_v2_object_prefix(rbd_dev);
  3189. if (ret < 0)
  3190. goto out_err;
  3191. /* Get the and check features for the image */
  3192. ret = rbd_dev_v2_features(rbd_dev);
  3193. if (ret < 0)
  3194. goto out_err;
  3195. /* If the image supports layering, get the parent info */
  3196. if (rbd_dev->header.features & RBD_FEATURE_LAYERING) {
  3197. ret = rbd_dev_v2_parent_info(rbd_dev);
  3198. if (ret < 0)
  3199. goto out_err;
  3200. }
  3201. /* crypto and compression type aren't (yet) supported for v2 images */
  3202. rbd_dev->header.crypt_type = 0;
  3203. rbd_dev->header.comp_type = 0;
  3204. /* Get the snapshot context, plus the header version */
  3205. ret = rbd_dev_v2_snap_context(rbd_dev, &ver);
  3206. if (ret)
  3207. goto out_err;
  3208. rbd_dev->header.obj_version = ver;
  3209. rbd_dev->image_format = 2;
  3210. dout("discovered version 2 image, header name is %s\n",
  3211. rbd_dev->header_name);
  3212. return 0;
  3213. out_err:
  3214. rbd_dev->parent_overlap = 0;
  3215. rbd_spec_put(rbd_dev->parent_spec);
  3216. rbd_dev->parent_spec = NULL;
  3217. kfree(rbd_dev->header_name);
  3218. rbd_dev->header_name = NULL;
  3219. kfree(rbd_dev->header.object_prefix);
  3220. rbd_dev->header.object_prefix = NULL;
  3221. return ret;
  3222. }
  3223. static int rbd_dev_probe_finish(struct rbd_device *rbd_dev)
  3224. {
  3225. int ret;
  3226. /* no need to lock here, as rbd_dev is not registered yet */
  3227. ret = rbd_dev_snaps_update(rbd_dev);
  3228. if (ret)
  3229. return ret;
  3230. ret = rbd_dev_probe_update_spec(rbd_dev);
  3231. if (ret)
  3232. goto err_out_snaps;
  3233. ret = rbd_dev_set_mapping(rbd_dev);
  3234. if (ret)
  3235. goto err_out_snaps;
  3236. /* generate unique id: find highest unique id, add one */
  3237. rbd_dev_id_get(rbd_dev);
  3238. /* Fill in the device name, now that we have its id. */
  3239. BUILD_BUG_ON(DEV_NAME_LEN
  3240. < sizeof (RBD_DRV_NAME) + MAX_INT_FORMAT_WIDTH);
  3241. sprintf(rbd_dev->name, "%s%d", RBD_DRV_NAME, rbd_dev->dev_id);
  3242. /* Get our block major device number. */
  3243. ret = register_blkdev(0, rbd_dev->name);
  3244. if (ret < 0)
  3245. goto err_out_id;
  3246. rbd_dev->major = ret;
  3247. /* Set up the blkdev mapping. */
  3248. ret = rbd_init_disk(rbd_dev);
  3249. if (ret)
  3250. goto err_out_blkdev;
  3251. ret = rbd_bus_add_dev(rbd_dev);
  3252. if (ret)
  3253. goto err_out_disk;
  3254. /*
  3255. * At this point cleanup in the event of an error is the job
  3256. * of the sysfs code (initiated by rbd_bus_del_dev()).
  3257. */
  3258. down_write(&rbd_dev->header_rwsem);
  3259. ret = rbd_dev_snaps_register(rbd_dev);
  3260. up_write(&rbd_dev->header_rwsem);
  3261. if (ret)
  3262. goto err_out_bus;
  3263. ret = rbd_dev_header_watch_sync(rbd_dev, 1);
  3264. if (ret)
  3265. goto err_out_bus;
  3266. /* Everything's ready. Announce the disk to the world. */
  3267. add_disk(rbd_dev->disk);
  3268. pr_info("%s: added with size 0x%llx\n", rbd_dev->disk->disk_name,
  3269. (unsigned long long) rbd_dev->mapping.size);
  3270. return ret;
  3271. err_out_bus:
  3272. /* this will also clean up rest of rbd_dev stuff */
  3273. rbd_bus_del_dev(rbd_dev);
  3274. return ret;
  3275. err_out_disk:
  3276. rbd_free_disk(rbd_dev);
  3277. err_out_blkdev:
  3278. unregister_blkdev(rbd_dev->major, rbd_dev->name);
  3279. err_out_id:
  3280. rbd_dev_id_put(rbd_dev);
  3281. err_out_snaps:
  3282. rbd_remove_all_snaps(rbd_dev);
  3283. return ret;
  3284. }
  3285. /*
  3286. * Probe for the existence of the header object for the given rbd
  3287. * device. For format 2 images this includes determining the image
  3288. * id.
  3289. */
  3290. static int rbd_dev_probe(struct rbd_device *rbd_dev)
  3291. {
  3292. int ret;
  3293. /*
  3294. * Get the id from the image id object. If it's not a
  3295. * format 2 image, we'll get ENOENT back, and we'll assume
  3296. * it's a format 1 image.
  3297. */
  3298. ret = rbd_dev_image_id(rbd_dev);
  3299. if (ret)
  3300. ret = rbd_dev_v1_probe(rbd_dev);
  3301. else
  3302. ret = rbd_dev_v2_probe(rbd_dev);
  3303. if (ret) {
  3304. dout("probe failed, returning %d\n", ret);
  3305. return ret;
  3306. }
  3307. ret = rbd_dev_probe_finish(rbd_dev);
  3308. if (ret)
  3309. rbd_header_free(&rbd_dev->header);
  3310. return ret;
  3311. }
  3312. static ssize_t rbd_add(struct bus_type *bus,
  3313. const char *buf,
  3314. size_t count)
  3315. {
  3316. struct rbd_device *rbd_dev = NULL;
  3317. struct ceph_options *ceph_opts = NULL;
  3318. struct rbd_options *rbd_opts = NULL;
  3319. struct rbd_spec *spec = NULL;
  3320. struct rbd_client *rbdc;
  3321. struct ceph_osd_client *osdc;
  3322. int rc = -ENOMEM;
  3323. if (!try_module_get(THIS_MODULE))
  3324. return -ENODEV;
  3325. /* parse add command */
  3326. rc = rbd_add_parse_args(buf, &ceph_opts, &rbd_opts, &spec);
  3327. if (rc < 0)
  3328. goto err_out_module;
  3329. rbdc = rbd_get_client(ceph_opts);
  3330. if (IS_ERR(rbdc)) {
  3331. rc = PTR_ERR(rbdc);
  3332. goto err_out_args;
  3333. }
  3334. ceph_opts = NULL; /* rbd_dev client now owns this */
  3335. /* pick the pool */
  3336. osdc = &rbdc->client->osdc;
  3337. rc = ceph_pg_poolid_by_name(osdc->osdmap, spec->pool_name);
  3338. if (rc < 0)
  3339. goto err_out_client;
  3340. spec->pool_id = (u64) rc;
  3341. /* The ceph file layout needs to fit pool id in 32 bits */
  3342. if (WARN_ON(spec->pool_id > (u64) U32_MAX)) {
  3343. rc = -EIO;
  3344. goto err_out_client;
  3345. }
  3346. rbd_dev = rbd_dev_create(rbdc, spec);
  3347. if (!rbd_dev)
  3348. goto err_out_client;
  3349. rbdc = NULL; /* rbd_dev now owns this */
  3350. spec = NULL; /* rbd_dev now owns this */
  3351. rbd_dev->mapping.read_only = rbd_opts->read_only;
  3352. kfree(rbd_opts);
  3353. rbd_opts = NULL; /* done with this */
  3354. rc = rbd_dev_probe(rbd_dev);
  3355. if (rc < 0)
  3356. goto err_out_rbd_dev;
  3357. return count;
  3358. err_out_rbd_dev:
  3359. rbd_dev_destroy(rbd_dev);
  3360. err_out_client:
  3361. rbd_put_client(rbdc);
  3362. err_out_args:
  3363. if (ceph_opts)
  3364. ceph_destroy_options(ceph_opts);
  3365. kfree(rbd_opts);
  3366. rbd_spec_put(spec);
  3367. err_out_module:
  3368. module_put(THIS_MODULE);
  3369. dout("Error adding device %s\n", buf);
  3370. return (ssize_t) rc;
  3371. }
  3372. static struct rbd_device *__rbd_get_dev(unsigned long dev_id)
  3373. {
  3374. struct list_head *tmp;
  3375. struct rbd_device *rbd_dev;
  3376. spin_lock(&rbd_dev_list_lock);
  3377. list_for_each(tmp, &rbd_dev_list) {
  3378. rbd_dev = list_entry(tmp, struct rbd_device, node);
  3379. if (rbd_dev->dev_id == dev_id) {
  3380. spin_unlock(&rbd_dev_list_lock);
  3381. return rbd_dev;
  3382. }
  3383. }
  3384. spin_unlock(&rbd_dev_list_lock);
  3385. return NULL;
  3386. }
  3387. static void rbd_dev_release(struct device *dev)
  3388. {
  3389. struct rbd_device *rbd_dev = dev_to_rbd_dev(dev);
  3390. if (rbd_dev->watch_request) {
  3391. struct ceph_client *client = rbd_dev->rbd_client->client;
  3392. ceph_osdc_unregister_linger_request(&client->osdc,
  3393. rbd_dev->watch_request);
  3394. }
  3395. if (rbd_dev->watch_event)
  3396. rbd_dev_header_watch_sync(rbd_dev, 0);
  3397. /* clean up and free blkdev */
  3398. rbd_free_disk(rbd_dev);
  3399. unregister_blkdev(rbd_dev->major, rbd_dev->name);
  3400. /* release allocated disk header fields */
  3401. rbd_header_free(&rbd_dev->header);
  3402. /* done with the id, and with the rbd_dev */
  3403. rbd_dev_id_put(rbd_dev);
  3404. rbd_assert(rbd_dev->rbd_client != NULL);
  3405. rbd_dev_destroy(rbd_dev);
  3406. /* release module ref */
  3407. module_put(THIS_MODULE);
  3408. }
  3409. static ssize_t rbd_remove(struct bus_type *bus,
  3410. const char *buf,
  3411. size_t count)
  3412. {
  3413. struct rbd_device *rbd_dev = NULL;
  3414. int target_id, rc;
  3415. unsigned long ul;
  3416. int ret = count;
  3417. rc = strict_strtoul(buf, 10, &ul);
  3418. if (rc)
  3419. return rc;
  3420. /* convert to int; abort if we lost anything in the conversion */
  3421. target_id = (int) ul;
  3422. if (target_id != ul)
  3423. return -EINVAL;
  3424. mutex_lock_nested(&ctl_mutex, SINGLE_DEPTH_NESTING);
  3425. rbd_dev = __rbd_get_dev(target_id);
  3426. if (!rbd_dev) {
  3427. ret = -ENOENT;
  3428. goto done;
  3429. }
  3430. if (rbd_dev->open_count) {
  3431. ret = -EBUSY;
  3432. goto done;
  3433. }
  3434. rbd_remove_all_snaps(rbd_dev);
  3435. rbd_bus_del_dev(rbd_dev);
  3436. done:
  3437. mutex_unlock(&ctl_mutex);
  3438. return ret;
  3439. }
  3440. /*
  3441. * create control files in sysfs
  3442. * /sys/bus/rbd/...
  3443. */
  3444. static int rbd_sysfs_init(void)
  3445. {
  3446. int ret;
  3447. ret = device_register(&rbd_root_dev);
  3448. if (ret < 0)
  3449. return ret;
  3450. ret = bus_register(&rbd_bus_type);
  3451. if (ret < 0)
  3452. device_unregister(&rbd_root_dev);
  3453. return ret;
  3454. }
  3455. static void rbd_sysfs_cleanup(void)
  3456. {
  3457. bus_unregister(&rbd_bus_type);
  3458. device_unregister(&rbd_root_dev);
  3459. }
  3460. int __init rbd_init(void)
  3461. {
  3462. int rc;
  3463. rc = rbd_sysfs_init();
  3464. if (rc)
  3465. return rc;
  3466. pr_info("loaded " RBD_DRV_NAME_LONG "\n");
  3467. return 0;
  3468. }
  3469. void __exit rbd_exit(void)
  3470. {
  3471. rbd_sysfs_cleanup();
  3472. }
  3473. module_init(rbd_init);
  3474. module_exit(rbd_exit);
  3475. MODULE_AUTHOR("Sage Weil <sage@newdream.net>");
  3476. MODULE_AUTHOR("Yehuda Sadeh <yehuda@hq.newdream.net>");
  3477. MODULE_DESCRIPTION("rados block device");
  3478. /* following authorship retained from original osdblk.c */
  3479. MODULE_AUTHOR("Jeff Garzik <jeff@garzik.org>");
  3480. MODULE_LICENSE("GPL");