osd_client.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368
  1. #include "ceph_debug.h"
  2. #include <linux/err.h>
  3. #include <linux/highmem.h>
  4. #include <linux/mm.h>
  5. #include <linux/pagemap.h>
  6. #include <linux/slab.h>
  7. #include <linux/uaccess.h>
  8. #include "super.h"
  9. #include "osd_client.h"
  10. #include "messenger.h"
  11. #include "decode.h"
  12. #include "auth.h"
  13. const static struct ceph_connection_operations osd_con_ops;
  14. static void kick_requests(struct ceph_osd_client *osdc, struct ceph_osd *osd);
  15. /*
  16. * Implement client access to distributed object storage cluster.
  17. *
  18. * All data objects are stored within a cluster/cloud of OSDs, or
  19. * "object storage devices." (Note that Ceph OSDs have _nothing_ to
  20. * do with the T10 OSD extensions to SCSI.) Ceph OSDs are simply
  21. * remote daemons serving up and coordinating consistent and safe
  22. * access to storage.
  23. *
  24. * Cluster membership and the mapping of data objects onto storage devices
  25. * are described by the osd map.
  26. *
  27. * We keep track of pending OSD requests (read, write), resubmit
  28. * requests to different OSDs when the cluster topology/data layout
  29. * change, or retry the affected requests when the communications
  30. * channel with an OSD is reset.
  31. */
  32. /*
  33. * calculate the mapping of a file extent onto an object, and fill out the
  34. * request accordingly. shorten extent as necessary if it crosses an
  35. * object boundary.
  36. *
  37. * fill osd op in request message.
  38. */
  39. static void calc_layout(struct ceph_osd_client *osdc,
  40. struct ceph_vino vino, struct ceph_file_layout *layout,
  41. u64 off, u64 *plen,
  42. struct ceph_osd_request *req)
  43. {
  44. struct ceph_osd_request_head *reqhead = req->r_request->front.iov_base;
  45. struct ceph_osd_op *op = (void *)(reqhead + 1);
  46. u64 orig_len = *plen;
  47. u64 objoff, objlen; /* extent in object */
  48. u64 bno;
  49. reqhead->snapid = cpu_to_le64(vino.snap);
  50. /* object extent? */
  51. ceph_calc_file_object_mapping(layout, off, plen, &bno,
  52. &objoff, &objlen);
  53. if (*plen < orig_len)
  54. dout(" skipping last %llu, final file extent %llu~%llu\n",
  55. orig_len - *plen, off, *plen);
  56. sprintf(req->r_oid, "%llx.%08llx", vino.ino, bno);
  57. req->r_oid_len = strlen(req->r_oid);
  58. op->extent.offset = cpu_to_le64(objoff);
  59. op->extent.length = cpu_to_le64(objlen);
  60. req->r_num_pages = calc_pages_for(off, *plen);
  61. dout("calc_layout %s (%d) %llu~%llu (%d pages)\n",
  62. req->r_oid, req->r_oid_len, objoff, objlen, req->r_num_pages);
  63. }
  64. /*
  65. * requests
  66. */
  67. void ceph_osdc_release_request(struct kref *kref)
  68. {
  69. struct ceph_osd_request *req = container_of(kref,
  70. struct ceph_osd_request,
  71. r_kref);
  72. if (req->r_request)
  73. ceph_msg_put(req->r_request);
  74. if (req->r_reply)
  75. ceph_msg_put(req->r_reply);
  76. if (req->r_own_pages)
  77. ceph_release_page_vector(req->r_pages,
  78. req->r_num_pages);
  79. ceph_put_snap_context(req->r_snapc);
  80. if (req->r_mempool)
  81. mempool_free(req, req->r_osdc->req_mempool);
  82. else
  83. kfree(req);
  84. }
  85. /*
  86. * build new request AND message, calculate layout, and adjust file
  87. * extent as needed.
  88. *
  89. * if the file was recently truncated, we include information about its
  90. * old and new size so that the object can be updated appropriately. (we
  91. * avoid synchronously deleting truncated objects because it's slow.)
  92. *
  93. * if @do_sync, include a 'startsync' command so that the osd will flush
  94. * data quickly.
  95. */
  96. struct ceph_osd_request *ceph_osdc_new_request(struct ceph_osd_client *osdc,
  97. struct ceph_file_layout *layout,
  98. struct ceph_vino vino,
  99. u64 off, u64 *plen,
  100. int opcode, int flags,
  101. struct ceph_snap_context *snapc,
  102. int do_sync,
  103. u32 truncate_seq,
  104. u64 truncate_size,
  105. struct timespec *mtime,
  106. bool use_mempool, int num_reply)
  107. {
  108. struct ceph_osd_request *req;
  109. struct ceph_msg *msg;
  110. struct ceph_osd_request_head *head;
  111. struct ceph_osd_op *op;
  112. void *p;
  113. int do_trunc = truncate_seq && (off + *plen > truncate_size);
  114. int num_op = 1 + do_sync + do_trunc;
  115. size_t msg_size = sizeof(*head) + num_op*sizeof(*op);
  116. int err, i;
  117. u64 prevofs;
  118. if (use_mempool) {
  119. req = mempool_alloc(osdc->req_mempool, GFP_NOFS);
  120. memset(req, 0, sizeof(*req));
  121. } else {
  122. req = kzalloc(sizeof(*req), GFP_NOFS);
  123. }
  124. if (req == NULL)
  125. return ERR_PTR(-ENOMEM);
  126. err = ceph_msgpool_resv(&osdc->msgpool_op_reply, num_reply);
  127. if (err) {
  128. ceph_osdc_put_request(req);
  129. return ERR_PTR(-ENOMEM);
  130. }
  131. req->r_num_prealloc_reply = num_reply;
  132. req->r_osdc = osdc;
  133. req->r_mempool = use_mempool;
  134. kref_init(&req->r_kref);
  135. init_completion(&req->r_completion);
  136. init_completion(&req->r_safe_completion);
  137. INIT_LIST_HEAD(&req->r_unsafe_item);
  138. req->r_flags = flags;
  139. WARN_ON((flags & (CEPH_OSD_FLAG_READ|CEPH_OSD_FLAG_WRITE)) == 0);
  140. /* create message; allow space for oid */
  141. msg_size += 40;
  142. if (snapc)
  143. msg_size += sizeof(u64) * snapc->num_snaps;
  144. if (use_mempool)
  145. msg = ceph_msgpool_get(&osdc->msgpool_op, 0);
  146. else
  147. msg = ceph_msg_new(CEPH_MSG_OSD_OP, msg_size, 0, 0, NULL);
  148. if (IS_ERR(msg)) {
  149. ceph_msgpool_resv(&osdc->msgpool_op_reply, -num_reply);
  150. ceph_osdc_put_request(req);
  151. return ERR_PTR(PTR_ERR(msg));
  152. }
  153. msg->hdr.type = cpu_to_le16(CEPH_MSG_OSD_OP);
  154. memset(msg->front.iov_base, 0, msg->front.iov_len);
  155. head = msg->front.iov_base;
  156. op = (void *)(head + 1);
  157. p = (void *)(op + num_op);
  158. req->r_request = msg;
  159. req->r_snapc = ceph_get_snap_context(snapc);
  160. head->client_inc = cpu_to_le32(1); /* always, for now. */
  161. head->flags = cpu_to_le32(flags);
  162. if (flags & CEPH_OSD_FLAG_WRITE)
  163. ceph_encode_timespec(&head->mtime, mtime);
  164. head->num_ops = cpu_to_le16(num_op);
  165. op->op = cpu_to_le16(opcode);
  166. /* calculate max write size */
  167. calc_layout(osdc, vino, layout, off, plen, req);
  168. req->r_file_layout = *layout; /* keep a copy */
  169. if (flags & CEPH_OSD_FLAG_WRITE) {
  170. req->r_request->hdr.data_off = cpu_to_le16(off);
  171. req->r_request->hdr.data_len = cpu_to_le32(*plen);
  172. op->payload_len = cpu_to_le32(*plen);
  173. }
  174. /* fill in oid */
  175. head->object_len = cpu_to_le32(req->r_oid_len);
  176. memcpy(p, req->r_oid, req->r_oid_len);
  177. p += req->r_oid_len;
  178. /* additional ops */
  179. if (do_trunc) {
  180. op++;
  181. op->op = cpu_to_le16(opcode == CEPH_OSD_OP_READ ?
  182. CEPH_OSD_OP_MASKTRUNC : CEPH_OSD_OP_SETTRUNC);
  183. op->trunc.truncate_seq = cpu_to_le32(truncate_seq);
  184. prevofs = le64_to_cpu((op-1)->extent.offset);
  185. op->trunc.truncate_size = cpu_to_le64(truncate_size -
  186. (off-prevofs));
  187. }
  188. if (do_sync) {
  189. op++;
  190. op->op = cpu_to_le16(CEPH_OSD_OP_STARTSYNC);
  191. }
  192. if (snapc) {
  193. head->snap_seq = cpu_to_le64(snapc->seq);
  194. head->num_snaps = cpu_to_le32(snapc->num_snaps);
  195. for (i = 0; i < snapc->num_snaps; i++) {
  196. put_unaligned_le64(snapc->snaps[i], p);
  197. p += sizeof(u64);
  198. }
  199. }
  200. BUG_ON(p > msg->front.iov_base + msg->front.iov_len);
  201. return req;
  202. }
  203. /*
  204. * We keep osd requests in an rbtree, sorted by ->r_tid.
  205. */
  206. static void __insert_request(struct ceph_osd_client *osdc,
  207. struct ceph_osd_request *new)
  208. {
  209. struct rb_node **p = &osdc->requests.rb_node;
  210. struct rb_node *parent = NULL;
  211. struct ceph_osd_request *req = NULL;
  212. while (*p) {
  213. parent = *p;
  214. req = rb_entry(parent, struct ceph_osd_request, r_node);
  215. if (new->r_tid < req->r_tid)
  216. p = &(*p)->rb_left;
  217. else if (new->r_tid > req->r_tid)
  218. p = &(*p)->rb_right;
  219. else
  220. BUG();
  221. }
  222. rb_link_node(&new->r_node, parent, p);
  223. rb_insert_color(&new->r_node, &osdc->requests);
  224. }
  225. static struct ceph_osd_request *__lookup_request(struct ceph_osd_client *osdc,
  226. u64 tid)
  227. {
  228. struct ceph_osd_request *req;
  229. struct rb_node *n = osdc->requests.rb_node;
  230. while (n) {
  231. req = rb_entry(n, struct ceph_osd_request, r_node);
  232. if (tid < req->r_tid)
  233. n = n->rb_left;
  234. else if (tid > req->r_tid)
  235. n = n->rb_right;
  236. else
  237. return req;
  238. }
  239. return NULL;
  240. }
  241. static struct ceph_osd_request *
  242. __lookup_request_ge(struct ceph_osd_client *osdc,
  243. u64 tid)
  244. {
  245. struct ceph_osd_request *req;
  246. struct rb_node *n = osdc->requests.rb_node;
  247. while (n) {
  248. req = rb_entry(n, struct ceph_osd_request, r_node);
  249. if (tid < req->r_tid) {
  250. if (!n->rb_left)
  251. return req;
  252. n = n->rb_left;
  253. } else if (tid > req->r_tid) {
  254. n = n->rb_right;
  255. } else {
  256. return req;
  257. }
  258. }
  259. return NULL;
  260. }
  261. /*
  262. * If the osd connection drops, we need to resubmit all requests.
  263. */
  264. static void osd_reset(struct ceph_connection *con)
  265. {
  266. struct ceph_osd *osd = con->private;
  267. struct ceph_osd_client *osdc;
  268. if (!osd)
  269. return;
  270. dout("osd_reset osd%d\n", osd->o_osd);
  271. osdc = osd->o_osdc;
  272. osd->o_incarnation++;
  273. down_read(&osdc->map_sem);
  274. kick_requests(osdc, osd);
  275. up_read(&osdc->map_sem);
  276. }
  277. /*
  278. * Track open sessions with osds.
  279. */
  280. static struct ceph_osd *create_osd(struct ceph_osd_client *osdc)
  281. {
  282. struct ceph_osd *osd;
  283. osd = kzalloc(sizeof(*osd), GFP_NOFS);
  284. if (!osd)
  285. return NULL;
  286. atomic_set(&osd->o_ref, 1);
  287. osd->o_osdc = osdc;
  288. INIT_LIST_HEAD(&osd->o_requests);
  289. osd->o_incarnation = 1;
  290. ceph_con_init(osdc->client->msgr, &osd->o_con);
  291. osd->o_con.private = osd;
  292. osd->o_con.ops = &osd_con_ops;
  293. osd->o_con.peer_name.type = CEPH_ENTITY_TYPE_OSD;
  294. return osd;
  295. }
  296. static struct ceph_osd *get_osd(struct ceph_osd *osd)
  297. {
  298. if (atomic_inc_not_zero(&osd->o_ref)) {
  299. dout("get_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref)-1,
  300. atomic_read(&osd->o_ref));
  301. return osd;
  302. } else {
  303. dout("get_osd %p FAIL\n", osd);
  304. return NULL;
  305. }
  306. }
  307. static void put_osd(struct ceph_osd *osd)
  308. {
  309. dout("put_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref),
  310. atomic_read(&osd->o_ref) - 1);
  311. if (atomic_dec_and_test(&osd->o_ref))
  312. kfree(osd);
  313. }
  314. /*
  315. * remove an osd from our map
  316. */
  317. static void remove_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
  318. {
  319. dout("remove_osd %p\n", osd);
  320. BUG_ON(!list_empty(&osd->o_requests));
  321. rb_erase(&osd->o_node, &osdc->osds);
  322. ceph_con_close(&osd->o_con);
  323. put_osd(osd);
  324. }
  325. /*
  326. * reset osd connect
  327. */
  328. static int reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
  329. {
  330. int ret = 0;
  331. dout("reset_osd %p osd%d\n", osd, osd->o_osd);
  332. if (list_empty(&osd->o_requests)) {
  333. remove_osd(osdc, osd);
  334. } else {
  335. ceph_con_close(&osd->o_con);
  336. ceph_con_open(&osd->o_con, &osdc->osdmap->osd_addr[osd->o_osd]);
  337. osd->o_incarnation++;
  338. }
  339. return ret;
  340. }
  341. static void __insert_osd(struct ceph_osd_client *osdc, struct ceph_osd *new)
  342. {
  343. struct rb_node **p = &osdc->osds.rb_node;
  344. struct rb_node *parent = NULL;
  345. struct ceph_osd *osd = NULL;
  346. while (*p) {
  347. parent = *p;
  348. osd = rb_entry(parent, struct ceph_osd, o_node);
  349. if (new->o_osd < osd->o_osd)
  350. p = &(*p)->rb_left;
  351. else if (new->o_osd > osd->o_osd)
  352. p = &(*p)->rb_right;
  353. else
  354. BUG();
  355. }
  356. rb_link_node(&new->o_node, parent, p);
  357. rb_insert_color(&new->o_node, &osdc->osds);
  358. }
  359. static struct ceph_osd *__lookup_osd(struct ceph_osd_client *osdc, int o)
  360. {
  361. struct ceph_osd *osd;
  362. struct rb_node *n = osdc->osds.rb_node;
  363. while (n) {
  364. osd = rb_entry(n, struct ceph_osd, o_node);
  365. if (o < osd->o_osd)
  366. n = n->rb_left;
  367. else if (o > osd->o_osd)
  368. n = n->rb_right;
  369. else
  370. return osd;
  371. }
  372. return NULL;
  373. }
  374. /*
  375. * Register request, assign tid. If this is the first request, set up
  376. * the timeout event.
  377. */
  378. static void register_request(struct ceph_osd_client *osdc,
  379. struct ceph_osd_request *req)
  380. {
  381. struct ceph_osd_request_head *head = req->r_request->front.iov_base;
  382. mutex_lock(&osdc->request_mutex);
  383. req->r_tid = ++osdc->last_tid;
  384. head->tid = cpu_to_le64(req->r_tid);
  385. dout("register_request %p tid %lld\n", req, req->r_tid);
  386. __insert_request(osdc, req);
  387. ceph_osdc_get_request(req);
  388. osdc->num_requests++;
  389. req->r_timeout_stamp =
  390. jiffies + osdc->client->mount_args->osd_timeout*HZ;
  391. if (osdc->num_requests == 1) {
  392. osdc->timeout_tid = req->r_tid;
  393. dout(" timeout on tid %llu at %lu\n", req->r_tid,
  394. req->r_timeout_stamp);
  395. schedule_delayed_work(&osdc->timeout_work,
  396. round_jiffies_relative(req->r_timeout_stamp - jiffies));
  397. }
  398. mutex_unlock(&osdc->request_mutex);
  399. }
  400. /*
  401. * called under osdc->request_mutex
  402. */
  403. static void __unregister_request(struct ceph_osd_client *osdc,
  404. struct ceph_osd_request *req)
  405. {
  406. dout("__unregister_request %p tid %lld\n", req, req->r_tid);
  407. rb_erase(&req->r_node, &osdc->requests);
  408. osdc->num_requests--;
  409. ceph_msgpool_resv(&osdc->msgpool_op_reply, -req->r_num_prealloc_reply);
  410. if (req->r_osd) {
  411. /* make sure the original request isn't in flight. */
  412. ceph_con_revoke(&req->r_osd->o_con, req->r_request);
  413. list_del_init(&req->r_osd_item);
  414. if (list_empty(&req->r_osd->o_requests))
  415. remove_osd(osdc, req->r_osd);
  416. req->r_osd = NULL;
  417. }
  418. ceph_osdc_put_request(req);
  419. if (req->r_tid == osdc->timeout_tid) {
  420. if (osdc->num_requests == 0) {
  421. dout("no requests, canceling timeout\n");
  422. osdc->timeout_tid = 0;
  423. cancel_delayed_work(&osdc->timeout_work);
  424. } else {
  425. req = rb_entry(rb_first(&osdc->requests),
  426. struct ceph_osd_request, r_node);
  427. osdc->timeout_tid = req->r_tid;
  428. dout("rescheduled timeout on tid %llu at %lu\n",
  429. req->r_tid, req->r_timeout_stamp);
  430. schedule_delayed_work(&osdc->timeout_work,
  431. round_jiffies_relative(req->r_timeout_stamp -
  432. jiffies));
  433. }
  434. }
  435. }
  436. /*
  437. * Cancel a previously queued request message
  438. */
  439. static void __cancel_request(struct ceph_osd_request *req)
  440. {
  441. if (req->r_sent) {
  442. ceph_con_revoke(&req->r_osd->o_con, req->r_request);
  443. req->r_sent = 0;
  444. }
  445. }
  446. /*
  447. * Pick an osd (the first 'up' osd in the pg), allocate the osd struct
  448. * (as needed), and set the request r_osd appropriately. If there is
  449. * no up osd, set r_osd to NULL.
  450. *
  451. * Return 0 if unchanged, 1 if changed, or negative on error.
  452. *
  453. * Caller should hold map_sem for read and request_mutex.
  454. */
  455. static int __map_osds(struct ceph_osd_client *osdc,
  456. struct ceph_osd_request *req)
  457. {
  458. struct ceph_osd_request_head *reqhead = req->r_request->front.iov_base;
  459. struct ceph_pg pgid;
  460. int o = -1;
  461. int err;
  462. struct ceph_osd *newosd = NULL;
  463. dout("map_osds %p tid %lld\n", req, req->r_tid);
  464. err = ceph_calc_object_layout(&reqhead->layout, req->r_oid,
  465. &req->r_file_layout, osdc->osdmap);
  466. if (err)
  467. return err;
  468. pgid = reqhead->layout.ol_pgid;
  469. o = ceph_calc_pg_primary(osdc->osdmap, pgid);
  470. if ((req->r_osd && req->r_osd->o_osd == o &&
  471. req->r_sent >= req->r_osd->o_incarnation) ||
  472. (req->r_osd == NULL && o == -1))
  473. return 0; /* no change */
  474. dout("map_osds tid %llu pgid %d.%x osd%d (was osd%d)\n",
  475. req->r_tid, le32_to_cpu(pgid.pool), le16_to_cpu(pgid.ps), o,
  476. req->r_osd ? req->r_osd->o_osd : -1);
  477. if (req->r_osd) {
  478. __cancel_request(req);
  479. list_del_init(&req->r_osd_item);
  480. if (list_empty(&req->r_osd->o_requests)) {
  481. /* try to re-use r_osd if possible */
  482. newosd = get_osd(req->r_osd);
  483. remove_osd(osdc, newosd);
  484. }
  485. req->r_osd = NULL;
  486. }
  487. req->r_osd = __lookup_osd(osdc, o);
  488. if (!req->r_osd && o >= 0) {
  489. if (newosd) {
  490. req->r_osd = newosd;
  491. newosd = NULL;
  492. } else {
  493. err = -ENOMEM;
  494. req->r_osd = create_osd(osdc);
  495. if (!req->r_osd)
  496. goto out;
  497. }
  498. dout("map_osds osd %p is osd%d\n", req->r_osd, o);
  499. req->r_osd->o_osd = o;
  500. req->r_osd->o_con.peer_name.num = cpu_to_le64(o);
  501. __insert_osd(osdc, req->r_osd);
  502. ceph_con_open(&req->r_osd->o_con, &osdc->osdmap->osd_addr[o]);
  503. }
  504. if (req->r_osd)
  505. list_add(&req->r_osd_item, &req->r_osd->o_requests);
  506. err = 1; /* osd changed */
  507. out:
  508. if (newosd)
  509. put_osd(newosd);
  510. return err;
  511. }
  512. /*
  513. * caller should hold map_sem (for read) and request_mutex
  514. */
  515. static int __send_request(struct ceph_osd_client *osdc,
  516. struct ceph_osd_request *req)
  517. {
  518. struct ceph_osd_request_head *reqhead;
  519. int err;
  520. err = __map_osds(osdc, req);
  521. if (err < 0)
  522. return err;
  523. if (req->r_osd == NULL) {
  524. dout("send_request %p no up osds in pg\n", req);
  525. ceph_monc_request_next_osdmap(&osdc->client->monc);
  526. return 0;
  527. }
  528. dout("send_request %p tid %llu to osd%d flags %d\n",
  529. req, req->r_tid, req->r_osd->o_osd, req->r_flags);
  530. reqhead = req->r_request->front.iov_base;
  531. reqhead->osdmap_epoch = cpu_to_le32(osdc->osdmap->epoch);
  532. reqhead->flags |= cpu_to_le32(req->r_flags); /* e.g., RETRY */
  533. reqhead->reassert_version = req->r_reassert_version;
  534. req->r_timeout_stamp = jiffies+osdc->client->mount_args->osd_timeout*HZ;
  535. ceph_msg_get(req->r_request); /* send consumes a ref */
  536. ceph_con_send(&req->r_osd->o_con, req->r_request);
  537. req->r_sent = req->r_osd->o_incarnation;
  538. return 0;
  539. }
  540. /*
  541. * Timeout callback, called every N seconds when 1 or more osd
  542. * requests has been active for more than N seconds. When this
  543. * happens, we ping all OSDs with requests who have timed out to
  544. * ensure any communications channel reset is detected. Reset the
  545. * request timeouts another N seconds in the future as we go.
  546. * Reschedule the timeout event another N seconds in future (unless
  547. * there are no open requests).
  548. */
  549. static void handle_timeout(struct work_struct *work)
  550. {
  551. struct ceph_osd_client *osdc =
  552. container_of(work, struct ceph_osd_client, timeout_work.work);
  553. struct ceph_osd_request *req;
  554. struct ceph_osd *osd;
  555. unsigned long timeout = osdc->client->mount_args->osd_timeout * HZ;
  556. unsigned long next_timeout = timeout + jiffies;
  557. struct rb_node *p;
  558. dout("timeout\n");
  559. down_read(&osdc->map_sem);
  560. ceph_monc_request_next_osdmap(&osdc->client->monc);
  561. mutex_lock(&osdc->request_mutex);
  562. for (p = rb_first(&osdc->requests); p; p = rb_next(p)) {
  563. req = rb_entry(p, struct ceph_osd_request, r_node);
  564. if (req->r_resend) {
  565. int err;
  566. dout("osdc resending prev failed %lld\n", req->r_tid);
  567. err = __send_request(osdc, req);
  568. if (err)
  569. dout("osdc failed again on %lld\n", req->r_tid);
  570. else
  571. req->r_resend = false;
  572. continue;
  573. }
  574. }
  575. for (p = rb_first(&osdc->osds); p; p = rb_next(p)) {
  576. osd = rb_entry(p, struct ceph_osd, o_node);
  577. if (list_empty(&osd->o_requests))
  578. continue;
  579. req = list_first_entry(&osd->o_requests,
  580. struct ceph_osd_request, r_osd_item);
  581. if (time_before(jiffies, req->r_timeout_stamp))
  582. continue;
  583. dout(" tid %llu (at least) timed out on osd%d\n",
  584. req->r_tid, osd->o_osd);
  585. req->r_timeout_stamp = next_timeout;
  586. ceph_con_keepalive(&osd->o_con);
  587. }
  588. if (osdc->timeout_tid)
  589. schedule_delayed_work(&osdc->timeout_work,
  590. round_jiffies_relative(timeout));
  591. mutex_unlock(&osdc->request_mutex);
  592. up_read(&osdc->map_sem);
  593. }
  594. /*
  595. * handle osd op reply. either call the callback if it is specified,
  596. * or do the completion to wake up the waiting thread.
  597. */
  598. static void handle_reply(struct ceph_osd_client *osdc, struct ceph_msg *msg)
  599. {
  600. struct ceph_osd_reply_head *rhead = msg->front.iov_base;
  601. struct ceph_osd_request *req;
  602. u64 tid;
  603. int numops, object_len, flags;
  604. if (msg->front.iov_len < sizeof(*rhead))
  605. goto bad;
  606. tid = le64_to_cpu(rhead->tid);
  607. numops = le32_to_cpu(rhead->num_ops);
  608. object_len = le32_to_cpu(rhead->object_len);
  609. if (msg->front.iov_len != sizeof(*rhead) + object_len +
  610. numops * sizeof(struct ceph_osd_op))
  611. goto bad;
  612. dout("handle_reply %p tid %llu\n", msg, tid);
  613. /* lookup */
  614. mutex_lock(&osdc->request_mutex);
  615. req = __lookup_request(osdc, tid);
  616. if (req == NULL) {
  617. dout("handle_reply tid %llu dne\n", tid);
  618. mutex_unlock(&osdc->request_mutex);
  619. return;
  620. }
  621. ceph_osdc_get_request(req);
  622. flags = le32_to_cpu(rhead->flags);
  623. if (req->r_reply) {
  624. /*
  625. * once we see the message has been received, we don't
  626. * need a ref (which is only needed for revoking
  627. * pages)
  628. */
  629. ceph_msg_put(req->r_reply);
  630. req->r_reply = NULL;
  631. }
  632. if (!req->r_got_reply) {
  633. unsigned bytes;
  634. req->r_result = le32_to_cpu(rhead->result);
  635. bytes = le32_to_cpu(msg->hdr.data_len);
  636. dout("handle_reply result %d bytes %d\n", req->r_result,
  637. bytes);
  638. if (req->r_result == 0)
  639. req->r_result = bytes;
  640. /* in case this is a write and we need to replay, */
  641. req->r_reassert_version = rhead->reassert_version;
  642. req->r_got_reply = 1;
  643. } else if ((flags & CEPH_OSD_FLAG_ONDISK) == 0) {
  644. dout("handle_reply tid %llu dup ack\n", tid);
  645. mutex_unlock(&osdc->request_mutex);
  646. goto done;
  647. }
  648. dout("handle_reply tid %llu flags %d\n", tid, flags);
  649. /* either this is a read, or we got the safe response */
  650. if ((flags & CEPH_OSD_FLAG_ONDISK) ||
  651. ((flags & CEPH_OSD_FLAG_WRITE) == 0))
  652. __unregister_request(osdc, req);
  653. mutex_unlock(&osdc->request_mutex);
  654. if (req->r_callback)
  655. req->r_callback(req, msg);
  656. else
  657. complete(&req->r_completion);
  658. if (flags & CEPH_OSD_FLAG_ONDISK) {
  659. if (req->r_safe_callback)
  660. req->r_safe_callback(req, msg);
  661. complete(&req->r_safe_completion); /* fsync waiter */
  662. }
  663. done:
  664. ceph_osdc_put_request(req);
  665. return;
  666. bad:
  667. pr_err("corrupt osd_op_reply got %d %d expected %d\n",
  668. (int)msg->front.iov_len, le32_to_cpu(msg->hdr.front_len),
  669. (int)sizeof(*rhead));
  670. ceph_msg_dump(msg);
  671. }
  672. /*
  673. * Resubmit osd requests whose osd or osd address has changed. Request
  674. * a new osd map if osds are down, or we are otherwise unable to determine
  675. * how to direct a request.
  676. *
  677. * Close connections to down osds.
  678. *
  679. * If @who is specified, resubmit requests for that specific osd.
  680. *
  681. * Caller should hold map_sem for read and request_mutex.
  682. */
  683. static void kick_requests(struct ceph_osd_client *osdc,
  684. struct ceph_osd *kickosd)
  685. {
  686. struct ceph_osd_request *req;
  687. struct rb_node *p, *n;
  688. int needmap = 0;
  689. int err;
  690. dout("kick_requests osd%d\n", kickosd ? kickosd->o_osd : -1);
  691. mutex_lock(&osdc->request_mutex);
  692. if (!kickosd) {
  693. for (p = rb_first(&osdc->osds); p; p = n) {
  694. struct ceph_osd *osd =
  695. rb_entry(p, struct ceph_osd, o_node);
  696. n = rb_next(p);
  697. if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) ||
  698. !ceph_entity_addr_equal(&osd->o_con.peer_addr,
  699. ceph_osd_addr(osdc->osdmap,
  700. osd->o_osd)))
  701. reset_osd(osdc, osd);
  702. }
  703. }
  704. for (p = rb_first(&osdc->requests); p; p = rb_next(p)) {
  705. req = rb_entry(p, struct ceph_osd_request, r_node);
  706. if (req->r_resend) {
  707. dout(" r_resend set on tid %llu\n", req->r_tid);
  708. __cancel_request(req);
  709. goto kick;
  710. }
  711. if (req->r_osd && kickosd == req->r_osd) {
  712. __cancel_request(req);
  713. goto kick;
  714. }
  715. err = __map_osds(osdc, req);
  716. if (err == 0)
  717. continue; /* no change */
  718. if (err < 0) {
  719. /*
  720. * FIXME: really, we should set the request
  721. * error and fail if this isn't a 'nofail'
  722. * request, but that's a fair bit more
  723. * complicated to do. So retry!
  724. */
  725. dout(" setting r_resend on %llu\n", req->r_tid);
  726. req->r_resend = true;
  727. continue;
  728. }
  729. if (req->r_osd == NULL) {
  730. dout("tid %llu maps to no valid osd\n", req->r_tid);
  731. needmap++; /* request a newer map */
  732. continue;
  733. }
  734. kick:
  735. dout("kicking %p tid %llu osd%d\n", req, req->r_tid,
  736. req->r_osd->o_osd);
  737. req->r_flags |= CEPH_OSD_FLAG_RETRY;
  738. err = __send_request(osdc, req);
  739. if (err) {
  740. dout(" setting r_resend on %llu\n", req->r_tid);
  741. req->r_resend = true;
  742. }
  743. }
  744. mutex_unlock(&osdc->request_mutex);
  745. if (needmap) {
  746. dout("%d requests for down osds, need new map\n", needmap);
  747. ceph_monc_request_next_osdmap(&osdc->client->monc);
  748. }
  749. }
  750. /*
  751. * Process updated osd map.
  752. *
  753. * The message contains any number of incremental and full maps, normally
  754. * indicating some sort of topology change in the cluster. Kick requests
  755. * off to different OSDs as needed.
  756. */
  757. void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg)
  758. {
  759. void *p, *end, *next;
  760. u32 nr_maps, maplen;
  761. u32 epoch;
  762. struct ceph_osdmap *newmap = NULL, *oldmap;
  763. int err;
  764. struct ceph_fsid fsid;
  765. dout("handle_map have %u\n", osdc->osdmap ? osdc->osdmap->epoch : 0);
  766. p = msg->front.iov_base;
  767. end = p + msg->front.iov_len;
  768. /* verify fsid */
  769. ceph_decode_need(&p, end, sizeof(fsid), bad);
  770. ceph_decode_copy(&p, &fsid, sizeof(fsid));
  771. if (ceph_check_fsid(osdc->client, &fsid) < 0)
  772. return;
  773. down_write(&osdc->map_sem);
  774. /* incremental maps */
  775. ceph_decode_32_safe(&p, end, nr_maps, bad);
  776. dout(" %d inc maps\n", nr_maps);
  777. while (nr_maps > 0) {
  778. ceph_decode_need(&p, end, 2*sizeof(u32), bad);
  779. epoch = ceph_decode_32(&p);
  780. maplen = ceph_decode_32(&p);
  781. ceph_decode_need(&p, end, maplen, bad);
  782. next = p + maplen;
  783. if (osdc->osdmap && osdc->osdmap->epoch+1 == epoch) {
  784. dout("applying incremental map %u len %d\n",
  785. epoch, maplen);
  786. newmap = osdmap_apply_incremental(&p, next,
  787. osdc->osdmap,
  788. osdc->client->msgr);
  789. if (IS_ERR(newmap)) {
  790. err = PTR_ERR(newmap);
  791. goto bad;
  792. }
  793. if (newmap != osdc->osdmap) {
  794. ceph_osdmap_destroy(osdc->osdmap);
  795. osdc->osdmap = newmap;
  796. }
  797. } else {
  798. dout("ignoring incremental map %u len %d\n",
  799. epoch, maplen);
  800. }
  801. p = next;
  802. nr_maps--;
  803. }
  804. if (newmap)
  805. goto done;
  806. /* full maps */
  807. ceph_decode_32_safe(&p, end, nr_maps, bad);
  808. dout(" %d full maps\n", nr_maps);
  809. while (nr_maps) {
  810. ceph_decode_need(&p, end, 2*sizeof(u32), bad);
  811. epoch = ceph_decode_32(&p);
  812. maplen = ceph_decode_32(&p);
  813. ceph_decode_need(&p, end, maplen, bad);
  814. if (nr_maps > 1) {
  815. dout("skipping non-latest full map %u len %d\n",
  816. epoch, maplen);
  817. } else if (osdc->osdmap && osdc->osdmap->epoch >= epoch) {
  818. dout("skipping full map %u len %d, "
  819. "older than our %u\n", epoch, maplen,
  820. osdc->osdmap->epoch);
  821. } else {
  822. dout("taking full map %u len %d\n", epoch, maplen);
  823. newmap = osdmap_decode(&p, p+maplen);
  824. if (IS_ERR(newmap)) {
  825. err = PTR_ERR(newmap);
  826. goto bad;
  827. }
  828. oldmap = osdc->osdmap;
  829. osdc->osdmap = newmap;
  830. if (oldmap)
  831. ceph_osdmap_destroy(oldmap);
  832. }
  833. p += maplen;
  834. nr_maps--;
  835. }
  836. done:
  837. downgrade_write(&osdc->map_sem);
  838. ceph_monc_got_osdmap(&osdc->client->monc, osdc->osdmap->epoch);
  839. if (newmap)
  840. kick_requests(osdc, NULL);
  841. up_read(&osdc->map_sem);
  842. return;
  843. bad:
  844. pr_err("osdc handle_map corrupt msg\n");
  845. ceph_msg_dump(msg);
  846. up_write(&osdc->map_sem);
  847. return;
  848. }
  849. /*
  850. * A read request prepares specific pages that data is to be read into.
  851. * When a message is being read off the wire, we call prepare_pages to
  852. * find those pages.
  853. * 0 = success, -1 failure.
  854. */
  855. static int prepare_pages(struct ceph_connection *con, struct ceph_msg *m,
  856. int want)
  857. {
  858. struct ceph_osd *osd = con->private;
  859. struct ceph_osd_client *osdc;
  860. struct ceph_osd_reply_head *rhead = m->front.iov_base;
  861. struct ceph_osd_request *req;
  862. u64 tid;
  863. int ret = -1;
  864. int type = le16_to_cpu(m->hdr.type);
  865. if (!osd)
  866. return -1;
  867. osdc = osd->o_osdc;
  868. dout("prepare_pages on msg %p want %d\n", m, want);
  869. if (unlikely(type != CEPH_MSG_OSD_OPREPLY))
  870. return -1; /* hmm! */
  871. tid = le64_to_cpu(rhead->tid);
  872. mutex_lock(&osdc->request_mutex);
  873. req = __lookup_request(osdc, tid);
  874. if (!req) {
  875. dout("prepare_pages unknown tid %llu\n", tid);
  876. goto out;
  877. }
  878. dout("prepare_pages tid %llu has %d pages, want %d\n",
  879. tid, req->r_num_pages, want);
  880. if (likely(req->r_num_pages >= want && !req->r_prepared_pages)) {
  881. m->pages = req->r_pages;
  882. m->nr_pages = req->r_num_pages;
  883. req->r_reply = m; /* only for duration of read over socket */
  884. ceph_msg_get(m);
  885. req->r_prepared_pages = 1;
  886. ret = 0; /* success */
  887. }
  888. out:
  889. mutex_unlock(&osdc->request_mutex);
  890. return ret;
  891. }
  892. /*
  893. * Register request, send initial attempt.
  894. */
  895. int ceph_osdc_start_request(struct ceph_osd_client *osdc,
  896. struct ceph_osd_request *req,
  897. bool nofail)
  898. {
  899. int rc = 0;
  900. req->r_request->pages = req->r_pages;
  901. req->r_request->nr_pages = req->r_num_pages;
  902. register_request(osdc, req);
  903. down_read(&osdc->map_sem);
  904. mutex_lock(&osdc->request_mutex);
  905. /*
  906. * a racing kick_requests() may have sent the message for us
  907. * while we dropped request_mutex above, so only send now if
  908. * the request still han't been touched yet.
  909. */
  910. if (req->r_sent == 0) {
  911. rc = __send_request(osdc, req);
  912. if (rc) {
  913. if (nofail) {
  914. dout("osdc_start_request failed send, "
  915. " marking %lld\n", req->r_tid);
  916. req->r_resend = true;
  917. rc = 0;
  918. } else {
  919. __unregister_request(osdc, req);
  920. }
  921. }
  922. }
  923. mutex_unlock(&osdc->request_mutex);
  924. up_read(&osdc->map_sem);
  925. return rc;
  926. }
  927. /*
  928. * wait for a request to complete
  929. */
  930. int ceph_osdc_wait_request(struct ceph_osd_client *osdc,
  931. struct ceph_osd_request *req)
  932. {
  933. int rc;
  934. rc = wait_for_completion_interruptible(&req->r_completion);
  935. if (rc < 0) {
  936. mutex_lock(&osdc->request_mutex);
  937. __cancel_request(req);
  938. mutex_unlock(&osdc->request_mutex);
  939. dout("wait_request tid %llu timed out\n", req->r_tid);
  940. return rc;
  941. }
  942. dout("wait_request tid %llu result %d\n", req->r_tid, req->r_result);
  943. return req->r_result;
  944. }
  945. /*
  946. * sync - wait for all in-flight requests to flush. avoid starvation.
  947. */
  948. void ceph_osdc_sync(struct ceph_osd_client *osdc)
  949. {
  950. struct ceph_osd_request *req;
  951. u64 last_tid, next_tid = 0;
  952. mutex_lock(&osdc->request_mutex);
  953. last_tid = osdc->last_tid;
  954. while (1) {
  955. req = __lookup_request_ge(osdc, next_tid);
  956. if (!req)
  957. break;
  958. if (req->r_tid > last_tid)
  959. break;
  960. next_tid = req->r_tid + 1;
  961. if ((req->r_flags & CEPH_OSD_FLAG_WRITE) == 0)
  962. continue;
  963. ceph_osdc_get_request(req);
  964. mutex_unlock(&osdc->request_mutex);
  965. dout("sync waiting on tid %llu (last is %llu)\n",
  966. req->r_tid, last_tid);
  967. wait_for_completion(&req->r_safe_completion);
  968. mutex_lock(&osdc->request_mutex);
  969. ceph_osdc_put_request(req);
  970. }
  971. mutex_unlock(&osdc->request_mutex);
  972. dout("sync done (thru tid %llu)\n", last_tid);
  973. }
  974. /*
  975. * init, shutdown
  976. */
  977. int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client)
  978. {
  979. int err;
  980. dout("init\n");
  981. osdc->client = client;
  982. osdc->osdmap = NULL;
  983. init_rwsem(&osdc->map_sem);
  984. init_completion(&osdc->map_waiters);
  985. osdc->last_requested_map = 0;
  986. mutex_init(&osdc->request_mutex);
  987. osdc->timeout_tid = 0;
  988. osdc->last_tid = 0;
  989. osdc->osds = RB_ROOT;
  990. osdc->requests = RB_ROOT;
  991. osdc->num_requests = 0;
  992. INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout);
  993. err = -ENOMEM;
  994. osdc->req_mempool = mempool_create_kmalloc_pool(10,
  995. sizeof(struct ceph_osd_request));
  996. if (!osdc->req_mempool)
  997. goto out;
  998. err = ceph_msgpool_init(&osdc->msgpool_op, 4096, 10, true);
  999. if (err < 0)
  1000. goto out_mempool;
  1001. err = ceph_msgpool_init(&osdc->msgpool_op_reply, 512, 0, false);
  1002. if (err < 0)
  1003. goto out_msgpool;
  1004. return 0;
  1005. out_msgpool:
  1006. ceph_msgpool_destroy(&osdc->msgpool_op);
  1007. out_mempool:
  1008. mempool_destroy(osdc->req_mempool);
  1009. out:
  1010. return err;
  1011. }
  1012. void ceph_osdc_stop(struct ceph_osd_client *osdc)
  1013. {
  1014. cancel_delayed_work_sync(&osdc->timeout_work);
  1015. if (osdc->osdmap) {
  1016. ceph_osdmap_destroy(osdc->osdmap);
  1017. osdc->osdmap = NULL;
  1018. }
  1019. mempool_destroy(osdc->req_mempool);
  1020. ceph_msgpool_destroy(&osdc->msgpool_op);
  1021. ceph_msgpool_destroy(&osdc->msgpool_op_reply);
  1022. }
  1023. /*
  1024. * Read some contiguous pages. If we cross a stripe boundary, shorten
  1025. * *plen. Return number of bytes read, or error.
  1026. */
  1027. int ceph_osdc_readpages(struct ceph_osd_client *osdc,
  1028. struct ceph_vino vino, struct ceph_file_layout *layout,
  1029. u64 off, u64 *plen,
  1030. u32 truncate_seq, u64 truncate_size,
  1031. struct page **pages, int num_pages)
  1032. {
  1033. struct ceph_osd_request *req;
  1034. int rc = 0;
  1035. dout("readpages on ino %llx.%llx on %llu~%llu\n", vino.ino,
  1036. vino.snap, off, *plen);
  1037. req = ceph_osdc_new_request(osdc, layout, vino, off, plen,
  1038. CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ,
  1039. NULL, 0, truncate_seq, truncate_size, NULL,
  1040. false, 1);
  1041. if (IS_ERR(req))
  1042. return PTR_ERR(req);
  1043. /* it may be a short read due to an object boundary */
  1044. req->r_pages = pages;
  1045. num_pages = calc_pages_for(off, *plen);
  1046. req->r_num_pages = num_pages;
  1047. dout("readpages final extent is %llu~%llu (%d pages)\n",
  1048. off, *plen, req->r_num_pages);
  1049. rc = ceph_osdc_start_request(osdc, req, false);
  1050. if (!rc)
  1051. rc = ceph_osdc_wait_request(osdc, req);
  1052. ceph_osdc_put_request(req);
  1053. dout("readpages result %d\n", rc);
  1054. return rc;
  1055. }
  1056. /*
  1057. * do a synchronous write on N pages
  1058. */
  1059. int ceph_osdc_writepages(struct ceph_osd_client *osdc, struct ceph_vino vino,
  1060. struct ceph_file_layout *layout,
  1061. struct ceph_snap_context *snapc,
  1062. u64 off, u64 len,
  1063. u32 truncate_seq, u64 truncate_size,
  1064. struct timespec *mtime,
  1065. struct page **pages, int num_pages,
  1066. int flags, int do_sync, bool nofail)
  1067. {
  1068. struct ceph_osd_request *req;
  1069. int rc = 0;
  1070. BUG_ON(vino.snap != CEPH_NOSNAP);
  1071. req = ceph_osdc_new_request(osdc, layout, vino, off, &len,
  1072. CEPH_OSD_OP_WRITE,
  1073. flags | CEPH_OSD_FLAG_ONDISK |
  1074. CEPH_OSD_FLAG_WRITE,
  1075. snapc, do_sync,
  1076. truncate_seq, truncate_size, mtime,
  1077. nofail, 1);
  1078. if (IS_ERR(req))
  1079. return PTR_ERR(req);
  1080. /* it may be a short write due to an object boundary */
  1081. req->r_pages = pages;
  1082. req->r_num_pages = calc_pages_for(off, len);
  1083. dout("writepages %llu~%llu (%d pages)\n", off, len,
  1084. req->r_num_pages);
  1085. rc = ceph_osdc_start_request(osdc, req, nofail);
  1086. if (!rc)
  1087. rc = ceph_osdc_wait_request(osdc, req);
  1088. ceph_osdc_put_request(req);
  1089. if (rc == 0)
  1090. rc = len;
  1091. dout("writepages result %d\n", rc);
  1092. return rc;
  1093. }
  1094. /*
  1095. * handle incoming message
  1096. */
  1097. static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
  1098. {
  1099. struct ceph_osd *osd = con->private;
  1100. struct ceph_osd_client *osdc;
  1101. int type = le16_to_cpu(msg->hdr.type);
  1102. if (!osd)
  1103. return;
  1104. osdc = osd->o_osdc;
  1105. switch (type) {
  1106. case CEPH_MSG_OSD_MAP:
  1107. ceph_osdc_handle_map(osdc, msg);
  1108. break;
  1109. case CEPH_MSG_OSD_OPREPLY:
  1110. handle_reply(osdc, msg);
  1111. break;
  1112. default:
  1113. pr_err("received unknown message type %d %s\n", type,
  1114. ceph_msg_type_name(type));
  1115. }
  1116. ceph_msg_put(msg);
  1117. }
  1118. static struct ceph_msg *alloc_msg(struct ceph_connection *con,
  1119. struct ceph_msg_header *hdr)
  1120. {
  1121. struct ceph_osd *osd = con->private;
  1122. struct ceph_osd_client *osdc = osd->o_osdc;
  1123. int type = le16_to_cpu(hdr->type);
  1124. int front = le32_to_cpu(hdr->front_len);
  1125. switch (type) {
  1126. case CEPH_MSG_OSD_OPREPLY:
  1127. return ceph_msgpool_get(&osdc->msgpool_op_reply, front);
  1128. }
  1129. return ceph_alloc_msg(con, hdr);
  1130. }
  1131. /*
  1132. * Wrappers to refcount containing ceph_osd struct
  1133. */
  1134. static struct ceph_connection *get_osd_con(struct ceph_connection *con)
  1135. {
  1136. struct ceph_osd *osd = con->private;
  1137. if (get_osd(osd))
  1138. return con;
  1139. return NULL;
  1140. }
  1141. static void put_osd_con(struct ceph_connection *con)
  1142. {
  1143. struct ceph_osd *osd = con->private;
  1144. put_osd(osd);
  1145. }
  1146. /*
  1147. * authentication
  1148. */
  1149. static int get_authorizer(struct ceph_connection *con,
  1150. void **buf, int *len, int *proto,
  1151. void **reply_buf, int *reply_len, int force_new)
  1152. {
  1153. struct ceph_osd *o = con->private;
  1154. struct ceph_osd_client *osdc = o->o_osdc;
  1155. struct ceph_auth_client *ac = osdc->client->monc.auth;
  1156. int ret = 0;
  1157. if (force_new && o->o_authorizer) {
  1158. ac->ops->destroy_authorizer(ac, o->o_authorizer);
  1159. o->o_authorizer = NULL;
  1160. }
  1161. if (o->o_authorizer == NULL) {
  1162. ret = ac->ops->create_authorizer(
  1163. ac, CEPH_ENTITY_TYPE_OSD,
  1164. &o->o_authorizer,
  1165. &o->o_authorizer_buf,
  1166. &o->o_authorizer_buf_len,
  1167. &o->o_authorizer_reply_buf,
  1168. &o->o_authorizer_reply_buf_len);
  1169. if (ret)
  1170. return ret;
  1171. }
  1172. *proto = ac->protocol;
  1173. *buf = o->o_authorizer_buf;
  1174. *len = o->o_authorizer_buf_len;
  1175. *reply_buf = o->o_authorizer_reply_buf;
  1176. *reply_len = o->o_authorizer_reply_buf_len;
  1177. return 0;
  1178. }
  1179. static int verify_authorizer_reply(struct ceph_connection *con, int len)
  1180. {
  1181. struct ceph_osd *o = con->private;
  1182. struct ceph_osd_client *osdc = o->o_osdc;
  1183. struct ceph_auth_client *ac = osdc->client->monc.auth;
  1184. return ac->ops->verify_authorizer_reply(ac, o->o_authorizer, len);
  1185. }
  1186. const static struct ceph_connection_operations osd_con_ops = {
  1187. .get = get_osd_con,
  1188. .put = put_osd_con,
  1189. .dispatch = dispatch,
  1190. .get_authorizer = get_authorizer,
  1191. .verify_authorizer_reply = verify_authorizer_reply,
  1192. .alloc_msg = alloc_msg,
  1193. .fault = osd_reset,
  1194. .alloc_middle = ceph_alloc_middle,
  1195. .prepare_pages = prepare_pages,
  1196. };