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