osd_client.c 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115
  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, int num_reply,
  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. mutex_unlock(&osdc->request_mutex);
  491. send_queued(osdc);
  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. mutex_lock(&osdc->request_mutex);
  877. list_for_each_entry_safe(req, tmp, &osdc->req_unsent, r_req_lru_item) {
  878. __send_request(osdc, req);
  879. }
  880. mutex_unlock(&osdc->request_mutex);
  881. }
  882. /*
  883. * Timeout callback, called every N seconds when 1 or more osd
  884. * requests has been active for more than N seconds. When this
  885. * happens, we ping all OSDs with requests who have timed out to
  886. * ensure any communications channel reset is detected. Reset the
  887. * request timeouts another N seconds in the future as we go.
  888. * Reschedule the timeout event another N seconds in future (unless
  889. * there are no open requests).
  890. */
  891. static void handle_timeout(struct work_struct *work)
  892. {
  893. struct ceph_osd_client *osdc =
  894. container_of(work, struct ceph_osd_client, timeout_work.work);
  895. struct ceph_osd_request *req;
  896. struct ceph_osd *osd;
  897. unsigned long keepalive =
  898. osdc->client->options->osd_keepalive_timeout * HZ;
  899. struct list_head slow_osds;
  900. dout("timeout\n");
  901. down_read(&osdc->map_sem);
  902. ceph_monc_request_next_osdmap(&osdc->client->monc);
  903. mutex_lock(&osdc->request_mutex);
  904. /*
  905. * ping osds that are a bit slow. this ensures that if there
  906. * is a break in the TCP connection we will notice, and reopen
  907. * a connection with that osd (from the fault callback).
  908. */
  909. INIT_LIST_HEAD(&slow_osds);
  910. list_for_each_entry(req, &osdc->req_lru, r_req_lru_item) {
  911. if (time_before(jiffies, req->r_stamp + keepalive))
  912. break;
  913. osd = req->r_osd;
  914. BUG_ON(!osd);
  915. dout(" tid %llu is slow, will send keepalive on osd%d\n",
  916. req->r_tid, osd->o_osd);
  917. list_move_tail(&osd->o_keepalive_item, &slow_osds);
  918. }
  919. while (!list_empty(&slow_osds)) {
  920. osd = list_entry(slow_osds.next, struct ceph_osd,
  921. o_keepalive_item);
  922. list_del_init(&osd->o_keepalive_item);
  923. ceph_con_keepalive(&osd->o_con);
  924. }
  925. __schedule_osd_timeout(osdc);
  926. mutex_unlock(&osdc->request_mutex);
  927. send_queued(osdc);
  928. up_read(&osdc->map_sem);
  929. }
  930. static void handle_osds_timeout(struct work_struct *work)
  931. {
  932. struct ceph_osd_client *osdc =
  933. container_of(work, struct ceph_osd_client,
  934. osds_timeout_work.work);
  935. unsigned long delay =
  936. osdc->client->options->osd_idle_ttl * HZ >> 2;
  937. dout("osds timeout\n");
  938. down_read(&osdc->map_sem);
  939. remove_old_osds(osdc);
  940. up_read(&osdc->map_sem);
  941. schedule_delayed_work(&osdc->osds_timeout_work,
  942. round_jiffies_relative(delay));
  943. }
  944. static void complete_request(struct ceph_osd_request *req)
  945. {
  946. if (req->r_safe_callback)
  947. req->r_safe_callback(req, NULL);
  948. complete_all(&req->r_safe_completion); /* fsync waiter */
  949. }
  950. /*
  951. * handle osd op reply. either call the callback if it is specified,
  952. * or do the completion to wake up the waiting thread.
  953. */
  954. static void handle_reply(struct ceph_osd_client *osdc, struct ceph_msg *msg,
  955. struct ceph_connection *con)
  956. {
  957. struct ceph_osd_reply_head *rhead = msg->front.iov_base;
  958. struct ceph_osd_request *req;
  959. u64 tid;
  960. int numops, object_len, flags;
  961. s32 result;
  962. tid = le64_to_cpu(msg->hdr.tid);
  963. if (msg->front.iov_len < sizeof(*rhead))
  964. goto bad;
  965. numops = le32_to_cpu(rhead->num_ops);
  966. object_len = le32_to_cpu(rhead->object_len);
  967. result = le32_to_cpu(rhead->result);
  968. if (msg->front.iov_len != sizeof(*rhead) + object_len +
  969. numops * sizeof(struct ceph_osd_op))
  970. goto bad;
  971. dout("handle_reply %p tid %llu result %d\n", msg, tid, (int)result);
  972. /* lookup */
  973. mutex_lock(&osdc->request_mutex);
  974. req = __lookup_request(osdc, tid);
  975. if (req == NULL) {
  976. dout("handle_reply tid %llu dne\n", tid);
  977. mutex_unlock(&osdc->request_mutex);
  978. return;
  979. }
  980. ceph_osdc_get_request(req);
  981. flags = le32_to_cpu(rhead->flags);
  982. /*
  983. * if this connection filled our message, drop our reference now, to
  984. * avoid a (safe but slower) revoke later.
  985. */
  986. if (req->r_con_filling_msg == con && req->r_reply == msg) {
  987. dout(" dropping con_filling_msg ref %p\n", con);
  988. req->r_con_filling_msg = NULL;
  989. con->ops->put(con);
  990. }
  991. if (!req->r_got_reply) {
  992. unsigned int bytes;
  993. req->r_result = le32_to_cpu(rhead->result);
  994. bytes = le32_to_cpu(msg->hdr.data_len);
  995. dout("handle_reply result %d bytes %d\n", req->r_result,
  996. bytes);
  997. if (req->r_result == 0)
  998. req->r_result = bytes;
  999. /* in case this is a write and we need to replay, */
  1000. req->r_reassert_version = rhead->reassert_version;
  1001. req->r_got_reply = 1;
  1002. } else if ((flags & CEPH_OSD_FLAG_ONDISK) == 0) {
  1003. dout("handle_reply tid %llu dup ack\n", tid);
  1004. mutex_unlock(&osdc->request_mutex);
  1005. goto done;
  1006. }
  1007. dout("handle_reply tid %llu flags %d\n", tid, flags);
  1008. if (req->r_linger && (flags & CEPH_OSD_FLAG_ONDISK))
  1009. __register_linger_request(osdc, req);
  1010. /* either this is a read, or we got the safe response */
  1011. if (result < 0 ||
  1012. (flags & CEPH_OSD_FLAG_ONDISK) ||
  1013. ((flags & CEPH_OSD_FLAG_WRITE) == 0))
  1014. __unregister_request(osdc, req);
  1015. mutex_unlock(&osdc->request_mutex);
  1016. if (req->r_callback)
  1017. req->r_callback(req, msg);
  1018. else
  1019. complete_all(&req->r_completion);
  1020. if (flags & CEPH_OSD_FLAG_ONDISK)
  1021. complete_request(req);
  1022. done:
  1023. dout("req=%p req->r_linger=%d\n", req, req->r_linger);
  1024. ceph_osdc_put_request(req);
  1025. return;
  1026. bad:
  1027. pr_err("corrupt osd_op_reply got %d %d expected %d\n",
  1028. (int)msg->front.iov_len, le32_to_cpu(msg->hdr.front_len),
  1029. (int)sizeof(*rhead));
  1030. ceph_msg_dump(msg);
  1031. }
  1032. static void reset_changed_osds(struct ceph_osd_client *osdc)
  1033. {
  1034. struct rb_node *p, *n;
  1035. for (p = rb_first(&osdc->osds); p; p = n) {
  1036. struct ceph_osd *osd = rb_entry(p, struct ceph_osd, o_node);
  1037. n = rb_next(p);
  1038. if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) ||
  1039. memcmp(&osd->o_con.peer_addr,
  1040. ceph_osd_addr(osdc->osdmap,
  1041. osd->o_osd),
  1042. sizeof(struct ceph_entity_addr)) != 0)
  1043. __reset_osd(osdc, osd);
  1044. }
  1045. }
  1046. /*
  1047. * Requeue requests whose mapping to an OSD has changed. If requests map to
  1048. * no osd, request a new map.
  1049. *
  1050. * Caller should hold map_sem for read.
  1051. */
  1052. static void kick_requests(struct ceph_osd_client *osdc, int force_resend)
  1053. {
  1054. struct ceph_osd_request *req, *nreq;
  1055. struct rb_node *p;
  1056. int needmap = 0;
  1057. int err;
  1058. dout("kick_requests %s\n", force_resend ? " (force resend)" : "");
  1059. mutex_lock(&osdc->request_mutex);
  1060. for (p = rb_first(&osdc->requests); p; ) {
  1061. req = rb_entry(p, struct ceph_osd_request, r_node);
  1062. p = rb_next(p);
  1063. /*
  1064. * For linger requests that have not yet been
  1065. * registered, move them to the linger list; they'll
  1066. * be sent to the osd in the loop below. Unregister
  1067. * the request before re-registering it as a linger
  1068. * request to ensure the __map_request() below
  1069. * will decide it needs to be sent.
  1070. */
  1071. if (req->r_linger && list_empty(&req->r_linger_item)) {
  1072. dout("%p tid %llu restart on osd%d\n",
  1073. req, req->r_tid,
  1074. req->r_osd ? req->r_osd->o_osd : -1);
  1075. __unregister_request(osdc, req);
  1076. __register_linger_request(osdc, req);
  1077. continue;
  1078. }
  1079. err = __map_request(osdc, req, force_resend);
  1080. if (err < 0)
  1081. continue; /* error */
  1082. if (req->r_osd == NULL) {
  1083. dout("%p tid %llu maps to no osd\n", req, req->r_tid);
  1084. needmap++; /* request a newer map */
  1085. } else if (err > 0) {
  1086. if (!req->r_linger) {
  1087. dout("%p tid %llu requeued on osd%d\n", req,
  1088. req->r_tid,
  1089. req->r_osd ? req->r_osd->o_osd : -1);
  1090. req->r_flags |= CEPH_OSD_FLAG_RETRY;
  1091. }
  1092. }
  1093. }
  1094. list_for_each_entry_safe(req, nreq, &osdc->req_linger,
  1095. r_linger_item) {
  1096. dout("linger req=%p req->r_osd=%p\n", req, req->r_osd);
  1097. err = __map_request(osdc, req, force_resend);
  1098. dout("__map_request returned %d\n", err);
  1099. if (err == 0)
  1100. continue; /* no change and no osd was specified */
  1101. if (err < 0)
  1102. continue; /* hrm! */
  1103. if (req->r_osd == NULL) {
  1104. dout("tid %llu maps to no valid osd\n", req->r_tid);
  1105. needmap++; /* request a newer map */
  1106. continue;
  1107. }
  1108. dout("kicking lingering %p tid %llu osd%d\n", req, req->r_tid,
  1109. req->r_osd ? req->r_osd->o_osd : -1);
  1110. __register_request(osdc, req);
  1111. __unregister_linger_request(osdc, req);
  1112. }
  1113. mutex_unlock(&osdc->request_mutex);
  1114. if (needmap) {
  1115. dout("%d requests for down osds, need new map\n", needmap);
  1116. ceph_monc_request_next_osdmap(&osdc->client->monc);
  1117. }
  1118. reset_changed_osds(osdc);
  1119. }
  1120. /*
  1121. * Process updated osd map.
  1122. *
  1123. * The message contains any number of incremental and full maps, normally
  1124. * indicating some sort of topology change in the cluster. Kick requests
  1125. * off to different OSDs as needed.
  1126. */
  1127. void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg)
  1128. {
  1129. void *p, *end, *next;
  1130. u32 nr_maps, maplen;
  1131. u32 epoch;
  1132. struct ceph_osdmap *newmap = NULL, *oldmap;
  1133. int err;
  1134. struct ceph_fsid fsid;
  1135. dout("handle_map have %u\n", osdc->osdmap ? osdc->osdmap->epoch : 0);
  1136. p = msg->front.iov_base;
  1137. end = p + msg->front.iov_len;
  1138. /* verify fsid */
  1139. ceph_decode_need(&p, end, sizeof(fsid), bad);
  1140. ceph_decode_copy(&p, &fsid, sizeof(fsid));
  1141. if (ceph_check_fsid(osdc->client, &fsid) < 0)
  1142. return;
  1143. down_write(&osdc->map_sem);
  1144. /* incremental maps */
  1145. ceph_decode_32_safe(&p, end, nr_maps, bad);
  1146. dout(" %d inc maps\n", nr_maps);
  1147. while (nr_maps > 0) {
  1148. ceph_decode_need(&p, end, 2*sizeof(u32), bad);
  1149. epoch = ceph_decode_32(&p);
  1150. maplen = ceph_decode_32(&p);
  1151. ceph_decode_need(&p, end, maplen, bad);
  1152. next = p + maplen;
  1153. if (osdc->osdmap && osdc->osdmap->epoch+1 == epoch) {
  1154. dout("applying incremental map %u len %d\n",
  1155. epoch, maplen);
  1156. newmap = osdmap_apply_incremental(&p, next,
  1157. osdc->osdmap,
  1158. &osdc->client->msgr);
  1159. if (IS_ERR(newmap)) {
  1160. err = PTR_ERR(newmap);
  1161. goto bad;
  1162. }
  1163. BUG_ON(!newmap);
  1164. if (newmap != osdc->osdmap) {
  1165. ceph_osdmap_destroy(osdc->osdmap);
  1166. osdc->osdmap = newmap;
  1167. }
  1168. kick_requests(osdc, 0);
  1169. } else {
  1170. dout("ignoring incremental map %u len %d\n",
  1171. epoch, maplen);
  1172. }
  1173. p = next;
  1174. nr_maps--;
  1175. }
  1176. if (newmap)
  1177. goto done;
  1178. /* full maps */
  1179. ceph_decode_32_safe(&p, end, nr_maps, bad);
  1180. dout(" %d full maps\n", nr_maps);
  1181. while (nr_maps) {
  1182. ceph_decode_need(&p, end, 2*sizeof(u32), bad);
  1183. epoch = ceph_decode_32(&p);
  1184. maplen = ceph_decode_32(&p);
  1185. ceph_decode_need(&p, end, maplen, bad);
  1186. if (nr_maps > 1) {
  1187. dout("skipping non-latest full map %u len %d\n",
  1188. epoch, maplen);
  1189. } else if (osdc->osdmap && osdc->osdmap->epoch >= epoch) {
  1190. dout("skipping full map %u len %d, "
  1191. "older than our %u\n", epoch, maplen,
  1192. osdc->osdmap->epoch);
  1193. } else {
  1194. int skipped_map = 0;
  1195. dout("taking full map %u len %d\n", epoch, maplen);
  1196. newmap = osdmap_decode(&p, p+maplen);
  1197. if (IS_ERR(newmap)) {
  1198. err = PTR_ERR(newmap);
  1199. goto bad;
  1200. }
  1201. BUG_ON(!newmap);
  1202. oldmap = osdc->osdmap;
  1203. osdc->osdmap = newmap;
  1204. if (oldmap) {
  1205. if (oldmap->epoch + 1 < newmap->epoch)
  1206. skipped_map = 1;
  1207. ceph_osdmap_destroy(oldmap);
  1208. }
  1209. kick_requests(osdc, skipped_map);
  1210. }
  1211. p += maplen;
  1212. nr_maps--;
  1213. }
  1214. done:
  1215. downgrade_write(&osdc->map_sem);
  1216. ceph_monc_got_osdmap(&osdc->client->monc, osdc->osdmap->epoch);
  1217. /*
  1218. * subscribe to subsequent osdmap updates if full to ensure
  1219. * we find out when we are no longer full and stop returning
  1220. * ENOSPC.
  1221. */
  1222. if (ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL))
  1223. ceph_monc_request_next_osdmap(&osdc->client->monc);
  1224. send_queued(osdc);
  1225. up_read(&osdc->map_sem);
  1226. wake_up_all(&osdc->client->auth_wq);
  1227. return;
  1228. bad:
  1229. pr_err("osdc handle_map corrupt msg\n");
  1230. ceph_msg_dump(msg);
  1231. up_write(&osdc->map_sem);
  1232. return;
  1233. }
  1234. /*
  1235. * watch/notify callback event infrastructure
  1236. *
  1237. * These callbacks are used both for watch and notify operations.
  1238. */
  1239. static void __release_event(struct kref *kref)
  1240. {
  1241. struct ceph_osd_event *event =
  1242. container_of(kref, struct ceph_osd_event, kref);
  1243. dout("__release_event %p\n", event);
  1244. kfree(event);
  1245. }
  1246. static void get_event(struct ceph_osd_event *event)
  1247. {
  1248. kref_get(&event->kref);
  1249. }
  1250. void ceph_osdc_put_event(struct ceph_osd_event *event)
  1251. {
  1252. kref_put(&event->kref, __release_event);
  1253. }
  1254. EXPORT_SYMBOL(ceph_osdc_put_event);
  1255. static void __insert_event(struct ceph_osd_client *osdc,
  1256. struct ceph_osd_event *new)
  1257. {
  1258. struct rb_node **p = &osdc->event_tree.rb_node;
  1259. struct rb_node *parent = NULL;
  1260. struct ceph_osd_event *event = NULL;
  1261. while (*p) {
  1262. parent = *p;
  1263. event = rb_entry(parent, struct ceph_osd_event, node);
  1264. if (new->cookie < event->cookie)
  1265. p = &(*p)->rb_left;
  1266. else if (new->cookie > event->cookie)
  1267. p = &(*p)->rb_right;
  1268. else
  1269. BUG();
  1270. }
  1271. rb_link_node(&new->node, parent, p);
  1272. rb_insert_color(&new->node, &osdc->event_tree);
  1273. }
  1274. static struct ceph_osd_event *__find_event(struct ceph_osd_client *osdc,
  1275. u64 cookie)
  1276. {
  1277. struct rb_node **p = &osdc->event_tree.rb_node;
  1278. struct rb_node *parent = NULL;
  1279. struct ceph_osd_event *event = NULL;
  1280. while (*p) {
  1281. parent = *p;
  1282. event = rb_entry(parent, struct ceph_osd_event, node);
  1283. if (cookie < event->cookie)
  1284. p = &(*p)->rb_left;
  1285. else if (cookie > event->cookie)
  1286. p = &(*p)->rb_right;
  1287. else
  1288. return event;
  1289. }
  1290. return NULL;
  1291. }
  1292. static void __remove_event(struct ceph_osd_event *event)
  1293. {
  1294. struct ceph_osd_client *osdc = event->osdc;
  1295. if (!RB_EMPTY_NODE(&event->node)) {
  1296. dout("__remove_event removed %p\n", event);
  1297. rb_erase(&event->node, &osdc->event_tree);
  1298. ceph_osdc_put_event(event);
  1299. } else {
  1300. dout("__remove_event didn't remove %p\n", event);
  1301. }
  1302. }
  1303. int ceph_osdc_create_event(struct ceph_osd_client *osdc,
  1304. void (*event_cb)(u64, u64, u8, void *),
  1305. int one_shot, void *data,
  1306. struct ceph_osd_event **pevent)
  1307. {
  1308. struct ceph_osd_event *event;
  1309. event = kmalloc(sizeof(*event), GFP_NOIO);
  1310. if (!event)
  1311. return -ENOMEM;
  1312. dout("create_event %p\n", event);
  1313. event->cb = event_cb;
  1314. event->one_shot = one_shot;
  1315. event->data = data;
  1316. event->osdc = osdc;
  1317. INIT_LIST_HEAD(&event->osd_node);
  1318. RB_CLEAR_NODE(&event->node);
  1319. kref_init(&event->kref); /* one ref for us */
  1320. kref_get(&event->kref); /* one ref for the caller */
  1321. init_completion(&event->completion);
  1322. spin_lock(&osdc->event_lock);
  1323. event->cookie = ++osdc->event_count;
  1324. __insert_event(osdc, event);
  1325. spin_unlock(&osdc->event_lock);
  1326. *pevent = event;
  1327. return 0;
  1328. }
  1329. EXPORT_SYMBOL(ceph_osdc_create_event);
  1330. void ceph_osdc_cancel_event(struct ceph_osd_event *event)
  1331. {
  1332. struct ceph_osd_client *osdc = event->osdc;
  1333. dout("cancel_event %p\n", event);
  1334. spin_lock(&osdc->event_lock);
  1335. __remove_event(event);
  1336. spin_unlock(&osdc->event_lock);
  1337. ceph_osdc_put_event(event); /* caller's */
  1338. }
  1339. EXPORT_SYMBOL(ceph_osdc_cancel_event);
  1340. static void do_event_work(struct work_struct *work)
  1341. {
  1342. struct ceph_osd_event_work *event_work =
  1343. container_of(work, struct ceph_osd_event_work, work);
  1344. struct ceph_osd_event *event = event_work->event;
  1345. u64 ver = event_work->ver;
  1346. u64 notify_id = event_work->notify_id;
  1347. u8 opcode = event_work->opcode;
  1348. dout("do_event_work completing %p\n", event);
  1349. event->cb(ver, notify_id, opcode, event->data);
  1350. complete(&event->completion);
  1351. dout("do_event_work completed %p\n", event);
  1352. ceph_osdc_put_event(event);
  1353. kfree(event_work);
  1354. }
  1355. /*
  1356. * Process osd watch notifications
  1357. */
  1358. void handle_watch_notify(struct ceph_osd_client *osdc, struct ceph_msg *msg)
  1359. {
  1360. void *p, *end;
  1361. u8 proto_ver;
  1362. u64 cookie, ver, notify_id;
  1363. u8 opcode;
  1364. struct ceph_osd_event *event;
  1365. struct ceph_osd_event_work *event_work;
  1366. p = msg->front.iov_base;
  1367. end = p + msg->front.iov_len;
  1368. ceph_decode_8_safe(&p, end, proto_ver, bad);
  1369. ceph_decode_8_safe(&p, end, opcode, bad);
  1370. ceph_decode_64_safe(&p, end, cookie, bad);
  1371. ceph_decode_64_safe(&p, end, ver, bad);
  1372. ceph_decode_64_safe(&p, end, notify_id, bad);
  1373. spin_lock(&osdc->event_lock);
  1374. event = __find_event(osdc, cookie);
  1375. if (event) {
  1376. get_event(event);
  1377. if (event->one_shot)
  1378. __remove_event(event);
  1379. }
  1380. spin_unlock(&osdc->event_lock);
  1381. dout("handle_watch_notify cookie %lld ver %lld event %p\n",
  1382. cookie, ver, event);
  1383. if (event) {
  1384. event_work = kmalloc(sizeof(*event_work), GFP_NOIO);
  1385. if (!event_work) {
  1386. dout("ERROR: could not allocate event_work\n");
  1387. goto done_err;
  1388. }
  1389. INIT_WORK(&event_work->work, do_event_work);
  1390. event_work->event = event;
  1391. event_work->ver = ver;
  1392. event_work->notify_id = notify_id;
  1393. event_work->opcode = opcode;
  1394. if (!queue_work(osdc->notify_wq, &event_work->work)) {
  1395. dout("WARNING: failed to queue notify event work\n");
  1396. goto done_err;
  1397. }
  1398. }
  1399. return;
  1400. done_err:
  1401. complete(&event->completion);
  1402. ceph_osdc_put_event(event);
  1403. return;
  1404. bad:
  1405. pr_err("osdc handle_watch_notify corrupt msg\n");
  1406. return;
  1407. }
  1408. int ceph_osdc_wait_event(struct ceph_osd_event *event, unsigned long timeout)
  1409. {
  1410. int err;
  1411. dout("wait_event %p\n", event);
  1412. err = wait_for_completion_interruptible_timeout(&event->completion,
  1413. timeout * HZ);
  1414. ceph_osdc_put_event(event);
  1415. if (err > 0)
  1416. err = 0;
  1417. dout("wait_event %p returns %d\n", event, err);
  1418. return err;
  1419. }
  1420. EXPORT_SYMBOL(ceph_osdc_wait_event);
  1421. /*
  1422. * Register request, send initial attempt.
  1423. */
  1424. int ceph_osdc_start_request(struct ceph_osd_client *osdc,
  1425. struct ceph_osd_request *req,
  1426. bool nofail)
  1427. {
  1428. int rc = 0;
  1429. req->r_request->pages = req->r_pages;
  1430. req->r_request->nr_pages = req->r_num_pages;
  1431. #ifdef CONFIG_BLOCK
  1432. req->r_request->bio = req->r_bio;
  1433. #endif
  1434. req->r_request->trail = &req->r_trail;
  1435. register_request(osdc, req);
  1436. down_read(&osdc->map_sem);
  1437. mutex_lock(&osdc->request_mutex);
  1438. /*
  1439. * a racing kick_requests() may have sent the message for us
  1440. * while we dropped request_mutex above, so only send now if
  1441. * the request still han't been touched yet.
  1442. */
  1443. if (req->r_sent == 0) {
  1444. rc = __map_request(osdc, req, 0);
  1445. if (rc < 0) {
  1446. if (nofail) {
  1447. dout("osdc_start_request failed map, "
  1448. " will retry %lld\n", req->r_tid);
  1449. rc = 0;
  1450. }
  1451. goto out_unlock;
  1452. }
  1453. if (req->r_osd == NULL) {
  1454. dout("send_request %p no up osds in pg\n", req);
  1455. ceph_monc_request_next_osdmap(&osdc->client->monc);
  1456. } else {
  1457. __send_request(osdc, req);
  1458. }
  1459. rc = 0;
  1460. }
  1461. out_unlock:
  1462. mutex_unlock(&osdc->request_mutex);
  1463. up_read(&osdc->map_sem);
  1464. return rc;
  1465. }
  1466. EXPORT_SYMBOL(ceph_osdc_start_request);
  1467. /*
  1468. * wait for a request to complete
  1469. */
  1470. int ceph_osdc_wait_request(struct ceph_osd_client *osdc,
  1471. struct ceph_osd_request *req)
  1472. {
  1473. int rc;
  1474. rc = wait_for_completion_interruptible(&req->r_completion);
  1475. if (rc < 0) {
  1476. mutex_lock(&osdc->request_mutex);
  1477. __cancel_request(req);
  1478. __unregister_request(osdc, req);
  1479. mutex_unlock(&osdc->request_mutex);
  1480. complete_request(req);
  1481. dout("wait_request tid %llu canceled/timed out\n", req->r_tid);
  1482. return rc;
  1483. }
  1484. dout("wait_request tid %llu result %d\n", req->r_tid, req->r_result);
  1485. return req->r_result;
  1486. }
  1487. EXPORT_SYMBOL(ceph_osdc_wait_request);
  1488. /*
  1489. * sync - wait for all in-flight requests to flush. avoid starvation.
  1490. */
  1491. void ceph_osdc_sync(struct ceph_osd_client *osdc)
  1492. {
  1493. struct ceph_osd_request *req;
  1494. u64 last_tid, next_tid = 0;
  1495. mutex_lock(&osdc->request_mutex);
  1496. last_tid = osdc->last_tid;
  1497. while (1) {
  1498. req = __lookup_request_ge(osdc, next_tid);
  1499. if (!req)
  1500. break;
  1501. if (req->r_tid > last_tid)
  1502. break;
  1503. next_tid = req->r_tid + 1;
  1504. if ((req->r_flags & CEPH_OSD_FLAG_WRITE) == 0)
  1505. continue;
  1506. ceph_osdc_get_request(req);
  1507. mutex_unlock(&osdc->request_mutex);
  1508. dout("sync waiting on tid %llu (last is %llu)\n",
  1509. req->r_tid, last_tid);
  1510. wait_for_completion(&req->r_safe_completion);
  1511. mutex_lock(&osdc->request_mutex);
  1512. ceph_osdc_put_request(req);
  1513. }
  1514. mutex_unlock(&osdc->request_mutex);
  1515. dout("sync done (thru tid %llu)\n", last_tid);
  1516. }
  1517. EXPORT_SYMBOL(ceph_osdc_sync);
  1518. /*
  1519. * init, shutdown
  1520. */
  1521. int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client)
  1522. {
  1523. int err;
  1524. dout("init\n");
  1525. osdc->client = client;
  1526. osdc->osdmap = NULL;
  1527. init_rwsem(&osdc->map_sem);
  1528. init_completion(&osdc->map_waiters);
  1529. osdc->last_requested_map = 0;
  1530. mutex_init(&osdc->request_mutex);
  1531. osdc->last_tid = 0;
  1532. osdc->osds = RB_ROOT;
  1533. INIT_LIST_HEAD(&osdc->osd_lru);
  1534. osdc->requests = RB_ROOT;
  1535. INIT_LIST_HEAD(&osdc->req_lru);
  1536. INIT_LIST_HEAD(&osdc->req_unsent);
  1537. INIT_LIST_HEAD(&osdc->req_notarget);
  1538. INIT_LIST_HEAD(&osdc->req_linger);
  1539. osdc->num_requests = 0;
  1540. INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout);
  1541. INIT_DELAYED_WORK(&osdc->osds_timeout_work, handle_osds_timeout);
  1542. spin_lock_init(&osdc->event_lock);
  1543. osdc->event_tree = RB_ROOT;
  1544. osdc->event_count = 0;
  1545. schedule_delayed_work(&osdc->osds_timeout_work,
  1546. round_jiffies_relative(osdc->client->options->osd_idle_ttl * HZ));
  1547. err = -ENOMEM;
  1548. osdc->req_mempool = mempool_create_kmalloc_pool(10,
  1549. sizeof(struct ceph_osd_request));
  1550. if (!osdc->req_mempool)
  1551. goto out;
  1552. err = ceph_msgpool_init(&osdc->msgpool_op, CEPH_MSG_OSD_OP,
  1553. OSD_OP_FRONT_LEN, 10, true,
  1554. "osd_op");
  1555. if (err < 0)
  1556. goto out_mempool;
  1557. err = ceph_msgpool_init(&osdc->msgpool_op_reply, CEPH_MSG_OSD_OPREPLY,
  1558. OSD_OPREPLY_FRONT_LEN, 10, true,
  1559. "osd_op_reply");
  1560. if (err < 0)
  1561. goto out_msgpool;
  1562. osdc->notify_wq = create_singlethread_workqueue("ceph-watch-notify");
  1563. if (IS_ERR(osdc->notify_wq)) {
  1564. err = PTR_ERR(osdc->notify_wq);
  1565. osdc->notify_wq = NULL;
  1566. goto out_msgpool;
  1567. }
  1568. return 0;
  1569. out_msgpool:
  1570. ceph_msgpool_destroy(&osdc->msgpool_op);
  1571. out_mempool:
  1572. mempool_destroy(osdc->req_mempool);
  1573. out:
  1574. return err;
  1575. }
  1576. EXPORT_SYMBOL(ceph_osdc_init);
  1577. void ceph_osdc_stop(struct ceph_osd_client *osdc)
  1578. {
  1579. flush_workqueue(osdc->notify_wq);
  1580. destroy_workqueue(osdc->notify_wq);
  1581. cancel_delayed_work_sync(&osdc->timeout_work);
  1582. cancel_delayed_work_sync(&osdc->osds_timeout_work);
  1583. if (osdc->osdmap) {
  1584. ceph_osdmap_destroy(osdc->osdmap);
  1585. osdc->osdmap = NULL;
  1586. }
  1587. remove_all_osds(osdc);
  1588. mempool_destroy(osdc->req_mempool);
  1589. ceph_msgpool_destroy(&osdc->msgpool_op);
  1590. ceph_msgpool_destroy(&osdc->msgpool_op_reply);
  1591. }
  1592. EXPORT_SYMBOL(ceph_osdc_stop);
  1593. /*
  1594. * Read some contiguous pages. If we cross a stripe boundary, shorten
  1595. * *plen. Return number of bytes read, or error.
  1596. */
  1597. int ceph_osdc_readpages(struct ceph_osd_client *osdc,
  1598. struct ceph_vino vino, struct ceph_file_layout *layout,
  1599. u64 off, u64 *plen,
  1600. u32 truncate_seq, u64 truncate_size,
  1601. struct page **pages, int num_pages, int page_align)
  1602. {
  1603. struct ceph_osd_request *req;
  1604. int rc = 0;
  1605. dout("readpages on ino %llx.%llx on %llu~%llu\n", vino.ino,
  1606. vino.snap, off, *plen);
  1607. req = ceph_osdc_new_request(osdc, layout, vino, off, plen,
  1608. CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ,
  1609. NULL, 0, truncate_seq, truncate_size, NULL,
  1610. false, 1, page_align);
  1611. if (IS_ERR(req))
  1612. return PTR_ERR(req);
  1613. /* it may be a short read due to an object boundary */
  1614. req->r_pages = pages;
  1615. dout("readpages final extent is %llu~%llu (%d pages align %d)\n",
  1616. off, *plen, req->r_num_pages, page_align);
  1617. rc = ceph_osdc_start_request(osdc, req, false);
  1618. if (!rc)
  1619. rc = ceph_osdc_wait_request(osdc, req);
  1620. ceph_osdc_put_request(req);
  1621. dout("readpages result %d\n", rc);
  1622. return rc;
  1623. }
  1624. EXPORT_SYMBOL(ceph_osdc_readpages);
  1625. /*
  1626. * do a synchronous write on N pages
  1627. */
  1628. int ceph_osdc_writepages(struct ceph_osd_client *osdc, struct ceph_vino vino,
  1629. struct ceph_file_layout *layout,
  1630. struct ceph_snap_context *snapc,
  1631. u64 off, u64 len,
  1632. u32 truncate_seq, u64 truncate_size,
  1633. struct timespec *mtime,
  1634. struct page **pages, int num_pages)
  1635. {
  1636. struct ceph_osd_request *req;
  1637. int rc = 0;
  1638. int page_align = off & ~PAGE_MASK;
  1639. BUG_ON(vino.snap != CEPH_NOSNAP);
  1640. req = ceph_osdc_new_request(osdc, layout, vino, off, &len,
  1641. CEPH_OSD_OP_WRITE,
  1642. CEPH_OSD_FLAG_ONDISK | CEPH_OSD_FLAG_WRITE,
  1643. snapc, 0,
  1644. truncate_seq, truncate_size, mtime,
  1645. true, 1, page_align);
  1646. if (IS_ERR(req))
  1647. return PTR_ERR(req);
  1648. /* it may be a short write due to an object boundary */
  1649. req->r_pages = pages;
  1650. dout("writepages %llu~%llu (%d pages)\n", off, len,
  1651. req->r_num_pages);
  1652. rc = ceph_osdc_start_request(osdc, req, true);
  1653. if (!rc)
  1654. rc = ceph_osdc_wait_request(osdc, req);
  1655. ceph_osdc_put_request(req);
  1656. if (rc == 0)
  1657. rc = len;
  1658. dout("writepages result %d\n", rc);
  1659. return rc;
  1660. }
  1661. EXPORT_SYMBOL(ceph_osdc_writepages);
  1662. /*
  1663. * handle incoming message
  1664. */
  1665. static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
  1666. {
  1667. struct ceph_osd *osd = con->private;
  1668. struct ceph_osd_client *osdc;
  1669. int type = le16_to_cpu(msg->hdr.type);
  1670. if (!osd)
  1671. goto out;
  1672. osdc = osd->o_osdc;
  1673. switch (type) {
  1674. case CEPH_MSG_OSD_MAP:
  1675. ceph_osdc_handle_map(osdc, msg);
  1676. break;
  1677. case CEPH_MSG_OSD_OPREPLY:
  1678. handle_reply(osdc, msg, con);
  1679. break;
  1680. case CEPH_MSG_WATCH_NOTIFY:
  1681. handle_watch_notify(osdc, msg);
  1682. break;
  1683. default:
  1684. pr_err("received unknown message type %d %s\n", type,
  1685. ceph_msg_type_name(type));
  1686. }
  1687. out:
  1688. ceph_msg_put(msg);
  1689. }
  1690. /*
  1691. * lookup and return message for incoming reply. set up reply message
  1692. * pages.
  1693. */
  1694. static struct ceph_msg *get_reply(struct ceph_connection *con,
  1695. struct ceph_msg_header *hdr,
  1696. int *skip)
  1697. {
  1698. struct ceph_osd *osd = con->private;
  1699. struct ceph_osd_client *osdc = osd->o_osdc;
  1700. struct ceph_msg *m;
  1701. struct ceph_osd_request *req;
  1702. int front = le32_to_cpu(hdr->front_len);
  1703. int data_len = le32_to_cpu(hdr->data_len);
  1704. u64 tid;
  1705. tid = le64_to_cpu(hdr->tid);
  1706. mutex_lock(&osdc->request_mutex);
  1707. req = __lookup_request(osdc, tid);
  1708. if (!req) {
  1709. *skip = 1;
  1710. m = NULL;
  1711. dout("get_reply unknown tid %llu from osd%d\n", tid,
  1712. osd->o_osd);
  1713. goto out;
  1714. }
  1715. if (req->r_con_filling_msg) {
  1716. dout("%s revoking msg %p from old con %p\n", __func__,
  1717. req->r_reply, req->r_con_filling_msg);
  1718. ceph_msg_revoke_incoming(req->r_reply);
  1719. req->r_con_filling_msg->ops->put(req->r_con_filling_msg);
  1720. req->r_con_filling_msg = NULL;
  1721. }
  1722. if (front > req->r_reply->front.iov_len) {
  1723. pr_warning("get_reply front %d > preallocated %d\n",
  1724. front, (int)req->r_reply->front.iov_len);
  1725. m = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, front, GFP_NOFS, false);
  1726. if (!m)
  1727. goto out;
  1728. ceph_msg_put(req->r_reply);
  1729. req->r_reply = m;
  1730. }
  1731. m = ceph_msg_get(req->r_reply);
  1732. if (data_len > 0) {
  1733. int want = calc_pages_for(req->r_page_alignment, data_len);
  1734. if (req->r_pages && unlikely(req->r_num_pages < want)) {
  1735. pr_warning("tid %lld reply has %d bytes %d pages, we"
  1736. " had only %d pages ready\n", tid, data_len,
  1737. want, req->r_num_pages);
  1738. *skip = 1;
  1739. ceph_msg_put(m);
  1740. m = NULL;
  1741. goto out;
  1742. }
  1743. m->pages = req->r_pages;
  1744. m->nr_pages = req->r_num_pages;
  1745. m->page_alignment = req->r_page_alignment;
  1746. #ifdef CONFIG_BLOCK
  1747. m->bio = req->r_bio;
  1748. #endif
  1749. }
  1750. *skip = 0;
  1751. req->r_con_filling_msg = con->ops->get(con);
  1752. dout("get_reply tid %lld %p\n", tid, m);
  1753. out:
  1754. mutex_unlock(&osdc->request_mutex);
  1755. return m;
  1756. }
  1757. static struct ceph_msg *alloc_msg(struct ceph_connection *con,
  1758. struct ceph_msg_header *hdr,
  1759. int *skip)
  1760. {
  1761. struct ceph_osd *osd = con->private;
  1762. int type = le16_to_cpu(hdr->type);
  1763. int front = le32_to_cpu(hdr->front_len);
  1764. *skip = 0;
  1765. switch (type) {
  1766. case CEPH_MSG_OSD_MAP:
  1767. case CEPH_MSG_WATCH_NOTIFY:
  1768. return ceph_msg_new(type, front, GFP_NOFS, false);
  1769. case CEPH_MSG_OSD_OPREPLY:
  1770. return get_reply(con, hdr, skip);
  1771. default:
  1772. pr_info("alloc_msg unexpected msg type %d from osd%d\n", type,
  1773. osd->o_osd);
  1774. *skip = 1;
  1775. return NULL;
  1776. }
  1777. }
  1778. /*
  1779. * Wrappers to refcount containing ceph_osd struct
  1780. */
  1781. static struct ceph_connection *get_osd_con(struct ceph_connection *con)
  1782. {
  1783. struct ceph_osd *osd = con->private;
  1784. if (get_osd(osd))
  1785. return con;
  1786. return NULL;
  1787. }
  1788. static void put_osd_con(struct ceph_connection *con)
  1789. {
  1790. struct ceph_osd *osd = con->private;
  1791. put_osd(osd);
  1792. }
  1793. /*
  1794. * authentication
  1795. */
  1796. /*
  1797. * Note: returned pointer is the address of a structure that's
  1798. * managed separately. Caller must *not* attempt to free it.
  1799. */
  1800. static struct ceph_auth_handshake *get_authorizer(struct ceph_connection *con,
  1801. int *proto, int force_new)
  1802. {
  1803. struct ceph_osd *o = con->private;
  1804. struct ceph_osd_client *osdc = o->o_osdc;
  1805. struct ceph_auth_client *ac = osdc->client->monc.auth;
  1806. struct ceph_auth_handshake *auth = &o->o_auth;
  1807. if (force_new && auth->authorizer) {
  1808. if (ac->ops && ac->ops->destroy_authorizer)
  1809. ac->ops->destroy_authorizer(ac, auth->authorizer);
  1810. auth->authorizer = NULL;
  1811. }
  1812. if (!auth->authorizer && ac->ops && ac->ops->create_authorizer) {
  1813. int ret = ac->ops->create_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
  1814. auth);
  1815. if (ret)
  1816. return ERR_PTR(ret);
  1817. }
  1818. *proto = ac->protocol;
  1819. return auth;
  1820. }
  1821. static int verify_authorizer_reply(struct ceph_connection *con, int len)
  1822. {
  1823. struct ceph_osd *o = con->private;
  1824. struct ceph_osd_client *osdc = o->o_osdc;
  1825. struct ceph_auth_client *ac = osdc->client->monc.auth;
  1826. /*
  1827. * XXX If ac->ops or ac->ops->verify_authorizer_reply is null,
  1828. * XXX which do we do: succeed or fail?
  1829. */
  1830. return ac->ops->verify_authorizer_reply(ac, o->o_auth.authorizer, len);
  1831. }
  1832. static int invalidate_authorizer(struct ceph_connection *con)
  1833. {
  1834. struct ceph_osd *o = con->private;
  1835. struct ceph_osd_client *osdc = o->o_osdc;
  1836. struct ceph_auth_client *ac = osdc->client->monc.auth;
  1837. if (ac->ops && ac->ops->invalidate_authorizer)
  1838. ac->ops->invalidate_authorizer(ac, CEPH_ENTITY_TYPE_OSD);
  1839. return ceph_monc_validate_auth(&osdc->client->monc);
  1840. }
  1841. static const struct ceph_connection_operations osd_con_ops = {
  1842. .get = get_osd_con,
  1843. .put = put_osd_con,
  1844. .dispatch = dispatch,
  1845. .get_authorizer = get_authorizer,
  1846. .verify_authorizer_reply = verify_authorizer_reply,
  1847. .invalidate_authorizer = invalidate_authorizer,
  1848. .alloc_msg = alloc_msg,
  1849. .fault = osd_reset,
  1850. };