osd_client.c 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579
  1. #include <linux/ceph/ceph_debug.h>
  2. #include <linux/module.h>
  3. #include <linux/err.h>
  4. #include <linux/highmem.h>
  5. #include <linux/mm.h>
  6. #include <linux/pagemap.h>
  7. #include <linux/slab.h>
  8. #include <linux/uaccess.h>
  9. #ifdef CONFIG_BLOCK
  10. #include <linux/bio.h>
  11. #endif
  12. #include <linux/ceph/libceph.h>
  13. #include <linux/ceph/osd_client.h>
  14. #include <linux/ceph/messenger.h>
  15. #include <linux/ceph/decode.h>
  16. #include <linux/ceph/auth.h>
  17. #include <linux/ceph/pagelist.h>
  18. #define OSD_OP_FRONT_LEN 4096
  19. #define OSD_OPREPLY_FRONT_LEN 512
  20. static const struct ceph_connection_operations osd_con_ops;
  21. static void __send_queued(struct ceph_osd_client *osdc);
  22. static int __reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd);
  23. static void __register_request(struct ceph_osd_client *osdc,
  24. struct ceph_osd_request *req);
  25. static void __unregister_linger_request(struct ceph_osd_client *osdc,
  26. struct ceph_osd_request *req);
  27. static void __send_request(struct ceph_osd_client *osdc,
  28. struct ceph_osd_request *req);
  29. /*
  30. * Implement client access to distributed object storage cluster.
  31. *
  32. * All data objects are stored within a cluster/cloud of OSDs, or
  33. * "object storage devices." (Note that Ceph OSDs have _nothing_ to
  34. * do with the T10 OSD extensions to SCSI.) Ceph OSDs are simply
  35. * remote daemons serving up and coordinating consistent and safe
  36. * access to storage.
  37. *
  38. * Cluster membership and the mapping of data objects onto storage devices
  39. * are described by the osd map.
  40. *
  41. * We keep track of pending OSD requests (read, write), resubmit
  42. * requests to different OSDs when the cluster topology/data layout
  43. * change, or retry the affected requests when the communications
  44. * channel with an OSD is reset.
  45. */
  46. /*
  47. * calculate the mapping of a file extent onto an object, and fill out the
  48. * request accordingly. shorten extent as necessary if it crosses an
  49. * object boundary.
  50. *
  51. * fill osd op in request message.
  52. */
  53. static int calc_layout(struct ceph_file_layout *layout, u64 off, u64 *plen,
  54. u64 *objnum, u64 *objoff, u64 *objlen)
  55. {
  56. u64 orig_len = *plen;
  57. int r;
  58. /* object extent? */
  59. r = ceph_calc_file_object_mapping(layout, off, orig_len, objnum,
  60. objoff, objlen);
  61. if (r < 0)
  62. return r;
  63. if (*objlen < orig_len) {
  64. *plen = *objlen;
  65. dout(" skipping last %llu, final file extent %llu~%llu\n",
  66. orig_len - *plen, off, *plen);
  67. }
  68. dout("calc_layout objnum=%llx %llu~%llu\n", *objnum, *objoff, *objlen);
  69. return 0;
  70. }
  71. static void ceph_osd_data_init(struct ceph_osd_data *osd_data)
  72. {
  73. memset(osd_data, 0, sizeof (*osd_data));
  74. osd_data->type = CEPH_OSD_DATA_TYPE_NONE;
  75. }
  76. static void ceph_osd_data_pages_init(struct ceph_osd_data *osd_data,
  77. struct page **pages, u64 length, u32 alignment,
  78. bool pages_from_pool, bool own_pages)
  79. {
  80. osd_data->type = CEPH_OSD_DATA_TYPE_PAGES;
  81. osd_data->pages = pages;
  82. osd_data->length = length;
  83. osd_data->alignment = alignment;
  84. osd_data->pages_from_pool = pages_from_pool;
  85. osd_data->own_pages = own_pages;
  86. }
  87. static void ceph_osd_data_pagelist_init(struct ceph_osd_data *osd_data,
  88. struct ceph_pagelist *pagelist)
  89. {
  90. osd_data->type = CEPH_OSD_DATA_TYPE_PAGELIST;
  91. osd_data->pagelist = pagelist;
  92. }
  93. #ifdef CONFIG_BLOCK
  94. static void ceph_osd_data_bio_init(struct ceph_osd_data *osd_data,
  95. struct bio *bio, size_t bio_length)
  96. {
  97. osd_data->type = CEPH_OSD_DATA_TYPE_BIO;
  98. osd_data->bio = bio;
  99. osd_data->bio_length = bio_length;
  100. }
  101. #endif /* CONFIG_BLOCK */
  102. #define osd_req_op_data(oreq, whch, typ, fld) \
  103. ({ \
  104. BUG_ON(whch >= (oreq)->r_num_ops); \
  105. &(oreq)->r_ops[whch].typ.fld; \
  106. })
  107. static struct ceph_osd_data *
  108. osd_req_op_raw_data_in(struct ceph_osd_request *osd_req, unsigned int which)
  109. {
  110. BUG_ON(which >= osd_req->r_num_ops);
  111. return &osd_req->r_ops[which].raw_data_in;
  112. }
  113. struct ceph_osd_data *
  114. osd_req_op_extent_osd_data(struct ceph_osd_request *osd_req,
  115. unsigned int which)
  116. {
  117. return osd_req_op_data(osd_req, which, extent, osd_data);
  118. }
  119. EXPORT_SYMBOL(osd_req_op_extent_osd_data);
  120. struct ceph_osd_data *
  121. osd_req_op_cls_response_data(struct ceph_osd_request *osd_req,
  122. unsigned int which)
  123. {
  124. return osd_req_op_data(osd_req, which, cls, response_data);
  125. }
  126. EXPORT_SYMBOL(osd_req_op_cls_response_data); /* ??? */
  127. void osd_req_op_raw_data_in_pages(struct ceph_osd_request *osd_req,
  128. unsigned int which, struct page **pages,
  129. u64 length, u32 alignment,
  130. bool pages_from_pool, bool own_pages)
  131. {
  132. struct ceph_osd_data *osd_data;
  133. osd_data = osd_req_op_raw_data_in(osd_req, which);
  134. ceph_osd_data_pages_init(osd_data, pages, length, alignment,
  135. pages_from_pool, own_pages);
  136. }
  137. EXPORT_SYMBOL(osd_req_op_raw_data_in_pages);
  138. void osd_req_op_extent_osd_data_pages(struct ceph_osd_request *osd_req,
  139. unsigned int which, struct page **pages,
  140. u64 length, u32 alignment,
  141. bool pages_from_pool, bool own_pages)
  142. {
  143. struct ceph_osd_data *osd_data;
  144. osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
  145. ceph_osd_data_pages_init(osd_data, pages, length, alignment,
  146. pages_from_pool, own_pages);
  147. }
  148. EXPORT_SYMBOL(osd_req_op_extent_osd_data_pages);
  149. void osd_req_op_extent_osd_data_pagelist(struct ceph_osd_request *osd_req,
  150. unsigned int which, struct ceph_pagelist *pagelist)
  151. {
  152. struct ceph_osd_data *osd_data;
  153. osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
  154. ceph_osd_data_pagelist_init(osd_data, pagelist);
  155. }
  156. EXPORT_SYMBOL(osd_req_op_extent_osd_data_pagelist);
  157. #ifdef CONFIG_BLOCK
  158. void osd_req_op_extent_osd_data_bio(struct ceph_osd_request *osd_req,
  159. unsigned int which, struct bio *bio, size_t bio_length)
  160. {
  161. struct ceph_osd_data *osd_data;
  162. osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
  163. ceph_osd_data_bio_init(osd_data, bio, bio_length);
  164. }
  165. EXPORT_SYMBOL(osd_req_op_extent_osd_data_bio);
  166. #endif /* CONFIG_BLOCK */
  167. static void osd_req_op_cls_request_info_pagelist(
  168. struct ceph_osd_request *osd_req,
  169. unsigned int which, struct ceph_pagelist *pagelist)
  170. {
  171. struct ceph_osd_data *osd_data;
  172. osd_data = osd_req_op_data(osd_req, which, cls, request_info);
  173. ceph_osd_data_pagelist_init(osd_data, pagelist);
  174. }
  175. void osd_req_op_cls_request_data_pagelist(
  176. struct ceph_osd_request *osd_req,
  177. unsigned int which, struct ceph_pagelist *pagelist)
  178. {
  179. struct ceph_osd_data *osd_data;
  180. osd_data = osd_req_op_data(osd_req, which, cls, request_data);
  181. ceph_osd_data_pagelist_init(osd_data, pagelist);
  182. }
  183. EXPORT_SYMBOL(osd_req_op_cls_request_data_pagelist);
  184. void osd_req_op_cls_request_data_pages(struct ceph_osd_request *osd_req,
  185. unsigned int which, struct page **pages, u64 length,
  186. u32 alignment, bool pages_from_pool, bool own_pages)
  187. {
  188. struct ceph_osd_data *osd_data;
  189. osd_data = osd_req_op_data(osd_req, which, cls, request_data);
  190. ceph_osd_data_pages_init(osd_data, pages, length, alignment,
  191. pages_from_pool, own_pages);
  192. }
  193. EXPORT_SYMBOL(osd_req_op_cls_request_data_pages);
  194. void osd_req_op_cls_response_data_pages(struct ceph_osd_request *osd_req,
  195. unsigned int which, struct page **pages, u64 length,
  196. u32 alignment, bool pages_from_pool, bool own_pages)
  197. {
  198. struct ceph_osd_data *osd_data;
  199. osd_data = osd_req_op_data(osd_req, which, cls, response_data);
  200. ceph_osd_data_pages_init(osd_data, pages, length, alignment,
  201. pages_from_pool, own_pages);
  202. }
  203. EXPORT_SYMBOL(osd_req_op_cls_response_data_pages);
  204. static u64 ceph_osd_data_length(struct ceph_osd_data *osd_data)
  205. {
  206. switch (osd_data->type) {
  207. case CEPH_OSD_DATA_TYPE_NONE:
  208. return 0;
  209. case CEPH_OSD_DATA_TYPE_PAGES:
  210. return osd_data->length;
  211. case CEPH_OSD_DATA_TYPE_PAGELIST:
  212. return (u64)osd_data->pagelist->length;
  213. #ifdef CONFIG_BLOCK
  214. case CEPH_OSD_DATA_TYPE_BIO:
  215. return (u64)osd_data->bio_length;
  216. #endif /* CONFIG_BLOCK */
  217. default:
  218. WARN(true, "unrecognized data type %d\n", (int)osd_data->type);
  219. return 0;
  220. }
  221. }
  222. static void ceph_osd_data_release(struct ceph_osd_data *osd_data)
  223. {
  224. if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES && osd_data->own_pages) {
  225. int num_pages;
  226. num_pages = calc_pages_for((u64)osd_data->alignment,
  227. (u64)osd_data->length);
  228. ceph_release_page_vector(osd_data->pages, num_pages);
  229. }
  230. ceph_osd_data_init(osd_data);
  231. }
  232. static void osd_req_op_data_release(struct ceph_osd_request *osd_req,
  233. unsigned int which)
  234. {
  235. struct ceph_osd_req_op *op;
  236. BUG_ON(which >= osd_req->r_num_ops);
  237. op = &osd_req->r_ops[which];
  238. switch (op->op) {
  239. case CEPH_OSD_OP_READ:
  240. case CEPH_OSD_OP_WRITE:
  241. ceph_osd_data_release(&op->extent.osd_data);
  242. break;
  243. case CEPH_OSD_OP_CALL:
  244. ceph_osd_data_release(&op->cls.request_info);
  245. ceph_osd_data_release(&op->cls.request_data);
  246. ceph_osd_data_release(&op->cls.response_data);
  247. break;
  248. default:
  249. break;
  250. }
  251. }
  252. /*
  253. * requests
  254. */
  255. void ceph_osdc_release_request(struct kref *kref)
  256. {
  257. struct ceph_osd_request *req;
  258. unsigned int which;
  259. req = container_of(kref, struct ceph_osd_request, r_kref);
  260. if (req->r_request)
  261. ceph_msg_put(req->r_request);
  262. if (req->r_reply) {
  263. ceph_msg_revoke_incoming(req->r_reply);
  264. ceph_msg_put(req->r_reply);
  265. }
  266. for (which = 0; which < req->r_num_ops; which++)
  267. osd_req_op_data_release(req, which);
  268. ceph_put_snap_context(req->r_snapc);
  269. if (req->r_mempool)
  270. mempool_free(req, req->r_osdc->req_mempool);
  271. else
  272. kfree(req);
  273. }
  274. EXPORT_SYMBOL(ceph_osdc_release_request);
  275. struct ceph_osd_request *ceph_osdc_alloc_request(struct ceph_osd_client *osdc,
  276. struct ceph_snap_context *snapc,
  277. unsigned int num_ops,
  278. bool use_mempool,
  279. gfp_t gfp_flags)
  280. {
  281. struct ceph_osd_request *req;
  282. struct ceph_msg *msg;
  283. size_t msg_size;
  284. BUILD_BUG_ON(CEPH_OSD_MAX_OP > U16_MAX);
  285. BUG_ON(num_ops > CEPH_OSD_MAX_OP);
  286. msg_size = 4 + 4 + 8 + 8 + 4+8;
  287. msg_size += 2 + 4 + 8 + 4 + 4; /* oloc */
  288. msg_size += 1 + 8 + 4 + 4; /* pg_t */
  289. msg_size += 4 + MAX_OBJ_NAME_SIZE;
  290. msg_size += 2 + num_ops*sizeof(struct ceph_osd_op);
  291. msg_size += 8; /* snapid */
  292. msg_size += 8; /* snap_seq */
  293. msg_size += 8 * (snapc ? snapc->num_snaps : 0); /* snaps */
  294. msg_size += 4;
  295. if (use_mempool) {
  296. req = mempool_alloc(osdc->req_mempool, gfp_flags);
  297. memset(req, 0, sizeof(*req));
  298. } else {
  299. req = kzalloc(sizeof(*req), gfp_flags);
  300. }
  301. if (req == NULL)
  302. return NULL;
  303. req->r_osdc = osdc;
  304. req->r_mempool = use_mempool;
  305. req->r_num_ops = num_ops;
  306. kref_init(&req->r_kref);
  307. init_completion(&req->r_completion);
  308. init_completion(&req->r_safe_completion);
  309. RB_CLEAR_NODE(&req->r_node);
  310. INIT_LIST_HEAD(&req->r_unsafe_item);
  311. INIT_LIST_HEAD(&req->r_linger_item);
  312. INIT_LIST_HEAD(&req->r_linger_osd);
  313. INIT_LIST_HEAD(&req->r_req_lru_item);
  314. INIT_LIST_HEAD(&req->r_osd_item);
  315. /* create reply message */
  316. if (use_mempool)
  317. msg = ceph_msgpool_get(&osdc->msgpool_op_reply, 0);
  318. else
  319. msg = ceph_msg_new(CEPH_MSG_OSD_OPREPLY,
  320. OSD_OPREPLY_FRONT_LEN, gfp_flags, true);
  321. if (!msg) {
  322. ceph_osdc_put_request(req);
  323. return NULL;
  324. }
  325. req->r_reply = msg;
  326. /* create request message; allow space for oid */
  327. if (use_mempool)
  328. msg = ceph_msgpool_get(&osdc->msgpool_op, 0);
  329. else
  330. msg = ceph_msg_new(CEPH_MSG_OSD_OP, msg_size, gfp_flags, true);
  331. if (!msg) {
  332. ceph_osdc_put_request(req);
  333. return NULL;
  334. }
  335. memset(msg->front.iov_base, 0, msg->front.iov_len);
  336. req->r_request = msg;
  337. return req;
  338. }
  339. EXPORT_SYMBOL(ceph_osdc_alloc_request);
  340. static bool osd_req_opcode_valid(u16 opcode)
  341. {
  342. switch (opcode) {
  343. case CEPH_OSD_OP_READ:
  344. case CEPH_OSD_OP_STAT:
  345. case CEPH_OSD_OP_MAPEXT:
  346. case CEPH_OSD_OP_MASKTRUNC:
  347. case CEPH_OSD_OP_SPARSE_READ:
  348. case CEPH_OSD_OP_NOTIFY:
  349. case CEPH_OSD_OP_NOTIFY_ACK:
  350. case CEPH_OSD_OP_ASSERT_VER:
  351. case CEPH_OSD_OP_WRITE:
  352. case CEPH_OSD_OP_WRITEFULL:
  353. case CEPH_OSD_OP_TRUNCATE:
  354. case CEPH_OSD_OP_ZERO:
  355. case CEPH_OSD_OP_DELETE:
  356. case CEPH_OSD_OP_APPEND:
  357. case CEPH_OSD_OP_STARTSYNC:
  358. case CEPH_OSD_OP_SETTRUNC:
  359. case CEPH_OSD_OP_TRIMTRUNC:
  360. case CEPH_OSD_OP_TMAPUP:
  361. case CEPH_OSD_OP_TMAPPUT:
  362. case CEPH_OSD_OP_TMAPGET:
  363. case CEPH_OSD_OP_CREATE:
  364. case CEPH_OSD_OP_ROLLBACK:
  365. case CEPH_OSD_OP_WATCH:
  366. case CEPH_OSD_OP_OMAPGETKEYS:
  367. case CEPH_OSD_OP_OMAPGETVALS:
  368. case CEPH_OSD_OP_OMAPGETHEADER:
  369. case CEPH_OSD_OP_OMAPGETVALSBYKEYS:
  370. case CEPH_OSD_OP_OMAPSETVALS:
  371. case CEPH_OSD_OP_OMAPSETHEADER:
  372. case CEPH_OSD_OP_OMAPCLEAR:
  373. case CEPH_OSD_OP_OMAPRMKEYS:
  374. case CEPH_OSD_OP_OMAP_CMP:
  375. case CEPH_OSD_OP_CLONERANGE:
  376. case CEPH_OSD_OP_ASSERT_SRC_VERSION:
  377. case CEPH_OSD_OP_SRC_CMPXATTR:
  378. case CEPH_OSD_OP_GETXATTR:
  379. case CEPH_OSD_OP_GETXATTRS:
  380. case CEPH_OSD_OP_CMPXATTR:
  381. case CEPH_OSD_OP_SETXATTR:
  382. case CEPH_OSD_OP_SETXATTRS:
  383. case CEPH_OSD_OP_RESETXATTRS:
  384. case CEPH_OSD_OP_RMXATTR:
  385. case CEPH_OSD_OP_PULL:
  386. case CEPH_OSD_OP_PUSH:
  387. case CEPH_OSD_OP_BALANCEREADS:
  388. case CEPH_OSD_OP_UNBALANCEREADS:
  389. case CEPH_OSD_OP_SCRUB:
  390. case CEPH_OSD_OP_SCRUB_RESERVE:
  391. case CEPH_OSD_OP_SCRUB_UNRESERVE:
  392. case CEPH_OSD_OP_SCRUB_STOP:
  393. case CEPH_OSD_OP_SCRUB_MAP:
  394. case CEPH_OSD_OP_WRLOCK:
  395. case CEPH_OSD_OP_WRUNLOCK:
  396. case CEPH_OSD_OP_RDLOCK:
  397. case CEPH_OSD_OP_RDUNLOCK:
  398. case CEPH_OSD_OP_UPLOCK:
  399. case CEPH_OSD_OP_DNLOCK:
  400. case CEPH_OSD_OP_CALL:
  401. case CEPH_OSD_OP_PGLS:
  402. case CEPH_OSD_OP_PGLS_FILTER:
  403. return true;
  404. default:
  405. return false;
  406. }
  407. }
  408. /*
  409. * This is an osd op init function for opcodes that have no data or
  410. * other information associated with them. It also serves as a
  411. * common init routine for all the other init functions, below.
  412. */
  413. static struct ceph_osd_req_op *
  414. _osd_req_op_init(struct ceph_osd_request *osd_req, unsigned int which,
  415. u16 opcode)
  416. {
  417. struct ceph_osd_req_op *op;
  418. BUG_ON(which >= osd_req->r_num_ops);
  419. BUG_ON(!osd_req_opcode_valid(opcode));
  420. op = &osd_req->r_ops[which];
  421. memset(op, 0, sizeof (*op));
  422. op->op = opcode;
  423. return op;
  424. }
  425. void osd_req_op_init(struct ceph_osd_request *osd_req,
  426. unsigned int which, u16 opcode)
  427. {
  428. (void)_osd_req_op_init(osd_req, which, opcode);
  429. }
  430. EXPORT_SYMBOL(osd_req_op_init);
  431. void osd_req_op_extent_init(struct ceph_osd_request *osd_req,
  432. unsigned int which, u16 opcode,
  433. u64 offset, u64 length,
  434. u64 truncate_size, u32 truncate_seq)
  435. {
  436. struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which, opcode);
  437. size_t payload_len = 0;
  438. BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE);
  439. op->extent.offset = offset;
  440. op->extent.length = length;
  441. op->extent.truncate_size = truncate_size;
  442. op->extent.truncate_seq = truncate_seq;
  443. if (opcode == CEPH_OSD_OP_WRITE)
  444. payload_len += length;
  445. op->payload_len = payload_len;
  446. }
  447. EXPORT_SYMBOL(osd_req_op_extent_init);
  448. void osd_req_op_extent_update(struct ceph_osd_request *osd_req,
  449. unsigned int which, u64 length)
  450. {
  451. struct ceph_osd_req_op *op;
  452. u64 previous;
  453. BUG_ON(which >= osd_req->r_num_ops);
  454. op = &osd_req->r_ops[which];
  455. previous = op->extent.length;
  456. if (length == previous)
  457. return; /* Nothing to do */
  458. BUG_ON(length > previous);
  459. op->extent.length = length;
  460. op->payload_len -= previous - length;
  461. }
  462. EXPORT_SYMBOL(osd_req_op_extent_update);
  463. void osd_req_op_cls_init(struct ceph_osd_request *osd_req, unsigned int which,
  464. u16 opcode, const char *class, const char *method)
  465. {
  466. struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which, opcode);
  467. struct ceph_pagelist *pagelist;
  468. size_t payload_len = 0;
  469. size_t size;
  470. BUG_ON(opcode != CEPH_OSD_OP_CALL);
  471. pagelist = kmalloc(sizeof (*pagelist), GFP_NOFS);
  472. BUG_ON(!pagelist);
  473. ceph_pagelist_init(pagelist);
  474. op->cls.class_name = class;
  475. size = strlen(class);
  476. BUG_ON(size > (size_t) U8_MAX);
  477. op->cls.class_len = size;
  478. ceph_pagelist_append(pagelist, class, size);
  479. payload_len += size;
  480. op->cls.method_name = method;
  481. size = strlen(method);
  482. BUG_ON(size > (size_t) U8_MAX);
  483. op->cls.method_len = size;
  484. ceph_pagelist_append(pagelist, method, size);
  485. payload_len += size;
  486. osd_req_op_cls_request_info_pagelist(osd_req, which, pagelist);
  487. op->cls.argc = 0; /* currently unused */
  488. op->payload_len = payload_len;
  489. }
  490. EXPORT_SYMBOL(osd_req_op_cls_init);
  491. void osd_req_op_watch_init(struct ceph_osd_request *osd_req,
  492. unsigned int which, u16 opcode,
  493. u64 cookie, u64 version, int flag)
  494. {
  495. struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which, opcode);
  496. BUG_ON(opcode != CEPH_OSD_OP_NOTIFY_ACK && opcode != CEPH_OSD_OP_WATCH);
  497. op->watch.cookie = cookie;
  498. op->watch.ver = version;
  499. if (opcode == CEPH_OSD_OP_WATCH && flag)
  500. op->watch.flag = (u8)1;
  501. }
  502. EXPORT_SYMBOL(osd_req_op_watch_init);
  503. static void ceph_osdc_msg_data_add(struct ceph_msg *msg,
  504. struct ceph_osd_data *osd_data)
  505. {
  506. u64 length = ceph_osd_data_length(osd_data);
  507. if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES) {
  508. BUG_ON(length > (u64) SIZE_MAX);
  509. if (length)
  510. ceph_msg_data_add_pages(msg, osd_data->pages,
  511. length, osd_data->alignment);
  512. } else if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGELIST) {
  513. BUG_ON(!length);
  514. ceph_msg_data_add_pagelist(msg, osd_data->pagelist);
  515. #ifdef CONFIG_BLOCK
  516. } else if (osd_data->type == CEPH_OSD_DATA_TYPE_BIO) {
  517. ceph_msg_data_add_bio(msg, osd_data->bio, length);
  518. #endif
  519. } else {
  520. BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_NONE);
  521. }
  522. }
  523. static u64 osd_req_encode_op(struct ceph_osd_request *req,
  524. struct ceph_osd_op *dst, unsigned int which)
  525. {
  526. struct ceph_osd_req_op *src;
  527. struct ceph_osd_data *osd_data;
  528. u64 request_data_len = 0;
  529. u64 data_length;
  530. BUG_ON(which >= req->r_num_ops);
  531. src = &req->r_ops[which];
  532. if (WARN_ON(!osd_req_opcode_valid(src->op))) {
  533. pr_err("unrecognized osd opcode %d\n", src->op);
  534. return 0;
  535. }
  536. switch (src->op) {
  537. case CEPH_OSD_OP_STAT:
  538. osd_data = &src->raw_data_in;
  539. ceph_osdc_msg_data_add(req->r_reply, osd_data);
  540. break;
  541. case CEPH_OSD_OP_READ:
  542. case CEPH_OSD_OP_WRITE:
  543. if (src->op == CEPH_OSD_OP_WRITE)
  544. request_data_len = src->extent.length;
  545. dst->extent.offset = cpu_to_le64(src->extent.offset);
  546. dst->extent.length = cpu_to_le64(src->extent.length);
  547. dst->extent.truncate_size =
  548. cpu_to_le64(src->extent.truncate_size);
  549. dst->extent.truncate_seq =
  550. cpu_to_le32(src->extent.truncate_seq);
  551. osd_data = &src->extent.osd_data;
  552. if (src->op == CEPH_OSD_OP_WRITE)
  553. ceph_osdc_msg_data_add(req->r_request, osd_data);
  554. else
  555. ceph_osdc_msg_data_add(req->r_reply, osd_data);
  556. break;
  557. case CEPH_OSD_OP_CALL:
  558. dst->cls.class_len = src->cls.class_len;
  559. dst->cls.method_len = src->cls.method_len;
  560. osd_data = &src->cls.request_info;
  561. ceph_osdc_msg_data_add(req->r_request, osd_data);
  562. BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_PAGELIST);
  563. request_data_len = osd_data->pagelist->length;
  564. osd_data = &src->cls.request_data;
  565. data_length = ceph_osd_data_length(osd_data);
  566. if (data_length) {
  567. BUG_ON(osd_data->type == CEPH_OSD_DATA_TYPE_NONE);
  568. dst->cls.indata_len = cpu_to_le32(data_length);
  569. ceph_osdc_msg_data_add(req->r_request, osd_data);
  570. src->payload_len += data_length;
  571. request_data_len += data_length;
  572. }
  573. osd_data = &src->cls.response_data;
  574. ceph_osdc_msg_data_add(req->r_reply, osd_data);
  575. break;
  576. case CEPH_OSD_OP_STARTSYNC:
  577. break;
  578. case CEPH_OSD_OP_NOTIFY_ACK:
  579. case CEPH_OSD_OP_WATCH:
  580. dst->watch.cookie = cpu_to_le64(src->watch.cookie);
  581. dst->watch.ver = cpu_to_le64(src->watch.ver);
  582. dst->watch.flag = src->watch.flag;
  583. break;
  584. default:
  585. pr_err("unsupported osd opcode %s\n",
  586. ceph_osd_op_name(src->op));
  587. WARN_ON(1);
  588. return 0;
  589. }
  590. dst->op = cpu_to_le16(src->op);
  591. dst->payload_len = cpu_to_le32(src->payload_len);
  592. return request_data_len;
  593. }
  594. /*
  595. * build new request AND message, calculate layout, and adjust file
  596. * extent as needed.
  597. *
  598. * if the file was recently truncated, we include information about its
  599. * old and new size so that the object can be updated appropriately. (we
  600. * avoid synchronously deleting truncated objects because it's slow.)
  601. *
  602. * if @do_sync, include a 'startsync' command so that the osd will flush
  603. * data quickly.
  604. */
  605. struct ceph_osd_request *ceph_osdc_new_request(struct ceph_osd_client *osdc,
  606. struct ceph_file_layout *layout,
  607. struct ceph_vino vino,
  608. u64 off, u64 *plen, int num_ops,
  609. int opcode, int flags,
  610. struct ceph_snap_context *snapc,
  611. u32 truncate_seq,
  612. u64 truncate_size,
  613. bool use_mempool)
  614. {
  615. struct ceph_osd_request *req;
  616. u64 objnum = 0;
  617. u64 objoff = 0;
  618. u64 objlen = 0;
  619. u32 object_size;
  620. u64 object_base;
  621. int r;
  622. BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE);
  623. req = ceph_osdc_alloc_request(osdc, snapc, num_ops, use_mempool,
  624. GFP_NOFS);
  625. if (!req)
  626. return ERR_PTR(-ENOMEM);
  627. req->r_flags = flags;
  628. /* calculate max write size */
  629. r = calc_layout(layout, off, plen, &objnum, &objoff, &objlen);
  630. if (r < 0) {
  631. ceph_osdc_put_request(req);
  632. return ERR_PTR(r);
  633. }
  634. object_size = le32_to_cpu(layout->fl_object_size);
  635. object_base = off - objoff;
  636. if (truncate_size <= object_base) {
  637. truncate_size = 0;
  638. } else {
  639. truncate_size -= object_base;
  640. if (truncate_size > object_size)
  641. truncate_size = object_size;
  642. }
  643. osd_req_op_extent_init(req, 0, opcode, objoff, objlen,
  644. truncate_size, truncate_seq);
  645. /*
  646. * A second op in the ops array means the caller wants to
  647. * also issue a include a 'startsync' command so that the
  648. * osd will flush data quickly.
  649. */
  650. if (num_ops > 1)
  651. osd_req_op_init(req, 1, CEPH_OSD_OP_STARTSYNC);
  652. req->r_file_layout = *layout; /* keep a copy */
  653. snprintf(req->r_oid, sizeof(req->r_oid), "%llx.%08llx",
  654. vino.ino, objnum);
  655. req->r_oid_len = strlen(req->r_oid);
  656. return req;
  657. }
  658. EXPORT_SYMBOL(ceph_osdc_new_request);
  659. /*
  660. * We keep osd requests in an rbtree, sorted by ->r_tid.
  661. */
  662. static void __insert_request(struct ceph_osd_client *osdc,
  663. struct ceph_osd_request *new)
  664. {
  665. struct rb_node **p = &osdc->requests.rb_node;
  666. struct rb_node *parent = NULL;
  667. struct ceph_osd_request *req = NULL;
  668. while (*p) {
  669. parent = *p;
  670. req = rb_entry(parent, struct ceph_osd_request, r_node);
  671. if (new->r_tid < req->r_tid)
  672. p = &(*p)->rb_left;
  673. else if (new->r_tid > req->r_tid)
  674. p = &(*p)->rb_right;
  675. else
  676. BUG();
  677. }
  678. rb_link_node(&new->r_node, parent, p);
  679. rb_insert_color(&new->r_node, &osdc->requests);
  680. }
  681. static struct ceph_osd_request *__lookup_request(struct ceph_osd_client *osdc,
  682. u64 tid)
  683. {
  684. struct ceph_osd_request *req;
  685. struct rb_node *n = osdc->requests.rb_node;
  686. while (n) {
  687. req = rb_entry(n, struct ceph_osd_request, r_node);
  688. if (tid < req->r_tid)
  689. n = n->rb_left;
  690. else if (tid > req->r_tid)
  691. n = n->rb_right;
  692. else
  693. return req;
  694. }
  695. return NULL;
  696. }
  697. static struct ceph_osd_request *
  698. __lookup_request_ge(struct ceph_osd_client *osdc,
  699. u64 tid)
  700. {
  701. struct ceph_osd_request *req;
  702. struct rb_node *n = osdc->requests.rb_node;
  703. while (n) {
  704. req = rb_entry(n, struct ceph_osd_request, r_node);
  705. if (tid < req->r_tid) {
  706. if (!n->rb_left)
  707. return req;
  708. n = n->rb_left;
  709. } else if (tid > req->r_tid) {
  710. n = n->rb_right;
  711. } else {
  712. return req;
  713. }
  714. }
  715. return NULL;
  716. }
  717. /*
  718. * Resubmit requests pending on the given osd.
  719. */
  720. static void __kick_osd_requests(struct ceph_osd_client *osdc,
  721. struct ceph_osd *osd)
  722. {
  723. struct ceph_osd_request *req, *nreq;
  724. LIST_HEAD(resend);
  725. int err;
  726. dout("__kick_osd_requests osd%d\n", osd->o_osd);
  727. err = __reset_osd(osdc, osd);
  728. if (err)
  729. return;
  730. /*
  731. * Build up a list of requests to resend by traversing the
  732. * osd's list of requests. Requests for a given object are
  733. * sent in tid order, and that is also the order they're
  734. * kept on this list. Therefore all requests that are in
  735. * flight will be found first, followed by all requests that
  736. * have not yet been sent. And to resend requests while
  737. * preserving this order we will want to put any sent
  738. * requests back on the front of the osd client's unsent
  739. * list.
  740. *
  741. * So we build a separate ordered list of already-sent
  742. * requests for the affected osd and splice it onto the
  743. * front of the osd client's unsent list. Once we've seen a
  744. * request that has not yet been sent we're done. Those
  745. * requests are already sitting right where they belong.
  746. */
  747. list_for_each_entry(req, &osd->o_requests, r_osd_item) {
  748. if (!req->r_sent)
  749. break;
  750. list_move_tail(&req->r_req_lru_item, &resend);
  751. dout("requeueing %p tid %llu osd%d\n", req, req->r_tid,
  752. osd->o_osd);
  753. if (!req->r_linger)
  754. req->r_flags |= CEPH_OSD_FLAG_RETRY;
  755. }
  756. list_splice(&resend, &osdc->req_unsent);
  757. /*
  758. * Linger requests are re-registered before sending, which
  759. * sets up a new tid for each. We add them to the unsent
  760. * list at the end to keep things in tid order.
  761. */
  762. list_for_each_entry_safe(req, nreq, &osd->o_linger_requests,
  763. r_linger_osd) {
  764. /*
  765. * reregister request prior to unregistering linger so
  766. * that r_osd is preserved.
  767. */
  768. BUG_ON(!list_empty(&req->r_req_lru_item));
  769. __register_request(osdc, req);
  770. list_add_tail(&req->r_req_lru_item, &osdc->req_unsent);
  771. list_add_tail(&req->r_osd_item, &req->r_osd->o_requests);
  772. __unregister_linger_request(osdc, req);
  773. dout("requeued lingering %p tid %llu osd%d\n", req, req->r_tid,
  774. osd->o_osd);
  775. }
  776. }
  777. /*
  778. * If the osd connection drops, we need to resubmit all requests.
  779. */
  780. static void osd_reset(struct ceph_connection *con)
  781. {
  782. struct ceph_osd *osd = con->private;
  783. struct ceph_osd_client *osdc;
  784. if (!osd)
  785. return;
  786. dout("osd_reset osd%d\n", osd->o_osd);
  787. osdc = osd->o_osdc;
  788. down_read(&osdc->map_sem);
  789. mutex_lock(&osdc->request_mutex);
  790. __kick_osd_requests(osdc, osd);
  791. __send_queued(osdc);
  792. mutex_unlock(&osdc->request_mutex);
  793. up_read(&osdc->map_sem);
  794. }
  795. /*
  796. * Track open sessions with osds.
  797. */
  798. static struct ceph_osd *create_osd(struct ceph_osd_client *osdc, int onum)
  799. {
  800. struct ceph_osd *osd;
  801. osd = kzalloc(sizeof(*osd), GFP_NOFS);
  802. if (!osd)
  803. return NULL;
  804. atomic_set(&osd->o_ref, 1);
  805. osd->o_osdc = osdc;
  806. osd->o_osd = onum;
  807. RB_CLEAR_NODE(&osd->o_node);
  808. INIT_LIST_HEAD(&osd->o_requests);
  809. INIT_LIST_HEAD(&osd->o_linger_requests);
  810. INIT_LIST_HEAD(&osd->o_osd_lru);
  811. osd->o_incarnation = 1;
  812. ceph_con_init(&osd->o_con, osd, &osd_con_ops, &osdc->client->msgr);
  813. INIT_LIST_HEAD(&osd->o_keepalive_item);
  814. return osd;
  815. }
  816. static struct ceph_osd *get_osd(struct ceph_osd *osd)
  817. {
  818. if (atomic_inc_not_zero(&osd->o_ref)) {
  819. dout("get_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref)-1,
  820. atomic_read(&osd->o_ref));
  821. return osd;
  822. } else {
  823. dout("get_osd %p FAIL\n", osd);
  824. return NULL;
  825. }
  826. }
  827. static void put_osd(struct ceph_osd *osd)
  828. {
  829. dout("put_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref),
  830. atomic_read(&osd->o_ref) - 1);
  831. if (atomic_dec_and_test(&osd->o_ref) && osd->o_auth.authorizer) {
  832. struct ceph_auth_client *ac = osd->o_osdc->client->monc.auth;
  833. ceph_auth_destroy_authorizer(ac, osd->o_auth.authorizer);
  834. kfree(osd);
  835. }
  836. }
  837. /*
  838. * remove an osd from our map
  839. */
  840. static void __remove_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
  841. {
  842. dout("__remove_osd %p\n", osd);
  843. BUG_ON(!list_empty(&osd->o_requests));
  844. rb_erase(&osd->o_node, &osdc->osds);
  845. list_del_init(&osd->o_osd_lru);
  846. ceph_con_close(&osd->o_con);
  847. put_osd(osd);
  848. }
  849. static void remove_all_osds(struct ceph_osd_client *osdc)
  850. {
  851. dout("%s %p\n", __func__, osdc);
  852. mutex_lock(&osdc->request_mutex);
  853. while (!RB_EMPTY_ROOT(&osdc->osds)) {
  854. struct ceph_osd *osd = rb_entry(rb_first(&osdc->osds),
  855. struct ceph_osd, o_node);
  856. __remove_osd(osdc, osd);
  857. }
  858. mutex_unlock(&osdc->request_mutex);
  859. }
  860. static void __move_osd_to_lru(struct ceph_osd_client *osdc,
  861. struct ceph_osd *osd)
  862. {
  863. dout("__move_osd_to_lru %p\n", osd);
  864. BUG_ON(!list_empty(&osd->o_osd_lru));
  865. list_add_tail(&osd->o_osd_lru, &osdc->osd_lru);
  866. osd->lru_ttl = jiffies + osdc->client->options->osd_idle_ttl * HZ;
  867. }
  868. static void __remove_osd_from_lru(struct ceph_osd *osd)
  869. {
  870. dout("__remove_osd_from_lru %p\n", osd);
  871. if (!list_empty(&osd->o_osd_lru))
  872. list_del_init(&osd->o_osd_lru);
  873. }
  874. static void remove_old_osds(struct ceph_osd_client *osdc)
  875. {
  876. struct ceph_osd *osd, *nosd;
  877. dout("__remove_old_osds %p\n", osdc);
  878. mutex_lock(&osdc->request_mutex);
  879. list_for_each_entry_safe(osd, nosd, &osdc->osd_lru, o_osd_lru) {
  880. if (time_before(jiffies, osd->lru_ttl))
  881. break;
  882. __remove_osd(osdc, osd);
  883. }
  884. mutex_unlock(&osdc->request_mutex);
  885. }
  886. /*
  887. * reset osd connect
  888. */
  889. static int __reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
  890. {
  891. struct ceph_entity_addr *peer_addr;
  892. dout("__reset_osd %p osd%d\n", osd, osd->o_osd);
  893. if (list_empty(&osd->o_requests) &&
  894. list_empty(&osd->o_linger_requests)) {
  895. __remove_osd(osdc, osd);
  896. return -ENODEV;
  897. }
  898. peer_addr = &osdc->osdmap->osd_addr[osd->o_osd];
  899. if (!memcmp(peer_addr, &osd->o_con.peer_addr, sizeof (*peer_addr)) &&
  900. !ceph_con_opened(&osd->o_con)) {
  901. struct ceph_osd_request *req;
  902. dout(" osd addr hasn't changed and connection never opened,"
  903. " letting msgr retry");
  904. /* touch each r_stamp for handle_timeout()'s benfit */
  905. list_for_each_entry(req, &osd->o_requests, r_osd_item)
  906. req->r_stamp = jiffies;
  907. return -EAGAIN;
  908. }
  909. ceph_con_close(&osd->o_con);
  910. ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd, peer_addr);
  911. osd->o_incarnation++;
  912. return 0;
  913. }
  914. static void __insert_osd(struct ceph_osd_client *osdc, struct ceph_osd *new)
  915. {
  916. struct rb_node **p = &osdc->osds.rb_node;
  917. struct rb_node *parent = NULL;
  918. struct ceph_osd *osd = NULL;
  919. dout("__insert_osd %p osd%d\n", new, new->o_osd);
  920. while (*p) {
  921. parent = *p;
  922. osd = rb_entry(parent, struct ceph_osd, o_node);
  923. if (new->o_osd < osd->o_osd)
  924. p = &(*p)->rb_left;
  925. else if (new->o_osd > osd->o_osd)
  926. p = &(*p)->rb_right;
  927. else
  928. BUG();
  929. }
  930. rb_link_node(&new->o_node, parent, p);
  931. rb_insert_color(&new->o_node, &osdc->osds);
  932. }
  933. static struct ceph_osd *__lookup_osd(struct ceph_osd_client *osdc, int o)
  934. {
  935. struct ceph_osd *osd;
  936. struct rb_node *n = osdc->osds.rb_node;
  937. while (n) {
  938. osd = rb_entry(n, struct ceph_osd, o_node);
  939. if (o < osd->o_osd)
  940. n = n->rb_left;
  941. else if (o > osd->o_osd)
  942. n = n->rb_right;
  943. else
  944. return osd;
  945. }
  946. return NULL;
  947. }
  948. static void __schedule_osd_timeout(struct ceph_osd_client *osdc)
  949. {
  950. schedule_delayed_work(&osdc->timeout_work,
  951. osdc->client->options->osd_keepalive_timeout * HZ);
  952. }
  953. static void __cancel_osd_timeout(struct ceph_osd_client *osdc)
  954. {
  955. cancel_delayed_work(&osdc->timeout_work);
  956. }
  957. /*
  958. * Register request, assign tid. If this is the first request, set up
  959. * the timeout event.
  960. */
  961. static void __register_request(struct ceph_osd_client *osdc,
  962. struct ceph_osd_request *req)
  963. {
  964. req->r_tid = ++osdc->last_tid;
  965. req->r_request->hdr.tid = cpu_to_le64(req->r_tid);
  966. dout("__register_request %p tid %lld\n", req, req->r_tid);
  967. __insert_request(osdc, req);
  968. ceph_osdc_get_request(req);
  969. osdc->num_requests++;
  970. if (osdc->num_requests == 1) {
  971. dout(" first request, scheduling timeout\n");
  972. __schedule_osd_timeout(osdc);
  973. }
  974. }
  975. /*
  976. * called under osdc->request_mutex
  977. */
  978. static void __unregister_request(struct ceph_osd_client *osdc,
  979. struct ceph_osd_request *req)
  980. {
  981. if (RB_EMPTY_NODE(&req->r_node)) {
  982. dout("__unregister_request %p tid %lld not registered\n",
  983. req, req->r_tid);
  984. return;
  985. }
  986. dout("__unregister_request %p tid %lld\n", req, req->r_tid);
  987. rb_erase(&req->r_node, &osdc->requests);
  988. osdc->num_requests--;
  989. if (req->r_osd) {
  990. /* make sure the original request isn't in flight. */
  991. ceph_msg_revoke(req->r_request);
  992. list_del_init(&req->r_osd_item);
  993. if (list_empty(&req->r_osd->o_requests) &&
  994. list_empty(&req->r_osd->o_linger_requests)) {
  995. dout("moving osd to %p lru\n", req->r_osd);
  996. __move_osd_to_lru(osdc, req->r_osd);
  997. }
  998. if (list_empty(&req->r_linger_item))
  999. req->r_osd = NULL;
  1000. }
  1001. list_del_init(&req->r_req_lru_item);
  1002. ceph_osdc_put_request(req);
  1003. if (osdc->num_requests == 0) {
  1004. dout(" no requests, canceling timeout\n");
  1005. __cancel_osd_timeout(osdc);
  1006. }
  1007. }
  1008. /*
  1009. * Cancel a previously queued request message
  1010. */
  1011. static void __cancel_request(struct ceph_osd_request *req)
  1012. {
  1013. if (req->r_sent && req->r_osd) {
  1014. ceph_msg_revoke(req->r_request);
  1015. req->r_sent = 0;
  1016. }
  1017. }
  1018. static void __register_linger_request(struct ceph_osd_client *osdc,
  1019. struct ceph_osd_request *req)
  1020. {
  1021. dout("__register_linger_request %p\n", req);
  1022. list_add_tail(&req->r_linger_item, &osdc->req_linger);
  1023. if (req->r_osd)
  1024. list_add_tail(&req->r_linger_osd,
  1025. &req->r_osd->o_linger_requests);
  1026. }
  1027. static void __unregister_linger_request(struct ceph_osd_client *osdc,
  1028. struct ceph_osd_request *req)
  1029. {
  1030. dout("__unregister_linger_request %p\n", req);
  1031. list_del_init(&req->r_linger_item);
  1032. if (req->r_osd) {
  1033. list_del_init(&req->r_linger_osd);
  1034. if (list_empty(&req->r_osd->o_requests) &&
  1035. list_empty(&req->r_osd->o_linger_requests)) {
  1036. dout("moving osd to %p lru\n", req->r_osd);
  1037. __move_osd_to_lru(osdc, req->r_osd);
  1038. }
  1039. if (list_empty(&req->r_osd_item))
  1040. req->r_osd = NULL;
  1041. }
  1042. }
  1043. void ceph_osdc_unregister_linger_request(struct ceph_osd_client *osdc,
  1044. struct ceph_osd_request *req)
  1045. {
  1046. mutex_lock(&osdc->request_mutex);
  1047. if (req->r_linger) {
  1048. __unregister_linger_request(osdc, req);
  1049. ceph_osdc_put_request(req);
  1050. }
  1051. mutex_unlock(&osdc->request_mutex);
  1052. }
  1053. EXPORT_SYMBOL(ceph_osdc_unregister_linger_request);
  1054. void ceph_osdc_set_request_linger(struct ceph_osd_client *osdc,
  1055. struct ceph_osd_request *req)
  1056. {
  1057. if (!req->r_linger) {
  1058. dout("set_request_linger %p\n", req);
  1059. req->r_linger = 1;
  1060. /*
  1061. * caller is now responsible for calling
  1062. * unregister_linger_request
  1063. */
  1064. ceph_osdc_get_request(req);
  1065. }
  1066. }
  1067. EXPORT_SYMBOL(ceph_osdc_set_request_linger);
  1068. /*
  1069. * Pick an osd (the first 'up' osd in the pg), allocate the osd struct
  1070. * (as needed), and set the request r_osd appropriately. If there is
  1071. * no up osd, set r_osd to NULL. Move the request to the appropriate list
  1072. * (unsent, homeless) or leave on in-flight lru.
  1073. *
  1074. * Return 0 if unchanged, 1 if changed, or negative on error.
  1075. *
  1076. * Caller should hold map_sem for read and request_mutex.
  1077. */
  1078. static int __map_request(struct ceph_osd_client *osdc,
  1079. struct ceph_osd_request *req, int force_resend)
  1080. {
  1081. struct ceph_pg pgid;
  1082. int acting[CEPH_PG_MAX_SIZE];
  1083. int o = -1, num = 0;
  1084. int err;
  1085. dout("map_request %p tid %lld\n", req, req->r_tid);
  1086. err = ceph_calc_ceph_pg(&pgid, req->r_oid, osdc->osdmap,
  1087. ceph_file_layout_pg_pool(req->r_file_layout));
  1088. if (err) {
  1089. list_move(&req->r_req_lru_item, &osdc->req_notarget);
  1090. return err;
  1091. }
  1092. req->r_pgid = pgid;
  1093. err = ceph_calc_pg_acting(osdc->osdmap, pgid, acting);
  1094. if (err > 0) {
  1095. o = acting[0];
  1096. num = err;
  1097. }
  1098. if ((!force_resend &&
  1099. req->r_osd && req->r_osd->o_osd == o &&
  1100. req->r_sent >= req->r_osd->o_incarnation &&
  1101. req->r_num_pg_osds == num &&
  1102. memcmp(req->r_pg_osds, acting, sizeof(acting[0])*num) == 0) ||
  1103. (req->r_osd == NULL && o == -1))
  1104. return 0; /* no change */
  1105. dout("map_request tid %llu pgid %lld.%x osd%d (was osd%d)\n",
  1106. req->r_tid, pgid.pool, pgid.seed, o,
  1107. req->r_osd ? req->r_osd->o_osd : -1);
  1108. /* record full pg acting set */
  1109. memcpy(req->r_pg_osds, acting, sizeof(acting[0]) * num);
  1110. req->r_num_pg_osds = num;
  1111. if (req->r_osd) {
  1112. __cancel_request(req);
  1113. list_del_init(&req->r_osd_item);
  1114. req->r_osd = NULL;
  1115. }
  1116. req->r_osd = __lookup_osd(osdc, o);
  1117. if (!req->r_osd && o >= 0) {
  1118. err = -ENOMEM;
  1119. req->r_osd = create_osd(osdc, o);
  1120. if (!req->r_osd) {
  1121. list_move(&req->r_req_lru_item, &osdc->req_notarget);
  1122. goto out;
  1123. }
  1124. dout("map_request osd %p is osd%d\n", req->r_osd, o);
  1125. __insert_osd(osdc, req->r_osd);
  1126. ceph_con_open(&req->r_osd->o_con,
  1127. CEPH_ENTITY_TYPE_OSD, o,
  1128. &osdc->osdmap->osd_addr[o]);
  1129. }
  1130. if (req->r_osd) {
  1131. __remove_osd_from_lru(req->r_osd);
  1132. list_add_tail(&req->r_osd_item, &req->r_osd->o_requests);
  1133. list_move_tail(&req->r_req_lru_item, &osdc->req_unsent);
  1134. } else {
  1135. list_move_tail(&req->r_req_lru_item, &osdc->req_notarget);
  1136. }
  1137. err = 1; /* osd or pg changed */
  1138. out:
  1139. return err;
  1140. }
  1141. /*
  1142. * caller should hold map_sem (for read) and request_mutex
  1143. */
  1144. static void __send_request(struct ceph_osd_client *osdc,
  1145. struct ceph_osd_request *req)
  1146. {
  1147. void *p;
  1148. dout("send_request %p tid %llu to osd%d flags %d pg %lld.%x\n",
  1149. req, req->r_tid, req->r_osd->o_osd, req->r_flags,
  1150. (unsigned long long)req->r_pgid.pool, req->r_pgid.seed);
  1151. /* fill in message content that changes each time we send it */
  1152. put_unaligned_le32(osdc->osdmap->epoch, req->r_request_osdmap_epoch);
  1153. put_unaligned_le32(req->r_flags, req->r_request_flags);
  1154. put_unaligned_le64(req->r_pgid.pool, req->r_request_pool);
  1155. p = req->r_request_pgid;
  1156. ceph_encode_64(&p, req->r_pgid.pool);
  1157. ceph_encode_32(&p, req->r_pgid.seed);
  1158. put_unaligned_le64(1, req->r_request_attempts); /* FIXME */
  1159. memcpy(req->r_request_reassert_version, &req->r_reassert_version,
  1160. sizeof(req->r_reassert_version));
  1161. req->r_stamp = jiffies;
  1162. list_move_tail(&req->r_req_lru_item, &osdc->req_lru);
  1163. ceph_msg_get(req->r_request); /* send consumes a ref */
  1164. /* Mark the request unsafe if this is the first timet's being sent. */
  1165. if (!req->r_sent && req->r_unsafe_callback)
  1166. req->r_unsafe_callback(req, true);
  1167. req->r_sent = req->r_osd->o_incarnation;
  1168. ceph_con_send(&req->r_osd->o_con, req->r_request);
  1169. }
  1170. /*
  1171. * Send any requests in the queue (req_unsent).
  1172. */
  1173. static void __send_queued(struct ceph_osd_client *osdc)
  1174. {
  1175. struct ceph_osd_request *req, *tmp;
  1176. dout("__send_queued\n");
  1177. list_for_each_entry_safe(req, tmp, &osdc->req_unsent, r_req_lru_item)
  1178. __send_request(osdc, req);
  1179. }
  1180. /*
  1181. * Timeout callback, called every N seconds when 1 or more osd
  1182. * requests has been active for more than N seconds. When this
  1183. * happens, we ping all OSDs with requests who have timed out to
  1184. * ensure any communications channel reset is detected. Reset the
  1185. * request timeouts another N seconds in the future as we go.
  1186. * Reschedule the timeout event another N seconds in future (unless
  1187. * there are no open requests).
  1188. */
  1189. static void handle_timeout(struct work_struct *work)
  1190. {
  1191. struct ceph_osd_client *osdc =
  1192. container_of(work, struct ceph_osd_client, timeout_work.work);
  1193. struct ceph_osd_request *req;
  1194. struct ceph_osd *osd;
  1195. unsigned long keepalive =
  1196. osdc->client->options->osd_keepalive_timeout * HZ;
  1197. struct list_head slow_osds;
  1198. dout("timeout\n");
  1199. down_read(&osdc->map_sem);
  1200. ceph_monc_request_next_osdmap(&osdc->client->monc);
  1201. mutex_lock(&osdc->request_mutex);
  1202. /*
  1203. * ping osds that are a bit slow. this ensures that if there
  1204. * is a break in the TCP connection we will notice, and reopen
  1205. * a connection with that osd (from the fault callback).
  1206. */
  1207. INIT_LIST_HEAD(&slow_osds);
  1208. list_for_each_entry(req, &osdc->req_lru, r_req_lru_item) {
  1209. if (time_before(jiffies, req->r_stamp + keepalive))
  1210. break;
  1211. osd = req->r_osd;
  1212. BUG_ON(!osd);
  1213. dout(" tid %llu is slow, will send keepalive on osd%d\n",
  1214. req->r_tid, osd->o_osd);
  1215. list_move_tail(&osd->o_keepalive_item, &slow_osds);
  1216. }
  1217. while (!list_empty(&slow_osds)) {
  1218. osd = list_entry(slow_osds.next, struct ceph_osd,
  1219. o_keepalive_item);
  1220. list_del_init(&osd->o_keepalive_item);
  1221. ceph_con_keepalive(&osd->o_con);
  1222. }
  1223. __schedule_osd_timeout(osdc);
  1224. __send_queued(osdc);
  1225. mutex_unlock(&osdc->request_mutex);
  1226. up_read(&osdc->map_sem);
  1227. }
  1228. static void handle_osds_timeout(struct work_struct *work)
  1229. {
  1230. struct ceph_osd_client *osdc =
  1231. container_of(work, struct ceph_osd_client,
  1232. osds_timeout_work.work);
  1233. unsigned long delay =
  1234. osdc->client->options->osd_idle_ttl * HZ >> 2;
  1235. dout("osds timeout\n");
  1236. down_read(&osdc->map_sem);
  1237. remove_old_osds(osdc);
  1238. up_read(&osdc->map_sem);
  1239. schedule_delayed_work(&osdc->osds_timeout_work,
  1240. round_jiffies_relative(delay));
  1241. }
  1242. static void complete_request(struct ceph_osd_request *req)
  1243. {
  1244. if (req->r_unsafe_callback)
  1245. req->r_unsafe_callback(req, false);
  1246. complete_all(&req->r_safe_completion); /* fsync waiter */
  1247. }
  1248. /*
  1249. * handle osd op reply. either call the callback if it is specified,
  1250. * or do the completion to wake up the waiting thread.
  1251. */
  1252. static void handle_reply(struct ceph_osd_client *osdc, struct ceph_msg *msg,
  1253. struct ceph_connection *con)
  1254. {
  1255. void *p, *end;
  1256. struct ceph_osd_request *req;
  1257. u64 tid;
  1258. int object_len;
  1259. unsigned int numops;
  1260. int payload_len, flags;
  1261. s32 result;
  1262. s32 retry_attempt;
  1263. struct ceph_pg pg;
  1264. int err;
  1265. u32 reassert_epoch;
  1266. u64 reassert_version;
  1267. u32 osdmap_epoch;
  1268. int already_completed;
  1269. u32 bytes;
  1270. unsigned int i;
  1271. tid = le64_to_cpu(msg->hdr.tid);
  1272. dout("handle_reply %p tid %llu\n", msg, tid);
  1273. p = msg->front.iov_base;
  1274. end = p + msg->front.iov_len;
  1275. ceph_decode_need(&p, end, 4, bad);
  1276. object_len = ceph_decode_32(&p);
  1277. ceph_decode_need(&p, end, object_len, bad);
  1278. p += object_len;
  1279. err = ceph_decode_pgid(&p, end, &pg);
  1280. if (err)
  1281. goto bad;
  1282. ceph_decode_need(&p, end, 8 + 4 + 4 + 8 + 4, bad);
  1283. flags = ceph_decode_64(&p);
  1284. result = ceph_decode_32(&p);
  1285. reassert_epoch = ceph_decode_32(&p);
  1286. reassert_version = ceph_decode_64(&p);
  1287. osdmap_epoch = ceph_decode_32(&p);
  1288. /* lookup */
  1289. mutex_lock(&osdc->request_mutex);
  1290. req = __lookup_request(osdc, tid);
  1291. if (req == NULL) {
  1292. dout("handle_reply tid %llu dne\n", tid);
  1293. goto bad_mutex;
  1294. }
  1295. ceph_osdc_get_request(req);
  1296. dout("handle_reply %p tid %llu req %p result %d\n", msg, tid,
  1297. req, result);
  1298. ceph_decode_need(&p, end, 4, bad);
  1299. numops = ceph_decode_32(&p);
  1300. if (numops > CEPH_OSD_MAX_OP)
  1301. goto bad_put;
  1302. if (numops != req->r_num_ops)
  1303. goto bad_put;
  1304. payload_len = 0;
  1305. ceph_decode_need(&p, end, numops * sizeof(struct ceph_osd_op), bad);
  1306. for (i = 0; i < numops; i++) {
  1307. struct ceph_osd_op *op = p;
  1308. int len;
  1309. len = le32_to_cpu(op->payload_len);
  1310. req->r_reply_op_len[i] = len;
  1311. dout(" op %d has %d bytes\n", i, len);
  1312. payload_len += len;
  1313. p += sizeof(*op);
  1314. }
  1315. bytes = le32_to_cpu(msg->hdr.data_len);
  1316. if (payload_len != bytes) {
  1317. pr_warning("sum of op payload lens %d != data_len %d",
  1318. payload_len, bytes);
  1319. goto bad_put;
  1320. }
  1321. ceph_decode_need(&p, end, 4 + numops * 4, bad);
  1322. retry_attempt = ceph_decode_32(&p);
  1323. for (i = 0; i < numops; i++)
  1324. req->r_reply_op_result[i] = ceph_decode_32(&p);
  1325. if (!req->r_got_reply) {
  1326. req->r_result = result;
  1327. dout("handle_reply result %d bytes %d\n", req->r_result,
  1328. bytes);
  1329. if (req->r_result == 0)
  1330. req->r_result = bytes;
  1331. /* in case this is a write and we need to replay, */
  1332. req->r_reassert_version.epoch = cpu_to_le32(reassert_epoch);
  1333. req->r_reassert_version.version = cpu_to_le64(reassert_version);
  1334. req->r_got_reply = 1;
  1335. } else if ((flags & CEPH_OSD_FLAG_ONDISK) == 0) {
  1336. dout("handle_reply tid %llu dup ack\n", tid);
  1337. mutex_unlock(&osdc->request_mutex);
  1338. goto done;
  1339. }
  1340. dout("handle_reply tid %llu flags %d\n", tid, flags);
  1341. if (req->r_linger && (flags & CEPH_OSD_FLAG_ONDISK))
  1342. __register_linger_request(osdc, req);
  1343. /* either this is a read, or we got the safe response */
  1344. if (result < 0 ||
  1345. (flags & CEPH_OSD_FLAG_ONDISK) ||
  1346. ((flags & CEPH_OSD_FLAG_WRITE) == 0))
  1347. __unregister_request(osdc, req);
  1348. already_completed = req->r_completed;
  1349. req->r_completed = 1;
  1350. mutex_unlock(&osdc->request_mutex);
  1351. if (already_completed)
  1352. goto done;
  1353. if (req->r_callback)
  1354. req->r_callback(req, msg);
  1355. else
  1356. complete_all(&req->r_completion);
  1357. if (flags & CEPH_OSD_FLAG_ONDISK)
  1358. complete_request(req);
  1359. done:
  1360. dout("req=%p req->r_linger=%d\n", req, req->r_linger);
  1361. ceph_osdc_put_request(req);
  1362. return;
  1363. bad_put:
  1364. ceph_osdc_put_request(req);
  1365. bad_mutex:
  1366. mutex_unlock(&osdc->request_mutex);
  1367. bad:
  1368. pr_err("corrupt osd_op_reply got %d %d\n",
  1369. (int)msg->front.iov_len, le32_to_cpu(msg->hdr.front_len));
  1370. ceph_msg_dump(msg);
  1371. }
  1372. static void reset_changed_osds(struct ceph_osd_client *osdc)
  1373. {
  1374. struct rb_node *p, *n;
  1375. for (p = rb_first(&osdc->osds); p; p = n) {
  1376. struct ceph_osd *osd = rb_entry(p, struct ceph_osd, o_node);
  1377. n = rb_next(p);
  1378. if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) ||
  1379. memcmp(&osd->o_con.peer_addr,
  1380. ceph_osd_addr(osdc->osdmap,
  1381. osd->o_osd),
  1382. sizeof(struct ceph_entity_addr)) != 0)
  1383. __reset_osd(osdc, osd);
  1384. }
  1385. }
  1386. /*
  1387. * Requeue requests whose mapping to an OSD has changed. If requests map to
  1388. * no osd, request a new map.
  1389. *
  1390. * Caller should hold map_sem for read.
  1391. */
  1392. static void kick_requests(struct ceph_osd_client *osdc, int force_resend)
  1393. {
  1394. struct ceph_osd_request *req, *nreq;
  1395. struct rb_node *p;
  1396. int needmap = 0;
  1397. int err;
  1398. dout("kick_requests %s\n", force_resend ? " (force resend)" : "");
  1399. mutex_lock(&osdc->request_mutex);
  1400. for (p = rb_first(&osdc->requests); p; ) {
  1401. req = rb_entry(p, struct ceph_osd_request, r_node);
  1402. p = rb_next(p);
  1403. /*
  1404. * For linger requests that have not yet been
  1405. * registered, move them to the linger list; they'll
  1406. * be sent to the osd in the loop below. Unregister
  1407. * the request before re-registering it as a linger
  1408. * request to ensure the __map_request() below
  1409. * will decide it needs to be sent.
  1410. */
  1411. if (req->r_linger && list_empty(&req->r_linger_item)) {
  1412. dout("%p tid %llu restart on osd%d\n",
  1413. req, req->r_tid,
  1414. req->r_osd ? req->r_osd->o_osd : -1);
  1415. __unregister_request(osdc, req);
  1416. __register_linger_request(osdc, req);
  1417. continue;
  1418. }
  1419. err = __map_request(osdc, req, force_resend);
  1420. if (err < 0)
  1421. continue; /* error */
  1422. if (req->r_osd == NULL) {
  1423. dout("%p tid %llu maps to no osd\n", req, req->r_tid);
  1424. needmap++; /* request a newer map */
  1425. } else if (err > 0) {
  1426. if (!req->r_linger) {
  1427. dout("%p tid %llu requeued on osd%d\n", req,
  1428. req->r_tid,
  1429. req->r_osd ? req->r_osd->o_osd : -1);
  1430. req->r_flags |= CEPH_OSD_FLAG_RETRY;
  1431. }
  1432. }
  1433. }
  1434. list_for_each_entry_safe(req, nreq, &osdc->req_linger,
  1435. r_linger_item) {
  1436. dout("linger req=%p req->r_osd=%p\n", req, req->r_osd);
  1437. err = __map_request(osdc, req, force_resend);
  1438. dout("__map_request returned %d\n", err);
  1439. if (err == 0)
  1440. continue; /* no change and no osd was specified */
  1441. if (err < 0)
  1442. continue; /* hrm! */
  1443. if (req->r_osd == NULL) {
  1444. dout("tid %llu maps to no valid osd\n", req->r_tid);
  1445. needmap++; /* request a newer map */
  1446. continue;
  1447. }
  1448. dout("kicking lingering %p tid %llu osd%d\n", req, req->r_tid,
  1449. req->r_osd ? req->r_osd->o_osd : -1);
  1450. __register_request(osdc, req);
  1451. __unregister_linger_request(osdc, req);
  1452. }
  1453. mutex_unlock(&osdc->request_mutex);
  1454. if (needmap) {
  1455. dout("%d requests for down osds, need new map\n", needmap);
  1456. ceph_monc_request_next_osdmap(&osdc->client->monc);
  1457. }
  1458. reset_changed_osds(osdc);
  1459. }
  1460. /*
  1461. * Process updated osd map.
  1462. *
  1463. * The message contains any number of incremental and full maps, normally
  1464. * indicating some sort of topology change in the cluster. Kick requests
  1465. * off to different OSDs as needed.
  1466. */
  1467. void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg)
  1468. {
  1469. void *p, *end, *next;
  1470. u32 nr_maps, maplen;
  1471. u32 epoch;
  1472. struct ceph_osdmap *newmap = NULL, *oldmap;
  1473. int err;
  1474. struct ceph_fsid fsid;
  1475. dout("handle_map have %u\n", osdc->osdmap ? osdc->osdmap->epoch : 0);
  1476. p = msg->front.iov_base;
  1477. end = p + msg->front.iov_len;
  1478. /* verify fsid */
  1479. ceph_decode_need(&p, end, sizeof(fsid), bad);
  1480. ceph_decode_copy(&p, &fsid, sizeof(fsid));
  1481. if (ceph_check_fsid(osdc->client, &fsid) < 0)
  1482. return;
  1483. down_write(&osdc->map_sem);
  1484. /* incremental maps */
  1485. ceph_decode_32_safe(&p, end, nr_maps, bad);
  1486. dout(" %d inc maps\n", nr_maps);
  1487. while (nr_maps > 0) {
  1488. ceph_decode_need(&p, end, 2*sizeof(u32), bad);
  1489. epoch = ceph_decode_32(&p);
  1490. maplen = ceph_decode_32(&p);
  1491. ceph_decode_need(&p, end, maplen, bad);
  1492. next = p + maplen;
  1493. if (osdc->osdmap && osdc->osdmap->epoch+1 == epoch) {
  1494. dout("applying incremental map %u len %d\n",
  1495. epoch, maplen);
  1496. newmap = osdmap_apply_incremental(&p, next,
  1497. osdc->osdmap,
  1498. &osdc->client->msgr);
  1499. if (IS_ERR(newmap)) {
  1500. err = PTR_ERR(newmap);
  1501. goto bad;
  1502. }
  1503. BUG_ON(!newmap);
  1504. if (newmap != osdc->osdmap) {
  1505. ceph_osdmap_destroy(osdc->osdmap);
  1506. osdc->osdmap = newmap;
  1507. }
  1508. kick_requests(osdc, 0);
  1509. } else {
  1510. dout("ignoring incremental map %u len %d\n",
  1511. epoch, maplen);
  1512. }
  1513. p = next;
  1514. nr_maps--;
  1515. }
  1516. if (newmap)
  1517. goto done;
  1518. /* full maps */
  1519. ceph_decode_32_safe(&p, end, nr_maps, bad);
  1520. dout(" %d full maps\n", nr_maps);
  1521. while (nr_maps) {
  1522. ceph_decode_need(&p, end, 2*sizeof(u32), bad);
  1523. epoch = ceph_decode_32(&p);
  1524. maplen = ceph_decode_32(&p);
  1525. ceph_decode_need(&p, end, maplen, bad);
  1526. if (nr_maps > 1) {
  1527. dout("skipping non-latest full map %u len %d\n",
  1528. epoch, maplen);
  1529. } else if (osdc->osdmap && osdc->osdmap->epoch >= epoch) {
  1530. dout("skipping full map %u len %d, "
  1531. "older than our %u\n", epoch, maplen,
  1532. osdc->osdmap->epoch);
  1533. } else {
  1534. int skipped_map = 0;
  1535. dout("taking full map %u len %d\n", epoch, maplen);
  1536. newmap = osdmap_decode(&p, p+maplen);
  1537. if (IS_ERR(newmap)) {
  1538. err = PTR_ERR(newmap);
  1539. goto bad;
  1540. }
  1541. BUG_ON(!newmap);
  1542. oldmap = osdc->osdmap;
  1543. osdc->osdmap = newmap;
  1544. if (oldmap) {
  1545. if (oldmap->epoch + 1 < newmap->epoch)
  1546. skipped_map = 1;
  1547. ceph_osdmap_destroy(oldmap);
  1548. }
  1549. kick_requests(osdc, skipped_map);
  1550. }
  1551. p += maplen;
  1552. nr_maps--;
  1553. }
  1554. done:
  1555. downgrade_write(&osdc->map_sem);
  1556. ceph_monc_got_osdmap(&osdc->client->monc, osdc->osdmap->epoch);
  1557. /*
  1558. * subscribe to subsequent osdmap updates if full to ensure
  1559. * we find out when we are no longer full and stop returning
  1560. * ENOSPC.
  1561. */
  1562. if (ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL))
  1563. ceph_monc_request_next_osdmap(&osdc->client->monc);
  1564. mutex_lock(&osdc->request_mutex);
  1565. __send_queued(osdc);
  1566. mutex_unlock(&osdc->request_mutex);
  1567. up_read(&osdc->map_sem);
  1568. wake_up_all(&osdc->client->auth_wq);
  1569. return;
  1570. bad:
  1571. pr_err("osdc handle_map corrupt msg\n");
  1572. ceph_msg_dump(msg);
  1573. up_write(&osdc->map_sem);
  1574. return;
  1575. }
  1576. /*
  1577. * watch/notify callback event infrastructure
  1578. *
  1579. * These callbacks are used both for watch and notify operations.
  1580. */
  1581. static void __release_event(struct kref *kref)
  1582. {
  1583. struct ceph_osd_event *event =
  1584. container_of(kref, struct ceph_osd_event, kref);
  1585. dout("__release_event %p\n", event);
  1586. kfree(event);
  1587. }
  1588. static void get_event(struct ceph_osd_event *event)
  1589. {
  1590. kref_get(&event->kref);
  1591. }
  1592. void ceph_osdc_put_event(struct ceph_osd_event *event)
  1593. {
  1594. kref_put(&event->kref, __release_event);
  1595. }
  1596. EXPORT_SYMBOL(ceph_osdc_put_event);
  1597. static void __insert_event(struct ceph_osd_client *osdc,
  1598. struct ceph_osd_event *new)
  1599. {
  1600. struct rb_node **p = &osdc->event_tree.rb_node;
  1601. struct rb_node *parent = NULL;
  1602. struct ceph_osd_event *event = NULL;
  1603. while (*p) {
  1604. parent = *p;
  1605. event = rb_entry(parent, struct ceph_osd_event, node);
  1606. if (new->cookie < event->cookie)
  1607. p = &(*p)->rb_left;
  1608. else if (new->cookie > event->cookie)
  1609. p = &(*p)->rb_right;
  1610. else
  1611. BUG();
  1612. }
  1613. rb_link_node(&new->node, parent, p);
  1614. rb_insert_color(&new->node, &osdc->event_tree);
  1615. }
  1616. static struct ceph_osd_event *__find_event(struct ceph_osd_client *osdc,
  1617. u64 cookie)
  1618. {
  1619. struct rb_node **p = &osdc->event_tree.rb_node;
  1620. struct rb_node *parent = NULL;
  1621. struct ceph_osd_event *event = NULL;
  1622. while (*p) {
  1623. parent = *p;
  1624. event = rb_entry(parent, struct ceph_osd_event, node);
  1625. if (cookie < event->cookie)
  1626. p = &(*p)->rb_left;
  1627. else if (cookie > event->cookie)
  1628. p = &(*p)->rb_right;
  1629. else
  1630. return event;
  1631. }
  1632. return NULL;
  1633. }
  1634. static void __remove_event(struct ceph_osd_event *event)
  1635. {
  1636. struct ceph_osd_client *osdc = event->osdc;
  1637. if (!RB_EMPTY_NODE(&event->node)) {
  1638. dout("__remove_event removed %p\n", event);
  1639. rb_erase(&event->node, &osdc->event_tree);
  1640. ceph_osdc_put_event(event);
  1641. } else {
  1642. dout("__remove_event didn't remove %p\n", event);
  1643. }
  1644. }
  1645. int ceph_osdc_create_event(struct ceph_osd_client *osdc,
  1646. void (*event_cb)(u64, u64, u8, void *),
  1647. void *data, struct ceph_osd_event **pevent)
  1648. {
  1649. struct ceph_osd_event *event;
  1650. event = kmalloc(sizeof(*event), GFP_NOIO);
  1651. if (!event)
  1652. return -ENOMEM;
  1653. dout("create_event %p\n", event);
  1654. event->cb = event_cb;
  1655. event->one_shot = 0;
  1656. event->data = data;
  1657. event->osdc = osdc;
  1658. INIT_LIST_HEAD(&event->osd_node);
  1659. RB_CLEAR_NODE(&event->node);
  1660. kref_init(&event->kref); /* one ref for us */
  1661. kref_get(&event->kref); /* one ref for the caller */
  1662. spin_lock(&osdc->event_lock);
  1663. event->cookie = ++osdc->event_count;
  1664. __insert_event(osdc, event);
  1665. spin_unlock(&osdc->event_lock);
  1666. *pevent = event;
  1667. return 0;
  1668. }
  1669. EXPORT_SYMBOL(ceph_osdc_create_event);
  1670. void ceph_osdc_cancel_event(struct ceph_osd_event *event)
  1671. {
  1672. struct ceph_osd_client *osdc = event->osdc;
  1673. dout("cancel_event %p\n", event);
  1674. spin_lock(&osdc->event_lock);
  1675. __remove_event(event);
  1676. spin_unlock(&osdc->event_lock);
  1677. ceph_osdc_put_event(event); /* caller's */
  1678. }
  1679. EXPORT_SYMBOL(ceph_osdc_cancel_event);
  1680. static void do_event_work(struct work_struct *work)
  1681. {
  1682. struct ceph_osd_event_work *event_work =
  1683. container_of(work, struct ceph_osd_event_work, work);
  1684. struct ceph_osd_event *event = event_work->event;
  1685. u64 ver = event_work->ver;
  1686. u64 notify_id = event_work->notify_id;
  1687. u8 opcode = event_work->opcode;
  1688. dout("do_event_work completing %p\n", event);
  1689. event->cb(ver, notify_id, opcode, event->data);
  1690. dout("do_event_work completed %p\n", event);
  1691. ceph_osdc_put_event(event);
  1692. kfree(event_work);
  1693. }
  1694. /*
  1695. * Process osd watch notifications
  1696. */
  1697. static void handle_watch_notify(struct ceph_osd_client *osdc,
  1698. struct ceph_msg *msg)
  1699. {
  1700. void *p, *end;
  1701. u8 proto_ver;
  1702. u64 cookie, ver, notify_id;
  1703. u8 opcode;
  1704. struct ceph_osd_event *event;
  1705. struct ceph_osd_event_work *event_work;
  1706. p = msg->front.iov_base;
  1707. end = p + msg->front.iov_len;
  1708. ceph_decode_8_safe(&p, end, proto_ver, bad);
  1709. ceph_decode_8_safe(&p, end, opcode, bad);
  1710. ceph_decode_64_safe(&p, end, cookie, bad);
  1711. ceph_decode_64_safe(&p, end, ver, bad);
  1712. ceph_decode_64_safe(&p, end, notify_id, bad);
  1713. spin_lock(&osdc->event_lock);
  1714. event = __find_event(osdc, cookie);
  1715. if (event) {
  1716. BUG_ON(event->one_shot);
  1717. get_event(event);
  1718. }
  1719. spin_unlock(&osdc->event_lock);
  1720. dout("handle_watch_notify cookie %lld ver %lld event %p\n",
  1721. cookie, ver, event);
  1722. if (event) {
  1723. event_work = kmalloc(sizeof(*event_work), GFP_NOIO);
  1724. if (!event_work) {
  1725. dout("ERROR: could not allocate event_work\n");
  1726. goto done_err;
  1727. }
  1728. INIT_WORK(&event_work->work, do_event_work);
  1729. event_work->event = event;
  1730. event_work->ver = ver;
  1731. event_work->notify_id = notify_id;
  1732. event_work->opcode = opcode;
  1733. if (!queue_work(osdc->notify_wq, &event_work->work)) {
  1734. dout("WARNING: failed to queue notify event work\n");
  1735. goto done_err;
  1736. }
  1737. }
  1738. return;
  1739. done_err:
  1740. ceph_osdc_put_event(event);
  1741. return;
  1742. bad:
  1743. pr_err("osdc handle_watch_notify corrupt msg\n");
  1744. return;
  1745. }
  1746. /*
  1747. * build new request AND message
  1748. *
  1749. */
  1750. void ceph_osdc_build_request(struct ceph_osd_request *req, u64 off,
  1751. struct ceph_snap_context *snapc, u64 snap_id,
  1752. struct timespec *mtime)
  1753. {
  1754. struct ceph_msg *msg = req->r_request;
  1755. void *p;
  1756. size_t msg_size;
  1757. int flags = req->r_flags;
  1758. u64 data_len;
  1759. unsigned int i;
  1760. req->r_snapid = snap_id;
  1761. req->r_snapc = ceph_get_snap_context(snapc);
  1762. /* encode request */
  1763. msg->hdr.version = cpu_to_le16(4);
  1764. p = msg->front.iov_base;
  1765. ceph_encode_32(&p, 1); /* client_inc is always 1 */
  1766. req->r_request_osdmap_epoch = p;
  1767. p += 4;
  1768. req->r_request_flags = p;
  1769. p += 4;
  1770. if (req->r_flags & CEPH_OSD_FLAG_WRITE)
  1771. ceph_encode_timespec(p, mtime);
  1772. p += sizeof(struct ceph_timespec);
  1773. req->r_request_reassert_version = p;
  1774. p += sizeof(struct ceph_eversion); /* will get filled in */
  1775. /* oloc */
  1776. ceph_encode_8(&p, 4);
  1777. ceph_encode_8(&p, 4);
  1778. ceph_encode_32(&p, 8 + 4 + 4);
  1779. req->r_request_pool = p;
  1780. p += 8;
  1781. ceph_encode_32(&p, -1); /* preferred */
  1782. ceph_encode_32(&p, 0); /* key len */
  1783. ceph_encode_8(&p, 1);
  1784. req->r_request_pgid = p;
  1785. p += 8 + 4;
  1786. ceph_encode_32(&p, -1); /* preferred */
  1787. /* oid */
  1788. ceph_encode_32(&p, req->r_oid_len);
  1789. memcpy(p, req->r_oid, req->r_oid_len);
  1790. dout("oid '%.*s' len %d\n", req->r_oid_len, req->r_oid, req->r_oid_len);
  1791. p += req->r_oid_len;
  1792. /* ops--can imply data */
  1793. ceph_encode_16(&p, (u16)req->r_num_ops);
  1794. data_len = 0;
  1795. for (i = 0; i < req->r_num_ops; i++) {
  1796. data_len += osd_req_encode_op(req, p, i);
  1797. p += sizeof(struct ceph_osd_op);
  1798. }
  1799. /* snaps */
  1800. ceph_encode_64(&p, req->r_snapid);
  1801. ceph_encode_64(&p, req->r_snapc ? req->r_snapc->seq : 0);
  1802. ceph_encode_32(&p, req->r_snapc ? req->r_snapc->num_snaps : 0);
  1803. if (req->r_snapc) {
  1804. for (i = 0; i < snapc->num_snaps; i++) {
  1805. ceph_encode_64(&p, req->r_snapc->snaps[i]);
  1806. }
  1807. }
  1808. req->r_request_attempts = p;
  1809. p += 4;
  1810. /* data */
  1811. if (flags & CEPH_OSD_FLAG_WRITE) {
  1812. u16 data_off;
  1813. /*
  1814. * The header "data_off" is a hint to the receiver
  1815. * allowing it to align received data into its
  1816. * buffers such that there's no need to re-copy
  1817. * it before writing it to disk (direct I/O).
  1818. */
  1819. data_off = (u16) (off & 0xffff);
  1820. req->r_request->hdr.data_off = cpu_to_le16(data_off);
  1821. }
  1822. req->r_request->hdr.data_len = cpu_to_le32(data_len);
  1823. BUG_ON(p > msg->front.iov_base + msg->front.iov_len);
  1824. msg_size = p - msg->front.iov_base;
  1825. msg->front.iov_len = msg_size;
  1826. msg->hdr.front_len = cpu_to_le32(msg_size);
  1827. dout("build_request msg_size was %d\n", (int)msg_size);
  1828. }
  1829. EXPORT_SYMBOL(ceph_osdc_build_request);
  1830. /*
  1831. * Register request, send initial attempt.
  1832. */
  1833. int ceph_osdc_start_request(struct ceph_osd_client *osdc,
  1834. struct ceph_osd_request *req,
  1835. bool nofail)
  1836. {
  1837. int rc = 0;
  1838. down_read(&osdc->map_sem);
  1839. mutex_lock(&osdc->request_mutex);
  1840. __register_request(osdc, req);
  1841. WARN_ON(req->r_sent);
  1842. rc = __map_request(osdc, req, 0);
  1843. if (rc < 0) {
  1844. if (nofail) {
  1845. dout("osdc_start_request failed map, "
  1846. " will retry %lld\n", req->r_tid);
  1847. rc = 0;
  1848. }
  1849. goto out_unlock;
  1850. }
  1851. if (req->r_osd == NULL) {
  1852. dout("send_request %p no up osds in pg\n", req);
  1853. ceph_monc_request_next_osdmap(&osdc->client->monc);
  1854. } else {
  1855. __send_queued(osdc);
  1856. }
  1857. rc = 0;
  1858. out_unlock:
  1859. mutex_unlock(&osdc->request_mutex);
  1860. up_read(&osdc->map_sem);
  1861. return rc;
  1862. }
  1863. EXPORT_SYMBOL(ceph_osdc_start_request);
  1864. /*
  1865. * wait for a request to complete
  1866. */
  1867. int ceph_osdc_wait_request(struct ceph_osd_client *osdc,
  1868. struct ceph_osd_request *req)
  1869. {
  1870. int rc;
  1871. rc = wait_for_completion_interruptible(&req->r_completion);
  1872. if (rc < 0) {
  1873. mutex_lock(&osdc->request_mutex);
  1874. __cancel_request(req);
  1875. __unregister_request(osdc, req);
  1876. mutex_unlock(&osdc->request_mutex);
  1877. complete_request(req);
  1878. dout("wait_request tid %llu canceled/timed out\n", req->r_tid);
  1879. return rc;
  1880. }
  1881. dout("wait_request tid %llu result %d\n", req->r_tid, req->r_result);
  1882. return req->r_result;
  1883. }
  1884. EXPORT_SYMBOL(ceph_osdc_wait_request);
  1885. /*
  1886. * sync - wait for all in-flight requests to flush. avoid starvation.
  1887. */
  1888. void ceph_osdc_sync(struct ceph_osd_client *osdc)
  1889. {
  1890. struct ceph_osd_request *req;
  1891. u64 last_tid, next_tid = 0;
  1892. mutex_lock(&osdc->request_mutex);
  1893. last_tid = osdc->last_tid;
  1894. while (1) {
  1895. req = __lookup_request_ge(osdc, next_tid);
  1896. if (!req)
  1897. break;
  1898. if (req->r_tid > last_tid)
  1899. break;
  1900. next_tid = req->r_tid + 1;
  1901. if ((req->r_flags & CEPH_OSD_FLAG_WRITE) == 0)
  1902. continue;
  1903. ceph_osdc_get_request(req);
  1904. mutex_unlock(&osdc->request_mutex);
  1905. dout("sync waiting on tid %llu (last is %llu)\n",
  1906. req->r_tid, last_tid);
  1907. wait_for_completion(&req->r_safe_completion);
  1908. mutex_lock(&osdc->request_mutex);
  1909. ceph_osdc_put_request(req);
  1910. }
  1911. mutex_unlock(&osdc->request_mutex);
  1912. dout("sync done (thru tid %llu)\n", last_tid);
  1913. }
  1914. EXPORT_SYMBOL(ceph_osdc_sync);
  1915. /*
  1916. * init, shutdown
  1917. */
  1918. int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client)
  1919. {
  1920. int err;
  1921. dout("init\n");
  1922. osdc->client = client;
  1923. osdc->osdmap = NULL;
  1924. init_rwsem(&osdc->map_sem);
  1925. init_completion(&osdc->map_waiters);
  1926. osdc->last_requested_map = 0;
  1927. mutex_init(&osdc->request_mutex);
  1928. osdc->last_tid = 0;
  1929. osdc->osds = RB_ROOT;
  1930. INIT_LIST_HEAD(&osdc->osd_lru);
  1931. osdc->requests = RB_ROOT;
  1932. INIT_LIST_HEAD(&osdc->req_lru);
  1933. INIT_LIST_HEAD(&osdc->req_unsent);
  1934. INIT_LIST_HEAD(&osdc->req_notarget);
  1935. INIT_LIST_HEAD(&osdc->req_linger);
  1936. osdc->num_requests = 0;
  1937. INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout);
  1938. INIT_DELAYED_WORK(&osdc->osds_timeout_work, handle_osds_timeout);
  1939. spin_lock_init(&osdc->event_lock);
  1940. osdc->event_tree = RB_ROOT;
  1941. osdc->event_count = 0;
  1942. schedule_delayed_work(&osdc->osds_timeout_work,
  1943. round_jiffies_relative(osdc->client->options->osd_idle_ttl * HZ));
  1944. err = -ENOMEM;
  1945. osdc->req_mempool = mempool_create_kmalloc_pool(10,
  1946. sizeof(struct ceph_osd_request));
  1947. if (!osdc->req_mempool)
  1948. goto out;
  1949. err = ceph_msgpool_init(&osdc->msgpool_op, CEPH_MSG_OSD_OP,
  1950. OSD_OP_FRONT_LEN, 10, true,
  1951. "osd_op");
  1952. if (err < 0)
  1953. goto out_mempool;
  1954. err = ceph_msgpool_init(&osdc->msgpool_op_reply, CEPH_MSG_OSD_OPREPLY,
  1955. OSD_OPREPLY_FRONT_LEN, 10, true,
  1956. "osd_op_reply");
  1957. if (err < 0)
  1958. goto out_msgpool;
  1959. osdc->notify_wq = create_singlethread_workqueue("ceph-watch-notify");
  1960. if (IS_ERR(osdc->notify_wq)) {
  1961. err = PTR_ERR(osdc->notify_wq);
  1962. osdc->notify_wq = NULL;
  1963. goto out_msgpool;
  1964. }
  1965. return 0;
  1966. out_msgpool:
  1967. ceph_msgpool_destroy(&osdc->msgpool_op);
  1968. out_mempool:
  1969. mempool_destroy(osdc->req_mempool);
  1970. out:
  1971. return err;
  1972. }
  1973. void ceph_osdc_stop(struct ceph_osd_client *osdc)
  1974. {
  1975. flush_workqueue(osdc->notify_wq);
  1976. destroy_workqueue(osdc->notify_wq);
  1977. cancel_delayed_work_sync(&osdc->timeout_work);
  1978. cancel_delayed_work_sync(&osdc->osds_timeout_work);
  1979. if (osdc->osdmap) {
  1980. ceph_osdmap_destroy(osdc->osdmap);
  1981. osdc->osdmap = NULL;
  1982. }
  1983. remove_all_osds(osdc);
  1984. mempool_destroy(osdc->req_mempool);
  1985. ceph_msgpool_destroy(&osdc->msgpool_op);
  1986. ceph_msgpool_destroy(&osdc->msgpool_op_reply);
  1987. }
  1988. /*
  1989. * Read some contiguous pages. If we cross a stripe boundary, shorten
  1990. * *plen. Return number of bytes read, or error.
  1991. */
  1992. int ceph_osdc_readpages(struct ceph_osd_client *osdc,
  1993. struct ceph_vino vino, struct ceph_file_layout *layout,
  1994. u64 off, u64 *plen,
  1995. u32 truncate_seq, u64 truncate_size,
  1996. struct page **pages, int num_pages, int page_align)
  1997. {
  1998. struct ceph_osd_request *req;
  1999. int rc = 0;
  2000. dout("readpages on ino %llx.%llx on %llu~%llu\n", vino.ino,
  2001. vino.snap, off, *plen);
  2002. req = ceph_osdc_new_request(osdc, layout, vino, off, plen, 1,
  2003. CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ,
  2004. NULL, truncate_seq, truncate_size,
  2005. false);
  2006. if (IS_ERR(req))
  2007. return PTR_ERR(req);
  2008. /* it may be a short read due to an object boundary */
  2009. osd_req_op_extent_osd_data_pages(req, 0,
  2010. pages, *plen, page_align, false, false);
  2011. dout("readpages final extent is %llu~%llu (%llu bytes align %d)\n",
  2012. off, *plen, *plen, page_align);
  2013. ceph_osdc_build_request(req, off, NULL, vino.snap, NULL);
  2014. rc = ceph_osdc_start_request(osdc, req, false);
  2015. if (!rc)
  2016. rc = ceph_osdc_wait_request(osdc, req);
  2017. ceph_osdc_put_request(req);
  2018. dout("readpages result %d\n", rc);
  2019. return rc;
  2020. }
  2021. EXPORT_SYMBOL(ceph_osdc_readpages);
  2022. /*
  2023. * do a synchronous write on N pages
  2024. */
  2025. int ceph_osdc_writepages(struct ceph_osd_client *osdc, struct ceph_vino vino,
  2026. struct ceph_file_layout *layout,
  2027. struct ceph_snap_context *snapc,
  2028. u64 off, u64 len,
  2029. u32 truncate_seq, u64 truncate_size,
  2030. struct timespec *mtime,
  2031. struct page **pages, int num_pages)
  2032. {
  2033. struct ceph_osd_request *req;
  2034. int rc = 0;
  2035. int page_align = off & ~PAGE_MASK;
  2036. BUG_ON(vino.snap != CEPH_NOSNAP); /* snapshots aren't writeable */
  2037. req = ceph_osdc_new_request(osdc, layout, vino, off, &len, 1,
  2038. CEPH_OSD_OP_WRITE,
  2039. CEPH_OSD_FLAG_ONDISK | CEPH_OSD_FLAG_WRITE,
  2040. snapc, truncate_seq, truncate_size,
  2041. true);
  2042. if (IS_ERR(req))
  2043. return PTR_ERR(req);
  2044. /* it may be a short write due to an object boundary */
  2045. osd_req_op_extent_osd_data_pages(req, 0, pages, len, page_align,
  2046. false, false);
  2047. dout("writepages %llu~%llu (%llu bytes)\n", off, len, len);
  2048. ceph_osdc_build_request(req, off, snapc, CEPH_NOSNAP, mtime);
  2049. rc = ceph_osdc_start_request(osdc, req, true);
  2050. if (!rc)
  2051. rc = ceph_osdc_wait_request(osdc, req);
  2052. ceph_osdc_put_request(req);
  2053. if (rc == 0)
  2054. rc = len;
  2055. dout("writepages result %d\n", rc);
  2056. return rc;
  2057. }
  2058. EXPORT_SYMBOL(ceph_osdc_writepages);
  2059. /*
  2060. * handle incoming message
  2061. */
  2062. static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
  2063. {
  2064. struct ceph_osd *osd = con->private;
  2065. struct ceph_osd_client *osdc;
  2066. int type = le16_to_cpu(msg->hdr.type);
  2067. if (!osd)
  2068. goto out;
  2069. osdc = osd->o_osdc;
  2070. switch (type) {
  2071. case CEPH_MSG_OSD_MAP:
  2072. ceph_osdc_handle_map(osdc, msg);
  2073. break;
  2074. case CEPH_MSG_OSD_OPREPLY:
  2075. handle_reply(osdc, msg, con);
  2076. break;
  2077. case CEPH_MSG_WATCH_NOTIFY:
  2078. handle_watch_notify(osdc, msg);
  2079. break;
  2080. default:
  2081. pr_err("received unknown message type %d %s\n", type,
  2082. ceph_msg_type_name(type));
  2083. }
  2084. out:
  2085. ceph_msg_put(msg);
  2086. }
  2087. /*
  2088. * lookup and return message for incoming reply. set up reply message
  2089. * pages.
  2090. */
  2091. static struct ceph_msg *get_reply(struct ceph_connection *con,
  2092. struct ceph_msg_header *hdr,
  2093. int *skip)
  2094. {
  2095. struct ceph_osd *osd = con->private;
  2096. struct ceph_osd_client *osdc = osd->o_osdc;
  2097. struct ceph_msg *m;
  2098. struct ceph_osd_request *req;
  2099. int front = le32_to_cpu(hdr->front_len);
  2100. int data_len = le32_to_cpu(hdr->data_len);
  2101. u64 tid;
  2102. tid = le64_to_cpu(hdr->tid);
  2103. mutex_lock(&osdc->request_mutex);
  2104. req = __lookup_request(osdc, tid);
  2105. if (!req) {
  2106. *skip = 1;
  2107. m = NULL;
  2108. dout("get_reply unknown tid %llu from osd%d\n", tid,
  2109. osd->o_osd);
  2110. goto out;
  2111. }
  2112. if (req->r_reply->con)
  2113. dout("%s revoking msg %p from old con %p\n", __func__,
  2114. req->r_reply, req->r_reply->con);
  2115. ceph_msg_revoke_incoming(req->r_reply);
  2116. if (front > req->r_reply->front.iov_len) {
  2117. pr_warning("get_reply front %d > preallocated %d\n",
  2118. front, (int)req->r_reply->front.iov_len);
  2119. m = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, front, GFP_NOFS, false);
  2120. if (!m)
  2121. goto out;
  2122. ceph_msg_put(req->r_reply);
  2123. req->r_reply = m;
  2124. }
  2125. m = ceph_msg_get(req->r_reply);
  2126. if (data_len > 0) {
  2127. struct ceph_osd_data *osd_data;
  2128. /*
  2129. * XXX This is assuming there is only one op containing
  2130. * XXX page data. Probably OK for reads, but this
  2131. * XXX ought to be done more generally.
  2132. */
  2133. osd_data = osd_req_op_extent_osd_data(req, 0);
  2134. if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES) {
  2135. if (osd_data->pages &&
  2136. unlikely(osd_data->length < data_len)) {
  2137. pr_warning("tid %lld reply has %d bytes "
  2138. "we had only %llu bytes ready\n",
  2139. tid, data_len, osd_data->length);
  2140. *skip = 1;
  2141. ceph_msg_put(m);
  2142. m = NULL;
  2143. goto out;
  2144. }
  2145. }
  2146. }
  2147. *skip = 0;
  2148. dout("get_reply tid %lld %p\n", tid, m);
  2149. out:
  2150. mutex_unlock(&osdc->request_mutex);
  2151. return m;
  2152. }
  2153. static struct ceph_msg *alloc_msg(struct ceph_connection *con,
  2154. struct ceph_msg_header *hdr,
  2155. int *skip)
  2156. {
  2157. struct ceph_osd *osd = con->private;
  2158. int type = le16_to_cpu(hdr->type);
  2159. int front = le32_to_cpu(hdr->front_len);
  2160. *skip = 0;
  2161. switch (type) {
  2162. case CEPH_MSG_OSD_MAP:
  2163. case CEPH_MSG_WATCH_NOTIFY:
  2164. return ceph_msg_new(type, front, GFP_NOFS, false);
  2165. case CEPH_MSG_OSD_OPREPLY:
  2166. return get_reply(con, hdr, skip);
  2167. default:
  2168. pr_info("alloc_msg unexpected msg type %d from osd%d\n", type,
  2169. osd->o_osd);
  2170. *skip = 1;
  2171. return NULL;
  2172. }
  2173. }
  2174. /*
  2175. * Wrappers to refcount containing ceph_osd struct
  2176. */
  2177. static struct ceph_connection *get_osd_con(struct ceph_connection *con)
  2178. {
  2179. struct ceph_osd *osd = con->private;
  2180. if (get_osd(osd))
  2181. return con;
  2182. return NULL;
  2183. }
  2184. static void put_osd_con(struct ceph_connection *con)
  2185. {
  2186. struct ceph_osd *osd = con->private;
  2187. put_osd(osd);
  2188. }
  2189. /*
  2190. * authentication
  2191. */
  2192. /*
  2193. * Note: returned pointer is the address of a structure that's
  2194. * managed separately. Caller must *not* attempt to free it.
  2195. */
  2196. static struct ceph_auth_handshake *get_authorizer(struct ceph_connection *con,
  2197. int *proto, int force_new)
  2198. {
  2199. struct ceph_osd *o = con->private;
  2200. struct ceph_osd_client *osdc = o->o_osdc;
  2201. struct ceph_auth_client *ac = osdc->client->monc.auth;
  2202. struct ceph_auth_handshake *auth = &o->o_auth;
  2203. if (force_new && auth->authorizer) {
  2204. ceph_auth_destroy_authorizer(ac, auth->authorizer);
  2205. auth->authorizer = NULL;
  2206. }
  2207. if (!auth->authorizer) {
  2208. int ret = ceph_auth_create_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
  2209. auth);
  2210. if (ret)
  2211. return ERR_PTR(ret);
  2212. } else {
  2213. int ret = ceph_auth_update_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
  2214. auth);
  2215. if (ret)
  2216. return ERR_PTR(ret);
  2217. }
  2218. *proto = ac->protocol;
  2219. return auth;
  2220. }
  2221. static int verify_authorizer_reply(struct ceph_connection *con, int len)
  2222. {
  2223. struct ceph_osd *o = con->private;
  2224. struct ceph_osd_client *osdc = o->o_osdc;
  2225. struct ceph_auth_client *ac = osdc->client->monc.auth;
  2226. return ceph_auth_verify_authorizer_reply(ac, o->o_auth.authorizer, len);
  2227. }
  2228. static int invalidate_authorizer(struct ceph_connection *con)
  2229. {
  2230. struct ceph_osd *o = con->private;
  2231. struct ceph_osd_client *osdc = o->o_osdc;
  2232. struct ceph_auth_client *ac = osdc->client->monc.auth;
  2233. ceph_auth_invalidate_authorizer(ac, CEPH_ENTITY_TYPE_OSD);
  2234. return ceph_monc_validate_auth(&osdc->client->monc);
  2235. }
  2236. static const struct ceph_connection_operations osd_con_ops = {
  2237. .get = get_osd_con,
  2238. .put = put_osd_con,
  2239. .dispatch = dispatch,
  2240. .get_authorizer = get_authorizer,
  2241. .verify_authorizer_reply = verify_authorizer_reply,
  2242. .invalidate_authorizer = invalidate_authorizer,
  2243. .alloc_msg = alloc_msg,
  2244. .fault = osd_reset,
  2245. };