Whamcloud - gitweb
LU-3105 osd: remove capa related stuff from servers
[fs/lustre-release.git] / lustre / target / tgt_lastrcvd.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.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, 2014, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * Lustre Unified Target
37  * These are common function to work with last_received file
38  *
39  * Author: Mikhail Pershin <mike.pershin@intel.com>
40  */
41 #include <obd.h>
42 #include <obd_class.h>
43 #include <lustre_fid.h>
44
45 #include "tgt_internal.h"
46
47 static inline struct lu_buf *tti_buf_lsd(struct tgt_thread_info *tti)
48 {
49         tti->tti_buf.lb_buf = &tti->tti_lsd;
50         tti->tti_buf.lb_len = sizeof(tti->tti_lsd);
51         return &tti->tti_buf;
52 }
53
54 static inline struct lu_buf *tti_buf_lcd(struct tgt_thread_info *tti)
55 {
56         tti->tti_buf.lb_buf = &tti->tti_lcd;
57         tti->tti_buf.lb_len = sizeof(tti->tti_lcd);
58         return &tti->tti_buf;
59 }
60
61 /**
62  * Allocate in-memory data for client slot related to export.
63  */
64 int tgt_client_alloc(struct obd_export *exp)
65 {
66         ENTRY;
67         LASSERT(exp != exp->exp_obd->obd_self_export);
68
69         OBD_ALLOC_PTR(exp->exp_target_data.ted_lcd);
70         if (exp->exp_target_data.ted_lcd == NULL)
71                 RETURN(-ENOMEM);
72         /* Mark that slot is not yet valid, 0 doesn't work here */
73         exp->exp_target_data.ted_lr_idx = -1;
74         RETURN(0);
75 }
76 EXPORT_SYMBOL(tgt_client_alloc);
77
78 /**
79  * Free in-memory data for client slot related to export.
80  */
81 void tgt_client_free(struct obd_export *exp)
82 {
83         struct tg_export_data   *ted = &exp->exp_target_data;
84         struct lu_target        *lut = class_exp2tgt(exp);
85
86         LASSERT(exp != exp->exp_obd->obd_self_export);
87
88         OBD_FREE_PTR(ted->ted_lcd);
89         ted->ted_lcd = NULL;
90
91         /* Slot may be not yet assigned */
92         if (ted->ted_lr_idx < 0)
93                 return;
94         /* Clear bit when lcd is freed */
95         LASSERT(lut->lut_client_bitmap);
96         if (!test_and_clear_bit(ted->ted_lr_idx, lut->lut_client_bitmap)) {
97                 CERROR("%s: client %u bit already clear in bitmap\n",
98                        exp->exp_obd->obd_name, ted->ted_lr_idx);
99                 LBUG();
100         }
101 }
102 EXPORT_SYMBOL(tgt_client_free);
103
104 int tgt_client_data_read(const struct lu_env *env, struct lu_target *tgt,
105                          struct lsd_client_data *lcd, loff_t *off, int index)
106 {
107         struct tgt_thread_info  *tti = tgt_th_info(env);
108         int                      rc;
109
110         tti_buf_lcd(tti);
111         rc = dt_record_read(env, tgt->lut_last_rcvd, &tti->tti_buf, off);
112         if (rc == 0) {
113                 check_lcd(tgt->lut_obd->obd_name, index, &tti->tti_lcd);
114                 lcd_le_to_cpu(&tti->tti_lcd, lcd);
115                 lcd->lcd_last_result = ptlrpc_status_ntoh(lcd->lcd_last_result);
116                 lcd->lcd_last_close_result =
117                         ptlrpc_status_ntoh(lcd->lcd_last_close_result);
118         }
119
120         CDEBUG(D_INFO, "%s: read lcd @%lld uuid = %s, last_transno = "LPU64
121                ", last_xid = "LPU64", last_result = %u, last_data = %u, "
122                "last_close_transno = "LPU64", last_close_xid = "LPU64", "
123                "last_close_result = %u, rc = %d\n", tgt->lut_obd->obd_name,
124                *off, lcd->lcd_uuid, lcd->lcd_last_transno, lcd->lcd_last_xid,
125                lcd->lcd_last_result, lcd->lcd_last_data,
126                lcd->lcd_last_close_transno, lcd->lcd_last_close_xid,
127                lcd->lcd_last_close_result, rc);
128         return rc;
129 }
130
131 int tgt_client_data_write(const struct lu_env *env, struct lu_target *tgt,
132                           struct lsd_client_data *lcd, loff_t *off,
133                           struct thandle *th)
134 {
135         struct tgt_thread_info *tti = tgt_th_info(env);
136
137         lcd->lcd_last_result = ptlrpc_status_hton(lcd->lcd_last_result);
138         lcd->lcd_last_close_result =
139                 ptlrpc_status_hton(lcd->lcd_last_close_result);
140         lcd_cpu_to_le(lcd, &tti->tti_lcd);
141         tti_buf_lcd(tti);
142
143         return dt_record_write(env, tgt->lut_last_rcvd, &tti->tti_buf, off, th);
144 }
145
146 /**
147  * Update client data in last_rcvd
148  */
149 static int tgt_client_data_update(const struct lu_env *env,
150                                   struct obd_export *exp)
151 {
152         struct tg_export_data   *ted = &exp->exp_target_data;
153         struct lu_target        *tgt = class_exp2tgt(exp);
154         struct tgt_thread_info  *tti = tgt_th_info(env);
155         struct thandle          *th;
156         int                      rc = 0;
157
158         ENTRY;
159
160         th = dt_trans_create(env, tgt->lut_bottom);
161         if (IS_ERR(th))
162                 RETURN(PTR_ERR(th));
163
164         tti_buf_lcd(tti);
165         rc = dt_declare_record_write(env, tgt->lut_last_rcvd,
166                                      &tti->tti_buf,
167                                      ted->ted_lr_off, th);
168         if (rc)
169                 GOTO(out, rc);
170
171         rc = dt_trans_start_local(env, tgt->lut_bottom, th);
172         if (rc)
173                 GOTO(out, rc);
174         /*
175          * Until this operations will be committed the sync is needed
176          * for this export. This should be done _after_ starting the
177          * transaction so that many connecting clients will not bring
178          * server down with lots of sync writes.
179          */
180         rc = tgt_new_client_cb_add(th, exp);
181         if (rc) {
182                 /* can't add callback, do sync now */
183                 th->th_sync = 1;
184         } else {
185                 spin_lock(&exp->exp_lock);
186                 exp->exp_need_sync = 1;
187                 spin_unlock(&exp->exp_lock);
188         }
189
190         tti->tti_off = ted->ted_lr_off;
191         rc = tgt_client_data_write(env, tgt, ted->ted_lcd, &tti->tti_off, th);
192         EXIT;
193 out:
194         dt_trans_stop(env, tgt->lut_bottom, th);
195         CDEBUG(D_INFO, "%s: update last_rcvd client data for UUID = %s, "
196                "last_transno = "LPU64": rc = %d\n", tgt->lut_obd->obd_name,
197                tgt->lut_lsd.lsd_uuid, tgt->lut_lsd.lsd_last_transno, rc);
198
199         return rc;
200 }
201
202 int tgt_server_data_read(const struct lu_env *env, struct lu_target *tgt)
203 {
204         struct tgt_thread_info  *tti = tgt_th_info(env);
205         int                      rc;
206
207         tti->tti_off = 0;
208         tti_buf_lsd(tti);
209         rc = dt_record_read(env, tgt->lut_last_rcvd, &tti->tti_buf,
210                             &tti->tti_off);
211         if (rc == 0)
212                 lsd_le_to_cpu(&tti->tti_lsd, &tgt->lut_lsd);
213
214         CDEBUG(D_INFO, "%s: read last_rcvd server data for UUID = %s, "
215                "last_transno = "LPU64": rc = %d\n", tgt->lut_obd->obd_name,
216                tgt->lut_lsd.lsd_uuid, tgt->lut_lsd.lsd_last_transno, rc);
217         return rc;
218 }
219
220 int tgt_server_data_write(const struct lu_env *env, struct lu_target *tgt,
221                           struct thandle *th)
222 {
223         struct tgt_thread_info  *tti = tgt_th_info(env);
224         int                      rc;
225
226         ENTRY;
227
228         tti->tti_off = 0;
229         tti_buf_lsd(tti);
230         lsd_cpu_to_le(&tgt->lut_lsd, &tti->tti_lsd);
231
232         rc = dt_record_write(env, tgt->lut_last_rcvd, &tti->tti_buf,
233                              &tti->tti_off, th);
234
235         CDEBUG(D_INFO, "%s: write last_rcvd server data for UUID = %s, "
236                "last_transno = "LPU64": rc = %d\n", tgt->lut_obd->obd_name,
237                tgt->lut_lsd.lsd_uuid, tgt->lut_lsd.lsd_last_transno, rc);
238
239         RETURN(rc);
240 }
241
242 /**
243  * Update server data in last_rcvd
244  */
245 int tgt_server_data_update(const struct lu_env *env, struct lu_target *tgt,
246                            int sync)
247 {
248         struct tgt_thread_info  *tti = tgt_th_info(env);
249         struct thandle          *th;
250         int                      rc = 0;
251
252         ENTRY;
253
254         CDEBUG(D_SUPER,
255                "%s: mount_count is "LPU64", last_transno is "LPU64"\n",
256                tgt->lut_lsd.lsd_uuid, tgt->lut_obd->u.obt.obt_mount_count,
257                tgt->lut_last_transno);
258
259         /* Always save latest transno to keep it fresh */
260         spin_lock(&tgt->lut_translock);
261         tgt->lut_lsd.lsd_last_transno = tgt->lut_last_transno;
262         spin_unlock(&tgt->lut_translock);
263
264         th = dt_trans_create(env, tgt->lut_bottom);
265         if (IS_ERR(th))
266                 RETURN(PTR_ERR(th));
267
268         th->th_sync = sync;
269
270         tti_buf_lsd(tti);
271         rc = dt_declare_record_write(env, tgt->lut_last_rcvd,
272                                      &tti->tti_buf, tti->tti_off, th);
273         if (rc)
274                 GOTO(out, rc);
275
276         rc = dt_trans_start(env, tgt->lut_bottom, th);
277         if (rc)
278                 GOTO(out, rc);
279
280         rc = tgt_server_data_write(env, tgt, th);
281 out:
282         dt_trans_stop(env, tgt->lut_bottom, th);
283
284         CDEBUG(D_INFO, "%s: update last_rcvd server data for UUID = %s, "
285                "last_transno = "LPU64": rc = %d\n", tgt->lut_obd->obd_name,
286                tgt->lut_lsd.lsd_uuid, tgt->lut_lsd.lsd_last_transno, rc);
287         RETURN(rc);
288 }
289 EXPORT_SYMBOL(tgt_server_data_update);
290
291 int tgt_truncate_last_rcvd(const struct lu_env *env, struct lu_target *tgt,
292                            loff_t size)
293 {
294         struct dt_object *dt = tgt->lut_last_rcvd;
295         struct thandle   *th;
296         struct lu_attr    attr;
297         int               rc;
298
299         ENTRY;
300
301         attr.la_size = size;
302         attr.la_valid = LA_SIZE;
303
304         th = dt_trans_create(env, tgt->lut_bottom);
305         if (IS_ERR(th))
306                 RETURN(PTR_ERR(th));
307         rc = dt_declare_punch(env, dt, size, OBD_OBJECT_EOF, th);
308         if (rc)
309                 GOTO(cleanup, rc);
310         rc = dt_declare_attr_set(env, dt, &attr, th);
311         if (rc)
312                 GOTO(cleanup, rc);
313         rc = dt_trans_start_local(env, tgt->lut_bottom, th);
314         if (rc)
315                 GOTO(cleanup, rc);
316
317         rc = dt_punch(env, dt, size, OBD_OBJECT_EOF, th);
318         if (rc == 0)
319                 rc = dt_attr_set(env, dt, &attr, th);
320
321 cleanup:
322         dt_trans_stop(env, tgt->lut_bottom, th);
323
324         RETURN(rc);
325 }
326
327 static void tgt_client_epoch_update(const struct lu_env *env,
328                                     struct obd_export *exp)
329 {
330         struct lsd_client_data  *lcd = exp->exp_target_data.ted_lcd;
331         struct lu_target        *tgt = class_exp2tgt(exp);
332
333         LASSERT(tgt->lut_bottom);
334         /** VBR: set client last_epoch to current epoch */
335         if (lcd->lcd_last_epoch >= tgt->lut_lsd.lsd_start_epoch)
336                 return;
337         lcd->lcd_last_epoch = tgt->lut_lsd.lsd_start_epoch;
338         tgt_client_data_update(env, exp);
339 }
340
341 /**
342  * Update boot epoch when recovery ends
343  */
344 void tgt_boot_epoch_update(struct lu_target *tgt)
345 {
346         struct lu_env            env;
347         struct ptlrpc_request   *req;
348         __u32                    start_epoch;
349         struct list_head         client_list;
350         int                      rc;
351
352         if (tgt->lut_obd->obd_stopping)
353                 return;
354
355         rc = lu_env_init(&env, LCT_DT_THREAD);
356         if (rc) {
357                 CERROR("%s: can't initialize environment: rc = %d\n",
358                         tgt->lut_obd->obd_name, rc);
359                 return;
360         }
361
362         spin_lock(&tgt->lut_translock);
363         start_epoch = lr_epoch(tgt->lut_last_transno) + 1;
364         tgt->lut_last_transno = (__u64)start_epoch << LR_EPOCH_BITS;
365         tgt->lut_lsd.lsd_start_epoch = start_epoch;
366         spin_unlock(&tgt->lut_translock);
367
368         INIT_LIST_HEAD(&client_list);
369         /**
370          * The recovery is not yet finished and final queue can still be updated
371          * with resend requests. Move final list to separate one for processing
372          */
373         spin_lock(&tgt->lut_obd->obd_recovery_task_lock);
374         list_splice_init(&tgt->lut_obd->obd_final_req_queue, &client_list);
375         spin_unlock(&tgt->lut_obd->obd_recovery_task_lock);
376
377         /**
378          * go through list of exports participated in recovery and
379          * set new epoch for them
380          */
381         list_for_each_entry(req, &client_list, rq_list) {
382                 LASSERT(!req->rq_export->exp_delayed);
383                 if (!req->rq_export->exp_vbr_failed)
384                         tgt_client_epoch_update(&env, req->rq_export);
385         }
386         /** return list back at once */
387         spin_lock(&tgt->lut_obd->obd_recovery_task_lock);
388         list_splice_init(&client_list, &tgt->lut_obd->obd_final_req_queue);
389         spin_unlock(&tgt->lut_obd->obd_recovery_task_lock);
390         /** update server epoch */
391         tgt_server_data_update(&env, tgt, 1);
392         lu_env_fini(&env);
393 }
394
395 /**
396  * commit callback, need to update last_commited value
397  */
398 struct tgt_last_committed_callback {
399         struct dt_txn_commit_cb  llcc_cb;
400         struct lu_target        *llcc_tgt;
401         struct obd_export       *llcc_exp;
402         __u64                    llcc_transno;
403 };
404
405 static void tgt_cb_last_committed(struct lu_env *env, struct thandle *th,
406                                   struct dt_txn_commit_cb *cb, int err)
407 {
408         struct tgt_last_committed_callback *ccb;
409
410         ccb = container_of0(cb, struct tgt_last_committed_callback, llcc_cb);
411
412         LASSERT(ccb->llcc_tgt != NULL);
413         LASSERT(ccb->llcc_exp->exp_obd == ccb->llcc_tgt->lut_obd);
414
415         spin_lock(&ccb->llcc_tgt->lut_translock);
416         if (ccb->llcc_transno > ccb->llcc_tgt->lut_obd->obd_last_committed)
417                 ccb->llcc_tgt->lut_obd->obd_last_committed = ccb->llcc_transno;
418
419         LASSERT(ccb->llcc_exp);
420         if (ccb->llcc_transno > ccb->llcc_exp->exp_last_committed) {
421                 ccb->llcc_exp->exp_last_committed = ccb->llcc_transno;
422                 spin_unlock(&ccb->llcc_tgt->lut_translock);
423                 ptlrpc_commit_replies(ccb->llcc_exp);
424         } else {
425                 spin_unlock(&ccb->llcc_tgt->lut_translock);
426         }
427         class_export_cb_put(ccb->llcc_exp);
428         if (ccb->llcc_transno)
429                 CDEBUG(D_HA, "%s: transno "LPD64" is committed\n",
430                        ccb->llcc_tgt->lut_obd->obd_name, ccb->llcc_transno);
431         OBD_FREE_PTR(ccb);
432 }
433
434 int tgt_last_commit_cb_add(struct thandle *th, struct lu_target *tgt,
435                            struct obd_export *exp, __u64 transno)
436 {
437         struct tgt_last_committed_callback      *ccb;
438         struct dt_txn_commit_cb                 *dcb;
439         int                                      rc;
440
441         OBD_ALLOC_PTR(ccb);
442         if (ccb == NULL)
443                 return -ENOMEM;
444
445         ccb->llcc_tgt = tgt;
446         ccb->llcc_exp = class_export_cb_get(exp);
447         ccb->llcc_transno = transno;
448
449         dcb = &ccb->llcc_cb;
450         dcb->dcb_func = tgt_cb_last_committed;
451         INIT_LIST_HEAD(&dcb->dcb_linkage);
452         strlcpy(dcb->dcb_name, "tgt_cb_last_committed", sizeof(dcb->dcb_name));
453
454         rc = dt_trans_cb_add(th, dcb);
455         if (rc) {
456                 class_export_cb_put(exp);
457                 OBD_FREE_PTR(ccb);
458         }
459
460         if (exp_connect_flags(exp) & OBD_CONNECT_LIGHTWEIGHT)
461                 /* report failure to force synchronous operation */
462                 return -EPERM;
463
464         return rc;
465 }
466
467 struct tgt_new_client_callback {
468         struct dt_txn_commit_cb  lncc_cb;
469         struct obd_export       *lncc_exp;
470 };
471
472 static void tgt_cb_new_client(struct lu_env *env, struct thandle *th,
473                               struct dt_txn_commit_cb *cb, int err)
474 {
475         struct tgt_new_client_callback *ccb;
476
477         ccb = container_of0(cb, struct tgt_new_client_callback, lncc_cb);
478
479         LASSERT(ccb->lncc_exp->exp_obd);
480
481         CDEBUG(D_RPCTRACE, "%s: committing for initial connect of %s\n",
482                ccb->lncc_exp->exp_obd->obd_name,
483                ccb->lncc_exp->exp_client_uuid.uuid);
484
485         spin_lock(&ccb->lncc_exp->exp_lock);
486         /* XXX: Currently, we use per-export based sync/async policy for
487          *      the update via OUT RPC, it is coarse-grained policy, and
488          *      will be changed as per-request based by DNE II patches. */
489         if (!ccb->lncc_exp->exp_keep_sync)
490                 ccb->lncc_exp->exp_need_sync = 0;
491
492         spin_unlock(&ccb->lncc_exp->exp_lock);
493         class_export_cb_put(ccb->lncc_exp);
494
495         OBD_FREE_PTR(ccb);
496 }
497
498 int tgt_new_client_cb_add(struct thandle *th, struct obd_export *exp)
499 {
500         struct tgt_new_client_callback  *ccb;
501         struct dt_txn_commit_cb         *dcb;
502         int                              rc;
503
504         OBD_ALLOC_PTR(ccb);
505         if (ccb == NULL)
506                 return -ENOMEM;
507
508         ccb->lncc_exp = class_export_cb_get(exp);
509
510         dcb = &ccb->lncc_cb;
511         dcb->dcb_func = tgt_cb_new_client;
512         INIT_LIST_HEAD(&dcb->dcb_linkage);
513         strlcpy(dcb->dcb_name, "tgt_cb_new_client", sizeof(dcb->dcb_name));
514
515         rc = dt_trans_cb_add(th, dcb);
516         if (rc) {
517                 class_export_cb_put(exp);
518                 OBD_FREE_PTR(ccb);
519         }
520         return rc;
521 }
522
523 /**
524  * Add new client to the last_rcvd upon new connection.
525  *
526  * We use a bitmap to locate a free space in the last_rcvd file and initialize
527  * tg_export_data.
528  */
529 int tgt_client_new(const struct lu_env *env, struct obd_export *exp)
530 {
531         struct tg_export_data   *ted = &exp->exp_target_data;
532         struct lu_target        *tgt = class_exp2tgt(exp);
533         int                      rc = 0, idx;
534
535         ENTRY;
536
537         LASSERT(tgt->lut_client_bitmap != NULL);
538         if (!strcmp(ted->ted_lcd->lcd_uuid, tgt->lut_obd->obd_uuid.uuid))
539                 RETURN(0);
540
541         mutex_init(&ted->ted_lcd_lock);
542
543         if (exp_connect_flags(exp) & OBD_CONNECT_LIGHTWEIGHT)
544                 RETURN(0);
545
546         /* the bitmap operations can handle cl_idx > sizeof(long) * 8, so
547          * there's no need for extra complication here
548          */
549         idx = find_first_zero_bit(tgt->lut_client_bitmap, LR_MAX_CLIENTS);
550 repeat:
551         if (idx >= LR_MAX_CLIENTS ||
552             OBD_FAIL_CHECK(OBD_FAIL_MDS_CLIENT_ADD)) {
553                 CERROR("%s: no room for %u clients - fix LR_MAX_CLIENTS\n",
554                        tgt->lut_obd->obd_name,  idx);
555                 RETURN(-EOVERFLOW);
556         }
557         if (test_and_set_bit(idx, tgt->lut_client_bitmap)) {
558                 idx = find_next_zero_bit(tgt->lut_client_bitmap,
559                                              LR_MAX_CLIENTS, idx);
560                 goto repeat;
561         }
562
563         CDEBUG(D_INFO, "%s: client at idx %d with UUID '%s' added\n",
564                tgt->lut_obd->obd_name, idx, ted->ted_lcd->lcd_uuid);
565
566         ted->ted_lr_idx = idx;
567         ted->ted_lr_off = tgt->lut_lsd.lsd_client_start +
568                           idx * tgt->lut_lsd.lsd_client_size;
569
570         LASSERTF(ted->ted_lr_off > 0, "ted_lr_off = %llu\n", ted->ted_lr_off);
571
572         CDEBUG(D_INFO, "%s: new client at index %d (%llu) with UUID '%s'\n",
573                tgt->lut_obd->obd_name, ted->ted_lr_idx, ted->ted_lr_off,
574                ted->ted_lcd->lcd_uuid);
575
576         if (OBD_FAIL_CHECK(OBD_FAIL_TGT_CLIENT_ADD))
577                 RETURN(-ENOSPC);
578
579         rc = tgt_client_data_update(env, exp);
580         if (rc)
581                 CERROR("%s: Failed to write client lcd at idx %d, rc %d\n",
582                        tgt->lut_obd->obd_name, idx, rc);
583
584         RETURN(rc);
585 }
586 EXPORT_SYMBOL(tgt_client_new);
587
588 /* Add client data to the MDS.  We use a bitmap to locate a free space
589  * in the last_rcvd file if cl_off is -1 (i.e. a new client).
590  * Otherwise, we just have to read the data from the last_rcvd file and
591  * we know its offset.
592  *
593  * It should not be possible to fail adding an existing client - otherwise
594  * mdt_init_server_data() callsite needs to be fixed.
595  */
596 int tgt_client_add(const struct lu_env *env,  struct obd_export *exp, int idx)
597 {
598         struct tg_export_data   *ted = &exp->exp_target_data;
599         struct lu_target        *tgt = class_exp2tgt(exp);
600
601         ENTRY;
602
603         LASSERT(tgt->lut_client_bitmap != NULL);
604         LASSERTF(idx >= 0, "%d\n", idx);
605
606         if (!strcmp(ted->ted_lcd->lcd_uuid, tgt->lut_obd->obd_uuid.uuid) ||
607             exp_connect_flags(exp) & OBD_CONNECT_LIGHTWEIGHT)
608                 RETURN(0);
609
610         if (test_and_set_bit(idx, tgt->lut_client_bitmap)) {
611                 CERROR("%s: client %d: bit already set in bitmap!!\n",
612                        tgt->lut_obd->obd_name,  idx);
613                 LBUG();
614         }
615
616         CDEBUG(D_INFO, "%s: client at idx %d with UUID '%s' added\n",
617                tgt->lut_obd->obd_name, idx, ted->ted_lcd->lcd_uuid);
618
619         ted->ted_lr_idx = idx;
620         ted->ted_lr_off = tgt->lut_lsd.lsd_client_start +
621                           idx * tgt->lut_lsd.lsd_client_size;
622
623         mutex_init(&ted->ted_lcd_lock);
624
625         LASSERTF(ted->ted_lr_off > 0, "ted_lr_off = %llu\n", ted->ted_lr_off);
626
627         RETURN(0);
628 }
629
630 int tgt_client_del(const struct lu_env *env, struct obd_export *exp)
631 {
632         struct tg_export_data   *ted = &exp->exp_target_data;
633         struct lu_target        *tgt = class_exp2tgt(exp);
634         int                      rc;
635
636         ENTRY;
637
638         LASSERT(ted->ted_lcd);
639
640         /* XXX if lcd_uuid were a real obd_uuid, I could use obd_uuid_equals */
641         if (!strcmp((char *)ted->ted_lcd->lcd_uuid,
642                     (char *)tgt->lut_obd->obd_uuid.uuid) ||
643             exp_connect_flags(exp) & OBD_CONNECT_LIGHTWEIGHT)
644                 RETURN(0);
645
646         CDEBUG(D_INFO, "%s: del client at idx %u, off %lld, UUID '%s'\n",
647                tgt->lut_obd->obd_name, ted->ted_lr_idx, ted->ted_lr_off,
648                ted->ted_lcd->lcd_uuid);
649
650         /* Clear the bit _after_ zeroing out the client so we don't
651            race with filter_client_add and zero out new clients.*/
652         if (!test_bit(ted->ted_lr_idx, tgt->lut_client_bitmap)) {
653                 CERROR("%s: client %u: bit already clear in bitmap!!\n",
654                        tgt->lut_obd->obd_name, ted->ted_lr_idx);
655                 LBUG();
656         }
657
658         /* Do not erase record for recoverable client. */
659         if (exp->exp_flags & OBD_OPT_FAILOVER)
660                 RETURN(0);
661
662         /* Make sure the server's last_transno is up to date.
663          * This should be done before zeroing client slot so last_transno will
664          * be in server data or in client data in case of failure */
665         rc = tgt_server_data_update(env, tgt, 0);
666         if (rc != 0) {
667                 CERROR("%s: failed to update server data, skip client %s "
668                        "zeroing, rc %d\n", tgt->lut_obd->obd_name,
669                        ted->ted_lcd->lcd_uuid, rc);
670                 RETURN(rc);
671         }
672
673         mutex_lock(&ted->ted_lcd_lock);
674         memset(ted->ted_lcd->lcd_uuid, 0, sizeof ted->ted_lcd->lcd_uuid);
675         rc = tgt_client_data_update(env, exp);
676         mutex_unlock(&ted->ted_lcd_lock);
677
678         CDEBUG(rc == 0 ? D_INFO : D_ERROR,
679                "%s: zeroing out client %s at idx %u (%llu), rc %d\n",
680                tgt->lut_obd->obd_name, ted->ted_lcd->lcd_uuid,
681                ted->ted_lr_idx, ted->ted_lr_off, rc);
682         RETURN(rc);
683 }
684 EXPORT_SYMBOL(tgt_client_del);
685
686 /*
687  * last_rcvd & last_committed update callbacks
688  */
689 static int tgt_last_rcvd_update(const struct lu_env *env, struct lu_target *tgt,
690                                 struct dt_object *obj, __u64 opdata,
691                                 struct thandle *th, struct ptlrpc_request *req)
692 {
693         struct tgt_thread_info  *tti = tgt_th_info(env);
694         struct tg_export_data   *ted;
695         __u64                   *transno_p;
696         int                      rc = 0;
697         bool                     lw_client, update = false;
698
699         ENTRY;
700
701         ted = &req->rq_export->exp_target_data;
702
703         lw_client = exp_connect_flags(req->rq_export) & OBD_CONNECT_LIGHTWEIGHT;
704         if (ted->ted_lr_idx < 0 && !lw_client)
705                 /* ofd connect may cause transaction before export has
706                  * last_rcvd slot */
707                 RETURN(0);
708
709         tti->tti_transno = lustre_msg_get_transno(req->rq_reqmsg);
710
711         spin_lock(&tgt->lut_translock);
712         if (th->th_result != 0) {
713                 if (tti->tti_transno != 0) {
714                         CERROR("%s: replay transno "LPU64" failed: rc = %d\n",
715                                tgt_name(tgt), tti->tti_transno, th->th_result);
716                 }
717         } else if (tti->tti_transno == 0) {
718                 tti->tti_transno = ++tgt->lut_last_transno;
719         } else {
720                 /* should be replay */
721                 if (tti->tti_transno > tgt->lut_last_transno)
722                         tgt->lut_last_transno = tti->tti_transno;
723         }
724         spin_unlock(&tgt->lut_translock);
725
726         /** VBR: set new versions */
727         if (th->th_result == 0 && obj != NULL)
728                 dt_version_set(env, obj, tti->tti_transno, th);
729
730         /* filling reply data */
731         CDEBUG(D_INODE, "transno = "LPU64", last_committed = "LPU64"\n",
732                tti->tti_transno, tgt->lut_obd->obd_last_committed);
733
734         req->rq_transno = tti->tti_transno;
735         lustre_msg_set_transno(req->rq_repmsg, tti->tti_transno);
736
737         /* if can't add callback, do sync write */
738         th->th_sync |= !!tgt_last_commit_cb_add(th, tgt, req->rq_export,
739                                                 tti->tti_transno);
740
741         if (lw_client) {
742                 /* All operations performed by LW clients are synchronous and
743                  * we store the committed transno in the last_rcvd header */
744                 spin_lock(&tgt->lut_translock);
745                 if (tti->tti_transno > tgt->lut_lsd.lsd_last_transno) {
746                         tgt->lut_lsd.lsd_last_transno = tti->tti_transno;
747                         update = true;
748                 }
749                 spin_unlock(&tgt->lut_translock);
750                 /* Although lightweight (LW) connections have no slot in
751                  * last_rcvd, we still want to maintain the in-memory
752                  * lsd_client_data structure in order to properly handle reply
753                  * reconstruction. */
754         } else if (ted->ted_lr_off == 0) {
755                 CERROR("%s: client idx %d has offset %lld\n",
756                        tgt_name(tgt), ted->ted_lr_idx, ted->ted_lr_off);
757                 RETURN(-EINVAL);
758         }
759
760         /* if the export has already been disconnected, we have no last_rcvd
761          * slot, update server data with latest transno then */
762         if (ted->ted_lcd == NULL) {
763                 CWARN("commit transaction for disconnected client %s: rc %d\n",
764                       req->rq_export->exp_client_uuid.uuid, rc);
765                 GOTO(srv_update, rc = 0);
766         }
767
768         mutex_lock(&ted->ted_lcd_lock);
769         LASSERT(ergo(tti->tti_transno == 0, th->th_result != 0));
770         if (lustre_msg_get_opc(req->rq_reqmsg) == MDS_CLOSE ||
771             lustre_msg_get_opc(req->rq_reqmsg) == MDS_DONE_WRITING) {
772                 transno_p = &ted->ted_lcd->lcd_last_close_transno;
773                 ted->ted_lcd->lcd_last_close_xid = req->rq_xid;
774                 ted->ted_lcd->lcd_last_close_result = th->th_result;
775         } else {
776                 /* VBR: save versions in last_rcvd for reconstruct. */
777                 __u64 *pre_versions = lustre_msg_get_versions(req->rq_repmsg);
778
779                 if (pre_versions) {
780                         ted->ted_lcd->lcd_pre_versions[0] = pre_versions[0];
781                         ted->ted_lcd->lcd_pre_versions[1] = pre_versions[1];
782                         ted->ted_lcd->lcd_pre_versions[2] = pre_versions[2];
783                         ted->ted_lcd->lcd_pre_versions[3] = pre_versions[3];
784                 }
785                 transno_p = &ted->ted_lcd->lcd_last_transno;
786                 ted->ted_lcd->lcd_last_xid = req->rq_xid;
787                 ted->ted_lcd->lcd_last_result = th->th_result;
788                 /* XXX: lcd_last_data is __u32 but intent_dispostion is __u64,
789                  * see struct ldlm_reply->lock_policy_res1; */
790                 ted->ted_lcd->lcd_last_data = opdata;
791         }
792
793         /* Update transno in slot only if non-zero number, i.e. no errors */
794         if (likely(tti->tti_transno != 0)) {
795                 if (*transno_p > tti->tti_transno &&
796                     !tgt->lut_no_reconstruct) {
797                         CERROR("%s: trying to overwrite bigger transno:"
798                                "on-disk: "LPU64", new: "LPU64" replay: %d. "
799                                "see LU-617.\n", tgt_name(tgt), *transno_p,
800                                tti->tti_transno, req_is_replay(req));
801                         if (req_is_replay(req)) {
802                                 spin_lock(&req->rq_export->exp_lock);
803                                 req->rq_export->exp_vbr_failed = 1;
804                                 spin_unlock(&req->rq_export->exp_lock);
805                         }
806                         mutex_unlock(&ted->ted_lcd_lock);
807                         RETURN(req_is_replay(req) ? -EOVERFLOW : 0);
808                 }
809                 *transno_p = tti->tti_transno;
810         }
811
812         if (!lw_client) {
813                 tti->tti_off = ted->ted_lr_off;
814                 rc = tgt_client_data_write(env, tgt, ted->ted_lcd, &tti->tti_off, th);
815                 if (rc < 0) {
816                         mutex_unlock(&ted->ted_lcd_lock);
817                         RETURN(rc);
818                 }
819         }
820         mutex_unlock(&ted->ted_lcd_lock);
821         EXIT;
822 srv_update:
823         if (update)
824                 rc = tgt_server_data_write(env, tgt, th);
825         return rc;
826 }
827
828 /*
829  * last_rcvd update for echo client simulation.
830  * It updates last_rcvd client slot and version of object in
831  * simple way but with all locks to simulate all drawbacks
832  */
833 static int tgt_last_rcvd_update_echo(const struct lu_env *env,
834                                      struct lu_target *tgt,
835                                      struct dt_object *obj,
836                                      struct thandle *th,
837                                      struct obd_export *exp)
838 {
839         struct tgt_thread_info  *tti = tgt_th_info(env);
840         struct tg_export_data   *ted = &exp->exp_target_data;
841         int                      rc = 0;
842
843         ENTRY;
844
845         tti->tti_transno = 0;
846
847         spin_lock(&tgt->lut_translock);
848         if (th->th_result == 0)
849                 tti->tti_transno = ++tgt->lut_last_transno;
850         spin_unlock(&tgt->lut_translock);
851
852         /** VBR: set new versions */
853         if (th->th_result == 0 && obj != NULL)
854                 dt_version_set(env, obj, tti->tti_transno, th);
855
856         /* if can't add callback, do sync write */
857         th->th_sync |= !!tgt_last_commit_cb_add(th, tgt, exp,
858                                                 tti->tti_transno);
859
860         LASSERT(ted->ted_lr_off > 0);
861
862         mutex_lock(&ted->ted_lcd_lock);
863         LASSERT(ergo(tti->tti_transno == 0, th->th_result != 0));
864         ted->ted_lcd->lcd_last_transno = tti->tti_transno;
865         ted->ted_lcd->lcd_last_result = th->th_result;
866
867         tti->tti_off = ted->ted_lr_off;
868         rc = tgt_client_data_write(env, tgt, ted->ted_lcd, &tti->tti_off, th);
869         mutex_unlock(&ted->ted_lcd_lock);
870         RETURN(rc);
871 }
872
873 static int tgt_clients_data_init(const struct lu_env *env,
874                                  struct lu_target *tgt,
875                                  unsigned long last_size)
876 {
877         struct obd_device       *obd = tgt->lut_obd;
878         struct lr_server_data   *lsd = &tgt->lut_lsd;
879         struct lsd_client_data  *lcd = NULL;
880         struct tg_export_data   *ted;
881         int                      cl_idx;
882         int                      rc = 0;
883         loff_t                   off = lsd->lsd_client_start;
884
885         ENTRY;
886
887         CLASSERT(offsetof(struct lsd_client_data, lcd_padding) +
888                  sizeof(lcd->lcd_padding) == LR_CLIENT_SIZE);
889
890         OBD_ALLOC_PTR(lcd);
891         if (lcd == NULL)
892                 RETURN(-ENOMEM);
893
894         for (cl_idx = 0; off < last_size; cl_idx++) {
895                 struct obd_export       *exp;
896                 __u64                    last_transno;
897
898                 /* Don't assume off is incremented properly by
899                  * read_record(), in case sizeof(*lcd)
900                  * isn't the same as fsd->lsd_client_size.  */
901                 off = lsd->lsd_client_start + cl_idx * lsd->lsd_client_size;
902                 rc = tgt_client_data_read(env, tgt, lcd, &off, cl_idx);
903                 if (rc) {
904                         CERROR("%s: error reading last_rcvd %s idx %d off "
905                                "%llu: rc = %d\n", tgt_name(tgt), LAST_RCVD,
906                                cl_idx, off, rc);
907                         rc = 0;
908                         break; /* read error shouldn't cause startup to fail */
909                 }
910
911                 if (lcd->lcd_uuid[0] == '\0') {
912                         CDEBUG(D_INFO, "skipping zeroed client at offset %d\n",
913                                cl_idx);
914                         continue;
915                 }
916
917                 last_transno = lcd_last_transno(lcd);
918
919                 /* These exports are cleaned up by disconnect, so they
920                  * need to be set up like real exports as connect does.
921                  */
922                 CDEBUG(D_HA, "RCVRNG CLIENT uuid: %s idx: %d lr: "LPU64
923                        " srv lr: "LPU64" lx: "LPU64"\n", lcd->lcd_uuid, cl_idx,
924                        last_transno, lsd->lsd_last_transno, lcd_last_xid(lcd));
925
926                 exp = class_new_export(obd, (struct obd_uuid *)lcd->lcd_uuid);
927                 if (IS_ERR(exp)) {
928                         if (PTR_ERR(exp) == -EALREADY) {
929                                 /* export already exists, zero out this one */
930                                 CERROR("%s: Duplicate export %s!\n",
931                                        tgt_name(tgt), lcd->lcd_uuid);
932                                 continue;
933                         }
934                         GOTO(err_out, rc = PTR_ERR(exp));
935                 }
936
937                 ted = &exp->exp_target_data;
938                 *ted->ted_lcd = *lcd;
939
940                 rc = tgt_client_add(env, exp, cl_idx);
941                 LASSERTF(rc == 0, "rc = %d\n", rc); /* can't fail existing */
942                 /* VBR: set export last committed version */
943                 exp->exp_last_committed = last_transno;
944                 spin_lock(&exp->exp_lock);
945                 exp->exp_connecting = 0;
946                 exp->exp_in_recovery = 0;
947                 spin_unlock(&exp->exp_lock);
948                 obd->obd_max_recoverable_clients++;
949                 class_export_put(exp);
950
951                 /* Need to check last_rcvd even for duplicated exports. */
952                 CDEBUG(D_OTHER, "client at idx %d has last_transno = "LPU64"\n",
953                        cl_idx, last_transno);
954
955                 spin_lock(&tgt->lut_translock);
956                 tgt->lut_last_transno = max(last_transno,
957                                             tgt->lut_last_transno);
958                 spin_unlock(&tgt->lut_translock);
959         }
960
961 err_out:
962         OBD_FREE_PTR(lcd);
963         RETURN(rc);
964 }
965
966 struct server_compat_data {
967         __u32 rocompat;
968         __u32 incompat;
969         __u32 rocinit;
970         __u32 incinit;
971 };
972
973 static struct server_compat_data tgt_scd[] = {
974         [LDD_F_SV_TYPE_MDT] = {
975                 .rocompat = OBD_ROCOMPAT_LOVOBJID,
976                 .incompat = OBD_INCOMPAT_MDT | OBD_INCOMPAT_COMMON_LR |
977                             OBD_INCOMPAT_FID | OBD_INCOMPAT_IAM_DIR |
978                             OBD_INCOMPAT_LMM_VER | OBD_INCOMPAT_MULTI_OI,
979                 .rocinit = OBD_ROCOMPAT_LOVOBJID,
980                 .incinit = OBD_INCOMPAT_MDT | OBD_INCOMPAT_COMMON_LR |
981                            OBD_INCOMPAT_MULTI_OI,
982         },
983         [LDD_F_SV_TYPE_OST] = {
984                 .rocompat = OBD_ROCOMPAT_IDX_IN_IDIF,
985                 .incompat = OBD_INCOMPAT_OST | OBD_INCOMPAT_COMMON_LR |
986                             OBD_INCOMPAT_FID,
987                 .rocinit = OBD_ROCOMPAT_IDX_IN_IDIF,
988                 .incinit = OBD_INCOMPAT_OST | OBD_INCOMPAT_COMMON_LR,
989         }
990 };
991
992 int tgt_server_data_init(const struct lu_env *env, struct lu_target *tgt)
993 {
994         struct tgt_thread_info          *tti = tgt_th_info(env);
995         struct lr_server_data           *lsd = &tgt->lut_lsd;
996         unsigned long                    last_rcvd_size;
997         __u32                            index;
998         int                              rc, type;
999
1000         rc = dt_attr_get(env, tgt->lut_last_rcvd, &tti->tti_attr);
1001         if (rc)
1002                 RETURN(rc);
1003
1004         last_rcvd_size = (unsigned long)tti->tti_attr.la_size;
1005
1006         /* ensure padding in the struct is the correct size */
1007         CLASSERT(offsetof(struct lr_server_data, lsd_padding) +
1008                  sizeof(lsd->lsd_padding) == LR_SERVER_SIZE);
1009
1010         rc = server_name2index(tgt_name(tgt), &index, NULL);
1011         if (rc < 0) {
1012                 CERROR("%s: Can not get index from name: rc = %d\n",
1013                        tgt_name(tgt), rc);
1014                 RETURN(rc);
1015         }
1016         /* server_name2index() returns type */
1017         type = rc;
1018         if (type != LDD_F_SV_TYPE_MDT && type != LDD_F_SV_TYPE_OST) {
1019                 CERROR("%s: unknown target type %x\n", tgt_name(tgt), type);
1020                 RETURN(-EINVAL);
1021         }
1022
1023         /* last_rcvd on OST doesn't provide reconstruct support because there
1024          * may be up to 8 in-flight write requests per single slot in
1025          * last_rcvd client data
1026          */
1027         tgt->lut_no_reconstruct = (type == LDD_F_SV_TYPE_OST);
1028
1029         if (last_rcvd_size == 0) {
1030                 LCONSOLE_WARN("%s: new disk, initializing\n", tgt_name(tgt));
1031
1032                 memcpy(lsd->lsd_uuid, tgt->lut_obd->obd_uuid.uuid,
1033                        sizeof(lsd->lsd_uuid));
1034                 lsd->lsd_last_transno = 0;
1035                 lsd->lsd_mount_count = 0;
1036                 lsd->lsd_server_size = LR_SERVER_SIZE;
1037                 lsd->lsd_client_start = LR_CLIENT_START;
1038                 lsd->lsd_client_size = LR_CLIENT_SIZE;
1039                 lsd->lsd_subdir_count = OBJ_SUBDIR_COUNT;
1040                 lsd->lsd_osd_index = index;
1041                 lsd->lsd_feature_rocompat = tgt_scd[type].rocinit;
1042                 lsd->lsd_feature_incompat = tgt_scd[type].incinit;
1043         } else {
1044                 rc = tgt_server_data_read(env, tgt);
1045                 if (rc) {
1046                         CERROR("%s: error reading LAST_RCVD: rc= %d\n",
1047                                tgt_name(tgt), rc);
1048                         RETURN(rc);
1049                 }
1050                 if (strcmp(lsd->lsd_uuid, tgt->lut_obd->obd_uuid.uuid)) {
1051                         LCONSOLE_ERROR_MSG(0x157, "Trying to start OBD %s "
1052                                            "using the wrong disk %s. Were the"
1053                                            " /dev/ assignments rearranged?\n",
1054                                            tgt->lut_obd->obd_uuid.uuid,
1055                                            lsd->lsd_uuid);
1056                         RETURN(-EINVAL);
1057                 }
1058
1059                 if (lsd->lsd_osd_index != index) {
1060                         LCONSOLE_ERROR_MSG(0x157, "%s: index %d in last rcvd "
1061                                            "is different with the index %d in"
1062                                            "config log, It might be disk"
1063                                            "corruption!\n", tgt_name(tgt),
1064                                            lsd->lsd_osd_index, index);
1065                         RETURN(-EINVAL);
1066                 }
1067         }
1068
1069         if (lsd->lsd_feature_incompat & ~tgt_scd[type].incompat) {
1070                 CERROR("%s: unsupported incompat filesystem feature(s) %x\n",
1071                        tgt_name(tgt),
1072                        lsd->lsd_feature_incompat & ~tgt_scd[type].incompat);
1073                 RETURN(-EINVAL);
1074         }
1075
1076         if (type == LDD_F_SV_TYPE_MDT)
1077                 lsd->lsd_feature_incompat |= OBD_INCOMPAT_FID;
1078
1079         if (lsd->lsd_feature_rocompat & ~tgt_scd[type].rocompat) {
1080                 CERROR("%s: unsupported read-only filesystem feature(s) %x\n",
1081                        tgt_name(tgt),
1082                        lsd->lsd_feature_rocompat & ~tgt_scd[type].rocompat);
1083                 RETURN(-EINVAL);
1084         }
1085         /** Interop: evict all clients at first boot with 1.8 last_rcvd */
1086         if (type == LDD_F_SV_TYPE_MDT &&
1087             !(lsd->lsd_feature_compat & OBD_COMPAT_20)) {
1088                 if (last_rcvd_size > lsd->lsd_client_start) {
1089                         LCONSOLE_WARN("%s: mounting at first time on 1.8 FS, "
1090                                       "remove all clients for interop needs\n",
1091                                       tgt_name(tgt));
1092                         rc = tgt_truncate_last_rcvd(env, tgt,
1093                                                     lsd->lsd_client_start);
1094                         if (rc)
1095                                 RETURN(rc);
1096                         last_rcvd_size = lsd->lsd_client_start;
1097                 }
1098                 /** set 2.0 flag to upgrade/downgrade between 1.8 and 2.0 */
1099                 lsd->lsd_feature_compat |= OBD_COMPAT_20;
1100         }
1101
1102         spin_lock(&tgt->lut_translock);
1103         tgt->lut_last_transno = lsd->lsd_last_transno;
1104         spin_unlock(&tgt->lut_translock);
1105
1106         lsd->lsd_mount_count++;
1107
1108         CDEBUG(D_INODE, "=======,=BEGIN DUMPING LAST_RCVD========\n");
1109         CDEBUG(D_INODE, "%s: server last_transno: "LPU64"\n",
1110                tgt_name(tgt), tgt->lut_last_transno);
1111         CDEBUG(D_INODE, "%s: server mount_count: "LPU64"\n",
1112                tgt_name(tgt), lsd->lsd_mount_count);
1113         CDEBUG(D_INODE, "%s: server data size: %u\n",
1114                tgt_name(tgt), lsd->lsd_server_size);
1115         CDEBUG(D_INODE, "%s: per-client data start: %u\n",
1116                tgt_name(tgt), lsd->lsd_client_start);
1117         CDEBUG(D_INODE, "%s: per-client data size: %u\n",
1118                tgt_name(tgt), lsd->lsd_client_size);
1119         CDEBUG(D_INODE, "%s: last_rcvd size: %lu\n",
1120                tgt_name(tgt), last_rcvd_size);
1121         CDEBUG(D_INODE, "%s: server subdir_count: %u\n",
1122                tgt_name(tgt), lsd->lsd_subdir_count);
1123         CDEBUG(D_INODE, "%s: last_rcvd clients: %lu\n", tgt_name(tgt),
1124                last_rcvd_size <= lsd->lsd_client_start ? 0 :
1125                (last_rcvd_size - lsd->lsd_client_start) /
1126                 lsd->lsd_client_size);
1127         CDEBUG(D_INODE, "========END DUMPING LAST_RCVD========\n");
1128
1129         if (lsd->lsd_server_size == 0 || lsd->lsd_client_start == 0 ||
1130             lsd->lsd_client_size == 0) {
1131                 CERROR("%s: bad last_rcvd contents!\n", tgt_name(tgt));
1132                 RETURN(-EINVAL);
1133         }
1134
1135         if (!tgt->lut_obd->obd_replayable)
1136                 CWARN("%s: recovery support OFF\n", tgt_name(tgt));
1137
1138         rc = tgt_clients_data_init(env, tgt, last_rcvd_size);
1139         if (rc < 0)
1140                 GOTO(err_client, rc);
1141
1142         spin_lock(&tgt->lut_translock);
1143         /* obd_last_committed is used for compatibility
1144          * with other lustre recovery code */
1145         tgt->lut_obd->obd_last_committed = tgt->lut_last_transno;
1146         spin_unlock(&tgt->lut_translock);
1147
1148         tgt->lut_obd->u.obt.obt_mount_count = lsd->lsd_mount_count;
1149         tgt->lut_obd->u.obt.obt_instance = (__u32)lsd->lsd_mount_count;
1150
1151         /* save it, so mount count and last_transno is current */
1152         rc = tgt_server_data_update(env, tgt, 0);
1153         if (rc < 0)
1154                 GOTO(err_client, rc);
1155
1156         RETURN(0);
1157
1158 err_client:
1159         class_disconnect_exports(tgt->lut_obd);
1160         return rc;
1161 }
1162
1163 /* add credits for last_rcvd update */
1164 int tgt_txn_start_cb(const struct lu_env *env, struct thandle *th,
1165                      void *cookie)
1166 {
1167         struct lu_target        *tgt = cookie;
1168         struct tgt_session_info *tsi;
1169         struct tgt_thread_info  *tti = tgt_th_info(env);
1170         int                      rc;
1171
1172         /* if there is no session, then this transaction is not result of
1173          * request processing but some local operation */
1174         if (env->le_ses == NULL)
1175                 return 0;
1176
1177         LASSERT(tgt->lut_last_rcvd);
1178         tsi = tgt_ses_info(env);
1179         /* OFD may start transaction without export assigned */
1180         if (tsi->tsi_exp == NULL)
1181                 return 0;
1182
1183         tti_buf_lcd(tti);
1184         rc = dt_declare_record_write(env, tgt->lut_last_rcvd,
1185                                      &tti->tti_buf,
1186                                      tsi->tsi_exp->exp_target_data.ted_lr_off,
1187                                      th);
1188         if (rc)
1189                 return rc;
1190
1191         tti_buf_lsd(tti);
1192         rc = dt_declare_record_write(env, tgt->lut_last_rcvd,
1193                                      &tti->tti_buf, 0, th);
1194         if (rc)
1195                 return rc;
1196
1197         if (tsi->tsi_vbr_obj != NULL &&
1198             !lu_object_remote(&tsi->tsi_vbr_obj->do_lu))
1199                 rc = dt_declare_version_set(env, tsi->tsi_vbr_obj, th);
1200
1201         return rc;
1202 }
1203
1204 /* Update last_rcvd records with latests transaction data */
1205 int tgt_txn_stop_cb(const struct lu_env *env, struct thandle *th,
1206                     void *cookie)
1207 {
1208         struct lu_target        *tgt = cookie;
1209         struct tgt_session_info *tsi;
1210         struct tgt_thread_info  *tti = tgt_th_info(env);
1211         struct dt_object        *obj = NULL;
1212         int                      rc;
1213         bool                     echo_client;
1214
1215         if (env->le_ses == NULL)
1216                 return 0;
1217
1218         tsi = tgt_ses_info(env);
1219         /* OFD may start transaction without export assigned */
1220         if (tsi->tsi_exp == NULL)
1221                 return 0;
1222
1223         echo_client = (tgt_ses_req(tsi) == NULL);
1224
1225         if (tti->tti_has_trans && !echo_client) {
1226                 if (tti->tti_mult_trans == 0) {
1227                         CDEBUG(D_HA, "More than one transaction "LPU64"\n",
1228                                tti->tti_transno);
1229                         RETURN(0);
1230                 }
1231                 /* we need another transno to be assigned */
1232                 tti->tti_transno = 0;
1233         } else if (th->th_result == 0) {
1234                 tti->tti_has_trans = 1;
1235         }
1236
1237         if (tsi->tsi_vbr_obj != NULL &&
1238             !lu_object_remote(&tsi->tsi_vbr_obj->do_lu)) {
1239                 obj = tsi->tsi_vbr_obj;
1240         }
1241
1242         if (unlikely(echo_client)) /* echo client special case */
1243                 rc = tgt_last_rcvd_update_echo(env, tgt, obj, th,
1244                                                tsi->tsi_exp);
1245         else
1246                 rc = tgt_last_rcvd_update(env, tgt, obj, tsi->tsi_opdata, th,
1247                                           tgt_ses_req(tsi));
1248         return rc;
1249 }