osd_client.c 37 KB

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