osd_client.c 35 KB

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