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