osd_client.c 55 KB

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