cls_lock_client.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. #include <linux/ceph/ceph_debug.h>
  2. #include <linux/types.h>
  3. #include <linux/slab.h>
  4. #include <linux/ceph/cls_lock_client.h>
  5. #include <linux/ceph/decode.h>
  6. /**
  7. * ceph_cls_lock - grab rados lock for object
  8. * @oid, @oloc: object to lock
  9. * @lock_name: the name of the lock
  10. * @type: lock type (CEPH_CLS_LOCK_EXCLUSIVE or CEPH_CLS_LOCK_SHARED)
  11. * @cookie: user-defined identifier for this instance of the lock
  12. * @tag: user-defined tag
  13. * @desc: user-defined lock description
  14. * @flags: lock flags
  15. *
  16. * All operations on the same lock should use the same tag.
  17. */
  18. int ceph_cls_lock(struct ceph_osd_client *osdc,
  19. struct ceph_object_id *oid,
  20. struct ceph_object_locator *oloc,
  21. char *lock_name, u8 type, char *cookie,
  22. char *tag, char *desc, u8 flags)
  23. {
  24. int lock_op_buf_size;
  25. int name_len = strlen(lock_name);
  26. int cookie_len = strlen(cookie);
  27. int tag_len = strlen(tag);
  28. int desc_len = strlen(desc);
  29. void *p, *end;
  30. struct page *lock_op_page;
  31. struct timespec mtime;
  32. int ret;
  33. lock_op_buf_size = name_len + sizeof(__le32) +
  34. cookie_len + sizeof(__le32) +
  35. tag_len + sizeof(__le32) +
  36. desc_len + sizeof(__le32) +
  37. sizeof(struct ceph_timespec) +
  38. /* flag and type */
  39. sizeof(u8) + sizeof(u8) +
  40. CEPH_ENCODING_START_BLK_LEN;
  41. if (lock_op_buf_size > PAGE_SIZE)
  42. return -E2BIG;
  43. lock_op_page = alloc_page(GFP_NOIO);
  44. if (!lock_op_page)
  45. return -ENOMEM;
  46. p = page_address(lock_op_page);
  47. end = p + lock_op_buf_size;
  48. /* encode cls_lock_lock_op struct */
  49. ceph_start_encoding(&p, 1, 1,
  50. lock_op_buf_size - CEPH_ENCODING_START_BLK_LEN);
  51. ceph_encode_string(&p, end, lock_name, name_len);
  52. ceph_encode_8(&p, type);
  53. ceph_encode_string(&p, end, cookie, cookie_len);
  54. ceph_encode_string(&p, end, tag, tag_len);
  55. ceph_encode_string(&p, end, desc, desc_len);
  56. /* only support infinite duration */
  57. memset(&mtime, 0, sizeof(mtime));
  58. ceph_encode_timespec(p, &mtime);
  59. p += sizeof(struct ceph_timespec);
  60. ceph_encode_8(&p, flags);
  61. dout("%s lock_name %s type %d cookie %s tag %s desc %s flags 0x%x\n",
  62. __func__, lock_name, type, cookie, tag, desc, flags);
  63. ret = ceph_osdc_call(osdc, oid, oloc, "lock", "lock",
  64. CEPH_OSD_FLAG_WRITE | CEPH_OSD_FLAG_ONDISK,
  65. lock_op_page, lock_op_buf_size, NULL, NULL);
  66. dout("%s: status %d\n", __func__, ret);
  67. __free_page(lock_op_page);
  68. return ret;
  69. }
  70. EXPORT_SYMBOL(ceph_cls_lock);
  71. /**
  72. * ceph_cls_unlock - release rados lock for object
  73. * @oid, @oloc: object to lock
  74. * @lock_name: the name of the lock
  75. * @cookie: user-defined identifier for this instance of the lock
  76. */
  77. int ceph_cls_unlock(struct ceph_osd_client *osdc,
  78. struct ceph_object_id *oid,
  79. struct ceph_object_locator *oloc,
  80. char *lock_name, char *cookie)
  81. {
  82. int unlock_op_buf_size;
  83. int name_len = strlen(lock_name);
  84. int cookie_len = strlen(cookie);
  85. void *p, *end;
  86. struct page *unlock_op_page;
  87. int ret;
  88. unlock_op_buf_size = name_len + sizeof(__le32) +
  89. cookie_len + sizeof(__le32) +
  90. CEPH_ENCODING_START_BLK_LEN;
  91. if (unlock_op_buf_size > PAGE_SIZE)
  92. return -E2BIG;
  93. unlock_op_page = alloc_page(GFP_NOIO);
  94. if (!unlock_op_page)
  95. return -ENOMEM;
  96. p = page_address(unlock_op_page);
  97. end = p + unlock_op_buf_size;
  98. /* encode cls_lock_unlock_op struct */
  99. ceph_start_encoding(&p, 1, 1,
  100. unlock_op_buf_size - CEPH_ENCODING_START_BLK_LEN);
  101. ceph_encode_string(&p, end, lock_name, name_len);
  102. ceph_encode_string(&p, end, cookie, cookie_len);
  103. dout("%s lock_name %s cookie %s\n", __func__, lock_name, cookie);
  104. ret = ceph_osdc_call(osdc, oid, oloc, "lock", "unlock",
  105. CEPH_OSD_FLAG_WRITE | CEPH_OSD_FLAG_ONDISK,
  106. unlock_op_page, unlock_op_buf_size, NULL, NULL);
  107. dout("%s: status %d\n", __func__, ret);
  108. __free_page(unlock_op_page);
  109. return ret;
  110. }
  111. EXPORT_SYMBOL(ceph_cls_unlock);
  112. /**
  113. * ceph_cls_break_lock - release rados lock for object for specified client
  114. * @oid, @oloc: object to lock
  115. * @lock_name: the name of the lock
  116. * @cookie: user-defined identifier for this instance of the lock
  117. * @locker: current lock owner
  118. */
  119. int ceph_cls_break_lock(struct ceph_osd_client *osdc,
  120. struct ceph_object_id *oid,
  121. struct ceph_object_locator *oloc,
  122. char *lock_name, char *cookie,
  123. struct ceph_entity_name *locker)
  124. {
  125. int break_op_buf_size;
  126. int name_len = strlen(lock_name);
  127. int cookie_len = strlen(cookie);
  128. struct page *break_op_page;
  129. void *p, *end;
  130. int ret;
  131. break_op_buf_size = name_len + sizeof(__le32) +
  132. cookie_len + sizeof(__le32) +
  133. sizeof(u8) + sizeof(__le64) +
  134. CEPH_ENCODING_START_BLK_LEN;
  135. if (break_op_buf_size > PAGE_SIZE)
  136. return -E2BIG;
  137. break_op_page = alloc_page(GFP_NOIO);
  138. if (!break_op_page)
  139. return -ENOMEM;
  140. p = page_address(break_op_page);
  141. end = p + break_op_buf_size;
  142. /* encode cls_lock_break_op struct */
  143. ceph_start_encoding(&p, 1, 1,
  144. break_op_buf_size - CEPH_ENCODING_START_BLK_LEN);
  145. ceph_encode_string(&p, end, lock_name, name_len);
  146. ceph_encode_copy(&p, locker, sizeof(*locker));
  147. ceph_encode_string(&p, end, cookie, cookie_len);
  148. dout("%s lock_name %s cookie %s locker %s%llu\n", __func__, lock_name,
  149. cookie, ENTITY_NAME(*locker));
  150. ret = ceph_osdc_call(osdc, oid, oloc, "lock", "break_lock",
  151. CEPH_OSD_FLAG_WRITE | CEPH_OSD_FLAG_ONDISK,
  152. break_op_page, break_op_buf_size, NULL, NULL);
  153. dout("%s: status %d\n", __func__, ret);
  154. __free_page(break_op_page);
  155. return ret;
  156. }
  157. EXPORT_SYMBOL(ceph_cls_break_lock);
  158. void ceph_free_lockers(struct ceph_locker *lockers, u32 num_lockers)
  159. {
  160. int i;
  161. for (i = 0; i < num_lockers; i++)
  162. kfree(lockers[i].id.cookie);
  163. kfree(lockers);
  164. }
  165. EXPORT_SYMBOL(ceph_free_lockers);
  166. static int decode_locker(void **p, void *end, struct ceph_locker *locker)
  167. {
  168. u8 struct_v;
  169. u32 len;
  170. char *s;
  171. int ret;
  172. ret = ceph_start_decoding(p, end, 1, "locker_id_t", &struct_v, &len);
  173. if (ret)
  174. return ret;
  175. ceph_decode_copy(p, &locker->id.name, sizeof(locker->id.name));
  176. s = ceph_extract_encoded_string(p, end, NULL, GFP_NOIO);
  177. if (IS_ERR(s))
  178. return PTR_ERR(s);
  179. locker->id.cookie = s;
  180. ret = ceph_start_decoding(p, end, 1, "locker_info_t", &struct_v, &len);
  181. if (ret)
  182. return ret;
  183. *p += sizeof(struct ceph_timespec); /* skip expiration */
  184. ceph_decode_copy(p, &locker->info.addr, sizeof(locker->info.addr));
  185. ceph_decode_addr(&locker->info.addr);
  186. len = ceph_decode_32(p);
  187. *p += len; /* skip description */
  188. dout("%s %s%llu cookie %s addr %s\n", __func__,
  189. ENTITY_NAME(locker->id.name), locker->id.cookie,
  190. ceph_pr_addr(&locker->info.addr.in_addr));
  191. return 0;
  192. }
  193. static int decode_lockers(void **p, void *end, u8 *type, char **tag,
  194. struct ceph_locker **lockers, u32 *num_lockers)
  195. {
  196. u8 struct_v;
  197. u32 struct_len;
  198. char *s;
  199. int i;
  200. int ret;
  201. ret = ceph_start_decoding(p, end, 1, "cls_lock_get_info_reply",
  202. &struct_v, &struct_len);
  203. if (ret)
  204. return ret;
  205. *num_lockers = ceph_decode_32(p);
  206. *lockers = kcalloc(*num_lockers, sizeof(**lockers), GFP_NOIO);
  207. if (!*lockers)
  208. return -ENOMEM;
  209. for (i = 0; i < *num_lockers; i++) {
  210. ret = decode_locker(p, end, *lockers + i);
  211. if (ret)
  212. goto err_free_lockers;
  213. }
  214. *type = ceph_decode_8(p);
  215. s = ceph_extract_encoded_string(p, end, NULL, GFP_NOIO);
  216. if (IS_ERR(s)) {
  217. ret = PTR_ERR(s);
  218. goto err_free_lockers;
  219. }
  220. *tag = s;
  221. return 0;
  222. err_free_lockers:
  223. ceph_free_lockers(*lockers, *num_lockers);
  224. return ret;
  225. }
  226. /*
  227. * On success, the caller is responsible for:
  228. *
  229. * kfree(tag);
  230. * ceph_free_lockers(lockers, num_lockers);
  231. */
  232. int ceph_cls_lock_info(struct ceph_osd_client *osdc,
  233. struct ceph_object_id *oid,
  234. struct ceph_object_locator *oloc,
  235. char *lock_name, u8 *type, char **tag,
  236. struct ceph_locker **lockers, u32 *num_lockers)
  237. {
  238. int get_info_op_buf_size;
  239. int name_len = strlen(lock_name);
  240. struct page *get_info_op_page, *reply_page;
  241. size_t reply_len;
  242. void *p, *end;
  243. int ret;
  244. get_info_op_buf_size = name_len + sizeof(__le32) +
  245. CEPH_ENCODING_START_BLK_LEN;
  246. if (get_info_op_buf_size > PAGE_SIZE)
  247. return -E2BIG;
  248. get_info_op_page = alloc_page(GFP_NOIO);
  249. if (!get_info_op_page)
  250. return -ENOMEM;
  251. reply_page = alloc_page(GFP_NOIO);
  252. if (!reply_page) {
  253. __free_page(get_info_op_page);
  254. return -ENOMEM;
  255. }
  256. p = page_address(get_info_op_page);
  257. end = p + get_info_op_buf_size;
  258. /* encode cls_lock_get_info_op struct */
  259. ceph_start_encoding(&p, 1, 1,
  260. get_info_op_buf_size - CEPH_ENCODING_START_BLK_LEN);
  261. ceph_encode_string(&p, end, lock_name, name_len);
  262. dout("%s lock_name %s\n", __func__, lock_name);
  263. ret = ceph_osdc_call(osdc, oid, oloc, "lock", "get_info",
  264. CEPH_OSD_FLAG_READ, get_info_op_page,
  265. get_info_op_buf_size, reply_page, &reply_len);
  266. dout("%s: status %d\n", __func__, ret);
  267. if (ret >= 0) {
  268. p = page_address(reply_page);
  269. end = p + reply_len;
  270. ret = decode_lockers(&p, end, type, tag, lockers, num_lockers);
  271. }
  272. __free_page(get_info_op_page);
  273. __free_page(reply_page);
  274. return ret;
  275. }
  276. EXPORT_SYMBOL(ceph_cls_lock_info);