osd_client.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550
  1. #include "ceph_debug.h"
  2. #include <linux/err.h>
  3. #include <linux/highmem.h>
  4. #include <linux/mm.h>
  5. #include <linux/pagemap.h>
  6. #include <linux/slab.h>
  7. #include <linux/uaccess.h>
  8. #include "super.h"
  9. #include "osd_client.h"
  10. #include "messenger.h"
  11. #include "decode.h"
  12. #include "auth.h"
  13. #define OSD_OP_FRONT_LEN 4096
  14. #define OSD_OPREPLY_FRONT_LEN 512
  15. const static struct ceph_connection_operations osd_con_ops;
  16. static 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 ERR_PTR(-ENOMEM);
  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, 0, 0, NULL);
  149. if (IS_ERR(msg)) {
  150. ceph_osdc_put_request(req);
  151. return ERR_PTR(PTR_ERR(msg));
  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, 0, 0, NULL);
  162. if (IS_ERR(msg)) {
  163. ceph_osdc_put_request(req);
  164. return ERR_PTR(PTR_ERR(msg));
  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. kfree(osd);
  322. }
  323. /*
  324. * remove an osd from our map
  325. */
  326. static void __remove_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
  327. {
  328. dout("__remove_osd %p\n", osd);
  329. BUG_ON(!list_empty(&osd->o_requests));
  330. rb_erase(&osd->o_node, &osdc->osds);
  331. list_del_init(&osd->o_osd_lru);
  332. ceph_con_close(&osd->o_con);
  333. put_osd(osd);
  334. }
  335. static void __move_osd_to_lru(struct ceph_osd_client *osdc,
  336. struct ceph_osd *osd)
  337. {
  338. dout("__move_osd_to_lru %p\n", osd);
  339. BUG_ON(!list_empty(&osd->o_osd_lru));
  340. list_add_tail(&osd->o_osd_lru, &osdc->osd_lru);
  341. osd->lru_ttl = jiffies + osdc->client->mount_args->osd_idle_ttl * HZ;
  342. }
  343. static void __remove_osd_from_lru(struct ceph_osd *osd)
  344. {
  345. dout("__remove_osd_from_lru %p\n", osd);
  346. if (!list_empty(&osd->o_osd_lru))
  347. list_del_init(&osd->o_osd_lru);
  348. }
  349. static void remove_old_osds(struct ceph_osd_client *osdc, int remove_all)
  350. {
  351. struct ceph_osd *osd, *nosd;
  352. dout("__remove_old_osds %p\n", osdc);
  353. mutex_lock(&osdc->request_mutex);
  354. list_for_each_entry_safe(osd, nosd, &osdc->osd_lru, o_osd_lru) {
  355. if (!remove_all && time_before(jiffies, osd->lru_ttl))
  356. break;
  357. __remove_osd(osdc, osd);
  358. }
  359. mutex_unlock(&osdc->request_mutex);
  360. }
  361. /*
  362. * reset osd connect
  363. */
  364. static int __reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
  365. {
  366. struct ceph_osd_request *req;
  367. int ret = 0;
  368. dout("__reset_osd %p osd%d\n", osd, osd->o_osd);
  369. if (list_empty(&osd->o_requests)) {
  370. __remove_osd(osdc, osd);
  371. } else if (memcmp(&osdc->osdmap->osd_addr[osd->o_osd],
  372. &osd->o_con.peer_addr,
  373. sizeof(osd->o_con.peer_addr)) == 0 &&
  374. !ceph_con_opened(&osd->o_con)) {
  375. dout(" osd addr hasn't changed and connection never opened,"
  376. " letting msgr retry");
  377. /* touch each r_stamp for handle_timeout()'s benfit */
  378. list_for_each_entry(req, &osd->o_requests, r_osd_item)
  379. req->r_stamp = jiffies;
  380. ret = -EAGAIN;
  381. } else {
  382. ceph_con_close(&osd->o_con);
  383. ceph_con_open(&osd->o_con, &osdc->osdmap->osd_addr[osd->o_osd]);
  384. osd->o_incarnation++;
  385. }
  386. return ret;
  387. }
  388. static void __insert_osd(struct ceph_osd_client *osdc, struct ceph_osd *new)
  389. {
  390. struct rb_node **p = &osdc->osds.rb_node;
  391. struct rb_node *parent = NULL;
  392. struct ceph_osd *osd = NULL;
  393. while (*p) {
  394. parent = *p;
  395. osd = rb_entry(parent, struct ceph_osd, o_node);
  396. if (new->o_osd < osd->o_osd)
  397. p = &(*p)->rb_left;
  398. else if (new->o_osd > osd->o_osd)
  399. p = &(*p)->rb_right;
  400. else
  401. BUG();
  402. }
  403. rb_link_node(&new->o_node, parent, p);
  404. rb_insert_color(&new->o_node, &osdc->osds);
  405. }
  406. static struct ceph_osd *__lookup_osd(struct ceph_osd_client *osdc, int o)
  407. {
  408. struct ceph_osd *osd;
  409. struct rb_node *n = osdc->osds.rb_node;
  410. while (n) {
  411. osd = rb_entry(n, struct ceph_osd, o_node);
  412. if (o < osd->o_osd)
  413. n = n->rb_left;
  414. else if (o > osd->o_osd)
  415. n = n->rb_right;
  416. else
  417. return osd;
  418. }
  419. return NULL;
  420. }
  421. static void __schedule_osd_timeout(struct ceph_osd_client *osdc)
  422. {
  423. schedule_delayed_work(&osdc->timeout_work,
  424. osdc->client->mount_args->osd_keepalive_timeout * HZ);
  425. }
  426. static void __cancel_osd_timeout(struct ceph_osd_client *osdc)
  427. {
  428. cancel_delayed_work(&osdc->timeout_work);
  429. }
  430. /*
  431. * Register request, assign tid. If this is the first request, set up
  432. * the timeout event.
  433. */
  434. static void register_request(struct ceph_osd_client *osdc,
  435. struct ceph_osd_request *req)
  436. {
  437. mutex_lock(&osdc->request_mutex);
  438. req->r_tid = ++osdc->last_tid;
  439. req->r_request->hdr.tid = cpu_to_le64(req->r_tid);
  440. INIT_LIST_HEAD(&req->r_req_lru_item);
  441. dout("register_request %p tid %lld\n", req, req->r_tid);
  442. __insert_request(osdc, req);
  443. ceph_osdc_get_request(req);
  444. osdc->num_requests++;
  445. if (osdc->num_requests == 1) {
  446. dout(" first request, scheduling timeout\n");
  447. __schedule_osd_timeout(osdc);
  448. }
  449. mutex_unlock(&osdc->request_mutex);
  450. }
  451. /*
  452. * called under osdc->request_mutex
  453. */
  454. static void __unregister_request(struct ceph_osd_client *osdc,
  455. struct ceph_osd_request *req)
  456. {
  457. dout("__unregister_request %p tid %lld\n", req, req->r_tid);
  458. rb_erase(&req->r_node, &osdc->requests);
  459. osdc->num_requests--;
  460. if (req->r_osd) {
  461. /* make sure the original request isn't in flight. */
  462. ceph_con_revoke(&req->r_osd->o_con, req->r_request);
  463. list_del_init(&req->r_osd_item);
  464. if (list_empty(&req->r_osd->o_requests))
  465. __move_osd_to_lru(osdc, req->r_osd);
  466. req->r_osd = NULL;
  467. }
  468. ceph_osdc_put_request(req);
  469. list_del_init(&req->r_req_lru_item);
  470. if (osdc->num_requests == 0) {
  471. dout(" no requests, canceling timeout\n");
  472. __cancel_osd_timeout(osdc);
  473. }
  474. }
  475. /*
  476. * Cancel a previously queued request message
  477. */
  478. static void __cancel_request(struct ceph_osd_request *req)
  479. {
  480. if (req->r_sent) {
  481. ceph_con_revoke(&req->r_osd->o_con, req->r_request);
  482. req->r_sent = 0;
  483. }
  484. list_del_init(&req->r_req_lru_item);
  485. }
  486. /*
  487. * Pick an osd (the first 'up' osd in the pg), allocate the osd struct
  488. * (as needed), and set the request r_osd appropriately. If there is
  489. * no up osd, set r_osd to NULL.
  490. *
  491. * Return 0 if unchanged, 1 if changed, or negative on error.
  492. *
  493. * Caller should hold map_sem for read and request_mutex.
  494. */
  495. static int __map_osds(struct ceph_osd_client *osdc,
  496. struct ceph_osd_request *req)
  497. {
  498. struct ceph_osd_request_head *reqhead = req->r_request->front.iov_base;
  499. struct ceph_pg pgid;
  500. int o = -1;
  501. int err;
  502. dout("map_osds %p tid %lld\n", req, req->r_tid);
  503. err = ceph_calc_object_layout(&reqhead->layout, req->r_oid,
  504. &req->r_file_layout, osdc->osdmap);
  505. if (err)
  506. return err;
  507. pgid = reqhead->layout.ol_pgid;
  508. req->r_pgid = pgid;
  509. o = ceph_calc_pg_primary(osdc->osdmap, pgid);
  510. if ((req->r_osd && req->r_osd->o_osd == o &&
  511. req->r_sent >= req->r_osd->o_incarnation) ||
  512. (req->r_osd == NULL && o == -1))
  513. return 0; /* no change */
  514. dout("map_osds tid %llu pgid %d.%x osd%d (was osd%d)\n",
  515. req->r_tid, le32_to_cpu(pgid.pool), le16_to_cpu(pgid.ps), o,
  516. req->r_osd ? req->r_osd->o_osd : -1);
  517. if (req->r_osd) {
  518. __cancel_request(req);
  519. list_del_init(&req->r_osd_item);
  520. req->r_osd = NULL;
  521. }
  522. req->r_osd = __lookup_osd(osdc, o);
  523. if (!req->r_osd && o >= 0) {
  524. err = -ENOMEM;
  525. req->r_osd = create_osd(osdc);
  526. if (!req->r_osd)
  527. goto out;
  528. dout("map_osds osd %p is osd%d\n", req->r_osd, o);
  529. req->r_osd->o_osd = o;
  530. req->r_osd->o_con.peer_name.num = cpu_to_le64(o);
  531. __insert_osd(osdc, req->r_osd);
  532. ceph_con_open(&req->r_osd->o_con, &osdc->osdmap->osd_addr[o]);
  533. }
  534. if (req->r_osd) {
  535. __remove_osd_from_lru(req->r_osd);
  536. list_add(&req->r_osd_item, &req->r_osd->o_requests);
  537. }
  538. err = 1; /* osd changed */
  539. out:
  540. return err;
  541. }
  542. /*
  543. * caller should hold map_sem (for read) and request_mutex
  544. */
  545. static int __send_request(struct ceph_osd_client *osdc,
  546. struct ceph_osd_request *req)
  547. {
  548. struct ceph_osd_request_head *reqhead;
  549. int err;
  550. err = __map_osds(osdc, req);
  551. if (err < 0)
  552. return err;
  553. if (req->r_osd == NULL) {
  554. dout("send_request %p no up osds in pg\n", req);
  555. ceph_monc_request_next_osdmap(&osdc->client->monc);
  556. return 0;
  557. }
  558. dout("send_request %p tid %llu to osd%d flags %d\n",
  559. req, req->r_tid, req->r_osd->o_osd, req->r_flags);
  560. reqhead = req->r_request->front.iov_base;
  561. reqhead->osdmap_epoch = cpu_to_le32(osdc->osdmap->epoch);
  562. reqhead->flags |= cpu_to_le32(req->r_flags); /* e.g., RETRY */
  563. reqhead->reassert_version = req->r_reassert_version;
  564. req->r_stamp = jiffies;
  565. list_move_tail(&osdc->req_lru, &req->r_req_lru_item);
  566. ceph_msg_get(req->r_request); /* send consumes a ref */
  567. ceph_con_send(&req->r_osd->o_con, req->r_request);
  568. req->r_sent = req->r_osd->o_incarnation;
  569. return 0;
  570. }
  571. /*
  572. * Timeout callback, called every N seconds when 1 or more osd
  573. * requests has been active for more than N seconds. When this
  574. * happens, we ping all OSDs with requests who have timed out to
  575. * ensure any communications channel reset is detected. Reset the
  576. * request timeouts another N seconds in the future as we go.
  577. * Reschedule the timeout event another N seconds in future (unless
  578. * there are no open requests).
  579. */
  580. static void handle_timeout(struct work_struct *work)
  581. {
  582. struct ceph_osd_client *osdc =
  583. container_of(work, struct ceph_osd_client, timeout_work.work);
  584. struct ceph_osd_request *req, *last_req = NULL;
  585. struct ceph_osd *osd;
  586. unsigned long timeout = osdc->client->mount_args->osd_timeout * HZ;
  587. unsigned long keepalive =
  588. osdc->client->mount_args->osd_keepalive_timeout * HZ;
  589. unsigned long last_stamp = 0;
  590. struct rb_node *p;
  591. struct list_head slow_osds;
  592. dout("timeout\n");
  593. down_read(&osdc->map_sem);
  594. ceph_monc_request_next_osdmap(&osdc->client->monc);
  595. mutex_lock(&osdc->request_mutex);
  596. for (p = rb_first(&osdc->requests); p; p = rb_next(p)) {
  597. req = rb_entry(p, struct ceph_osd_request, r_node);
  598. if (req->r_resend) {
  599. int err;
  600. dout("osdc resending prev failed %lld\n", req->r_tid);
  601. err = __send_request(osdc, req);
  602. if (err)
  603. dout("osdc failed again on %lld\n", req->r_tid);
  604. else
  605. req->r_resend = false;
  606. continue;
  607. }
  608. }
  609. /*
  610. * reset osds that appear to be _really_ unresponsive. this
  611. * is a failsafe measure.. we really shouldn't be getting to
  612. * this point if the system is working properly. the monitors
  613. * should mark the osd as failed and we should find out about
  614. * it from an updated osd map.
  615. */
  616. while (!list_empty(&osdc->req_lru)) {
  617. req = list_entry(osdc->req_lru.next, struct ceph_osd_request,
  618. r_req_lru_item);
  619. if (time_before(jiffies, req->r_stamp + timeout))
  620. break;
  621. BUG_ON(req == last_req && req->r_stamp == last_stamp);
  622. last_req = req;
  623. last_stamp = req->r_stamp;
  624. osd = req->r_osd;
  625. BUG_ON(!osd);
  626. pr_warning(" tid %llu timed out on osd%d, will reset osd\n",
  627. req->r_tid, osd->o_osd);
  628. __kick_requests(osdc, osd);
  629. }
  630. /*
  631. * ping osds that are a bit slow. this ensures that if there
  632. * is a break in the TCP connection we will notice, and reopen
  633. * a connection with that osd (from the fault callback).
  634. */
  635. INIT_LIST_HEAD(&slow_osds);
  636. list_for_each_entry(req, &osdc->req_lru, r_req_lru_item) {
  637. if (time_before(jiffies, req->r_stamp + keepalive))
  638. break;
  639. osd = req->r_osd;
  640. BUG_ON(!osd);
  641. dout(" tid %llu is slow, will send keepalive on osd%d\n",
  642. req->r_tid, osd->o_osd);
  643. list_move_tail(&osd->o_keepalive_item, &slow_osds);
  644. }
  645. while (!list_empty(&slow_osds)) {
  646. osd = list_entry(slow_osds.next, struct ceph_osd,
  647. o_keepalive_item);
  648. list_del_init(&osd->o_keepalive_item);
  649. ceph_con_keepalive(&osd->o_con);
  650. }
  651. __schedule_osd_timeout(osdc);
  652. mutex_unlock(&osdc->request_mutex);
  653. up_read(&osdc->map_sem);
  654. }
  655. static void handle_osds_timeout(struct work_struct *work)
  656. {
  657. struct ceph_osd_client *osdc =
  658. container_of(work, struct ceph_osd_client,
  659. osds_timeout_work.work);
  660. unsigned long delay =
  661. osdc->client->mount_args->osd_idle_ttl * HZ >> 2;
  662. dout("osds timeout\n");
  663. down_read(&osdc->map_sem);
  664. remove_old_osds(osdc, 0);
  665. up_read(&osdc->map_sem);
  666. schedule_delayed_work(&osdc->osds_timeout_work,
  667. round_jiffies_relative(delay));
  668. }
  669. /*
  670. * handle osd op reply. either call the callback if it is specified,
  671. * or do the completion to wake up the waiting thread.
  672. */
  673. static void handle_reply(struct ceph_osd_client *osdc, struct ceph_msg *msg,
  674. struct ceph_connection *con)
  675. {
  676. struct ceph_osd_reply_head *rhead = msg->front.iov_base;
  677. struct ceph_osd_request *req;
  678. u64 tid;
  679. int numops, object_len, flags;
  680. tid = le64_to_cpu(msg->hdr.tid);
  681. if (msg->front.iov_len < sizeof(*rhead))
  682. goto bad;
  683. numops = le32_to_cpu(rhead->num_ops);
  684. object_len = le32_to_cpu(rhead->object_len);
  685. if (msg->front.iov_len != sizeof(*rhead) + object_len +
  686. numops * sizeof(struct ceph_osd_op))
  687. goto bad;
  688. dout("handle_reply %p tid %llu\n", msg, tid);
  689. /* lookup */
  690. mutex_lock(&osdc->request_mutex);
  691. req = __lookup_request(osdc, tid);
  692. if (req == NULL) {
  693. dout("handle_reply tid %llu dne\n", tid);
  694. mutex_unlock(&osdc->request_mutex);
  695. return;
  696. }
  697. ceph_osdc_get_request(req);
  698. flags = le32_to_cpu(rhead->flags);
  699. /*
  700. * if this connection filled our message, drop our reference now, to
  701. * avoid a (safe but slower) revoke later.
  702. */
  703. if (req->r_con_filling_msg == con && req->r_reply == msg) {
  704. dout(" dropping con_filling_msg ref %p\n", con);
  705. req->r_con_filling_msg = NULL;
  706. ceph_con_put(con);
  707. }
  708. if (!req->r_got_reply) {
  709. unsigned bytes;
  710. req->r_result = le32_to_cpu(rhead->result);
  711. bytes = le32_to_cpu(msg->hdr.data_len);
  712. dout("handle_reply result %d bytes %d\n", req->r_result,
  713. bytes);
  714. if (req->r_result == 0)
  715. req->r_result = bytes;
  716. /* in case this is a write and we need to replay, */
  717. req->r_reassert_version = rhead->reassert_version;
  718. req->r_got_reply = 1;
  719. } else if ((flags & CEPH_OSD_FLAG_ONDISK) == 0) {
  720. dout("handle_reply tid %llu dup ack\n", tid);
  721. mutex_unlock(&osdc->request_mutex);
  722. goto done;
  723. }
  724. dout("handle_reply tid %llu flags %d\n", tid, flags);
  725. /* either this is a read, or we got the safe response */
  726. if ((flags & CEPH_OSD_FLAG_ONDISK) ||
  727. ((flags & CEPH_OSD_FLAG_WRITE) == 0))
  728. __unregister_request(osdc, req);
  729. mutex_unlock(&osdc->request_mutex);
  730. if (req->r_callback)
  731. req->r_callback(req, msg);
  732. else
  733. complete(&req->r_completion);
  734. if (flags & CEPH_OSD_FLAG_ONDISK) {
  735. if (req->r_safe_callback)
  736. req->r_safe_callback(req, msg);
  737. complete(&req->r_safe_completion); /* fsync waiter */
  738. }
  739. done:
  740. ceph_osdc_put_request(req);
  741. return;
  742. bad:
  743. pr_err("corrupt osd_op_reply got %d %d expected %d\n",
  744. (int)msg->front.iov_len, le32_to_cpu(msg->hdr.front_len),
  745. (int)sizeof(*rhead));
  746. ceph_msg_dump(msg);
  747. }
  748. static int __kick_requests(struct ceph_osd_client *osdc,
  749. struct ceph_osd *kickosd)
  750. {
  751. struct ceph_osd_request *req;
  752. struct rb_node *p, *n;
  753. int needmap = 0;
  754. int err;
  755. dout("kick_requests osd%d\n", kickosd ? kickosd->o_osd : -1);
  756. if (kickosd) {
  757. err = __reset_osd(osdc, kickosd);
  758. if (err == -EAGAIN)
  759. return 1;
  760. } else {
  761. for (p = rb_first(&osdc->osds); p; p = n) {
  762. struct ceph_osd *osd =
  763. rb_entry(p, struct ceph_osd, o_node);
  764. n = rb_next(p);
  765. if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) ||
  766. memcmp(&osd->o_con.peer_addr,
  767. ceph_osd_addr(osdc->osdmap,
  768. osd->o_osd),
  769. sizeof(struct ceph_entity_addr)) != 0)
  770. __reset_osd(osdc, osd);
  771. }
  772. }
  773. for (p = rb_first(&osdc->requests); p; p = rb_next(p)) {
  774. req = rb_entry(p, struct ceph_osd_request, r_node);
  775. if (req->r_resend) {
  776. dout(" r_resend set on tid %llu\n", req->r_tid);
  777. __cancel_request(req);
  778. goto kick;
  779. }
  780. if (req->r_osd && kickosd == req->r_osd) {
  781. __cancel_request(req);
  782. goto kick;
  783. }
  784. err = __map_osds(osdc, req);
  785. if (err == 0)
  786. continue; /* no change */
  787. if (err < 0) {
  788. /*
  789. * FIXME: really, we should set the request
  790. * error and fail if this isn't a 'nofail'
  791. * request, but that's a fair bit more
  792. * complicated to do. So retry!
  793. */
  794. dout(" setting r_resend on %llu\n", req->r_tid);
  795. req->r_resend = true;
  796. continue;
  797. }
  798. if (req->r_osd == NULL) {
  799. dout("tid %llu maps to no valid osd\n", req->r_tid);
  800. needmap++; /* request a newer map */
  801. continue;
  802. }
  803. kick:
  804. dout("kicking %p tid %llu osd%d\n", req, req->r_tid,
  805. req->r_osd ? req->r_osd->o_osd : -1);
  806. req->r_flags |= CEPH_OSD_FLAG_RETRY;
  807. err = __send_request(osdc, req);
  808. if (err) {
  809. dout(" setting r_resend on %llu\n", req->r_tid);
  810. req->r_resend = true;
  811. }
  812. }
  813. return needmap;
  814. }
  815. /*
  816. * Resubmit osd requests whose osd or osd address has changed. Request
  817. * a new osd map if osds are down, or we are otherwise unable to determine
  818. * how to direct a request.
  819. *
  820. * Close connections to down osds.
  821. *
  822. * If @who is specified, resubmit requests for that specific osd.
  823. *
  824. * Caller should hold map_sem for read and request_mutex.
  825. */
  826. static void kick_requests(struct ceph_osd_client *osdc,
  827. struct ceph_osd *kickosd)
  828. {
  829. int needmap;
  830. mutex_lock(&osdc->request_mutex);
  831. needmap = __kick_requests(osdc, kickosd);
  832. mutex_unlock(&osdc->request_mutex);
  833. if (needmap) {
  834. dout("%d requests for down osds, need new map\n", needmap);
  835. ceph_monc_request_next_osdmap(&osdc->client->monc);
  836. }
  837. }
  838. /*
  839. * Process updated osd map.
  840. *
  841. * The message contains any number of incremental and full maps, normally
  842. * indicating some sort of topology change in the cluster. Kick requests
  843. * off to different OSDs as needed.
  844. */
  845. void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg)
  846. {
  847. void *p, *end, *next;
  848. u32 nr_maps, maplen;
  849. u32 epoch;
  850. struct ceph_osdmap *newmap = NULL, *oldmap;
  851. int err;
  852. struct ceph_fsid fsid;
  853. dout("handle_map have %u\n", osdc->osdmap ? osdc->osdmap->epoch : 0);
  854. p = msg->front.iov_base;
  855. end = p + msg->front.iov_len;
  856. /* verify fsid */
  857. ceph_decode_need(&p, end, sizeof(fsid), bad);
  858. ceph_decode_copy(&p, &fsid, sizeof(fsid));
  859. if (ceph_check_fsid(osdc->client, &fsid) < 0)
  860. return;
  861. down_write(&osdc->map_sem);
  862. /* incremental maps */
  863. ceph_decode_32_safe(&p, end, nr_maps, bad);
  864. dout(" %d inc maps\n", nr_maps);
  865. while (nr_maps > 0) {
  866. ceph_decode_need(&p, end, 2*sizeof(u32), bad);
  867. epoch = ceph_decode_32(&p);
  868. maplen = ceph_decode_32(&p);
  869. ceph_decode_need(&p, end, maplen, bad);
  870. next = p + maplen;
  871. if (osdc->osdmap && osdc->osdmap->epoch+1 == epoch) {
  872. dout("applying incremental map %u len %d\n",
  873. epoch, maplen);
  874. newmap = osdmap_apply_incremental(&p, next,
  875. osdc->osdmap,
  876. osdc->client->msgr);
  877. if (IS_ERR(newmap)) {
  878. err = PTR_ERR(newmap);
  879. goto bad;
  880. }
  881. BUG_ON(!newmap);
  882. if (newmap != osdc->osdmap) {
  883. ceph_osdmap_destroy(osdc->osdmap);
  884. osdc->osdmap = newmap;
  885. }
  886. } else {
  887. dout("ignoring incremental map %u len %d\n",
  888. epoch, maplen);
  889. }
  890. p = next;
  891. nr_maps--;
  892. }
  893. if (newmap)
  894. goto done;
  895. /* full maps */
  896. ceph_decode_32_safe(&p, end, nr_maps, bad);
  897. dout(" %d full maps\n", nr_maps);
  898. while (nr_maps) {
  899. ceph_decode_need(&p, end, 2*sizeof(u32), bad);
  900. epoch = ceph_decode_32(&p);
  901. maplen = ceph_decode_32(&p);
  902. ceph_decode_need(&p, end, maplen, bad);
  903. if (nr_maps > 1) {
  904. dout("skipping non-latest full map %u len %d\n",
  905. epoch, maplen);
  906. } else if (osdc->osdmap && osdc->osdmap->epoch >= epoch) {
  907. dout("skipping full map %u len %d, "
  908. "older than our %u\n", epoch, maplen,
  909. osdc->osdmap->epoch);
  910. } else {
  911. dout("taking full map %u len %d\n", epoch, maplen);
  912. newmap = osdmap_decode(&p, p+maplen);
  913. if (IS_ERR(newmap)) {
  914. err = PTR_ERR(newmap);
  915. goto bad;
  916. }
  917. BUG_ON(!newmap);
  918. oldmap = osdc->osdmap;
  919. osdc->osdmap = newmap;
  920. if (oldmap)
  921. ceph_osdmap_destroy(oldmap);
  922. }
  923. p += maplen;
  924. nr_maps--;
  925. }
  926. done:
  927. downgrade_write(&osdc->map_sem);
  928. ceph_monc_got_osdmap(&osdc->client->monc, osdc->osdmap->epoch);
  929. if (newmap)
  930. kick_requests(osdc, NULL);
  931. up_read(&osdc->map_sem);
  932. return;
  933. bad:
  934. pr_err("osdc handle_map corrupt msg\n");
  935. ceph_msg_dump(msg);
  936. up_write(&osdc->map_sem);
  937. return;
  938. }
  939. /*
  940. * A read request prepares specific pages that data is to be read into.
  941. * When a message is being read off the wire, we call prepare_pages to
  942. * find those pages.
  943. * 0 = success, -1 failure.
  944. */
  945. static int __prepare_pages(struct ceph_connection *con,
  946. struct ceph_msg_header *hdr,
  947. struct ceph_osd_request *req,
  948. u64 tid,
  949. struct ceph_msg *m)
  950. {
  951. struct ceph_osd *osd = con->private;
  952. struct ceph_osd_client *osdc;
  953. int ret = -1;
  954. int data_len = le32_to_cpu(hdr->data_len);
  955. unsigned data_off = le16_to_cpu(hdr->data_off);
  956. int want = calc_pages_for(data_off & ~PAGE_MASK, data_len);
  957. if (!osd)
  958. return -1;
  959. osdc = osd->o_osdc;
  960. dout("__prepare_pages on msg %p tid %llu, has %d pages, want %d\n", m,
  961. tid, req->r_num_pages, want);
  962. if (unlikely(req->r_num_pages < want))
  963. goto out;
  964. m->pages = req->r_pages;
  965. m->nr_pages = req->r_num_pages;
  966. ret = 0; /* success */
  967. out:
  968. BUG_ON(ret < 0 || m->nr_pages < want);
  969. return ret;
  970. }
  971. /*
  972. * Register request, send initial attempt.
  973. */
  974. int ceph_osdc_start_request(struct ceph_osd_client *osdc,
  975. struct ceph_osd_request *req,
  976. bool nofail)
  977. {
  978. int rc = 0;
  979. req->r_request->pages = req->r_pages;
  980. req->r_request->nr_pages = req->r_num_pages;
  981. register_request(osdc, req);
  982. down_read(&osdc->map_sem);
  983. mutex_lock(&osdc->request_mutex);
  984. /*
  985. * a racing kick_requests() may have sent the message for us
  986. * while we dropped request_mutex above, so only send now if
  987. * the request still han't been touched yet.
  988. */
  989. if (req->r_sent == 0) {
  990. rc = __send_request(osdc, req);
  991. if (rc) {
  992. if (nofail) {
  993. dout("osdc_start_request failed send, "
  994. " marking %lld\n", req->r_tid);
  995. req->r_resend = true;
  996. rc = 0;
  997. } else {
  998. __unregister_request(osdc, req);
  999. }
  1000. }
  1001. }
  1002. mutex_unlock(&osdc->request_mutex);
  1003. up_read(&osdc->map_sem);
  1004. return rc;
  1005. }
  1006. /*
  1007. * wait for a request to complete
  1008. */
  1009. int ceph_osdc_wait_request(struct ceph_osd_client *osdc,
  1010. struct ceph_osd_request *req)
  1011. {
  1012. int rc;
  1013. rc = wait_for_completion_interruptible(&req->r_completion);
  1014. if (rc < 0) {
  1015. mutex_lock(&osdc->request_mutex);
  1016. __cancel_request(req);
  1017. __unregister_request(osdc, req);
  1018. mutex_unlock(&osdc->request_mutex);
  1019. dout("wait_request tid %llu canceled/timed out\n", req->r_tid);
  1020. return rc;
  1021. }
  1022. dout("wait_request tid %llu result %d\n", req->r_tid, req->r_result);
  1023. return req->r_result;
  1024. }
  1025. /*
  1026. * sync - wait for all in-flight requests to flush. avoid starvation.
  1027. */
  1028. void ceph_osdc_sync(struct ceph_osd_client *osdc)
  1029. {
  1030. struct ceph_osd_request *req;
  1031. u64 last_tid, next_tid = 0;
  1032. mutex_lock(&osdc->request_mutex);
  1033. last_tid = osdc->last_tid;
  1034. while (1) {
  1035. req = __lookup_request_ge(osdc, next_tid);
  1036. if (!req)
  1037. break;
  1038. if (req->r_tid > last_tid)
  1039. break;
  1040. next_tid = req->r_tid + 1;
  1041. if ((req->r_flags & CEPH_OSD_FLAG_WRITE) == 0)
  1042. continue;
  1043. ceph_osdc_get_request(req);
  1044. mutex_unlock(&osdc->request_mutex);
  1045. dout("sync waiting on tid %llu (last is %llu)\n",
  1046. req->r_tid, last_tid);
  1047. wait_for_completion(&req->r_safe_completion);
  1048. mutex_lock(&osdc->request_mutex);
  1049. ceph_osdc_put_request(req);
  1050. }
  1051. mutex_unlock(&osdc->request_mutex);
  1052. dout("sync done (thru tid %llu)\n", last_tid);
  1053. }
  1054. /*
  1055. * init, shutdown
  1056. */
  1057. int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client)
  1058. {
  1059. int err;
  1060. dout("init\n");
  1061. osdc->client = client;
  1062. osdc->osdmap = NULL;
  1063. init_rwsem(&osdc->map_sem);
  1064. init_completion(&osdc->map_waiters);
  1065. osdc->last_requested_map = 0;
  1066. mutex_init(&osdc->request_mutex);
  1067. osdc->last_tid = 0;
  1068. osdc->osds = RB_ROOT;
  1069. INIT_LIST_HEAD(&osdc->osd_lru);
  1070. osdc->requests = RB_ROOT;
  1071. INIT_LIST_HEAD(&osdc->req_lru);
  1072. osdc->num_requests = 0;
  1073. INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout);
  1074. INIT_DELAYED_WORK(&osdc->osds_timeout_work, handle_osds_timeout);
  1075. schedule_delayed_work(&osdc->osds_timeout_work,
  1076. round_jiffies_relative(osdc->client->mount_args->osd_idle_ttl * HZ));
  1077. err = -ENOMEM;
  1078. osdc->req_mempool = mempool_create_kmalloc_pool(10,
  1079. sizeof(struct ceph_osd_request));
  1080. if (!osdc->req_mempool)
  1081. goto out;
  1082. err = ceph_msgpool_init(&osdc->msgpool_op, OSD_OP_FRONT_LEN, 10, true);
  1083. if (err < 0)
  1084. goto out_mempool;
  1085. err = ceph_msgpool_init(&osdc->msgpool_op_reply,
  1086. OSD_OPREPLY_FRONT_LEN, 10, true);
  1087. if (err < 0)
  1088. goto out_msgpool;
  1089. return 0;
  1090. out_msgpool:
  1091. ceph_msgpool_destroy(&osdc->msgpool_op);
  1092. out_mempool:
  1093. mempool_destroy(osdc->req_mempool);
  1094. out:
  1095. return err;
  1096. }
  1097. void ceph_osdc_stop(struct ceph_osd_client *osdc)
  1098. {
  1099. cancel_delayed_work_sync(&osdc->timeout_work);
  1100. cancel_delayed_work_sync(&osdc->osds_timeout_work);
  1101. if (osdc->osdmap) {
  1102. ceph_osdmap_destroy(osdc->osdmap);
  1103. osdc->osdmap = NULL;
  1104. }
  1105. remove_old_osds(osdc, 1);
  1106. mempool_destroy(osdc->req_mempool);
  1107. ceph_msgpool_destroy(&osdc->msgpool_op);
  1108. ceph_msgpool_destroy(&osdc->msgpool_op_reply);
  1109. }
  1110. /*
  1111. * Read some contiguous pages. If we cross a stripe boundary, shorten
  1112. * *plen. Return number of bytes read, or error.
  1113. */
  1114. int ceph_osdc_readpages(struct ceph_osd_client *osdc,
  1115. struct ceph_vino vino, struct ceph_file_layout *layout,
  1116. u64 off, u64 *plen,
  1117. u32 truncate_seq, u64 truncate_size,
  1118. struct page **pages, int num_pages)
  1119. {
  1120. struct ceph_osd_request *req;
  1121. int rc = 0;
  1122. dout("readpages on ino %llx.%llx on %llu~%llu\n", vino.ino,
  1123. vino.snap, off, *plen);
  1124. req = ceph_osdc_new_request(osdc, layout, vino, off, plen,
  1125. CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ,
  1126. NULL, 0, truncate_seq, truncate_size, NULL,
  1127. false, 1);
  1128. if (IS_ERR(req))
  1129. return PTR_ERR(req);
  1130. /* it may be a short read due to an object boundary */
  1131. req->r_pages = pages;
  1132. num_pages = calc_pages_for(off, *plen);
  1133. req->r_num_pages = num_pages;
  1134. dout("readpages final extent is %llu~%llu (%d pages)\n",
  1135. off, *plen, req->r_num_pages);
  1136. rc = ceph_osdc_start_request(osdc, req, false);
  1137. if (!rc)
  1138. rc = ceph_osdc_wait_request(osdc, req);
  1139. ceph_osdc_put_request(req);
  1140. dout("readpages result %d\n", rc);
  1141. return rc;
  1142. }
  1143. /*
  1144. * do a synchronous write on N pages
  1145. */
  1146. int ceph_osdc_writepages(struct ceph_osd_client *osdc, struct ceph_vino vino,
  1147. struct ceph_file_layout *layout,
  1148. struct ceph_snap_context *snapc,
  1149. u64 off, u64 len,
  1150. u32 truncate_seq, u64 truncate_size,
  1151. struct timespec *mtime,
  1152. struct page **pages, int num_pages,
  1153. int flags, int do_sync, bool nofail)
  1154. {
  1155. struct ceph_osd_request *req;
  1156. int rc = 0;
  1157. BUG_ON(vino.snap != CEPH_NOSNAP);
  1158. req = ceph_osdc_new_request(osdc, layout, vino, off, &len,
  1159. CEPH_OSD_OP_WRITE,
  1160. flags | CEPH_OSD_FLAG_ONDISK |
  1161. CEPH_OSD_FLAG_WRITE,
  1162. snapc, do_sync,
  1163. truncate_seq, truncate_size, mtime,
  1164. nofail, 1);
  1165. if (IS_ERR(req))
  1166. return PTR_ERR(req);
  1167. /* it may be a short write due to an object boundary */
  1168. req->r_pages = pages;
  1169. req->r_num_pages = calc_pages_for(off, len);
  1170. dout("writepages %llu~%llu (%d pages)\n", off, len,
  1171. req->r_num_pages);
  1172. rc = ceph_osdc_start_request(osdc, req, nofail);
  1173. if (!rc)
  1174. rc = ceph_osdc_wait_request(osdc, req);
  1175. ceph_osdc_put_request(req);
  1176. if (rc == 0)
  1177. rc = len;
  1178. dout("writepages result %d\n", rc);
  1179. return rc;
  1180. }
  1181. /*
  1182. * handle incoming message
  1183. */
  1184. static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
  1185. {
  1186. struct ceph_osd *osd = con->private;
  1187. struct ceph_osd_client *osdc;
  1188. int type = le16_to_cpu(msg->hdr.type);
  1189. if (!osd)
  1190. return;
  1191. osdc = osd->o_osdc;
  1192. switch (type) {
  1193. case CEPH_MSG_OSD_MAP:
  1194. ceph_osdc_handle_map(osdc, msg);
  1195. break;
  1196. case CEPH_MSG_OSD_OPREPLY:
  1197. handle_reply(osdc, msg, con);
  1198. break;
  1199. default:
  1200. pr_err("received unknown message type %d %s\n", type,
  1201. ceph_msg_type_name(type));
  1202. }
  1203. ceph_msg_put(msg);
  1204. }
  1205. /*
  1206. * lookup and return message for incoming reply
  1207. */
  1208. static struct ceph_msg *get_reply(struct ceph_connection *con,
  1209. struct ceph_msg_header *hdr,
  1210. int *skip)
  1211. {
  1212. struct ceph_osd *osd = con->private;
  1213. struct ceph_osd_client *osdc = osd->o_osdc;
  1214. struct ceph_msg *m;
  1215. struct ceph_osd_request *req;
  1216. int front = le32_to_cpu(hdr->front_len);
  1217. int data_len = le32_to_cpu(hdr->data_len);
  1218. u64 tid;
  1219. int err;
  1220. tid = le64_to_cpu(hdr->tid);
  1221. mutex_lock(&osdc->request_mutex);
  1222. req = __lookup_request(osdc, tid);
  1223. if (!req) {
  1224. *skip = 1;
  1225. m = NULL;
  1226. pr_info("get_reply unknown tid %llu from osd%d\n", tid,
  1227. osd->o_osd);
  1228. goto out;
  1229. }
  1230. if (req->r_con_filling_msg) {
  1231. dout("get_reply revoking msg %p from old con %p\n",
  1232. req->r_reply, req->r_con_filling_msg);
  1233. ceph_con_revoke_message(req->r_con_filling_msg, req->r_reply);
  1234. ceph_con_put(req->r_con_filling_msg);
  1235. }
  1236. if (front > req->r_reply->front.iov_len) {
  1237. pr_warning("get_reply front %d > preallocated %d\n",
  1238. front, (int)req->r_reply->front.iov_len);
  1239. m = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, front, 0, 0, NULL);
  1240. if (IS_ERR(m))
  1241. goto out;
  1242. ceph_msg_put(req->r_reply);
  1243. req->r_reply = m;
  1244. }
  1245. m = ceph_msg_get(req->r_reply);
  1246. if (data_len > 0) {
  1247. err = __prepare_pages(con, hdr, req, tid, m);
  1248. if (err < 0) {
  1249. *skip = 1;
  1250. ceph_msg_put(m);
  1251. m = ERR_PTR(err);
  1252. }
  1253. }
  1254. *skip = 0;
  1255. req->r_con_filling_msg = ceph_con_get(con);
  1256. dout("get_reply tid %lld %p\n", tid, m);
  1257. out:
  1258. mutex_unlock(&osdc->request_mutex);
  1259. return m;
  1260. }
  1261. static struct ceph_msg *alloc_msg(struct ceph_connection *con,
  1262. struct ceph_msg_header *hdr,
  1263. int *skip)
  1264. {
  1265. struct ceph_osd *osd = con->private;
  1266. int type = le16_to_cpu(hdr->type);
  1267. int front = le32_to_cpu(hdr->front_len);
  1268. switch (type) {
  1269. case CEPH_MSG_OSD_MAP:
  1270. return ceph_msg_new(type, front, 0, 0, NULL);
  1271. case CEPH_MSG_OSD_OPREPLY:
  1272. return get_reply(con, hdr, skip);
  1273. default:
  1274. pr_info("alloc_msg unexpected msg type %d from osd%d\n", type,
  1275. osd->o_osd);
  1276. *skip = 1;
  1277. return NULL;
  1278. }
  1279. }
  1280. /*
  1281. * Wrappers to refcount containing ceph_osd struct
  1282. */
  1283. static struct ceph_connection *get_osd_con(struct ceph_connection *con)
  1284. {
  1285. struct ceph_osd *osd = con->private;
  1286. if (get_osd(osd))
  1287. return con;
  1288. return NULL;
  1289. }
  1290. static void put_osd_con(struct ceph_connection *con)
  1291. {
  1292. struct ceph_osd *osd = con->private;
  1293. put_osd(osd);
  1294. }
  1295. /*
  1296. * authentication
  1297. */
  1298. static int get_authorizer(struct ceph_connection *con,
  1299. void **buf, int *len, int *proto,
  1300. void **reply_buf, int *reply_len, int force_new)
  1301. {
  1302. struct ceph_osd *o = con->private;
  1303. struct ceph_osd_client *osdc = o->o_osdc;
  1304. struct ceph_auth_client *ac = osdc->client->monc.auth;
  1305. int ret = 0;
  1306. if (force_new && o->o_authorizer) {
  1307. ac->ops->destroy_authorizer(ac, o->o_authorizer);
  1308. o->o_authorizer = NULL;
  1309. }
  1310. if (o->o_authorizer == NULL) {
  1311. ret = ac->ops->create_authorizer(
  1312. ac, CEPH_ENTITY_TYPE_OSD,
  1313. &o->o_authorizer,
  1314. &o->o_authorizer_buf,
  1315. &o->o_authorizer_buf_len,
  1316. &o->o_authorizer_reply_buf,
  1317. &o->o_authorizer_reply_buf_len);
  1318. if (ret)
  1319. return ret;
  1320. }
  1321. *proto = ac->protocol;
  1322. *buf = o->o_authorizer_buf;
  1323. *len = o->o_authorizer_buf_len;
  1324. *reply_buf = o->o_authorizer_reply_buf;
  1325. *reply_len = o->o_authorizer_reply_buf_len;
  1326. return 0;
  1327. }
  1328. static int verify_authorizer_reply(struct ceph_connection *con, int len)
  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. return ac->ops->verify_authorizer_reply(ac, o->o_authorizer, len);
  1334. }
  1335. static int invalidate_authorizer(struct ceph_connection *con)
  1336. {
  1337. struct ceph_osd *o = con->private;
  1338. struct ceph_osd_client *osdc = o->o_osdc;
  1339. struct ceph_auth_client *ac = osdc->client->monc.auth;
  1340. if (ac->ops->invalidate_authorizer)
  1341. ac->ops->invalidate_authorizer(ac, CEPH_ENTITY_TYPE_OSD);
  1342. return ceph_monc_validate_auth(&osdc->client->monc);
  1343. }
  1344. const static struct ceph_connection_operations osd_con_ops = {
  1345. .get = get_osd_con,
  1346. .put = put_osd_con,
  1347. .dispatch = dispatch,
  1348. .get_authorizer = get_authorizer,
  1349. .verify_authorizer_reply = verify_authorizer_reply,
  1350. .invalidate_authorizer = invalidate_authorizer,
  1351. .alloc_msg = alloc_msg,
  1352. .fault = osd_reset,
  1353. };