osd_client.c 38 KB

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