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