osd_client.c 59 KB

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