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