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