osd_client.c 68 KB

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