osd_client.c 37 KB

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