1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/ceph/ceph_debug.h>
4 #include <linux/types.h>
5 #include <linux/slab.h>
7 #include <linux/ceph/cls_lock_client.h>
8 #include <linux/ceph/decode.h>
9 #include <linux/ceph/libceph.h>
12 * ceph_cls_lock - grab rados lock for object
13 * @osdc: OSD client instance
14 * @oid: object to lock
15 * @oloc: object to lock
16 * @lock_name: the name of the lock
17 * @type: lock type (CEPH_CLS_LOCK_EXCLUSIVE or CEPH_CLS_LOCK_SHARED)
18 * @cookie: user-defined identifier for this instance of the lock
19 * @tag: user-defined tag
20 * @desc: user-defined lock description
23 * All operations on the same lock should use the same tag.
25 int ceph_cls_lock(struct ceph_osd_client *osdc,
26 struct ceph_object_id *oid,
27 struct ceph_object_locator *oloc,
28 char *lock_name, u8 type, char *cookie,
29 char *tag, char *desc, u8 flags)
32 int name_len = strlen(lock_name);
33 int cookie_len = strlen(cookie);
34 int tag_len = strlen(tag);
35 int desc_len = strlen(desc);
37 struct page *lock_op_page;
38 struct timespec64 mtime;
41 lock_op_buf_size = name_len + sizeof(__le32) +
42 cookie_len + sizeof(__le32) +
43 tag_len + sizeof(__le32) +
44 desc_len + sizeof(__le32) +
45 sizeof(struct ceph_timespec) +
47 sizeof(u8) + sizeof(u8) +
48 CEPH_ENCODING_START_BLK_LEN;
49 if (lock_op_buf_size > PAGE_SIZE)
52 lock_op_page = alloc_page(GFP_NOIO);
56 p = page_address(lock_op_page);
57 end = p + lock_op_buf_size;
59 /* encode cls_lock_lock_op struct */
60 ceph_start_encoding(&p, 1, 1,
61 lock_op_buf_size - CEPH_ENCODING_START_BLK_LEN);
62 ceph_encode_string(&p, end, lock_name, name_len);
63 ceph_encode_8(&p, type);
64 ceph_encode_string(&p, end, cookie, cookie_len);
65 ceph_encode_string(&p, end, tag, tag_len);
66 ceph_encode_string(&p, end, desc, desc_len);
67 /* only support infinite duration */
68 memset(&mtime, 0, sizeof(mtime));
69 ceph_encode_timespec64(p, &mtime);
70 p += sizeof(struct ceph_timespec);
71 ceph_encode_8(&p, flags);
73 dout("%s lock_name %s type %d cookie %s tag %s desc %s flags 0x%x\n",
74 __func__, lock_name, type, cookie, tag, desc, flags);
75 ret = ceph_osdc_call(osdc, oid, oloc, "lock", "lock",
76 CEPH_OSD_FLAG_WRITE, lock_op_page,
77 lock_op_buf_size, NULL, NULL);
79 dout("%s: status %d\n", __func__, ret);
80 __free_page(lock_op_page);
83 EXPORT_SYMBOL(ceph_cls_lock);
86 * ceph_cls_unlock - release rados lock for object
87 * @osdc: OSD client instance
88 * @oid: object to lock
89 * @oloc: object to lock
90 * @lock_name: the name of the lock
91 * @cookie: user-defined identifier for this instance of the lock
93 int ceph_cls_unlock(struct ceph_osd_client *osdc,
94 struct ceph_object_id *oid,
95 struct ceph_object_locator *oloc,
96 char *lock_name, char *cookie)
98 int unlock_op_buf_size;
99 int name_len = strlen(lock_name);
100 int cookie_len = strlen(cookie);
102 struct page *unlock_op_page;
105 unlock_op_buf_size = name_len + sizeof(__le32) +
106 cookie_len + sizeof(__le32) +
107 CEPH_ENCODING_START_BLK_LEN;
108 if (unlock_op_buf_size > PAGE_SIZE)
111 unlock_op_page = alloc_page(GFP_NOIO);
115 p = page_address(unlock_op_page);
116 end = p + unlock_op_buf_size;
118 /* encode cls_lock_unlock_op struct */
119 ceph_start_encoding(&p, 1, 1,
120 unlock_op_buf_size - CEPH_ENCODING_START_BLK_LEN);
121 ceph_encode_string(&p, end, lock_name, name_len);
122 ceph_encode_string(&p, end, cookie, cookie_len);
124 dout("%s lock_name %s cookie %s\n", __func__, lock_name, cookie);
125 ret = ceph_osdc_call(osdc, oid, oloc, "lock", "unlock",
126 CEPH_OSD_FLAG_WRITE, unlock_op_page,
127 unlock_op_buf_size, NULL, NULL);
129 dout("%s: status %d\n", __func__, ret);
130 __free_page(unlock_op_page);
133 EXPORT_SYMBOL(ceph_cls_unlock);
136 * ceph_cls_break_lock - release rados lock for object for specified client
137 * @osdc: OSD client instance
138 * @oid: object to lock
139 * @oloc: object to lock
140 * @lock_name: the name of the lock
141 * @cookie: user-defined identifier for this instance of the lock
142 * @locker: current lock owner
144 int ceph_cls_break_lock(struct ceph_osd_client *osdc,
145 struct ceph_object_id *oid,
146 struct ceph_object_locator *oloc,
147 char *lock_name, char *cookie,
148 struct ceph_entity_name *locker)
150 int break_op_buf_size;
151 int name_len = strlen(lock_name);
152 int cookie_len = strlen(cookie);
153 struct page *break_op_page;
157 break_op_buf_size = name_len + sizeof(__le32) +
158 cookie_len + sizeof(__le32) +
159 sizeof(u8) + sizeof(__le64) +
160 CEPH_ENCODING_START_BLK_LEN;
161 if (break_op_buf_size > PAGE_SIZE)
164 break_op_page = alloc_page(GFP_NOIO);
168 p = page_address(break_op_page);
169 end = p + break_op_buf_size;
171 /* encode cls_lock_break_op struct */
172 ceph_start_encoding(&p, 1, 1,
173 break_op_buf_size - CEPH_ENCODING_START_BLK_LEN);
174 ceph_encode_string(&p, end, lock_name, name_len);
175 ceph_encode_copy(&p, locker, sizeof(*locker));
176 ceph_encode_string(&p, end, cookie, cookie_len);
178 dout("%s lock_name %s cookie %s locker %s%llu\n", __func__, lock_name,
179 cookie, ENTITY_NAME(*locker));
180 ret = ceph_osdc_call(osdc, oid, oloc, "lock", "break_lock",
181 CEPH_OSD_FLAG_WRITE, break_op_page,
182 break_op_buf_size, NULL, NULL);
184 dout("%s: status %d\n", __func__, ret);
185 __free_page(break_op_page);
188 EXPORT_SYMBOL(ceph_cls_break_lock);
190 int ceph_cls_set_cookie(struct ceph_osd_client *osdc,
191 struct ceph_object_id *oid,
192 struct ceph_object_locator *oloc,
193 char *lock_name, u8 type, char *old_cookie,
194 char *tag, char *new_cookie)
196 int cookie_op_buf_size;
197 int name_len = strlen(lock_name);
198 int old_cookie_len = strlen(old_cookie);
199 int tag_len = strlen(tag);
200 int new_cookie_len = strlen(new_cookie);
202 struct page *cookie_op_page;
205 cookie_op_buf_size = name_len + sizeof(__le32) +
206 old_cookie_len + sizeof(__le32) +
207 tag_len + sizeof(__le32) +
208 new_cookie_len + sizeof(__le32) +
209 sizeof(u8) + CEPH_ENCODING_START_BLK_LEN;
210 if (cookie_op_buf_size > PAGE_SIZE)
213 cookie_op_page = alloc_page(GFP_NOIO);
217 p = page_address(cookie_op_page);
218 end = p + cookie_op_buf_size;
220 /* encode cls_lock_set_cookie_op struct */
221 ceph_start_encoding(&p, 1, 1,
222 cookie_op_buf_size - CEPH_ENCODING_START_BLK_LEN);
223 ceph_encode_string(&p, end, lock_name, name_len);
224 ceph_encode_8(&p, type);
225 ceph_encode_string(&p, end, old_cookie, old_cookie_len);
226 ceph_encode_string(&p, end, tag, tag_len);
227 ceph_encode_string(&p, end, new_cookie, new_cookie_len);
229 dout("%s lock_name %s type %d old_cookie %s tag %s new_cookie %s\n",
230 __func__, lock_name, type, old_cookie, tag, new_cookie);
231 ret = ceph_osdc_call(osdc, oid, oloc, "lock", "set_cookie",
232 CEPH_OSD_FLAG_WRITE, cookie_op_page,
233 cookie_op_buf_size, NULL, NULL);
235 dout("%s: status %d\n", __func__, ret);
236 __free_page(cookie_op_page);
239 EXPORT_SYMBOL(ceph_cls_set_cookie);
241 void ceph_free_lockers(struct ceph_locker *lockers, u32 num_lockers)
245 for (i = 0; i < num_lockers; i++)
246 kfree(lockers[i].id.cookie);
249 EXPORT_SYMBOL(ceph_free_lockers);
251 static int decode_locker(void **p, void *end, struct ceph_locker *locker)
258 ret = ceph_start_decoding(p, end, 1, "locker_id_t", &struct_v, &len);
262 ceph_decode_copy(p, &locker->id.name, sizeof(locker->id.name));
263 s = ceph_extract_encoded_string(p, end, NULL, GFP_NOIO);
267 locker->id.cookie = s;
269 ret = ceph_start_decoding(p, end, 1, "locker_info_t", &struct_v, &len);
273 *p += sizeof(struct ceph_timespec); /* skip expiration */
275 ret = ceph_decode_entity_addr(p, end, &locker->info.addr);
279 len = ceph_decode_32(p);
280 *p += len; /* skip description */
282 dout("%s %s%llu cookie %s addr %s\n", __func__,
283 ENTITY_NAME(locker->id.name), locker->id.cookie,
284 ceph_pr_addr(&locker->info.addr));
288 static int decode_lockers(void **p, void *end, u8 *type, char **tag,
289 struct ceph_locker **lockers, u32 *num_lockers)
297 ret = ceph_start_decoding(p, end, 1, "cls_lock_get_info_reply",
298 &struct_v, &struct_len);
302 *num_lockers = ceph_decode_32(p);
303 *lockers = kcalloc(*num_lockers, sizeof(**lockers), GFP_NOIO);
307 for (i = 0; i < *num_lockers; i++) {
308 ret = decode_locker(p, end, *lockers + i);
310 goto err_free_lockers;
313 *type = ceph_decode_8(p);
314 s = ceph_extract_encoded_string(p, end, NULL, GFP_NOIO);
317 goto err_free_lockers;
324 ceph_free_lockers(*lockers, *num_lockers);
329 * On success, the caller is responsible for:
332 * ceph_free_lockers(lockers, num_lockers);
334 int ceph_cls_lock_info(struct ceph_osd_client *osdc,
335 struct ceph_object_id *oid,
336 struct ceph_object_locator *oloc,
337 char *lock_name, u8 *type, char **tag,
338 struct ceph_locker **lockers, u32 *num_lockers)
340 int get_info_op_buf_size;
341 int name_len = strlen(lock_name);
342 struct page *get_info_op_page, *reply_page;
343 size_t reply_len = PAGE_SIZE;
347 get_info_op_buf_size = name_len + sizeof(__le32) +
348 CEPH_ENCODING_START_BLK_LEN;
349 if (get_info_op_buf_size > PAGE_SIZE)
352 get_info_op_page = alloc_page(GFP_NOIO);
353 if (!get_info_op_page)
356 reply_page = alloc_page(GFP_NOIO);
358 __free_page(get_info_op_page);
362 p = page_address(get_info_op_page);
363 end = p + get_info_op_buf_size;
365 /* encode cls_lock_get_info_op struct */
366 ceph_start_encoding(&p, 1, 1,
367 get_info_op_buf_size - CEPH_ENCODING_START_BLK_LEN);
368 ceph_encode_string(&p, end, lock_name, name_len);
370 dout("%s lock_name %s\n", __func__, lock_name);
371 ret = ceph_osdc_call(osdc, oid, oloc, "lock", "get_info",
372 CEPH_OSD_FLAG_READ, get_info_op_page,
373 get_info_op_buf_size, &reply_page, &reply_len);
375 dout("%s: status %d\n", __func__, ret);
377 p = page_address(reply_page);
380 ret = decode_lockers(&p, end, type, tag, lockers, num_lockers);
383 __free_page(get_info_op_page);
384 __free_page(reply_page);
387 EXPORT_SYMBOL(ceph_cls_lock_info);
389 int ceph_cls_assert_locked(struct ceph_osd_request *req, int which,
390 char *lock_name, u8 type, char *cookie, char *tag)
392 int assert_op_buf_size;
393 int name_len = strlen(lock_name);
394 int cookie_len = strlen(cookie);
395 int tag_len = strlen(tag);
400 assert_op_buf_size = name_len + sizeof(__le32) +
401 cookie_len + sizeof(__le32) +
402 tag_len + sizeof(__le32) +
403 sizeof(u8) + CEPH_ENCODING_START_BLK_LEN;
404 if (assert_op_buf_size > PAGE_SIZE)
407 ret = osd_req_op_cls_init(req, which, "lock", "assert_locked");
411 pages = ceph_alloc_page_vector(1, GFP_NOIO);
413 return PTR_ERR(pages);
415 p = page_address(pages[0]);
416 end = p + assert_op_buf_size;
418 /* encode cls_lock_assert_op struct */
419 ceph_start_encoding(&p, 1, 1,
420 assert_op_buf_size - CEPH_ENCODING_START_BLK_LEN);
421 ceph_encode_string(&p, end, lock_name, name_len);
422 ceph_encode_8(&p, type);
423 ceph_encode_string(&p, end, cookie, cookie_len);
424 ceph_encode_string(&p, end, tag, tag_len);
427 osd_req_op_cls_request_data_pages(req, which, pages, assert_op_buf_size,
431 EXPORT_SYMBOL(ceph_cls_assert_locked);