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 "osp_internal.h"
72 * The argument for the interpreter callback of osp request.
74 struct osp_update_args {
75 struct osp_update_request *oaua_update;
77 wait_queue_head_t *oaua_waitq;
78 bool oaua_flow_control;
82 * Call back for each update request.
84 struct osp_update_callback {
85 /* list in the osp_update_request::our_cb_items */
86 struct list_head ouc_list;
88 /* The target of the async update request. */
89 struct osp_object *ouc_obj;
91 /* The data used by or_interpreter. */
94 /* The interpreter function called after the async request handled. */
95 osp_update_interpreter_t ouc_interpreter;
98 static struct object_update_request *object_update_request_alloc(size_t size)
100 struct object_update_request *ourq;
102 OBD_ALLOC_LARGE(ourq, size);
104 return ERR_PTR(-ENOMEM);
106 ourq->ourq_magic = UPDATE_REQUEST_MAGIC;
107 ourq->ourq_count = 0;
112 static void object_update_request_free(struct object_update_request *ourq,
116 OBD_FREE_LARGE(ourq, ourq_size);
120 * Allocate and initialize osp_update_request
122 * osp_update_request is being used to track updates being executed on
123 * this dt_device(OSD or OSP). The update buffer will be 4k initially,
124 * and increased if needed.
126 * \param [in] dt dt device
128 * \retval osp_update_request being allocated if succeed
129 * \retval ERR_PTR(errno) if failed
131 struct osp_update_request *osp_update_request_create(struct dt_device *dt)
133 struct osp_update_request *osp_update_req;
134 struct object_update_request *ourq;
136 OBD_ALLOC_PTR(osp_update_req);
137 if (osp_update_req == NULL)
138 return ERR_PTR(-ENOMEM);
140 ourq = object_update_request_alloc(OUT_UPDATE_INIT_BUFFER_SIZE);
142 OBD_FREE_PTR(osp_update_req);
143 return ERR_CAST(ourq);
146 osp_update_req->our_req = ourq;
147 osp_update_req->our_req_size = OUT_UPDATE_INIT_BUFFER_SIZE;
149 INIT_LIST_HEAD(&osp_update_req->our_cb_items);
150 INIT_LIST_HEAD(&osp_update_req->our_list);
152 return osp_update_req;
155 void osp_update_request_destroy(struct osp_update_request *our)
160 object_update_request_free(our->our_req,
166 object_update_request_dump(const struct object_update_request *ourq,
170 size_t total_size = 0;
172 for (i = 0; i < ourq->ourq_count; i++) {
173 struct object_update *update;
176 update = object_update_request_get(ourq, i, &size);
177 LASSERT(update != NULL);
178 CDEBUG(mask, "i = %u fid = "DFID" op = %s master = %u"
179 "params = %d batchid = "LPU64" size = %zu\n",
180 i, PFID(&update->ou_fid),
181 update_op_str(update->ou_type),
182 update->ou_master_index, update->ou_params_count,
183 update->ou_batchid, size);
188 CDEBUG(mask, "updates = %p magic = %x count = %d size = %zu\n", ourq,
189 ourq->ourq_magic, ourq->ourq_count, total_size);
192 static void osp_trans_stop_cb(struct osp_thandle *oth, int result)
194 struct dt_txn_commit_cb *dcb;
195 struct dt_txn_commit_cb *tmp;
197 /* call per-transaction stop callbacks if any */
198 list_for_each_entry_safe(dcb, tmp, &oth->ot_stop_dcb_list,
200 LASSERTF(dcb->dcb_magic == TRANS_COMMIT_CB_MAGIC,
201 "commit callback entry: magic=%x name='%s'\n",
202 dcb->dcb_magic, dcb->dcb_name);
203 list_del_init(&dcb->dcb_linkage);
204 dcb->dcb_func(NULL, &oth->ot_super, dcb, result);
209 * Allocate an osp request and initialize it with the given parameters.
211 * \param[in] obj pointer to the operation target
212 * \param[in] data pointer to the data used by the interpreter
213 * \param[in] interpreter pointer to the interpreter function
215 * \retval pointer to the asychronous request
216 * \retval NULL if the allocation failed
218 static struct osp_update_callback *
219 osp_update_callback_init(struct osp_object *obj, void *data,
220 osp_update_interpreter_t interpreter)
222 struct osp_update_callback *ouc;
228 lu_object_get(osp2lu_obj(obj));
229 INIT_LIST_HEAD(&ouc->ouc_list);
231 ouc->ouc_data = data;
232 ouc->ouc_interpreter = interpreter;
238 * Destroy the osp_update_callback.
240 * \param[in] env pointer to the thread context
241 * \param[in] ouc pointer to osp_update_callback
243 static void osp_update_callback_fini(const struct lu_env *env,
244 struct osp_update_callback *ouc)
246 LASSERT(list_empty(&ouc->ouc_list));
248 lu_object_put(env, osp2lu_obj(ouc->ouc_obj));
253 * Interpret the packaged OUT RPC results.
255 * For every packaged sub-request, call its registered interpreter function.
256 * Then destroy the sub-request.
258 * \param[in] env pointer to the thread context
259 * \param[in] req pointer to the RPC
260 * \param[in] arg pointer to data used by the interpreter
261 * \param[in] rc the RPC return value
263 * \retval 0 for success
264 * \retval negative error number on failure
266 static int osp_update_interpret(const struct lu_env *env,
267 struct ptlrpc_request *req, void *arg, int rc)
269 struct object_update_reply *reply = NULL;
270 struct osp_update_args *oaua = arg;
271 struct osp_update_request *our = oaua->oaua_update;
272 struct osp_thandle *oth;
273 struct osp_update_callback *ouc;
274 struct osp_update_callback *next;
284 oaua->oaua_update = NULL;
286 if (oaua->oaua_flow_control) {
287 struct osp_device *osp;
289 LASSERT(oth != NULL);
290 osp = dt2osp_dev(oth->ot_super.th_dev);
291 obd_put_request_slot(&osp->opd_obd->u.cli);
294 /* Unpack the results from the reply message. */
295 if (req->rq_repmsg != NULL) {
296 reply = req_capsule_server_sized_get(&req->rq_pill,
297 &RMF_OUT_UPDATE_REPLY,
298 OUT_UPDATE_REPLY_SIZE);
299 if (reply == NULL || reply->ourp_magic != UPDATE_REPLY_MAGIC)
302 count = reply->ourp_count;
307 list_for_each_entry_safe(ouc, next, &our->our_cb_items, ouc_list) {
308 list_del_init(&ouc->ouc_list);
310 /* The peer may only have handled some requests (indicated
311 * by the 'count') in the packaged OUT RPC, we can only get
312 * results for the handled part. */
313 if (index < count && reply->ourp_lens[index] > 0) {
314 struct object_update_result *result;
316 result = object_update_result_get(reply, index, NULL);
320 rc1 = result->our_rc;
323 if (unlikely(rc1 == 0))
327 if (ouc->ouc_interpreter != NULL)
328 ouc->ouc_interpreter(env, reply, req, ouc->ouc_obj,
329 ouc->ouc_data, index, rc1);
331 osp_update_callback_fini(env, ouc);
335 if (oaua->oaua_count != NULL && atomic_dec_and_test(oaua->oaua_count))
336 wake_up_all(oaua->oaua_waitq);
339 /* oth and osp_update_requests will be destoryed in
341 osp_trans_stop_cb(oth, rc);
342 osp_thandle_put(oth);
344 osp_update_request_destroy(our);
351 * Pack all the requests in the shared asynchronous idempotent request queue
352 * into a single OUT RPC that will be given to the background ptlrpcd daemon.
354 * \param[in] env pointer to the thread context
355 * \param[in] osp pointer to the OSP device
356 * \param[in] our pointer to the shared queue
358 * \retval 0 for success
359 * \retval negative error number on failure
361 int osp_unplug_async_request(const struct lu_env *env,
362 struct osp_device *osp,
363 struct osp_update_request *our)
365 struct osp_update_args *args;
366 struct ptlrpc_request *req = NULL;
369 rc = osp_prep_update_req(env, osp->opd_obd->u.cli.cl_import,
372 struct osp_update_callback *ouc;
373 struct osp_update_callback *next;
375 list_for_each_entry_safe(ouc, next,
376 &our->our_cb_items, ouc_list) {
377 list_del_init(&ouc->ouc_list);
378 if (ouc->ouc_interpreter != NULL)
379 ouc->ouc_interpreter(env, NULL, NULL,
381 ouc->ouc_data, 0, rc);
382 osp_update_callback_fini(env, ouc);
384 osp_update_request_destroy(our);
386 args = ptlrpc_req_async_args(req);
387 args->oaua_update = our;
388 args->oaua_count = NULL;
389 args->oaua_waitq = NULL;
390 args->oaua_flow_control = false;
391 req->rq_interpret_reply = osp_update_interpret;
392 ptlrpcd_add_req(req, PDL_POLICY_LOCAL, -1);
399 * Find or create (if NOT exist or purged) the shared asynchronous idempotent
400 * request queue - osp_device::opd_async_requests.
402 * If the osp_device::opd_async_requests is not NULL, then return it directly;
403 * otherwise create new osp_update_request and attach it to opd_async_requests.
405 * \param[in] osp pointer to the OSP device
407 * \retval pointer to the shared queue
408 * \retval negative error number on failure
410 static struct osp_update_request *
411 osp_find_or_create_async_update_request(struct osp_device *osp)
413 struct osp_update_request *our = osp->opd_async_requests;
418 our = osp_update_request_create(&osp->opd_dt_dev);
422 osp->opd_async_requests = our;
428 * Insert an osp_update_callback into the osp_update_request.
430 * Insert an osp_update_callback to the osp_update_request. Usually each update
431 * in the osp_update_request will have one correspondent callback, and these
432 * callbacks will be called in rq_interpret_reply.
434 * \param[in] env pointer to the thread context
435 * \param[in] obj pointer to the operation target object
436 * \param[in] data pointer to the data used by the interpreter
437 * \param[in] interpreter pointer to the interpreter function
439 * \retval 0 for success
440 * \retval negative error number on failure
442 int osp_insert_update_callback(const struct lu_env *env,
443 struct osp_update_request *our,
444 struct osp_object *obj, void *data,
445 osp_update_interpreter_t interpreter)
447 struct osp_update_callback *ouc;
449 ouc = osp_update_callback_init(obj, data, interpreter);
453 list_add_tail(&ouc->ouc_list, &our->our_cb_items);
459 * Insert an asynchronous idempotent request to the shared request queue that
460 * is attached to the osp_device.
462 * This function generates a new osp_async_request with the given parameters,
463 * then tries to insert the request into the osp_device-based shared request
464 * queue. If the queue is full, then triggers the packaged OUT RPC to purge
465 * the shared queue firstly, and then re-tries.
467 * NOTE: must hold the osp::opd_async_requests_mutex to serialize concurrent
468 * osp_insert_async_request call from others.
470 * \param[in] env pointer to the thread context
471 * \param[in] op operation type, see 'enum update_type'
472 * \param[in] obj pointer to the operation target
473 * \param[in] count array size of the subsequent \a lens and \a bufs
474 * \param[in] lens buffer length array for the subsequent \a bufs
475 * \param[in] bufs the buffers to compose the request
476 * \param[in] data pointer to the data used by the interpreter
477 * \param[in] interpreter pointer to the interpreter function
479 * \retval 0 for success
480 * \retval negative error number on failure
482 int osp_insert_async_request(const struct lu_env *env, enum update_type op,
483 struct osp_object *obj, int count,
484 __u16 *lens, const void **bufs, void *data,
485 osp_update_interpreter_t interpreter)
487 struct osp_device *osp;
488 struct osp_update_request *our;
489 struct object_update *object_update;
490 size_t max_update_size;
491 struct object_update_request *ureq;
495 osp = lu2osp_dev(osp2lu_obj(obj)->lo_dev);
496 our = osp_find_or_create_async_update_request(osp);
498 RETURN(PTR_ERR(our));
502 max_update_size = our->our_req_size - object_update_request_size(ureq);
504 object_update = update_buffer_get_update(ureq, ureq->ourq_count);
505 rc = out_update_pack(env, object_update, max_update_size, op,
506 lu_object_fid(osp2lu_obj(obj)), count, lens, bufs);
507 /* The queue is full. */
509 osp->opd_async_requests = NULL;
510 mutex_unlock(&osp->opd_async_requests_mutex);
512 rc = osp_unplug_async_request(env, osp, our);
513 mutex_lock(&osp->opd_async_requests_mutex);
517 our = osp_find_or_create_async_update_request(osp);
519 RETURN(PTR_ERR(our));
529 rc = osp_insert_update_callback(env, our, obj, data, interpreter);
534 int osp_trans_update_request_create(struct thandle *th)
536 struct osp_thandle *oth = thandle_to_osp_thandle(th);
537 struct osp_update_request *our;
539 if (oth->ot_our != NULL)
542 our = osp_update_request_create(th->th_dev);
544 th->th_result = PTR_ERR(our);
548 if (dt2osp_dev(th->th_dev)->opd_connect_mdt)
549 our->our_flags = UPDATE_FL_SYNC;
556 void osp_thandle_destroy(struct osp_thandle *oth)
558 LASSERT(oth->ot_magic == OSP_THANDLE_MAGIC);
559 LASSERT(list_empty(&oth->ot_commit_dcb_list));
560 LASSERT(list_empty(&oth->ot_stop_dcb_list));
561 if (oth->ot_our != NULL)
562 osp_update_request_destroy(oth->ot_our);
567 * The OSP layer dt_device_operations::dt_trans_create() interface
568 * to create a transaction.
570 * There are two kinds of transactions that will involve OSP:
572 * 1) If the transaction only contains the updates on remote server
573 * (MDT or OST), such as re-generating the lost OST-object for
574 * LFSCK, then it is a remote transaction. For remote transaction,
575 * the upper layer caller (such as the LFSCK engine) will call the
576 * dt_trans_create() (with the OSP dt_device as the parameter),
577 * then the call will be directed to the osp_trans_create() that
578 * creates the transaction handler and returns it to the caller.
580 * 2) If the transcation contains both local and remote updates,
581 * such as cross MDTs create under DNE mode, then the upper layer
582 * caller will not trigger osp_trans_create(). Instead, it will
583 * call dt_trans_create() on other dt_device, such as LOD that
584 * will generate the transaction handler. Such handler will be
585 * used by the whole transaction in subsequent sub-operations.
587 * \param[in] env pointer to the thread context
588 * \param[in] d pointer to the OSP dt_device
590 * \retval pointer to the transaction handler
591 * \retval negative error number on failure
593 struct thandle *osp_trans_create(const struct lu_env *env, struct dt_device *d)
595 struct osp_thandle *oth;
596 struct thandle *th = NULL;
600 if (unlikely(oth == NULL))
601 RETURN(ERR_PTR(-ENOMEM));
603 oth->ot_magic = OSP_THANDLE_MAGIC;
606 th->th_tags = LCT_TX_HANDLE;
608 atomic_set(&oth->ot_refcount, 1);
609 INIT_LIST_HEAD(&oth->ot_commit_dcb_list);
610 INIT_LIST_HEAD(&oth->ot_stop_dcb_list);
616 * Prepare update request.
618 * Prepare OUT update ptlrpc request, and the request usually includes
619 * all of updates (stored in \param ureq) from one operation.
621 * \param[in] env execution environment
622 * \param[in] imp import on which ptlrpc request will be sent
623 * \param[in] ureq hold all of updates which will be packed into the req
624 * \param[in] reqp request to be created
626 * \retval 0 if preparation succeeds.
627 * \retval negative errno if preparation fails.
629 int osp_prep_update_req(const struct lu_env *env, struct obd_import *imp,
630 const struct object_update_request *ureq,
631 struct ptlrpc_request **reqp)
633 struct ptlrpc_request *req;
634 struct object_update_request *tmp;
639 object_update_request_dump(ureq, D_INFO);
640 req = ptlrpc_request_alloc(imp, &RQF_OUT_UPDATE);
644 ureq_len = object_update_request_size(ureq);
645 req_capsule_set_size(&req->rq_pill, &RMF_OUT_UPDATE, RCL_CLIENT,
648 rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, OUT_UPDATE);
650 ptlrpc_req_finished(req);
654 req_capsule_set_size(&req->rq_pill, &RMF_OUT_UPDATE_REPLY,
655 RCL_SERVER, OUT_UPDATE_REPLY_SIZE);
657 tmp = req_capsule_client_get(&req->rq_pill, &RMF_OUT_UPDATE);
658 memcpy(tmp, ureq, ureq_len);
660 ptlrpc_request_set_replen(req);
661 req->rq_request_portal = OUT_PORTAL;
662 req->rq_reply_portal = OSC_REPLY_PORTAL;
671 * Send update request to the remote MDT synchronously.
673 * \param[in] env execution environment
674 * \param[in] imp import on which ptlrpc request will be sent
675 * \param[in] our hold all of updates which will be packed into the req
676 * \param[in] reqp request to be created
678 * \retval 0 if RPC succeeds.
679 * \retval negative errno if RPC fails.
682 int osp_remote_sync(const struct lu_env *env, struct osp_device *osp,
683 struct osp_update_request *our,
684 struct ptlrpc_request **reqp)
686 struct obd_import *imp = osp->opd_obd->u.cli.cl_import;
687 struct ptlrpc_request *req = NULL;
691 rc = osp_prep_update_req(env, imp, our->our_req, &req);
695 /* This will only be called with read-only update, and these updates
696 * might be used to retrieve update log during recovery process, so
697 * it will be allowed to send during recovery process */
698 req->rq_allow_replay = 1;
700 /* Note: some dt index api might return non-zero result here, like
701 * osd_index_ea_lookup, so we should only check rc < 0 here */
702 rc = ptlrpc_queue_wait(req);
704 ptlrpc_req_finished(req);
716 ptlrpc_req_finished(req);
722 * Add commit callback to transaction.
724 * Add commit callback to the osp thandle, which will be called
725 * when the thandle is committed remotely.
727 * \param[in] th the thandle
728 * \param[in] dcb commit callback structure
730 * \retval only return 0 for now.
732 int osp_trans_cb_add(struct thandle *th, struct dt_txn_commit_cb *dcb)
734 struct osp_thandle *oth = thandle_to_osp_thandle(th);
736 LASSERT(dcb->dcb_magic == TRANS_COMMIT_CB_MAGIC);
737 LASSERT(&dcb->dcb_func != NULL);
738 if (dcb->dcb_flags & DCB_TRANS_STOP)
739 list_add(&dcb->dcb_linkage, &oth->ot_stop_dcb_list);
741 list_add(&dcb->dcb_linkage, &oth->ot_commit_dcb_list);
745 static void osp_trans_commit_cb(struct osp_thandle *oth, int result)
747 struct dt_txn_commit_cb *dcb;
748 struct dt_txn_commit_cb *tmp;
750 LASSERT(atomic_read(&oth->ot_refcount) > 0);
751 /* call per-transaction callbacks if any */
752 list_for_each_entry_safe(dcb, tmp, &oth->ot_commit_dcb_list,
754 LASSERTF(dcb->dcb_magic == TRANS_COMMIT_CB_MAGIC,
755 "commit callback entry: magic=%x name='%s'\n",
756 dcb->dcb_magic, dcb->dcb_name);
757 list_del_init(&dcb->dcb_linkage);
758 dcb->dcb_func(NULL, &oth->ot_super, dcb, result);
762 static void osp_request_commit_cb(struct ptlrpc_request *req)
764 struct thandle *th = req->rq_cb_data;
765 struct osp_thandle *oth;
766 __u64 last_committed_transno = 0;
767 int result = req->rq_status;
773 oth = thandle_to_osp_thandle(th);
774 if (lustre_msg_get_last_committed(req->rq_repmsg))
775 last_committed_transno =
776 lustre_msg_get_last_committed(req->rq_repmsg);
778 if (last_committed_transno <
779 req->rq_import->imp_peer_committed_transno)
780 last_committed_transno =
781 req->rq_import->imp_peer_committed_transno;
783 CDEBUG(D_HA, "trans no "LPU64" committed transno "LPU64"\n",
784 req->rq_transno, last_committed_transno);
786 /* If the transaction is not really committed, mark result = 1 */
787 if (req->rq_transno != 0 &&
788 (req->rq_transno > last_committed_transno) && result == 0)
791 osp_trans_commit_cb(oth, result);
792 req->rq_committed = 1;
793 osp_thandle_put(oth);
798 * callback of osp transaction
800 * Call all of callbacks for this osp thandle. This will only be
801 * called in error handler path. In the normal processing path,
802 * these callback will be called in osp_request_commit_cb() and
803 * osp_update_interpret().
805 * \param [in] env execution environment
806 * \param [in] oth osp thandle
807 * \param [in] rc result of the osp thandle
809 void osp_trans_callback(const struct lu_env *env,
810 struct osp_thandle *oth, int rc)
812 struct osp_update_callback *ouc;
813 struct osp_update_callback *next;
815 if (oth->ot_our != NULL) {
816 list_for_each_entry_safe(ouc, next,
817 &oth->ot_our->our_cb_items, ouc_list) {
818 list_del_init(&ouc->ouc_list);
819 if (ouc->ouc_interpreter != NULL)
820 ouc->ouc_interpreter(env, NULL, NULL,
822 ouc->ouc_data, 0, rc);
823 osp_update_callback_fini(env, ouc);
826 osp_trans_stop_cb(oth, rc);
827 osp_trans_commit_cb(oth, rc);
831 * Send the request for remote updates.
833 * Send updates to the remote MDT. Prepare the request by osp_update_req
834 * and send them to remote MDT, for sync request, it will wait
835 * until the reply return, otherwise hand it to ptlrpcd.
837 * Please refer to osp_trans_create() for transaction type.
839 * \param[in] env pointer to the thread context
840 * \param[in] osp pointer to the OSP device
841 * \param[in] our pointer to the osp_update_request
843 * \retval 0 for success
844 * \retval negative error number on failure
846 static int osp_send_update_req(const struct lu_env *env,
847 struct osp_device *osp,
848 struct osp_update_request *our)
850 struct osp_update_args *args;
851 struct ptlrpc_request *req;
852 struct lu_device *top_device;
853 struct osp_thandle *oth = our->our_th;
857 LASSERT(oth != NULL);
858 LASSERT(our->our_req_sent == 0);
859 rc = osp_prep_update_req(env, osp->opd_obd->u.cli.cl_import,
862 osp_trans_callback(env, oth, rc);
866 args = ptlrpc_req_async_args(req);
867 args->oaua_update = our;
868 osp_thandle_get(oth); /* hold for update interpret */
869 req->rq_interpret_reply = osp_update_interpret;
870 if (!oth->ot_super.th_wait_submit && !oth->ot_super.th_sync) {
871 if (!osp->opd_imp_active || !osp->opd_imp_connected) {
872 osp_trans_callback(env, oth, rc);
873 osp_thandle_put(oth);
874 GOTO(out, rc = -ENOTCONN);
877 rc = obd_get_request_slot(&osp->opd_obd->u.cli);
879 osp_trans_callback(env, oth, rc);
880 osp_thandle_put(oth);
881 GOTO(out, rc = -ENOTCONN);
883 args->oaua_flow_control = true;
885 if (!osp->opd_connect_mdt) {
886 down_read(&osp->opd_async_updates_rwsem);
887 args->oaua_count = &osp->opd_async_updates_count;
888 args->oaua_waitq = &osp->opd_syn_barrier_waitq;
889 up_read(&osp->opd_async_updates_rwsem);
890 atomic_inc(args->oaua_count);
893 ptlrpcd_add_req(req, PDL_POLICY_LOCAL, -1);
896 osp_thandle_get(oth); /* hold for commit callback */
897 req->rq_commit_cb = osp_request_commit_cb;
898 req->rq_cb_data = &oth->ot_super;
899 args->oaua_flow_control = false;
901 /* If the transaction is created during MDT recoverying
902 * process, it means this is an recovery update, we need
903 * to let OSP send it anyway without checking recoverying
904 * status, in case the other target is being recoveried
905 * at the same time, and if we wait here for the import
906 * to be recoveryed, it might cause deadlock */
907 top_device = osp->opd_dt_dev.dd_lu_dev.ld_site->ls_top_dev;
908 if (top_device->ld_obd->obd_recovering)
909 req->rq_allow_replay = 1;
911 osp_get_rpc_lock(osp);
912 rc = ptlrpc_queue_wait(req);
913 osp_put_rpc_lock(osp);
914 if ((rc == -ENOMEM && req->rq_set == NULL) ||
915 (req->rq_transno == 0 && !req->rq_committed)) {
916 if (args->oaua_update != NULL) {
917 /* If osp_update_interpret is not being called,
918 * release the osp_thandle */
919 args->oaua_update = NULL;
920 osp_thandle_put(oth);
923 req->rq_cb_data = NULL;
924 rc = rc == 0 ? req->rq_status : rc;
925 osp_trans_callback(env, oth, rc);
926 osp_thandle_put(oth);
932 ptlrpc_req_finished(req);
938 * Get local thandle for osp_thandle
940 * Get the local OSD thandle from the OSP thandle. Currently, there
941 * are a few OSP API (osp_object_create() and osp_sync_add()) needs
942 * to update the object on local OSD device.
944 * If the osp_thandle comes from normal stack (MDD->LOD->OSP), then
945 * we will get local thandle by thandle_get_sub_by_dt.
947 * If the osp_thandle is remote thandle (th_top == NULL, only used
948 * by LFSCK), then it will create a local thandle, and stop it in
949 * osp_trans_stop(). And this only happens on OSP for OST.
951 * These are temporary solution, once OSP accessing OSD object is
952 * being fixed properly, this function should be removed. XXX
954 * \param[in] env pointer to the thread context
955 * \param[in] th pointer to the transaction handler
956 * \param[in] dt pointer to the OSP device
958 * \retval pointer to the local thandle
959 * \retval ERR_PTR(errno) if it fails.
961 struct thandle *osp_get_storage_thandle(const struct lu_env *env,
963 struct osp_device *osp)
965 struct osp_thandle *oth;
966 struct thandle *local_th;
968 if (th->th_top != NULL)
969 return thandle_get_sub_by_dt(env, th->th_top,
972 LASSERT(!osp->opd_connect_mdt);
973 oth = thandle_to_osp_thandle(th);
974 if (oth->ot_storage_th != NULL)
975 return oth->ot_storage_th;
977 local_th = dt_trans_create(env, osp->opd_storage);
978 if (IS_ERR(local_th))
981 oth->ot_storage_th = local_th;
987 * Set version for the transaction
989 * Set the version for the transaction, then the osp RPC will be
990 * sent in the order of version, i.e. the transaction with lower
991 * version will be sent first.
993 * \param [in] oth osp thandle to be set version.
995 * \retval 0 if set version succeeds
996 * negative errno if set version fails.
998 int osp_check_and_set_rpc_version(struct osp_thandle *oth)
1000 struct osp_device *osp = dt2osp_dev(oth->ot_super.th_dev);
1001 struct osp_updates *ou = osp->opd_update;
1006 if (oth->ot_version != 0)
1009 spin_lock(&ou->ou_lock);
1010 oth->ot_version = ou->ou_version++;
1011 spin_unlock(&ou->ou_lock);
1013 CDEBUG(D_INFO, "%s: version "LPU64" oth:version %p:"LPU64"\n",
1014 osp->opd_obd->obd_name, ou->ou_version, oth, oth->ot_version);
1020 * Get next OSP update request in the sending list
1021 * Get next OSP update request in the sending list by version number, next
1023 * 1. transaction which does not have a version number.
1024 * 2. transaction whose version == opd_rpc_version.
1026 * \param [in] ou osp update structure.
1027 * \param [out] ourp the pointer holding the next update request.
1029 * \retval true if getting the next transaction.
1030 * \retval false if not getting the next transaction.
1033 osp_get_next_request(struct osp_updates *ou, struct osp_update_request **ourp)
1035 struct osp_update_request *our;
1036 struct osp_update_request *tmp;
1037 bool got_req = false;
1039 spin_lock(&ou->ou_lock);
1040 list_for_each_entry_safe(our, tmp, &ou->ou_list, our_list) {
1041 LASSERT(our->our_th != NULL);
1042 CDEBUG(D_INFO, "our %p version "LPU64" rpc_version "LPU64"\n",
1043 our, our->our_th->ot_version, ou->ou_rpc_version);
1044 if (our->our_th->ot_version == 0) {
1045 list_del_init(&our->our_list);
1051 /* Find next osp_update_request in the list */
1052 if (our->our_th->ot_version == ou->ou_rpc_version) {
1053 list_del_init(&our->our_list);
1059 spin_unlock(&ou->ou_lock);
1064 static void osp_update_rpc_version(struct osp_updates *ou,
1065 struct osp_thandle *oth)
1067 if (oth->ot_version == 0)
1070 LASSERT(oth->ot_version == ou->ou_rpc_version);
1071 spin_lock(&ou->ou_lock);
1072 ou->ou_rpc_version++;
1073 spin_unlock(&ou->ou_lock);
1077 * Sending update thread
1079 * Create thread to send update request to other MDTs, this thread will pull
1080 * out update request from the list in OSP by version number, i.e. it will
1081 * make sure the update request with lower version number will be sent first.
1083 * \param[in] arg hold the OSP device.
1085 * \retval 0 if the thread is created successfully.
1086 * \retal negative error if the thread is not created
1089 int osp_send_update_thread(void *arg)
1092 struct osp_device *osp = arg;
1093 struct l_wait_info lwi = { 0 };
1094 struct osp_updates *ou = osp->opd_update;
1095 struct ptlrpc_thread *thread = &osp->opd_update_thread;
1096 struct osp_update_request *our = NULL;
1100 LASSERT(ou != NULL);
1101 rc = lu_env_init(&env, osp->opd_dt_dev.dd_lu_dev.ld_type->ldt_ctx_tags);
1103 CERROR("%s: init env error: rc = %d\n", osp->opd_obd->obd_name,
1108 thread->t_flags = SVC_RUNNING;
1109 wake_up(&thread->t_ctl_waitq);
1112 l_wait_event(ou->ou_waitq,
1113 !osp_send_update_thread_running(osp) ||
1114 osp_get_next_request(ou, &our),
1117 if (!osp_send_update_thread_running(osp)) {
1118 if (our != NULL && our->our_th != NULL) {
1119 osp_trans_callback(&env, our->our_th, -EINTR);
1120 osp_thandle_put(our->our_th);
1125 if (our->our_req_sent == 0) {
1126 if (our->our_th != NULL &&
1127 our->our_th->ot_super.th_result != 0)
1128 osp_trans_callback(&env, our->our_th,
1129 our->our_th->ot_super.th_result);
1131 rc = osp_send_update_req(&env, osp, our);
1134 if (our->our_th != NULL) {
1135 /* Update the rpc version */
1136 osp_update_rpc_version(ou, our->our_th);
1137 /* Balanced for thandle_get in osp_trans_trigger() */
1138 osp_thandle_put(our->our_th);
1142 thread->t_flags = SVC_STOPPED;
1144 wake_up(&thread->t_ctl_waitq);
1150 * Trigger the request for remote updates.
1152 * Add the request to the sending list, and wake up osp update
1155 * \param[in] env pointer to the thread context
1156 * \param[in] osp pointer to the OSP device
1157 * \param[in] oth pointer to the transaction handler
1160 static void osp_trans_trigger(const struct lu_env *env,
1161 struct osp_device *osp,
1162 struct osp_thandle *oth)
1165 CDEBUG(D_INFO, "%s: add oth %p with version "LPU64"\n",
1166 osp->opd_obd->obd_name, oth, oth->ot_version);
1168 LASSERT(oth->ot_magic == OSP_THANDLE_MAGIC);
1169 osp_thandle_get(oth);
1170 LASSERT(oth->ot_our != NULL);
1171 spin_lock(&osp->opd_update->ou_lock);
1172 list_add_tail(&oth->ot_our->our_list,
1173 &osp->opd_update->ou_list);
1174 spin_unlock(&osp->opd_update->ou_lock);
1176 wake_up(&osp->opd_update->ou_waitq);
1180 * The OSP layer dt_device_operations::dt_trans_start() interface
1181 * to start the transaction.
1183 * If the transaction is a remote transaction, then related remote
1184 * updates will be triggered in the osp_trans_stop().
1185 * Please refer to osp_trans_create() for transaction type.
1187 * \param[in] env pointer to the thread context
1188 * \param[in] dt pointer to the OSP dt_device
1189 * \param[in] th pointer to the transaction handler
1191 * \retval 0 for success
1192 * \retval negative error number on failure
1194 int osp_trans_start(const struct lu_env *env, struct dt_device *dt,
1197 struct osp_thandle *oth = thandle_to_osp_thandle(th);
1199 /* For remote thandle, if there are local thandle, start it here*/
1200 if (is_only_remote_trans(th) && oth->ot_storage_th != NULL)
1201 return dt_trans_start(env, oth->ot_storage_th->th_dev,
1202 oth->ot_storage_th);
1207 * The OSP layer dt_device_operations::dt_trans_stop() interface
1208 * to stop the transaction.
1210 * If the transaction is a remote transaction, related remote
1211 * updates will be triggered here via osp_trans_trigger().
1213 * For synchronous mode update or any failed update, the request
1214 * will be destroyed explicitly when the osp_trans_stop().
1216 * Please refer to osp_trans_create() for transaction type.
1218 * \param[in] env pointer to the thread context
1219 * \param[in] dt pointer to the OSP dt_device
1220 * \param[in] th pointer to the transaction handler
1222 * \retval 0 for success
1223 * \retval negative error number on failure
1225 int osp_trans_stop(const struct lu_env *env, struct dt_device *dt,
1228 struct osp_thandle *oth = thandle_to_osp_thandle(th);
1229 struct osp_update_request *our = oth->ot_our;
1230 struct osp_device *osp = dt2osp_dev(dt);
1234 /* For remote transaction, if there is local storage thandle,
1236 if (oth->ot_storage_th != NULL && th->th_top == NULL) {
1237 dt_trans_stop(env, oth->ot_storage_th->th_dev,
1238 oth->ot_storage_th);
1239 oth->ot_storage_th = NULL;
1242 if (our == NULL || our->our_req == NULL ||
1243 our->our_req->ourq_count == 0) {
1244 osp_trans_callback(env, oth, th->th_result);
1245 GOTO(out, rc = th->th_result);
1248 if (!osp->opd_connect_mdt) {
1249 rc = osp_send_update_req(env, osp, oth->ot_our);
1253 if (osp->opd_update == NULL ||
1254 !osp_send_update_thread_running(osp)) {
1255 osp_trans_callback(env, oth, -EIO);
1256 GOTO(out, rc = -EIO);
1260 /* if th_sync is set, then it needs to be sent
1261 * right away. Note: even thought the RPC has been
1262 * sent, it still needs to be added to the sending
1263 * list (see osp_trans_trigger()), so ou_rpc_version
1264 * can be updated correctly. */
1265 rc = osp_send_update_req(env, osp, our);
1266 our->our_req_sent = 1;
1269 osp_trans_trigger(env, osp, oth);
1271 osp_thandle_put(oth);