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