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