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