osd_client.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392
  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. o = ceph_calc_pg_primary(osdc->osdmap, pgid);
  476. if ((req->r_osd && req->r_osd->o_osd == o &&
  477. req->r_sent >= req->r_osd->o_incarnation) ||
  478. (req->r_osd == NULL && o == -1))
  479. return 0; /* no change */
  480. dout("map_osds tid %llu pgid %d.%x osd%d (was osd%d)\n",
  481. req->r_tid, le32_to_cpu(pgid.pool), le16_to_cpu(pgid.ps), o,
  482. req->r_osd ? req->r_osd->o_osd : -1);
  483. if (req->r_osd) {
  484. __cancel_request(req);
  485. list_del_init(&req->r_osd_item);
  486. if (list_empty(&req->r_osd->o_requests)) {
  487. /* try to re-use r_osd if possible */
  488. newosd = get_osd(req->r_osd);
  489. remove_osd(osdc, newosd);
  490. }
  491. req->r_osd = NULL;
  492. }
  493. req->r_osd = __lookup_osd(osdc, o);
  494. if (!req->r_osd && o >= 0) {
  495. if (newosd) {
  496. req->r_osd = newosd;
  497. newosd = NULL;
  498. } else {
  499. err = -ENOMEM;
  500. req->r_osd = create_osd(osdc);
  501. if (!req->r_osd)
  502. goto out;
  503. }
  504. dout("map_osds osd %p is osd%d\n", req->r_osd, o);
  505. req->r_osd->o_osd = o;
  506. req->r_osd->o_con.peer_name.num = cpu_to_le64(o);
  507. __insert_osd(osdc, req->r_osd);
  508. ceph_con_open(&req->r_osd->o_con, &osdc->osdmap->osd_addr[o]);
  509. }
  510. if (req->r_osd)
  511. list_add(&req->r_osd_item, &req->r_osd->o_requests);
  512. err = 1; /* osd changed */
  513. out:
  514. if (newosd)
  515. put_osd(newosd);
  516. return err;
  517. }
  518. /*
  519. * caller should hold map_sem (for read) and request_mutex
  520. */
  521. static int __send_request(struct ceph_osd_client *osdc,
  522. struct ceph_osd_request *req)
  523. {
  524. struct ceph_osd_request_head *reqhead;
  525. int err;
  526. err = __map_osds(osdc, req);
  527. if (err < 0)
  528. return err;
  529. if (req->r_osd == NULL) {
  530. dout("send_request %p no up osds in pg\n", req);
  531. ceph_monc_request_next_osdmap(&osdc->client->monc);
  532. return 0;
  533. }
  534. dout("send_request %p tid %llu to osd%d flags %d\n",
  535. req, req->r_tid, req->r_osd->o_osd, req->r_flags);
  536. reqhead = req->r_request->front.iov_base;
  537. reqhead->osdmap_epoch = cpu_to_le32(osdc->osdmap->epoch);
  538. reqhead->flags |= cpu_to_le32(req->r_flags); /* e.g., RETRY */
  539. reqhead->reassert_version = req->r_reassert_version;
  540. req->r_timeout_stamp = jiffies+osdc->client->mount_args->osd_timeout*HZ;
  541. ceph_msg_get(req->r_request); /* send consumes a ref */
  542. ceph_con_send(&req->r_osd->o_con, req->r_request);
  543. req->r_sent = req->r_osd->o_incarnation;
  544. return 0;
  545. }
  546. /*
  547. * Timeout callback, called every N seconds when 1 or more osd
  548. * requests has been active for more than N seconds. When this
  549. * happens, we ping all OSDs with requests who have timed out to
  550. * ensure any communications channel reset is detected. Reset the
  551. * request timeouts another N seconds in the future as we go.
  552. * Reschedule the timeout event another N seconds in future (unless
  553. * there are no open requests).
  554. */
  555. static void handle_timeout(struct work_struct *work)
  556. {
  557. struct ceph_osd_client *osdc =
  558. container_of(work, struct ceph_osd_client, timeout_work.work);
  559. struct ceph_osd_request *req;
  560. struct ceph_osd *osd;
  561. unsigned long timeout = osdc->client->mount_args->osd_timeout * HZ;
  562. unsigned long next_timeout = timeout + jiffies;
  563. struct rb_node *p;
  564. dout("timeout\n");
  565. down_read(&osdc->map_sem);
  566. ceph_monc_request_next_osdmap(&osdc->client->monc);
  567. mutex_lock(&osdc->request_mutex);
  568. for (p = rb_first(&osdc->requests); p; p = rb_next(p)) {
  569. req = rb_entry(p, struct ceph_osd_request, r_node);
  570. if (req->r_resend) {
  571. int err;
  572. dout("osdc resending prev failed %lld\n", req->r_tid);
  573. err = __send_request(osdc, req);
  574. if (err)
  575. dout("osdc failed again on %lld\n", req->r_tid);
  576. else
  577. req->r_resend = false;
  578. continue;
  579. }
  580. }
  581. for (p = rb_first(&osdc->osds); p; p = rb_next(p)) {
  582. osd = rb_entry(p, struct ceph_osd, o_node);
  583. if (list_empty(&osd->o_requests))
  584. continue;
  585. req = list_first_entry(&osd->o_requests,
  586. struct ceph_osd_request, r_osd_item);
  587. if (time_before(jiffies, req->r_timeout_stamp))
  588. continue;
  589. dout(" tid %llu (at least) timed out on osd%d\n",
  590. req->r_tid, osd->o_osd);
  591. req->r_timeout_stamp = next_timeout;
  592. ceph_con_keepalive(&osd->o_con);
  593. }
  594. if (osdc->timeout_tid)
  595. schedule_delayed_work(&osdc->timeout_work,
  596. round_jiffies_relative(timeout));
  597. mutex_unlock(&osdc->request_mutex);
  598. up_read(&osdc->map_sem);
  599. }
  600. /*
  601. * handle osd op reply. either call the callback if it is specified,
  602. * or do the completion to wake up the waiting thread.
  603. */
  604. static void handle_reply(struct ceph_osd_client *osdc, struct ceph_msg *msg,
  605. struct ceph_connection *con)
  606. {
  607. struct ceph_osd_reply_head *rhead = msg->front.iov_base;
  608. struct ceph_osd_request *req;
  609. u64 tid;
  610. int numops, object_len, flags;
  611. tid = le64_to_cpu(msg->hdr.tid);
  612. if (msg->front.iov_len < sizeof(*rhead))
  613. goto bad;
  614. numops = le32_to_cpu(rhead->num_ops);
  615. object_len = le32_to_cpu(rhead->object_len);
  616. if (msg->front.iov_len != sizeof(*rhead) + object_len +
  617. numops * sizeof(struct ceph_osd_op))
  618. goto bad;
  619. dout("handle_reply %p tid %llu\n", msg, tid);
  620. /* lookup */
  621. mutex_lock(&osdc->request_mutex);
  622. req = __lookup_request(osdc, tid);
  623. if (req == NULL) {
  624. dout("handle_reply tid %llu dne\n", tid);
  625. mutex_unlock(&osdc->request_mutex);
  626. return;
  627. }
  628. ceph_osdc_get_request(req);
  629. flags = le32_to_cpu(rhead->flags);
  630. /*
  631. * if this connection filled our pages, drop our reference now, to
  632. * avoid a (safe but slower) revoke later.
  633. */
  634. if (req->r_con_filling_pages == con && req->r_pages == msg->pages) {
  635. dout(" got pages, dropping con_filling_pages ref %p\n", con);
  636. req->r_con_filling_pages = NULL;
  637. ceph_con_put(con);
  638. }
  639. if (req->r_reply) {
  640. /*
  641. * once we see the message has been received, we don't
  642. * need a ref (which is only needed for revoking
  643. * pages)
  644. */
  645. ceph_msg_put(req->r_reply);
  646. req->r_reply = NULL;
  647. }
  648. if (!req->r_got_reply) {
  649. unsigned bytes;
  650. req->r_result = le32_to_cpu(rhead->result);
  651. bytes = le32_to_cpu(msg->hdr.data_len);
  652. dout("handle_reply result %d bytes %d\n", req->r_result,
  653. bytes);
  654. if (req->r_result == 0)
  655. req->r_result = bytes;
  656. /* in case this is a write and we need to replay, */
  657. req->r_reassert_version = rhead->reassert_version;
  658. req->r_got_reply = 1;
  659. } else if ((flags & CEPH_OSD_FLAG_ONDISK) == 0) {
  660. dout("handle_reply tid %llu dup ack\n", tid);
  661. mutex_unlock(&osdc->request_mutex);
  662. goto done;
  663. }
  664. dout("handle_reply tid %llu flags %d\n", tid, flags);
  665. /* either this is a read, or we got the safe response */
  666. if ((flags & CEPH_OSD_FLAG_ONDISK) ||
  667. ((flags & CEPH_OSD_FLAG_WRITE) == 0))
  668. __unregister_request(osdc, req);
  669. mutex_unlock(&osdc->request_mutex);
  670. if (req->r_callback)
  671. req->r_callback(req, msg);
  672. else
  673. complete(&req->r_completion);
  674. if (flags & CEPH_OSD_FLAG_ONDISK) {
  675. if (req->r_safe_callback)
  676. req->r_safe_callback(req, msg);
  677. complete(&req->r_safe_completion); /* fsync waiter */
  678. }
  679. done:
  680. ceph_osdc_put_request(req);
  681. return;
  682. bad:
  683. pr_err("corrupt osd_op_reply got %d %d expected %d\n",
  684. (int)msg->front.iov_len, le32_to_cpu(msg->hdr.front_len),
  685. (int)sizeof(*rhead));
  686. ceph_msg_dump(msg);
  687. }
  688. /*
  689. * Resubmit osd requests whose osd or osd address has changed. Request
  690. * a new osd map if osds are down, or we are otherwise unable to determine
  691. * how to direct a request.
  692. *
  693. * Close connections to down osds.
  694. *
  695. * If @who is specified, resubmit requests for that specific osd.
  696. *
  697. * Caller should hold map_sem for read and request_mutex.
  698. */
  699. static void kick_requests(struct ceph_osd_client *osdc,
  700. struct ceph_osd *kickosd)
  701. {
  702. struct ceph_osd_request *req;
  703. struct rb_node *p, *n;
  704. int needmap = 0;
  705. int err;
  706. dout("kick_requests osd%d\n", kickosd ? kickosd->o_osd : -1);
  707. mutex_lock(&osdc->request_mutex);
  708. if (!kickosd) {
  709. for (p = rb_first(&osdc->osds); p; p = n) {
  710. struct ceph_osd *osd =
  711. rb_entry(p, struct ceph_osd, o_node);
  712. n = rb_next(p);
  713. if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) ||
  714. !ceph_entity_addr_equal(&osd->o_con.peer_addr,
  715. ceph_osd_addr(osdc->osdmap,
  716. osd->o_osd)))
  717. reset_osd(osdc, osd);
  718. }
  719. }
  720. for (p = rb_first(&osdc->requests); p; p = rb_next(p)) {
  721. req = rb_entry(p, struct ceph_osd_request, r_node);
  722. if (req->r_resend) {
  723. dout(" r_resend set on tid %llu\n", req->r_tid);
  724. __cancel_request(req);
  725. goto kick;
  726. }
  727. if (req->r_osd && kickosd == req->r_osd) {
  728. __cancel_request(req);
  729. goto kick;
  730. }
  731. err = __map_osds(osdc, req);
  732. if (err == 0)
  733. continue; /* no change */
  734. if (err < 0) {
  735. /*
  736. * FIXME: really, we should set the request
  737. * error and fail if this isn't a 'nofail'
  738. * request, but that's a fair bit more
  739. * complicated to do. So retry!
  740. */
  741. dout(" setting r_resend on %llu\n", req->r_tid);
  742. req->r_resend = true;
  743. continue;
  744. }
  745. if (req->r_osd == NULL) {
  746. dout("tid %llu maps to no valid osd\n", req->r_tid);
  747. needmap++; /* request a newer map */
  748. continue;
  749. }
  750. kick:
  751. dout("kicking %p tid %llu osd%d\n", req, req->r_tid,
  752. req->r_osd->o_osd);
  753. req->r_flags |= CEPH_OSD_FLAG_RETRY;
  754. err = __send_request(osdc, req);
  755. if (err) {
  756. dout(" setting r_resend on %llu\n", req->r_tid);
  757. req->r_resend = true;
  758. }
  759. }
  760. mutex_unlock(&osdc->request_mutex);
  761. if (needmap) {
  762. dout("%d requests for down osds, need new map\n", needmap);
  763. ceph_monc_request_next_osdmap(&osdc->client->monc);
  764. }
  765. }
  766. /*
  767. * Process updated osd map.
  768. *
  769. * The message contains any number of incremental and full maps, normally
  770. * indicating some sort of topology change in the cluster. Kick requests
  771. * off to different OSDs as needed.
  772. */
  773. void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg)
  774. {
  775. void *p, *end, *next;
  776. u32 nr_maps, maplen;
  777. u32 epoch;
  778. struct ceph_osdmap *newmap = NULL, *oldmap;
  779. int err;
  780. struct ceph_fsid fsid;
  781. dout("handle_map have %u\n", osdc->osdmap ? osdc->osdmap->epoch : 0);
  782. p = msg->front.iov_base;
  783. end = p + msg->front.iov_len;
  784. /* verify fsid */
  785. ceph_decode_need(&p, end, sizeof(fsid), bad);
  786. ceph_decode_copy(&p, &fsid, sizeof(fsid));
  787. if (ceph_check_fsid(osdc->client, &fsid) < 0)
  788. return;
  789. down_write(&osdc->map_sem);
  790. /* incremental maps */
  791. ceph_decode_32_safe(&p, end, nr_maps, bad);
  792. dout(" %d inc maps\n", nr_maps);
  793. while (nr_maps > 0) {
  794. ceph_decode_need(&p, end, 2*sizeof(u32), bad);
  795. epoch = ceph_decode_32(&p);
  796. maplen = ceph_decode_32(&p);
  797. ceph_decode_need(&p, end, maplen, bad);
  798. next = p + maplen;
  799. if (osdc->osdmap && osdc->osdmap->epoch+1 == epoch) {
  800. dout("applying incremental map %u len %d\n",
  801. epoch, maplen);
  802. newmap = osdmap_apply_incremental(&p, next,
  803. osdc->osdmap,
  804. osdc->client->msgr);
  805. if (IS_ERR(newmap)) {
  806. err = PTR_ERR(newmap);
  807. goto bad;
  808. }
  809. BUG_ON(!newmap);
  810. if (newmap != osdc->osdmap) {
  811. ceph_osdmap_destroy(osdc->osdmap);
  812. osdc->osdmap = newmap;
  813. }
  814. } else {
  815. dout("ignoring incremental map %u len %d\n",
  816. epoch, maplen);
  817. }
  818. p = next;
  819. nr_maps--;
  820. }
  821. if (newmap)
  822. goto done;
  823. /* full maps */
  824. ceph_decode_32_safe(&p, end, nr_maps, bad);
  825. dout(" %d full maps\n", nr_maps);
  826. while (nr_maps) {
  827. ceph_decode_need(&p, end, 2*sizeof(u32), bad);
  828. epoch = ceph_decode_32(&p);
  829. maplen = ceph_decode_32(&p);
  830. ceph_decode_need(&p, end, maplen, bad);
  831. if (nr_maps > 1) {
  832. dout("skipping non-latest full map %u len %d\n",
  833. epoch, maplen);
  834. } else if (osdc->osdmap && osdc->osdmap->epoch >= epoch) {
  835. dout("skipping full map %u len %d, "
  836. "older than our %u\n", epoch, maplen,
  837. osdc->osdmap->epoch);
  838. } else {
  839. dout("taking full map %u len %d\n", epoch, maplen);
  840. newmap = osdmap_decode(&p, p+maplen);
  841. if (IS_ERR(newmap)) {
  842. err = PTR_ERR(newmap);
  843. goto bad;
  844. }
  845. BUG_ON(!newmap);
  846. oldmap = osdc->osdmap;
  847. osdc->osdmap = newmap;
  848. if (oldmap)
  849. ceph_osdmap_destroy(oldmap);
  850. }
  851. p += maplen;
  852. nr_maps--;
  853. }
  854. done:
  855. downgrade_write(&osdc->map_sem);
  856. ceph_monc_got_osdmap(&osdc->client->monc, osdc->osdmap->epoch);
  857. if (newmap)
  858. kick_requests(osdc, NULL);
  859. up_read(&osdc->map_sem);
  860. return;
  861. bad:
  862. pr_err("osdc handle_map corrupt msg\n");
  863. ceph_msg_dump(msg);
  864. up_write(&osdc->map_sem);
  865. return;
  866. }
  867. /*
  868. * A read request prepares specific pages that data is to be read into.
  869. * When a message is being read off the wire, we call prepare_pages to
  870. * find those pages.
  871. * 0 = success, -1 failure.
  872. */
  873. static int prepare_pages(struct ceph_connection *con, struct ceph_msg *m,
  874. int want)
  875. {
  876. struct ceph_osd *osd = con->private;
  877. struct ceph_osd_client *osdc;
  878. struct ceph_osd_request *req;
  879. u64 tid;
  880. int ret = -1;
  881. int type = le16_to_cpu(m->hdr.type);
  882. if (!osd)
  883. return -1;
  884. osdc = osd->o_osdc;
  885. dout("prepare_pages on msg %p want %d\n", m, want);
  886. if (unlikely(type != CEPH_MSG_OSD_OPREPLY))
  887. return -1; /* hmm! */
  888. tid = le64_to_cpu(m->hdr.tid);
  889. mutex_lock(&osdc->request_mutex);
  890. req = __lookup_request(osdc, tid);
  891. if (!req) {
  892. dout("prepare_pages unknown tid %llu\n", tid);
  893. goto out;
  894. }
  895. dout("prepare_pages tid %llu has %d pages, want %d\n",
  896. tid, req->r_num_pages, want);
  897. if (unlikely(req->r_num_pages < want))
  898. goto out;
  899. if (req->r_con_filling_pages) {
  900. dout("revoking pages %p from old con %p\n", req->r_pages,
  901. req->r_con_filling_pages);
  902. ceph_con_revoke_pages(req->r_con_filling_pages, req->r_pages);
  903. ceph_con_put(req->r_con_filling_pages);
  904. }
  905. req->r_con_filling_pages = ceph_con_get(con);
  906. req->r_reply = ceph_msg_get(m); /* for duration of read over socket */
  907. m->pages = req->r_pages;
  908. m->nr_pages = req->r_num_pages;
  909. ret = 0; /* success */
  910. out:
  911. mutex_unlock(&osdc->request_mutex);
  912. return ret;
  913. }
  914. /*
  915. * Register request, send initial attempt.
  916. */
  917. int ceph_osdc_start_request(struct ceph_osd_client *osdc,
  918. struct ceph_osd_request *req,
  919. bool nofail)
  920. {
  921. int rc = 0;
  922. req->r_request->pages = req->r_pages;
  923. req->r_request->nr_pages = req->r_num_pages;
  924. register_request(osdc, req);
  925. down_read(&osdc->map_sem);
  926. mutex_lock(&osdc->request_mutex);
  927. /*
  928. * a racing kick_requests() may have sent the message for us
  929. * while we dropped request_mutex above, so only send now if
  930. * the request still han't been touched yet.
  931. */
  932. if (req->r_sent == 0) {
  933. rc = __send_request(osdc, req);
  934. if (rc) {
  935. if (nofail) {
  936. dout("osdc_start_request failed send, "
  937. " marking %lld\n", req->r_tid);
  938. req->r_resend = true;
  939. rc = 0;
  940. } else {
  941. __unregister_request(osdc, req);
  942. }
  943. }
  944. }
  945. mutex_unlock(&osdc->request_mutex);
  946. up_read(&osdc->map_sem);
  947. return rc;
  948. }
  949. /*
  950. * wait for a request to complete
  951. */
  952. int ceph_osdc_wait_request(struct ceph_osd_client *osdc,
  953. struct ceph_osd_request *req)
  954. {
  955. int rc;
  956. rc = wait_for_completion_interruptible(&req->r_completion);
  957. if (rc < 0) {
  958. mutex_lock(&osdc->request_mutex);
  959. __cancel_request(req);
  960. __unregister_request(osdc, req);
  961. mutex_unlock(&osdc->request_mutex);
  962. dout("wait_request tid %llu canceled/timed out\n", req->r_tid);
  963. return rc;
  964. }
  965. dout("wait_request tid %llu result %d\n", req->r_tid, req->r_result);
  966. return req->r_result;
  967. }
  968. /*
  969. * sync - wait for all in-flight requests to flush. avoid starvation.
  970. */
  971. void ceph_osdc_sync(struct ceph_osd_client *osdc)
  972. {
  973. struct ceph_osd_request *req;
  974. u64 last_tid, next_tid = 0;
  975. mutex_lock(&osdc->request_mutex);
  976. last_tid = osdc->last_tid;
  977. while (1) {
  978. req = __lookup_request_ge(osdc, next_tid);
  979. if (!req)
  980. break;
  981. if (req->r_tid > last_tid)
  982. break;
  983. next_tid = req->r_tid + 1;
  984. if ((req->r_flags & CEPH_OSD_FLAG_WRITE) == 0)
  985. continue;
  986. ceph_osdc_get_request(req);
  987. mutex_unlock(&osdc->request_mutex);
  988. dout("sync waiting on tid %llu (last is %llu)\n",
  989. req->r_tid, last_tid);
  990. wait_for_completion(&req->r_safe_completion);
  991. mutex_lock(&osdc->request_mutex);
  992. ceph_osdc_put_request(req);
  993. }
  994. mutex_unlock(&osdc->request_mutex);
  995. dout("sync done (thru tid %llu)\n", last_tid);
  996. }
  997. /*
  998. * init, shutdown
  999. */
  1000. int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client)
  1001. {
  1002. int err;
  1003. dout("init\n");
  1004. osdc->client = client;
  1005. osdc->osdmap = NULL;
  1006. init_rwsem(&osdc->map_sem);
  1007. init_completion(&osdc->map_waiters);
  1008. osdc->last_requested_map = 0;
  1009. mutex_init(&osdc->request_mutex);
  1010. osdc->timeout_tid = 0;
  1011. osdc->last_tid = 0;
  1012. osdc->osds = RB_ROOT;
  1013. osdc->requests = RB_ROOT;
  1014. osdc->num_requests = 0;
  1015. INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout);
  1016. err = -ENOMEM;
  1017. osdc->req_mempool = mempool_create_kmalloc_pool(10,
  1018. sizeof(struct ceph_osd_request));
  1019. if (!osdc->req_mempool)
  1020. goto out;
  1021. err = ceph_msgpool_init(&osdc->msgpool_op, 4096, 10, true);
  1022. if (err < 0)
  1023. goto out_mempool;
  1024. err = ceph_msgpool_init(&osdc->msgpool_op_reply, 512, 0, false);
  1025. if (err < 0)
  1026. goto out_msgpool;
  1027. return 0;
  1028. out_msgpool:
  1029. ceph_msgpool_destroy(&osdc->msgpool_op);
  1030. out_mempool:
  1031. mempool_destroy(osdc->req_mempool);
  1032. out:
  1033. return err;
  1034. }
  1035. void ceph_osdc_stop(struct ceph_osd_client *osdc)
  1036. {
  1037. cancel_delayed_work_sync(&osdc->timeout_work);
  1038. if (osdc->osdmap) {
  1039. ceph_osdmap_destroy(osdc->osdmap);
  1040. osdc->osdmap = NULL;
  1041. }
  1042. mempool_destroy(osdc->req_mempool);
  1043. ceph_msgpool_destroy(&osdc->msgpool_op);
  1044. ceph_msgpool_destroy(&osdc->msgpool_op_reply);
  1045. }
  1046. /*
  1047. * Read some contiguous pages. If we cross a stripe boundary, shorten
  1048. * *plen. Return number of bytes read, or error.
  1049. */
  1050. int ceph_osdc_readpages(struct ceph_osd_client *osdc,
  1051. struct ceph_vino vino, struct ceph_file_layout *layout,
  1052. u64 off, u64 *plen,
  1053. u32 truncate_seq, u64 truncate_size,
  1054. struct page **pages, int num_pages)
  1055. {
  1056. struct ceph_osd_request *req;
  1057. int rc = 0;
  1058. dout("readpages on ino %llx.%llx on %llu~%llu\n", vino.ino,
  1059. vino.snap, off, *plen);
  1060. req = ceph_osdc_new_request(osdc, layout, vino, off, plen,
  1061. CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ,
  1062. NULL, 0, truncate_seq, truncate_size, NULL,
  1063. false, 1);
  1064. if (IS_ERR(req))
  1065. return PTR_ERR(req);
  1066. /* it may be a short read due to an object boundary */
  1067. req->r_pages = pages;
  1068. num_pages = calc_pages_for(off, *plen);
  1069. req->r_num_pages = num_pages;
  1070. dout("readpages final extent is %llu~%llu (%d pages)\n",
  1071. off, *plen, req->r_num_pages);
  1072. rc = ceph_osdc_start_request(osdc, req, false);
  1073. if (!rc)
  1074. rc = ceph_osdc_wait_request(osdc, req);
  1075. ceph_osdc_put_request(req);
  1076. dout("readpages result %d\n", rc);
  1077. return rc;
  1078. }
  1079. /*
  1080. * do a synchronous write on N pages
  1081. */
  1082. int ceph_osdc_writepages(struct ceph_osd_client *osdc, struct ceph_vino vino,
  1083. struct ceph_file_layout *layout,
  1084. struct ceph_snap_context *snapc,
  1085. u64 off, u64 len,
  1086. u32 truncate_seq, u64 truncate_size,
  1087. struct timespec *mtime,
  1088. struct page **pages, int num_pages,
  1089. int flags, int do_sync, bool nofail)
  1090. {
  1091. struct ceph_osd_request *req;
  1092. int rc = 0;
  1093. BUG_ON(vino.snap != CEPH_NOSNAP);
  1094. req = ceph_osdc_new_request(osdc, layout, vino, off, &len,
  1095. CEPH_OSD_OP_WRITE,
  1096. flags | CEPH_OSD_FLAG_ONDISK |
  1097. CEPH_OSD_FLAG_WRITE,
  1098. snapc, do_sync,
  1099. truncate_seq, truncate_size, mtime,
  1100. nofail, 1);
  1101. if (IS_ERR(req))
  1102. return PTR_ERR(req);
  1103. /* it may be a short write due to an object boundary */
  1104. req->r_pages = pages;
  1105. req->r_num_pages = calc_pages_for(off, len);
  1106. dout("writepages %llu~%llu (%d pages)\n", off, len,
  1107. req->r_num_pages);
  1108. rc = ceph_osdc_start_request(osdc, req, nofail);
  1109. if (!rc)
  1110. rc = ceph_osdc_wait_request(osdc, req);
  1111. ceph_osdc_put_request(req);
  1112. if (rc == 0)
  1113. rc = len;
  1114. dout("writepages result %d\n", rc);
  1115. return rc;
  1116. }
  1117. /*
  1118. * handle incoming message
  1119. */
  1120. static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
  1121. {
  1122. struct ceph_osd *osd = con->private;
  1123. struct ceph_osd_client *osdc;
  1124. int type = le16_to_cpu(msg->hdr.type);
  1125. if (!osd)
  1126. return;
  1127. osdc = osd->o_osdc;
  1128. switch (type) {
  1129. case CEPH_MSG_OSD_MAP:
  1130. ceph_osdc_handle_map(osdc, msg);
  1131. break;
  1132. case CEPH_MSG_OSD_OPREPLY:
  1133. handle_reply(osdc, msg, con);
  1134. break;
  1135. default:
  1136. pr_err("received unknown message type %d %s\n", type,
  1137. ceph_msg_type_name(type));
  1138. }
  1139. ceph_msg_put(msg);
  1140. }
  1141. static struct ceph_msg *alloc_msg(struct ceph_connection *con,
  1142. struct ceph_msg_header *hdr)
  1143. {
  1144. struct ceph_osd *osd = con->private;
  1145. struct ceph_osd_client *osdc = osd->o_osdc;
  1146. int type = le16_to_cpu(hdr->type);
  1147. int front = le32_to_cpu(hdr->front_len);
  1148. switch (type) {
  1149. case CEPH_MSG_OSD_OPREPLY:
  1150. return ceph_msgpool_get(&osdc->msgpool_op_reply, front);
  1151. }
  1152. return ceph_alloc_msg(con, hdr);
  1153. }
  1154. /*
  1155. * Wrappers to refcount containing ceph_osd struct
  1156. */
  1157. static struct ceph_connection *get_osd_con(struct ceph_connection *con)
  1158. {
  1159. struct ceph_osd *osd = con->private;
  1160. if (get_osd(osd))
  1161. return con;
  1162. return NULL;
  1163. }
  1164. static void put_osd_con(struct ceph_connection *con)
  1165. {
  1166. struct ceph_osd *osd = con->private;
  1167. put_osd(osd);
  1168. }
  1169. /*
  1170. * authentication
  1171. */
  1172. static int get_authorizer(struct ceph_connection *con,
  1173. void **buf, int *len, int *proto,
  1174. void **reply_buf, int *reply_len, int force_new)
  1175. {
  1176. struct ceph_osd *o = con->private;
  1177. struct ceph_osd_client *osdc = o->o_osdc;
  1178. struct ceph_auth_client *ac = osdc->client->monc.auth;
  1179. int ret = 0;
  1180. if (force_new && o->o_authorizer) {
  1181. ac->ops->destroy_authorizer(ac, o->o_authorizer);
  1182. o->o_authorizer = NULL;
  1183. }
  1184. if (o->o_authorizer == NULL) {
  1185. ret = ac->ops->create_authorizer(
  1186. ac, CEPH_ENTITY_TYPE_OSD,
  1187. &o->o_authorizer,
  1188. &o->o_authorizer_buf,
  1189. &o->o_authorizer_buf_len,
  1190. &o->o_authorizer_reply_buf,
  1191. &o->o_authorizer_reply_buf_len);
  1192. if (ret)
  1193. return ret;
  1194. }
  1195. *proto = ac->protocol;
  1196. *buf = o->o_authorizer_buf;
  1197. *len = o->o_authorizer_buf_len;
  1198. *reply_buf = o->o_authorizer_reply_buf;
  1199. *reply_len = o->o_authorizer_reply_buf_len;
  1200. return 0;
  1201. }
  1202. static int verify_authorizer_reply(struct ceph_connection *con, int len)
  1203. {
  1204. struct ceph_osd *o = con->private;
  1205. struct ceph_osd_client *osdc = o->o_osdc;
  1206. struct ceph_auth_client *ac = osdc->client->monc.auth;
  1207. return ac->ops->verify_authorizer_reply(ac, o->o_authorizer, len);
  1208. }
  1209. const static struct ceph_connection_operations osd_con_ops = {
  1210. .get = get_osd_con,
  1211. .put = put_osd_con,
  1212. .dispatch = dispatch,
  1213. .get_authorizer = get_authorizer,
  1214. .verify_authorizer_reply = verify_authorizer_reply,
  1215. .alloc_msg = alloc_msg,
  1216. .fault = osd_reset,
  1217. .alloc_middle = ceph_alloc_middle,
  1218. .prepare_pages = prepare_pages,
  1219. };