osd_client.c 39 KB

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