osd_client.c 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112
  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. static int op_has_extent(int op)
  30. {
  31. return (op == CEPH_OSD_OP_READ ||
  32. op == CEPH_OSD_OP_WRITE);
  33. }
  34. int ceph_calc_raw_layout(struct ceph_file_layout *layout,
  35. u64 off, u64 *plen, u64 *bno,
  36. struct ceph_osd_request *req,
  37. struct ceph_osd_req_op *op)
  38. {
  39. u64 orig_len = *plen;
  40. u64 objoff, objlen; /* extent in object */
  41. int r;
  42. /* object extent? */
  43. r = ceph_calc_file_object_mapping(layout, off, orig_len, bno,
  44. &objoff, &objlen);
  45. if (r < 0)
  46. return r;
  47. if (objlen < orig_len) {
  48. *plen = objlen;
  49. dout(" skipping last %llu, final file extent %llu~%llu\n",
  50. orig_len - *plen, off, *plen);
  51. }
  52. if (op_has_extent(op->op)) {
  53. u32 osize = le32_to_cpu(layout->fl_object_size);
  54. op->extent.offset = objoff;
  55. op->extent.length = objlen;
  56. if (op->extent.truncate_size <= off - objoff) {
  57. op->extent.truncate_size = 0;
  58. } else {
  59. op->extent.truncate_size -= off - objoff;
  60. if (op->extent.truncate_size > osize)
  61. op->extent.truncate_size = osize;
  62. }
  63. }
  64. req->r_num_pages = calc_pages_for(off, *plen);
  65. req->r_page_alignment = off & ~PAGE_MASK;
  66. if (op->op == CEPH_OSD_OP_WRITE)
  67. op->payload_len = *plen;
  68. dout("calc_layout bno=%llx %llu~%llu (%d pages)\n",
  69. *bno, objoff, objlen, req->r_num_pages);
  70. return 0;
  71. }
  72. EXPORT_SYMBOL(ceph_calc_raw_layout);
  73. /*
  74. * Implement client access to distributed object storage cluster.
  75. *
  76. * All data objects are stored within a cluster/cloud of OSDs, or
  77. * "object storage devices." (Note that Ceph OSDs have _nothing_ to
  78. * do with the T10 OSD extensions to SCSI.) Ceph OSDs are simply
  79. * remote daemons serving up and coordinating consistent and safe
  80. * access to storage.
  81. *
  82. * Cluster membership and the mapping of data objects onto storage devices
  83. * are described by the osd map.
  84. *
  85. * We keep track of pending OSD requests (read, write), resubmit
  86. * requests to different OSDs when the cluster topology/data layout
  87. * change, or retry the affected requests when the communications
  88. * channel with an OSD is reset.
  89. */
  90. /*
  91. * calculate the mapping of a file extent onto an object, and fill out the
  92. * request accordingly. shorten extent as necessary if it crosses an
  93. * object boundary.
  94. *
  95. * fill osd op in request message.
  96. */
  97. static int calc_layout(struct ceph_vino vino,
  98. struct ceph_file_layout *layout,
  99. u64 off, u64 *plen,
  100. struct ceph_osd_request *req,
  101. struct ceph_osd_req_op *op)
  102. {
  103. u64 bno;
  104. int r;
  105. r = ceph_calc_raw_layout(layout, off, plen, &bno, req, op);
  106. if (r < 0)
  107. return r;
  108. snprintf(req->r_oid, sizeof(req->r_oid), "%llx.%08llx", vino.ino, bno);
  109. req->r_oid_len = strlen(req->r_oid);
  110. return r;
  111. }
  112. /*
  113. * requests
  114. */
  115. void ceph_osdc_release_request(struct kref *kref)
  116. {
  117. struct ceph_osd_request *req = container_of(kref,
  118. struct ceph_osd_request,
  119. r_kref);
  120. if (req->r_request)
  121. ceph_msg_put(req->r_request);
  122. if (req->r_con_filling_msg) {
  123. dout("%s revoking msg %p from con %p\n", __func__,
  124. req->r_reply, req->r_con_filling_msg);
  125. ceph_msg_revoke_incoming(req->r_reply);
  126. req->r_con_filling_msg->ops->put(req->r_con_filling_msg);
  127. req->r_con_filling_msg = NULL;
  128. }
  129. if (req->r_reply)
  130. ceph_msg_put(req->r_reply);
  131. if (req->r_own_pages)
  132. ceph_release_page_vector(req->r_pages,
  133. req->r_num_pages);
  134. ceph_put_snap_context(req->r_snapc);
  135. ceph_pagelist_release(&req->r_trail);
  136. if (req->r_mempool)
  137. mempool_free(req, req->r_osdc->req_mempool);
  138. else
  139. kfree(req);
  140. }
  141. EXPORT_SYMBOL(ceph_osdc_release_request);
  142. struct ceph_osd_request *ceph_osdc_alloc_request(struct ceph_osd_client *osdc,
  143. struct ceph_snap_context *snapc,
  144. unsigned int num_op,
  145. bool use_mempool,
  146. gfp_t gfp_flags)
  147. {
  148. struct ceph_osd_request *req;
  149. struct ceph_msg *msg;
  150. size_t msg_size = sizeof(struct ceph_osd_request_head);
  151. msg_size += num_op*sizeof(struct ceph_osd_op);
  152. if (use_mempool) {
  153. req = mempool_alloc(osdc->req_mempool, gfp_flags);
  154. memset(req, 0, sizeof(*req));
  155. } else {
  156. req = kzalloc(sizeof(*req), gfp_flags);
  157. }
  158. if (req == NULL)
  159. return NULL;
  160. req->r_osdc = osdc;
  161. req->r_mempool = use_mempool;
  162. kref_init(&req->r_kref);
  163. init_completion(&req->r_completion);
  164. init_completion(&req->r_safe_completion);
  165. RB_CLEAR_NODE(&req->r_node);
  166. INIT_LIST_HEAD(&req->r_unsafe_item);
  167. INIT_LIST_HEAD(&req->r_linger_item);
  168. INIT_LIST_HEAD(&req->r_linger_osd);
  169. INIT_LIST_HEAD(&req->r_req_lru_item);
  170. INIT_LIST_HEAD(&req->r_osd_item);
  171. /* create reply message */
  172. if (use_mempool)
  173. msg = ceph_msgpool_get(&osdc->msgpool_op_reply, 0);
  174. else
  175. msg = ceph_msg_new(CEPH_MSG_OSD_OPREPLY,
  176. OSD_OPREPLY_FRONT_LEN, gfp_flags, true);
  177. if (!msg) {
  178. ceph_osdc_put_request(req);
  179. return NULL;
  180. }
  181. req->r_reply = msg;
  182. ceph_pagelist_init(&req->r_trail);
  183. /* create request message; allow space for oid */
  184. msg_size += MAX_OBJ_NAME_SIZE;
  185. if (snapc)
  186. msg_size += sizeof(u64) * snapc->num_snaps;
  187. if (use_mempool)
  188. msg = ceph_msgpool_get(&osdc->msgpool_op, 0);
  189. else
  190. msg = ceph_msg_new(CEPH_MSG_OSD_OP, msg_size, gfp_flags, true);
  191. if (!msg) {
  192. ceph_osdc_put_request(req);
  193. return NULL;
  194. }
  195. memset(msg->front.iov_base, 0, msg->front.iov_len);
  196. req->r_request = msg;
  197. return req;
  198. }
  199. EXPORT_SYMBOL(ceph_osdc_alloc_request);
  200. static void osd_req_encode_op(struct ceph_osd_request *req,
  201. struct ceph_osd_op *dst,
  202. struct ceph_osd_req_op *src)
  203. {
  204. dst->op = cpu_to_le16(src->op);
  205. switch (src->op) {
  206. case CEPH_OSD_OP_READ:
  207. case CEPH_OSD_OP_WRITE:
  208. dst->extent.offset =
  209. cpu_to_le64(src->extent.offset);
  210. dst->extent.length =
  211. cpu_to_le64(src->extent.length);
  212. dst->extent.truncate_size =
  213. cpu_to_le64(src->extent.truncate_size);
  214. dst->extent.truncate_seq =
  215. cpu_to_le32(src->extent.truncate_seq);
  216. break;
  217. case CEPH_OSD_OP_GETXATTR:
  218. case CEPH_OSD_OP_SETXATTR:
  219. case CEPH_OSD_OP_CMPXATTR:
  220. dst->xattr.name_len = cpu_to_le32(src->xattr.name_len);
  221. dst->xattr.value_len = cpu_to_le32(src->xattr.value_len);
  222. dst->xattr.cmp_op = src->xattr.cmp_op;
  223. dst->xattr.cmp_mode = src->xattr.cmp_mode;
  224. ceph_pagelist_append(&req->r_trail, src->xattr.name,
  225. src->xattr.name_len);
  226. ceph_pagelist_append(&req->r_trail, src->xattr.val,
  227. src->xattr.value_len);
  228. break;
  229. case CEPH_OSD_OP_CALL:
  230. dst->cls.class_len = src->cls.class_len;
  231. dst->cls.method_len = src->cls.method_len;
  232. dst->cls.indata_len = cpu_to_le32(src->cls.indata_len);
  233. ceph_pagelist_append(&req->r_trail, src->cls.class_name,
  234. src->cls.class_len);
  235. ceph_pagelist_append(&req->r_trail, src->cls.method_name,
  236. src->cls.method_len);
  237. ceph_pagelist_append(&req->r_trail, src->cls.indata,
  238. src->cls.indata_len);
  239. break;
  240. case CEPH_OSD_OP_ROLLBACK:
  241. dst->snap.snapid = cpu_to_le64(src->snap.snapid);
  242. break;
  243. case CEPH_OSD_OP_STARTSYNC:
  244. break;
  245. case CEPH_OSD_OP_NOTIFY:
  246. {
  247. __le32 prot_ver = cpu_to_le32(src->watch.prot_ver);
  248. __le32 timeout = cpu_to_le32(src->watch.timeout);
  249. ceph_pagelist_append(&req->r_trail,
  250. &prot_ver, sizeof(prot_ver));
  251. ceph_pagelist_append(&req->r_trail,
  252. &timeout, sizeof(timeout));
  253. }
  254. case CEPH_OSD_OP_NOTIFY_ACK:
  255. case CEPH_OSD_OP_WATCH:
  256. dst->watch.cookie = cpu_to_le64(src->watch.cookie);
  257. dst->watch.ver = cpu_to_le64(src->watch.ver);
  258. dst->watch.flag = src->watch.flag;
  259. break;
  260. default:
  261. pr_err("unrecognized osd opcode %d\n", dst->op);
  262. WARN_ON(1);
  263. break;
  264. }
  265. dst->payload_len = cpu_to_le32(src->payload_len);
  266. }
  267. /*
  268. * build new request AND message
  269. *
  270. */
  271. void ceph_osdc_build_request(struct ceph_osd_request *req,
  272. u64 off, u64 len, unsigned int num_op,
  273. struct ceph_osd_req_op *src_ops,
  274. struct ceph_snap_context *snapc, u64 snap_id,
  275. struct timespec *mtime)
  276. {
  277. struct ceph_msg *msg = req->r_request;
  278. struct ceph_osd_request_head *head;
  279. struct ceph_osd_req_op *src_op;
  280. struct ceph_osd_op *op;
  281. void *p;
  282. size_t msg_size = sizeof(*head) + num_op*sizeof(*op);
  283. int flags = req->r_flags;
  284. u64 data_len = 0;
  285. int i;
  286. WARN_ON((flags & (CEPH_OSD_FLAG_READ|CEPH_OSD_FLAG_WRITE)) == 0);
  287. head = msg->front.iov_base;
  288. head->snapid = cpu_to_le64(snap_id);
  289. op = (void *)(head + 1);
  290. p = (void *)(op + num_op);
  291. req->r_snapc = ceph_get_snap_context(snapc);
  292. head->client_inc = cpu_to_le32(1); /* always, for now. */
  293. head->flags = cpu_to_le32(flags);
  294. if (flags & CEPH_OSD_FLAG_WRITE)
  295. ceph_encode_timespec(&head->mtime, mtime);
  296. BUG_ON(num_op > (unsigned int) ((u16) -1));
  297. head->num_ops = cpu_to_le16(num_op);
  298. /* fill in oid */
  299. head->object_len = cpu_to_le32(req->r_oid_len);
  300. memcpy(p, req->r_oid, req->r_oid_len);
  301. p += req->r_oid_len;
  302. src_op = src_ops;
  303. while (num_op--)
  304. osd_req_encode_op(req, op++, src_op++);
  305. data_len += req->r_trail.length;
  306. if (snapc) {
  307. head->snap_seq = cpu_to_le64(snapc->seq);
  308. head->num_snaps = cpu_to_le32(snapc->num_snaps);
  309. for (i = 0; i < snapc->num_snaps; i++) {
  310. put_unaligned_le64(snapc->snaps[i], p);
  311. p += sizeof(u64);
  312. }
  313. }
  314. if (flags & CEPH_OSD_FLAG_WRITE) {
  315. req->r_request->hdr.data_off = cpu_to_le16(off);
  316. req->r_request->hdr.data_len = cpu_to_le32(len + data_len);
  317. } else if (data_len) {
  318. req->r_request->hdr.data_off = 0;
  319. req->r_request->hdr.data_len = cpu_to_le32(data_len);
  320. }
  321. req->r_request->page_alignment = req->r_page_alignment;
  322. BUG_ON(p > msg->front.iov_base + msg->front.iov_len);
  323. msg_size = p - msg->front.iov_base;
  324. msg->front.iov_len = msg_size;
  325. msg->hdr.front_len = cpu_to_le32(msg_size);
  326. return;
  327. }
  328. EXPORT_SYMBOL(ceph_osdc_build_request);
  329. /*
  330. * build new request AND message, calculate layout, and adjust file
  331. * extent as needed.
  332. *
  333. * if the file was recently truncated, we include information about its
  334. * old and new size so that the object can be updated appropriately. (we
  335. * avoid synchronously deleting truncated objects because it's slow.)
  336. *
  337. * if @do_sync, include a 'startsync' command so that the osd will flush
  338. * data quickly.
  339. */
  340. struct ceph_osd_request *ceph_osdc_new_request(struct ceph_osd_client *osdc,
  341. struct ceph_file_layout *layout,
  342. struct ceph_vino vino,
  343. u64 off, u64 *plen,
  344. int opcode, int flags,
  345. struct ceph_snap_context *snapc,
  346. int do_sync,
  347. u32 truncate_seq,
  348. u64 truncate_size,
  349. struct timespec *mtime,
  350. bool use_mempool,
  351. int page_align)
  352. {
  353. struct ceph_osd_req_op ops[2];
  354. struct ceph_osd_request *req;
  355. unsigned int num_op = 1;
  356. int r;
  357. memset(&ops, 0, sizeof ops);
  358. ops[0].op = opcode;
  359. ops[0].extent.truncate_seq = truncate_seq;
  360. ops[0].extent.truncate_size = truncate_size;
  361. if (do_sync) {
  362. ops[1].op = CEPH_OSD_OP_STARTSYNC;
  363. num_op++;
  364. }
  365. req = ceph_osdc_alloc_request(osdc, snapc, num_op, use_mempool,
  366. GFP_NOFS);
  367. if (!req)
  368. return ERR_PTR(-ENOMEM);
  369. req->r_flags = flags;
  370. /* calculate max write size */
  371. r = calc_layout(vino, layout, off, plen, req, ops);
  372. if (r < 0)
  373. return ERR_PTR(r);
  374. req->r_file_layout = *layout; /* keep a copy */
  375. /* in case it differs from natural (file) alignment that
  376. calc_layout filled in for us */
  377. req->r_num_pages = calc_pages_for(page_align, *plen);
  378. req->r_page_alignment = page_align;
  379. ceph_osdc_build_request(req, off, *plen, num_op, ops,
  380. snapc, vino.snap, mtime);
  381. return req;
  382. }
  383. EXPORT_SYMBOL(ceph_osdc_new_request);
  384. /*
  385. * We keep osd requests in an rbtree, sorted by ->r_tid.
  386. */
  387. static void __insert_request(struct ceph_osd_client *osdc,
  388. struct ceph_osd_request *new)
  389. {
  390. struct rb_node **p = &osdc->requests.rb_node;
  391. struct rb_node *parent = NULL;
  392. struct ceph_osd_request *req = NULL;
  393. while (*p) {
  394. parent = *p;
  395. req = rb_entry(parent, struct ceph_osd_request, r_node);
  396. if (new->r_tid < req->r_tid)
  397. p = &(*p)->rb_left;
  398. else if (new->r_tid > req->r_tid)
  399. p = &(*p)->rb_right;
  400. else
  401. BUG();
  402. }
  403. rb_link_node(&new->r_node, parent, p);
  404. rb_insert_color(&new->r_node, &osdc->requests);
  405. }
  406. static struct ceph_osd_request *__lookup_request(struct ceph_osd_client *osdc,
  407. u64 tid)
  408. {
  409. struct ceph_osd_request *req;
  410. struct rb_node *n = osdc->requests.rb_node;
  411. while (n) {
  412. req = rb_entry(n, struct ceph_osd_request, r_node);
  413. if (tid < req->r_tid)
  414. n = n->rb_left;
  415. else if (tid > req->r_tid)
  416. n = n->rb_right;
  417. else
  418. return req;
  419. }
  420. return NULL;
  421. }
  422. static struct ceph_osd_request *
  423. __lookup_request_ge(struct ceph_osd_client *osdc,
  424. u64 tid)
  425. {
  426. struct ceph_osd_request *req;
  427. struct rb_node *n = osdc->requests.rb_node;
  428. while (n) {
  429. req = rb_entry(n, struct ceph_osd_request, r_node);
  430. if (tid < req->r_tid) {
  431. if (!n->rb_left)
  432. return req;
  433. n = n->rb_left;
  434. } else if (tid > req->r_tid) {
  435. n = n->rb_right;
  436. } else {
  437. return req;
  438. }
  439. }
  440. return NULL;
  441. }
  442. /*
  443. * Resubmit requests pending on the given osd.
  444. */
  445. static void __kick_osd_requests(struct ceph_osd_client *osdc,
  446. struct ceph_osd *osd)
  447. {
  448. struct ceph_osd_request *req, *nreq;
  449. int err;
  450. dout("__kick_osd_requests osd%d\n", osd->o_osd);
  451. err = __reset_osd(osdc, osd);
  452. if (err)
  453. return;
  454. list_for_each_entry(req, &osd->o_requests, r_osd_item) {
  455. list_move(&req->r_req_lru_item, &osdc->req_unsent);
  456. dout("requeued %p tid %llu osd%d\n", req, req->r_tid,
  457. osd->o_osd);
  458. if (!req->r_linger)
  459. req->r_flags |= CEPH_OSD_FLAG_RETRY;
  460. }
  461. list_for_each_entry_safe(req, nreq, &osd->o_linger_requests,
  462. r_linger_osd) {
  463. /*
  464. * reregister request prior to unregistering linger so
  465. * that r_osd is preserved.
  466. */
  467. BUG_ON(!list_empty(&req->r_req_lru_item));
  468. __register_request(osdc, req);
  469. list_add(&req->r_req_lru_item, &osdc->req_unsent);
  470. list_add(&req->r_osd_item, &req->r_osd->o_requests);
  471. __unregister_linger_request(osdc, req);
  472. dout("requeued lingering %p tid %llu osd%d\n", req, req->r_tid,
  473. osd->o_osd);
  474. }
  475. }
  476. /*
  477. * If the osd connection drops, we need to resubmit all requests.
  478. */
  479. static void osd_reset(struct ceph_connection *con)
  480. {
  481. struct ceph_osd *osd = con->private;
  482. struct ceph_osd_client *osdc;
  483. if (!osd)
  484. return;
  485. dout("osd_reset osd%d\n", osd->o_osd);
  486. osdc = osd->o_osdc;
  487. down_read(&osdc->map_sem);
  488. mutex_lock(&osdc->request_mutex);
  489. __kick_osd_requests(osdc, osd);
  490. __send_queued(osdc);
  491. mutex_unlock(&osdc->request_mutex);
  492. up_read(&osdc->map_sem);
  493. }
  494. /*
  495. * Track open sessions with osds.
  496. */
  497. static struct ceph_osd *create_osd(struct ceph_osd_client *osdc, int onum)
  498. {
  499. struct ceph_osd *osd;
  500. osd = kzalloc(sizeof(*osd), GFP_NOFS);
  501. if (!osd)
  502. return NULL;
  503. atomic_set(&osd->o_ref, 1);
  504. osd->o_osdc = osdc;
  505. osd->o_osd = onum;
  506. RB_CLEAR_NODE(&osd->o_node);
  507. INIT_LIST_HEAD(&osd->o_requests);
  508. INIT_LIST_HEAD(&osd->o_linger_requests);
  509. INIT_LIST_HEAD(&osd->o_osd_lru);
  510. osd->o_incarnation = 1;
  511. ceph_con_init(&osd->o_con, osd, &osd_con_ops, &osdc->client->msgr);
  512. INIT_LIST_HEAD(&osd->o_keepalive_item);
  513. return osd;
  514. }
  515. static struct ceph_osd *get_osd(struct ceph_osd *osd)
  516. {
  517. if (atomic_inc_not_zero(&osd->o_ref)) {
  518. dout("get_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref)-1,
  519. atomic_read(&osd->o_ref));
  520. return osd;
  521. } else {
  522. dout("get_osd %p FAIL\n", osd);
  523. return NULL;
  524. }
  525. }
  526. static void put_osd(struct ceph_osd *osd)
  527. {
  528. dout("put_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref),
  529. atomic_read(&osd->o_ref) - 1);
  530. if (atomic_dec_and_test(&osd->o_ref) && osd->o_auth.authorizer) {
  531. struct ceph_auth_client *ac = osd->o_osdc->client->monc.auth;
  532. if (ac->ops && ac->ops->destroy_authorizer)
  533. ac->ops->destroy_authorizer(ac, osd->o_auth.authorizer);
  534. kfree(osd);
  535. }
  536. }
  537. /*
  538. * remove an osd from our map
  539. */
  540. static void __remove_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
  541. {
  542. dout("__remove_osd %p\n", osd);
  543. BUG_ON(!list_empty(&osd->o_requests));
  544. rb_erase(&osd->o_node, &osdc->osds);
  545. list_del_init(&osd->o_osd_lru);
  546. ceph_con_close(&osd->o_con);
  547. put_osd(osd);
  548. }
  549. static void remove_all_osds(struct ceph_osd_client *osdc)
  550. {
  551. dout("%s %p\n", __func__, osdc);
  552. mutex_lock(&osdc->request_mutex);
  553. while (!RB_EMPTY_ROOT(&osdc->osds)) {
  554. struct ceph_osd *osd = rb_entry(rb_first(&osdc->osds),
  555. struct ceph_osd, o_node);
  556. __remove_osd(osdc, osd);
  557. }
  558. mutex_unlock(&osdc->request_mutex);
  559. }
  560. static void __move_osd_to_lru(struct ceph_osd_client *osdc,
  561. struct ceph_osd *osd)
  562. {
  563. dout("__move_osd_to_lru %p\n", osd);
  564. BUG_ON(!list_empty(&osd->o_osd_lru));
  565. list_add_tail(&osd->o_osd_lru, &osdc->osd_lru);
  566. osd->lru_ttl = jiffies + osdc->client->options->osd_idle_ttl * HZ;
  567. }
  568. static void __remove_osd_from_lru(struct ceph_osd *osd)
  569. {
  570. dout("__remove_osd_from_lru %p\n", osd);
  571. if (!list_empty(&osd->o_osd_lru))
  572. list_del_init(&osd->o_osd_lru);
  573. }
  574. static void remove_old_osds(struct ceph_osd_client *osdc)
  575. {
  576. struct ceph_osd *osd, *nosd;
  577. dout("__remove_old_osds %p\n", osdc);
  578. mutex_lock(&osdc->request_mutex);
  579. list_for_each_entry_safe(osd, nosd, &osdc->osd_lru, o_osd_lru) {
  580. if (time_before(jiffies, osd->lru_ttl))
  581. break;
  582. __remove_osd(osdc, osd);
  583. }
  584. mutex_unlock(&osdc->request_mutex);
  585. }
  586. /*
  587. * reset osd connect
  588. */
  589. static int __reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
  590. {
  591. struct ceph_entity_addr *peer_addr;
  592. dout("__reset_osd %p osd%d\n", osd, osd->o_osd);
  593. if (list_empty(&osd->o_requests) &&
  594. list_empty(&osd->o_linger_requests)) {
  595. __remove_osd(osdc, osd);
  596. return -ENODEV;
  597. }
  598. peer_addr = &osdc->osdmap->osd_addr[osd->o_osd];
  599. if (!memcmp(peer_addr, &osd->o_con.peer_addr, sizeof (*peer_addr)) &&
  600. !ceph_con_opened(&osd->o_con)) {
  601. struct ceph_osd_request *req;
  602. dout(" osd addr hasn't changed and connection never opened,"
  603. " letting msgr retry");
  604. /* touch each r_stamp for handle_timeout()'s benfit */
  605. list_for_each_entry(req, &osd->o_requests, r_osd_item)
  606. req->r_stamp = jiffies;
  607. return -EAGAIN;
  608. }
  609. ceph_con_close(&osd->o_con);
  610. ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd, peer_addr);
  611. osd->o_incarnation++;
  612. return 0;
  613. }
  614. static void __insert_osd(struct ceph_osd_client *osdc, struct ceph_osd *new)
  615. {
  616. struct rb_node **p = &osdc->osds.rb_node;
  617. struct rb_node *parent = NULL;
  618. struct ceph_osd *osd = NULL;
  619. dout("__insert_osd %p osd%d\n", new, new->o_osd);
  620. while (*p) {
  621. parent = *p;
  622. osd = rb_entry(parent, struct ceph_osd, o_node);
  623. if (new->o_osd < osd->o_osd)
  624. p = &(*p)->rb_left;
  625. else if (new->o_osd > osd->o_osd)
  626. p = &(*p)->rb_right;
  627. else
  628. BUG();
  629. }
  630. rb_link_node(&new->o_node, parent, p);
  631. rb_insert_color(&new->o_node, &osdc->osds);
  632. }
  633. static struct ceph_osd *__lookup_osd(struct ceph_osd_client *osdc, int o)
  634. {
  635. struct ceph_osd *osd;
  636. struct rb_node *n = osdc->osds.rb_node;
  637. while (n) {
  638. osd = rb_entry(n, struct ceph_osd, o_node);
  639. if (o < osd->o_osd)
  640. n = n->rb_left;
  641. else if (o > osd->o_osd)
  642. n = n->rb_right;
  643. else
  644. return osd;
  645. }
  646. return NULL;
  647. }
  648. static void __schedule_osd_timeout(struct ceph_osd_client *osdc)
  649. {
  650. schedule_delayed_work(&osdc->timeout_work,
  651. osdc->client->options->osd_keepalive_timeout * HZ);
  652. }
  653. static void __cancel_osd_timeout(struct ceph_osd_client *osdc)
  654. {
  655. cancel_delayed_work(&osdc->timeout_work);
  656. }
  657. /*
  658. * Register request, assign tid. If this is the first request, set up
  659. * the timeout event.
  660. */
  661. static void __register_request(struct ceph_osd_client *osdc,
  662. struct ceph_osd_request *req)
  663. {
  664. req->r_tid = ++osdc->last_tid;
  665. req->r_request->hdr.tid = cpu_to_le64(req->r_tid);
  666. dout("__register_request %p tid %lld\n", req, req->r_tid);
  667. __insert_request(osdc, req);
  668. ceph_osdc_get_request(req);
  669. osdc->num_requests++;
  670. if (osdc->num_requests == 1) {
  671. dout(" first request, scheduling timeout\n");
  672. __schedule_osd_timeout(osdc);
  673. }
  674. }
  675. static void register_request(struct ceph_osd_client *osdc,
  676. struct ceph_osd_request *req)
  677. {
  678. mutex_lock(&osdc->request_mutex);
  679. __register_request(osdc, req);
  680. mutex_unlock(&osdc->request_mutex);
  681. }
  682. /*
  683. * called under osdc->request_mutex
  684. */
  685. static void __unregister_request(struct ceph_osd_client *osdc,
  686. struct ceph_osd_request *req)
  687. {
  688. if (RB_EMPTY_NODE(&req->r_node)) {
  689. dout("__unregister_request %p tid %lld not registered\n",
  690. req, req->r_tid);
  691. return;
  692. }
  693. dout("__unregister_request %p tid %lld\n", req, req->r_tid);
  694. rb_erase(&req->r_node, &osdc->requests);
  695. osdc->num_requests--;
  696. if (req->r_osd) {
  697. /* make sure the original request isn't in flight. */
  698. ceph_msg_revoke(req->r_request);
  699. list_del_init(&req->r_osd_item);
  700. if (list_empty(&req->r_osd->o_requests) &&
  701. list_empty(&req->r_osd->o_linger_requests)) {
  702. dout("moving osd to %p lru\n", req->r_osd);
  703. __move_osd_to_lru(osdc, req->r_osd);
  704. }
  705. if (list_empty(&req->r_linger_item))
  706. req->r_osd = NULL;
  707. }
  708. list_del_init(&req->r_req_lru_item);
  709. ceph_osdc_put_request(req);
  710. if (osdc->num_requests == 0) {
  711. dout(" no requests, canceling timeout\n");
  712. __cancel_osd_timeout(osdc);
  713. }
  714. }
  715. /*
  716. * Cancel a previously queued request message
  717. */
  718. static void __cancel_request(struct ceph_osd_request *req)
  719. {
  720. if (req->r_sent && req->r_osd) {
  721. ceph_msg_revoke(req->r_request);
  722. req->r_sent = 0;
  723. }
  724. }
  725. static void __register_linger_request(struct ceph_osd_client *osdc,
  726. struct ceph_osd_request *req)
  727. {
  728. dout("__register_linger_request %p\n", req);
  729. list_add_tail(&req->r_linger_item, &osdc->req_linger);
  730. if (req->r_osd)
  731. list_add_tail(&req->r_linger_osd,
  732. &req->r_osd->o_linger_requests);
  733. }
  734. static void __unregister_linger_request(struct ceph_osd_client *osdc,
  735. struct ceph_osd_request *req)
  736. {
  737. dout("__unregister_linger_request %p\n", req);
  738. list_del_init(&req->r_linger_item);
  739. if (req->r_osd) {
  740. list_del_init(&req->r_linger_osd);
  741. if (list_empty(&req->r_osd->o_requests) &&
  742. list_empty(&req->r_osd->o_linger_requests)) {
  743. dout("moving osd to %p lru\n", req->r_osd);
  744. __move_osd_to_lru(osdc, req->r_osd);
  745. }
  746. if (list_empty(&req->r_osd_item))
  747. req->r_osd = NULL;
  748. }
  749. }
  750. void ceph_osdc_unregister_linger_request(struct ceph_osd_client *osdc,
  751. struct ceph_osd_request *req)
  752. {
  753. mutex_lock(&osdc->request_mutex);
  754. if (req->r_linger) {
  755. __unregister_linger_request(osdc, req);
  756. ceph_osdc_put_request(req);
  757. }
  758. mutex_unlock(&osdc->request_mutex);
  759. }
  760. EXPORT_SYMBOL(ceph_osdc_unregister_linger_request);
  761. void ceph_osdc_set_request_linger(struct ceph_osd_client *osdc,
  762. struct ceph_osd_request *req)
  763. {
  764. if (!req->r_linger) {
  765. dout("set_request_linger %p\n", req);
  766. req->r_linger = 1;
  767. /*
  768. * caller is now responsible for calling
  769. * unregister_linger_request
  770. */
  771. ceph_osdc_get_request(req);
  772. }
  773. }
  774. EXPORT_SYMBOL(ceph_osdc_set_request_linger);
  775. /*
  776. * Pick an osd (the first 'up' osd in the pg), allocate the osd struct
  777. * (as needed), and set the request r_osd appropriately. If there is
  778. * no up osd, set r_osd to NULL. Move the request to the appropriate list
  779. * (unsent, homeless) or leave on in-flight lru.
  780. *
  781. * Return 0 if unchanged, 1 if changed, or negative on error.
  782. *
  783. * Caller should hold map_sem for read and request_mutex.
  784. */
  785. static int __map_request(struct ceph_osd_client *osdc,
  786. struct ceph_osd_request *req, int force_resend)
  787. {
  788. struct ceph_osd_request_head *reqhead = req->r_request->front.iov_base;
  789. struct ceph_pg pgid;
  790. int acting[CEPH_PG_MAX_SIZE];
  791. int o = -1, num = 0;
  792. int err;
  793. dout("map_request %p tid %lld\n", req, req->r_tid);
  794. err = ceph_calc_object_layout(&reqhead->layout, req->r_oid,
  795. &req->r_file_layout, osdc->osdmap);
  796. if (err) {
  797. list_move(&req->r_req_lru_item, &osdc->req_notarget);
  798. return err;
  799. }
  800. pgid = reqhead->layout.ol_pgid;
  801. req->r_pgid = pgid;
  802. err = ceph_calc_pg_acting(osdc->osdmap, pgid, acting);
  803. if (err > 0) {
  804. o = acting[0];
  805. num = err;
  806. }
  807. if ((!force_resend &&
  808. req->r_osd && req->r_osd->o_osd == o &&
  809. req->r_sent >= req->r_osd->o_incarnation &&
  810. req->r_num_pg_osds == num &&
  811. memcmp(req->r_pg_osds, acting, sizeof(acting[0])*num) == 0) ||
  812. (req->r_osd == NULL && o == -1))
  813. return 0; /* no change */
  814. dout("map_request tid %llu pgid %d.%x osd%d (was osd%d)\n",
  815. req->r_tid, le32_to_cpu(pgid.pool), le16_to_cpu(pgid.ps), o,
  816. req->r_osd ? req->r_osd->o_osd : -1);
  817. /* record full pg acting set */
  818. memcpy(req->r_pg_osds, acting, sizeof(acting[0]) * num);
  819. req->r_num_pg_osds = num;
  820. if (req->r_osd) {
  821. __cancel_request(req);
  822. list_del_init(&req->r_osd_item);
  823. req->r_osd = NULL;
  824. }
  825. req->r_osd = __lookup_osd(osdc, o);
  826. if (!req->r_osd && o >= 0) {
  827. err = -ENOMEM;
  828. req->r_osd = create_osd(osdc, o);
  829. if (!req->r_osd) {
  830. list_move(&req->r_req_lru_item, &osdc->req_notarget);
  831. goto out;
  832. }
  833. dout("map_request osd %p is osd%d\n", req->r_osd, o);
  834. __insert_osd(osdc, req->r_osd);
  835. ceph_con_open(&req->r_osd->o_con,
  836. CEPH_ENTITY_TYPE_OSD, o,
  837. &osdc->osdmap->osd_addr[o]);
  838. }
  839. if (req->r_osd) {
  840. __remove_osd_from_lru(req->r_osd);
  841. list_add(&req->r_osd_item, &req->r_osd->o_requests);
  842. list_move(&req->r_req_lru_item, &osdc->req_unsent);
  843. } else {
  844. list_move(&req->r_req_lru_item, &osdc->req_notarget);
  845. }
  846. err = 1; /* osd or pg changed */
  847. out:
  848. return err;
  849. }
  850. /*
  851. * caller should hold map_sem (for read) and request_mutex
  852. */
  853. static void __send_request(struct ceph_osd_client *osdc,
  854. struct ceph_osd_request *req)
  855. {
  856. struct ceph_osd_request_head *reqhead;
  857. dout("send_request %p tid %llu to osd%d flags %d\n",
  858. req, req->r_tid, req->r_osd->o_osd, req->r_flags);
  859. reqhead = req->r_request->front.iov_base;
  860. reqhead->osdmap_epoch = cpu_to_le32(osdc->osdmap->epoch);
  861. reqhead->flags |= cpu_to_le32(req->r_flags); /* e.g., RETRY */
  862. reqhead->reassert_version = req->r_reassert_version;
  863. req->r_stamp = jiffies;
  864. list_move_tail(&req->r_req_lru_item, &osdc->req_lru);
  865. ceph_msg_get(req->r_request); /* send consumes a ref */
  866. ceph_con_send(&req->r_osd->o_con, req->r_request);
  867. req->r_sent = req->r_osd->o_incarnation;
  868. }
  869. /*
  870. * Send any requests in the queue (req_unsent).
  871. */
  872. static void __send_queued(struct ceph_osd_client *osdc)
  873. {
  874. struct ceph_osd_request *req, *tmp;
  875. dout("__send_queued\n");
  876. list_for_each_entry_safe(req, tmp, &osdc->req_unsent, r_req_lru_item)
  877. __send_request(osdc, req);
  878. }
  879. /*
  880. * Timeout callback, called every N seconds when 1 or more osd
  881. * requests has been active for more than N seconds. When this
  882. * happens, we ping all OSDs with requests who have timed out to
  883. * ensure any communications channel reset is detected. Reset the
  884. * request timeouts another N seconds in the future as we go.
  885. * Reschedule the timeout event another N seconds in future (unless
  886. * there are no open requests).
  887. */
  888. static void handle_timeout(struct work_struct *work)
  889. {
  890. struct ceph_osd_client *osdc =
  891. container_of(work, struct ceph_osd_client, timeout_work.work);
  892. struct ceph_osd_request *req;
  893. struct ceph_osd *osd;
  894. unsigned long keepalive =
  895. osdc->client->options->osd_keepalive_timeout * HZ;
  896. struct list_head slow_osds;
  897. dout("timeout\n");
  898. down_read(&osdc->map_sem);
  899. ceph_monc_request_next_osdmap(&osdc->client->monc);
  900. mutex_lock(&osdc->request_mutex);
  901. /*
  902. * ping osds that are a bit slow. this ensures that if there
  903. * is a break in the TCP connection we will notice, and reopen
  904. * a connection with that osd (from the fault callback).
  905. */
  906. INIT_LIST_HEAD(&slow_osds);
  907. list_for_each_entry(req, &osdc->req_lru, r_req_lru_item) {
  908. if (time_before(jiffies, req->r_stamp + keepalive))
  909. break;
  910. osd = req->r_osd;
  911. BUG_ON(!osd);
  912. dout(" tid %llu is slow, will send keepalive on osd%d\n",
  913. req->r_tid, osd->o_osd);
  914. list_move_tail(&osd->o_keepalive_item, &slow_osds);
  915. }
  916. while (!list_empty(&slow_osds)) {
  917. osd = list_entry(slow_osds.next, struct ceph_osd,
  918. o_keepalive_item);
  919. list_del_init(&osd->o_keepalive_item);
  920. ceph_con_keepalive(&osd->o_con);
  921. }
  922. __schedule_osd_timeout(osdc);
  923. __send_queued(osdc);
  924. mutex_unlock(&osdc->request_mutex);
  925. up_read(&osdc->map_sem);
  926. }
  927. static void handle_osds_timeout(struct work_struct *work)
  928. {
  929. struct ceph_osd_client *osdc =
  930. container_of(work, struct ceph_osd_client,
  931. osds_timeout_work.work);
  932. unsigned long delay =
  933. osdc->client->options->osd_idle_ttl * HZ >> 2;
  934. dout("osds timeout\n");
  935. down_read(&osdc->map_sem);
  936. remove_old_osds(osdc);
  937. up_read(&osdc->map_sem);
  938. schedule_delayed_work(&osdc->osds_timeout_work,
  939. round_jiffies_relative(delay));
  940. }
  941. static void complete_request(struct ceph_osd_request *req)
  942. {
  943. if (req->r_safe_callback)
  944. req->r_safe_callback(req, NULL);
  945. complete_all(&req->r_safe_completion); /* fsync waiter */
  946. }
  947. /*
  948. * handle osd op reply. either call the callback if it is specified,
  949. * or do the completion to wake up the waiting thread.
  950. */
  951. static void handle_reply(struct ceph_osd_client *osdc, struct ceph_msg *msg,
  952. struct ceph_connection *con)
  953. {
  954. struct ceph_osd_reply_head *rhead = msg->front.iov_base;
  955. struct ceph_osd_request *req;
  956. u64 tid;
  957. int numops, object_len, flags;
  958. s32 result;
  959. tid = le64_to_cpu(msg->hdr.tid);
  960. if (msg->front.iov_len < sizeof(*rhead))
  961. goto bad;
  962. numops = le32_to_cpu(rhead->num_ops);
  963. object_len = le32_to_cpu(rhead->object_len);
  964. result = le32_to_cpu(rhead->result);
  965. if (msg->front.iov_len != sizeof(*rhead) + object_len +
  966. numops * sizeof(struct ceph_osd_op))
  967. goto bad;
  968. dout("handle_reply %p tid %llu result %d\n", msg, tid, (int)result);
  969. /* lookup */
  970. mutex_lock(&osdc->request_mutex);
  971. req = __lookup_request(osdc, tid);
  972. if (req == NULL) {
  973. dout("handle_reply tid %llu dne\n", tid);
  974. mutex_unlock(&osdc->request_mutex);
  975. return;
  976. }
  977. ceph_osdc_get_request(req);
  978. flags = le32_to_cpu(rhead->flags);
  979. /*
  980. * if this connection filled our message, drop our reference now, to
  981. * avoid a (safe but slower) revoke later.
  982. */
  983. if (req->r_con_filling_msg == con && req->r_reply == msg) {
  984. dout(" dropping con_filling_msg ref %p\n", con);
  985. req->r_con_filling_msg = NULL;
  986. con->ops->put(con);
  987. }
  988. if (!req->r_got_reply) {
  989. unsigned int bytes;
  990. req->r_result = le32_to_cpu(rhead->result);
  991. bytes = le32_to_cpu(msg->hdr.data_len);
  992. dout("handle_reply result %d bytes %d\n", req->r_result,
  993. bytes);
  994. if (req->r_result == 0)
  995. req->r_result = bytes;
  996. /* in case this is a write and we need to replay, */
  997. req->r_reassert_version = rhead->reassert_version;
  998. req->r_got_reply = 1;
  999. } else if ((flags & CEPH_OSD_FLAG_ONDISK) == 0) {
  1000. dout("handle_reply tid %llu dup ack\n", tid);
  1001. mutex_unlock(&osdc->request_mutex);
  1002. goto done;
  1003. }
  1004. dout("handle_reply tid %llu flags %d\n", tid, flags);
  1005. if (req->r_linger && (flags & CEPH_OSD_FLAG_ONDISK))
  1006. __register_linger_request(osdc, req);
  1007. /* either this is a read, or we got the safe response */
  1008. if (result < 0 ||
  1009. (flags & CEPH_OSD_FLAG_ONDISK) ||
  1010. ((flags & CEPH_OSD_FLAG_WRITE) == 0))
  1011. __unregister_request(osdc, req);
  1012. mutex_unlock(&osdc->request_mutex);
  1013. if (req->r_callback)
  1014. req->r_callback(req, msg);
  1015. else
  1016. complete_all(&req->r_completion);
  1017. if (flags & CEPH_OSD_FLAG_ONDISK)
  1018. complete_request(req);
  1019. done:
  1020. dout("req=%p req->r_linger=%d\n", req, req->r_linger);
  1021. ceph_osdc_put_request(req);
  1022. return;
  1023. bad:
  1024. pr_err("corrupt osd_op_reply got %d %d expected %d\n",
  1025. (int)msg->front.iov_len, le32_to_cpu(msg->hdr.front_len),
  1026. (int)sizeof(*rhead));
  1027. ceph_msg_dump(msg);
  1028. }
  1029. static void reset_changed_osds(struct ceph_osd_client *osdc)
  1030. {
  1031. struct rb_node *p, *n;
  1032. for (p = rb_first(&osdc->osds); p; p = n) {
  1033. struct ceph_osd *osd = rb_entry(p, struct ceph_osd, o_node);
  1034. n = rb_next(p);
  1035. if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) ||
  1036. memcmp(&osd->o_con.peer_addr,
  1037. ceph_osd_addr(osdc->osdmap,
  1038. osd->o_osd),
  1039. sizeof(struct ceph_entity_addr)) != 0)
  1040. __reset_osd(osdc, osd);
  1041. }
  1042. }
  1043. /*
  1044. * Requeue requests whose mapping to an OSD has changed. If requests map to
  1045. * no osd, request a new map.
  1046. *
  1047. * Caller should hold map_sem for read.
  1048. */
  1049. static void kick_requests(struct ceph_osd_client *osdc, int force_resend)
  1050. {
  1051. struct ceph_osd_request *req, *nreq;
  1052. struct rb_node *p;
  1053. int needmap = 0;
  1054. int err;
  1055. dout("kick_requests %s\n", force_resend ? " (force resend)" : "");
  1056. mutex_lock(&osdc->request_mutex);
  1057. for (p = rb_first(&osdc->requests); p; ) {
  1058. req = rb_entry(p, struct ceph_osd_request, r_node);
  1059. p = rb_next(p);
  1060. /*
  1061. * For linger requests that have not yet been
  1062. * registered, move them to the linger list; they'll
  1063. * be sent to the osd in the loop below. Unregister
  1064. * the request before re-registering it as a linger
  1065. * request to ensure the __map_request() below
  1066. * will decide it needs to be sent.
  1067. */
  1068. if (req->r_linger && list_empty(&req->r_linger_item)) {
  1069. dout("%p tid %llu restart on osd%d\n",
  1070. req, req->r_tid,
  1071. req->r_osd ? req->r_osd->o_osd : -1);
  1072. __unregister_request(osdc, req);
  1073. __register_linger_request(osdc, req);
  1074. continue;
  1075. }
  1076. err = __map_request(osdc, req, force_resend);
  1077. if (err < 0)
  1078. continue; /* error */
  1079. if (req->r_osd == NULL) {
  1080. dout("%p tid %llu maps to no osd\n", req, req->r_tid);
  1081. needmap++; /* request a newer map */
  1082. } else if (err > 0) {
  1083. if (!req->r_linger) {
  1084. dout("%p tid %llu requeued on osd%d\n", req,
  1085. req->r_tid,
  1086. req->r_osd ? req->r_osd->o_osd : -1);
  1087. req->r_flags |= CEPH_OSD_FLAG_RETRY;
  1088. }
  1089. }
  1090. }
  1091. list_for_each_entry_safe(req, nreq, &osdc->req_linger,
  1092. r_linger_item) {
  1093. dout("linger req=%p req->r_osd=%p\n", req, req->r_osd);
  1094. err = __map_request(osdc, req, force_resend);
  1095. dout("__map_request returned %d\n", err);
  1096. if (err == 0)
  1097. continue; /* no change and no osd was specified */
  1098. if (err < 0)
  1099. continue; /* hrm! */
  1100. if (req->r_osd == NULL) {
  1101. dout("tid %llu maps to no valid osd\n", req->r_tid);
  1102. needmap++; /* request a newer map */
  1103. continue;
  1104. }
  1105. dout("kicking lingering %p tid %llu osd%d\n", req, req->r_tid,
  1106. req->r_osd ? req->r_osd->o_osd : -1);
  1107. __register_request(osdc, req);
  1108. __unregister_linger_request(osdc, req);
  1109. }
  1110. mutex_unlock(&osdc->request_mutex);
  1111. if (needmap) {
  1112. dout("%d requests for down osds, need new map\n", needmap);
  1113. ceph_monc_request_next_osdmap(&osdc->client->monc);
  1114. }
  1115. reset_changed_osds(osdc);
  1116. }
  1117. /*
  1118. * Process updated osd map.
  1119. *
  1120. * The message contains any number of incremental and full maps, normally
  1121. * indicating some sort of topology change in the cluster. Kick requests
  1122. * off to different OSDs as needed.
  1123. */
  1124. void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg)
  1125. {
  1126. void *p, *end, *next;
  1127. u32 nr_maps, maplen;
  1128. u32 epoch;
  1129. struct ceph_osdmap *newmap = NULL, *oldmap;
  1130. int err;
  1131. struct ceph_fsid fsid;
  1132. dout("handle_map have %u\n", osdc->osdmap ? osdc->osdmap->epoch : 0);
  1133. p = msg->front.iov_base;
  1134. end = p + msg->front.iov_len;
  1135. /* verify fsid */
  1136. ceph_decode_need(&p, end, sizeof(fsid), bad);
  1137. ceph_decode_copy(&p, &fsid, sizeof(fsid));
  1138. if (ceph_check_fsid(osdc->client, &fsid) < 0)
  1139. return;
  1140. down_write(&osdc->map_sem);
  1141. /* incremental maps */
  1142. ceph_decode_32_safe(&p, end, nr_maps, bad);
  1143. dout(" %d inc maps\n", nr_maps);
  1144. while (nr_maps > 0) {
  1145. ceph_decode_need(&p, end, 2*sizeof(u32), bad);
  1146. epoch = ceph_decode_32(&p);
  1147. maplen = ceph_decode_32(&p);
  1148. ceph_decode_need(&p, end, maplen, bad);
  1149. next = p + maplen;
  1150. if (osdc->osdmap && osdc->osdmap->epoch+1 == epoch) {
  1151. dout("applying incremental map %u len %d\n",
  1152. epoch, maplen);
  1153. newmap = osdmap_apply_incremental(&p, next,
  1154. osdc->osdmap,
  1155. &osdc->client->msgr);
  1156. if (IS_ERR(newmap)) {
  1157. err = PTR_ERR(newmap);
  1158. goto bad;
  1159. }
  1160. BUG_ON(!newmap);
  1161. if (newmap != osdc->osdmap) {
  1162. ceph_osdmap_destroy(osdc->osdmap);
  1163. osdc->osdmap = newmap;
  1164. }
  1165. kick_requests(osdc, 0);
  1166. } else {
  1167. dout("ignoring incremental map %u len %d\n",
  1168. epoch, maplen);
  1169. }
  1170. p = next;
  1171. nr_maps--;
  1172. }
  1173. if (newmap)
  1174. goto done;
  1175. /* full maps */
  1176. ceph_decode_32_safe(&p, end, nr_maps, bad);
  1177. dout(" %d full maps\n", nr_maps);
  1178. while (nr_maps) {
  1179. ceph_decode_need(&p, end, 2*sizeof(u32), bad);
  1180. epoch = ceph_decode_32(&p);
  1181. maplen = ceph_decode_32(&p);
  1182. ceph_decode_need(&p, end, maplen, bad);
  1183. if (nr_maps > 1) {
  1184. dout("skipping non-latest full map %u len %d\n",
  1185. epoch, maplen);
  1186. } else if (osdc->osdmap && osdc->osdmap->epoch >= epoch) {
  1187. dout("skipping full map %u len %d, "
  1188. "older than our %u\n", epoch, maplen,
  1189. osdc->osdmap->epoch);
  1190. } else {
  1191. int skipped_map = 0;
  1192. dout("taking full map %u len %d\n", epoch, maplen);
  1193. newmap = osdmap_decode(&p, p+maplen);
  1194. if (IS_ERR(newmap)) {
  1195. err = PTR_ERR(newmap);
  1196. goto bad;
  1197. }
  1198. BUG_ON(!newmap);
  1199. oldmap = osdc->osdmap;
  1200. osdc->osdmap = newmap;
  1201. if (oldmap) {
  1202. if (oldmap->epoch + 1 < newmap->epoch)
  1203. skipped_map = 1;
  1204. ceph_osdmap_destroy(oldmap);
  1205. }
  1206. kick_requests(osdc, skipped_map);
  1207. }
  1208. p += maplen;
  1209. nr_maps--;
  1210. }
  1211. done:
  1212. downgrade_write(&osdc->map_sem);
  1213. ceph_monc_got_osdmap(&osdc->client->monc, osdc->osdmap->epoch);
  1214. /*
  1215. * subscribe to subsequent osdmap updates if full to ensure
  1216. * we find out when we are no longer full and stop returning
  1217. * ENOSPC.
  1218. */
  1219. if (ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL))
  1220. ceph_monc_request_next_osdmap(&osdc->client->monc);
  1221. mutex_lock(&osdc->request_mutex);
  1222. __send_queued(osdc);
  1223. mutex_unlock(&osdc->request_mutex);
  1224. up_read(&osdc->map_sem);
  1225. wake_up_all(&osdc->client->auth_wq);
  1226. return;
  1227. bad:
  1228. pr_err("osdc handle_map corrupt msg\n");
  1229. ceph_msg_dump(msg);
  1230. up_write(&osdc->map_sem);
  1231. return;
  1232. }
  1233. /*
  1234. * watch/notify callback event infrastructure
  1235. *
  1236. * These callbacks are used both for watch and notify operations.
  1237. */
  1238. static void __release_event(struct kref *kref)
  1239. {
  1240. struct ceph_osd_event *event =
  1241. container_of(kref, struct ceph_osd_event, kref);
  1242. dout("__release_event %p\n", event);
  1243. kfree(event);
  1244. }
  1245. static void get_event(struct ceph_osd_event *event)
  1246. {
  1247. kref_get(&event->kref);
  1248. }
  1249. void ceph_osdc_put_event(struct ceph_osd_event *event)
  1250. {
  1251. kref_put(&event->kref, __release_event);
  1252. }
  1253. EXPORT_SYMBOL(ceph_osdc_put_event);
  1254. static void __insert_event(struct ceph_osd_client *osdc,
  1255. struct ceph_osd_event *new)
  1256. {
  1257. struct rb_node **p = &osdc->event_tree.rb_node;
  1258. struct rb_node *parent = NULL;
  1259. struct ceph_osd_event *event = NULL;
  1260. while (*p) {
  1261. parent = *p;
  1262. event = rb_entry(parent, struct ceph_osd_event, node);
  1263. if (new->cookie < event->cookie)
  1264. p = &(*p)->rb_left;
  1265. else if (new->cookie > event->cookie)
  1266. p = &(*p)->rb_right;
  1267. else
  1268. BUG();
  1269. }
  1270. rb_link_node(&new->node, parent, p);
  1271. rb_insert_color(&new->node, &osdc->event_tree);
  1272. }
  1273. static struct ceph_osd_event *__find_event(struct ceph_osd_client *osdc,
  1274. u64 cookie)
  1275. {
  1276. struct rb_node **p = &osdc->event_tree.rb_node;
  1277. struct rb_node *parent = NULL;
  1278. struct ceph_osd_event *event = NULL;
  1279. while (*p) {
  1280. parent = *p;
  1281. event = rb_entry(parent, struct ceph_osd_event, node);
  1282. if (cookie < event->cookie)
  1283. p = &(*p)->rb_left;
  1284. else if (cookie > event->cookie)
  1285. p = &(*p)->rb_right;
  1286. else
  1287. return event;
  1288. }
  1289. return NULL;
  1290. }
  1291. static void __remove_event(struct ceph_osd_event *event)
  1292. {
  1293. struct ceph_osd_client *osdc = event->osdc;
  1294. if (!RB_EMPTY_NODE(&event->node)) {
  1295. dout("__remove_event removed %p\n", event);
  1296. rb_erase(&event->node, &osdc->event_tree);
  1297. ceph_osdc_put_event(event);
  1298. } else {
  1299. dout("__remove_event didn't remove %p\n", event);
  1300. }
  1301. }
  1302. int ceph_osdc_create_event(struct ceph_osd_client *osdc,
  1303. void (*event_cb)(u64, u64, u8, void *),
  1304. int one_shot, void *data,
  1305. struct ceph_osd_event **pevent)
  1306. {
  1307. struct ceph_osd_event *event;
  1308. event = kmalloc(sizeof(*event), GFP_NOIO);
  1309. if (!event)
  1310. return -ENOMEM;
  1311. dout("create_event %p\n", event);
  1312. event->cb = event_cb;
  1313. event->one_shot = one_shot;
  1314. event->data = data;
  1315. event->osdc = osdc;
  1316. INIT_LIST_HEAD(&event->osd_node);
  1317. RB_CLEAR_NODE(&event->node);
  1318. kref_init(&event->kref); /* one ref for us */
  1319. kref_get(&event->kref); /* one ref for the caller */
  1320. init_completion(&event->completion);
  1321. spin_lock(&osdc->event_lock);
  1322. event->cookie = ++osdc->event_count;
  1323. __insert_event(osdc, event);
  1324. spin_unlock(&osdc->event_lock);
  1325. *pevent = event;
  1326. return 0;
  1327. }
  1328. EXPORT_SYMBOL(ceph_osdc_create_event);
  1329. void ceph_osdc_cancel_event(struct ceph_osd_event *event)
  1330. {
  1331. struct ceph_osd_client *osdc = event->osdc;
  1332. dout("cancel_event %p\n", event);
  1333. spin_lock(&osdc->event_lock);
  1334. __remove_event(event);
  1335. spin_unlock(&osdc->event_lock);
  1336. ceph_osdc_put_event(event); /* caller's */
  1337. }
  1338. EXPORT_SYMBOL(ceph_osdc_cancel_event);
  1339. static void do_event_work(struct work_struct *work)
  1340. {
  1341. struct ceph_osd_event_work *event_work =
  1342. container_of(work, struct ceph_osd_event_work, work);
  1343. struct ceph_osd_event *event = event_work->event;
  1344. u64 ver = event_work->ver;
  1345. u64 notify_id = event_work->notify_id;
  1346. u8 opcode = event_work->opcode;
  1347. dout("do_event_work completing %p\n", event);
  1348. event->cb(ver, notify_id, opcode, event->data);
  1349. complete(&event->completion);
  1350. dout("do_event_work completed %p\n", event);
  1351. ceph_osdc_put_event(event);
  1352. kfree(event_work);
  1353. }
  1354. /*
  1355. * Process osd watch notifications
  1356. */
  1357. void handle_watch_notify(struct ceph_osd_client *osdc, struct ceph_msg *msg)
  1358. {
  1359. void *p, *end;
  1360. u8 proto_ver;
  1361. u64 cookie, ver, notify_id;
  1362. u8 opcode;
  1363. struct ceph_osd_event *event;
  1364. struct ceph_osd_event_work *event_work;
  1365. p = msg->front.iov_base;
  1366. end = p + msg->front.iov_len;
  1367. ceph_decode_8_safe(&p, end, proto_ver, bad);
  1368. ceph_decode_8_safe(&p, end, opcode, bad);
  1369. ceph_decode_64_safe(&p, end, cookie, bad);
  1370. ceph_decode_64_safe(&p, end, ver, bad);
  1371. ceph_decode_64_safe(&p, end, notify_id, bad);
  1372. spin_lock(&osdc->event_lock);
  1373. event = __find_event(osdc, cookie);
  1374. if (event) {
  1375. get_event(event);
  1376. if (event->one_shot)
  1377. __remove_event(event);
  1378. }
  1379. spin_unlock(&osdc->event_lock);
  1380. dout("handle_watch_notify cookie %lld ver %lld event %p\n",
  1381. cookie, ver, event);
  1382. if (event) {
  1383. event_work = kmalloc(sizeof(*event_work), GFP_NOIO);
  1384. if (!event_work) {
  1385. dout("ERROR: could not allocate event_work\n");
  1386. goto done_err;
  1387. }
  1388. INIT_WORK(&event_work->work, do_event_work);
  1389. event_work->event = event;
  1390. event_work->ver = ver;
  1391. event_work->notify_id = notify_id;
  1392. event_work->opcode = opcode;
  1393. if (!queue_work(osdc->notify_wq, &event_work->work)) {
  1394. dout("WARNING: failed to queue notify event work\n");
  1395. goto done_err;
  1396. }
  1397. }
  1398. return;
  1399. done_err:
  1400. complete(&event->completion);
  1401. ceph_osdc_put_event(event);
  1402. return;
  1403. bad:
  1404. pr_err("osdc handle_watch_notify corrupt msg\n");
  1405. return;
  1406. }
  1407. int ceph_osdc_wait_event(struct ceph_osd_event *event, unsigned long timeout)
  1408. {
  1409. int err;
  1410. dout("wait_event %p\n", event);
  1411. err = wait_for_completion_interruptible_timeout(&event->completion,
  1412. timeout * HZ);
  1413. ceph_osdc_put_event(event);
  1414. if (err > 0)
  1415. err = 0;
  1416. dout("wait_event %p returns %d\n", event, err);
  1417. return err;
  1418. }
  1419. EXPORT_SYMBOL(ceph_osdc_wait_event);
  1420. /*
  1421. * Register request, send initial attempt.
  1422. */
  1423. int ceph_osdc_start_request(struct ceph_osd_client *osdc,
  1424. struct ceph_osd_request *req,
  1425. bool nofail)
  1426. {
  1427. int rc = 0;
  1428. req->r_request->pages = req->r_pages;
  1429. req->r_request->nr_pages = req->r_num_pages;
  1430. #ifdef CONFIG_BLOCK
  1431. req->r_request->bio = req->r_bio;
  1432. #endif
  1433. req->r_request->trail = &req->r_trail;
  1434. register_request(osdc, req);
  1435. down_read(&osdc->map_sem);
  1436. mutex_lock(&osdc->request_mutex);
  1437. /*
  1438. * a racing kick_requests() may have sent the message for us
  1439. * while we dropped request_mutex above, so only send now if
  1440. * the request still han't been touched yet.
  1441. */
  1442. if (req->r_sent == 0) {
  1443. rc = __map_request(osdc, req, 0);
  1444. if (rc < 0) {
  1445. if (nofail) {
  1446. dout("osdc_start_request failed map, "
  1447. " will retry %lld\n", req->r_tid);
  1448. rc = 0;
  1449. }
  1450. goto out_unlock;
  1451. }
  1452. if (req->r_osd == NULL) {
  1453. dout("send_request %p no up osds in pg\n", req);
  1454. ceph_monc_request_next_osdmap(&osdc->client->monc);
  1455. } else {
  1456. __send_request(osdc, req);
  1457. }
  1458. rc = 0;
  1459. }
  1460. out_unlock:
  1461. mutex_unlock(&osdc->request_mutex);
  1462. up_read(&osdc->map_sem);
  1463. return rc;
  1464. }
  1465. EXPORT_SYMBOL(ceph_osdc_start_request);
  1466. /*
  1467. * wait for a request to complete
  1468. */
  1469. int ceph_osdc_wait_request(struct ceph_osd_client *osdc,
  1470. struct ceph_osd_request *req)
  1471. {
  1472. int rc;
  1473. rc = wait_for_completion_interruptible(&req->r_completion);
  1474. if (rc < 0) {
  1475. mutex_lock(&osdc->request_mutex);
  1476. __cancel_request(req);
  1477. __unregister_request(osdc, req);
  1478. mutex_unlock(&osdc->request_mutex);
  1479. complete_request(req);
  1480. dout("wait_request tid %llu canceled/timed out\n", req->r_tid);
  1481. return rc;
  1482. }
  1483. dout("wait_request tid %llu result %d\n", req->r_tid, req->r_result);
  1484. return req->r_result;
  1485. }
  1486. EXPORT_SYMBOL(ceph_osdc_wait_request);
  1487. /*
  1488. * sync - wait for all in-flight requests to flush. avoid starvation.
  1489. */
  1490. void ceph_osdc_sync(struct ceph_osd_client *osdc)
  1491. {
  1492. struct ceph_osd_request *req;
  1493. u64 last_tid, next_tid = 0;
  1494. mutex_lock(&osdc->request_mutex);
  1495. last_tid = osdc->last_tid;
  1496. while (1) {
  1497. req = __lookup_request_ge(osdc, next_tid);
  1498. if (!req)
  1499. break;
  1500. if (req->r_tid > last_tid)
  1501. break;
  1502. next_tid = req->r_tid + 1;
  1503. if ((req->r_flags & CEPH_OSD_FLAG_WRITE) == 0)
  1504. continue;
  1505. ceph_osdc_get_request(req);
  1506. mutex_unlock(&osdc->request_mutex);
  1507. dout("sync waiting on tid %llu (last is %llu)\n",
  1508. req->r_tid, last_tid);
  1509. wait_for_completion(&req->r_safe_completion);
  1510. mutex_lock(&osdc->request_mutex);
  1511. ceph_osdc_put_request(req);
  1512. }
  1513. mutex_unlock(&osdc->request_mutex);
  1514. dout("sync done (thru tid %llu)\n", last_tid);
  1515. }
  1516. EXPORT_SYMBOL(ceph_osdc_sync);
  1517. /*
  1518. * init, shutdown
  1519. */
  1520. int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client)
  1521. {
  1522. int err;
  1523. dout("init\n");
  1524. osdc->client = client;
  1525. osdc->osdmap = NULL;
  1526. init_rwsem(&osdc->map_sem);
  1527. init_completion(&osdc->map_waiters);
  1528. osdc->last_requested_map = 0;
  1529. mutex_init(&osdc->request_mutex);
  1530. osdc->last_tid = 0;
  1531. osdc->osds = RB_ROOT;
  1532. INIT_LIST_HEAD(&osdc->osd_lru);
  1533. osdc->requests = RB_ROOT;
  1534. INIT_LIST_HEAD(&osdc->req_lru);
  1535. INIT_LIST_HEAD(&osdc->req_unsent);
  1536. INIT_LIST_HEAD(&osdc->req_notarget);
  1537. INIT_LIST_HEAD(&osdc->req_linger);
  1538. osdc->num_requests = 0;
  1539. INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout);
  1540. INIT_DELAYED_WORK(&osdc->osds_timeout_work, handle_osds_timeout);
  1541. spin_lock_init(&osdc->event_lock);
  1542. osdc->event_tree = RB_ROOT;
  1543. osdc->event_count = 0;
  1544. schedule_delayed_work(&osdc->osds_timeout_work,
  1545. round_jiffies_relative(osdc->client->options->osd_idle_ttl * HZ));
  1546. err = -ENOMEM;
  1547. osdc->req_mempool = mempool_create_kmalloc_pool(10,
  1548. sizeof(struct ceph_osd_request));
  1549. if (!osdc->req_mempool)
  1550. goto out;
  1551. err = ceph_msgpool_init(&osdc->msgpool_op, CEPH_MSG_OSD_OP,
  1552. OSD_OP_FRONT_LEN, 10, true,
  1553. "osd_op");
  1554. if (err < 0)
  1555. goto out_mempool;
  1556. err = ceph_msgpool_init(&osdc->msgpool_op_reply, CEPH_MSG_OSD_OPREPLY,
  1557. OSD_OPREPLY_FRONT_LEN, 10, true,
  1558. "osd_op_reply");
  1559. if (err < 0)
  1560. goto out_msgpool;
  1561. osdc->notify_wq = create_singlethread_workqueue("ceph-watch-notify");
  1562. if (IS_ERR(osdc->notify_wq)) {
  1563. err = PTR_ERR(osdc->notify_wq);
  1564. osdc->notify_wq = NULL;
  1565. goto out_msgpool;
  1566. }
  1567. return 0;
  1568. out_msgpool:
  1569. ceph_msgpool_destroy(&osdc->msgpool_op);
  1570. out_mempool:
  1571. mempool_destroy(osdc->req_mempool);
  1572. out:
  1573. return err;
  1574. }
  1575. void ceph_osdc_stop(struct ceph_osd_client *osdc)
  1576. {
  1577. flush_workqueue(osdc->notify_wq);
  1578. destroy_workqueue(osdc->notify_wq);
  1579. cancel_delayed_work_sync(&osdc->timeout_work);
  1580. cancel_delayed_work_sync(&osdc->osds_timeout_work);
  1581. if (osdc->osdmap) {
  1582. ceph_osdmap_destroy(osdc->osdmap);
  1583. osdc->osdmap = NULL;
  1584. }
  1585. remove_all_osds(osdc);
  1586. mempool_destroy(osdc->req_mempool);
  1587. ceph_msgpool_destroy(&osdc->msgpool_op);
  1588. ceph_msgpool_destroy(&osdc->msgpool_op_reply);
  1589. }
  1590. /*
  1591. * Read some contiguous pages. If we cross a stripe boundary, shorten
  1592. * *plen. Return number of bytes read, or error.
  1593. */
  1594. int ceph_osdc_readpages(struct ceph_osd_client *osdc,
  1595. struct ceph_vino vino, struct ceph_file_layout *layout,
  1596. u64 off, u64 *plen,
  1597. u32 truncate_seq, u64 truncate_size,
  1598. struct page **pages, int num_pages, int page_align)
  1599. {
  1600. struct ceph_osd_request *req;
  1601. int rc = 0;
  1602. dout("readpages on ino %llx.%llx on %llu~%llu\n", vino.ino,
  1603. vino.snap, off, *plen);
  1604. req = ceph_osdc_new_request(osdc, layout, vino, off, plen,
  1605. CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ,
  1606. NULL, 0, truncate_seq, truncate_size, NULL,
  1607. false, page_align);
  1608. if (IS_ERR(req))
  1609. return PTR_ERR(req);
  1610. /* it may be a short read due to an object boundary */
  1611. req->r_pages = pages;
  1612. dout("readpages final extent is %llu~%llu (%d pages align %d)\n",
  1613. off, *plen, req->r_num_pages, page_align);
  1614. rc = ceph_osdc_start_request(osdc, req, false);
  1615. if (!rc)
  1616. rc = ceph_osdc_wait_request(osdc, req);
  1617. ceph_osdc_put_request(req);
  1618. dout("readpages result %d\n", rc);
  1619. return rc;
  1620. }
  1621. EXPORT_SYMBOL(ceph_osdc_readpages);
  1622. /*
  1623. * do a synchronous write on N pages
  1624. */
  1625. int ceph_osdc_writepages(struct ceph_osd_client *osdc, struct ceph_vino vino,
  1626. struct ceph_file_layout *layout,
  1627. struct ceph_snap_context *snapc,
  1628. u64 off, u64 len,
  1629. u32 truncate_seq, u64 truncate_size,
  1630. struct timespec *mtime,
  1631. struct page **pages, int num_pages)
  1632. {
  1633. struct ceph_osd_request *req;
  1634. int rc = 0;
  1635. int page_align = off & ~PAGE_MASK;
  1636. BUG_ON(vino.snap != CEPH_NOSNAP);
  1637. req = ceph_osdc_new_request(osdc, layout, vino, off, &len,
  1638. CEPH_OSD_OP_WRITE,
  1639. CEPH_OSD_FLAG_ONDISK | CEPH_OSD_FLAG_WRITE,
  1640. snapc, 0,
  1641. truncate_seq, truncate_size, mtime,
  1642. true, page_align);
  1643. if (IS_ERR(req))
  1644. return PTR_ERR(req);
  1645. /* it may be a short write due to an object boundary */
  1646. req->r_pages = pages;
  1647. dout("writepages %llu~%llu (%d pages)\n", off, len,
  1648. req->r_num_pages);
  1649. rc = ceph_osdc_start_request(osdc, req, true);
  1650. if (!rc)
  1651. rc = ceph_osdc_wait_request(osdc, req);
  1652. ceph_osdc_put_request(req);
  1653. if (rc == 0)
  1654. rc = len;
  1655. dout("writepages result %d\n", rc);
  1656. return rc;
  1657. }
  1658. EXPORT_SYMBOL(ceph_osdc_writepages);
  1659. /*
  1660. * handle incoming message
  1661. */
  1662. static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
  1663. {
  1664. struct ceph_osd *osd = con->private;
  1665. struct ceph_osd_client *osdc;
  1666. int type = le16_to_cpu(msg->hdr.type);
  1667. if (!osd)
  1668. goto out;
  1669. osdc = osd->o_osdc;
  1670. switch (type) {
  1671. case CEPH_MSG_OSD_MAP:
  1672. ceph_osdc_handle_map(osdc, msg);
  1673. break;
  1674. case CEPH_MSG_OSD_OPREPLY:
  1675. handle_reply(osdc, msg, con);
  1676. break;
  1677. case CEPH_MSG_WATCH_NOTIFY:
  1678. handle_watch_notify(osdc, msg);
  1679. break;
  1680. default:
  1681. pr_err("received unknown message type %d %s\n", type,
  1682. ceph_msg_type_name(type));
  1683. }
  1684. out:
  1685. ceph_msg_put(msg);
  1686. }
  1687. /*
  1688. * lookup and return message for incoming reply. set up reply message
  1689. * pages.
  1690. */
  1691. static struct ceph_msg *get_reply(struct ceph_connection *con,
  1692. struct ceph_msg_header *hdr,
  1693. int *skip)
  1694. {
  1695. struct ceph_osd *osd = con->private;
  1696. struct ceph_osd_client *osdc = osd->o_osdc;
  1697. struct ceph_msg *m;
  1698. struct ceph_osd_request *req;
  1699. int front = le32_to_cpu(hdr->front_len);
  1700. int data_len = le32_to_cpu(hdr->data_len);
  1701. u64 tid;
  1702. tid = le64_to_cpu(hdr->tid);
  1703. mutex_lock(&osdc->request_mutex);
  1704. req = __lookup_request(osdc, tid);
  1705. if (!req) {
  1706. *skip = 1;
  1707. m = NULL;
  1708. dout("get_reply unknown tid %llu from osd%d\n", tid,
  1709. osd->o_osd);
  1710. goto out;
  1711. }
  1712. if (req->r_con_filling_msg) {
  1713. dout("%s revoking msg %p from old con %p\n", __func__,
  1714. req->r_reply, req->r_con_filling_msg);
  1715. ceph_msg_revoke_incoming(req->r_reply);
  1716. req->r_con_filling_msg->ops->put(req->r_con_filling_msg);
  1717. req->r_con_filling_msg = NULL;
  1718. }
  1719. if (front > req->r_reply->front.iov_len) {
  1720. pr_warning("get_reply front %d > preallocated %d\n",
  1721. front, (int)req->r_reply->front.iov_len);
  1722. m = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, front, GFP_NOFS, false);
  1723. if (!m)
  1724. goto out;
  1725. ceph_msg_put(req->r_reply);
  1726. req->r_reply = m;
  1727. }
  1728. m = ceph_msg_get(req->r_reply);
  1729. if (data_len > 0) {
  1730. int want = calc_pages_for(req->r_page_alignment, data_len);
  1731. if (req->r_pages && unlikely(req->r_num_pages < want)) {
  1732. pr_warning("tid %lld reply has %d bytes %d pages, we"
  1733. " had only %d pages ready\n", tid, data_len,
  1734. want, req->r_num_pages);
  1735. *skip = 1;
  1736. ceph_msg_put(m);
  1737. m = NULL;
  1738. goto out;
  1739. }
  1740. m->pages = req->r_pages;
  1741. m->nr_pages = req->r_num_pages;
  1742. m->page_alignment = req->r_page_alignment;
  1743. #ifdef CONFIG_BLOCK
  1744. m->bio = req->r_bio;
  1745. #endif
  1746. }
  1747. *skip = 0;
  1748. req->r_con_filling_msg = con->ops->get(con);
  1749. dout("get_reply tid %lld %p\n", tid, m);
  1750. out:
  1751. mutex_unlock(&osdc->request_mutex);
  1752. return m;
  1753. }
  1754. static struct ceph_msg *alloc_msg(struct ceph_connection *con,
  1755. struct ceph_msg_header *hdr,
  1756. int *skip)
  1757. {
  1758. struct ceph_osd *osd = con->private;
  1759. int type = le16_to_cpu(hdr->type);
  1760. int front = le32_to_cpu(hdr->front_len);
  1761. *skip = 0;
  1762. switch (type) {
  1763. case CEPH_MSG_OSD_MAP:
  1764. case CEPH_MSG_WATCH_NOTIFY:
  1765. return ceph_msg_new(type, front, GFP_NOFS, false);
  1766. case CEPH_MSG_OSD_OPREPLY:
  1767. return get_reply(con, hdr, skip);
  1768. default:
  1769. pr_info("alloc_msg unexpected msg type %d from osd%d\n", type,
  1770. osd->o_osd);
  1771. *skip = 1;
  1772. return NULL;
  1773. }
  1774. }
  1775. /*
  1776. * Wrappers to refcount containing ceph_osd struct
  1777. */
  1778. static struct ceph_connection *get_osd_con(struct ceph_connection *con)
  1779. {
  1780. struct ceph_osd *osd = con->private;
  1781. if (get_osd(osd))
  1782. return con;
  1783. return NULL;
  1784. }
  1785. static void put_osd_con(struct ceph_connection *con)
  1786. {
  1787. struct ceph_osd *osd = con->private;
  1788. put_osd(osd);
  1789. }
  1790. /*
  1791. * authentication
  1792. */
  1793. /*
  1794. * Note: returned pointer is the address of a structure that's
  1795. * managed separately. Caller must *not* attempt to free it.
  1796. */
  1797. static struct ceph_auth_handshake *get_authorizer(struct ceph_connection *con,
  1798. int *proto, int force_new)
  1799. {
  1800. struct ceph_osd *o = con->private;
  1801. struct ceph_osd_client *osdc = o->o_osdc;
  1802. struct ceph_auth_client *ac = osdc->client->monc.auth;
  1803. struct ceph_auth_handshake *auth = &o->o_auth;
  1804. if (force_new && auth->authorizer) {
  1805. if (ac->ops && ac->ops->destroy_authorizer)
  1806. ac->ops->destroy_authorizer(ac, auth->authorizer);
  1807. auth->authorizer = NULL;
  1808. }
  1809. if (!auth->authorizer && ac->ops && ac->ops->create_authorizer) {
  1810. int ret = ac->ops->create_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
  1811. auth);
  1812. if (ret)
  1813. return ERR_PTR(ret);
  1814. }
  1815. *proto = ac->protocol;
  1816. return auth;
  1817. }
  1818. static int verify_authorizer_reply(struct ceph_connection *con, int len)
  1819. {
  1820. struct ceph_osd *o = con->private;
  1821. struct ceph_osd_client *osdc = o->o_osdc;
  1822. struct ceph_auth_client *ac = osdc->client->monc.auth;
  1823. /*
  1824. * XXX If ac->ops or ac->ops->verify_authorizer_reply is null,
  1825. * XXX which do we do: succeed or fail?
  1826. */
  1827. return ac->ops->verify_authorizer_reply(ac, o->o_auth.authorizer, len);
  1828. }
  1829. static int invalidate_authorizer(struct ceph_connection *con)
  1830. {
  1831. struct ceph_osd *o = con->private;
  1832. struct ceph_osd_client *osdc = o->o_osdc;
  1833. struct ceph_auth_client *ac = osdc->client->monc.auth;
  1834. if (ac->ops && ac->ops->invalidate_authorizer)
  1835. ac->ops->invalidate_authorizer(ac, CEPH_ENTITY_TYPE_OSD);
  1836. return ceph_monc_validate_auth(&osdc->client->monc);
  1837. }
  1838. static const struct ceph_connection_operations osd_con_ops = {
  1839. .get = get_osd_con,
  1840. .put = put_osd_con,
  1841. .dispatch = dispatch,
  1842. .get_authorizer = get_authorizer,
  1843. .verify_authorizer_reply = verify_authorizer_reply,
  1844. .invalidate_authorizer = invalidate_authorizer,
  1845. .alloc_msg = alloc_msg,
  1846. .fault = osd_reset,
  1847. };