osd_client.c 62 KB

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