Merge tag 'iommu-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/arm64/linux
[linux/fpc-iii.git] / net / ceph / cls_lock_client.c
blob17447c19d9376f37a267c000edcad997cbbe0c5e
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>
11 /**
12 * ceph_cls_lock - grab rados lock for object
13 * @oid, @oloc: object to lock
14 * @lock_name: the name of the lock
15 * @type: lock type (CEPH_CLS_LOCK_EXCLUSIVE or CEPH_CLS_LOCK_SHARED)
16 * @cookie: user-defined identifier for this instance of the lock
17 * @tag: user-defined tag
18 * @desc: user-defined lock description
19 * @flags: lock flags
21 * All operations on the same lock should use the same tag.
23 int ceph_cls_lock(struct ceph_osd_client *osdc,
24 struct ceph_object_id *oid,
25 struct ceph_object_locator *oloc,
26 char *lock_name, u8 type, char *cookie,
27 char *tag, char *desc, u8 flags)
29 int lock_op_buf_size;
30 int name_len = strlen(lock_name);
31 int cookie_len = strlen(cookie);
32 int tag_len = strlen(tag);
33 int desc_len = strlen(desc);
34 void *p, *end;
35 struct page *lock_op_page;
36 struct timespec64 mtime;
37 int ret;
39 lock_op_buf_size = name_len + sizeof(__le32) +
40 cookie_len + sizeof(__le32) +
41 tag_len + sizeof(__le32) +
42 desc_len + sizeof(__le32) +
43 sizeof(struct ceph_timespec) +
44 /* flag and type */
45 sizeof(u8) + sizeof(u8) +
46 CEPH_ENCODING_START_BLK_LEN;
47 if (lock_op_buf_size > PAGE_SIZE)
48 return -E2BIG;
50 lock_op_page = alloc_page(GFP_NOIO);
51 if (!lock_op_page)
52 return -ENOMEM;
54 p = page_address(lock_op_page);
55 end = p + lock_op_buf_size;
57 /* encode cls_lock_lock_op struct */
58 ceph_start_encoding(&p, 1, 1,
59 lock_op_buf_size - CEPH_ENCODING_START_BLK_LEN);
60 ceph_encode_string(&p, end, lock_name, name_len);
61 ceph_encode_8(&p, type);
62 ceph_encode_string(&p, end, cookie, cookie_len);
63 ceph_encode_string(&p, end, tag, tag_len);
64 ceph_encode_string(&p, end, desc, desc_len);
65 /* only support infinite duration */
66 memset(&mtime, 0, sizeof(mtime));
67 ceph_encode_timespec64(p, &mtime);
68 p += sizeof(struct ceph_timespec);
69 ceph_encode_8(&p, flags);
71 dout("%s lock_name %s type %d cookie %s tag %s desc %s flags 0x%x\n",
72 __func__, lock_name, type, cookie, tag, desc, flags);
73 ret = ceph_osdc_call(osdc, oid, oloc, "lock", "lock",
74 CEPH_OSD_FLAG_WRITE, lock_op_page,
75 lock_op_buf_size, NULL, NULL);
77 dout("%s: status %d\n", __func__, ret);
78 __free_page(lock_op_page);
79 return ret;
81 EXPORT_SYMBOL(ceph_cls_lock);
83 /**
84 * ceph_cls_unlock - release rados lock for object
85 * @oid, @oloc: object to lock
86 * @lock_name: the name of the lock
87 * @cookie: user-defined identifier for this instance of the lock
89 int ceph_cls_unlock(struct ceph_osd_client *osdc,
90 struct ceph_object_id *oid,
91 struct ceph_object_locator *oloc,
92 char *lock_name, char *cookie)
94 int unlock_op_buf_size;
95 int name_len = strlen(lock_name);
96 int cookie_len = strlen(cookie);
97 void *p, *end;
98 struct page *unlock_op_page;
99 int ret;
101 unlock_op_buf_size = name_len + sizeof(__le32) +
102 cookie_len + sizeof(__le32) +
103 CEPH_ENCODING_START_BLK_LEN;
104 if (unlock_op_buf_size > PAGE_SIZE)
105 return -E2BIG;
107 unlock_op_page = alloc_page(GFP_NOIO);
108 if (!unlock_op_page)
109 return -ENOMEM;
111 p = page_address(unlock_op_page);
112 end = p + unlock_op_buf_size;
114 /* encode cls_lock_unlock_op struct */
115 ceph_start_encoding(&p, 1, 1,
116 unlock_op_buf_size - CEPH_ENCODING_START_BLK_LEN);
117 ceph_encode_string(&p, end, lock_name, name_len);
118 ceph_encode_string(&p, end, cookie, cookie_len);
120 dout("%s lock_name %s cookie %s\n", __func__, lock_name, cookie);
121 ret = ceph_osdc_call(osdc, oid, oloc, "lock", "unlock",
122 CEPH_OSD_FLAG_WRITE, unlock_op_page,
123 unlock_op_buf_size, NULL, NULL);
125 dout("%s: status %d\n", __func__, ret);
126 __free_page(unlock_op_page);
127 return ret;
129 EXPORT_SYMBOL(ceph_cls_unlock);
132 * ceph_cls_break_lock - release rados lock for object for specified client
133 * @oid, @oloc: object to lock
134 * @lock_name: the name of the lock
135 * @cookie: user-defined identifier for this instance of the lock
136 * @locker: current lock owner
138 int ceph_cls_break_lock(struct ceph_osd_client *osdc,
139 struct ceph_object_id *oid,
140 struct ceph_object_locator *oloc,
141 char *lock_name, char *cookie,
142 struct ceph_entity_name *locker)
144 int break_op_buf_size;
145 int name_len = strlen(lock_name);
146 int cookie_len = strlen(cookie);
147 struct page *break_op_page;
148 void *p, *end;
149 int ret;
151 break_op_buf_size = name_len + sizeof(__le32) +
152 cookie_len + sizeof(__le32) +
153 sizeof(u8) + sizeof(__le64) +
154 CEPH_ENCODING_START_BLK_LEN;
155 if (break_op_buf_size > PAGE_SIZE)
156 return -E2BIG;
158 break_op_page = alloc_page(GFP_NOIO);
159 if (!break_op_page)
160 return -ENOMEM;
162 p = page_address(break_op_page);
163 end = p + break_op_buf_size;
165 /* encode cls_lock_break_op struct */
166 ceph_start_encoding(&p, 1, 1,
167 break_op_buf_size - CEPH_ENCODING_START_BLK_LEN);
168 ceph_encode_string(&p, end, lock_name, name_len);
169 ceph_encode_copy(&p, locker, sizeof(*locker));
170 ceph_encode_string(&p, end, cookie, cookie_len);
172 dout("%s lock_name %s cookie %s locker %s%llu\n", __func__, lock_name,
173 cookie, ENTITY_NAME(*locker));
174 ret = ceph_osdc_call(osdc, oid, oloc, "lock", "break_lock",
175 CEPH_OSD_FLAG_WRITE, break_op_page,
176 break_op_buf_size, NULL, NULL);
178 dout("%s: status %d\n", __func__, ret);
179 __free_page(break_op_page);
180 return ret;
182 EXPORT_SYMBOL(ceph_cls_break_lock);
184 int ceph_cls_set_cookie(struct ceph_osd_client *osdc,
185 struct ceph_object_id *oid,
186 struct ceph_object_locator *oloc,
187 char *lock_name, u8 type, char *old_cookie,
188 char *tag, char *new_cookie)
190 int cookie_op_buf_size;
191 int name_len = strlen(lock_name);
192 int old_cookie_len = strlen(old_cookie);
193 int tag_len = strlen(tag);
194 int new_cookie_len = strlen(new_cookie);
195 void *p, *end;
196 struct page *cookie_op_page;
197 int ret;
199 cookie_op_buf_size = name_len + sizeof(__le32) +
200 old_cookie_len + sizeof(__le32) +
201 tag_len + sizeof(__le32) +
202 new_cookie_len + sizeof(__le32) +
203 sizeof(u8) + CEPH_ENCODING_START_BLK_LEN;
204 if (cookie_op_buf_size > PAGE_SIZE)
205 return -E2BIG;
207 cookie_op_page = alloc_page(GFP_NOIO);
208 if (!cookie_op_page)
209 return -ENOMEM;
211 p = page_address(cookie_op_page);
212 end = p + cookie_op_buf_size;
214 /* encode cls_lock_set_cookie_op struct */
215 ceph_start_encoding(&p, 1, 1,
216 cookie_op_buf_size - CEPH_ENCODING_START_BLK_LEN);
217 ceph_encode_string(&p, end, lock_name, name_len);
218 ceph_encode_8(&p, type);
219 ceph_encode_string(&p, end, old_cookie, old_cookie_len);
220 ceph_encode_string(&p, end, tag, tag_len);
221 ceph_encode_string(&p, end, new_cookie, new_cookie_len);
223 dout("%s lock_name %s type %d old_cookie %s tag %s new_cookie %s\n",
224 __func__, lock_name, type, old_cookie, tag, new_cookie);
225 ret = ceph_osdc_call(osdc, oid, oloc, "lock", "set_cookie",
226 CEPH_OSD_FLAG_WRITE, cookie_op_page,
227 cookie_op_buf_size, NULL, NULL);
229 dout("%s: status %d\n", __func__, ret);
230 __free_page(cookie_op_page);
231 return ret;
233 EXPORT_SYMBOL(ceph_cls_set_cookie);
235 void ceph_free_lockers(struct ceph_locker *lockers, u32 num_lockers)
237 int i;
239 for (i = 0; i < num_lockers; i++)
240 kfree(lockers[i].id.cookie);
241 kfree(lockers);
243 EXPORT_SYMBOL(ceph_free_lockers);
245 static int decode_locker(void **p, void *end, struct ceph_locker *locker)
247 u8 struct_v;
248 u32 len;
249 char *s;
250 int ret;
252 ret = ceph_start_decoding(p, end, 1, "locker_id_t", &struct_v, &len);
253 if (ret)
254 return ret;
256 ceph_decode_copy(p, &locker->id.name, sizeof(locker->id.name));
257 s = ceph_extract_encoded_string(p, end, NULL, GFP_NOIO);
258 if (IS_ERR(s))
259 return PTR_ERR(s);
261 locker->id.cookie = s;
263 ret = ceph_start_decoding(p, end, 1, "locker_info_t", &struct_v, &len);
264 if (ret)
265 return ret;
267 *p += sizeof(struct ceph_timespec); /* skip expiration */
269 ret = ceph_decode_entity_addr(p, end, &locker->info.addr);
270 if (ret)
271 return ret;
273 len = ceph_decode_32(p);
274 *p += len; /* skip description */
276 dout("%s %s%llu cookie %s addr %s\n", __func__,
277 ENTITY_NAME(locker->id.name), locker->id.cookie,
278 ceph_pr_addr(&locker->info.addr));
279 return 0;
282 static int decode_lockers(void **p, void *end, u8 *type, char **tag,
283 struct ceph_locker **lockers, u32 *num_lockers)
285 u8 struct_v;
286 u32 struct_len;
287 char *s;
288 int i;
289 int ret;
291 ret = ceph_start_decoding(p, end, 1, "cls_lock_get_info_reply",
292 &struct_v, &struct_len);
293 if (ret)
294 return ret;
296 *num_lockers = ceph_decode_32(p);
297 *lockers = kcalloc(*num_lockers, sizeof(**lockers), GFP_NOIO);
298 if (!*lockers)
299 return -ENOMEM;
301 for (i = 0; i < *num_lockers; i++) {
302 ret = decode_locker(p, end, *lockers + i);
303 if (ret)
304 goto err_free_lockers;
307 *type = ceph_decode_8(p);
308 s = ceph_extract_encoded_string(p, end, NULL, GFP_NOIO);
309 if (IS_ERR(s)) {
310 ret = PTR_ERR(s);
311 goto err_free_lockers;
314 *tag = s;
315 return 0;
317 err_free_lockers:
318 ceph_free_lockers(*lockers, *num_lockers);
319 return ret;
323 * On success, the caller is responsible for:
325 * kfree(tag);
326 * ceph_free_lockers(lockers, num_lockers);
328 int ceph_cls_lock_info(struct ceph_osd_client *osdc,
329 struct ceph_object_id *oid,
330 struct ceph_object_locator *oloc,
331 char *lock_name, u8 *type, char **tag,
332 struct ceph_locker **lockers, u32 *num_lockers)
334 int get_info_op_buf_size;
335 int name_len = strlen(lock_name);
336 struct page *get_info_op_page, *reply_page;
337 size_t reply_len = PAGE_SIZE;
338 void *p, *end;
339 int ret;
341 get_info_op_buf_size = name_len + sizeof(__le32) +
342 CEPH_ENCODING_START_BLK_LEN;
343 if (get_info_op_buf_size > PAGE_SIZE)
344 return -E2BIG;
346 get_info_op_page = alloc_page(GFP_NOIO);
347 if (!get_info_op_page)
348 return -ENOMEM;
350 reply_page = alloc_page(GFP_NOIO);
351 if (!reply_page) {
352 __free_page(get_info_op_page);
353 return -ENOMEM;
356 p = page_address(get_info_op_page);
357 end = p + get_info_op_buf_size;
359 /* encode cls_lock_get_info_op struct */
360 ceph_start_encoding(&p, 1, 1,
361 get_info_op_buf_size - CEPH_ENCODING_START_BLK_LEN);
362 ceph_encode_string(&p, end, lock_name, name_len);
364 dout("%s lock_name %s\n", __func__, lock_name);
365 ret = ceph_osdc_call(osdc, oid, oloc, "lock", "get_info",
366 CEPH_OSD_FLAG_READ, get_info_op_page,
367 get_info_op_buf_size, &reply_page, &reply_len);
369 dout("%s: status %d\n", __func__, ret);
370 if (ret >= 0) {
371 p = page_address(reply_page);
372 end = p + reply_len;
374 ret = decode_lockers(&p, end, type, tag, lockers, num_lockers);
377 __free_page(get_info_op_page);
378 __free_page(reply_page);
379 return ret;
381 EXPORT_SYMBOL(ceph_cls_lock_info);
383 int ceph_cls_assert_locked(struct ceph_osd_request *req, int which,
384 char *lock_name, u8 type, char *cookie, char *tag)
386 int assert_op_buf_size;
387 int name_len = strlen(lock_name);
388 int cookie_len = strlen(cookie);
389 int tag_len = strlen(tag);
390 struct page **pages;
391 void *p, *end;
392 int ret;
394 assert_op_buf_size = name_len + sizeof(__le32) +
395 cookie_len + sizeof(__le32) +
396 tag_len + sizeof(__le32) +
397 sizeof(u8) + CEPH_ENCODING_START_BLK_LEN;
398 if (assert_op_buf_size > PAGE_SIZE)
399 return -E2BIG;
401 ret = osd_req_op_cls_init(req, which, "lock", "assert_locked");
402 if (ret)
403 return ret;
405 pages = ceph_alloc_page_vector(1, GFP_NOIO);
406 if (IS_ERR(pages))
407 return PTR_ERR(pages);
409 p = page_address(pages[0]);
410 end = p + assert_op_buf_size;
412 /* encode cls_lock_assert_op struct */
413 ceph_start_encoding(&p, 1, 1,
414 assert_op_buf_size - CEPH_ENCODING_START_BLK_LEN);
415 ceph_encode_string(&p, end, lock_name, name_len);
416 ceph_encode_8(&p, type);
417 ceph_encode_string(&p, end, cookie, cookie_len);
418 ceph_encode_string(&p, end, tag, tag_len);
419 WARN_ON(p != end);
421 osd_req_op_cls_request_data_pages(req, which, pages, assert_op_buf_size,
422 0, false, true);
423 return 0;
425 EXPORT_SYMBOL(ceph_cls_assert_locked);