4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2014, 2015, Intel Corporation.
26 * lustre/osp/osp_trans.c
29 * 1. OSP (Object Storage Proxy) transaction methods
31 * Implement OSP layer transaction related interfaces for the dt_device API
32 * dt_device_operations.
35 * 2. Handle asynchronous idempotent operations
37 * The OSP uses OUT (Object Unified Target) RPC to talk with other server
38 * (MDT or OST) for kinds of operations, such as create, unlink, insert,
39 * delete, lookup, set_(x)attr, get_(x)attr, and etc. To reduce the number
40 * of RPCs, we allow multiple operations to be packaged together in single
43 * For the asynchronous idempotent operations, such as get_(x)attr, related
44 * RPCs will be inserted into an osp_device based shared asynchronous request
45 * queue - osp_device::opd_async_requests. When the queue is full, all the
46 * requests in the queue will be packaged into a single OUT RPC and given to
47 * the ptlrpcd daemon (for sending), then the queue is purged and other new
48 * requests can be inserted into it.
50 * When the asynchronous idempotent operation inserts the request into the
51 * shared queue, it will register an interpreter. When the packaged OUT RPC
52 * is replied (or failed to be sent out), all the registered interpreters
53 * will be called one by one to handle each own result.
56 * There are three kinds of transactions
58 * 1. Local transaction, all of updates of the transaction are in the local MDT.
59 * 2. Remote transaction, all of updates of the transaction are in one remote
60 * MDT, which only happens in LFSCK now.
61 * 3. Distribute transaction, updates for the transaction are in mulitple MDTs.
63 * Author: Di Wang <di.wang@intel.com>
64 * Author: Fan, Yong <fan.yong@intel.com>
67 #define DEBUG_SUBSYSTEM S_MDS
69 #include <lustre_net.h>
70 #include "osp_internal.h"
73 * The argument for the interpreter callback of osp request.
75 struct osp_update_args {
76 struct osp_update_request *oaua_update;
78 wait_queue_head_t *oaua_waitq;
79 bool oaua_flow_control;
80 const struct lu_env *oaua_update_env;
84 * Call back for each update request.
86 struct osp_update_callback {
87 /* list in the osp_update_request::our_cb_items */
88 struct list_head ouc_list;
90 /* The target of the async update request. */
91 struct osp_object *ouc_obj;
93 /* The data used by or_interpreter. */
96 /* The interpreter function called after the async request handled. */
97 osp_update_interpreter_t ouc_interpreter;
100 static struct object_update_request *object_update_request_alloc(size_t size)
102 struct object_update_request *ourq;
104 OBD_ALLOC_LARGE(ourq, size);
106 return ERR_PTR(-ENOMEM);
108 ourq->ourq_magic = UPDATE_REQUEST_MAGIC;
109 ourq->ourq_count = 0;
115 * Allocate new update request
117 * Allocate new update request and insert it to the req_update_list.
119 * \param [in] our osp_udate_request where to create a new
122 * \retval 0 if creation succeeds.
123 * \retval negative errno if creation fails.
125 int osp_object_update_request_create(struct osp_update_request *our,
128 struct osp_update_request_sub *ours;
134 if (size < OUT_UPDATE_INIT_BUFFER_SIZE)
135 size = OUT_UPDATE_INIT_BUFFER_SIZE;
137 ours->ours_req = object_update_request_alloc(size);
139 if (IS_ERR(ours->ours_req)) {
144 ours->ours_req_size = size;
145 INIT_LIST_HEAD(&ours->ours_list);
146 list_add_tail(&ours->ours_list, &our->our_req_list);
153 * Get current update request
155 * Get current object update request from our_req_list in
156 * osp_update_request, because we always insert the new update
157 * request in the last position, so the last update request
158 * in the list will be the current update req.
160 * \param[in] our osp update request where to get the
161 * current object update.
163 * \retval the current object update.
165 struct osp_update_request_sub *
166 osp_current_object_update_request(struct osp_update_request *our)
168 if (list_empty(&our->our_req_list))
171 return list_entry(our->our_req_list.prev, struct osp_update_request_sub,
176 * Allocate and initialize osp_update_request
178 * osp_update_request is being used to track updates being executed on
179 * this dt_device(OSD or OSP). The update buffer will be 4k initially,
180 * and increased if needed.
182 * \param [in] dt dt device
184 * \retval osp_update_request being allocated if succeed
185 * \retval ERR_PTR(errno) if failed
187 struct osp_update_request *osp_update_request_create(struct dt_device *dt)
189 struct osp_update_request *our;
193 return ERR_PTR(-ENOMEM);
195 INIT_LIST_HEAD(&our->our_req_list);
196 INIT_LIST_HEAD(&our->our_cb_items);
197 INIT_LIST_HEAD(&our->our_list);
198 spin_lock_init(&our->our_list_lock);
200 osp_object_update_request_create(our, OUT_UPDATE_INIT_BUFFER_SIZE);
204 void osp_update_request_destroy(struct osp_update_request *our)
206 struct osp_update_request_sub *ours;
207 struct osp_update_request_sub *tmp;
212 list_for_each_entry_safe(ours, tmp, &our->our_req_list, ours_list) {
213 list_del(&ours->ours_list);
214 if (ours->ours_req != NULL)
215 OBD_FREE_LARGE(ours->ours_req, ours->ours_req_size);
222 object_update_request_dump(const struct object_update_request *ourq,
226 size_t total_size = 0;
228 for (i = 0; i < ourq->ourq_count; i++) {
229 struct object_update *update;
232 update = object_update_request_get(ourq, i, &size);
233 LASSERT(update != NULL);
234 CDEBUG(mask, "i = %u fid = "DFID" op = %s "
235 "params = %d batchid = "LPU64" size = %zu repsize %u\n",
236 i, PFID(&update->ou_fid),
237 update_op_str(update->ou_type),
238 update->ou_params_count,
239 update->ou_batchid, size,
240 (unsigned)update->ou_result_size);
245 CDEBUG(mask, "updates = %p magic = %x count = %d size = %zu\n", ourq,
246 ourq->ourq_magic, ourq->ourq_count, total_size);
250 * Prepare inline update request
252 * Prepare OUT update ptlrpc inline request, and the request usually includes
253 * one update buffer, which does not need bulk transfer.
255 * \param[in] env execution environment
256 * \param[in] req ptlrpc request
257 * \param[in] ours sub osp_update_request to be packed
259 * \retval 0 if packing succeeds
260 * \retval negative errno if packing fails
262 int osp_prep_inline_update_req(const struct lu_env *env,
263 struct ptlrpc_request *req,
264 struct osp_update_request *our,
267 struct osp_update_request_sub *ours;
268 struct out_update_header *ouh;
269 __u32 update_req_size;
272 ours = list_entry(our->our_req_list.next,
273 struct osp_update_request_sub, ours_list);
274 update_req_size = object_update_request_size(ours->ours_req);
275 req_capsule_set_size(&req->rq_pill, &RMF_OUT_UPDATE_HEADER, RCL_CLIENT,
276 update_req_size + sizeof(*ouh));
278 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, OUT_UPDATE);
282 ouh = req_capsule_client_get(&req->rq_pill, &RMF_OUT_UPDATE_HEADER);
283 ouh->ouh_magic = OUT_UPDATE_HEADER_MAGIC;
285 ouh->ouh_inline_length = update_req_size;
286 ouh->ouh_reply_size = repsize;
288 memcpy(ouh->ouh_inline_data, ours->ours_req, update_req_size);
290 req_capsule_set_size(&req->rq_pill, &RMF_OUT_UPDATE_REPLY,
291 RCL_SERVER, repsize);
293 ptlrpc_request_set_replen(req);
294 req->rq_request_portal = OUT_PORTAL;
295 req->rq_reply_portal = OSC_REPLY_PORTAL;
301 * Prepare update request.
303 * Prepare OUT update ptlrpc request, and the request usually includes
304 * all of updates (stored in \param ureq) from one operation.
306 * \param[in] env execution environment
307 * \param[in] imp import on which ptlrpc request will be sent
308 * \param[in] ureq hold all of updates which will be packed into the req
309 * \param[in] reqp request to be created
311 * \retval 0 if preparation succeeds.
312 * \retval negative errno if preparation fails.
314 int osp_prep_update_req(const struct lu_env *env, struct obd_import *imp,
315 struct osp_update_request *our,
316 struct ptlrpc_request **reqp)
318 struct ptlrpc_request *req;
319 struct ptlrpc_bulk_desc *desc;
320 struct osp_update_request_sub *ours;
321 const struct object_update_request *ourq;
322 struct out_update_header *ouh;
323 struct out_update_buffer *oub;
326 struct object_update_reply *reply;
331 list_for_each_entry(ours, &our->our_req_list, ours_list) {
332 object_update_request_dump(ours->ours_req, D_INFO);
334 ourq = ours->ours_req;
335 for (i = 0; i < ourq->ourq_count; i++) {
336 struct object_update *update;
340 /* XXX: it's very inefficient to lookup update
341 * this way, iterating from the beginning
343 update = object_update_request_get(ourq, i, &size);
344 LASSERT(update != NULL);
346 repsize += sizeof(reply->ourp_lens[0]);
347 repsize += sizeof(struct object_update_result);
348 repsize += update->ou_result_size;
353 repsize += sizeof(*reply);
354 repsize = (repsize + OUT_UPDATE_REPLY_SIZE - 1) &
355 ~(OUT_UPDATE_REPLY_SIZE - 1);
356 LASSERT(buf_count > 0);
358 req = ptlrpc_request_alloc(imp, &RQF_OUT_UPDATE);
362 if (buf_count == 1) {
363 ours = list_entry(our->our_req_list.next,
364 struct osp_update_request_sub, ours_list);
366 /* Let's check if it can be packed inline */
367 if (object_update_request_size(ours->ours_req) +
368 sizeof(struct out_update_header) <
369 OUT_UPDATE_MAX_INLINE_SIZE) {
370 rc = osp_prep_inline_update_req(env, req, our, repsize);
377 req_capsule_set_size(&req->rq_pill, &RMF_OUT_UPDATE_HEADER, RCL_CLIENT,
378 sizeof(struct osp_update_request));
380 req_capsule_set_size(&req->rq_pill, &RMF_OUT_UPDATE_BUF, RCL_CLIENT,
381 buf_count * sizeof(*oub));
383 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, OUT_UPDATE);
387 ouh = req_capsule_client_get(&req->rq_pill, &RMF_OUT_UPDATE_HEADER);
388 ouh->ouh_magic = OUT_UPDATE_HEADER_MAGIC;
389 ouh->ouh_count = buf_count;
390 ouh->ouh_inline_length = 0;
391 ouh->ouh_reply_size = repsize;
392 oub = req_capsule_client_get(&req->rq_pill, &RMF_OUT_UPDATE_BUF);
393 list_for_each_entry(ours, &our->our_req_list, ours_list) {
394 oub->oub_size = ours->ours_req_size;
398 req->rq_bulk_write = 1;
399 desc = ptlrpc_prep_bulk_imp(req, buf_count,
400 MD_MAX_BRW_SIZE >> LNET_MTU_BITS,
401 PTLRPC_BULK_GET_SOURCE | PTLRPC_BULK_BUF_KVEC,
402 MDS_BULK_PORTAL, &ptlrpc_bulk_kvec_ops);
404 GOTO(out_req, rc = -ENOMEM);
406 /* NB req now owns desc and will free it when it gets freed */
407 list_for_each_entry(ours, &our->our_req_list, ours_list) {
408 desc->bd_frag_ops->add_iov_frag(desc, ours->ours_req,
409 ours->ours_req_size);
410 total += ours->ours_req_size;
412 CDEBUG(D_OTHER, "total %d in %u\n", total, our->our_update_nr);
414 req_capsule_set_size(&req->rq_pill, &RMF_OUT_UPDATE_REPLY,
415 RCL_SERVER, repsize);
417 ptlrpc_request_set_replen(req);
418 req->rq_request_portal = OUT_PORTAL;
419 req->rq_reply_portal = OSC_REPLY_PORTAL;
424 ptlrpc_req_finished(req);
432 * Send update request to the remote MDT synchronously.
434 * \param[in] env execution environment
435 * \param[in] imp import on which ptlrpc request will be sent
436 * \param[in] our hold all of updates which will be packed into the req
437 * \param[in] reqp request to be created
439 * \retval 0 if RPC succeeds.
440 * \retval negative errno if RPC fails.
442 int osp_remote_sync(const struct lu_env *env, struct osp_device *osp,
443 struct osp_update_request *our,
444 struct ptlrpc_request **reqp)
446 struct obd_import *imp = osp->opd_obd->u.cli.cl_import;
447 struct ptlrpc_request *req = NULL;
451 rc = osp_prep_update_req(env, imp, our, &req);
455 /* This will only be called with read-only update, and these updates
456 * might be used to retrieve update log during recovery process, so
457 * it will be allowed to send during recovery process */
458 req->rq_allow_replay = 1;
459 req->rq_allow_intr = 1;
461 /* Note: some dt index api might return non-zero result here, like
462 * osd_index_ea_lookup, so we should only check rc < 0 here */
463 rc = ptlrpc_queue_wait(req);
465 if (rc < 0 || reqp == NULL)
466 ptlrpc_req_finished(req);
474 * Invalidate all objects in the osp thandle
476 * invalidate all of objects in the update request, which will be called
477 * when the transaction is aborted.
479 * \param[in] oth osp thandle.
481 static void osp_thandle_invalidate_object(const struct lu_env *env,
482 struct osp_thandle *oth)
484 struct osp_update_request *our = oth->ot_our;
485 struct osp_update_request_sub *ours;
490 list_for_each_entry(ours, &our->our_req_list, ours_list) {
491 struct object_update_request *our_req = ours->ours_req;
493 struct lu_object *obj;
495 for (i = 0; i < our_req->ourq_count; i++) {
496 struct object_update *update;
498 update = object_update_request_get(our_req, i, NULL);
502 if (update->ou_type != OUT_WRITE)
505 if (!fid_is_sane(&update->ou_fid))
508 obj = lu_object_find_slice(env,
509 &oth->ot_super.th_dev->dd_lu_dev,
510 &update->ou_fid, NULL);
514 osp_invalidate(env, lu2dt_obj(obj));
515 lu_object_put(env, obj);
520 static void osp_trans_stop_cb(const struct lu_env *env,
521 struct osp_thandle *oth, int result)
523 struct dt_txn_commit_cb *dcb;
524 struct dt_txn_commit_cb *tmp;
526 /* call per-transaction stop callbacks if any */
527 list_for_each_entry_safe(dcb, tmp, &oth->ot_stop_dcb_list,
529 LASSERTF(dcb->dcb_magic == TRANS_COMMIT_CB_MAGIC,
530 "commit callback entry: magic=%x name='%s'\n",
531 dcb->dcb_magic, dcb->dcb_name);
532 list_del_init(&dcb->dcb_linkage);
533 dcb->dcb_func(NULL, &oth->ot_super, dcb, result);
537 osp_thandle_invalidate_object(env, oth);
541 * Allocate an osp request and initialize it with the given parameters.
543 * \param[in] obj pointer to the operation target
544 * \param[in] data pointer to the data used by the interpreter
545 * \param[in] interpreter pointer to the interpreter function
547 * \retval pointer to the asychronous request
548 * \retval NULL if the allocation failed
550 static struct osp_update_callback *
551 osp_update_callback_init(struct osp_object *obj, void *data,
552 osp_update_interpreter_t interpreter)
554 struct osp_update_callback *ouc;
560 lu_object_get(osp2lu_obj(obj));
561 INIT_LIST_HEAD(&ouc->ouc_list);
563 ouc->ouc_data = data;
564 ouc->ouc_interpreter = interpreter;
570 * Destroy the osp_update_callback.
572 * \param[in] env pointer to the thread context
573 * \param[in] ouc pointer to osp_update_callback
575 static void osp_update_callback_fini(const struct lu_env *env,
576 struct osp_update_callback *ouc)
578 LASSERT(list_empty(&ouc->ouc_list));
580 lu_object_put(env, osp2lu_obj(ouc->ouc_obj));
585 * Interpret the packaged OUT RPC results.
587 * For every packaged sub-request, call its registered interpreter function.
588 * Then destroy the sub-request.
590 * \param[in] env pointer to the thread context
591 * \param[in] req pointer to the RPC
592 * \param[in] arg pointer to data used by the interpreter
593 * \param[in] rc the RPC return value
595 * \retval 0 for success
596 * \retval negative error number on failure
598 static int osp_update_interpret(const struct lu_env *env,
599 struct ptlrpc_request *req, void *arg, int rc)
601 struct object_update_reply *reply = NULL;
602 struct osp_update_args *oaua = arg;
603 struct osp_update_request *our = oaua->oaua_update;
604 struct osp_thandle *oth;
605 struct osp_update_callback *ouc;
606 struct osp_update_callback *next;
616 /* Sigh env might be NULL in some cases, see
618 * osp_send_update_thread()
619 * ptlrpc_set_wait() ----> null env.
621 * osp_update_interpret()
622 * Let's use env in oaua for this case.
625 env = oaua->oaua_update_env;
627 oaua->oaua_update = NULL;
629 if (oaua->oaua_flow_control) {
630 struct osp_device *osp;
632 LASSERT(oth != NULL);
633 osp = dt2osp_dev(oth->ot_super.th_dev);
634 obd_put_request_slot(&osp->opd_obd->u.cli);
637 /* Unpack the results from the reply message. */
638 if (req->rq_repmsg != NULL && req->rq_replied) {
639 reply = req_capsule_server_sized_get(&req->rq_pill,
640 &RMF_OUT_UPDATE_REPLY,
641 OUT_UPDATE_REPLY_SIZE);
642 if (reply == NULL || reply->ourp_magic != UPDATE_REPLY_MAGIC) {
646 count = reply->ourp_count;
650 list_for_each_entry_safe(ouc, next, &our->our_cb_items, ouc_list) {
651 list_del_init(&ouc->ouc_list);
653 /* The peer may only have handled some requests (indicated
654 * by the 'count') in the packaged OUT RPC, we can only get
655 * results for the handled part. */
656 if (index < count && reply->ourp_lens[index] > 0 && rc >= 0) {
657 struct object_update_result *result;
659 result = object_update_result_get(reply, index, NULL);
663 rc1 = rc = result->our_rc;
664 } else if (rc1 >= 0) {
665 /* The peer did not handle these request, let's return
666 * -EINVAL to update interpret for now */
673 if (ouc->ouc_interpreter != NULL)
674 ouc->ouc_interpreter(env, reply, req, ouc->ouc_obj,
675 ouc->ouc_data, index, rc1);
677 osp_update_callback_fini(env, ouc);
681 if (oaua->oaua_count != NULL && atomic_dec_and_test(oaua->oaua_count))
682 wake_up_all(oaua->oaua_waitq);
685 /* oth and osp_update_requests will be destoryed in
687 osp_trans_stop_cb(env, oth, rc);
688 osp_thandle_put(oth);
690 osp_update_request_destroy(our);
697 * Pack all the requests in the shared asynchronous idempotent request queue
698 * into a single OUT RPC that will be given to the background ptlrpcd daemon.
700 * \param[in] env pointer to the thread context
701 * \param[in] osp pointer to the OSP device
702 * \param[in] our pointer to the shared queue
704 * \retval 0 for success
705 * \retval negative error number on failure
707 int osp_unplug_async_request(const struct lu_env *env,
708 struct osp_device *osp,
709 struct osp_update_request *our)
711 struct osp_update_args *args;
712 struct ptlrpc_request *req = NULL;
715 rc = osp_prep_update_req(env, osp->opd_obd->u.cli.cl_import,
718 struct osp_update_callback *ouc;
719 struct osp_update_callback *next;
721 list_for_each_entry_safe(ouc, next,
722 &our->our_cb_items, ouc_list) {
723 list_del_init(&ouc->ouc_list);
724 if (ouc->ouc_interpreter != NULL)
725 ouc->ouc_interpreter(env, NULL, NULL,
727 ouc->ouc_data, 0, rc);
728 osp_update_callback_fini(env, ouc);
730 osp_update_request_destroy(our);
732 args = ptlrpc_req_async_args(req);
733 args->oaua_update = our;
734 args->oaua_count = NULL;
735 args->oaua_waitq = NULL;
736 /* Note: this is asynchronous call for the request, so the
737 * interrupte cb and current function will be different
738 * thread, so we need use different env */
739 args->oaua_update_env = NULL;
740 args->oaua_flow_control = false;
741 req->rq_interpret_reply = osp_update_interpret;
742 ptlrpcd_add_req(req);
749 * Find or create (if NOT exist or purged) the shared asynchronous idempotent
750 * request queue - osp_device::opd_async_requests.
752 * If the osp_device::opd_async_requests is not NULL, then return it directly;
753 * otherwise create new osp_update_request and attach it to opd_async_requests.
755 * \param[in] osp pointer to the OSP device
757 * \retval pointer to the shared queue
758 * \retval negative error number on failure
760 static struct osp_update_request *
761 osp_find_or_create_async_update_request(struct osp_device *osp)
763 struct osp_update_request *our = osp->opd_async_requests;
768 our = osp_update_request_create(&osp->opd_dt_dev);
772 osp->opd_async_requests = our;
778 * Insert an osp_update_callback into the osp_update_request.
780 * Insert an osp_update_callback to the osp_update_request. Usually each update
781 * in the osp_update_request will have one correspondent callback, and these
782 * callbacks will be called in rq_interpret_reply.
784 * \param[in] env pointer to the thread context
785 * \param[in] obj pointer to the operation target object
786 * \param[in] data pointer to the data used by the interpreter
787 * \param[in] interpreter pointer to the interpreter function
789 * \retval 0 for success
790 * \retval negative error number on failure
792 int osp_insert_update_callback(const struct lu_env *env,
793 struct osp_update_request *our,
794 struct osp_object *obj, void *data,
795 osp_update_interpreter_t interpreter)
797 struct osp_update_callback *ouc;
799 ouc = osp_update_callback_init(obj, data, interpreter);
803 list_add_tail(&ouc->ouc_list, &our->our_cb_items);
809 * Insert an asynchronous idempotent request to the shared request queue that
810 * is attached to the osp_device.
812 * This function generates a new osp_async_request with the given parameters,
813 * then tries to insert the request into the osp_device-based shared request
814 * queue. If the queue is full, then triggers the packaged OUT RPC to purge
815 * the shared queue firstly, and then re-tries.
817 * NOTE: must hold the osp::opd_async_requests_mutex to serialize concurrent
818 * osp_insert_async_request call from others.
820 * \param[in] env pointer to the thread context
821 * \param[in] op operation type, see 'enum update_type'
822 * \param[in] obj pointer to the operation target
823 * \param[in] count array size of the subsequent \a lens and \a bufs
824 * \param[in] lens buffer length array for the subsequent \a bufs
825 * \param[in] bufs the buffers to compose the request
826 * \param[in] data pointer to the data used by the interpreter
827 * \param[in] repsize how many bytes the caller allocated for \a data
828 * \param[in] interpreter pointer to the interpreter function
830 * \retval 0 for success
831 * \retval negative error number on failure
833 int osp_insert_async_request(const struct lu_env *env, enum update_type op,
834 struct osp_object *obj, int count,
835 __u16 *lens, const void **bufs,
836 void *data, __u32 repsize,
837 osp_update_interpreter_t interpreter)
839 struct osp_device *osp;
840 struct osp_update_request *our;
841 struct object_update *object_update;
842 size_t max_update_size;
843 struct object_update_request *ureq;
844 struct osp_update_request_sub *ours;
848 osp = lu2osp_dev(osp2lu_obj(obj)->lo_dev);
849 our = osp_find_or_create_async_update_request(osp);
851 RETURN(PTR_ERR(our));
854 ours = osp_current_object_update_request(our);
856 ureq = ours->ours_req;
857 max_update_size = ours->ours_req_size -
858 object_update_request_size(ureq);
860 object_update = update_buffer_get_update(ureq, ureq->ourq_count);
861 rc = out_update_pack(env, object_update, &max_update_size, op,
862 lu_object_fid(osp2lu_obj(obj)), count, lens, bufs,
864 /* The queue is full. */
866 osp->opd_async_requests = NULL;
867 mutex_unlock(&osp->opd_async_requests_mutex);
869 rc = osp_unplug_async_request(env, osp, our);
870 mutex_lock(&osp->opd_async_requests_mutex);
874 our = osp_find_or_create_async_update_request(osp);
876 RETURN(PTR_ERR(our));
884 our->our_update_nr++;
887 rc = osp_insert_update_callback(env, our, obj, data, interpreter);
892 int osp_trans_update_request_create(struct thandle *th)
894 struct osp_thandle *oth = thandle_to_osp_thandle(th);
895 struct osp_update_request *our;
897 if (oth->ot_our != NULL)
900 our = osp_update_request_create(th->th_dev);
902 th->th_result = PTR_ERR(our);
912 void osp_thandle_destroy(struct osp_thandle *oth)
914 LASSERT(oth->ot_magic == OSP_THANDLE_MAGIC);
915 LASSERT(list_empty(&oth->ot_commit_dcb_list));
916 LASSERT(list_empty(&oth->ot_stop_dcb_list));
917 if (oth->ot_our != NULL)
918 osp_update_request_destroy(oth->ot_our);
923 * The OSP layer dt_device_operations::dt_trans_create() interface
924 * to create a transaction.
926 * There are two kinds of transactions that will involve OSP:
928 * 1) If the transaction only contains the updates on remote server
929 * (MDT or OST), such as re-generating the lost OST-object for
930 * LFSCK, then it is a remote transaction. For remote transaction,
931 * the upper layer caller (such as the LFSCK engine) will call the
932 * dt_trans_create() (with the OSP dt_device as the parameter),
933 * then the call will be directed to the osp_trans_create() that
934 * creates the transaction handler and returns it to the caller.
936 * 2) If the transcation contains both local and remote updates,
937 * such as cross MDTs create under DNE mode, then the upper layer
938 * caller will not trigger osp_trans_create(). Instead, it will
939 * call dt_trans_create() on other dt_device, such as LOD that
940 * will generate the transaction handler. Such handler will be
941 * used by the whole transaction in subsequent sub-operations.
943 * \param[in] env pointer to the thread context
944 * \param[in] d pointer to the OSP dt_device
946 * \retval pointer to the transaction handler
947 * \retval negative error number on failure
949 struct thandle *osp_trans_create(const struct lu_env *env, struct dt_device *d)
951 struct osp_thandle *oth;
952 struct thandle *th = NULL;
956 if (unlikely(oth == NULL))
957 RETURN(ERR_PTR(-ENOMEM));
959 oth->ot_magic = OSP_THANDLE_MAGIC;
962 th->th_tags = LCT_TX_HANDLE;
964 atomic_set(&oth->ot_refcount, 1);
965 INIT_LIST_HEAD(&oth->ot_commit_dcb_list);
966 INIT_LIST_HEAD(&oth->ot_stop_dcb_list);
972 * Add commit callback to transaction.
974 * Add commit callback to the osp thandle, which will be called
975 * when the thandle is committed remotely.
977 * \param[in] th the thandle
978 * \param[in] dcb commit callback structure
980 * \retval only return 0 for now.
982 int osp_trans_cb_add(struct thandle *th, struct dt_txn_commit_cb *dcb)
984 struct osp_thandle *oth = thandle_to_osp_thandle(th);
986 LASSERT(dcb->dcb_magic == TRANS_COMMIT_CB_MAGIC);
987 LASSERT(&dcb->dcb_func != NULL);
988 if (dcb->dcb_flags & DCB_TRANS_STOP)
989 list_add(&dcb->dcb_linkage, &oth->ot_stop_dcb_list);
991 list_add(&dcb->dcb_linkage, &oth->ot_commit_dcb_list);
995 static void osp_trans_commit_cb(struct osp_thandle *oth, int result)
997 struct dt_txn_commit_cb *dcb;
998 struct dt_txn_commit_cb *tmp;
1000 LASSERT(atomic_read(&oth->ot_refcount) > 0);
1001 /* call per-transaction callbacks if any */
1002 list_for_each_entry_safe(dcb, tmp, &oth->ot_commit_dcb_list,
1004 LASSERTF(dcb->dcb_magic == TRANS_COMMIT_CB_MAGIC,
1005 "commit callback entry: magic=%x name='%s'\n",
1006 dcb->dcb_magic, dcb->dcb_name);
1007 list_del_init(&dcb->dcb_linkage);
1008 dcb->dcb_func(NULL, &oth->ot_super, dcb, result);
1012 static void osp_request_commit_cb(struct ptlrpc_request *req)
1014 struct thandle *th = req->rq_cb_data;
1015 struct osp_thandle *oth;
1016 __u64 last_committed_transno = 0;
1017 int result = req->rq_status;
1023 oth = thandle_to_osp_thandle(th);
1024 if (req->rq_repmsg != NULL &&
1025 lustre_msg_get_last_committed(req->rq_repmsg))
1026 last_committed_transno =
1027 lustre_msg_get_last_committed(req->rq_repmsg);
1029 if (last_committed_transno <
1030 req->rq_import->imp_peer_committed_transno)
1031 last_committed_transno =
1032 req->rq_import->imp_peer_committed_transno;
1034 CDEBUG(D_HA, "trans no "LPU64" committed transno "LPU64"\n",
1035 req->rq_transno, last_committed_transno);
1037 /* If the transaction is not really committed, mark result = 1 */
1038 if (req->rq_transno != 0 &&
1039 (req->rq_transno > last_committed_transno) && result == 0)
1042 osp_trans_commit_cb(oth, result);
1043 req->rq_committed = 1;
1044 osp_thandle_put(oth);
1049 * callback of osp transaction
1051 * Call all of callbacks for this osp thandle. This will only be
1052 * called in error handler path. In the normal processing path,
1053 * these callback will be called in osp_request_commit_cb() and
1054 * osp_update_interpret().
1056 * \param [in] env execution environment
1057 * \param [in] oth osp thandle
1058 * \param [in] rc result of the osp thandle
1060 void osp_trans_callback(const struct lu_env *env,
1061 struct osp_thandle *oth, int rc)
1063 struct osp_update_callback *ouc;
1064 struct osp_update_callback *next;
1066 if (oth->ot_our != NULL) {
1067 list_for_each_entry_safe(ouc, next,
1068 &oth->ot_our->our_cb_items, ouc_list) {
1069 list_del_init(&ouc->ouc_list);
1070 if (ouc->ouc_interpreter != NULL)
1071 ouc->ouc_interpreter(env, NULL, NULL,
1073 ouc->ouc_data, 0, rc);
1074 osp_update_callback_fini(env, ouc);
1077 osp_trans_stop_cb(env, oth, rc);
1078 osp_trans_commit_cb(oth, rc);
1082 * Send the request for remote updates.
1084 * Send updates to the remote MDT. Prepare the request by osp_update_req
1085 * and send them to remote MDT, for sync request, it will wait
1086 * until the reply return, otherwise hand it to ptlrpcd.
1088 * Please refer to osp_trans_create() for transaction type.
1090 * \param[in] env pointer to the thread context
1091 * \param[in] osp pointer to the OSP device
1092 * \param[in] our pointer to the osp_update_request
1094 * \retval 0 for success
1095 * \retval negative error number on failure
1097 static int osp_send_update_req(const struct lu_env *env,
1098 struct osp_device *osp,
1099 struct osp_update_request *our)
1101 struct osp_update_args *args;
1102 struct ptlrpc_request *req;
1103 struct lu_device *top_device;
1104 struct osp_thandle *oth = our->our_th;
1108 LASSERT(oth != NULL);
1109 rc = osp_prep_update_req(env, osp->opd_obd->u.cli.cl_import,
1112 osp_trans_callback(env, oth, rc);
1116 args = ptlrpc_req_async_args(req);
1117 args->oaua_update = our;
1118 /* set env to NULL, in case the interrupt cb and current function
1119 * are in different thread */
1120 args->oaua_update_env = NULL;
1121 osp_thandle_get(oth); /* hold for update interpret */
1122 req->rq_interpret_reply = osp_update_interpret;
1123 if (!oth->ot_super.th_wait_submit && !oth->ot_super.th_sync) {
1124 if (!osp->opd_imp_active || !osp->opd_imp_connected) {
1125 osp_trans_callback(env, oth, rc);
1126 osp_thandle_put(oth);
1127 GOTO(out, rc = -ENOTCONN);
1130 rc = obd_get_request_slot(&osp->opd_obd->u.cli);
1132 osp_trans_callback(env, oth, rc);
1133 osp_thandle_put(oth);
1134 GOTO(out, rc = -ENOTCONN);
1136 args->oaua_flow_control = true;
1138 if (!osp->opd_connect_mdt) {
1139 down_read(&osp->opd_async_updates_rwsem);
1140 args->oaua_count = &osp->opd_async_updates_count;
1141 args->oaua_waitq = &osp->opd_syn_barrier_waitq;
1142 up_read(&osp->opd_async_updates_rwsem);
1143 atomic_inc(args->oaua_count);
1146 ptlrpcd_add_req(req);
1149 osp_thandle_get(oth); /* hold for commit callback */
1150 req->rq_commit_cb = osp_request_commit_cb;
1151 req->rq_cb_data = &oth->ot_super;
1152 args->oaua_flow_control = false;
1154 /* If the transaction is created during MDT recoverying
1155 * process, it means this is an recovery update, we need
1156 * to let OSP send it anyway without checking recoverying
1157 * status, in case the other target is being recoveried
1158 * at the same time, and if we wait here for the import
1159 * to be recoveryed, it might cause deadlock */
1160 top_device = osp->opd_dt_dev.dd_lu_dev.ld_site->ls_top_dev;
1161 if (top_device->ld_obd->obd_recovering)
1162 req->rq_allow_replay = 1;
1164 /* Because this req will be synchronus, i.e. it will be called
1165 * in the same thread, so it will be safe to use current
1167 args->oaua_update_env = env;
1168 if (osp->opd_connect_mdt)
1169 osp_get_rpc_lock(osp);
1170 rc = ptlrpc_queue_wait(req);
1171 if (osp->opd_connect_mdt)
1172 osp_put_rpc_lock(osp);
1173 if ((rc == -ENOMEM && req->rq_set == NULL) ||
1174 (req->rq_transno == 0 && !req->rq_committed)) {
1175 if (args->oaua_update != NULL) {
1176 /* If osp_update_interpret is not being called,
1177 * release the osp_thandle */
1178 args->oaua_update = NULL;
1179 osp_thandle_put(oth);
1182 req->rq_cb_data = NULL;
1183 rc = rc == 0 ? req->rq_status : rc;
1184 osp_trans_callback(env, oth, rc);
1185 osp_thandle_put(oth);
1191 ptlrpc_req_finished(req);
1197 * Get local thandle for osp_thandle
1199 * Get the local OSD thandle from the OSP thandle. Currently, there
1200 * are a few OSP API (osp_object_create() and osp_sync_add()) needs
1201 * to update the object on local OSD device.
1203 * If the osp_thandle comes from normal stack (MDD->LOD->OSP), then
1204 * we will get local thandle by thandle_get_sub_by_dt.
1206 * If the osp_thandle is remote thandle (th_top == NULL, only used
1207 * by LFSCK), then it will create a local thandle, and stop it in
1208 * osp_trans_stop(). And this only happens on OSP for OST.
1210 * These are temporary solution, once OSP accessing OSD object is
1211 * being fixed properly, this function should be removed. XXX
1213 * \param[in] env pointer to the thread context
1214 * \param[in] th pointer to the transaction handler
1215 * \param[in] dt pointer to the OSP device
1217 * \retval pointer to the local thandle
1218 * \retval ERR_PTR(errno) if it fails.
1220 struct thandle *osp_get_storage_thandle(const struct lu_env *env,
1222 struct osp_device *osp)
1224 struct osp_thandle *oth;
1225 struct thandle *local_th;
1227 if (th->th_top != NULL)
1228 return thandle_get_sub_by_dt(env, th->th_top,
1231 LASSERT(!osp->opd_connect_mdt);
1232 oth = thandle_to_osp_thandle(th);
1233 if (oth->ot_storage_th != NULL)
1234 return oth->ot_storage_th;
1236 local_th = dt_trans_create(env, osp->opd_storage);
1237 if (IS_ERR(local_th))
1240 oth->ot_storage_th = local_th;
1246 * Set version for the transaction
1248 * Set the version for the transaction and add the request to
1249 * the sending list, then after transaction stop, the request
1250 * will be picked in the order of version, by sending thread.
1252 * \param [in] oth osp thandle to be set version.
1254 * \retval 0 if set version succeeds
1255 * negative errno if set version fails.
1257 int osp_check_and_set_rpc_version(struct osp_thandle *oth,
1258 struct osp_object *obj)
1260 struct osp_device *osp = dt2osp_dev(oth->ot_super.th_dev);
1261 struct osp_updates *ou = osp->opd_update;
1266 if (oth->ot_our->our_version != 0)
1269 spin_lock(&ou->ou_lock);
1270 spin_lock(&oth->ot_our->our_list_lock);
1271 if (obj->opo_stale) {
1272 spin_unlock(&oth->ot_our->our_list_lock);
1273 spin_unlock(&ou->ou_lock);
1277 /* Assign the version and add it to the sending list */
1278 osp_thandle_get(oth);
1279 oth->ot_our->our_version = ou->ou_version++;
1280 list_add_tail(&oth->ot_our->our_list,
1281 &osp->opd_update->ou_list);
1282 oth->ot_our->our_req_ready = 0;
1283 spin_unlock(&oth->ot_our->our_list_lock);
1284 spin_unlock(&ou->ou_lock);
1286 LASSERT(oth->ot_super.th_wait_submit == 1);
1287 CDEBUG(D_INFO, "%s: version "LPU64" oth:version %p:"LPU64"\n",
1288 osp->opd_obd->obd_name, ou->ou_version, oth,
1289 oth->ot_our->our_version);
1295 * Get next OSP update request in the sending list
1296 * Get next OSP update request in the sending list by version number, next
1298 * 1. transaction which does not have a version number.
1299 * 2. transaction whose version == opd_rpc_version.
1301 * \param [in] ou osp update structure.
1302 * \param [out] ourp the pointer holding the next update request.
1304 * \retval true if getting the next transaction.
1305 * \retval false if not getting the next transaction.
1308 osp_get_next_request(struct osp_updates *ou, struct osp_update_request **ourp)
1310 struct osp_update_request *our;
1311 struct osp_update_request *tmp;
1312 bool got_req = false;
1314 spin_lock(&ou->ou_lock);
1315 list_for_each_entry_safe(our, tmp, &ou->ou_list, our_list) {
1316 LASSERT(our->our_th != NULL);
1317 CDEBUG(D_HA, "ou %p version "LPU64" rpc_version "LPU64"\n",
1318 ou, our->our_version, ou->ou_rpc_version);
1319 spin_lock(&our->our_list_lock);
1320 /* Find next osp_update_request in the list */
1321 if (our->our_version == ou->ou_rpc_version &&
1322 our->our_req_ready) {
1323 list_del_init(&our->our_list);
1324 spin_unlock(&our->our_list_lock);
1329 spin_unlock(&our->our_list_lock);
1331 spin_unlock(&ou->ou_lock);
1337 * Invalidate update request
1339 * Invalidate update request in the OSP sending list, so all of
1340 * requests in the sending list will return error, which happens
1341 * when it finds one update (with writing llog) requests fails or
1342 * the OSP is evicted by remote target. see osp_send_update_thread().
1344 * \param[in] osp OSP device whose update requests will be
1347 void osp_invalidate_request(struct osp_device *osp)
1350 struct osp_updates *ou = osp->opd_update;
1351 struct osp_update_request *our;
1352 struct osp_update_request *tmp;
1360 rc = lu_env_init(&env, osp->opd_dt_dev.dd_lu_dev.ld_type->ldt_ctx_tags);
1362 CERROR("%s: init env error: rc = %d\n", osp->opd_obd->obd_name,
1367 INIT_LIST_HEAD(&list);
1369 spin_lock(&ou->ou_lock);
1370 /* invalidate all of request in the sending list */
1371 list_for_each_entry_safe(our, tmp, &ou->ou_list, our_list) {
1372 spin_lock(&our->our_list_lock);
1373 if (our->our_req_ready)
1374 list_move(&our->our_list, &list);
1376 list_del_init(&our->our_list);
1378 if (our->our_th->ot_super.th_result == 0)
1379 our->our_th->ot_super.th_result = -EIO;
1381 if (our->our_version >= ou->ou_rpc_version)
1382 ou->ou_rpc_version = our->our_version + 1;
1383 spin_unlock(&our->our_list_lock);
1385 CDEBUG(D_HA, "%s invalidate our %p\n", osp->opd_obd->obd_name,
1389 spin_unlock(&ou->ou_lock);
1391 /* invalidate all of request in the sending list */
1392 list_for_each_entry_safe(our, tmp, &list, our_list) {
1393 spin_lock(&our->our_list_lock);
1394 list_del_init(&our->our_list);
1395 spin_unlock(&our->our_list_lock);
1396 osp_trans_callback(&env, our->our_th,
1397 our->our_th->ot_super.th_result);
1398 osp_thandle_put(our->our_th);
1404 * Sending update thread
1406 * Create thread to send update request to other MDTs, this thread will pull
1407 * out update request from the list in OSP by version number, i.e. it will
1408 * make sure the update request with lower version number will be sent first.
1410 * \param[in] arg hold the OSP device.
1412 * \retval 0 if the thread is created successfully.
1413 * \retal negative error if the thread is not created
1416 int osp_send_update_thread(void *arg)
1419 struct osp_device *osp = arg;
1420 struct l_wait_info lwi = { 0 };
1421 struct osp_updates *ou = osp->opd_update;
1422 struct ptlrpc_thread *thread = &osp->opd_update_thread;
1423 struct osp_update_request *our = NULL;
1427 LASSERT(ou != NULL);
1428 rc = lu_env_init(&env, osp->opd_dt_dev.dd_lu_dev.ld_type->ldt_ctx_tags);
1430 CERROR("%s: init env error: rc = %d\n", osp->opd_obd->obd_name,
1435 thread->t_flags = SVC_RUNNING;
1436 wake_up(&thread->t_ctl_waitq);
1439 l_wait_event(ou->ou_waitq,
1440 !osp_send_update_thread_running(osp) ||
1441 osp_get_next_request(ou, &our), &lwi);
1443 if (!osp_send_update_thread_running(osp)) {
1445 osp_trans_callback(&env, our->our_th, -EINTR);
1446 osp_thandle_put(our->our_th);
1451 LASSERT(our->our_th != NULL);
1452 if (our->our_th->ot_super.th_result != 0) {
1453 osp_trans_callback(&env, our->our_th,
1454 our->our_th->ot_super.th_result);
1455 rc = our->our_th->ot_super.th_result;
1456 } else if (OBD_FAIL_CHECK(OBD_FAIL_INVALIDATE_UPDATE)) {
1458 osp_trans_callback(&env, our->our_th, rc);
1460 rc = osp_send_update_req(&env, osp, our);
1463 /* Update the rpc version */
1464 spin_lock(&ou->ou_lock);
1465 if (our->our_version == ou->ou_rpc_version)
1466 ou->ou_rpc_version++;
1467 spin_unlock(&ou->ou_lock);
1469 /* If one update request fails, let's fail all of the requests
1470 * in the sending list, because the request in the sending
1471 * list are dependent on either other, continue sending these
1472 * request might cause llog or filesystem corruption */
1474 osp_invalidate_request(osp);
1476 /* Balanced for thandle_get in osp_check_and_set_rpc_version */
1477 osp_thandle_put(our->our_th);
1480 thread->t_flags = SVC_STOPPED;
1482 wake_up(&thread->t_ctl_waitq);
1488 * The OSP layer dt_device_operations::dt_trans_start() interface
1489 * to start the transaction.
1491 * If the transaction is a remote transaction, then related remote
1492 * updates will be triggered in the osp_trans_stop().
1493 * Please refer to osp_trans_create() for transaction type.
1495 * \param[in] env pointer to the thread context
1496 * \param[in] dt pointer to the OSP dt_device
1497 * \param[in] th pointer to the transaction handler
1499 * \retval 0 for success
1500 * \retval negative error number on failure
1502 int osp_trans_start(const struct lu_env *env, struct dt_device *dt,
1505 struct osp_thandle *oth = thandle_to_osp_thandle(th);
1507 if (oth->ot_super.th_sync)
1508 oth->ot_our->our_flags |= UPDATE_FL_SYNC;
1509 /* For remote thandle, if there are local thandle, start it here*/
1510 if (is_only_remote_trans(th) && oth->ot_storage_th != NULL)
1511 return dt_trans_start(env, oth->ot_storage_th->th_dev,
1512 oth->ot_storage_th);
1517 * The OSP layer dt_device_operations::dt_trans_stop() interface
1518 * to stop the transaction.
1520 * If the transaction is a remote transaction, related remote
1521 * updates will be triggered at the end of this function.
1523 * For synchronous mode update or any failed update, the request
1524 * will be destroyed explicitly when the osp_trans_stop().
1526 * Please refer to osp_trans_create() for transaction type.
1528 * \param[in] env pointer to the thread context
1529 * \param[in] dt pointer to the OSP dt_device
1530 * \param[in] th pointer to the transaction handler
1532 * \retval 0 for success
1533 * \retval negative error number on failure
1535 int osp_trans_stop(const struct lu_env *env, struct dt_device *dt,
1538 struct osp_thandle *oth = thandle_to_osp_thandle(th);
1539 struct osp_update_request *our = oth->ot_our;
1540 struct osp_device *osp = dt2osp_dev(dt);
1544 /* For remote transaction, if there is local storage thandle,
1546 if (oth->ot_storage_th != NULL && th->th_top == NULL) {
1547 dt_trans_stop(env, oth->ot_storage_th->th_dev,
1548 oth->ot_storage_th);
1549 oth->ot_storage_th = NULL;
1552 if (our == NULL || list_empty(&our->our_req_list)) {
1553 osp_trans_callback(env, oth, th->th_result);
1554 GOTO(out, rc = th->th_result);
1557 if (!osp->opd_connect_mdt) {
1558 osp_trans_callback(env, oth, th->th_result);
1559 rc = osp_send_update_req(env, osp, oth->ot_our);
1563 if (osp->opd_update == NULL ||
1564 !osp_send_update_thread_running(osp)) {
1565 osp_trans_callback(env, oth, -EIO);
1566 GOTO(out, rc = -EIO);
1569 CDEBUG(D_HA, "%s: add oth %p with version "LPU64"\n",
1570 osp->opd_obd->obd_name, oth, our->our_version);
1572 LASSERT(our->our_req_ready == 0);
1573 spin_lock(&our->our_list_lock);
1574 if (likely(!list_empty(&our->our_list))) {
1575 /* notify sending thread */
1576 our->our_req_ready = 1;
1577 wake_up(&osp->opd_update->ou_waitq);
1578 spin_unlock(&our->our_list_lock);
1579 } else if (th->th_result == 0) {
1580 /* if the request does not needs to be serialized,
1581 * read-only request etc, let's send it right away */
1582 spin_unlock(&our->our_list_lock);
1583 rc = osp_send_update_req(env, osp, our);
1585 spin_unlock(&our->our_list_lock);
1586 osp_trans_callback(env, oth, th->th_result);
1589 osp_thandle_put(oth);