osd_client.c 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190
  1. #include <linux/ceph/ceph_debug.h>
  2. #include <linux/module.h>
  3. #include <linux/err.h>
  4. #include <linux/highmem.h>
  5. #include <linux/mm.h>
  6. #include <linux/pagemap.h>
  7. #include <linux/slab.h>
  8. #include <linux/uaccess.h>
  9. #ifdef CONFIG_BLOCK
  10. #include <linux/bio.h>
  11. #endif
  12. #include <linux/ceph/libceph.h>
  13. #include <linux/ceph/osd_client.h>
  14. #include <linux/ceph/messenger.h>
  15. #include <linux/ceph/decode.h>
  16. #include <linux/ceph/auth.h>
  17. #include <linux/ceph/pagelist.h>
  18. #define OSD_OP_FRONT_LEN 4096
  19. #define OSD_OPREPLY_FRONT_LEN 512
  20. static const struct ceph_connection_operations osd_con_ops;
  21. static void send_queued(struct ceph_osd_client *osdc);
  22. static int __reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd);
  23. static void __register_request(struct ceph_osd_client *osdc,
  24. struct ceph_osd_request *req);
  25. static void __unregister_linger_request(struct ceph_osd_client *osdc,
  26. struct ceph_osd_request *req);
  27. static void __send_request(struct ceph_osd_client *osdc,
  28. struct ceph_osd_request *req);
  29. static int op_needs_trail(int op)
  30. {
  31. switch (op) {
  32. case CEPH_OSD_OP_GETXATTR:
  33. case CEPH_OSD_OP_SETXATTR:
  34. case CEPH_OSD_OP_CMPXATTR:
  35. case CEPH_OSD_OP_CALL:
  36. case CEPH_OSD_OP_NOTIFY:
  37. return 1;
  38. default:
  39. return 0;
  40. }
  41. }
  42. static int op_has_extent(int op)
  43. {
  44. return (op == CEPH_OSD_OP_READ ||
  45. op == CEPH_OSD_OP_WRITE);
  46. }
  47. int ceph_calc_raw_layout(struct ceph_osd_client *osdc,
  48. struct ceph_file_layout *layout,
  49. u64 snapid,
  50. u64 off, u64 *plen, u64 *bno,
  51. struct ceph_osd_request *req,
  52. struct ceph_osd_req_op *op)
  53. {
  54. struct ceph_osd_request_head *reqhead = req->r_request->front.iov_base;
  55. u64 orig_len = *plen;
  56. u64 objoff, objlen; /* extent in object */
  57. int r;
  58. reqhead->snapid = cpu_to_le64(snapid);
  59. /* object extent? */
  60. r = ceph_calc_file_object_mapping(layout, off, plen, bno,
  61. &objoff, &objlen);
  62. if (r < 0)
  63. return r;
  64. if (*plen < orig_len)
  65. dout(" skipping last %llu, final file extent %llu~%llu\n",
  66. orig_len - *plen, off, *plen);
  67. if (op_has_extent(op->op)) {
  68. u32 osize = le32_to_cpu(layout->fl_object_size);
  69. op->extent.offset = objoff;
  70. op->extent.length = objlen;
  71. if (op->extent.truncate_size <= off - objoff) {
  72. op->extent.truncate_size = 0;
  73. } else {
  74. op->extent.truncate_size -= off - objoff;
  75. if (op->extent.truncate_size > osize)
  76. op->extent.truncate_size = osize;
  77. }
  78. }
  79. req->r_num_pages = calc_pages_for(off, *plen);
  80. req->r_page_alignment = off & ~PAGE_MASK;
  81. if (op->op == CEPH_OSD_OP_WRITE)
  82. op->payload_len = *plen;
  83. dout("calc_layout bno=%llx %llu~%llu (%d pages)\n",
  84. *bno, objoff, objlen, req->r_num_pages);
  85. return 0;
  86. }
  87. EXPORT_SYMBOL(ceph_calc_raw_layout);
  88. /*
  89. * Implement client access to distributed object storage cluster.
  90. *
  91. * All data objects are stored within a cluster/cloud of OSDs, or
  92. * "object storage devices." (Note that Ceph OSDs have _nothing_ to
  93. * do with the T10 OSD extensions to SCSI.) Ceph OSDs are simply
  94. * remote daemons serving up and coordinating consistent and safe
  95. * access to storage.
  96. *
  97. * Cluster membership and the mapping of data objects onto storage devices
  98. * are described by the osd map.
  99. *
  100. * We keep track of pending OSD requests (read, write), resubmit
  101. * requests to different OSDs when the cluster topology/data layout
  102. * change, or retry the affected requests when the communications
  103. * channel with an OSD is reset.
  104. */
  105. /*
  106. * calculate the mapping of a file extent onto an object, and fill out the
  107. * request accordingly. shorten extent as necessary if it crosses an
  108. * object boundary.
  109. *
  110. * fill osd op in request message.
  111. */
  112. static int calc_layout(struct ceph_osd_client *osdc,
  113. struct ceph_vino vino,
  114. struct ceph_file_layout *layout,
  115. u64 off, u64 *plen,
  116. struct ceph_osd_request *req,
  117. struct ceph_osd_req_op *op)
  118. {
  119. u64 bno;
  120. int r;
  121. r = ceph_calc_raw_layout(osdc, layout, vino.snap, off,
  122. plen, &bno, req, op);
  123. if (r < 0)
  124. return r;
  125. snprintf(req->r_oid, sizeof(req->r_oid), "%llx.%08llx", vino.ino, bno);
  126. req->r_oid_len = strlen(req->r_oid);
  127. return r;
  128. }
  129. /*
  130. * requests
  131. */
  132. void ceph_osdc_release_request(struct kref *kref)
  133. {
  134. struct ceph_osd_request *req = container_of(kref,
  135. struct ceph_osd_request,
  136. r_kref);
  137. if (req->r_request)
  138. ceph_msg_put(req->r_request);
  139. if (req->r_con_filling_msg) {
  140. dout("%s revoking pages %p from con %p\n", __func__,
  141. req->r_pages, req->r_con_filling_msg);
  142. ceph_msg_revoke_incoming(req->r_reply);
  143. req->r_con_filling_msg->ops->put(req->r_con_filling_msg);
  144. }
  145. if (req->r_reply)
  146. ceph_msg_put(req->r_reply);
  147. if (req->r_own_pages)
  148. ceph_release_page_vector(req->r_pages,
  149. req->r_num_pages);
  150. #ifdef CONFIG_BLOCK
  151. if (req->r_bio)
  152. bio_put(req->r_bio);
  153. #endif
  154. ceph_put_snap_context(req->r_snapc);
  155. if (req->r_trail) {
  156. ceph_pagelist_release(req->r_trail);
  157. kfree(req->r_trail);
  158. }
  159. if (req->r_mempool)
  160. mempool_free(req, req->r_osdc->req_mempool);
  161. else
  162. kfree(req);
  163. }
  164. EXPORT_SYMBOL(ceph_osdc_release_request);
  165. static int get_num_ops(struct ceph_osd_req_op *ops, int *needs_trail)
  166. {
  167. int i = 0;
  168. if (needs_trail)
  169. *needs_trail = 0;
  170. while (ops[i].op) {
  171. if (needs_trail && op_needs_trail(ops[i].op))
  172. *needs_trail = 1;
  173. i++;
  174. }
  175. return i;
  176. }
  177. struct ceph_osd_request *ceph_osdc_alloc_request(struct ceph_osd_client *osdc,
  178. int flags,
  179. struct ceph_snap_context *snapc,
  180. struct ceph_osd_req_op *ops,
  181. bool use_mempool,
  182. gfp_t gfp_flags,
  183. struct page **pages,
  184. struct bio *bio)
  185. {
  186. struct ceph_osd_request *req;
  187. struct ceph_msg *msg;
  188. int needs_trail;
  189. int num_op = get_num_ops(ops, &needs_trail);
  190. size_t msg_size = sizeof(struct ceph_osd_request_head);
  191. msg_size += num_op*sizeof(struct ceph_osd_op);
  192. if (use_mempool) {
  193. req = mempool_alloc(osdc->req_mempool, gfp_flags);
  194. memset(req, 0, sizeof(*req));
  195. } else {
  196. req = kzalloc(sizeof(*req), gfp_flags);
  197. }
  198. if (req == NULL)
  199. return NULL;
  200. req->r_osdc = osdc;
  201. req->r_mempool = use_mempool;
  202. kref_init(&req->r_kref);
  203. init_completion(&req->r_completion);
  204. init_completion(&req->r_safe_completion);
  205. RB_CLEAR_NODE(&req->r_node);
  206. INIT_LIST_HEAD(&req->r_unsafe_item);
  207. INIT_LIST_HEAD(&req->r_linger_item);
  208. INIT_LIST_HEAD(&req->r_linger_osd);
  209. INIT_LIST_HEAD(&req->r_req_lru_item);
  210. INIT_LIST_HEAD(&req->r_osd_item);
  211. req->r_flags = flags;
  212. WARN_ON((flags & (CEPH_OSD_FLAG_READ|CEPH_OSD_FLAG_WRITE)) == 0);
  213. /* create reply message */
  214. if (use_mempool)
  215. msg = ceph_msgpool_get(&osdc->msgpool_op_reply, 0);
  216. else
  217. msg = ceph_msg_new(CEPH_MSG_OSD_OPREPLY,
  218. OSD_OPREPLY_FRONT_LEN, gfp_flags, true);
  219. if (!msg) {
  220. ceph_osdc_put_request(req);
  221. return NULL;
  222. }
  223. req->r_reply = msg;
  224. /* allocate space for the trailing data */
  225. if (needs_trail) {
  226. req->r_trail = kmalloc(sizeof(struct ceph_pagelist), gfp_flags);
  227. if (!req->r_trail) {
  228. ceph_osdc_put_request(req);
  229. return NULL;
  230. }
  231. ceph_pagelist_init(req->r_trail);
  232. }
  233. /* create request message; allow space for oid */
  234. msg_size += MAX_OBJ_NAME_SIZE;
  235. if (snapc)
  236. msg_size += sizeof(u64) * snapc->num_snaps;
  237. if (use_mempool)
  238. msg = ceph_msgpool_get(&osdc->msgpool_op, 0);
  239. else
  240. msg = ceph_msg_new(CEPH_MSG_OSD_OP, msg_size, gfp_flags, true);
  241. if (!msg) {
  242. ceph_osdc_put_request(req);
  243. return NULL;
  244. }
  245. memset(msg->front.iov_base, 0, msg->front.iov_len);
  246. req->r_request = msg;
  247. req->r_pages = pages;
  248. #ifdef CONFIG_BLOCK
  249. if (bio) {
  250. req->r_bio = bio;
  251. bio_get(req->r_bio);
  252. }
  253. #endif
  254. return req;
  255. }
  256. EXPORT_SYMBOL(ceph_osdc_alloc_request);
  257. static void osd_req_encode_op(struct ceph_osd_request *req,
  258. struct ceph_osd_op *dst,
  259. struct ceph_osd_req_op *src)
  260. {
  261. dst->op = cpu_to_le16(src->op);
  262. switch (src->op) {
  263. case CEPH_OSD_OP_READ:
  264. case CEPH_OSD_OP_WRITE:
  265. dst->extent.offset =
  266. cpu_to_le64(src->extent.offset);
  267. dst->extent.length =
  268. cpu_to_le64(src->extent.length);
  269. dst->extent.truncate_size =
  270. cpu_to_le64(src->extent.truncate_size);
  271. dst->extent.truncate_seq =
  272. cpu_to_le32(src->extent.truncate_seq);
  273. break;
  274. case CEPH_OSD_OP_GETXATTR:
  275. case CEPH_OSD_OP_SETXATTR:
  276. case CEPH_OSD_OP_CMPXATTR:
  277. BUG_ON(!req->r_trail);
  278. dst->xattr.name_len = cpu_to_le32(src->xattr.name_len);
  279. dst->xattr.value_len = cpu_to_le32(src->xattr.value_len);
  280. dst->xattr.cmp_op = src->xattr.cmp_op;
  281. dst->xattr.cmp_mode = src->xattr.cmp_mode;
  282. ceph_pagelist_append(req->r_trail, src->xattr.name,
  283. src->xattr.name_len);
  284. ceph_pagelist_append(req->r_trail, src->xattr.val,
  285. src->xattr.value_len);
  286. break;
  287. case CEPH_OSD_OP_CALL:
  288. BUG_ON(!req->r_trail);
  289. dst->cls.class_len = src->cls.class_len;
  290. dst->cls.method_len = src->cls.method_len;
  291. dst->cls.indata_len = cpu_to_le32(src->cls.indata_len);
  292. ceph_pagelist_append(req->r_trail, src->cls.class_name,
  293. src->cls.class_len);
  294. ceph_pagelist_append(req->r_trail, src->cls.method_name,
  295. src->cls.method_len);
  296. ceph_pagelist_append(req->r_trail, src->cls.indata,
  297. src->cls.indata_len);
  298. break;
  299. case CEPH_OSD_OP_ROLLBACK:
  300. dst->snap.snapid = cpu_to_le64(src->snap.snapid);
  301. break;
  302. case CEPH_OSD_OP_STARTSYNC:
  303. break;
  304. case CEPH_OSD_OP_NOTIFY:
  305. {
  306. __le32 prot_ver = cpu_to_le32(src->watch.prot_ver);
  307. __le32 timeout = cpu_to_le32(src->watch.timeout);
  308. BUG_ON(!req->r_trail);
  309. ceph_pagelist_append(req->r_trail,
  310. &prot_ver, sizeof(prot_ver));
  311. ceph_pagelist_append(req->r_trail,
  312. &timeout, sizeof(timeout));
  313. }
  314. case CEPH_OSD_OP_NOTIFY_ACK:
  315. case CEPH_OSD_OP_WATCH:
  316. dst->watch.cookie = cpu_to_le64(src->watch.cookie);
  317. dst->watch.ver = cpu_to_le64(src->watch.ver);
  318. dst->watch.flag = src->watch.flag;
  319. break;
  320. default:
  321. pr_err("unrecognized osd opcode %d\n", dst->op);
  322. WARN_ON(1);
  323. break;
  324. }
  325. dst->payload_len = cpu_to_le32(src->payload_len);
  326. }
  327. /*
  328. * build new request AND message
  329. *
  330. */
  331. void ceph_osdc_build_request(struct ceph_osd_request *req,
  332. u64 off, u64 *plen,
  333. struct ceph_osd_req_op *src_ops,
  334. struct ceph_snap_context *snapc,
  335. struct timespec *mtime,
  336. const char *oid,
  337. int oid_len)
  338. {
  339. struct ceph_msg *msg = req->r_request;
  340. struct ceph_osd_request_head *head;
  341. struct ceph_osd_req_op *src_op;
  342. struct ceph_osd_op *op;
  343. void *p;
  344. int num_op = get_num_ops(src_ops, NULL);
  345. size_t msg_size = sizeof(*head) + num_op*sizeof(*op);
  346. int flags = req->r_flags;
  347. u64 data_len = 0;
  348. int i;
  349. head = msg->front.iov_base;
  350. op = (void *)(head + 1);
  351. p = (void *)(op + num_op);
  352. req->r_snapc = ceph_get_snap_context(snapc);
  353. head->client_inc = cpu_to_le32(1); /* always, for now. */
  354. head->flags = cpu_to_le32(flags);
  355. if (flags & CEPH_OSD_FLAG_WRITE)
  356. ceph_encode_timespec(&head->mtime, mtime);
  357. head->num_ops = cpu_to_le16(num_op);
  358. /* fill in oid */
  359. head->object_len = cpu_to_le32(oid_len);
  360. memcpy(p, oid, oid_len);
  361. p += oid_len;
  362. src_op = src_ops;
  363. while (src_op->op) {
  364. osd_req_encode_op(req, op, src_op);
  365. src_op++;
  366. op++;
  367. }
  368. if (req->r_trail)
  369. data_len += req->r_trail->length;
  370. if (snapc) {
  371. head->snap_seq = cpu_to_le64(snapc->seq);
  372. head->num_snaps = cpu_to_le32(snapc->num_snaps);
  373. for (i = 0; i < snapc->num_snaps; i++) {
  374. put_unaligned_le64(snapc->snaps[i], p);
  375. p += sizeof(u64);
  376. }
  377. }
  378. if (flags & CEPH_OSD_FLAG_WRITE) {
  379. req->r_request->hdr.data_off = cpu_to_le16(off);
  380. req->r_request->hdr.data_len = cpu_to_le32(*plen + data_len);
  381. } else if (data_len) {
  382. req->r_request->hdr.data_off = 0;
  383. req->r_request->hdr.data_len = cpu_to_le32(data_len);
  384. }
  385. req->r_request->page_alignment = req->r_page_alignment;
  386. BUG_ON(p > msg->front.iov_base + msg->front.iov_len);
  387. msg_size = p - msg->front.iov_base;
  388. msg->front.iov_len = msg_size;
  389. msg->hdr.front_len = cpu_to_le32(msg_size);
  390. return;
  391. }
  392. EXPORT_SYMBOL(ceph_osdc_build_request);
  393. /*
  394. * build new request AND message, calculate layout, and adjust file
  395. * extent as needed.
  396. *
  397. * if the file was recently truncated, we include information about its
  398. * old and new size so that the object can be updated appropriately. (we
  399. * avoid synchronously deleting truncated objects because it's slow.)
  400. *
  401. * if @do_sync, include a 'startsync' command so that the osd will flush
  402. * data quickly.
  403. */
  404. struct ceph_osd_request *ceph_osdc_new_request(struct ceph_osd_client *osdc,
  405. struct ceph_file_layout *layout,
  406. struct ceph_vino vino,
  407. u64 off, u64 *plen,
  408. int opcode, int flags,
  409. struct ceph_snap_context *snapc,
  410. int do_sync,
  411. u32 truncate_seq,
  412. u64 truncate_size,
  413. struct timespec *mtime,
  414. bool use_mempool, int num_reply,
  415. int page_align)
  416. {
  417. struct ceph_osd_req_op ops[3];
  418. struct ceph_osd_request *req;
  419. int r;
  420. ops[0].op = opcode;
  421. ops[0].extent.truncate_seq = truncate_seq;
  422. ops[0].extent.truncate_size = truncate_size;
  423. ops[0].payload_len = 0;
  424. if (do_sync) {
  425. ops[1].op = CEPH_OSD_OP_STARTSYNC;
  426. ops[1].payload_len = 0;
  427. ops[2].op = 0;
  428. } else
  429. ops[1].op = 0;
  430. req = ceph_osdc_alloc_request(osdc, flags,
  431. snapc, ops,
  432. use_mempool,
  433. GFP_NOFS, NULL, NULL);
  434. if (!req)
  435. return ERR_PTR(-ENOMEM);
  436. /* calculate max write size */
  437. r = calc_layout(osdc, vino, layout, off, plen, req, ops);
  438. if (r < 0)
  439. return ERR_PTR(r);
  440. req->r_file_layout = *layout; /* keep a copy */
  441. /* in case it differs from natural (file) alignment that
  442. calc_layout filled in for us */
  443. req->r_num_pages = calc_pages_for(page_align, *plen);
  444. req->r_page_alignment = page_align;
  445. ceph_osdc_build_request(req, off, plen, ops,
  446. snapc,
  447. mtime,
  448. req->r_oid, req->r_oid_len);
  449. return req;
  450. }
  451. EXPORT_SYMBOL(ceph_osdc_new_request);
  452. /*
  453. * We keep osd requests in an rbtree, sorted by ->r_tid.
  454. */
  455. static void __insert_request(struct ceph_osd_client *osdc,
  456. struct ceph_osd_request *new)
  457. {
  458. struct rb_node **p = &osdc->requests.rb_node;
  459. struct rb_node *parent = NULL;
  460. struct ceph_osd_request *req = NULL;
  461. while (*p) {
  462. parent = *p;
  463. req = rb_entry(parent, struct ceph_osd_request, r_node);
  464. if (new->r_tid < req->r_tid)
  465. p = &(*p)->rb_left;
  466. else if (new->r_tid > req->r_tid)
  467. p = &(*p)->rb_right;
  468. else
  469. BUG();
  470. }
  471. rb_link_node(&new->r_node, parent, p);
  472. rb_insert_color(&new->r_node, &osdc->requests);
  473. }
  474. static struct ceph_osd_request *__lookup_request(struct ceph_osd_client *osdc,
  475. u64 tid)
  476. {
  477. struct ceph_osd_request *req;
  478. struct rb_node *n = osdc->requests.rb_node;
  479. while (n) {
  480. req = rb_entry(n, struct ceph_osd_request, r_node);
  481. if (tid < req->r_tid)
  482. n = n->rb_left;
  483. else if (tid > req->r_tid)
  484. n = n->rb_right;
  485. else
  486. return req;
  487. }
  488. return NULL;
  489. }
  490. static struct ceph_osd_request *
  491. __lookup_request_ge(struct ceph_osd_client *osdc,
  492. u64 tid)
  493. {
  494. struct ceph_osd_request *req;
  495. struct rb_node *n = osdc->requests.rb_node;
  496. while (n) {
  497. req = rb_entry(n, struct ceph_osd_request, r_node);
  498. if (tid < req->r_tid) {
  499. if (!n->rb_left)
  500. return req;
  501. n = n->rb_left;
  502. } else if (tid > req->r_tid) {
  503. n = n->rb_right;
  504. } else {
  505. return req;
  506. }
  507. }
  508. return NULL;
  509. }
  510. /*
  511. * Resubmit requests pending on the given osd.
  512. */
  513. static void __kick_osd_requests(struct ceph_osd_client *osdc,
  514. struct ceph_osd *osd)
  515. {
  516. struct ceph_osd_request *req, *nreq;
  517. int err;
  518. dout("__kick_osd_requests osd%d\n", osd->o_osd);
  519. err = __reset_osd(osdc, osd);
  520. if (err)
  521. return;
  522. list_for_each_entry(req, &osd->o_requests, r_osd_item) {
  523. list_move(&req->r_req_lru_item, &osdc->req_unsent);
  524. dout("requeued %p tid %llu osd%d\n", req, req->r_tid,
  525. osd->o_osd);
  526. if (!req->r_linger)
  527. req->r_flags |= CEPH_OSD_FLAG_RETRY;
  528. }
  529. list_for_each_entry_safe(req, nreq, &osd->o_linger_requests,
  530. r_linger_osd) {
  531. /*
  532. * reregister request prior to unregistering linger so
  533. * that r_osd is preserved.
  534. */
  535. BUG_ON(!list_empty(&req->r_req_lru_item));
  536. __register_request(osdc, req);
  537. list_add(&req->r_req_lru_item, &osdc->req_unsent);
  538. list_add(&req->r_osd_item, &req->r_osd->o_requests);
  539. __unregister_linger_request(osdc, req);
  540. dout("requeued lingering %p tid %llu osd%d\n", req, req->r_tid,
  541. osd->o_osd);
  542. }
  543. }
  544. /*
  545. * If the osd connection drops, we need to resubmit all requests.
  546. */
  547. static void osd_reset(struct ceph_connection *con)
  548. {
  549. struct ceph_osd *osd = con->private;
  550. struct ceph_osd_client *osdc;
  551. if (!osd)
  552. return;
  553. dout("osd_reset osd%d\n", osd->o_osd);
  554. osdc = osd->o_osdc;
  555. down_read(&osdc->map_sem);
  556. mutex_lock(&osdc->request_mutex);
  557. __kick_osd_requests(osdc, osd);
  558. mutex_unlock(&osdc->request_mutex);
  559. send_queued(osdc);
  560. up_read(&osdc->map_sem);
  561. }
  562. /*
  563. * Track open sessions with osds.
  564. */
  565. static struct ceph_osd *create_osd(struct ceph_osd_client *osdc, int onum)
  566. {
  567. struct ceph_osd *osd;
  568. osd = kzalloc(sizeof(*osd), GFP_NOFS);
  569. if (!osd)
  570. return NULL;
  571. atomic_set(&osd->o_ref, 1);
  572. osd->o_osdc = osdc;
  573. osd->o_osd = onum;
  574. RB_CLEAR_NODE(&osd->o_node);
  575. INIT_LIST_HEAD(&osd->o_requests);
  576. INIT_LIST_HEAD(&osd->o_linger_requests);
  577. INIT_LIST_HEAD(&osd->o_osd_lru);
  578. osd->o_incarnation = 1;
  579. ceph_con_init(&osd->o_con, osd, &osd_con_ops, &osdc->client->msgr);
  580. INIT_LIST_HEAD(&osd->o_keepalive_item);
  581. return osd;
  582. }
  583. static struct ceph_osd *get_osd(struct ceph_osd *osd)
  584. {
  585. if (atomic_inc_not_zero(&osd->o_ref)) {
  586. dout("get_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref)-1,
  587. atomic_read(&osd->o_ref));
  588. return osd;
  589. } else {
  590. dout("get_osd %p FAIL\n", osd);
  591. return NULL;
  592. }
  593. }
  594. static void put_osd(struct ceph_osd *osd)
  595. {
  596. dout("put_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref),
  597. atomic_read(&osd->o_ref) - 1);
  598. if (atomic_dec_and_test(&osd->o_ref) && osd->o_auth.authorizer) {
  599. struct ceph_auth_client *ac = osd->o_osdc->client->monc.auth;
  600. if (ac->ops && ac->ops->destroy_authorizer)
  601. ac->ops->destroy_authorizer(ac, osd->o_auth.authorizer);
  602. kfree(osd);
  603. }
  604. }
  605. /*
  606. * remove an osd from our map
  607. */
  608. static void __remove_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
  609. {
  610. dout("__remove_osd %p\n", osd);
  611. BUG_ON(!list_empty(&osd->o_requests));
  612. rb_erase(&osd->o_node, &osdc->osds);
  613. list_del_init(&osd->o_osd_lru);
  614. ceph_con_close(&osd->o_con);
  615. put_osd(osd);
  616. }
  617. static void remove_all_osds(struct ceph_osd_client *osdc)
  618. {
  619. dout("%s %p\n", __func__, osdc);
  620. mutex_lock(&osdc->request_mutex);
  621. while (!RB_EMPTY_ROOT(&osdc->osds)) {
  622. struct ceph_osd *osd = rb_entry(rb_first(&osdc->osds),
  623. struct ceph_osd, o_node);
  624. __remove_osd(osdc, osd);
  625. }
  626. mutex_unlock(&osdc->request_mutex);
  627. }
  628. static void __move_osd_to_lru(struct ceph_osd_client *osdc,
  629. struct ceph_osd *osd)
  630. {
  631. dout("__move_osd_to_lru %p\n", osd);
  632. BUG_ON(!list_empty(&osd->o_osd_lru));
  633. list_add_tail(&osd->o_osd_lru, &osdc->osd_lru);
  634. osd->lru_ttl = jiffies + osdc->client->options->osd_idle_ttl * HZ;
  635. }
  636. static void __remove_osd_from_lru(struct ceph_osd *osd)
  637. {
  638. dout("__remove_osd_from_lru %p\n", osd);
  639. if (!list_empty(&osd->o_osd_lru))
  640. list_del_init(&osd->o_osd_lru);
  641. }
  642. static void remove_old_osds(struct ceph_osd_client *osdc)
  643. {
  644. struct ceph_osd *osd, *nosd;
  645. dout("__remove_old_osds %p\n", osdc);
  646. mutex_lock(&osdc->request_mutex);
  647. list_for_each_entry_safe(osd, nosd, &osdc->osd_lru, o_osd_lru) {
  648. if (time_before(jiffies, osd->lru_ttl))
  649. break;
  650. __remove_osd(osdc, osd);
  651. }
  652. mutex_unlock(&osdc->request_mutex);
  653. }
  654. /*
  655. * reset osd connect
  656. */
  657. static int __reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
  658. {
  659. struct ceph_osd_request *req;
  660. int ret = 0;
  661. dout("__reset_osd %p osd%d\n", osd, osd->o_osd);
  662. if (list_empty(&osd->o_requests) &&
  663. list_empty(&osd->o_linger_requests)) {
  664. __remove_osd(osdc, osd);
  665. ret = -ENODEV;
  666. } else if (memcmp(&osdc->osdmap->osd_addr[osd->o_osd],
  667. &osd->o_con.peer_addr,
  668. sizeof(osd->o_con.peer_addr)) == 0 &&
  669. !ceph_con_opened(&osd->o_con)) {
  670. dout(" osd addr hasn't changed and connection never opened,"
  671. " letting msgr retry");
  672. /* touch each r_stamp for handle_timeout()'s benfit */
  673. list_for_each_entry(req, &osd->o_requests, r_osd_item)
  674. req->r_stamp = jiffies;
  675. ret = -EAGAIN;
  676. } else {
  677. ceph_con_close(&osd->o_con);
  678. ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd,
  679. &osdc->osdmap->osd_addr[osd->o_osd]);
  680. osd->o_incarnation++;
  681. }
  682. return ret;
  683. }
  684. static void __insert_osd(struct ceph_osd_client *osdc, struct ceph_osd *new)
  685. {
  686. struct rb_node **p = &osdc->osds.rb_node;
  687. struct rb_node *parent = NULL;
  688. struct ceph_osd *osd = NULL;
  689. dout("__insert_osd %p osd%d\n", new, new->o_osd);
  690. while (*p) {
  691. parent = *p;
  692. osd = rb_entry(parent, struct ceph_osd, o_node);
  693. if (new->o_osd < osd->o_osd)
  694. p = &(*p)->rb_left;
  695. else if (new->o_osd > osd->o_osd)
  696. p = &(*p)->rb_right;
  697. else
  698. BUG();
  699. }
  700. rb_link_node(&new->o_node, parent, p);
  701. rb_insert_color(&new->o_node, &osdc->osds);
  702. }
  703. static struct ceph_osd *__lookup_osd(struct ceph_osd_client *osdc, int o)
  704. {
  705. struct ceph_osd *osd;
  706. struct rb_node *n = osdc->osds.rb_node;
  707. while (n) {
  708. osd = rb_entry(n, struct ceph_osd, o_node);
  709. if (o < osd->o_osd)
  710. n = n->rb_left;
  711. else if (o > osd->o_osd)
  712. n = n->rb_right;
  713. else
  714. return osd;
  715. }
  716. return NULL;
  717. }
  718. static void __schedule_osd_timeout(struct ceph_osd_client *osdc)
  719. {
  720. schedule_delayed_work(&osdc->timeout_work,
  721. osdc->client->options->osd_keepalive_timeout * HZ);
  722. }
  723. static void __cancel_osd_timeout(struct ceph_osd_client *osdc)
  724. {
  725. cancel_delayed_work(&osdc->timeout_work);
  726. }
  727. /*
  728. * Register request, assign tid. If this is the first request, set up
  729. * the timeout event.
  730. */
  731. static void __register_request(struct ceph_osd_client *osdc,
  732. struct ceph_osd_request *req)
  733. {
  734. req->r_tid = ++osdc->last_tid;
  735. req->r_request->hdr.tid = cpu_to_le64(req->r_tid);
  736. dout("__register_request %p tid %lld\n", req, req->r_tid);
  737. __insert_request(osdc, req);
  738. ceph_osdc_get_request(req);
  739. osdc->num_requests++;
  740. if (osdc->num_requests == 1) {
  741. dout(" first request, scheduling timeout\n");
  742. __schedule_osd_timeout(osdc);
  743. }
  744. }
  745. static void register_request(struct ceph_osd_client *osdc,
  746. struct ceph_osd_request *req)
  747. {
  748. mutex_lock(&osdc->request_mutex);
  749. __register_request(osdc, req);
  750. mutex_unlock(&osdc->request_mutex);
  751. }
  752. /*
  753. * called under osdc->request_mutex
  754. */
  755. static void __unregister_request(struct ceph_osd_client *osdc,
  756. struct ceph_osd_request *req)
  757. {
  758. if (RB_EMPTY_NODE(&req->r_node)) {
  759. dout("__unregister_request %p tid %lld not registered\n",
  760. req, req->r_tid);
  761. return;
  762. }
  763. dout("__unregister_request %p tid %lld\n", req, req->r_tid);
  764. rb_erase(&req->r_node, &osdc->requests);
  765. osdc->num_requests--;
  766. if (req->r_osd) {
  767. /* make sure the original request isn't in flight. */
  768. ceph_msg_revoke(req->r_request);
  769. list_del_init(&req->r_osd_item);
  770. if (list_empty(&req->r_osd->o_requests) &&
  771. list_empty(&req->r_osd->o_linger_requests)) {
  772. dout("moving osd to %p lru\n", req->r_osd);
  773. __move_osd_to_lru(osdc, req->r_osd);
  774. }
  775. if (list_empty(&req->r_linger_item))
  776. req->r_osd = NULL;
  777. }
  778. list_del_init(&req->r_req_lru_item);
  779. ceph_osdc_put_request(req);
  780. if (osdc->num_requests == 0) {
  781. dout(" no requests, canceling timeout\n");
  782. __cancel_osd_timeout(osdc);
  783. }
  784. }
  785. /*
  786. * Cancel a previously queued request message
  787. */
  788. static void __cancel_request(struct ceph_osd_request *req)
  789. {
  790. if (req->r_sent && req->r_osd) {
  791. ceph_msg_revoke(req->r_request);
  792. req->r_sent = 0;
  793. }
  794. }
  795. static void __register_linger_request(struct ceph_osd_client *osdc,
  796. struct ceph_osd_request *req)
  797. {
  798. dout("__register_linger_request %p\n", req);
  799. list_add_tail(&req->r_linger_item, &osdc->req_linger);
  800. if (req->r_osd)
  801. list_add_tail(&req->r_linger_osd,
  802. &req->r_osd->o_linger_requests);
  803. }
  804. static void __unregister_linger_request(struct ceph_osd_client *osdc,
  805. struct ceph_osd_request *req)
  806. {
  807. dout("__unregister_linger_request %p\n", req);
  808. list_del_init(&req->r_linger_item);
  809. if (req->r_osd) {
  810. list_del_init(&req->r_linger_osd);
  811. if (list_empty(&req->r_osd->o_requests) &&
  812. list_empty(&req->r_osd->o_linger_requests)) {
  813. dout("moving osd to %p lru\n", req->r_osd);
  814. __move_osd_to_lru(osdc, req->r_osd);
  815. }
  816. if (list_empty(&req->r_osd_item))
  817. req->r_osd = NULL;
  818. }
  819. }
  820. void ceph_osdc_unregister_linger_request(struct ceph_osd_client *osdc,
  821. struct ceph_osd_request *req)
  822. {
  823. mutex_lock(&osdc->request_mutex);
  824. if (req->r_linger) {
  825. __unregister_linger_request(osdc, req);
  826. ceph_osdc_put_request(req);
  827. }
  828. mutex_unlock(&osdc->request_mutex);
  829. }
  830. EXPORT_SYMBOL(ceph_osdc_unregister_linger_request);
  831. void ceph_osdc_set_request_linger(struct ceph_osd_client *osdc,
  832. struct ceph_osd_request *req)
  833. {
  834. if (!req->r_linger) {
  835. dout("set_request_linger %p\n", req);
  836. req->r_linger = 1;
  837. /*
  838. * caller is now responsible for calling
  839. * unregister_linger_request
  840. */
  841. ceph_osdc_get_request(req);
  842. }
  843. }
  844. EXPORT_SYMBOL(ceph_osdc_set_request_linger);
  845. /*
  846. * Pick an osd (the first 'up' osd in the pg), allocate the osd struct
  847. * (as needed), and set the request r_osd appropriately. If there is
  848. * no up osd, set r_osd to NULL. Move the request to the appropriate list
  849. * (unsent, homeless) or leave on in-flight lru.
  850. *
  851. * Return 0 if unchanged, 1 if changed, or negative on error.
  852. *
  853. * Caller should hold map_sem for read and request_mutex.
  854. */
  855. static int __map_request(struct ceph_osd_client *osdc,
  856. struct ceph_osd_request *req, int force_resend)
  857. {
  858. struct ceph_osd_request_head *reqhead = req->r_request->front.iov_base;
  859. struct ceph_pg pgid;
  860. int acting[CEPH_PG_MAX_SIZE];
  861. int o = -1, num = 0;
  862. int err;
  863. dout("map_request %p tid %lld\n", req, req->r_tid);
  864. err = ceph_calc_object_layout(&reqhead->layout, req->r_oid,
  865. &req->r_file_layout, osdc->osdmap);
  866. if (err) {
  867. list_move(&req->r_req_lru_item, &osdc->req_notarget);
  868. return err;
  869. }
  870. pgid = reqhead->layout.ol_pgid;
  871. req->r_pgid = pgid;
  872. err = ceph_calc_pg_acting(osdc->osdmap, pgid, acting);
  873. if (err > 0) {
  874. o = acting[0];
  875. num = err;
  876. }
  877. if ((!force_resend &&
  878. req->r_osd && req->r_osd->o_osd == o &&
  879. req->r_sent >= req->r_osd->o_incarnation &&
  880. req->r_num_pg_osds == num &&
  881. memcmp(req->r_pg_osds, acting, sizeof(acting[0])*num) == 0) ||
  882. (req->r_osd == NULL && o == -1))
  883. return 0; /* no change */
  884. dout("map_request tid %llu pgid %d.%x osd%d (was osd%d)\n",
  885. req->r_tid, le32_to_cpu(pgid.pool), le16_to_cpu(pgid.ps), o,
  886. req->r_osd ? req->r_osd->o_osd : -1);
  887. /* record full pg acting set */
  888. memcpy(req->r_pg_osds, acting, sizeof(acting[0]) * num);
  889. req->r_num_pg_osds = num;
  890. if (req->r_osd) {
  891. __cancel_request(req);
  892. list_del_init(&req->r_osd_item);
  893. req->r_osd = NULL;
  894. }
  895. req->r_osd = __lookup_osd(osdc, o);
  896. if (!req->r_osd && o >= 0) {
  897. err = -ENOMEM;
  898. req->r_osd = create_osd(osdc, o);
  899. if (!req->r_osd) {
  900. list_move(&req->r_req_lru_item, &osdc->req_notarget);
  901. goto out;
  902. }
  903. dout("map_request osd %p is osd%d\n", req->r_osd, o);
  904. __insert_osd(osdc, req->r_osd);
  905. ceph_con_open(&req->r_osd->o_con,
  906. CEPH_ENTITY_TYPE_OSD, o,
  907. &osdc->osdmap->osd_addr[o]);
  908. }
  909. if (req->r_osd) {
  910. __remove_osd_from_lru(req->r_osd);
  911. list_add(&req->r_osd_item, &req->r_osd->o_requests);
  912. list_move(&req->r_req_lru_item, &osdc->req_unsent);
  913. } else {
  914. list_move(&req->r_req_lru_item, &osdc->req_notarget);
  915. }
  916. err = 1; /* osd or pg changed */
  917. out:
  918. return err;
  919. }
  920. /*
  921. * caller should hold map_sem (for read) and request_mutex
  922. */
  923. static void __send_request(struct ceph_osd_client *osdc,
  924. struct ceph_osd_request *req)
  925. {
  926. struct ceph_osd_request_head *reqhead;
  927. dout("send_request %p tid %llu to osd%d flags %d\n",
  928. req, req->r_tid, req->r_osd->o_osd, req->r_flags);
  929. reqhead = req->r_request->front.iov_base;
  930. reqhead->osdmap_epoch = cpu_to_le32(osdc->osdmap->epoch);
  931. reqhead->flags |= cpu_to_le32(req->r_flags); /* e.g., RETRY */
  932. reqhead->reassert_version = req->r_reassert_version;
  933. req->r_stamp = jiffies;
  934. list_move_tail(&req->r_req_lru_item, &osdc->req_lru);
  935. ceph_msg_get(req->r_request); /* send consumes a ref */
  936. ceph_con_send(&req->r_osd->o_con, req->r_request);
  937. req->r_sent = req->r_osd->o_incarnation;
  938. }
  939. /*
  940. * Send any requests in the queue (req_unsent).
  941. */
  942. static void send_queued(struct ceph_osd_client *osdc)
  943. {
  944. struct ceph_osd_request *req, *tmp;
  945. dout("send_queued\n");
  946. mutex_lock(&osdc->request_mutex);
  947. list_for_each_entry_safe(req, tmp, &osdc->req_unsent, r_req_lru_item) {
  948. __send_request(osdc, req);
  949. }
  950. mutex_unlock(&osdc->request_mutex);
  951. }
  952. /*
  953. * Timeout callback, called every N seconds when 1 or more osd
  954. * requests has been active for more than N seconds. When this
  955. * happens, we ping all OSDs with requests who have timed out to
  956. * ensure any communications channel reset is detected. Reset the
  957. * request timeouts another N seconds in the future as we go.
  958. * Reschedule the timeout event another N seconds in future (unless
  959. * there are no open requests).
  960. */
  961. static void handle_timeout(struct work_struct *work)
  962. {
  963. struct ceph_osd_client *osdc =
  964. container_of(work, struct ceph_osd_client, timeout_work.work);
  965. struct ceph_osd_request *req;
  966. struct ceph_osd *osd;
  967. unsigned long keepalive =
  968. osdc->client->options->osd_keepalive_timeout * HZ;
  969. struct list_head slow_osds;
  970. dout("timeout\n");
  971. down_read(&osdc->map_sem);
  972. ceph_monc_request_next_osdmap(&osdc->client->monc);
  973. mutex_lock(&osdc->request_mutex);
  974. /*
  975. * ping osds that are a bit slow. this ensures that if there
  976. * is a break in the TCP connection we will notice, and reopen
  977. * a connection with that osd (from the fault callback).
  978. */
  979. INIT_LIST_HEAD(&slow_osds);
  980. list_for_each_entry(req, &osdc->req_lru, r_req_lru_item) {
  981. if (time_before(jiffies, req->r_stamp + keepalive))
  982. break;
  983. osd = req->r_osd;
  984. BUG_ON(!osd);
  985. dout(" tid %llu is slow, will send keepalive on osd%d\n",
  986. req->r_tid, osd->o_osd);
  987. list_move_tail(&osd->o_keepalive_item, &slow_osds);
  988. }
  989. while (!list_empty(&slow_osds)) {
  990. osd = list_entry(slow_osds.next, struct ceph_osd,
  991. o_keepalive_item);
  992. list_del_init(&osd->o_keepalive_item);
  993. ceph_con_keepalive(&osd->o_con);
  994. }
  995. __schedule_osd_timeout(osdc);
  996. mutex_unlock(&osdc->request_mutex);
  997. send_queued(osdc);
  998. up_read(&osdc->map_sem);
  999. }
  1000. static void handle_osds_timeout(struct work_struct *work)
  1001. {
  1002. struct ceph_osd_client *osdc =
  1003. container_of(work, struct ceph_osd_client,
  1004. osds_timeout_work.work);
  1005. unsigned long delay =
  1006. osdc->client->options->osd_idle_ttl * HZ >> 2;
  1007. dout("osds timeout\n");
  1008. down_read(&osdc->map_sem);
  1009. remove_old_osds(osdc);
  1010. up_read(&osdc->map_sem);
  1011. schedule_delayed_work(&osdc->osds_timeout_work,
  1012. round_jiffies_relative(delay));
  1013. }
  1014. static void complete_request(struct ceph_osd_request *req)
  1015. {
  1016. if (req->r_safe_callback)
  1017. req->r_safe_callback(req, NULL);
  1018. complete_all(&req->r_safe_completion); /* fsync waiter */
  1019. }
  1020. /*
  1021. * handle osd op reply. either call the callback if it is specified,
  1022. * or do the completion to wake up the waiting thread.
  1023. */
  1024. static void handle_reply(struct ceph_osd_client *osdc, struct ceph_msg *msg,
  1025. struct ceph_connection *con)
  1026. {
  1027. struct ceph_osd_reply_head *rhead = msg->front.iov_base;
  1028. struct ceph_osd_request *req;
  1029. u64 tid;
  1030. int numops, object_len, flags;
  1031. s32 result;
  1032. tid = le64_to_cpu(msg->hdr.tid);
  1033. if (msg->front.iov_len < sizeof(*rhead))
  1034. goto bad;
  1035. numops = le32_to_cpu(rhead->num_ops);
  1036. object_len = le32_to_cpu(rhead->object_len);
  1037. result = le32_to_cpu(rhead->result);
  1038. if (msg->front.iov_len != sizeof(*rhead) + object_len +
  1039. numops * sizeof(struct ceph_osd_op))
  1040. goto bad;
  1041. dout("handle_reply %p tid %llu result %d\n", msg, tid, (int)result);
  1042. /* lookup */
  1043. mutex_lock(&osdc->request_mutex);
  1044. req = __lookup_request(osdc, tid);
  1045. if (req == NULL) {
  1046. dout("handle_reply tid %llu dne\n", tid);
  1047. mutex_unlock(&osdc->request_mutex);
  1048. return;
  1049. }
  1050. ceph_osdc_get_request(req);
  1051. flags = le32_to_cpu(rhead->flags);
  1052. /*
  1053. * if this connection filled our message, drop our reference now, to
  1054. * avoid a (safe but slower) revoke later.
  1055. */
  1056. if (req->r_con_filling_msg == con && req->r_reply == msg) {
  1057. dout(" dropping con_filling_msg ref %p\n", con);
  1058. req->r_con_filling_msg = NULL;
  1059. con->ops->put(con);
  1060. }
  1061. if (!req->r_got_reply) {
  1062. unsigned int bytes;
  1063. req->r_result = le32_to_cpu(rhead->result);
  1064. bytes = le32_to_cpu(msg->hdr.data_len);
  1065. dout("handle_reply result %d bytes %d\n", req->r_result,
  1066. bytes);
  1067. if (req->r_result == 0)
  1068. req->r_result = bytes;
  1069. /* in case this is a write and we need to replay, */
  1070. req->r_reassert_version = rhead->reassert_version;
  1071. req->r_got_reply = 1;
  1072. } else if ((flags & CEPH_OSD_FLAG_ONDISK) == 0) {
  1073. dout("handle_reply tid %llu dup ack\n", tid);
  1074. mutex_unlock(&osdc->request_mutex);
  1075. goto done;
  1076. }
  1077. dout("handle_reply tid %llu flags %d\n", tid, flags);
  1078. if (req->r_linger && (flags & CEPH_OSD_FLAG_ONDISK))
  1079. __register_linger_request(osdc, req);
  1080. /* either this is a read, or we got the safe response */
  1081. if (result < 0 ||
  1082. (flags & CEPH_OSD_FLAG_ONDISK) ||
  1083. ((flags & CEPH_OSD_FLAG_WRITE) == 0))
  1084. __unregister_request(osdc, req);
  1085. mutex_unlock(&osdc->request_mutex);
  1086. if (req->r_callback)
  1087. req->r_callback(req, msg);
  1088. else
  1089. complete_all(&req->r_completion);
  1090. if (flags & CEPH_OSD_FLAG_ONDISK)
  1091. complete_request(req);
  1092. done:
  1093. dout("req=%p req->r_linger=%d\n", req, req->r_linger);
  1094. ceph_osdc_put_request(req);
  1095. return;
  1096. bad:
  1097. pr_err("corrupt osd_op_reply got %d %d expected %d\n",
  1098. (int)msg->front.iov_len, le32_to_cpu(msg->hdr.front_len),
  1099. (int)sizeof(*rhead));
  1100. ceph_msg_dump(msg);
  1101. }
  1102. static void reset_changed_osds(struct ceph_osd_client *osdc)
  1103. {
  1104. struct rb_node *p, *n;
  1105. for (p = rb_first(&osdc->osds); p; p = n) {
  1106. struct ceph_osd *osd = rb_entry(p, struct ceph_osd, o_node);
  1107. n = rb_next(p);
  1108. if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) ||
  1109. memcmp(&osd->o_con.peer_addr,
  1110. ceph_osd_addr(osdc->osdmap,
  1111. osd->o_osd),
  1112. sizeof(struct ceph_entity_addr)) != 0)
  1113. __reset_osd(osdc, osd);
  1114. }
  1115. }
  1116. /*
  1117. * Requeue requests whose mapping to an OSD has changed. If requests map to
  1118. * no osd, request a new map.
  1119. *
  1120. * Caller should hold map_sem for read.
  1121. */
  1122. static void kick_requests(struct ceph_osd_client *osdc, int force_resend)
  1123. {
  1124. struct ceph_osd_request *req, *nreq;
  1125. struct rb_node *p;
  1126. int needmap = 0;
  1127. int err;
  1128. dout("kick_requests %s\n", force_resend ? " (force resend)" : "");
  1129. mutex_lock(&osdc->request_mutex);
  1130. for (p = rb_first(&osdc->requests); p; ) {
  1131. req = rb_entry(p, struct ceph_osd_request, r_node);
  1132. p = rb_next(p);
  1133. /*
  1134. * For linger requests that have not yet been
  1135. * registered, move them to the linger list; they'll
  1136. * be sent to the osd in the loop below. Unregister
  1137. * the request before re-registering it as a linger
  1138. * request to ensure the __map_request() below
  1139. * will decide it needs to be sent.
  1140. */
  1141. if (req->r_linger && list_empty(&req->r_linger_item)) {
  1142. dout("%p tid %llu restart on osd%d\n",
  1143. req, req->r_tid,
  1144. req->r_osd ? req->r_osd->o_osd : -1);
  1145. __unregister_request(osdc, req);
  1146. __register_linger_request(osdc, req);
  1147. continue;
  1148. }
  1149. err = __map_request(osdc, req, force_resend);
  1150. if (err < 0)
  1151. continue; /* error */
  1152. if (req->r_osd == NULL) {
  1153. dout("%p tid %llu maps to no osd\n", req, req->r_tid);
  1154. needmap++; /* request a newer map */
  1155. } else if (err > 0) {
  1156. if (!req->r_linger) {
  1157. dout("%p tid %llu requeued on osd%d\n", req,
  1158. req->r_tid,
  1159. req->r_osd ? req->r_osd->o_osd : -1);
  1160. req->r_flags |= CEPH_OSD_FLAG_RETRY;
  1161. }
  1162. }
  1163. }
  1164. list_for_each_entry_safe(req, nreq, &osdc->req_linger,
  1165. r_linger_item) {
  1166. dout("linger req=%p req->r_osd=%p\n", req, req->r_osd);
  1167. err = __map_request(osdc, req, force_resend);
  1168. dout("__map_request returned %d\n", err);
  1169. if (err == 0)
  1170. continue; /* no change and no osd was specified */
  1171. if (err < 0)
  1172. continue; /* hrm! */
  1173. if (req->r_osd == NULL) {
  1174. dout("tid %llu maps to no valid osd\n", req->r_tid);
  1175. needmap++; /* request a newer map */
  1176. continue;
  1177. }
  1178. dout("kicking lingering %p tid %llu osd%d\n", req, req->r_tid,
  1179. req->r_osd ? req->r_osd->o_osd : -1);
  1180. __register_request(osdc, req);
  1181. __unregister_linger_request(osdc, req);
  1182. }
  1183. mutex_unlock(&osdc->request_mutex);
  1184. if (needmap) {
  1185. dout("%d requests for down osds, need new map\n", needmap);
  1186. ceph_monc_request_next_osdmap(&osdc->client->monc);
  1187. }
  1188. reset_changed_osds(osdc);
  1189. }
  1190. /*
  1191. * Process updated osd map.
  1192. *
  1193. * The message contains any number of incremental and full maps, normally
  1194. * indicating some sort of topology change in the cluster. Kick requests
  1195. * off to different OSDs as needed.
  1196. */
  1197. void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg)
  1198. {
  1199. void *p, *end, *next;
  1200. u32 nr_maps, maplen;
  1201. u32 epoch;
  1202. struct ceph_osdmap *newmap = NULL, *oldmap;
  1203. int err;
  1204. struct ceph_fsid fsid;
  1205. dout("handle_map have %u\n", osdc->osdmap ? osdc->osdmap->epoch : 0);
  1206. p = msg->front.iov_base;
  1207. end = p + msg->front.iov_len;
  1208. /* verify fsid */
  1209. ceph_decode_need(&p, end, sizeof(fsid), bad);
  1210. ceph_decode_copy(&p, &fsid, sizeof(fsid));
  1211. if (ceph_check_fsid(osdc->client, &fsid) < 0)
  1212. return;
  1213. down_write(&osdc->map_sem);
  1214. /* incremental maps */
  1215. ceph_decode_32_safe(&p, end, nr_maps, bad);
  1216. dout(" %d inc maps\n", nr_maps);
  1217. while (nr_maps > 0) {
  1218. ceph_decode_need(&p, end, 2*sizeof(u32), bad);
  1219. epoch = ceph_decode_32(&p);
  1220. maplen = ceph_decode_32(&p);
  1221. ceph_decode_need(&p, end, maplen, bad);
  1222. next = p + maplen;
  1223. if (osdc->osdmap && osdc->osdmap->epoch+1 == epoch) {
  1224. dout("applying incremental map %u len %d\n",
  1225. epoch, maplen);
  1226. newmap = osdmap_apply_incremental(&p, next,
  1227. osdc->osdmap,
  1228. &osdc->client->msgr);
  1229. if (IS_ERR(newmap)) {
  1230. err = PTR_ERR(newmap);
  1231. goto bad;
  1232. }
  1233. BUG_ON(!newmap);
  1234. if (newmap != osdc->osdmap) {
  1235. ceph_osdmap_destroy(osdc->osdmap);
  1236. osdc->osdmap = newmap;
  1237. }
  1238. kick_requests(osdc, 0);
  1239. } else {
  1240. dout("ignoring incremental map %u len %d\n",
  1241. epoch, maplen);
  1242. }
  1243. p = next;
  1244. nr_maps--;
  1245. }
  1246. if (newmap)
  1247. goto done;
  1248. /* full maps */
  1249. ceph_decode_32_safe(&p, end, nr_maps, bad);
  1250. dout(" %d full maps\n", nr_maps);
  1251. while (nr_maps) {
  1252. ceph_decode_need(&p, end, 2*sizeof(u32), bad);
  1253. epoch = ceph_decode_32(&p);
  1254. maplen = ceph_decode_32(&p);
  1255. ceph_decode_need(&p, end, maplen, bad);
  1256. if (nr_maps > 1) {
  1257. dout("skipping non-latest full map %u len %d\n",
  1258. epoch, maplen);
  1259. } else if (osdc->osdmap && osdc->osdmap->epoch >= epoch) {
  1260. dout("skipping full map %u len %d, "
  1261. "older than our %u\n", epoch, maplen,
  1262. osdc->osdmap->epoch);
  1263. } else {
  1264. int skipped_map = 0;
  1265. dout("taking full map %u len %d\n", epoch, maplen);
  1266. newmap = osdmap_decode(&p, p+maplen);
  1267. if (IS_ERR(newmap)) {
  1268. err = PTR_ERR(newmap);
  1269. goto bad;
  1270. }
  1271. BUG_ON(!newmap);
  1272. oldmap = osdc->osdmap;
  1273. osdc->osdmap = newmap;
  1274. if (oldmap) {
  1275. if (oldmap->epoch + 1 < newmap->epoch)
  1276. skipped_map = 1;
  1277. ceph_osdmap_destroy(oldmap);
  1278. }
  1279. kick_requests(osdc, skipped_map);
  1280. }
  1281. p += maplen;
  1282. nr_maps--;
  1283. }
  1284. done:
  1285. downgrade_write(&osdc->map_sem);
  1286. ceph_monc_got_osdmap(&osdc->client->monc, osdc->osdmap->epoch);
  1287. /*
  1288. * subscribe to subsequent osdmap updates if full to ensure
  1289. * we find out when we are no longer full and stop returning
  1290. * ENOSPC.
  1291. */
  1292. if (ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL))
  1293. ceph_monc_request_next_osdmap(&osdc->client->monc);
  1294. send_queued(osdc);
  1295. up_read(&osdc->map_sem);
  1296. wake_up_all(&osdc->client->auth_wq);
  1297. return;
  1298. bad:
  1299. pr_err("osdc handle_map corrupt msg\n");
  1300. ceph_msg_dump(msg);
  1301. up_write(&osdc->map_sem);
  1302. return;
  1303. }
  1304. /*
  1305. * watch/notify callback event infrastructure
  1306. *
  1307. * These callbacks are used both for watch and notify operations.
  1308. */
  1309. static void __release_event(struct kref *kref)
  1310. {
  1311. struct ceph_osd_event *event =
  1312. container_of(kref, struct ceph_osd_event, kref);
  1313. dout("__release_event %p\n", event);
  1314. kfree(event);
  1315. }
  1316. static void get_event(struct ceph_osd_event *event)
  1317. {
  1318. kref_get(&event->kref);
  1319. }
  1320. void ceph_osdc_put_event(struct ceph_osd_event *event)
  1321. {
  1322. kref_put(&event->kref, __release_event);
  1323. }
  1324. EXPORT_SYMBOL(ceph_osdc_put_event);
  1325. static void __insert_event(struct ceph_osd_client *osdc,
  1326. struct ceph_osd_event *new)
  1327. {
  1328. struct rb_node **p = &osdc->event_tree.rb_node;
  1329. struct rb_node *parent = NULL;
  1330. struct ceph_osd_event *event = NULL;
  1331. while (*p) {
  1332. parent = *p;
  1333. event = rb_entry(parent, struct ceph_osd_event, node);
  1334. if (new->cookie < event->cookie)
  1335. p = &(*p)->rb_left;
  1336. else if (new->cookie > event->cookie)
  1337. p = &(*p)->rb_right;
  1338. else
  1339. BUG();
  1340. }
  1341. rb_link_node(&new->node, parent, p);
  1342. rb_insert_color(&new->node, &osdc->event_tree);
  1343. }
  1344. static struct ceph_osd_event *__find_event(struct ceph_osd_client *osdc,
  1345. u64 cookie)
  1346. {
  1347. struct rb_node **p = &osdc->event_tree.rb_node;
  1348. struct rb_node *parent = NULL;
  1349. struct ceph_osd_event *event = NULL;
  1350. while (*p) {
  1351. parent = *p;
  1352. event = rb_entry(parent, struct ceph_osd_event, node);
  1353. if (cookie < event->cookie)
  1354. p = &(*p)->rb_left;
  1355. else if (cookie > event->cookie)
  1356. p = &(*p)->rb_right;
  1357. else
  1358. return event;
  1359. }
  1360. return NULL;
  1361. }
  1362. static void __remove_event(struct ceph_osd_event *event)
  1363. {
  1364. struct ceph_osd_client *osdc = event->osdc;
  1365. if (!RB_EMPTY_NODE(&event->node)) {
  1366. dout("__remove_event removed %p\n", event);
  1367. rb_erase(&event->node, &osdc->event_tree);
  1368. ceph_osdc_put_event(event);
  1369. } else {
  1370. dout("__remove_event didn't remove %p\n", event);
  1371. }
  1372. }
  1373. int ceph_osdc_create_event(struct ceph_osd_client *osdc,
  1374. void (*event_cb)(u64, u64, u8, void *),
  1375. int one_shot, void *data,
  1376. struct ceph_osd_event **pevent)
  1377. {
  1378. struct ceph_osd_event *event;
  1379. event = kmalloc(sizeof(*event), GFP_NOIO);
  1380. if (!event)
  1381. return -ENOMEM;
  1382. dout("create_event %p\n", event);
  1383. event->cb = event_cb;
  1384. event->one_shot = one_shot;
  1385. event->data = data;
  1386. event->osdc = osdc;
  1387. INIT_LIST_HEAD(&event->osd_node);
  1388. RB_CLEAR_NODE(&event->node);
  1389. kref_init(&event->kref); /* one ref for us */
  1390. kref_get(&event->kref); /* one ref for the caller */
  1391. init_completion(&event->completion);
  1392. spin_lock(&osdc->event_lock);
  1393. event->cookie = ++osdc->event_count;
  1394. __insert_event(osdc, event);
  1395. spin_unlock(&osdc->event_lock);
  1396. *pevent = event;
  1397. return 0;
  1398. }
  1399. EXPORT_SYMBOL(ceph_osdc_create_event);
  1400. void ceph_osdc_cancel_event(struct ceph_osd_event *event)
  1401. {
  1402. struct ceph_osd_client *osdc = event->osdc;
  1403. dout("cancel_event %p\n", event);
  1404. spin_lock(&osdc->event_lock);
  1405. __remove_event(event);
  1406. spin_unlock(&osdc->event_lock);
  1407. ceph_osdc_put_event(event); /* caller's */
  1408. }
  1409. EXPORT_SYMBOL(ceph_osdc_cancel_event);
  1410. static void do_event_work(struct work_struct *work)
  1411. {
  1412. struct ceph_osd_event_work *event_work =
  1413. container_of(work, struct ceph_osd_event_work, work);
  1414. struct ceph_osd_event *event = event_work->event;
  1415. u64 ver = event_work->ver;
  1416. u64 notify_id = event_work->notify_id;
  1417. u8 opcode = event_work->opcode;
  1418. dout("do_event_work completing %p\n", event);
  1419. event->cb(ver, notify_id, opcode, event->data);
  1420. complete(&event->completion);
  1421. dout("do_event_work completed %p\n", event);
  1422. ceph_osdc_put_event(event);
  1423. kfree(event_work);
  1424. }
  1425. /*
  1426. * Process osd watch notifications
  1427. */
  1428. void handle_watch_notify(struct ceph_osd_client *osdc, struct ceph_msg *msg)
  1429. {
  1430. void *p, *end;
  1431. u8 proto_ver;
  1432. u64 cookie, ver, notify_id;
  1433. u8 opcode;
  1434. struct ceph_osd_event *event;
  1435. struct ceph_osd_event_work *event_work;
  1436. p = msg->front.iov_base;
  1437. end = p + msg->front.iov_len;
  1438. ceph_decode_8_safe(&p, end, proto_ver, bad);
  1439. ceph_decode_8_safe(&p, end, opcode, bad);
  1440. ceph_decode_64_safe(&p, end, cookie, bad);
  1441. ceph_decode_64_safe(&p, end, ver, bad);
  1442. ceph_decode_64_safe(&p, end, notify_id, bad);
  1443. spin_lock(&osdc->event_lock);
  1444. event = __find_event(osdc, cookie);
  1445. if (event) {
  1446. get_event(event);
  1447. if (event->one_shot)
  1448. __remove_event(event);
  1449. }
  1450. spin_unlock(&osdc->event_lock);
  1451. dout("handle_watch_notify cookie %lld ver %lld event %p\n",
  1452. cookie, ver, event);
  1453. if (event) {
  1454. event_work = kmalloc(sizeof(*event_work), GFP_NOIO);
  1455. if (!event_work) {
  1456. dout("ERROR: could not allocate event_work\n");
  1457. goto done_err;
  1458. }
  1459. INIT_WORK(&event_work->work, do_event_work);
  1460. event_work->event = event;
  1461. event_work->ver = ver;
  1462. event_work->notify_id = notify_id;
  1463. event_work->opcode = opcode;
  1464. if (!queue_work(osdc->notify_wq, &event_work->work)) {
  1465. dout("WARNING: failed to queue notify event work\n");
  1466. goto done_err;
  1467. }
  1468. }
  1469. return;
  1470. done_err:
  1471. complete(&event->completion);
  1472. ceph_osdc_put_event(event);
  1473. return;
  1474. bad:
  1475. pr_err("osdc handle_watch_notify corrupt msg\n");
  1476. return;
  1477. }
  1478. int ceph_osdc_wait_event(struct ceph_osd_event *event, unsigned long timeout)
  1479. {
  1480. int err;
  1481. dout("wait_event %p\n", event);
  1482. err = wait_for_completion_interruptible_timeout(&event->completion,
  1483. timeout * HZ);
  1484. ceph_osdc_put_event(event);
  1485. if (err > 0)
  1486. err = 0;
  1487. dout("wait_event %p returns %d\n", event, err);
  1488. return err;
  1489. }
  1490. EXPORT_SYMBOL(ceph_osdc_wait_event);
  1491. /*
  1492. * Register request, send initial attempt.
  1493. */
  1494. int ceph_osdc_start_request(struct ceph_osd_client *osdc,
  1495. struct ceph_osd_request *req,
  1496. bool nofail)
  1497. {
  1498. int rc = 0;
  1499. req->r_request->pages = req->r_pages;
  1500. req->r_request->nr_pages = req->r_num_pages;
  1501. #ifdef CONFIG_BLOCK
  1502. req->r_request->bio = req->r_bio;
  1503. #endif
  1504. req->r_request->trail = req->r_trail;
  1505. register_request(osdc, req);
  1506. down_read(&osdc->map_sem);
  1507. mutex_lock(&osdc->request_mutex);
  1508. /*
  1509. * a racing kick_requests() may have sent the message for us
  1510. * while we dropped request_mutex above, so only send now if
  1511. * the request still han't been touched yet.
  1512. */
  1513. if (req->r_sent == 0) {
  1514. rc = __map_request(osdc, req, 0);
  1515. if (rc < 0) {
  1516. if (nofail) {
  1517. dout("osdc_start_request failed map, "
  1518. " will retry %lld\n", req->r_tid);
  1519. rc = 0;
  1520. }
  1521. goto out_unlock;
  1522. }
  1523. if (req->r_osd == NULL) {
  1524. dout("send_request %p no up osds in pg\n", req);
  1525. ceph_monc_request_next_osdmap(&osdc->client->monc);
  1526. } else {
  1527. __send_request(osdc, req);
  1528. }
  1529. rc = 0;
  1530. }
  1531. out_unlock:
  1532. mutex_unlock(&osdc->request_mutex);
  1533. up_read(&osdc->map_sem);
  1534. return rc;
  1535. }
  1536. EXPORT_SYMBOL(ceph_osdc_start_request);
  1537. /*
  1538. * wait for a request to complete
  1539. */
  1540. int ceph_osdc_wait_request(struct ceph_osd_client *osdc,
  1541. struct ceph_osd_request *req)
  1542. {
  1543. int rc;
  1544. rc = wait_for_completion_interruptible(&req->r_completion);
  1545. if (rc < 0) {
  1546. mutex_lock(&osdc->request_mutex);
  1547. __cancel_request(req);
  1548. __unregister_request(osdc, req);
  1549. mutex_unlock(&osdc->request_mutex);
  1550. complete_request(req);
  1551. dout("wait_request tid %llu canceled/timed out\n", req->r_tid);
  1552. return rc;
  1553. }
  1554. dout("wait_request tid %llu result %d\n", req->r_tid, req->r_result);
  1555. return req->r_result;
  1556. }
  1557. EXPORT_SYMBOL(ceph_osdc_wait_request);
  1558. /*
  1559. * sync - wait for all in-flight requests to flush. avoid starvation.
  1560. */
  1561. void ceph_osdc_sync(struct ceph_osd_client *osdc)
  1562. {
  1563. struct ceph_osd_request *req;
  1564. u64 last_tid, next_tid = 0;
  1565. mutex_lock(&osdc->request_mutex);
  1566. last_tid = osdc->last_tid;
  1567. while (1) {
  1568. req = __lookup_request_ge(osdc, next_tid);
  1569. if (!req)
  1570. break;
  1571. if (req->r_tid > last_tid)
  1572. break;
  1573. next_tid = req->r_tid + 1;
  1574. if ((req->r_flags & CEPH_OSD_FLAG_WRITE) == 0)
  1575. continue;
  1576. ceph_osdc_get_request(req);
  1577. mutex_unlock(&osdc->request_mutex);
  1578. dout("sync waiting on tid %llu (last is %llu)\n",
  1579. req->r_tid, last_tid);
  1580. wait_for_completion(&req->r_safe_completion);
  1581. mutex_lock(&osdc->request_mutex);
  1582. ceph_osdc_put_request(req);
  1583. }
  1584. mutex_unlock(&osdc->request_mutex);
  1585. dout("sync done (thru tid %llu)\n", last_tid);
  1586. }
  1587. EXPORT_SYMBOL(ceph_osdc_sync);
  1588. /*
  1589. * init, shutdown
  1590. */
  1591. int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client)
  1592. {
  1593. int err;
  1594. dout("init\n");
  1595. osdc->client = client;
  1596. osdc->osdmap = NULL;
  1597. init_rwsem(&osdc->map_sem);
  1598. init_completion(&osdc->map_waiters);
  1599. osdc->last_requested_map = 0;
  1600. mutex_init(&osdc->request_mutex);
  1601. osdc->last_tid = 0;
  1602. osdc->osds = RB_ROOT;
  1603. INIT_LIST_HEAD(&osdc->osd_lru);
  1604. osdc->requests = RB_ROOT;
  1605. INIT_LIST_HEAD(&osdc->req_lru);
  1606. INIT_LIST_HEAD(&osdc->req_unsent);
  1607. INIT_LIST_HEAD(&osdc->req_notarget);
  1608. INIT_LIST_HEAD(&osdc->req_linger);
  1609. osdc->num_requests = 0;
  1610. INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout);
  1611. INIT_DELAYED_WORK(&osdc->osds_timeout_work, handle_osds_timeout);
  1612. spin_lock_init(&osdc->event_lock);
  1613. osdc->event_tree = RB_ROOT;
  1614. osdc->event_count = 0;
  1615. schedule_delayed_work(&osdc->osds_timeout_work,
  1616. round_jiffies_relative(osdc->client->options->osd_idle_ttl * HZ));
  1617. err = -ENOMEM;
  1618. osdc->req_mempool = mempool_create_kmalloc_pool(10,
  1619. sizeof(struct ceph_osd_request));
  1620. if (!osdc->req_mempool)
  1621. goto out;
  1622. err = ceph_msgpool_init(&osdc->msgpool_op, CEPH_MSG_OSD_OP,
  1623. OSD_OP_FRONT_LEN, 10, true,
  1624. "osd_op");
  1625. if (err < 0)
  1626. goto out_mempool;
  1627. err = ceph_msgpool_init(&osdc->msgpool_op_reply, CEPH_MSG_OSD_OPREPLY,
  1628. OSD_OPREPLY_FRONT_LEN, 10, true,
  1629. "osd_op_reply");
  1630. if (err < 0)
  1631. goto out_msgpool;
  1632. osdc->notify_wq = create_singlethread_workqueue("ceph-watch-notify");
  1633. if (IS_ERR(osdc->notify_wq)) {
  1634. err = PTR_ERR(osdc->notify_wq);
  1635. osdc->notify_wq = NULL;
  1636. goto out_msgpool;
  1637. }
  1638. return 0;
  1639. out_msgpool:
  1640. ceph_msgpool_destroy(&osdc->msgpool_op);
  1641. out_mempool:
  1642. mempool_destroy(osdc->req_mempool);
  1643. out:
  1644. return err;
  1645. }
  1646. EXPORT_SYMBOL(ceph_osdc_init);
  1647. void ceph_osdc_stop(struct ceph_osd_client *osdc)
  1648. {
  1649. flush_workqueue(osdc->notify_wq);
  1650. destroy_workqueue(osdc->notify_wq);
  1651. cancel_delayed_work_sync(&osdc->timeout_work);
  1652. cancel_delayed_work_sync(&osdc->osds_timeout_work);
  1653. if (osdc->osdmap) {
  1654. ceph_osdmap_destroy(osdc->osdmap);
  1655. osdc->osdmap = NULL;
  1656. }
  1657. remove_all_osds(osdc);
  1658. mempool_destroy(osdc->req_mempool);
  1659. ceph_msgpool_destroy(&osdc->msgpool_op);
  1660. ceph_msgpool_destroy(&osdc->msgpool_op_reply);
  1661. }
  1662. EXPORT_SYMBOL(ceph_osdc_stop);
  1663. /*
  1664. * Read some contiguous pages. If we cross a stripe boundary, shorten
  1665. * *plen. Return number of bytes read, or error.
  1666. */
  1667. int ceph_osdc_readpages(struct ceph_osd_client *osdc,
  1668. struct ceph_vino vino, struct ceph_file_layout *layout,
  1669. u64 off, u64 *plen,
  1670. u32 truncate_seq, u64 truncate_size,
  1671. struct page **pages, int num_pages, int page_align)
  1672. {
  1673. struct ceph_osd_request *req;
  1674. int rc = 0;
  1675. dout("readpages on ino %llx.%llx on %llu~%llu\n", vino.ino,
  1676. vino.snap, off, *plen);
  1677. req = ceph_osdc_new_request(osdc, layout, vino, off, plen,
  1678. CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ,
  1679. NULL, 0, truncate_seq, truncate_size, NULL,
  1680. false, 1, page_align);
  1681. if (IS_ERR(req))
  1682. return PTR_ERR(req);
  1683. /* it may be a short read due to an object boundary */
  1684. req->r_pages = pages;
  1685. dout("readpages final extent is %llu~%llu (%d pages align %d)\n",
  1686. off, *plen, req->r_num_pages, page_align);
  1687. rc = ceph_osdc_start_request(osdc, req, false);
  1688. if (!rc)
  1689. rc = ceph_osdc_wait_request(osdc, req);
  1690. ceph_osdc_put_request(req);
  1691. dout("readpages result %d\n", rc);
  1692. return rc;
  1693. }
  1694. EXPORT_SYMBOL(ceph_osdc_readpages);
  1695. /*
  1696. * do a synchronous write on N pages
  1697. */
  1698. int ceph_osdc_writepages(struct ceph_osd_client *osdc, struct ceph_vino vino,
  1699. struct ceph_file_layout *layout,
  1700. struct ceph_snap_context *snapc,
  1701. u64 off, u64 len,
  1702. u32 truncate_seq, u64 truncate_size,
  1703. struct timespec *mtime,
  1704. struct page **pages, int num_pages,
  1705. int flags, int do_sync, bool nofail)
  1706. {
  1707. struct ceph_osd_request *req;
  1708. int rc = 0;
  1709. int page_align = off & ~PAGE_MASK;
  1710. BUG_ON(vino.snap != CEPH_NOSNAP);
  1711. req = ceph_osdc_new_request(osdc, layout, vino, off, &len,
  1712. CEPH_OSD_OP_WRITE,
  1713. flags | CEPH_OSD_FLAG_ONDISK |
  1714. CEPH_OSD_FLAG_WRITE,
  1715. snapc, do_sync,
  1716. truncate_seq, truncate_size, mtime,
  1717. nofail, 1, page_align);
  1718. if (IS_ERR(req))
  1719. return PTR_ERR(req);
  1720. /* it may be a short write due to an object boundary */
  1721. req->r_pages = pages;
  1722. dout("writepages %llu~%llu (%d pages)\n", off, len,
  1723. req->r_num_pages);
  1724. rc = ceph_osdc_start_request(osdc, req, nofail);
  1725. if (!rc)
  1726. rc = ceph_osdc_wait_request(osdc, req);
  1727. ceph_osdc_put_request(req);
  1728. if (rc == 0)
  1729. rc = len;
  1730. dout("writepages result %d\n", rc);
  1731. return rc;
  1732. }
  1733. EXPORT_SYMBOL(ceph_osdc_writepages);
  1734. /*
  1735. * handle incoming message
  1736. */
  1737. static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
  1738. {
  1739. struct ceph_osd *osd = con->private;
  1740. struct ceph_osd_client *osdc;
  1741. int type = le16_to_cpu(msg->hdr.type);
  1742. if (!osd)
  1743. goto out;
  1744. osdc = osd->o_osdc;
  1745. switch (type) {
  1746. case CEPH_MSG_OSD_MAP:
  1747. ceph_osdc_handle_map(osdc, msg);
  1748. break;
  1749. case CEPH_MSG_OSD_OPREPLY:
  1750. handle_reply(osdc, msg, con);
  1751. break;
  1752. case CEPH_MSG_WATCH_NOTIFY:
  1753. handle_watch_notify(osdc, msg);
  1754. break;
  1755. default:
  1756. pr_err("received unknown message type %d %s\n", type,
  1757. ceph_msg_type_name(type));
  1758. }
  1759. out:
  1760. ceph_msg_put(msg);
  1761. }
  1762. /*
  1763. * lookup and return message for incoming reply. set up reply message
  1764. * pages.
  1765. */
  1766. static struct ceph_msg *get_reply(struct ceph_connection *con,
  1767. struct ceph_msg_header *hdr,
  1768. int *skip)
  1769. {
  1770. struct ceph_osd *osd = con->private;
  1771. struct ceph_osd_client *osdc = osd->o_osdc;
  1772. struct ceph_msg *m;
  1773. struct ceph_osd_request *req;
  1774. int front = le32_to_cpu(hdr->front_len);
  1775. int data_len = le32_to_cpu(hdr->data_len);
  1776. u64 tid;
  1777. tid = le64_to_cpu(hdr->tid);
  1778. mutex_lock(&osdc->request_mutex);
  1779. req = __lookup_request(osdc, tid);
  1780. if (!req) {
  1781. *skip = 1;
  1782. m = NULL;
  1783. dout("get_reply unknown tid %llu from osd%d\n", tid,
  1784. osd->o_osd);
  1785. goto out;
  1786. }
  1787. if (req->r_con_filling_msg) {
  1788. dout("%s revoking msg %p from old con %p\n", __func__,
  1789. req->r_reply, req->r_con_filling_msg);
  1790. ceph_msg_revoke_incoming(req->r_reply);
  1791. req->r_con_filling_msg->ops->put(req->r_con_filling_msg);
  1792. req->r_con_filling_msg = NULL;
  1793. }
  1794. if (front > req->r_reply->front.iov_len) {
  1795. pr_warning("get_reply front %d > preallocated %d\n",
  1796. front, (int)req->r_reply->front.iov_len);
  1797. m = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, front, GFP_NOFS, false);
  1798. if (!m)
  1799. goto out;
  1800. ceph_msg_put(req->r_reply);
  1801. req->r_reply = m;
  1802. }
  1803. m = ceph_msg_get(req->r_reply);
  1804. if (data_len > 0) {
  1805. int want = calc_pages_for(req->r_page_alignment, data_len);
  1806. if (unlikely(req->r_num_pages < want)) {
  1807. pr_warning("tid %lld reply has %d bytes %d pages, we"
  1808. " had only %d pages ready\n", tid, data_len,
  1809. want, req->r_num_pages);
  1810. *skip = 1;
  1811. ceph_msg_put(m);
  1812. m = NULL;
  1813. goto out;
  1814. }
  1815. m->pages = req->r_pages;
  1816. m->nr_pages = req->r_num_pages;
  1817. m->page_alignment = req->r_page_alignment;
  1818. #ifdef CONFIG_BLOCK
  1819. m->bio = req->r_bio;
  1820. #endif
  1821. }
  1822. *skip = 0;
  1823. req->r_con_filling_msg = con->ops->get(con);
  1824. dout("get_reply tid %lld %p\n", tid, m);
  1825. out:
  1826. mutex_unlock(&osdc->request_mutex);
  1827. return m;
  1828. }
  1829. static struct ceph_msg *alloc_msg(struct ceph_connection *con,
  1830. struct ceph_msg_header *hdr,
  1831. int *skip)
  1832. {
  1833. struct ceph_osd *osd = con->private;
  1834. int type = le16_to_cpu(hdr->type);
  1835. int front = le32_to_cpu(hdr->front_len);
  1836. *skip = 0;
  1837. switch (type) {
  1838. case CEPH_MSG_OSD_MAP:
  1839. case CEPH_MSG_WATCH_NOTIFY:
  1840. return ceph_msg_new(type, front, GFP_NOFS, false);
  1841. case CEPH_MSG_OSD_OPREPLY:
  1842. return get_reply(con, hdr, skip);
  1843. default:
  1844. pr_info("alloc_msg unexpected msg type %d from osd%d\n", type,
  1845. osd->o_osd);
  1846. *skip = 1;
  1847. return NULL;
  1848. }
  1849. }
  1850. /*
  1851. * Wrappers to refcount containing ceph_osd struct
  1852. */
  1853. static struct ceph_connection *get_osd_con(struct ceph_connection *con)
  1854. {
  1855. struct ceph_osd *osd = con->private;
  1856. if (get_osd(osd))
  1857. return con;
  1858. return NULL;
  1859. }
  1860. static void put_osd_con(struct ceph_connection *con)
  1861. {
  1862. struct ceph_osd *osd = con->private;
  1863. put_osd(osd);
  1864. }
  1865. /*
  1866. * authentication
  1867. */
  1868. /*
  1869. * Note: returned pointer is the address of a structure that's
  1870. * managed separately. Caller must *not* attempt to free it.
  1871. */
  1872. static struct ceph_auth_handshake *get_authorizer(struct ceph_connection *con,
  1873. int *proto, int force_new)
  1874. {
  1875. struct ceph_osd *o = con->private;
  1876. struct ceph_osd_client *osdc = o->o_osdc;
  1877. struct ceph_auth_client *ac = osdc->client->monc.auth;
  1878. struct ceph_auth_handshake *auth = &o->o_auth;
  1879. if (force_new && auth->authorizer) {
  1880. if (ac->ops && ac->ops->destroy_authorizer)
  1881. ac->ops->destroy_authorizer(ac, auth->authorizer);
  1882. auth->authorizer = NULL;
  1883. }
  1884. if (!auth->authorizer && ac->ops && ac->ops->create_authorizer) {
  1885. int ret = ac->ops->create_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
  1886. auth);
  1887. if (ret)
  1888. return ERR_PTR(ret);
  1889. }
  1890. *proto = ac->protocol;
  1891. return auth;
  1892. }
  1893. static int verify_authorizer_reply(struct ceph_connection *con, int len)
  1894. {
  1895. struct ceph_osd *o = con->private;
  1896. struct ceph_osd_client *osdc = o->o_osdc;
  1897. struct ceph_auth_client *ac = osdc->client->monc.auth;
  1898. /*
  1899. * XXX If ac->ops or ac->ops->verify_authorizer_reply is null,
  1900. * XXX which do we do: succeed or fail?
  1901. */
  1902. return ac->ops->verify_authorizer_reply(ac, o->o_auth.authorizer, len);
  1903. }
  1904. static int invalidate_authorizer(struct ceph_connection *con)
  1905. {
  1906. struct ceph_osd *o = con->private;
  1907. struct ceph_osd_client *osdc = o->o_osdc;
  1908. struct ceph_auth_client *ac = osdc->client->monc.auth;
  1909. if (ac->ops && ac->ops->invalidate_authorizer)
  1910. ac->ops->invalidate_authorizer(ac, CEPH_ENTITY_TYPE_OSD);
  1911. return ceph_monc_validate_auth(&osdc->client->monc);
  1912. }
  1913. static const struct ceph_connection_operations osd_con_ops = {
  1914. .get = get_osd_con,
  1915. .put = put_osd_con,
  1916. .dispatch = dispatch,
  1917. .get_authorizer = get_authorizer,
  1918. .verify_authorizer_reply = verify_authorizer_reply,
  1919. .invalidate_authorizer = invalidate_authorizer,
  1920. .alloc_msg = alloc_msg,
  1921. .fault = osd_reset,
  1922. };