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, 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;
83 * Call back for each update request.
85 struct osp_update_callback {
86 /* list in the osp_update_request::our_cb_items */
87 struct list_head ouc_list;
89 /* The target of the async update request. */
90 struct osp_object *ouc_obj;
92 /* The data used by or_interpreter. */
95 /* The interpreter function called after the async request handled. */
96 osp_update_interpreter_t ouc_interpreter;
99 static struct object_update_request *object_update_request_alloc(size_t size)
101 struct object_update_request *ourq;
103 OBD_ALLOC_LARGE(ourq, size);
105 return ERR_PTR(-ENOMEM);
107 ourq->ourq_magic = UPDATE_REQUEST_MAGIC;
108 ourq->ourq_count = 0;
114 * Allocate new update request
116 * Allocate new update request and insert it to the req_update_list.
118 * \param [in] our osp_udate_request where to create a new
121 * \retval 0 if creation succeeds.
122 * \retval negative errno if creation fails.
124 int osp_object_update_request_create(struct osp_update_request *our,
127 struct osp_update_request_sub *ours;
133 if (size < OUT_UPDATE_INIT_BUFFER_SIZE)
134 size = OUT_UPDATE_INIT_BUFFER_SIZE;
136 ours->ours_req = object_update_request_alloc(size);
138 if (IS_ERR(ours->ours_req)) {
143 ours->ours_req_size = size;
144 INIT_LIST_HEAD(&ours->ours_list);
145 list_add_tail(&ours->ours_list, &our->our_req_list);
151 * Get current update request
153 * Get current object update request from our_req_list in
154 * osp_update_request, because we always insert the new update
155 * request in the last position, so the last update request
156 * in the list will be the current update req.
158 * \param[in] our osp update request where to get the
159 * current object update.
161 * \retval the current object update.
163 struct osp_update_request_sub *
164 osp_current_object_update_request(struct osp_update_request *our)
166 if (list_empty(&our->our_req_list))
169 return list_entry(our->our_req_list.prev, struct osp_update_request_sub,
174 * Allocate and initialize osp_update_request
176 * osp_update_request is being used to track updates being executed on
177 * this dt_device(OSD or OSP). The update buffer will be 4k initially,
178 * and increased if needed.
180 * \param [in] dt dt device
182 * \retval osp_update_request being allocated if succeed
183 * \retval ERR_PTR(errno) if failed
185 struct osp_update_request *osp_update_request_create(struct dt_device *dt)
187 struct osp_update_request *our;
191 return ERR_PTR(-ENOMEM);
193 INIT_LIST_HEAD(&our->our_req_list);
194 INIT_LIST_HEAD(&our->our_cb_items);
195 INIT_LIST_HEAD(&our->our_list);
197 osp_object_update_request_create(our, OUT_UPDATE_INIT_BUFFER_SIZE);
201 void osp_update_request_destroy(struct osp_update_request *our)
203 struct osp_update_request_sub *ours;
204 struct osp_update_request_sub *tmp;
209 list_for_each_entry_safe(ours, tmp, &our->our_req_list, ours_list) {
210 list_del(&ours->ours_list);
211 if (ours->ours_req != NULL)
212 OBD_FREE(ours->ours_req, ours->ours_req_size);
219 object_update_request_dump(const struct object_update_request *ourq,
223 size_t total_size = 0;
225 for (i = 0; i < ourq->ourq_count; i++) {
226 struct object_update *update;
229 update = object_update_request_get(ourq, i, &size);
230 LASSERT(update != NULL);
231 CDEBUG(mask, "i = %u fid = "DFID" op = %s master = %u"
232 "params = %d batchid = "LPU64" size = %zu\n",
233 i, PFID(&update->ou_fid),
234 update_op_str(update->ou_type),
235 update->ou_master_index, update->ou_params_count,
236 update->ou_batchid, size);
241 CDEBUG(mask, "updates = %p magic = %x count = %d size = %zu\n", ourq,
242 ourq->ourq_magic, ourq->ourq_count, total_size);
246 * Prepare update request.
248 * Prepare OUT update ptlrpc request, and the request usually includes
249 * all of updates (stored in \param ureq) from one operation.
251 * \param[in] env execution environment
252 * \param[in] imp import on which ptlrpc request will be sent
253 * \param[in] ureq hold all of updates which will be packed into the req
254 * \param[in] reqp request to be created
256 * \retval 0 if preparation succeeds.
257 * \retval negative errno if preparation fails.
259 int osp_prep_update_req(const struct lu_env *env, struct obd_import *imp,
260 struct osp_update_request *our,
261 struct ptlrpc_request **reqp)
263 struct ptlrpc_request *req;
264 struct ptlrpc_bulk_desc *desc;
265 struct osp_update_request_sub *ours;
266 struct out_update_header *ouh;
267 struct out_update_buffer *oub;
272 list_for_each_entry(ours, &our->our_req_list, ours_list) {
273 object_update_request_dump(ours->ours_req, D_INFO);
277 req = ptlrpc_request_alloc(imp, &RQF_OUT_UPDATE);
281 req_capsule_set_size(&req->rq_pill, &RMF_OUT_UPDATE_BUF, RCL_CLIENT,
282 buf_count * sizeof(*oub));
284 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, OUT_UPDATE);
288 ouh = req_capsule_client_get(&req->rq_pill, &RMF_OUT_UPDATE_HEADER);
289 ouh->ouh_magic = OUT_UPDATE_HEADER_MAGIC;
290 ouh->ouh_count = buf_count;
292 oub = req_capsule_client_get(&req->rq_pill, &RMF_OUT_UPDATE_BUF);
293 list_for_each_entry(ours, &our->our_req_list, ours_list) {
294 oub->oub_size = ours->ours_req_size;
298 req->rq_bulk_write = 1;
299 desc = ptlrpc_prep_bulk_imp(req, buf_count,
300 MD_MAX_BRW_SIZE >> LNET_MTU_BITS,
301 PTLRPC_BULK_GET_SOURCE | PTLRPC_BULK_BUF_KVEC,
302 MDS_BULK_PORTAL, &ptlrpc_bulk_kvec_ops);
304 GOTO(out_req, rc = -ENOMEM);
306 /* NB req now owns desc and will free it when it gets freed */
307 list_for_each_entry(ours, &our->our_req_list, ours_list)
308 desc->bd_frag_ops->add_iov_frag(desc, ours->ours_req,
309 ours->ours_req_size);
311 req_capsule_set_size(&req->rq_pill, &RMF_OUT_UPDATE_REPLY,
312 RCL_SERVER, OUT_UPDATE_REPLY_SIZE);
314 ptlrpc_request_set_replen(req);
315 req->rq_request_portal = OUT_PORTAL;
316 req->rq_reply_portal = OSC_REPLY_PORTAL;
321 ptlrpc_req_finished(req);
329 * Send update request to the remote MDT synchronously.
331 * \param[in] env execution environment
332 * \param[in] imp import on which ptlrpc request will be sent
333 * \param[in] our hold all of updates which will be packed into the req
334 * \param[in] reqp request to be created
336 * \retval 0 if RPC succeeds.
337 * \retval negative errno if RPC fails.
340 int osp_remote_sync(const struct lu_env *env, struct osp_device *osp,
341 struct osp_update_request *our,
342 struct ptlrpc_request **reqp)
344 struct obd_import *imp = osp->opd_obd->u.cli.cl_import;
345 struct ptlrpc_request *req = NULL;
349 rc = osp_prep_update_req(env, imp, our, &req);
353 /* This will only be called with read-only update, and these updates
354 * might be used to retrieve update log during recovery process, so
355 * it will be allowed to send during recovery process */
356 req->rq_allow_replay = 1;
358 /* Note: some dt index api might return non-zero result here, like
359 * osd_index_ea_lookup, so we should only check rc < 0 here */
360 rc = ptlrpc_queue_wait(req);
362 if (rc < 0 || reqp == NULL)
363 ptlrpc_req_finished(req);
370 static void osp_trans_stop_cb(struct osp_thandle *oth, int result)
372 struct dt_txn_commit_cb *dcb;
373 struct dt_txn_commit_cb *tmp;
375 /* call per-transaction stop callbacks if any */
376 list_for_each_entry_safe(dcb, tmp, &oth->ot_stop_dcb_list,
378 LASSERTF(dcb->dcb_magic == TRANS_COMMIT_CB_MAGIC,
379 "commit callback entry: magic=%x name='%s'\n",
380 dcb->dcb_magic, dcb->dcb_name);
381 list_del_init(&dcb->dcb_linkage);
382 dcb->dcb_func(NULL, &oth->ot_super, dcb, result);
387 * Allocate an osp request and initialize it with the given parameters.
389 * \param[in] obj pointer to the operation target
390 * \param[in] data pointer to the data used by the interpreter
391 * \param[in] interpreter pointer to the interpreter function
393 * \retval pointer to the asychronous request
394 * \retval NULL if the allocation failed
396 static struct osp_update_callback *
397 osp_update_callback_init(struct osp_object *obj, void *data,
398 osp_update_interpreter_t interpreter)
400 struct osp_update_callback *ouc;
406 lu_object_get(osp2lu_obj(obj));
407 INIT_LIST_HEAD(&ouc->ouc_list);
409 ouc->ouc_data = data;
410 ouc->ouc_interpreter = interpreter;
416 * Destroy the osp_update_callback.
418 * \param[in] env pointer to the thread context
419 * \param[in] ouc pointer to osp_update_callback
421 static void osp_update_callback_fini(const struct lu_env *env,
422 struct osp_update_callback *ouc)
424 LASSERT(list_empty(&ouc->ouc_list));
426 lu_object_put(env, osp2lu_obj(ouc->ouc_obj));
431 * Interpret the packaged OUT RPC results.
433 * For every packaged sub-request, call its registered interpreter function.
434 * Then destroy the sub-request.
436 * \param[in] env pointer to the thread context
437 * \param[in] req pointer to the RPC
438 * \param[in] arg pointer to data used by the interpreter
439 * \param[in] rc the RPC return value
441 * \retval 0 for success
442 * \retval negative error number on failure
444 static int osp_update_interpret(const struct lu_env *env,
445 struct ptlrpc_request *req, void *arg, int rc)
447 struct object_update_reply *reply = NULL;
448 struct osp_update_args *oaua = arg;
449 struct osp_update_request *our = oaua->oaua_update;
450 struct osp_thandle *oth;
451 struct osp_update_callback *ouc;
452 struct osp_update_callback *next;
462 oaua->oaua_update = NULL;
464 if (oaua->oaua_flow_control) {
465 struct osp_device *osp;
467 LASSERT(oth != NULL);
468 osp = dt2osp_dev(oth->ot_super.th_dev);
469 obd_put_request_slot(&osp->opd_obd->u.cli);
472 /* Unpack the results from the reply message. */
473 if (req->rq_repmsg != NULL) {
474 reply = req_capsule_server_sized_get(&req->rq_pill,
475 &RMF_OUT_UPDATE_REPLY,
476 OUT_UPDATE_REPLY_SIZE);
477 if (reply == NULL || reply->ourp_magic != UPDATE_REPLY_MAGIC)
480 count = reply->ourp_count;
485 list_for_each_entry_safe(ouc, next, &our->our_cb_items, ouc_list) {
486 list_del_init(&ouc->ouc_list);
488 /* The peer may only have handled some requests (indicated
489 * by the 'count') in the packaged OUT RPC, we can only get
490 * results for the handled part. */
491 if (index < count && reply->ourp_lens[index] > 0) {
492 struct object_update_result *result;
494 result = object_update_result_get(reply, index, NULL);
498 rc1 = result->our_rc;
501 if (unlikely(rc1 == 0))
505 if (ouc->ouc_interpreter != NULL)
506 ouc->ouc_interpreter(env, reply, req, ouc->ouc_obj,
507 ouc->ouc_data, index, rc1);
509 osp_update_callback_fini(env, ouc);
513 if (oaua->oaua_count != NULL && atomic_dec_and_test(oaua->oaua_count))
514 wake_up_all(oaua->oaua_waitq);
517 /* oth and osp_update_requests will be destoryed in
519 osp_trans_stop_cb(oth, rc);
520 osp_thandle_put(oth);
522 osp_update_request_destroy(our);
529 * Pack all the requests in the shared asynchronous idempotent request queue
530 * into a single OUT RPC that will be given to the background ptlrpcd daemon.
532 * \param[in] env pointer to the thread context
533 * \param[in] osp pointer to the OSP device
534 * \param[in] our pointer to the shared queue
536 * \retval 0 for success
537 * \retval negative error number on failure
539 int osp_unplug_async_request(const struct lu_env *env,
540 struct osp_device *osp,
541 struct osp_update_request *our)
543 struct osp_update_args *args;
544 struct ptlrpc_request *req = NULL;
547 rc = osp_prep_update_req(env, osp->opd_obd->u.cli.cl_import,
550 struct osp_update_callback *ouc;
551 struct osp_update_callback *next;
553 list_for_each_entry_safe(ouc, next,
554 &our->our_cb_items, ouc_list) {
555 list_del_init(&ouc->ouc_list);
556 if (ouc->ouc_interpreter != NULL)
557 ouc->ouc_interpreter(env, NULL, NULL,
559 ouc->ouc_data, 0, rc);
560 osp_update_callback_fini(env, ouc);
562 osp_update_request_destroy(our);
564 args = ptlrpc_req_async_args(req);
565 args->oaua_update = our;
566 args->oaua_count = NULL;
567 args->oaua_waitq = NULL;
568 args->oaua_flow_control = false;
569 req->rq_interpret_reply = osp_update_interpret;
570 ptlrpcd_add_req(req);
577 * Find or create (if NOT exist or purged) the shared asynchronous idempotent
578 * request queue - osp_device::opd_async_requests.
580 * If the osp_device::opd_async_requests is not NULL, then return it directly;
581 * otherwise create new osp_update_request and attach it to opd_async_requests.
583 * \param[in] osp pointer to the OSP device
585 * \retval pointer to the shared queue
586 * \retval negative error number on failure
588 static struct osp_update_request *
589 osp_find_or_create_async_update_request(struct osp_device *osp)
591 struct osp_update_request *our = osp->opd_async_requests;
596 our = osp_update_request_create(&osp->opd_dt_dev);
600 osp->opd_async_requests = our;
606 * Insert an osp_update_callback into the osp_update_request.
608 * Insert an osp_update_callback to the osp_update_request. Usually each update
609 * in the osp_update_request will have one correspondent callback, and these
610 * callbacks will be called in rq_interpret_reply.
612 * \param[in] env pointer to the thread context
613 * \param[in] obj pointer to the operation target object
614 * \param[in] data pointer to the data used by the interpreter
615 * \param[in] interpreter pointer to the interpreter function
617 * \retval 0 for success
618 * \retval negative error number on failure
620 int osp_insert_update_callback(const struct lu_env *env,
621 struct osp_update_request *our,
622 struct osp_object *obj, void *data,
623 osp_update_interpreter_t interpreter)
625 struct osp_update_callback *ouc;
627 ouc = osp_update_callback_init(obj, data, interpreter);
631 list_add_tail(&ouc->ouc_list, &our->our_cb_items);
637 * Insert an asynchronous idempotent request to the shared request queue that
638 * is attached to the osp_device.
640 * This function generates a new osp_async_request with the given parameters,
641 * then tries to insert the request into the osp_device-based shared request
642 * queue. If the queue is full, then triggers the packaged OUT RPC to purge
643 * the shared queue firstly, and then re-tries.
645 * NOTE: must hold the osp::opd_async_requests_mutex to serialize concurrent
646 * osp_insert_async_request call from others.
648 * \param[in] env pointer to the thread context
649 * \param[in] op operation type, see 'enum update_type'
650 * \param[in] obj pointer to the operation target
651 * \param[in] count array size of the subsequent \a lens and \a bufs
652 * \param[in] lens buffer length array for the subsequent \a bufs
653 * \param[in] bufs the buffers to compose the request
654 * \param[in] data pointer to the data used by the interpreter
655 * \param[in] interpreter pointer to the interpreter function
657 * \retval 0 for success
658 * \retval negative error number on failure
660 int osp_insert_async_request(const struct lu_env *env, enum update_type op,
661 struct osp_object *obj, int count,
662 __u16 *lens, const void **bufs, void *data,
663 osp_update_interpreter_t interpreter)
665 struct osp_device *osp;
666 struct osp_update_request *our;
667 struct object_update *object_update;
668 size_t max_update_size;
669 struct object_update_request *ureq;
670 struct osp_update_request_sub *ours;
674 osp = lu2osp_dev(osp2lu_obj(obj)->lo_dev);
675 our = osp_find_or_create_async_update_request(osp);
677 RETURN(PTR_ERR(our));
680 ours = osp_current_object_update_request(our);
682 ureq = ours->ours_req;
683 max_update_size = ours->ours_req_size -
684 object_update_request_size(ureq);
686 object_update = update_buffer_get_update(ureq, ureq->ourq_count);
687 rc = out_update_pack(env, object_update, &max_update_size, op,
688 lu_object_fid(osp2lu_obj(obj)), count, lens, bufs);
689 /* The queue is full. */
691 osp->opd_async_requests = NULL;
692 mutex_unlock(&osp->opd_async_requests_mutex);
694 rc = osp_unplug_async_request(env, osp, our);
695 mutex_lock(&osp->opd_async_requests_mutex);
699 our = osp_find_or_create_async_update_request(osp);
701 RETURN(PTR_ERR(our));
711 rc = osp_insert_update_callback(env, our, obj, data, interpreter);
716 int osp_trans_update_request_create(struct thandle *th)
718 struct osp_thandle *oth = thandle_to_osp_thandle(th);
719 struct osp_update_request *our;
721 if (oth->ot_our != NULL)
724 our = osp_update_request_create(th->th_dev);
726 th->th_result = PTR_ERR(our);
736 void osp_thandle_destroy(struct osp_thandle *oth)
738 LASSERT(oth->ot_magic == OSP_THANDLE_MAGIC);
739 LASSERT(list_empty(&oth->ot_commit_dcb_list));
740 LASSERT(list_empty(&oth->ot_stop_dcb_list));
741 if (oth->ot_our != NULL)
742 osp_update_request_destroy(oth->ot_our);
747 * The OSP layer dt_device_operations::dt_trans_create() interface
748 * to create a transaction.
750 * There are two kinds of transactions that will involve OSP:
752 * 1) If the transaction only contains the updates on remote server
753 * (MDT or OST), such as re-generating the lost OST-object for
754 * LFSCK, then it is a remote transaction. For remote transaction,
755 * the upper layer caller (such as the LFSCK engine) will call the
756 * dt_trans_create() (with the OSP dt_device as the parameter),
757 * then the call will be directed to the osp_trans_create() that
758 * creates the transaction handler and returns it to the caller.
760 * 2) If the transcation contains both local and remote updates,
761 * such as cross MDTs create under DNE mode, then the upper layer
762 * caller will not trigger osp_trans_create(). Instead, it will
763 * call dt_trans_create() on other dt_device, such as LOD that
764 * will generate the transaction handler. Such handler will be
765 * used by the whole transaction in subsequent sub-operations.
767 * \param[in] env pointer to the thread context
768 * \param[in] d pointer to the OSP dt_device
770 * \retval pointer to the transaction handler
771 * \retval negative error number on failure
773 struct thandle *osp_trans_create(const struct lu_env *env, struct dt_device *d)
775 struct osp_thandle *oth;
776 struct thandle *th = NULL;
780 if (unlikely(oth == NULL))
781 RETURN(ERR_PTR(-ENOMEM));
783 oth->ot_magic = OSP_THANDLE_MAGIC;
786 th->th_tags = LCT_TX_HANDLE;
788 atomic_set(&oth->ot_refcount, 1);
789 INIT_LIST_HEAD(&oth->ot_commit_dcb_list);
790 INIT_LIST_HEAD(&oth->ot_stop_dcb_list);
796 * Add commit callback to transaction.
798 * Add commit callback to the osp thandle, which will be called
799 * when the thandle is committed remotely.
801 * \param[in] th the thandle
802 * \param[in] dcb commit callback structure
804 * \retval only return 0 for now.
806 int osp_trans_cb_add(struct thandle *th, struct dt_txn_commit_cb *dcb)
808 struct osp_thandle *oth = thandle_to_osp_thandle(th);
810 LASSERT(dcb->dcb_magic == TRANS_COMMIT_CB_MAGIC);
811 LASSERT(&dcb->dcb_func != NULL);
812 if (dcb->dcb_flags & DCB_TRANS_STOP)
813 list_add(&dcb->dcb_linkage, &oth->ot_stop_dcb_list);
815 list_add(&dcb->dcb_linkage, &oth->ot_commit_dcb_list);
819 static void osp_trans_commit_cb(struct osp_thandle *oth, int result)
821 struct dt_txn_commit_cb *dcb;
822 struct dt_txn_commit_cb *tmp;
824 LASSERT(atomic_read(&oth->ot_refcount) > 0);
825 /* call per-transaction callbacks if any */
826 list_for_each_entry_safe(dcb, tmp, &oth->ot_commit_dcb_list,
828 LASSERTF(dcb->dcb_magic == TRANS_COMMIT_CB_MAGIC,
829 "commit callback entry: magic=%x name='%s'\n",
830 dcb->dcb_magic, dcb->dcb_name);
831 list_del_init(&dcb->dcb_linkage);
832 dcb->dcb_func(NULL, &oth->ot_super, dcb, result);
836 static void osp_request_commit_cb(struct ptlrpc_request *req)
838 struct thandle *th = req->rq_cb_data;
839 struct osp_thandle *oth;
840 __u64 last_committed_transno = 0;
841 int result = req->rq_status;
847 oth = thandle_to_osp_thandle(th);
848 if (lustre_msg_get_last_committed(req->rq_repmsg))
849 last_committed_transno =
850 lustre_msg_get_last_committed(req->rq_repmsg);
852 if (last_committed_transno <
853 req->rq_import->imp_peer_committed_transno)
854 last_committed_transno =
855 req->rq_import->imp_peer_committed_transno;
857 CDEBUG(D_HA, "trans no "LPU64" committed transno "LPU64"\n",
858 req->rq_transno, last_committed_transno);
860 /* If the transaction is not really committed, mark result = 1 */
861 if (req->rq_transno != 0 &&
862 (req->rq_transno > last_committed_transno) && result == 0)
865 osp_trans_commit_cb(oth, result);
866 req->rq_committed = 1;
867 osp_thandle_put(oth);
872 * callback of osp transaction
874 * Call all of callbacks for this osp thandle. This will only be
875 * called in error handler path. In the normal processing path,
876 * these callback will be called in osp_request_commit_cb() and
877 * osp_update_interpret().
879 * \param [in] env execution environment
880 * \param [in] oth osp thandle
881 * \param [in] rc result of the osp thandle
883 void osp_trans_callback(const struct lu_env *env,
884 struct osp_thandle *oth, int rc)
886 struct osp_update_callback *ouc;
887 struct osp_update_callback *next;
889 if (oth->ot_our != NULL) {
890 list_for_each_entry_safe(ouc, next,
891 &oth->ot_our->our_cb_items, ouc_list) {
892 list_del_init(&ouc->ouc_list);
893 if (ouc->ouc_interpreter != NULL)
894 ouc->ouc_interpreter(env, NULL, NULL,
896 ouc->ouc_data, 0, rc);
897 osp_update_callback_fini(env, ouc);
900 osp_trans_stop_cb(oth, rc);
901 osp_trans_commit_cb(oth, rc);
905 * Send the request for remote updates.
907 * Send updates to the remote MDT. Prepare the request by osp_update_req
908 * and send them to remote MDT, for sync request, it will wait
909 * until the reply return, otherwise hand it to ptlrpcd.
911 * Please refer to osp_trans_create() for transaction type.
913 * \param[in] env pointer to the thread context
914 * \param[in] osp pointer to the OSP device
915 * \param[in] our pointer to the osp_update_request
917 * \retval 0 for success
918 * \retval negative error number on failure
920 static int osp_send_update_req(const struct lu_env *env,
921 struct osp_device *osp,
922 struct osp_update_request *our)
924 struct osp_update_args *args;
925 struct ptlrpc_request *req;
926 struct lu_device *top_device;
927 struct osp_thandle *oth = our->our_th;
931 LASSERT(oth != NULL);
932 LASSERT(our->our_req_sent == 0);
933 rc = osp_prep_update_req(env, osp->opd_obd->u.cli.cl_import,
936 osp_trans_callback(env, oth, rc);
940 args = ptlrpc_req_async_args(req);
941 args->oaua_update = our;
942 osp_thandle_get(oth); /* hold for update interpret */
943 req->rq_interpret_reply = osp_update_interpret;
944 if (!oth->ot_super.th_wait_submit && !oth->ot_super.th_sync) {
945 if (!osp->opd_imp_active || !osp->opd_imp_connected) {
946 osp_trans_callback(env, oth, rc);
947 osp_thandle_put(oth);
948 GOTO(out, rc = -ENOTCONN);
951 rc = obd_get_request_slot(&osp->opd_obd->u.cli);
953 osp_trans_callback(env, oth, rc);
954 osp_thandle_put(oth);
955 GOTO(out, rc = -ENOTCONN);
957 args->oaua_flow_control = true;
959 if (!osp->opd_connect_mdt) {
960 down_read(&osp->opd_async_updates_rwsem);
961 args->oaua_count = &osp->opd_async_updates_count;
962 args->oaua_waitq = &osp->opd_syn_barrier_waitq;
963 up_read(&osp->opd_async_updates_rwsem);
964 atomic_inc(args->oaua_count);
967 ptlrpcd_add_req(req);
970 osp_thandle_get(oth); /* hold for commit callback */
971 req->rq_commit_cb = osp_request_commit_cb;
972 req->rq_cb_data = &oth->ot_super;
973 args->oaua_flow_control = false;
975 /* If the transaction is created during MDT recoverying
976 * process, it means this is an recovery update, we need
977 * to let OSP send it anyway without checking recoverying
978 * status, in case the other target is being recoveried
979 * at the same time, and if we wait here for the import
980 * to be recoveryed, it might cause deadlock */
981 top_device = osp->opd_dt_dev.dd_lu_dev.ld_site->ls_top_dev;
982 if (top_device->ld_obd->obd_recovering)
983 req->rq_allow_replay = 1;
985 if (osp->opd_connect_mdt)
986 osp_get_rpc_lock(osp);
987 rc = ptlrpc_queue_wait(req);
988 if (osp->opd_connect_mdt)
989 osp_put_rpc_lock(osp);
990 if ((rc == -ENOMEM && req->rq_set == NULL) ||
991 (req->rq_transno == 0 && !req->rq_committed)) {
992 if (args->oaua_update != NULL) {
993 /* If osp_update_interpret is not being called,
994 * release the osp_thandle */
995 args->oaua_update = NULL;
996 osp_thandle_put(oth);
999 req->rq_cb_data = NULL;
1000 rc = rc == 0 ? req->rq_status : rc;
1001 osp_trans_callback(env, oth, rc);
1002 osp_thandle_put(oth);
1008 ptlrpc_req_finished(req);
1014 * Get local thandle for osp_thandle
1016 * Get the local OSD thandle from the OSP thandle. Currently, there
1017 * are a few OSP API (osp_object_create() and osp_sync_add()) needs
1018 * to update the object on local OSD device.
1020 * If the osp_thandle comes from normal stack (MDD->LOD->OSP), then
1021 * we will get local thandle by thandle_get_sub_by_dt.
1023 * If the osp_thandle is remote thandle (th_top == NULL, only used
1024 * by LFSCK), then it will create a local thandle, and stop it in
1025 * osp_trans_stop(). And this only happens on OSP for OST.
1027 * These are temporary solution, once OSP accessing OSD object is
1028 * being fixed properly, this function should be removed. XXX
1030 * \param[in] env pointer to the thread context
1031 * \param[in] th pointer to the transaction handler
1032 * \param[in] dt pointer to the OSP device
1034 * \retval pointer to the local thandle
1035 * \retval ERR_PTR(errno) if it fails.
1037 struct thandle *osp_get_storage_thandle(const struct lu_env *env,
1039 struct osp_device *osp)
1041 struct osp_thandle *oth;
1042 struct thandle *local_th;
1044 if (th->th_top != NULL)
1045 return thandle_get_sub_by_dt(env, th->th_top,
1048 LASSERT(!osp->opd_connect_mdt);
1049 oth = thandle_to_osp_thandle(th);
1050 if (oth->ot_storage_th != NULL)
1051 return oth->ot_storage_th;
1053 local_th = dt_trans_create(env, osp->opd_storage);
1054 if (IS_ERR(local_th))
1057 oth->ot_storage_th = local_th;
1063 * Set version for the transaction
1065 * Set the version for the transaction, then the osp RPC will be
1066 * sent in the order of version, i.e. the transaction with lower
1067 * version will be sent first.
1069 * \param [in] oth osp thandle to be set version.
1071 * \retval 0 if set version succeeds
1072 * negative errno if set version fails.
1074 int osp_check_and_set_rpc_version(struct osp_thandle *oth)
1076 struct osp_device *osp = dt2osp_dev(oth->ot_super.th_dev);
1077 struct osp_updates *ou = osp->opd_update;
1082 if (oth->ot_version != 0)
1085 spin_lock(&ou->ou_lock);
1086 oth->ot_version = ou->ou_version++;
1087 spin_unlock(&ou->ou_lock);
1089 CDEBUG(D_INFO, "%s: version "LPU64" oth:version %p:"LPU64"\n",
1090 osp->opd_obd->obd_name, ou->ou_version, oth, oth->ot_version);
1096 * Get next OSP update request in the sending list
1097 * Get next OSP update request in the sending list by version number, next
1099 * 1. transaction which does not have a version number.
1100 * 2. transaction whose version == opd_rpc_version.
1102 * \param [in] ou osp update structure.
1103 * \param [out] ourp the pointer holding the next update request.
1105 * \retval true if getting the next transaction.
1106 * \retval false if not getting the next transaction.
1109 osp_get_next_request(struct osp_updates *ou, struct osp_update_request **ourp)
1111 struct osp_update_request *our;
1112 struct osp_update_request *tmp;
1113 bool got_req = false;
1115 spin_lock(&ou->ou_lock);
1116 list_for_each_entry_safe(our, tmp, &ou->ou_list, our_list) {
1117 LASSERT(our->our_th != NULL);
1118 CDEBUG(D_INFO, "our %p version "LPU64" rpc_version "LPU64"\n",
1119 our, our->our_th->ot_version, ou->ou_rpc_version);
1120 if (our->our_th->ot_version == 0) {
1121 list_del_init(&our->our_list);
1127 /* Find next osp_update_request in the list */
1128 if (our->our_th->ot_version == ou->ou_rpc_version) {
1129 list_del_init(&our->our_list);
1135 spin_unlock(&ou->ou_lock);
1140 static void osp_update_rpc_version(struct osp_updates *ou,
1141 struct osp_thandle *oth)
1143 if (oth->ot_version == 0)
1146 LASSERT(oth->ot_version == ou->ou_rpc_version);
1147 spin_lock(&ou->ou_lock);
1148 ou->ou_rpc_version++;
1149 spin_unlock(&ou->ou_lock);
1153 * Sending update thread
1155 * Create thread to send update request to other MDTs, this thread will pull
1156 * out update request from the list in OSP by version number, i.e. it will
1157 * make sure the update request with lower version number will be sent first.
1159 * \param[in] arg hold the OSP device.
1161 * \retval 0 if the thread is created successfully.
1162 * \retal negative error if the thread is not created
1165 int osp_send_update_thread(void *arg)
1168 struct osp_device *osp = arg;
1169 struct l_wait_info lwi = { 0 };
1170 struct osp_updates *ou = osp->opd_update;
1171 struct ptlrpc_thread *thread = &osp->opd_update_thread;
1172 struct osp_update_request *our = NULL;
1176 LASSERT(ou != NULL);
1177 rc = lu_env_init(&env, osp->opd_dt_dev.dd_lu_dev.ld_type->ldt_ctx_tags);
1179 CERROR("%s: init env error: rc = %d\n", osp->opd_obd->obd_name,
1184 thread->t_flags = SVC_RUNNING;
1185 wake_up(&thread->t_ctl_waitq);
1188 l_wait_event(ou->ou_waitq,
1189 !osp_send_update_thread_running(osp) ||
1190 osp_get_next_request(ou, &our),
1193 if (!osp_send_update_thread_running(osp)) {
1194 if (our != NULL && our->our_th != NULL) {
1195 osp_trans_callback(&env, our->our_th, -EINTR);
1196 osp_thandle_put(our->our_th);
1201 if (our->our_req_sent == 0) {
1202 if (our->our_th != NULL &&
1203 our->our_th->ot_super.th_result != 0)
1204 osp_trans_callback(&env, our->our_th,
1205 our->our_th->ot_super.th_result);
1207 rc = osp_send_update_req(&env, osp, our);
1210 if (our->our_th != NULL) {
1211 /* Update the rpc version */
1212 osp_update_rpc_version(ou, our->our_th);
1213 /* Balanced for thandle_get in osp_trans_trigger() */
1214 osp_thandle_put(our->our_th);
1218 thread->t_flags = SVC_STOPPED;
1220 wake_up(&thread->t_ctl_waitq);
1226 * Trigger the request for remote updates.
1228 * Add the request to the sending list, and wake up osp update
1231 * \param[in] env pointer to the thread context
1232 * \param[in] osp pointer to the OSP device
1233 * \param[in] oth pointer to the transaction handler
1236 static void osp_trans_trigger(const struct lu_env *env,
1237 struct osp_device *osp,
1238 struct osp_thandle *oth)
1241 CDEBUG(D_INFO, "%s: add oth %p with version "LPU64"\n",
1242 osp->opd_obd->obd_name, oth, oth->ot_version);
1244 LASSERT(oth->ot_magic == OSP_THANDLE_MAGIC);
1245 osp_thandle_get(oth);
1246 LASSERT(oth->ot_our != NULL);
1247 spin_lock(&osp->opd_update->ou_lock);
1248 list_add_tail(&oth->ot_our->our_list,
1249 &osp->opd_update->ou_list);
1250 spin_unlock(&osp->opd_update->ou_lock);
1252 wake_up(&osp->opd_update->ou_waitq);
1256 * The OSP layer dt_device_operations::dt_trans_start() interface
1257 * to start the transaction.
1259 * If the transaction is a remote transaction, then related remote
1260 * updates will be triggered in the osp_trans_stop().
1261 * Please refer to osp_trans_create() for transaction type.
1263 * \param[in] env pointer to the thread context
1264 * \param[in] dt pointer to the OSP dt_device
1265 * \param[in] th pointer to the transaction handler
1267 * \retval 0 for success
1268 * \retval negative error number on failure
1270 int osp_trans_start(const struct lu_env *env, struct dt_device *dt,
1273 struct osp_thandle *oth = thandle_to_osp_thandle(th);
1275 if (oth->ot_super.th_sync)
1276 oth->ot_our->our_flags |= UPDATE_FL_SYNC;
1277 /* For remote thandle, if there are local thandle, start it here*/
1278 if (is_only_remote_trans(th) && oth->ot_storage_th != NULL)
1279 return dt_trans_start(env, oth->ot_storage_th->th_dev,
1280 oth->ot_storage_th);
1285 * The OSP layer dt_device_operations::dt_trans_stop() interface
1286 * to stop the transaction.
1288 * If the transaction is a remote transaction, related remote
1289 * updates will be triggered here via osp_trans_trigger().
1291 * For synchronous mode update or any failed update, the request
1292 * will be destroyed explicitly when the osp_trans_stop().
1294 * Please refer to osp_trans_create() for transaction type.
1296 * \param[in] env pointer to the thread context
1297 * \param[in] dt pointer to the OSP dt_device
1298 * \param[in] th pointer to the transaction handler
1300 * \retval 0 for success
1301 * \retval negative error number on failure
1303 int osp_trans_stop(const struct lu_env *env, struct dt_device *dt,
1306 struct osp_thandle *oth = thandle_to_osp_thandle(th);
1307 struct osp_update_request *our = oth->ot_our;
1308 struct osp_device *osp = dt2osp_dev(dt);
1312 /* For remote transaction, if there is local storage thandle,
1314 if (oth->ot_storage_th != NULL && th->th_top == NULL) {
1315 dt_trans_stop(env, oth->ot_storage_th->th_dev,
1316 oth->ot_storage_th);
1317 oth->ot_storage_th = NULL;
1320 if (our == NULL || list_empty(&our->our_req_list)) {
1321 osp_trans_callback(env, oth, th->th_result);
1322 GOTO(out, rc = th->th_result);
1325 if (!osp->opd_connect_mdt) {
1326 osp_trans_callback(env, oth, th->th_result);
1327 rc = osp_send_update_req(env, osp, oth->ot_our);
1331 if (osp->opd_update == NULL ||
1332 !osp_send_update_thread_running(osp)) {
1333 osp_trans_callback(env, oth, -EIO);
1334 GOTO(out, rc = -EIO);
1338 /* if th_sync is set, then it needs to be sent
1339 * right away. Note: even thought the RPC has been
1340 * sent, it still needs to be added to the sending
1341 * list (see osp_trans_trigger()), so ou_rpc_version
1342 * can be updated correctly. */
1343 rc = osp_send_update_req(env, osp, our);
1344 our->our_req_sent = 1;
1347 osp_trans_trigger(env, osp, oth);
1349 osp_thandle_put(oth);