osd_client.c 63 KB

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