Whamcloud - gitweb
LU-6245 server: remove types abstraction from MDS/MGS code
[fs/lustre-release.git] / lustre / osp / osp_trans.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
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.
9  *
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).
15  *
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
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2014, 2015, Intel Corporation.
24  */
25 /*
26  * lustre/osp/osp_trans.c
27  *
28  *
29  * 1. OSP (Object Storage Proxy) transaction methods
30  *
31  * Implement OSP layer transaction related interfaces for the dt_device API
32  * dt_device_operations.
33  *
34  *
35  * 2. Handle asynchronous idempotent operations
36  *
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
41  * OUT RPC.
42  *
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.
49  *
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.
54  *
55  *
56  * There are three kinds of transactions
57  *
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.
62  *
63  * Author: Di Wang <di.wang@intel.com>
64  * Author: Fan, Yong <fan.yong@intel.com>
65  */
66
67 #define DEBUG_SUBSYSTEM S_MDS
68
69 #include <lustre_net.h>
70 #include "osp_internal.h"
71
72 /**
73  * The argument for the interpreter callback of osp request.
74  */
75 struct osp_update_args {
76         struct osp_update_request *oaua_update;
77         atomic_t                 *oaua_count;
78         wait_queue_head_t        *oaua_waitq;
79         bool                      oaua_flow_control;
80         const struct lu_env      *oaua_update_env;
81 };
82
83 /**
84  * Call back for each update request.
85  */
86 struct osp_update_callback {
87         /* list in the osp_update_request::our_cb_items */
88         struct list_head                 ouc_list;
89
90         /* The target of the async update request. */
91         struct osp_object               *ouc_obj;
92
93         /* The data used by or_interpreter. */
94         void                            *ouc_data;
95
96         /* The interpreter function called after the async request handled. */
97         osp_update_interpreter_t        ouc_interpreter;
98 };
99
100 static struct object_update_request *object_update_request_alloc(size_t size)
101 {
102         struct object_update_request *ourq;
103
104         OBD_ALLOC_LARGE(ourq, size);
105         if (ourq == NULL)
106                 return ERR_PTR(-ENOMEM);
107
108         ourq->ourq_magic = UPDATE_REQUEST_MAGIC;
109         ourq->ourq_count = 0;
110
111         return ourq;
112 }
113
114 /**
115  * Allocate new update request
116  *
117  * Allocate new update request and insert it to the req_update_list.
118  *
119  * \param [in] our      osp_udate_request where to create a new
120  *                      update request
121  *
122  * \retval      0 if creation succeeds.
123  * \retval      negative errno if creation fails.
124  */
125 int osp_object_update_request_create(struct osp_update_request *our,
126                                      size_t size)
127 {
128         struct osp_update_request_sub *ours;
129
130         OBD_ALLOC_PTR(ours);
131         if (ours == NULL)
132                 return -ENOMEM;
133
134         if (size < OUT_UPDATE_INIT_BUFFER_SIZE)
135                 size = OUT_UPDATE_INIT_BUFFER_SIZE;
136
137         ours->ours_req = object_update_request_alloc(size);
138
139         if (IS_ERR(ours->ours_req)) {
140                 OBD_FREE_PTR(ours);
141                 return -ENOMEM;
142         }
143
144         ours->ours_req_size = size;
145         INIT_LIST_HEAD(&ours->ours_list);
146         list_add_tail(&ours->ours_list, &our->our_req_list);
147         our->our_req_nr++;
148
149         return 0;
150 }
151
152 /**
153  * Get current update request
154  *
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.
159  *
160  * \param[in] our       osp update request where to get the
161  *                      current object update.
162  *
163  * \retval              the current object update.
164  **/
165 struct osp_update_request_sub *
166 osp_current_object_update_request(struct osp_update_request *our)
167 {
168         if (list_empty(&our->our_req_list))
169                 return NULL;
170
171         return list_entry(our->our_req_list.prev, struct osp_update_request_sub,
172                           ours_list);
173 }
174
175 /**
176  * Allocate and initialize osp_update_request
177  *
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.
181  *
182  * \param [in] dt       dt device
183  *
184  * \retval              osp_update_request being allocated if succeed
185  * \retval              ERR_PTR(errno) if failed
186  */
187 struct osp_update_request *osp_update_request_create(struct dt_device *dt)
188 {
189         struct osp_update_request *our;
190         int rc;
191
192         OBD_ALLOC_PTR(our);
193         if (our == NULL)
194                 return ERR_PTR(-ENOMEM);
195
196         INIT_LIST_HEAD(&our->our_req_list);
197         INIT_LIST_HEAD(&our->our_cb_items);
198         INIT_LIST_HEAD(&our->our_list);
199         INIT_LIST_HEAD(&our->our_invalidate_cb_list);
200         spin_lock_init(&our->our_list_lock);
201
202         rc = osp_object_update_request_create(our, OUT_UPDATE_INIT_BUFFER_SIZE);
203         if (rc != 0) {
204                 OBD_FREE_PTR(our);
205                 return ERR_PTR(rc);
206         }
207         return our;
208 }
209
210 void osp_update_request_destroy(const struct lu_env *env,
211                                 struct osp_update_request *our)
212 {
213         struct osp_update_request_sub *ours;
214         struct osp_update_request_sub *tmp;
215
216         if (our == NULL)
217                 return;
218
219         list_for_each_entry_safe(ours, tmp, &our->our_req_list, ours_list) {
220                 list_del(&ours->ours_list);
221                 if (ours->ours_req != NULL)
222                         OBD_FREE_LARGE(ours->ours_req, ours->ours_req_size);
223                 OBD_FREE_PTR(ours);
224         }
225
226         if (!list_empty(&our->our_invalidate_cb_list)) {
227                 struct lu_env lenv;
228                 struct osp_object *obj;
229                 struct osp_object *next;
230
231                 if (env == NULL) {
232                         lu_env_init(&lenv, LCT_MD_THREAD | LCT_DT_THREAD);
233                         env = &lenv;
234                 }
235
236                 list_for_each_entry_safe(obj, next,
237                                          &our->our_invalidate_cb_list,
238                                          opo_invalidate_cb_list) {
239                         spin_lock(&obj->opo_lock);
240                         list_del_init(&obj->opo_invalidate_cb_list);
241                         spin_unlock(&obj->opo_lock);
242
243                         lu_object_put(env, &obj->opo_obj.do_lu);
244                 }
245
246                 if (env == &lenv)
247                         lu_env_fini(&lenv);
248         }
249
250         OBD_FREE_PTR(our);
251 }
252
253 static void
254 object_update_request_dump(const struct object_update_request *ourq,
255                            unsigned int mask)
256 {
257         unsigned int i;
258         size_t total_size = 0;
259
260         for (i = 0; i < ourq->ourq_count; i++) {
261                 struct object_update    *update;
262                 size_t                  size = 0;
263
264                 update = object_update_request_get(ourq, i, &size);
265                 LASSERT(update != NULL);
266                 CDEBUG(mask, "i = %u fid = "DFID" op = %s "
267                        "params = %d batchid = %llu size = %zu repsize %u\n",
268                        i, PFID(&update->ou_fid),
269                        update_op_str(update->ou_type),
270                        update->ou_params_count,
271                        update->ou_batchid, size,
272                        (unsigned)update->ou_result_size);
273
274                 total_size += size;
275         }
276
277         CDEBUG(mask, "updates = %p magic = %x count = %d size = %zu\n", ourq,
278                ourq->ourq_magic, ourq->ourq_count, total_size);
279 }
280
281 /**
282  * Prepare inline update request
283  *
284  * Prepare OUT update ptlrpc inline request, and the request usually includes
285  * one update buffer, which does not need bulk transfer.
286  *
287  * \param[in] env       execution environment
288  * \param[in] req       ptlrpc request
289  * \param[in] ours      sub osp_update_request to be packed
290  *
291  * \retval              0 if packing succeeds
292  * \retval              negative errno if packing fails
293  */
294 int osp_prep_inline_update_req(const struct lu_env *env,
295                                struct ptlrpc_request *req,
296                                struct osp_update_request *our,
297                                int repsize)
298 {
299         struct osp_update_request_sub *ours;
300         struct out_update_header *ouh;
301         __u32 update_req_size;
302         int rc;
303
304         ours = list_entry(our->our_req_list.next,
305                           struct osp_update_request_sub, ours_list);
306         update_req_size = object_update_request_size(ours->ours_req);
307         req_capsule_set_size(&req->rq_pill, &RMF_OUT_UPDATE_HEADER, RCL_CLIENT,
308                              update_req_size + sizeof(*ouh));
309
310         rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, OUT_UPDATE);
311         if (rc != 0)
312                 RETURN(rc);
313
314         ouh = req_capsule_client_get(&req->rq_pill, &RMF_OUT_UPDATE_HEADER);
315         ouh->ouh_magic = OUT_UPDATE_HEADER_MAGIC;
316         ouh->ouh_count = 1;
317         ouh->ouh_inline_length = update_req_size;
318         ouh->ouh_reply_size = repsize;
319
320         memcpy(ouh->ouh_inline_data, ours->ours_req, update_req_size);
321
322         req_capsule_set_size(&req->rq_pill, &RMF_OUT_UPDATE_REPLY,
323                              RCL_SERVER, repsize);
324
325         ptlrpc_request_set_replen(req);
326         req->rq_request_portal = OUT_PORTAL;
327         req->rq_reply_portal = OSC_REPLY_PORTAL;
328
329         RETURN(rc);
330 }
331
332 /**
333  * Prepare update request.
334  *
335  * Prepare OUT update ptlrpc request, and the request usually includes
336  * all of updates (stored in \param ureq) from one operation.
337  *
338  * \param[in] env       execution environment
339  * \param[in] imp       import on which ptlrpc request will be sent
340  * \param[in] ureq      hold all of updates which will be packed into the req
341  * \param[in] reqp      request to be created
342  *
343  * \retval              0 if preparation succeeds.
344  * \retval              negative errno if preparation fails.
345  */
346 int osp_prep_update_req(const struct lu_env *env, struct obd_import *imp,
347                         struct osp_update_request *our,
348                         struct ptlrpc_request **reqp)
349 {
350         struct ptlrpc_request           *req;
351         struct ptlrpc_bulk_desc         *desc;
352         struct osp_update_request_sub   *ours;
353         const struct object_update_request *ourq;
354         struct out_update_header        *ouh;
355         struct out_update_buffer        *oub;
356         __u32                           buf_count = 0;
357         int                             repsize = 0;
358         struct object_update_reply      *reply;
359         int                             rc, i;
360         int                             total = 0;
361         ENTRY;
362
363         list_for_each_entry(ours, &our->our_req_list, ours_list) {
364                 object_update_request_dump(ours->ours_req, D_INFO);
365
366                 ourq = ours->ours_req;
367                 for (i = 0; i < ourq->ourq_count; i++) {
368                         struct object_update    *update;
369                         size_t                  size = 0;
370
371
372                         /* XXX: it's very inefficient to lookup update
373                          *      this way, iterating from the beginning
374                          *      each time */
375                         update = object_update_request_get(ourq, i, &size);
376                         LASSERT(update != NULL);
377
378                         repsize += sizeof(reply->ourp_lens[0]);
379                         repsize += sizeof(struct object_update_result);
380                         repsize += update->ou_result_size;
381                 }
382
383                 buf_count++;
384         }
385         repsize += sizeof(*reply);
386         repsize = (repsize + OUT_UPDATE_REPLY_SIZE - 1) &
387                         ~(OUT_UPDATE_REPLY_SIZE - 1);
388         LASSERT(buf_count > 0);
389
390         req = ptlrpc_request_alloc(imp, &RQF_OUT_UPDATE);
391         if (req == NULL)
392                 RETURN(-ENOMEM);
393
394         if (buf_count == 1) {
395                 ours = list_entry(our->our_req_list.next,
396                                   struct osp_update_request_sub, ours_list);
397
398                 /* Let's check if it can be packed inline */
399                 if (object_update_request_size(ours->ours_req) +
400                     sizeof(struct out_update_header) <
401                                 OUT_UPDATE_MAX_INLINE_SIZE) {
402                         rc = osp_prep_inline_update_req(env, req, our, repsize);
403                         if (rc == 0)
404                                 *reqp = req;
405                         GOTO(out_req, rc);
406                 }
407         }
408
409         req_capsule_set_size(&req->rq_pill, &RMF_OUT_UPDATE_HEADER, RCL_CLIENT,
410                              sizeof(struct osp_update_request));
411
412         req_capsule_set_size(&req->rq_pill, &RMF_OUT_UPDATE_BUF, RCL_CLIENT,
413                              buf_count * sizeof(*oub));
414
415         rc = ptlrpc_request_pack(req, LUSTRE_MDS_VERSION, OUT_UPDATE);
416         if (rc != 0)
417                 GOTO(out_req, rc);
418
419         ouh = req_capsule_client_get(&req->rq_pill, &RMF_OUT_UPDATE_HEADER);
420         ouh->ouh_magic = OUT_UPDATE_HEADER_MAGIC;
421         ouh->ouh_count = buf_count;
422         ouh->ouh_inline_length = 0;
423         ouh->ouh_reply_size = repsize;
424         oub = req_capsule_client_get(&req->rq_pill, &RMF_OUT_UPDATE_BUF);
425         list_for_each_entry(ours, &our->our_req_list, ours_list) {
426                 oub->oub_size = ours->ours_req_size;
427                 oub++;
428         }
429
430         req->rq_bulk_write = 1;
431         desc = ptlrpc_prep_bulk_imp(req, buf_count,
432                 MD_MAX_BRW_SIZE >> LNET_MTU_BITS,
433                 PTLRPC_BULK_GET_SOURCE | PTLRPC_BULK_BUF_KVEC,
434                 MDS_BULK_PORTAL, &ptlrpc_bulk_kvec_ops);
435         if (desc == NULL)
436                 GOTO(out_req, rc = -ENOMEM);
437
438         /* NB req now owns desc and will free it when it gets freed */
439         list_for_each_entry(ours, &our->our_req_list, ours_list) {
440                 desc->bd_frag_ops->add_iov_frag(desc, ours->ours_req,
441                                                 ours->ours_req_size);
442                 total += ours->ours_req_size;
443         }
444         CDEBUG(D_OTHER, "total %d in %u\n", total, our->our_update_nr);
445
446         req_capsule_set_size(&req->rq_pill, &RMF_OUT_UPDATE_REPLY,
447                              RCL_SERVER, repsize);
448
449         ptlrpc_request_set_replen(req);
450         req->rq_request_portal = OUT_PORTAL;
451         req->rq_reply_portal = OSC_REPLY_PORTAL;
452         *reqp = req;
453
454 out_req:
455         if (rc < 0)
456                 ptlrpc_req_finished(req);
457
458         RETURN(rc);
459 }
460
461 /**
462  * Send update RPC.
463  *
464  * Send update request to the remote MDT synchronously.
465  *
466  * \param[in] env       execution environment
467  * \param[in] imp       import on which ptlrpc request will be sent
468  * \param[in] our       hold all of updates which will be packed into the req
469  * \param[in] reqp      request to be created
470  *
471  * \retval              0 if RPC succeeds.
472  * \retval              negative errno if RPC fails.
473  */
474 int osp_remote_sync(const struct lu_env *env, struct osp_device *osp,
475                     struct osp_update_request *our,
476                     struct ptlrpc_request **reqp)
477 {
478         struct obd_import       *imp = osp->opd_obd->u.cli.cl_import;
479         struct ptlrpc_request   *req = NULL;
480         int                     rc;
481         ENTRY;
482
483         rc = osp_prep_update_req(env, imp, our, &req);
484         if (rc != 0)
485                 RETURN(rc);
486
487         osp_set_req_replay(osp, req);
488         req->rq_allow_intr = 1;
489
490         /* Note: some dt index api might return non-zero result here, like
491          * osd_index_ea_lookup, so we should only check rc < 0 here */
492         rc = ptlrpc_queue_wait(req);
493         our->our_rc = rc;
494         if (rc < 0 || reqp == NULL)
495                 ptlrpc_req_finished(req);
496         else
497                 *reqp = req;
498
499         RETURN(rc);
500 }
501
502 /**
503  * Invalidate all objects in the osp thandle
504  *
505  * invalidate all of objects in the update request, which will be called
506  * when the transaction is aborted.
507  *
508  * \param[in] oth       osp thandle.
509  */
510 static void osp_thandle_invalidate_object(const struct lu_env *env,
511                                           struct osp_thandle *oth,
512                                           int result)
513 {
514         struct osp_update_request *our = oth->ot_our;
515         struct osp_object *obj;
516         struct osp_object *next;
517
518         if (our == NULL)
519                 return;
520
521         list_for_each_entry_safe(obj, next, &our->our_invalidate_cb_list,
522                                  opo_invalidate_cb_list) {
523                 if (result < 0)
524                         osp_invalidate(env, &obj->opo_obj);
525
526                 spin_lock(&obj->opo_lock);
527                 list_del_init(&obj->opo_invalidate_cb_list);
528                 spin_unlock(&obj->opo_lock);
529
530                 lu_object_put(env, &obj->opo_obj.do_lu);
531         }
532 }
533
534 static void osp_trans_stop_cb(const struct lu_env *env,
535                               struct osp_thandle *oth, int result)
536 {
537         struct dt_txn_commit_cb *dcb;
538         struct dt_txn_commit_cb *tmp;
539
540         /* call per-transaction stop callbacks if any */
541         list_for_each_entry_safe(dcb, tmp, &oth->ot_stop_dcb_list,
542                                  dcb_linkage) {
543                 LASSERTF(dcb->dcb_magic == TRANS_COMMIT_CB_MAGIC,
544                          "commit callback entry: magic=%x name='%s'\n",
545                          dcb->dcb_magic, dcb->dcb_name);
546                 list_del_init(&dcb->dcb_linkage);
547                 dcb->dcb_func(NULL, &oth->ot_super, dcb, result);
548         }
549
550         osp_thandle_invalidate_object(env, oth, result);
551 }
552
553 /**
554  * Allocate an osp request and initialize it with the given parameters.
555  *
556  * \param[in] obj               pointer to the operation target
557  * \param[in] data              pointer to the data used by the interpreter
558  * \param[in] interpreter       pointer to the interpreter function
559  *
560  * \retval                      pointer to the asychronous request
561  * \retval                      NULL if the allocation failed
562  */
563 static struct osp_update_callback *
564 osp_update_callback_init(struct osp_object *obj, void *data,
565                          osp_update_interpreter_t interpreter)
566 {
567         struct osp_update_callback *ouc;
568
569         OBD_ALLOC_PTR(ouc);
570         if (ouc == NULL)
571                 return NULL;
572
573         lu_object_get(osp2lu_obj(obj));
574         INIT_LIST_HEAD(&ouc->ouc_list);
575         ouc->ouc_obj = obj;
576         ouc->ouc_data = data;
577         ouc->ouc_interpreter = interpreter;
578
579         return ouc;
580 }
581
582 /**
583  * Destroy the osp_update_callback.
584  *
585  * \param[in] env       pointer to the thread context
586  * \param[in] ouc       pointer to osp_update_callback
587  */
588 static void osp_update_callback_fini(const struct lu_env *env,
589                                      struct osp_update_callback *ouc)
590 {
591         LASSERT(list_empty(&ouc->ouc_list));
592
593         lu_object_put(env, osp2lu_obj(ouc->ouc_obj));
594         OBD_FREE_PTR(ouc);
595 }
596
597 /**
598  * Interpret the packaged OUT RPC results.
599  *
600  * For every packaged sub-request, call its registered interpreter function.
601  * Then destroy the sub-request.
602  *
603  * \param[in] env       pointer to the thread context
604  * \param[in] req       pointer to the RPC
605  * \param[in] arg       pointer to data used by the interpreter
606  * \param[in] rc        the RPC return value
607  *
608  * \retval              0 for success
609  * \retval              negative error number on failure
610  */
611 static int osp_update_interpret(const struct lu_env *env,
612                                 struct ptlrpc_request *req, void *arg, int rc)
613 {
614         struct object_update_reply      *reply  = NULL;
615         struct osp_update_args          *oaua   = arg;
616         struct osp_update_request       *our = oaua->oaua_update;
617         struct osp_thandle              *oth;
618         struct osp_update_callback      *ouc;
619         struct osp_update_callback      *next;
620         int                              count  = 0;
621         int                              index  = 0;
622         int                              rc1    = 0;
623
624         ENTRY;
625
626         if (our == NULL)
627                 RETURN(0);
628
629         /* Sigh env might be NULL in some cases, see
630          * this calling path.
631          * osp_send_update_thread()
632          *  ptlrpc_set_wait() ----> null env.
633          *   ptlrpc_check_set()
634          *    osp_update_interpret()
635          * Let's use env in oaua for this case.
636          */
637         if (env == NULL)
638                 env = oaua->oaua_update_env;
639
640         oaua->oaua_update = NULL;
641         oth = our->our_th;
642         if (oaua->oaua_flow_control) {
643                 struct osp_device *osp;
644
645                 LASSERT(oth != NULL);
646                 osp = dt2osp_dev(oth->ot_super.th_dev);
647                 obd_put_request_slot(&osp->opd_obd->u.cli);
648         }
649
650         /* Unpack the results from the reply message. */
651         if (req->rq_repmsg != NULL && req->rq_replied) {
652                 reply = req_capsule_server_sized_get(&req->rq_pill,
653                                                      &RMF_OUT_UPDATE_REPLY,
654                                                      OUT_UPDATE_REPLY_SIZE);
655                 if (reply == NULL || reply->ourp_magic != UPDATE_REPLY_MAGIC) {
656                         if (rc == 0)
657                                 rc = -EPROTO;
658                 } else {
659                         count = reply->ourp_count;
660                 }
661         }
662
663         list_for_each_entry_safe(ouc, next, &our->our_cb_items, ouc_list) {
664                 list_del_init(&ouc->ouc_list);
665
666                 /* The peer may only have handled some requests (indicated
667                  * by the 'count') in the packaged OUT RPC, we can only get
668                  * results for the handled part. */
669                 if (index < count && reply->ourp_lens[index] > 0 && rc >= 0) {
670                         struct object_update_result *result;
671
672                         result = object_update_result_get(reply, index, NULL);
673                         if (result == NULL)
674                                 rc1 = rc = -EPROTO;
675                         else
676                                 rc1 = rc = result->our_rc;
677                 } else if (rc1 >= 0) {
678                         /* The peer did not handle these request, let's return
679                          * -EINVAL to update interpret for now */
680                         if (rc >= 0)
681                                 rc1 = -EINVAL;
682                         else
683                                 rc1 = rc;
684                 }
685
686                 if (ouc->ouc_interpreter != NULL)
687                         ouc->ouc_interpreter(env, reply, req, ouc->ouc_obj,
688                                              ouc->ouc_data, index, rc1);
689
690                 osp_update_callback_fini(env, ouc);
691                 index++;
692         }
693
694         if (oaua->oaua_count != NULL && atomic_dec_and_test(oaua->oaua_count))
695                 wake_up_all(oaua->oaua_waitq);
696
697         if (oth != NULL) {
698                 /* oth and osp_update_requests will be destoryed in
699                  * osp_thandle_put */
700                 osp_trans_stop_cb(env, oth, rc);
701                 osp_thandle_put(env, oth);
702         } else {
703                 osp_update_request_destroy(env, our);
704         }
705
706         RETURN(rc);
707 }
708
709 /**
710  * Pack all the requests in the shared asynchronous idempotent request queue
711  * into a single OUT RPC that will be given to the background ptlrpcd daemon.
712  *
713  * \param[in] env       pointer to the thread context
714  * \param[in] osp       pointer to the OSP device
715  * \param[in] our       pointer to the shared queue
716  *
717  * \retval              0 for success
718  * \retval              negative error number on failure
719  */
720 int osp_unplug_async_request(const struct lu_env *env,
721                              struct osp_device *osp,
722                              struct osp_update_request *our)
723 {
724         struct osp_update_args  *args;
725         struct ptlrpc_request   *req = NULL;
726         int                      rc;
727
728         rc = osp_prep_update_req(env, osp->opd_obd->u.cli.cl_import,
729                                  our, &req);
730         if (rc != 0) {
731                 struct osp_update_callback *ouc;
732                 struct osp_update_callback *next;
733
734                 list_for_each_entry_safe(ouc, next,
735                                          &our->our_cb_items, ouc_list) {
736                         list_del_init(&ouc->ouc_list);
737                         if (ouc->ouc_interpreter != NULL)
738                                 ouc->ouc_interpreter(env, NULL, NULL,
739                                                      ouc->ouc_obj,
740                                                      ouc->ouc_data, 0, rc);
741                         osp_update_callback_fini(env, ouc);
742                 }
743                 osp_update_request_destroy(env, our);
744         } else {
745                 args = ptlrpc_req_async_args(req);
746                 args->oaua_update = our;
747                 args->oaua_count = NULL;
748                 args->oaua_waitq = NULL;
749                 /* Note: this is asynchronous call for the request, so the
750                  * interrupte cb and current function will be different
751                  * thread, so we need use different env */
752                 args->oaua_update_env = NULL;
753                 args->oaua_flow_control = false;
754                 req->rq_interpret_reply = osp_update_interpret;
755                 ptlrpcd_add_req(req);
756         }
757
758         return rc;
759 }
760
761 /**
762  * Find or create (if NOT exist or purged) the shared asynchronous idempotent
763  * request queue - osp_device::opd_async_requests.
764  *
765  * If the osp_device::opd_async_requests is not NULL, then return it directly;
766  * otherwise create new osp_update_request and attach it to opd_async_requests.
767  *
768  * \param[in] osp       pointer to the OSP device
769  *
770  * \retval              pointer to the shared queue
771  * \retval              negative error number on failure
772  */
773 static struct osp_update_request *
774 osp_find_or_create_async_update_request(struct osp_device *osp)
775 {
776         struct osp_update_request *our = osp->opd_async_requests;
777
778         if (our != NULL)
779                 return our;
780
781         our = osp_update_request_create(&osp->opd_dt_dev);
782         if (IS_ERR(our))
783                 return our;
784
785         osp->opd_async_requests = our;
786
787         return our;
788 }
789
790 /**
791  * Insert an osp_update_callback into the osp_update_request.
792  *
793  * Insert an osp_update_callback to the osp_update_request. Usually each update
794  * in the osp_update_request will have one correspondent callback, and these
795  * callbacks will be called in rq_interpret_reply.
796  *
797  * \param[in] env               pointer to the thread context
798  * \param[in] obj               pointer to the operation target object
799  * \param[in] data              pointer to the data used by the interpreter
800  * \param[in] interpreter       pointer to the interpreter function
801  *
802  * \retval                      0 for success
803  * \retval                      negative error number on failure
804  */
805 int osp_insert_update_callback(const struct lu_env *env,
806                                struct osp_update_request *our,
807                                struct osp_object *obj, void *data,
808                                osp_update_interpreter_t interpreter)
809 {
810         struct osp_update_callback  *ouc;
811
812         ouc = osp_update_callback_init(obj, data, interpreter);
813         if (ouc == NULL)
814                 RETURN(-ENOMEM);
815
816         list_add_tail(&ouc->ouc_list, &our->our_cb_items);
817
818         return 0;
819 }
820
821 /**
822  * Insert an asynchronous idempotent request to the shared request queue that
823  * is attached to the osp_device.
824  *
825  * This function generates a new osp_async_request with the given parameters,
826  * then tries to insert the request into the osp_device-based shared request
827  * queue. If the queue is full, then triggers the packaged OUT RPC to purge
828  * the shared queue firstly, and then re-tries.
829  *
830  * NOTE: must hold the osp::opd_async_requests_mutex to serialize concurrent
831  *       osp_insert_async_request call from others.
832  *
833  * \param[in] env               pointer to the thread context
834  * \param[in] op                operation type, see 'enum update_type'
835  * \param[in] obj               pointer to the operation target
836  * \param[in] count             array size of the subsequent \a lens and \a bufs
837  * \param[in] lens              buffer length array for the subsequent \a bufs
838  * \param[in] bufs              the buffers to compose the request
839  * \param[in] data              pointer to the data used by the interpreter
840  * \param[in] repsize           how many bytes the caller allocated for \a data
841  * \param[in] interpreter       pointer to the interpreter function
842  *
843  * \retval                      0 for success
844  * \retval                      negative error number on failure
845  */
846 int osp_insert_async_request(const struct lu_env *env, enum update_type op,
847                              struct osp_object *obj, int count,
848                              __u16 *lens, const void **bufs,
849                              void *data, __u32 repsize,
850                              osp_update_interpreter_t interpreter)
851 {
852         struct osp_device               *osp;
853         struct osp_update_request       *our;
854         struct object_update            *object_update;
855         size_t                          max_update_size;
856         struct object_update_request    *ureq;
857         struct osp_update_request_sub   *ours;
858         int                             rc = 0;
859         ENTRY;
860
861         osp = lu2osp_dev(osp2lu_obj(obj)->lo_dev);
862         our = osp_find_or_create_async_update_request(osp);
863         if (IS_ERR(our))
864                 RETURN(PTR_ERR(our));
865
866 again:
867         ours = osp_current_object_update_request(our);
868
869         ureq = ours->ours_req;
870         max_update_size = ours->ours_req_size -
871                           object_update_request_size(ureq);
872
873         object_update = update_buffer_get_update(ureq, ureq->ourq_count);
874         rc = out_update_pack(env, object_update, &max_update_size, op,
875                              lu_object_fid(osp2lu_obj(obj)), count, lens, bufs,
876                              repsize);
877         /* The queue is full. */
878         if (rc == -E2BIG) {
879                 osp->opd_async_requests = NULL;
880                 mutex_unlock(&osp->opd_async_requests_mutex);
881
882                 rc = osp_unplug_async_request(env, osp, our);
883                 mutex_lock(&osp->opd_async_requests_mutex);
884                 if (rc != 0)
885                         RETURN(rc);
886
887                 our = osp_find_or_create_async_update_request(osp);
888                 if (IS_ERR(our))
889                         RETURN(PTR_ERR(our));
890
891                 goto again;
892         } else {
893                 if (rc < 0)
894                         RETURN(rc);
895
896                 ureq->ourq_count++;
897                 our->our_update_nr++;
898         }
899
900         rc = osp_insert_update_callback(env, our, obj, data, interpreter);
901
902         RETURN(rc);
903 }
904
905 int osp_trans_update_request_create(struct thandle *th)
906 {
907         struct osp_thandle              *oth = thandle_to_osp_thandle(th);
908         struct osp_update_request       *our;
909
910         if (oth->ot_our != NULL)
911                 return 0;
912
913         our = osp_update_request_create(th->th_dev);
914         if (IS_ERR(our)) {
915                 th->th_result = PTR_ERR(our);
916                 return PTR_ERR(our);
917         }
918
919         oth->ot_our = our;
920         our->our_th = oth;
921
922         return 0;
923 }
924
925 void osp_thandle_destroy(const struct lu_env *env,
926                          struct osp_thandle *oth)
927 {
928         LASSERT(oth->ot_magic == OSP_THANDLE_MAGIC);
929         LASSERT(list_empty(&oth->ot_commit_dcb_list));
930         LASSERT(list_empty(&oth->ot_stop_dcb_list));
931         if (oth->ot_our != NULL)
932                 osp_update_request_destroy(env, oth->ot_our);
933         OBD_FREE_PTR(oth);
934 }
935
936 /**
937  * The OSP layer dt_device_operations::dt_trans_create() interface
938  * to create a transaction.
939  *
940  * There are two kinds of transactions that will involve OSP:
941  *
942  * 1) If the transaction only contains the updates on remote server
943  *    (MDT or OST), such as re-generating the lost OST-object for
944  *    LFSCK, then it is a remote transaction. For remote transaction,
945  *    the upper layer caller (such as the LFSCK engine) will call the
946  *    dt_trans_create() (with the OSP dt_device as the parameter),
947  *    then the call will be directed to the osp_trans_create() that
948  *    creates the transaction handler and returns it to the caller.
949  *
950  * 2) If the transcation contains both local and remote updates,
951  *    such as cross MDTs create under DNE mode, then the upper layer
952  *    caller will not trigger osp_trans_create(). Instead, it will
953  *    call dt_trans_create() on other dt_device, such as LOD that
954  *    will generate the transaction handler. Such handler will be
955  *    used by the whole transaction in subsequent sub-operations.
956  *
957  * \param[in] env       pointer to the thread context
958  * \param[in] d         pointer to the OSP dt_device
959  *
960  * \retval              pointer to the transaction handler
961  * \retval              negative error number on failure
962  */
963 struct thandle *osp_trans_create(const struct lu_env *env, struct dt_device *d)
964 {
965         struct osp_thandle              *oth;
966         struct thandle                  *th = NULL;
967         ENTRY;
968
969         OBD_ALLOC_PTR(oth);
970         if (unlikely(oth == NULL))
971                 RETURN(ERR_PTR(-ENOMEM));
972
973         oth->ot_magic = OSP_THANDLE_MAGIC;
974         th = &oth->ot_super;
975         th->th_dev = d;
976         th->th_tags = LCT_TX_HANDLE;
977
978         atomic_set(&oth->ot_refcount, 1);
979         INIT_LIST_HEAD(&oth->ot_commit_dcb_list);
980         INIT_LIST_HEAD(&oth->ot_stop_dcb_list);
981
982         RETURN(th);
983 }
984
985 /**
986  * Add commit callback to transaction.
987  *
988  * Add commit callback to the osp thandle, which will be called
989  * when the thandle is committed remotely.
990  *
991  * \param[in] th        the thandle
992  * \param[in] dcb       commit callback structure
993  *
994  * \retval              only return 0 for now.
995  */
996 int osp_trans_cb_add(struct thandle *th, struct dt_txn_commit_cb *dcb)
997 {
998         struct osp_thandle *oth = thandle_to_osp_thandle(th);
999
1000         LASSERT(dcb->dcb_magic == TRANS_COMMIT_CB_MAGIC);
1001         LASSERT(&dcb->dcb_func != NULL);
1002         if (dcb->dcb_flags & DCB_TRANS_STOP)
1003                 list_add(&dcb->dcb_linkage, &oth->ot_stop_dcb_list);
1004         else
1005                 list_add(&dcb->dcb_linkage, &oth->ot_commit_dcb_list);
1006         return 0;
1007 }
1008
1009 static void osp_trans_commit_cb(struct osp_thandle *oth, int result)
1010 {
1011         struct dt_txn_commit_cb *dcb;
1012         struct dt_txn_commit_cb *tmp;
1013
1014         LASSERT(atomic_read(&oth->ot_refcount) > 0);
1015         /* call per-transaction callbacks if any */
1016         list_for_each_entry_safe(dcb, tmp, &oth->ot_commit_dcb_list,
1017                                  dcb_linkage) {
1018                 LASSERTF(dcb->dcb_magic == TRANS_COMMIT_CB_MAGIC,
1019                          "commit callback entry: magic=%x name='%s'\n",
1020                          dcb->dcb_magic, dcb->dcb_name);
1021                 list_del_init(&dcb->dcb_linkage);
1022                 dcb->dcb_func(NULL, &oth->ot_super, dcb, result);
1023         }
1024 }
1025
1026 static void osp_request_commit_cb(struct ptlrpc_request *req)
1027 {
1028         struct thandle          *th = req->rq_cb_data;
1029         struct osp_thandle      *oth;
1030         __u64                   last_committed_transno = 0;
1031         int                     result = req->rq_status;
1032         ENTRY;
1033
1034         if (th == NULL)
1035                 RETURN_EXIT;
1036
1037         oth = thandle_to_osp_thandle(th);
1038         if (req->rq_repmsg != NULL &&
1039             lustre_msg_get_last_committed(req->rq_repmsg))
1040                 last_committed_transno =
1041                         lustre_msg_get_last_committed(req->rq_repmsg);
1042
1043         if (last_committed_transno <
1044                 req->rq_import->imp_peer_committed_transno)
1045                 last_committed_transno =
1046                         req->rq_import->imp_peer_committed_transno;
1047
1048         CDEBUG(D_HA, "trans no %llu committed transno %llu\n",
1049                req->rq_transno, last_committed_transno);
1050
1051         /* If the transaction is not really committed, mark result = 1 */
1052         if (req->rq_transno != 0 &&
1053             (req->rq_transno > last_committed_transno) && result == 0)
1054                 result = 1;
1055
1056         osp_trans_commit_cb(oth, result);
1057         req->rq_committed = 1;
1058         osp_thandle_put(NULL, oth);
1059         EXIT;
1060 }
1061
1062 /**
1063  * callback of osp transaction
1064  *
1065  * Call all of callbacks for this osp thandle. This will only be
1066  * called in error handler path. In the normal processing path,
1067  * these callback will be called in osp_request_commit_cb() and
1068  * osp_update_interpret().
1069  *
1070  * \param [in] env      execution environment
1071  * \param [in] oth      osp thandle
1072  * \param [in] rc       result of the osp thandle
1073  */
1074 void osp_trans_callback(const struct lu_env *env,
1075                         struct osp_thandle *oth, int rc)
1076 {
1077         struct osp_update_callback *ouc;
1078         struct osp_update_callback *next;
1079
1080         if (oth->ot_our != NULL) {
1081                 list_for_each_entry_safe(ouc, next,
1082                                          &oth->ot_our->our_cb_items, ouc_list) {
1083                         list_del_init(&ouc->ouc_list);
1084                         if (ouc->ouc_interpreter != NULL)
1085                                 ouc->ouc_interpreter(env, NULL, NULL,
1086                                                      ouc->ouc_obj,
1087                                                      ouc->ouc_data, 0, rc);
1088                         osp_update_callback_fini(env, ouc);
1089                 }
1090         }
1091         osp_trans_stop_cb(env, oth, rc);
1092         osp_trans_commit_cb(oth, rc);
1093 }
1094
1095 /**
1096  * Send the request for remote updates.
1097  *
1098  * Send updates to the remote MDT. Prepare the request by osp_update_req
1099  * and send them to remote MDT, for sync request, it will wait
1100  * until the reply return, otherwise hand it to ptlrpcd.
1101  *
1102  * Please refer to osp_trans_create() for transaction type.
1103  *
1104  * \param[in] env               pointer to the thread context
1105  * \param[in] osp               pointer to the OSP device
1106  * \param[in] our               pointer to the osp_update_request
1107  *
1108  * \retval                      0 for success
1109  * \retval                      negative error number on failure
1110  */
1111 static int osp_send_update_req(const struct lu_env *env,
1112                                struct osp_device *osp,
1113                                struct osp_update_request *our)
1114 {
1115         struct osp_update_args  *args;
1116         struct ptlrpc_request   *req;
1117         struct osp_thandle      *oth = our->our_th;
1118         int     rc = 0;
1119         ENTRY;
1120
1121         LASSERT(oth != NULL);
1122         rc = osp_prep_update_req(env, osp->opd_obd->u.cli.cl_import,
1123                                  our, &req);
1124         if (rc != 0) {
1125                 osp_trans_callback(env, oth, rc);
1126                 RETURN(rc);
1127         }
1128
1129         args = ptlrpc_req_async_args(req);
1130         args->oaua_update = our;
1131         /* set env to NULL, in case the interrupt cb and current function
1132          * are in different thread */
1133         args->oaua_update_env = NULL;
1134         osp_thandle_get(oth); /* hold for update interpret */
1135         req->rq_interpret_reply = osp_update_interpret;
1136         if (!oth->ot_super.th_wait_submit && !oth->ot_super.th_sync) {
1137                 if (!osp->opd_imp_active || !osp->opd_imp_connected) {
1138                         osp_trans_callback(env, oth, rc);
1139                         osp_thandle_put(env, oth);
1140                         GOTO(out, rc = -ENOTCONN);
1141                 }
1142
1143                 rc = obd_get_request_slot(&osp->opd_obd->u.cli);
1144                 if (rc != 0) {
1145                         osp_trans_callback(env, oth, rc);
1146                         osp_thandle_put(env, oth);
1147                         GOTO(out, rc = -ENOTCONN);
1148                 }
1149                 args->oaua_flow_control = true;
1150
1151                 if (!osp->opd_connect_mdt) {
1152                         down_read(&osp->opd_async_updates_rwsem);
1153                         args->oaua_count = &osp->opd_async_updates_count;
1154                         args->oaua_waitq = &osp->opd_syn_barrier_waitq;
1155                         up_read(&osp->opd_async_updates_rwsem);
1156                         atomic_inc(args->oaua_count);
1157                 }
1158
1159                 ptlrpcd_add_req(req);
1160                 req = NULL;
1161         } else {
1162                 osp_thandle_get(oth); /* hold for commit callback */
1163                 req->rq_commit_cb = osp_request_commit_cb;
1164                 req->rq_cb_data = &oth->ot_super;
1165                 args->oaua_flow_control = false;
1166
1167                 /* If the transaction is created during MDT recoverying
1168                  * process, it means this is an recovery update, we need
1169                  * to let OSP send it anyway without checking recoverying
1170                  * status, in case the other target is being recoveried
1171                  * at the same time, and if we wait here for the import
1172                  * to be recoveryed, it might cause deadlock */
1173                 osp_set_req_replay(osp, req);
1174
1175                 /* Because this req will be synchronus, i.e. it will be called
1176                  * in the same thread, so it will be safe to use current
1177                  * env */
1178                 args->oaua_update_env = env;
1179                 if (osp->opd_connect_mdt)
1180                         osp_get_rpc_lock(osp);
1181                 rc = ptlrpc_queue_wait(req);
1182                 if (osp->opd_connect_mdt)
1183                         osp_put_rpc_lock(osp);
1184                 if ((rc == -ENOMEM && req->rq_set == NULL) ||
1185                     (req->rq_transno == 0 && !req->rq_committed)) {
1186                         if (args->oaua_update != NULL) {
1187                                 /* If osp_update_interpret is not being called,
1188                                  * release the osp_thandle */
1189                                 args->oaua_update = NULL;
1190                                 osp_thandle_put(env, oth);
1191                         }
1192
1193                         req->rq_cb_data = NULL;
1194                         rc = rc == 0 ? req->rq_status : rc;
1195                         osp_trans_callback(env, oth, rc);
1196                         osp_thandle_put(env, oth);
1197                         GOTO(out, rc);
1198                 }
1199         }
1200 out:
1201         if (req != NULL)
1202                 ptlrpc_req_finished(req);
1203
1204         RETURN(rc);
1205 }
1206
1207 /**
1208  * Get local thandle for osp_thandle
1209  *
1210  * Get the local OSD thandle from the OSP thandle. Currently, there
1211  * are a few OSP API (osp_object_create() and osp_sync_add()) needs
1212  * to update the object on local OSD device.
1213  *
1214  * If the osp_thandle comes from normal stack (MDD->LOD->OSP), then
1215  * we will get local thandle by thandle_get_sub_by_dt.
1216  *
1217  * If the osp_thandle is remote thandle (th_top == NULL, only used
1218  * by LFSCK), then it will create a local thandle, and stop it in
1219  * osp_trans_stop(). And this only happens on OSP for OST.
1220  *
1221  * These are temporary solution, once OSP accessing OSD object is
1222  * being fixed properly, this function should be removed. XXX
1223  *
1224  * \param[in] env               pointer to the thread context
1225  * \param[in] th                pointer to the transaction handler
1226  * \param[in] dt                pointer to the OSP device
1227  *
1228  * \retval                      pointer to the local thandle
1229  * \retval                      ERR_PTR(errno) if it fails.
1230  **/
1231 struct thandle *osp_get_storage_thandle(const struct lu_env *env,
1232                                         struct thandle *th,
1233                                         struct osp_device *osp)
1234 {
1235         struct osp_thandle      *oth;
1236         struct thandle          *local_th;
1237
1238         if (th->th_top != NULL)
1239                 return thandle_get_sub_by_dt(env, th->th_top,
1240                                              osp->opd_storage);
1241
1242         LASSERT(!osp->opd_connect_mdt);
1243         oth = thandle_to_osp_thandle(th);
1244         if (oth->ot_storage_th != NULL)
1245                 return oth->ot_storage_th;
1246
1247         local_th = dt_trans_create(env, osp->opd_storage);
1248         if (IS_ERR(local_th))
1249                 return local_th;
1250
1251         oth->ot_storage_th = local_th;
1252
1253         return local_th;
1254 }
1255
1256 /**
1257  * Set version for the transaction
1258  *
1259  * Set the version for the transaction and add the request to
1260  * the sending list, then after transaction stop, the request
1261  * will be picked in the order of version, by sending thread.
1262  *
1263  * \param [in] oth      osp thandle to be set version.
1264  *
1265  * \retval              0 if set version succeeds
1266  *                      negative errno if set version fails.
1267  */
1268 int osp_check_and_set_rpc_version(struct osp_thandle *oth,
1269                                   struct osp_object *obj)
1270 {
1271         struct osp_device *osp = dt2osp_dev(oth->ot_super.th_dev);
1272         struct osp_updates *ou = osp->opd_update;
1273
1274         if (ou == NULL)
1275                 return -EIO;
1276
1277         if (oth->ot_our->our_version != 0)
1278                 return 0;
1279
1280         spin_lock(&ou->ou_lock);
1281         spin_lock(&oth->ot_our->our_list_lock);
1282         if (obj->opo_stale) {
1283                 spin_unlock(&oth->ot_our->our_list_lock);
1284                 spin_unlock(&ou->ou_lock);
1285                 return -ESTALE;
1286         }
1287
1288         /* Assign the version and add it to the sending list */
1289         osp_thandle_get(oth);
1290         oth->ot_our->our_version = ou->ou_version++;
1291         list_add_tail(&oth->ot_our->our_list,
1292                       &osp->opd_update->ou_list);
1293         oth->ot_our->our_req_ready = 0;
1294         spin_unlock(&oth->ot_our->our_list_lock);
1295         spin_unlock(&ou->ou_lock);
1296
1297         LASSERT(oth->ot_super.th_wait_submit == 1);
1298         CDEBUG(D_INFO, "%s: version %llu oth:version %p:%llu\n",
1299                osp->opd_obd->obd_name, ou->ou_version, oth,
1300                oth->ot_our->our_version);
1301
1302         return 0;
1303 }
1304
1305 /**
1306  * Get next OSP update request in the sending list
1307  * Get next OSP update request in the sending list by version number, next
1308  * request will be
1309  * 1. transaction which does not have a version number.
1310  * 2. transaction whose version == opd_rpc_version.
1311  *
1312  * \param [in] ou       osp update structure.
1313  * \param [out] ourp    the pointer holding the next update request.
1314  *
1315  * \retval              true if getting the next transaction.
1316  * \retval              false if not getting the next transaction.
1317  */
1318 static bool
1319 osp_get_next_request(struct osp_updates *ou, struct osp_update_request **ourp)
1320 {
1321         struct osp_update_request *our;
1322         struct osp_update_request *tmp;
1323         bool                    got_req = false;
1324
1325         spin_lock(&ou->ou_lock);
1326         list_for_each_entry_safe(our, tmp, &ou->ou_list, our_list) {
1327                 LASSERT(our->our_th != NULL);
1328                 CDEBUG(D_HA, "ou %p version %llu rpc_version %llu\n",
1329                        ou, our->our_version, ou->ou_rpc_version);
1330                 spin_lock(&our->our_list_lock);
1331                 /* Find next osp_update_request in the list */
1332                 if (our->our_version == ou->ou_rpc_version &&
1333                     our->our_req_ready) {
1334                         list_del_init(&our->our_list);
1335                         spin_unlock(&our->our_list_lock);
1336                         *ourp = our;
1337                         got_req = true;
1338                         break;
1339                 }
1340                 spin_unlock(&our->our_list_lock);
1341         }
1342         spin_unlock(&ou->ou_lock);
1343
1344         return got_req;
1345 }
1346
1347 /**
1348  * Invalidate update request
1349  *
1350  * Invalidate update request in the OSP sending list, so all of
1351  * requests in the sending list will return error, which happens
1352  * when it finds one update (with writing llog) requests fails or
1353  * the OSP is evicted by remote target. see osp_send_update_thread().
1354  *
1355  * \param[in] osp       OSP device whose update requests will be
1356  *                      invalidated.
1357  **/
1358 void osp_invalidate_request(struct osp_device *osp)
1359 {
1360         struct lu_env env;
1361         struct osp_updates *ou = osp->opd_update;
1362         struct osp_update_request *our;
1363         struct osp_update_request *tmp;
1364         LIST_HEAD(list);
1365         int                     rc;
1366         ENTRY;
1367
1368         if (ou == NULL)
1369                 return;
1370
1371         rc = lu_env_init(&env, osp->opd_dt_dev.dd_lu_dev.ld_type->ldt_ctx_tags);
1372         if (rc < 0) {
1373                 CERROR("%s: init env error: rc = %d\n", osp->opd_obd->obd_name,
1374                        rc);
1375                 return;
1376         }
1377
1378         INIT_LIST_HEAD(&list);
1379
1380         spin_lock(&ou->ou_lock);
1381         /* invalidate all of request in the sending list */
1382         list_for_each_entry_safe(our, tmp, &ou->ou_list, our_list) {
1383                 spin_lock(&our->our_list_lock);
1384                 if (our->our_req_ready)
1385                         list_move(&our->our_list, &list);
1386                 else
1387                         list_del_init(&our->our_list);
1388
1389                 if (our->our_th->ot_super.th_result == 0)
1390                         our->our_th->ot_super.th_result = -EIO;
1391
1392                 if (our->our_version >= ou->ou_rpc_version)
1393                         ou->ou_rpc_version = our->our_version + 1;
1394                 spin_unlock(&our->our_list_lock);
1395
1396                 CDEBUG(D_HA, "%s invalidate our %p\n", osp->opd_obd->obd_name,
1397                        our);
1398         }
1399
1400         spin_unlock(&ou->ou_lock);
1401
1402         /* invalidate all of request in the sending list */
1403         list_for_each_entry_safe(our, tmp, &list, our_list) {
1404                 spin_lock(&our->our_list_lock);
1405                 list_del_init(&our->our_list);
1406                 spin_unlock(&our->our_list_lock);
1407                 osp_trans_callback(&env, our->our_th,
1408                                    our->our_th->ot_super.th_result);
1409                 osp_thandle_put(&env, our->our_th);
1410         }
1411         lu_env_fini(&env);
1412 }
1413
1414 /**
1415  * Sending update thread
1416  *
1417  * Create thread to send update request to other MDTs, this thread will pull
1418  * out update request from the list in OSP by version number, i.e. it will
1419  * make sure the update request with lower version number will be sent first.
1420  *
1421  * \param[in] arg       hold the OSP device.
1422  *
1423  * \retval              0 if the thread is created successfully.
1424  * \retal               negative error if the thread is not created
1425  *                      successfully.
1426  */
1427 int osp_send_update_thread(void *arg)
1428 {
1429         struct lu_env           env;
1430         struct osp_device       *osp = arg;
1431         struct l_wait_info       lwi = { 0 };
1432         struct osp_updates      *ou = osp->opd_update;
1433         struct ptlrpc_thread    *thread = &osp->opd_update_thread;
1434         struct osp_update_request *our = NULL;
1435         int                     rc;
1436         ENTRY;
1437
1438         LASSERT(ou != NULL);
1439         rc = lu_env_init(&env, osp->opd_dt_dev.dd_lu_dev.ld_type->ldt_ctx_tags);
1440         if (rc < 0) {
1441                 CERROR("%s: init env error: rc = %d\n", osp->opd_obd->obd_name,
1442                        rc);
1443                 RETURN(rc);
1444         }
1445
1446         thread->t_flags = SVC_RUNNING;
1447         wake_up(&thread->t_ctl_waitq);
1448         while (1) {
1449                 our = NULL;
1450                 l_wait_event(ou->ou_waitq,
1451                              !osp_send_update_thread_running(osp) ||
1452                              osp_get_next_request(ou, &our), &lwi);
1453
1454                 if (!osp_send_update_thread_running(osp)) {
1455                         if (our != NULL) {
1456                                 osp_trans_callback(&env, our->our_th, -EINTR);
1457                                 osp_thandle_put(&env, our->our_th);
1458                         }
1459                         break;
1460                 }
1461
1462                 LASSERT(our->our_th != NULL);
1463                 if (our->our_th->ot_super.th_result != 0) {
1464                         osp_trans_callback(&env, our->our_th,
1465                                 our->our_th->ot_super.th_result);
1466                         rc = our->our_th->ot_super.th_result;
1467                 } else if (OBD_FAIL_CHECK(OBD_FAIL_INVALIDATE_UPDATE)) {
1468                         rc = -EIO;
1469                         osp_trans_callback(&env, our->our_th, rc);
1470                 } else {
1471                         rc = osp_send_update_req(&env, osp, our);
1472                 }
1473
1474                 /* Update the rpc version */
1475                 spin_lock(&ou->ou_lock);
1476                 if (our->our_version == ou->ou_rpc_version)
1477                         ou->ou_rpc_version++;
1478                 spin_unlock(&ou->ou_lock);
1479
1480                 /* If one update request fails, let's fail all of the requests
1481                  * in the sending list, because the request in the sending
1482                  * list are dependent on either other, continue sending these
1483                  * request might cause llog or filesystem corruption */
1484                 if (rc < 0)
1485                         osp_invalidate_request(osp);
1486
1487                 /* Balanced for thandle_get in osp_check_and_set_rpc_version */
1488                 osp_thandle_put(&env, our->our_th);
1489         }
1490
1491         thread->t_flags = SVC_STOPPED;
1492         lu_env_fini(&env);
1493         wake_up(&thread->t_ctl_waitq);
1494
1495         RETURN(0);
1496 }
1497
1498 /**
1499  * The OSP layer dt_device_operations::dt_trans_start() interface
1500  * to start the transaction.
1501  *
1502  * If the transaction is a remote transaction, then related remote
1503  * updates will be triggered in the osp_trans_stop().
1504  * Please refer to osp_trans_create() for transaction type.
1505  *
1506  * \param[in] env               pointer to the thread context
1507  * \param[in] dt                pointer to the OSP dt_device
1508  * \param[in] th                pointer to the transaction handler
1509  *
1510  * \retval                      0 for success
1511  * \retval                      negative error number on failure
1512  */
1513 int osp_trans_start(const struct lu_env *env, struct dt_device *dt,
1514                     struct thandle *th)
1515 {
1516         struct osp_thandle      *oth = thandle_to_osp_thandle(th);
1517
1518         if (oth->ot_super.th_sync)
1519                 oth->ot_our->our_flags |= UPDATE_FL_SYNC;
1520         /* For remote thandle, if there are local thandle, start it here*/
1521         if (is_only_remote_trans(th) && oth->ot_storage_th != NULL)
1522                 return dt_trans_start(env, oth->ot_storage_th->th_dev,
1523                                       oth->ot_storage_th);
1524         return 0;
1525 }
1526
1527 /**
1528  * The OSP layer dt_device_operations::dt_trans_stop() interface
1529  * to stop the transaction.
1530  *
1531  * If the transaction is a remote transaction, related remote
1532  * updates will be triggered at the end of this function.
1533  *
1534  * For synchronous mode update or any failed update, the request
1535  * will be destroyed explicitly when the osp_trans_stop().
1536  *
1537  * Please refer to osp_trans_create() for transaction type.
1538  *
1539  * \param[in] env               pointer to the thread context
1540  * \param[in] dt                pointer to the OSP dt_device
1541  * \param[in] th                pointer to the transaction handler
1542  *
1543  * \retval                      0 for success
1544  * \retval                      negative error number on failure
1545  */
1546 int osp_trans_stop(const struct lu_env *env, struct dt_device *dt,
1547                    struct thandle *th)
1548 {
1549         struct osp_thandle       *oth = thandle_to_osp_thandle(th);
1550         struct osp_update_request *our = oth->ot_our;
1551         struct osp_device        *osp = dt2osp_dev(dt);
1552         int                      rc = 0;
1553         ENTRY;
1554
1555         /* For remote transaction, if there is local storage thandle,
1556          * stop it first */
1557         if (oth->ot_storage_th != NULL && th->th_top == NULL) {
1558                 dt_trans_stop(env, oth->ot_storage_th->th_dev,
1559                               oth->ot_storage_th);
1560                 oth->ot_storage_th = NULL;
1561         }
1562
1563         if (our == NULL || list_empty(&our->our_req_list)) {
1564                 osp_trans_callback(env, oth, th->th_result);
1565                 GOTO(out, rc = th->th_result);
1566         }
1567
1568         if (!osp->opd_connect_mdt) {
1569                 osp_trans_callback(env, oth, th->th_result);
1570                 rc = osp_send_update_req(env, osp, oth->ot_our);
1571                 GOTO(out, rc);
1572         }
1573
1574         if (osp->opd_update == NULL ||
1575             !osp_send_update_thread_running(osp)) {
1576                 osp_trans_callback(env, oth, -EIO);
1577                 GOTO(out, rc = -EIO);
1578         }
1579
1580         CDEBUG(D_HA, "%s: add oth %p with version %llu\n",
1581                osp->opd_obd->obd_name, oth, our->our_version);
1582
1583         LASSERT(our->our_req_ready == 0);
1584         spin_lock(&our->our_list_lock);
1585         if (likely(!list_empty(&our->our_list))) {
1586                 /* notify sending thread */
1587                 our->our_req_ready = 1;
1588                 wake_up(&osp->opd_update->ou_waitq);
1589                 spin_unlock(&our->our_list_lock);
1590         } else if (th->th_result == 0) {
1591                 /* if the request does not needs to be serialized,
1592                  * read-only request etc, let's send it right away */
1593                 spin_unlock(&our->our_list_lock);
1594                 rc = osp_send_update_req(env, osp, our);
1595         } else {
1596                 spin_unlock(&our->our_list_lock);
1597                 osp_trans_callback(env, oth, th->th_result);
1598         }
1599 out:
1600         osp_thandle_put(env, oth);
1601
1602         RETURN(rc);
1603 }