Whamcloud - gitweb
c7aecdf2171ea648f89043fa86b7b944edee75f1
[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.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, 2016, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  *
32  * Lustre Unified Target
33  * These are common function to work with last_received file
34  *
35  * Author: Mikhail Pershin <mike.pershin@intel.com>
36  */
37 #include <obd.h>
38 #include <obd_class.h>
39 #include <lustre_fid.h>
40
41 #include "tgt_internal.h"
42
43 /** version recovery epoch */
44 #define LR_EPOCH_BITS   32
45
46 /* Allocate a bitmap for a chunk of reply data slots */
47 static int tgt_bitmap_chunk_alloc(struct lu_target *lut, int chunk)
48 {
49         unsigned long *bm;
50
51         OBD_ALLOC_LARGE(bm, BITS_TO_LONGS(LUT_REPLY_SLOTS_PER_CHUNK) *
52                         sizeof(long));
53         if (bm == NULL)
54                 return -ENOMEM;
55
56         spin_lock(&lut->lut_client_bitmap_lock);
57
58         if (lut->lut_reply_bitmap[chunk] != NULL) {
59                 /* someone else already allocated the bitmap for this chunk */
60                 spin_unlock(&lut->lut_client_bitmap_lock);
61                 OBD_FREE_LARGE(bm, BITS_TO_LONGS(LUT_REPLY_SLOTS_PER_CHUNK) *
62                          sizeof(long));
63                 return 0;
64         }
65
66         lut->lut_reply_bitmap[chunk] = bm;
67
68         spin_unlock(&lut->lut_client_bitmap_lock);
69
70         return 0;
71 }
72
73 /* Look for an available reply data slot in the bitmap
74  * of the target @lut
75  * Allocate bitmap chunk when first used
76  * XXX algo could be improved if this routine limits performance
77  */
78 static int tgt_find_free_reply_slot(struct lu_target *lut)
79 {
80         unsigned long *bmp;
81         int chunk = 0;
82         int rc;
83         int b;
84
85         for (chunk = 0; chunk < LUT_REPLY_SLOTS_MAX_CHUNKS; chunk++) {
86                 /* allocate the bitmap chunk if necessary */
87                 if (unlikely(lut->lut_reply_bitmap[chunk] == NULL)) {
88                         rc = tgt_bitmap_chunk_alloc(lut, chunk);
89                         if (rc != 0)
90                                 return rc;
91                 }
92                 bmp = lut->lut_reply_bitmap[chunk];
93
94                 /* look for an available slot in this chunk */
95                 do {
96                         b = find_first_zero_bit(bmp, LUT_REPLY_SLOTS_PER_CHUNK);
97                         if (b >= LUT_REPLY_SLOTS_PER_CHUNK)
98                                 break;
99
100                         /* found one */
101                         if (test_and_set_bit(b, bmp) == 0)
102                                 return chunk * LUT_REPLY_SLOTS_PER_CHUNK + b;
103                 } while (true);
104         }
105
106         return -ENOSPC;
107 }
108
109 /* Mark the reply data slot @idx 'used' in the corresponding bitmap chunk
110  * of the target @lut
111  * Allocate the bitmap chunk if necessary
112  */
113 static int tgt_set_reply_slot(struct lu_target *lut, int idx)
114 {
115         int chunk;
116         int b;
117         int rc;
118
119         chunk = idx / LUT_REPLY_SLOTS_PER_CHUNK;
120         b = idx % LUT_REPLY_SLOTS_PER_CHUNK;
121
122         LASSERT(chunk < LUT_REPLY_SLOTS_MAX_CHUNKS);
123         LASSERT(b < LUT_REPLY_SLOTS_PER_CHUNK);
124
125         /* allocate the bitmap chunk if necessary */
126         if (unlikely(lut->lut_reply_bitmap[chunk] == NULL)) {
127                 rc = tgt_bitmap_chunk_alloc(lut, chunk);
128                 if (rc != 0)
129                         return rc;
130         }
131
132         /* mark the slot 'used' in this chunk */
133         if (test_and_set_bit(b, lut->lut_reply_bitmap[chunk]) != 0) {
134                 CERROR("%s: slot %d already set in bitmap\n",
135                        tgt_name(lut), idx);
136                 return -EALREADY;
137         }
138
139         return 0;
140 }
141
142
143 /* Mark the reply data slot @idx 'unused' in the corresponding bitmap chunk
144  * of the target @lut
145  */
146 static int tgt_clear_reply_slot(struct lu_target *lut, int idx)
147 {
148         int chunk;
149         int b;
150
151         chunk = idx / LUT_REPLY_SLOTS_PER_CHUNK;
152         b = idx % LUT_REPLY_SLOTS_PER_CHUNK;
153
154         LASSERT(chunk < LUT_REPLY_SLOTS_MAX_CHUNKS);
155         LASSERT(b < LUT_REPLY_SLOTS_PER_CHUNK);
156
157         if (lut->lut_reply_bitmap[chunk] == NULL) {
158                 CERROR("%s: slot %d not allocated\n",
159                        tgt_name(lut), idx);
160                 return -ENOENT;
161         }
162
163         if (test_and_clear_bit(b, lut->lut_reply_bitmap[chunk]) == 0) {
164                 CERROR("%s: slot %d already clear in bitmap\n",
165                        tgt_name(lut), idx);
166                 return -EALREADY;
167         }
168
169         return 0;
170 }
171
172
173 /* Read header of reply_data file of target @tgt into structure @lrh */
174 static int tgt_reply_header_read(const struct lu_env *env,
175                                  struct lu_target *tgt,
176                                  struct lsd_reply_header *lrh)
177 {
178         int                      rc;
179         struct lsd_reply_header  buf;
180         struct tgt_thread_info  *tti = tgt_th_info(env);
181
182         tti->tti_off = 0;
183         tti->tti_buf.lb_buf = &buf;
184         tti->tti_buf.lb_len = sizeof(buf);
185
186         rc = dt_record_read(env, tgt->lut_reply_data, &tti->tti_buf,
187                             &tti->tti_off);
188         if (rc != 0)
189                 return rc;
190
191         lrh->lrh_magic = le32_to_cpu(buf.lrh_magic);
192         lrh->lrh_header_size = le32_to_cpu(buf.lrh_header_size);
193         lrh->lrh_reply_size = le32_to_cpu(buf.lrh_reply_size);
194
195         CDEBUG(D_HA, "%s: read %s header. magic=0x%08x "
196                "header_size=%d reply_size=%d\n",
197                 tgt->lut_obd->obd_name, REPLY_DATA,
198                 lrh->lrh_magic, lrh->lrh_header_size, lrh->lrh_reply_size);
199
200         return 0;
201 }
202
203 /* Write header into replay_data file of target @tgt from structure @lrh */
204 static int tgt_reply_header_write(const struct lu_env *env,
205                                   struct lu_target *tgt,
206                                   struct lsd_reply_header *lrh)
207 {
208         int                      rc;
209         struct lsd_reply_header  buf;
210         struct tgt_thread_info  *tti = tgt_th_info(env);
211         struct thandle          *th;
212         struct dt_object        *dto;
213
214         CDEBUG(D_HA, "%s: write %s header. magic=0x%08x "
215                "header_size=%d reply_size=%d\n",
216                 tgt->lut_obd->obd_name, REPLY_DATA,
217                 lrh->lrh_magic, lrh->lrh_header_size, lrh->lrh_reply_size);
218
219         if (tgt->lut_bottom->dd_rdonly)
220                 RETURN(0);
221
222         buf.lrh_magic = cpu_to_le32(lrh->lrh_magic);
223         buf.lrh_header_size = cpu_to_le32(lrh->lrh_header_size);
224         buf.lrh_reply_size = cpu_to_le32(lrh->lrh_reply_size);
225
226         th = dt_trans_create(env, tgt->lut_bottom);
227         if (IS_ERR(th))
228                 return PTR_ERR(th);
229         th->th_sync = 1;
230
231         tti->tti_off = 0;
232         tti->tti_buf.lb_buf = &buf;
233         tti->tti_buf.lb_len = sizeof(buf);
234
235         rc = dt_declare_record_write(env, tgt->lut_reply_data,
236                                      &tti->tti_buf, tti->tti_off, th);
237         if (rc)
238                 GOTO(out, rc);
239
240         rc = dt_trans_start(env, tgt->lut_bottom, th);
241         if (rc)
242                 GOTO(out, rc);
243
244         dto = dt_object_locate(tgt->lut_reply_data, th->th_dev);
245         rc = dt_record_write(env, dto, &tti->tti_buf, &tti->tti_off, th);
246 out:
247         dt_trans_stop(env, tgt->lut_bottom, th);
248         return rc;
249 }
250
251 /* Write the reply data @lrd into reply_data file of target @tgt
252  * at offset @off
253  */
254 static int tgt_reply_data_write(const struct lu_env *env, struct lu_target *tgt,
255                                 struct lsd_reply_data *lrd, loff_t off,
256                                 struct thandle *th)
257 {
258         struct tgt_thread_info  *tti = tgt_th_info(env);
259         struct dt_object        *dto;
260         struct lsd_reply_data   *buf = &tti->tti_lrd;
261
262         lrd->lrd_result = ptlrpc_status_hton(lrd->lrd_result);
263
264         buf->lrd_transno         = cpu_to_le64(lrd->lrd_transno);
265         buf->lrd_xid             = cpu_to_le64(lrd->lrd_xid);
266         buf->lrd_data            = cpu_to_le64(lrd->lrd_data);
267         buf->lrd_result          = cpu_to_le32(lrd->lrd_result);
268         buf->lrd_client_gen      = cpu_to_le32(lrd->lrd_client_gen);
269
270         lrd->lrd_result = ptlrpc_status_ntoh(lrd->lrd_result);
271
272         tti->tti_off = off;
273         tti->tti_buf.lb_buf = buf;
274         tti->tti_buf.lb_len = sizeof(*buf);
275
276         dto = dt_object_locate(tgt->lut_reply_data, th->th_dev);
277         return dt_record_write(env, dto, &tti->tti_buf, &tti->tti_off, th);
278 }
279
280 /* Read the reply data from reply_data file of target @tgt at offset @off
281  * into structure @lrd
282  */
283 static int tgt_reply_data_read(const struct lu_env *env, struct lu_target *tgt,
284                                struct lsd_reply_data *lrd, loff_t off)
285 {
286         int                      rc;
287         struct tgt_thread_info  *tti = tgt_th_info(env);
288         struct lsd_reply_data   *buf = &tti->tti_lrd;
289
290         tti->tti_off = off;
291         tti->tti_buf.lb_buf = buf;
292         tti->tti_buf.lb_len = sizeof(*buf);
293
294         rc = dt_record_read(env, tgt->lut_reply_data, &tti->tti_buf,
295                             &tti->tti_off);
296         if (rc != 0)
297                 return rc;
298
299         lrd->lrd_transno         = le64_to_cpu(buf->lrd_transno);
300         lrd->lrd_xid             = le64_to_cpu(buf->lrd_xid);
301         lrd->lrd_data            = le64_to_cpu(buf->lrd_data);
302         lrd->lrd_result          = le32_to_cpu(buf->lrd_result);
303         lrd->lrd_client_gen      = le32_to_cpu(buf->lrd_client_gen);
304
305         return 0;
306 }
307
308
309 /* Free the in-memory reply data structure @trd and release
310  * the corresponding slot in the reply_data file of target @lut
311  * Called with ted_lcd_lock held
312  */
313 static void tgt_free_reply_data(struct lu_target *lut,
314                                 struct tg_export_data *ted,
315                                 struct tg_reply_data *trd)
316 {
317         CDEBUG(D_TRACE, "%s: free reply data %p: xid %llu, transno %llu, "
318                "client gen %u, slot idx %d\n",
319                lut == NULL ? "" : tgt_name(lut), trd, trd->trd_reply.lrd_xid,
320                trd->trd_reply.lrd_transno, trd->trd_reply.lrd_client_gen,
321                trd->trd_index);
322
323         LASSERT(mutex_is_locked(&ted->ted_lcd_lock));
324
325         list_del(&trd->trd_list);
326         ted->ted_reply_cnt--;
327         if (lut != NULL)
328                 tgt_clear_reply_slot(lut, trd->trd_index);
329         OBD_FREE_PTR(trd);
330 }
331
332 /* Release the reply data @trd from target @lut
333  * The reply data with the highest transno for this export
334  * is retained to ensure correctness of target recovery
335  * Called with ted_lcd_lock held
336  */
337 static void tgt_release_reply_data(struct lu_target *lut,
338                                    struct tg_export_data *ted,
339                                    struct tg_reply_data *trd)
340 {
341         CDEBUG(D_TRACE, "%s: release reply data %p: xid %llu, transno %llu, "
342                "client gen %u, slot idx %d\n",
343                lut == NULL ? "" : tgt_name(lut), trd, trd->trd_reply.lrd_xid,
344                trd->trd_reply.lrd_transno, trd->trd_reply.lrd_client_gen,
345                trd->trd_index);
346
347         LASSERT(mutex_is_locked(&ted->ted_lcd_lock));
348
349         /* Do not free the reply data corresponding to the
350          * highest transno of this export.
351          * This ensures on-disk reply data is kept and
352          * last committed transno can be restored from disk in case
353          * of target recovery
354          */
355         if (trd->trd_reply.lrd_transno == ted->ted_lcd->lcd_last_transno) {
356                 /* free previous retained reply */
357                 if (ted->ted_reply_last != NULL)
358                         tgt_free_reply_data(lut, ted, ted->ted_reply_last);
359                 /* retain the reply */
360                 list_del_init(&trd->trd_list);
361                 ted->ted_reply_last = trd;
362         } else {
363                 tgt_free_reply_data(lut, ted, trd);
364         }
365 }
366
367 static inline struct lu_buf *tti_buf_lsd(struct tgt_thread_info *tti)
368 {
369         tti->tti_buf.lb_buf = &tti->tti_lsd;
370         tti->tti_buf.lb_len = sizeof(tti->tti_lsd);
371         return &tti->tti_buf;
372 }
373
374 static inline struct lu_buf *tti_buf_lcd(struct tgt_thread_info *tti)
375 {
376         tti->tti_buf.lb_buf = &tti->tti_lcd;
377         tti->tti_buf.lb_len = sizeof(tti->tti_lcd);
378         return &tti->tti_buf;
379 }
380
381 /**
382  * Allocate in-memory data for client slot related to export.
383  */
384 int tgt_client_alloc(struct obd_export *exp)
385 {
386         ENTRY;
387         LASSERT(exp != exp->exp_obd->obd_self_export);
388
389         spin_lock_init(&exp->exp_target_data.ted_nodemap_lock);
390         INIT_LIST_HEAD(&exp->exp_target_data.ted_nodemap_member);
391
392         OBD_ALLOC_PTR(exp->exp_target_data.ted_lcd);
393         if (exp->exp_target_data.ted_lcd == NULL)
394                 RETURN(-ENOMEM);
395         /* Mark that slot is not yet valid, 0 doesn't work here */
396         exp->exp_target_data.ted_lr_idx = -1;
397         INIT_LIST_HEAD(&exp->exp_target_data.ted_reply_list);
398         mutex_init(&exp->exp_target_data.ted_lcd_lock);
399         RETURN(0);
400 }
401 EXPORT_SYMBOL(tgt_client_alloc);
402
403 /**
404  * Free in-memory data for client slot related to export.
405  */
406 void tgt_client_free(struct obd_export *exp)
407 {
408         struct tg_export_data   *ted = &exp->exp_target_data;
409         struct lu_target        *lut = class_exp2tgt(exp);
410         struct tg_reply_data    *trd, *tmp;
411
412         LASSERT(exp != exp->exp_obd->obd_self_export);
413
414         /* free reply data */
415         mutex_lock(&ted->ted_lcd_lock);
416         list_for_each_entry_safe(trd, tmp, &ted->ted_reply_list, trd_list) {
417                 tgt_release_reply_data(lut, ted, trd);
418         }
419         if (ted->ted_reply_last != NULL) {
420                 tgt_free_reply_data(lut, ted, ted->ted_reply_last);
421                 ted->ted_reply_last = NULL;
422         }
423         mutex_unlock(&ted->ted_lcd_lock);
424
425         if (!hlist_unhashed(&exp->exp_gen_hash))
426                 cfs_hash_del(exp->exp_obd->obd_gen_hash,
427                              &ted->ted_lcd->lcd_generation,
428                              &exp->exp_gen_hash);
429
430         OBD_FREE_PTR(ted->ted_lcd);
431         ted->ted_lcd = NULL;
432
433         /* Target may have been freed (see LU-7430)
434          * Slot may be not yet assigned */
435         if (exp->exp_obd->u.obt.obt_magic != OBT_MAGIC ||
436             ted->ted_lr_idx < 0)
437                 return;
438
439         /* Clear bit when lcd is freed */
440         LASSERT(lut && lut->lut_client_bitmap);
441         if (!test_and_clear_bit(ted->ted_lr_idx, lut->lut_client_bitmap)) {
442                 CERROR("%s: client %u bit already clear in bitmap\n",
443                        exp->exp_obd->obd_name, ted->ted_lr_idx);
444                 LBUG();
445         }
446
447         if (tgt_is_multimodrpcs_client(exp) && !exp->exp_obd->obd_stopping)
448                 atomic_dec(&lut->lut_num_clients);
449 }
450 EXPORT_SYMBOL(tgt_client_free);
451
452 static inline void tgt_check_lcd(const char *obd_name, int index,
453                                  struct lsd_client_data *lcd)
454 {
455         size_t uuid_size = sizeof(lcd->lcd_uuid);
456
457         if (strnlen((char*)lcd->lcd_uuid, uuid_size) == uuid_size) {
458                 lcd->lcd_uuid[uuid_size - 1] = '\0';
459
460                 LCONSOLE_ERROR("the client UUID (%s) on %s for exports stored in last_rcvd(index = %d) is bad!\n",
461                                lcd->lcd_uuid, obd_name, index);
462         }
463 }
464
465 static int tgt_client_data_read(const struct lu_env *env, struct lu_target *tgt,
466                                 struct lsd_client_data *lcd,
467                                 loff_t *off, int index)
468 {
469         struct tgt_thread_info  *tti = tgt_th_info(env);
470         int                      rc;
471
472         tti_buf_lcd(tti);
473         rc = dt_record_read(env, tgt->lut_last_rcvd, &tti->tti_buf, off);
474         if (rc == 0) {
475                 tgt_check_lcd(tgt->lut_obd->obd_name, index, &tti->tti_lcd);
476                 lcd_le_to_cpu(&tti->tti_lcd, lcd);
477                 lcd->lcd_last_result = ptlrpc_status_ntoh(lcd->lcd_last_result);
478                 lcd->lcd_last_close_result =
479                         ptlrpc_status_ntoh(lcd->lcd_last_close_result);
480         }
481
482         CDEBUG(D_INFO, "%s: read lcd @%lld uuid = %s, last_transno = %llu"
483                ", last_xid = %llu, last_result = %u, last_data = %u, "
484                "last_close_transno = %llu, last_close_xid = %llu, "
485                "last_close_result = %u, rc = %d\n", tgt->lut_obd->obd_name,
486                *off, lcd->lcd_uuid, lcd->lcd_last_transno, lcd->lcd_last_xid,
487                lcd->lcd_last_result, lcd->lcd_last_data,
488                lcd->lcd_last_close_transno, lcd->lcd_last_close_xid,
489                lcd->lcd_last_close_result, rc);
490         return rc;
491 }
492
493 static int tgt_client_data_write(const struct lu_env *env,
494                                  struct lu_target *tgt,
495                                  struct lsd_client_data *lcd,
496                                  loff_t *off, struct thandle *th)
497 {
498         struct tgt_thread_info *tti = tgt_th_info(env);
499         struct dt_object        *dto;
500
501         lcd->lcd_last_result = ptlrpc_status_hton(lcd->lcd_last_result);
502         lcd->lcd_last_close_result =
503                 ptlrpc_status_hton(lcd->lcd_last_close_result);
504         lcd_cpu_to_le(lcd, &tti->tti_lcd);
505         tti_buf_lcd(tti);
506
507         dto = dt_object_locate(tgt->lut_last_rcvd, th->th_dev);
508         return dt_record_write(env, dto, &tti->tti_buf, off, th);
509 }
510
511 struct tgt_new_client_callback {
512         struct dt_txn_commit_cb  lncc_cb;
513         struct obd_export       *lncc_exp;
514 };
515
516 static void tgt_cb_new_client(struct lu_env *env, struct thandle *th,
517                               struct dt_txn_commit_cb *cb, int err)
518 {
519         struct tgt_new_client_callback *ccb;
520
521         ccb = container_of0(cb, struct tgt_new_client_callback, lncc_cb);
522
523         LASSERT(ccb->lncc_exp->exp_obd);
524
525         CDEBUG(D_RPCTRACE, "%s: committing for initial connect of %s\n",
526                ccb->lncc_exp->exp_obd->obd_name,
527                ccb->lncc_exp->exp_client_uuid.uuid);
528
529         spin_lock(&ccb->lncc_exp->exp_lock);
530
531         ccb->lncc_exp->exp_need_sync = 0;
532
533         spin_unlock(&ccb->lncc_exp->exp_lock);
534         class_export_cb_put(ccb->lncc_exp);
535
536         OBD_FREE_PTR(ccb);
537 }
538
539 int tgt_new_client_cb_add(struct thandle *th, struct obd_export *exp)
540 {
541         struct tgt_new_client_callback  *ccb;
542         struct dt_txn_commit_cb         *dcb;
543         int                              rc;
544
545         OBD_ALLOC_PTR(ccb);
546         if (ccb == NULL)
547                 return -ENOMEM;
548
549         ccb->lncc_exp = class_export_cb_get(exp);
550
551         dcb = &ccb->lncc_cb;
552         dcb->dcb_func = tgt_cb_new_client;
553         INIT_LIST_HEAD(&dcb->dcb_linkage);
554         strlcpy(dcb->dcb_name, "tgt_cb_new_client", sizeof(dcb->dcb_name));
555
556         rc = dt_trans_cb_add(th, dcb);
557         if (rc) {
558                 class_export_cb_put(exp);
559                 OBD_FREE_PTR(ccb);
560         }
561         return rc;
562 }
563
564 /**
565  * Update client data in last_rcvd
566  */
567 static int tgt_client_data_update(const struct lu_env *env,
568                                   struct obd_export *exp)
569 {
570         struct tg_export_data   *ted = &exp->exp_target_data;
571         struct lu_target        *tgt = class_exp2tgt(exp);
572         struct tgt_thread_info  *tti = tgt_th_info(env);
573         struct thandle          *th;
574         int                      rc = 0;
575
576         ENTRY;
577
578         if (unlikely(tgt == NULL)) {
579                 CDEBUG(D_ERROR, "%s: No target for connected export\n",
580                           class_exp2obd(exp)->obd_name);
581                 RETURN(-EINVAL);
582         }
583
584         if (tgt->lut_bottom->dd_rdonly)
585                 RETURN(0);
586
587         th = dt_trans_create(env, tgt->lut_bottom);
588         if (IS_ERR(th))
589                 RETURN(PTR_ERR(th));
590
591         tti_buf_lcd(tti);
592         rc = dt_declare_record_write(env, tgt->lut_last_rcvd,
593                                      &tti->tti_buf,
594                                      ted->ted_lr_off, th);
595         if (rc)
596                 GOTO(out, rc);
597
598         rc = dt_trans_start_local(env, tgt->lut_bottom, th);
599         if (rc)
600                 GOTO(out, rc);
601
602         mutex_lock(&ted->ted_lcd_lock);
603
604         /*
605          * Until this operations will be committed the sync is needed
606          * for this export. This should be done _after_ starting the
607          * transaction so that many connecting clients will not bring
608          * server down with lots of sync writes.
609          */
610         rc = tgt_new_client_cb_add(th, exp);
611         if (rc) {
612                 /* can't add callback, do sync now */
613                 th->th_sync = 1;
614         } else {
615                 spin_lock(&exp->exp_lock);
616                 exp->exp_need_sync = 1;
617                 spin_unlock(&exp->exp_lock);
618         }
619
620         tti->tti_off = ted->ted_lr_off;
621         rc = tgt_client_data_write(env, tgt, ted->ted_lcd, &tti->tti_off, th);
622
623         mutex_unlock(&ted->ted_lcd_lock);
624
625         EXIT;
626 out:
627         dt_trans_stop(env, tgt->lut_bottom, th);
628         CDEBUG(D_INFO, "%s: update last_rcvd client data for UUID = %s, "
629                "last_transno = %llu: rc = %d\n", tgt->lut_obd->obd_name,
630                tgt->lut_lsd.lsd_uuid, tgt->lut_lsd.lsd_last_transno, rc);
631
632         return rc;
633 }
634
635 static int tgt_server_data_read(const struct lu_env *env, struct lu_target *tgt)
636 {
637         struct tgt_thread_info  *tti = tgt_th_info(env);
638         int                      rc;
639
640         tti->tti_off = 0;
641         tti_buf_lsd(tti);
642         rc = dt_record_read(env, tgt->lut_last_rcvd, &tti->tti_buf,
643                             &tti->tti_off);
644         if (rc == 0)
645                 lsd_le_to_cpu(&tti->tti_lsd, &tgt->lut_lsd);
646
647         CDEBUG(D_INFO, "%s: read last_rcvd server data for UUID = %s, "
648                "last_transno = %llu: rc = %d\n", tgt->lut_obd->obd_name,
649                tgt->lut_lsd.lsd_uuid, tgt->lut_lsd.lsd_last_transno, rc);
650         return rc;
651 }
652
653 static int tgt_server_data_write(const struct lu_env *env,
654                                  struct lu_target *tgt, struct thandle *th)
655 {
656         struct tgt_thread_info  *tti = tgt_th_info(env);
657         struct dt_object        *dto;
658         int                      rc;
659
660         ENTRY;
661
662         tti->tti_off = 0;
663         tti_buf_lsd(tti);
664         lsd_cpu_to_le(&tgt->lut_lsd, &tti->tti_lsd);
665
666         dto = dt_object_locate(tgt->lut_last_rcvd, th->th_dev);
667         rc = dt_record_write(env, dto, &tti->tti_buf, &tti->tti_off, th);
668
669         CDEBUG(D_INFO, "%s: write last_rcvd server data for UUID = %s, "
670                "last_transno = %llu: rc = %d\n", tgt->lut_obd->obd_name,
671                tgt->lut_lsd.lsd_uuid, tgt->lut_lsd.lsd_last_transno, rc);
672
673         RETURN(rc);
674 }
675
676 /**
677  * Update server data in last_rcvd
678  */
679 int tgt_server_data_update(const struct lu_env *env, struct lu_target *tgt,
680                            int sync)
681 {
682         struct tgt_thread_info  *tti = tgt_th_info(env);
683         struct thandle          *th;
684         int                      rc = 0;
685
686         ENTRY;
687
688         CDEBUG(D_SUPER,
689                "%s: mount_count is %llu, last_transno is %llu\n",
690                tgt->lut_lsd.lsd_uuid, tgt->lut_obd->u.obt.obt_mount_count,
691                tgt->lut_last_transno);
692
693         /* Always save latest transno to keep it fresh */
694         spin_lock(&tgt->lut_translock);
695         tgt->lut_lsd.lsd_last_transno = tgt->lut_last_transno;
696         spin_unlock(&tgt->lut_translock);
697
698         if (tgt->lut_bottom->dd_rdonly)
699                 RETURN(0);
700
701         th = dt_trans_create(env, tgt->lut_bottom);
702         if (IS_ERR(th))
703                 RETURN(PTR_ERR(th));
704
705         th->th_sync = sync;
706
707         tti_buf_lsd(tti);
708         rc = dt_declare_record_write(env, tgt->lut_last_rcvd,
709                                      &tti->tti_buf, tti->tti_off, th);
710         if (rc)
711                 GOTO(out, rc);
712
713         rc = dt_trans_start(env, tgt->lut_bottom, th);
714         if (rc)
715                 GOTO(out, rc);
716
717         rc = tgt_server_data_write(env, tgt, th);
718 out:
719         dt_trans_stop(env, tgt->lut_bottom, th);
720
721         CDEBUG(D_INFO, "%s: update last_rcvd server data for UUID = %s, "
722                "last_transno = %llu: rc = %d\n", tgt->lut_obd->obd_name,
723                tgt->lut_lsd.lsd_uuid, tgt->lut_lsd.lsd_last_transno, rc);
724         RETURN(rc);
725 }
726 EXPORT_SYMBOL(tgt_server_data_update);
727
728 static int tgt_truncate_last_rcvd(const struct lu_env *env,
729                                   struct lu_target *tgt, loff_t size)
730 {
731         struct dt_object *dt = tgt->lut_last_rcvd;
732         struct thandle   *th;
733         struct lu_attr    attr;
734         int               rc;
735
736         ENTRY;
737
738         if (tgt->lut_bottom->dd_rdonly)
739                 RETURN(0);
740
741         attr.la_size = size;
742         attr.la_valid = LA_SIZE;
743
744         th = dt_trans_create(env, tgt->lut_bottom);
745         if (IS_ERR(th))
746                 RETURN(PTR_ERR(th));
747         rc = dt_declare_punch(env, dt, size, OBD_OBJECT_EOF, th);
748         if (rc)
749                 GOTO(cleanup, rc);
750         rc = dt_declare_attr_set(env, dt, &attr, th);
751         if (rc)
752                 GOTO(cleanup, rc);
753         rc = dt_trans_start_local(env, tgt->lut_bottom, th);
754         if (rc)
755                 GOTO(cleanup, rc);
756
757         rc = dt_punch(env, dt, size, OBD_OBJECT_EOF, th);
758         if (rc == 0)
759                 rc = dt_attr_set(env, dt, &attr, th);
760
761 cleanup:
762         dt_trans_stop(env, tgt->lut_bottom, th);
763
764         RETURN(rc);
765 }
766
767 static void tgt_client_epoch_update(const struct lu_env *env,
768                                     struct obd_export *exp)
769 {
770         struct lsd_client_data  *lcd = exp->exp_target_data.ted_lcd;
771         struct lu_target        *tgt = class_exp2tgt(exp);
772
773         LASSERT(tgt && tgt->lut_bottom);
774         /** VBR: set client last_epoch to current epoch */
775         if (lcd->lcd_last_epoch >= tgt->lut_lsd.lsd_start_epoch)
776                 return;
777         lcd->lcd_last_epoch = tgt->lut_lsd.lsd_start_epoch;
778         tgt_client_data_update(env, exp);
779 }
780
781 /**
782  * Update boot epoch when recovery ends
783  */
784 void tgt_boot_epoch_update(struct lu_target *tgt)
785 {
786         struct lu_env            env;
787         struct ptlrpc_request   *req;
788         __u32                    start_epoch;
789         struct list_head         client_list;
790         int                      rc;
791
792         if (tgt->lut_obd->obd_stopping)
793                 return;
794
795         rc = lu_env_init(&env, LCT_DT_THREAD);
796         if (rc) {
797                 CERROR("%s: can't initialize environment: rc = %d\n",
798                         tgt->lut_obd->obd_name, rc);
799                 return;
800         }
801
802         spin_lock(&tgt->lut_translock);
803         start_epoch = (tgt->lut_last_transno >> LR_EPOCH_BITS) + 1;
804         tgt->lut_last_transno = (__u64)start_epoch << LR_EPOCH_BITS;
805         tgt->lut_lsd.lsd_start_epoch = start_epoch;
806         spin_unlock(&tgt->lut_translock);
807
808         INIT_LIST_HEAD(&client_list);
809         /**
810          * The recovery is not yet finished and final queue can still be updated
811          * with resend requests. Move final list to separate one for processing
812          */
813         spin_lock(&tgt->lut_obd->obd_recovery_task_lock);
814         list_splice_init(&tgt->lut_obd->obd_final_req_queue, &client_list);
815         spin_unlock(&tgt->lut_obd->obd_recovery_task_lock);
816
817         /**
818          * go through list of exports participated in recovery and
819          * set new epoch for them
820          */
821         list_for_each_entry(req, &client_list, rq_list) {
822                 LASSERT(!req->rq_export->exp_delayed);
823                 if (!req->rq_export->exp_vbr_failed)
824                         tgt_client_epoch_update(&env, req->rq_export);
825         }
826         /** return list back at once */
827         spin_lock(&tgt->lut_obd->obd_recovery_task_lock);
828         list_splice_init(&client_list, &tgt->lut_obd->obd_final_req_queue);
829         spin_unlock(&tgt->lut_obd->obd_recovery_task_lock);
830
831         /** Clear MULTI RPCS incompatibility flag if
832          * - target is MDT and
833          * - there is no client to recover or the recovery was aborted
834          */
835         if (!strncmp(tgt->lut_obd->obd_type->typ_name, LUSTRE_MDT_NAME, 3) &&
836             (tgt->lut_obd->obd_max_recoverable_clients == 0 ||
837             tgt->lut_obd->obd_abort_recovery))
838                 tgt->lut_lsd.lsd_feature_incompat &= ~OBD_INCOMPAT_MULTI_RPCS;
839
840         /** update server epoch */
841         tgt_server_data_update(&env, tgt, 1);
842         lu_env_fini(&env);
843 }
844
845 /**
846  * commit callback, need to update last_committed value
847  */
848 struct tgt_last_committed_callback {
849         struct dt_txn_commit_cb  llcc_cb;
850         struct lu_target        *llcc_tgt;
851         struct obd_export       *llcc_exp;
852         __u64                    llcc_transno;
853 };
854
855 static void tgt_cb_last_committed(struct lu_env *env, struct thandle *th,
856                                   struct dt_txn_commit_cb *cb, int err)
857 {
858         struct tgt_last_committed_callback *ccb;
859
860         ccb = container_of0(cb, struct tgt_last_committed_callback, llcc_cb);
861
862         LASSERT(ccb->llcc_exp);
863         LASSERT(ccb->llcc_tgt != NULL);
864         LASSERT(ccb->llcc_exp->exp_obd == ccb->llcc_tgt->lut_obd);
865
866         /* error hit, don't update last committed to provide chance to
867          * replay data after fail */
868         if (err != 0)
869                 goto out;
870
871         /* Fast path w/o spinlock, if exp_last_committed was updated
872          * with higher transno, no need to take spinlock and check,
873          * also no need to update obd_last_committed. */
874         if (ccb->llcc_transno <= ccb->llcc_exp->exp_last_committed)
875                 goto out;
876         spin_lock(&ccb->llcc_tgt->lut_translock);
877         if (ccb->llcc_transno > ccb->llcc_tgt->lut_obd->obd_last_committed)
878                 ccb->llcc_tgt->lut_obd->obd_last_committed = ccb->llcc_transno;
879
880         if (ccb->llcc_transno > ccb->llcc_exp->exp_last_committed) {
881                 ccb->llcc_exp->exp_last_committed = ccb->llcc_transno;
882                 spin_unlock(&ccb->llcc_tgt->lut_translock);
883
884                 ptlrpc_commit_replies(ccb->llcc_exp);
885                 tgt_cancel_slc_locks(ccb->llcc_tgt, ccb->llcc_transno);
886         } else {
887                 spin_unlock(&ccb->llcc_tgt->lut_translock);
888         }
889
890         CDEBUG(D_HA, "%s: transno %lld is committed\n",
891                ccb->llcc_tgt->lut_obd->obd_name, ccb->llcc_transno);
892
893 out:
894         class_export_cb_put(ccb->llcc_exp);
895         OBD_FREE_PTR(ccb);
896 }
897
898 /**
899  * Add commit callback function, it returns a non-zero value to inform
900  * caller to use sync transaction if necessary.
901  */
902 static int tgt_last_commit_cb_add(struct thandle *th, struct lu_target *tgt,
903                                   struct obd_export *exp, __u64 transno)
904 {
905         struct tgt_last_committed_callback      *ccb;
906         struct dt_txn_commit_cb                 *dcb;
907         int                                      rc;
908
909         OBD_ALLOC_PTR(ccb);
910         if (ccb == NULL)
911                 return -ENOMEM;
912
913         ccb->llcc_tgt = tgt;
914         ccb->llcc_exp = class_export_cb_get(exp);
915         ccb->llcc_transno = transno;
916
917         dcb = &ccb->llcc_cb;
918         dcb->dcb_func = tgt_cb_last_committed;
919         INIT_LIST_HEAD(&dcb->dcb_linkage);
920         strlcpy(dcb->dcb_name, "tgt_cb_last_committed", sizeof(dcb->dcb_name));
921
922         rc = dt_trans_cb_add(th, dcb);
923         if (rc) {
924                 class_export_cb_put(exp);
925                 OBD_FREE_PTR(ccb);
926         }
927
928         if (exp_connect_flags(exp) & OBD_CONNECT_LIGHTWEIGHT)
929                 /* report failure to force synchronous operation */
930                 return -EPERM;
931
932         /* if exp_need_sync is set, return non-zero value to force
933          * a sync transaction. */
934         return rc ? rc : exp->exp_need_sync;
935 }
936
937 /**
938  * Add new client to the last_rcvd upon new connection.
939  *
940  * We use a bitmap to locate a free space in the last_rcvd file and initialize
941  * tg_export_data.
942  */
943 int tgt_client_new(const struct lu_env *env, struct obd_export *exp)
944 {
945         struct tg_export_data   *ted = &exp->exp_target_data;
946         struct lu_target        *tgt = class_exp2tgt(exp);
947         int                      rc = 0, idx;
948
949         ENTRY;
950
951         LASSERT(tgt && tgt->lut_client_bitmap != NULL);
952         if (!strcmp(ted->ted_lcd->lcd_uuid, tgt->lut_obd->obd_uuid.uuid))
953                 RETURN(0);
954
955         if (exp_connect_flags(exp) & OBD_CONNECT_LIGHTWEIGHT)
956                 RETURN(0);
957
958         /* the bitmap operations can handle cl_idx > sizeof(long) * 8, so
959          * there's no need for extra complication here
960          */
961         idx = find_first_zero_bit(tgt->lut_client_bitmap, LR_MAX_CLIENTS);
962 repeat:
963         if (idx >= LR_MAX_CLIENTS ||
964             OBD_FAIL_CHECK(OBD_FAIL_MDS_CLIENT_ADD)) {
965                 CERROR("%s: no room for %u clients - fix LR_MAX_CLIENTS\n",
966                        tgt->lut_obd->obd_name,  idx);
967                 RETURN(-EOVERFLOW);
968         }
969         if (test_and_set_bit(idx, tgt->lut_client_bitmap)) {
970                 idx = find_next_zero_bit(tgt->lut_client_bitmap,
971                                              LR_MAX_CLIENTS, idx);
972                 goto repeat;
973         }
974
975         ted->ted_lr_idx = idx;
976         ted->ted_lr_off = tgt->lut_lsd.lsd_client_start +
977                           idx * tgt->lut_lsd.lsd_client_size;
978
979         LASSERTF(ted->ted_lr_off > 0, "ted_lr_off = %llu\n", ted->ted_lr_off);
980
981         if (tgt_is_multimodrpcs_client(exp)) {
982                 /* Set MULTI RPCS incompatibility flag to prevent previous
983                  * Lustre versions to mount a target with reply_data file */
984                 atomic_inc(&tgt->lut_num_clients);
985                 if (!(tgt->lut_lsd.lsd_feature_incompat &
986                       OBD_INCOMPAT_MULTI_RPCS)) {
987                         tgt->lut_lsd.lsd_feature_incompat |=
988                                                         OBD_INCOMPAT_MULTI_RPCS;
989                         rc = tgt_server_data_update(env, tgt, 1);
990                         if (rc < 0) {
991                                 CERROR("%s: unable to set MULTI RPCS "
992                                        "incompatibility flag\n",
993                                        exp->exp_obd->obd_name);
994                                 RETURN(rc);
995                         }
996                 }
997
998                 /* assign client slot generation */
999                 ted->ted_lcd->lcd_generation =
1000                                 atomic_inc_return(&tgt->lut_client_generation);
1001         } else {
1002                 ted->ted_lcd->lcd_generation = 0;
1003         }
1004
1005         CDEBUG(D_INFO, "%s: new client at index %d (%llu) with UUID '%s' "
1006                "generation %d\n",
1007                tgt->lut_obd->obd_name, ted->ted_lr_idx, ted->ted_lr_off,
1008                ted->ted_lcd->lcd_uuid, ted->ted_lcd->lcd_generation);
1009
1010         if (OBD_FAIL_CHECK(OBD_FAIL_TGT_CLIENT_ADD))
1011                 RETURN(-ENOSPC);
1012
1013         rc = tgt_client_data_update(env, exp);
1014         if (rc)
1015                 CERROR("%s: Failed to write client lcd at idx %d, rc %d\n",
1016                        tgt->lut_obd->obd_name, idx, rc);
1017
1018         RETURN(rc);
1019 }
1020 EXPORT_SYMBOL(tgt_client_new);
1021
1022 /* Add an existing client to the MDS in-memory state based on
1023  * a client that was previously found in the last_rcvd file and
1024  * already has an assigned slot (idx >= 0).
1025  *
1026  * It should not be possible to fail adding an existing client - otherwise
1027  * mdt_init_server_data() callsite needs to be fixed.
1028  */
1029 int tgt_client_add(const struct lu_env *env,  struct obd_export *exp, int idx)
1030 {
1031         struct tg_export_data   *ted = &exp->exp_target_data;
1032         struct lu_target        *tgt = class_exp2tgt(exp);
1033
1034         ENTRY;
1035
1036         LASSERT(tgt && tgt->lut_client_bitmap != NULL);
1037         LASSERTF(idx >= 0, "%d\n", idx);
1038
1039         if (!strcmp(ted->ted_lcd->lcd_uuid, tgt->lut_obd->obd_uuid.uuid) ||
1040             exp_connect_flags(exp) & OBD_CONNECT_LIGHTWEIGHT)
1041                 RETURN(0);
1042
1043         if (test_and_set_bit(idx, tgt->lut_client_bitmap)) {
1044                 CERROR("%s: client %d: bit already set in bitmap!!\n",
1045                        tgt->lut_obd->obd_name,  idx);
1046                 LBUG();
1047         }
1048         atomic_inc(&tgt->lut_num_clients);
1049
1050         CDEBUG(D_INFO, "%s: client at idx %d with UUID '%s' added, "
1051                "generation %d\n",
1052                tgt->lut_obd->obd_name, idx, ted->ted_lcd->lcd_uuid,
1053                ted->ted_lcd->lcd_generation);
1054
1055         ted->ted_lr_idx = idx;
1056         ted->ted_lr_off = tgt->lut_lsd.lsd_client_start +
1057                           idx * tgt->lut_lsd.lsd_client_size;
1058
1059         mutex_init(&ted->ted_lcd_lock);
1060
1061         LASSERTF(ted->ted_lr_off > 0, "ted_lr_off = %llu\n", ted->ted_lr_off);
1062
1063         RETURN(0);
1064 }
1065
1066 int tgt_client_del(const struct lu_env *env, struct obd_export *exp)
1067 {
1068         struct tg_export_data   *ted = &exp->exp_target_data;
1069         struct lu_target        *tgt = class_exp2tgt(exp);
1070         int                      rc;
1071
1072         ENTRY;
1073
1074         LASSERT(ted->ted_lcd);
1075
1076         if (unlikely(tgt == NULL)) {
1077                 CDEBUG(D_ERROR, "%s: No target for connected export\n",
1078                        class_exp2obd(exp)->obd_name);
1079                 RETURN(-EINVAL);
1080         }
1081
1082         /* XXX if lcd_uuid were a real obd_uuid, I could use obd_uuid_equals */
1083         if (!strcmp((char *)ted->ted_lcd->lcd_uuid,
1084                     (char *)tgt->lut_obd->obd_uuid.uuid) ||
1085             exp_connect_flags(exp) & OBD_CONNECT_LIGHTWEIGHT)
1086                 RETURN(0);
1087
1088         /* Slot may be not yet assigned, use case is race between Client
1089          * reconnect and forced eviction */
1090         if (ted->ted_lr_idx < 0) {
1091                 CWARN("%s: client with UUID '%s' not in bitmap\n",
1092                       tgt->lut_obd->obd_name, ted->ted_lcd->lcd_uuid);
1093                 RETURN(0);
1094         }
1095
1096         CDEBUG(D_INFO, "%s: del client at idx %u, off %lld, UUID '%s'\n",
1097                tgt->lut_obd->obd_name, ted->ted_lr_idx, ted->ted_lr_off,
1098                ted->ted_lcd->lcd_uuid);
1099
1100         /* Clear the bit _after_ zeroing out the client so we don't
1101            race with filter_client_add and zero out new clients.*/
1102         if (!test_bit(ted->ted_lr_idx, tgt->lut_client_bitmap)) {
1103                 CERROR("%s: client %u: bit already clear in bitmap!!\n",
1104                        tgt->lut_obd->obd_name, ted->ted_lr_idx);
1105                 LBUG();
1106         }
1107
1108         /* Do not erase record for recoverable client. */
1109         if (exp->exp_flags & OBD_OPT_FAILOVER)
1110                 RETURN(0);
1111
1112         if (OBD_FAIL_CHECK(OBD_FAIL_TGT_CLIENT_DEL))
1113                 RETURN(0);
1114
1115         /* Make sure the server's last_transno is up to date.
1116          * This should be done before zeroing client slot so last_transno will
1117          * be in server data or in client data in case of failure */
1118         rc = tgt_server_data_update(env, tgt, 0);
1119         if (rc != 0) {
1120                 CERROR("%s: failed to update server data, skip client %s "
1121                        "zeroing, rc %d\n", tgt->lut_obd->obd_name,
1122                        ted->ted_lcd->lcd_uuid, rc);
1123                 RETURN(rc);
1124         }
1125
1126         memset(ted->ted_lcd->lcd_uuid, 0, sizeof ted->ted_lcd->lcd_uuid);
1127         rc = tgt_client_data_update(env, exp);
1128
1129         CDEBUG(rc == 0 ? D_INFO : D_ERROR,
1130                "%s: zeroing out client %s at idx %u (%llu), rc %d\n",
1131                tgt->lut_obd->obd_name, ted->ted_lcd->lcd_uuid,
1132                ted->ted_lr_idx, ted->ted_lr_off, rc);
1133         RETURN(rc);
1134 }
1135 EXPORT_SYMBOL(tgt_client_del);
1136
1137 int tgt_add_reply_data(const struct lu_env *env, struct lu_target *tgt,
1138                        struct tg_export_data *ted, struct tg_reply_data *trd,
1139                        struct thandle *th, bool update_lrd_file)
1140 {
1141         struct lsd_reply_data   *lrd;
1142         int     i;
1143
1144         lrd = &trd->trd_reply;
1145         /* update export last transno */
1146         mutex_lock(&ted->ted_lcd_lock);
1147         if (lrd->lrd_transno > ted->ted_lcd->lcd_last_transno)
1148                 ted->ted_lcd->lcd_last_transno = lrd->lrd_transno;
1149         mutex_unlock(&ted->ted_lcd_lock);
1150
1151         /* find a empty slot */
1152         i = tgt_find_free_reply_slot(tgt);
1153         if (unlikely(i < 0)) {
1154                 CERROR("%s: couldn't find a slot for reply data: "
1155                        "rc = %d\n", tgt_name(tgt), i);
1156                 RETURN(i);
1157         }
1158         trd->trd_index = i;
1159
1160         if (update_lrd_file) {
1161                 loff_t  off;
1162                 int     rc;
1163
1164                 /* write reply data to disk */
1165                 off = sizeof(struct lsd_reply_header) + sizeof(*lrd) * i;
1166                 rc = tgt_reply_data_write(env, tgt, lrd, off, th);
1167                 if (unlikely(rc != 0)) {
1168                         CERROR("%s: can't update %s file: rc = %d\n",
1169                                tgt_name(tgt), REPLY_DATA, rc);
1170                         RETURN(rc);
1171                 }
1172         }
1173         /* add reply data to target export's reply list */
1174         mutex_lock(&ted->ted_lcd_lock);
1175         list_add(&trd->trd_list, &ted->ted_reply_list);
1176         ted->ted_reply_cnt++;
1177         if (ted->ted_reply_cnt > ted->ted_reply_max)
1178                 ted->ted_reply_max = ted->ted_reply_cnt;
1179         mutex_unlock(&ted->ted_lcd_lock);
1180
1181         CDEBUG(D_TRACE, "add reply %p: xid %llu, transno %llu, "
1182                "tag %hu, client gen %u, slot idx %d\n",
1183                trd, lrd->lrd_xid, lrd->lrd_transno,
1184                trd->trd_tag, lrd->lrd_client_gen, i);
1185         RETURN(0);
1186 }
1187 EXPORT_SYMBOL(tgt_add_reply_data);
1188
1189 /*
1190  * last_rcvd & last_committed update callbacks
1191  */
1192 static int tgt_last_rcvd_update(const struct lu_env *env, struct lu_target *tgt,
1193                                 struct dt_object *obj, __u64 opdata,
1194                                 struct thandle *th, struct ptlrpc_request *req)
1195 {
1196         struct tgt_thread_info  *tti = tgt_th_info(env);
1197         struct tgt_session_info *tsi = tgt_ses_info(env);
1198         struct obd_export       *exp = tsi->tsi_exp;
1199         struct tg_export_data   *ted;
1200         __u64                   *transno_p;
1201         int                      rc = 0;
1202         bool                     lw_client;
1203
1204         ENTRY;
1205
1206
1207         LASSERT(exp != NULL);
1208         ted = &exp->exp_target_data;
1209
1210         lw_client = exp_connect_flags(exp) & OBD_CONNECT_LIGHTWEIGHT;
1211         if (ted->ted_lr_idx < 0 && !lw_client)
1212                 /* ofd connect may cause transaction before export has
1213                  * last_rcvd slot */
1214                 RETURN(0);
1215
1216         if (req != NULL)
1217                 tti->tti_transno = lustre_msg_get_transno(req->rq_reqmsg);
1218         else
1219                 /* From update replay, tti_transno should be set already */
1220                 LASSERT(tti->tti_transno != 0);
1221
1222         spin_lock(&tgt->lut_translock);
1223         if (th->th_result != 0) {
1224                 if (tti->tti_transno != 0) {
1225                         CERROR("%s: replay transno %llu failed: rc = %d\n",
1226                                tgt_name(tgt), tti->tti_transno, th->th_result);
1227                 }
1228         } else if (tti->tti_transno == 0) {
1229                 tti->tti_transno = ++tgt->lut_last_transno;
1230         } else {
1231                 /* should be replay */
1232                 if (tti->tti_transno > tgt->lut_last_transno)
1233                         tgt->lut_last_transno = tti->tti_transno;
1234         }
1235         spin_unlock(&tgt->lut_translock);
1236
1237         /** VBR: set new versions */
1238         if (th->th_result == 0 && obj != NULL) {
1239                 struct dt_object *dto = dt_object_locate(obj, th->th_dev);
1240                 dt_version_set(env, dto, tti->tti_transno, th);
1241         }
1242
1243         /* filling reply data */
1244         CDEBUG(D_INODE, "transno = %llu, last_committed = %llu\n",
1245                tti->tti_transno, tgt->lut_obd->obd_last_committed);
1246
1247         if (req != NULL) {
1248                 req->rq_transno = tti->tti_transno;
1249                 lustre_msg_set_transno(req->rq_repmsg, tti->tti_transno);
1250         }
1251
1252         /* if can't add callback, do sync write */
1253         th->th_sync |= !!tgt_last_commit_cb_add(th, tgt, exp, tti->tti_transno);
1254
1255         if (lw_client) {
1256                 /* All operations performed by LW clients are synchronous and
1257                  * we store the committed transno in the last_rcvd header */
1258                 spin_lock(&tgt->lut_translock);
1259                 if (tti->tti_transno > tgt->lut_lsd.lsd_last_transno) {
1260                         tgt->lut_lsd.lsd_last_transno = tti->tti_transno;
1261                         spin_unlock(&tgt->lut_translock);
1262                         /* Although lightweight (LW) connections have no slot
1263                          * in the last_rcvd, we still want to maintain
1264                          * the in-memory lsd_client_data structure in order to
1265                          * properly handle reply reconstruction. */
1266                         rc = tgt_server_data_write(env, tgt, th);
1267                 } else {
1268                         spin_unlock(&tgt->lut_translock);
1269                 }
1270         } else if (ted->ted_lr_off == 0) {
1271                 CERROR("%s: client idx %d has offset %lld\n",
1272                        tgt_name(tgt), ted->ted_lr_idx, ted->ted_lr_off);
1273                 RETURN(-EINVAL);
1274         }
1275
1276         /* Target that supports multiple reply data */
1277         if (tgt_is_multimodrpcs_client(exp)) {
1278                 struct tg_reply_data    *trd;
1279                 struct lsd_reply_data   *lrd;
1280                 __u64                   *pre_versions;
1281                 bool                    write_update;
1282
1283                 OBD_ALLOC_PTR(trd);
1284                 if (unlikely(trd == NULL))
1285                         RETURN(-ENOMEM);
1286
1287                 /* fill reply data information */
1288                 lrd = &trd->trd_reply;
1289                 lrd->lrd_transno = tti->tti_transno;
1290                 if (req != NULL) {
1291                         lrd->lrd_xid = req->rq_xid;
1292                         trd->trd_tag = lustre_msg_get_tag(req->rq_reqmsg);
1293                         pre_versions = lustre_msg_get_versions(req->rq_repmsg);
1294                         lrd->lrd_result = th->th_result;
1295                         lrd->lrd_client_gen = ted->ted_lcd->lcd_generation;
1296                         write_update = true;
1297                 } else {
1298                         LASSERT(tsi->tsi_xid != 0);
1299                         lrd->lrd_xid = tsi->tsi_xid;
1300                         lrd->lrd_result = tsi->tsi_result;
1301                         lrd->lrd_client_gen = tsi->tsi_client_gen;
1302                         trd->trd_tag = 0;
1303                         pre_versions = NULL;
1304                         write_update = false;
1305                 }
1306
1307                 lrd->lrd_data = opdata;
1308                 if (pre_versions) {
1309                         trd->trd_pre_versions[0] = pre_versions[0];
1310                         trd->trd_pre_versions[1] = pre_versions[1];
1311                         trd->trd_pre_versions[2] = pre_versions[2];
1312                         trd->trd_pre_versions[3] = pre_versions[3];
1313                 }
1314
1315                 rc = tgt_add_reply_data(env, tgt, ted, trd, th, write_update);
1316                 if (rc < 0)
1317                         OBD_FREE_PTR(trd);
1318                 return rc;
1319         }
1320
1321         /* Enough for update replay, let's return */
1322         if (req == NULL)
1323                 RETURN(rc);
1324
1325         mutex_lock(&ted->ted_lcd_lock);
1326         LASSERT(ergo(tti->tti_transno == 0, th->th_result != 0));
1327         if (lustre_msg_get_opc(req->rq_reqmsg) == MDS_CLOSE) {
1328                 transno_p = &ted->ted_lcd->lcd_last_close_transno;
1329                 ted->ted_lcd->lcd_last_close_xid = req->rq_xid;
1330                 ted->ted_lcd->lcd_last_close_result = th->th_result;
1331         } else {
1332                 /* VBR: save versions in last_rcvd for reconstruct. */
1333                 __u64 *pre_versions = lustre_msg_get_versions(req->rq_repmsg);
1334
1335                 if (pre_versions) {
1336                         ted->ted_lcd->lcd_pre_versions[0] = pre_versions[0];
1337                         ted->ted_lcd->lcd_pre_versions[1] = pre_versions[1];
1338                         ted->ted_lcd->lcd_pre_versions[2] = pre_versions[2];
1339                         ted->ted_lcd->lcd_pre_versions[3] = pre_versions[3];
1340                 }
1341                 transno_p = &ted->ted_lcd->lcd_last_transno;
1342                 ted->ted_lcd->lcd_last_xid = req->rq_xid;
1343                 ted->ted_lcd->lcd_last_result = th->th_result;
1344                 /* XXX: lcd_last_data is __u32 but intent_dispostion is __u64,
1345                  * see struct ldlm_reply->lock_policy_res1; */
1346                 ted->ted_lcd->lcd_last_data = opdata;
1347         }
1348
1349         /* Update transno in slot only if non-zero number, i.e. no errors */
1350         if (likely(tti->tti_transno != 0)) {
1351                 /* Don't overwrite bigger transaction number with lower one.
1352                  * That is not sign of problem in all cases, but in any case
1353                  * this value should be monotonically increased only. */
1354                 if (*transno_p > tti->tti_transno) {
1355                         if (!tgt->lut_no_reconstruct) {
1356                                 CERROR("%s: trying to overwrite bigger transno:"
1357                                        "on-disk: %llu, new: %llu replay: "
1358                                        "%d. See LU-617.\n", tgt_name(tgt),
1359                                        *transno_p, tti->tti_transno,
1360                                        req_is_replay(req));
1361                                 if (req_is_replay(req)) {
1362                                         spin_lock(&req->rq_export->exp_lock);
1363                                         req->rq_export->exp_vbr_failed = 1;
1364                                         spin_unlock(&req->rq_export->exp_lock);
1365                                 }
1366                                 mutex_unlock(&ted->ted_lcd_lock);
1367                                 RETURN(req_is_replay(req) ? -EOVERFLOW : 0);
1368                         }
1369                 } else {
1370                         *transno_p = tti->tti_transno;
1371                 }
1372         }
1373
1374         if (!lw_client) {
1375                 tti->tti_off = ted->ted_lr_off;
1376                 if (CFS_FAIL_CHECK(OBD_FAIL_TGT_RCVD_EIO))
1377                         rc = -EIO;
1378                 else
1379                         rc = tgt_client_data_write(env, tgt, ted->ted_lcd,
1380                                                    &tti->tti_off, th);
1381                 if (rc < 0) {
1382                         mutex_unlock(&ted->ted_lcd_lock);
1383                         RETURN(rc);
1384                 }
1385         }
1386         mutex_unlock(&ted->ted_lcd_lock);
1387         RETURN(rc);
1388 }
1389
1390 /*
1391  * last_rcvd update for echo client simulation.
1392  * It updates last_rcvd client slot and version of object in
1393  * simple way but with all locks to simulate all drawbacks
1394  */
1395 static int tgt_last_rcvd_update_echo(const struct lu_env *env,
1396                                      struct lu_target *tgt,
1397                                      struct dt_object *obj,
1398                                      struct thandle *th,
1399                                      struct obd_export *exp)
1400 {
1401         struct tgt_thread_info  *tti = tgt_th_info(env);
1402         struct tg_export_data   *ted = &exp->exp_target_data;
1403         int                      rc = 0;
1404
1405         ENTRY;
1406
1407         tti->tti_transno = 0;
1408
1409         spin_lock(&tgt->lut_translock);
1410         if (th->th_result == 0)
1411                 tti->tti_transno = ++tgt->lut_last_transno;
1412         spin_unlock(&tgt->lut_translock);
1413
1414         /** VBR: set new versions */
1415         if (th->th_result == 0 && obj != NULL)
1416                 dt_version_set(env, obj, tti->tti_transno, th);
1417
1418         /* if can't add callback, do sync write */
1419         th->th_sync |= !!tgt_last_commit_cb_add(th, tgt, exp,
1420                                                 tti->tti_transno);
1421
1422         LASSERT(ted->ted_lr_off > 0);
1423
1424         mutex_lock(&ted->ted_lcd_lock);
1425         LASSERT(ergo(tti->tti_transno == 0, th->th_result != 0));
1426         ted->ted_lcd->lcd_last_transno = tti->tti_transno;
1427         ted->ted_lcd->lcd_last_result = th->th_result;
1428
1429         tti->tti_off = ted->ted_lr_off;
1430         rc = tgt_client_data_write(env, tgt, ted->ted_lcd, &tti->tti_off, th);
1431         mutex_unlock(&ted->ted_lcd_lock);
1432         RETURN(rc);
1433 }
1434
1435 static int tgt_clients_data_init(const struct lu_env *env,
1436                                  struct lu_target *tgt,
1437                                  unsigned long last_size)
1438 {
1439         struct obd_device       *obd = tgt->lut_obd;
1440         struct lr_server_data   *lsd = &tgt->lut_lsd;
1441         struct lsd_client_data  *lcd = NULL;
1442         struct tg_export_data   *ted;
1443         int                      cl_idx;
1444         int                      rc = 0;
1445         loff_t                   off = lsd->lsd_client_start;
1446         __u32                    generation = 0;
1447         struct cfs_hash         *hash = NULL;
1448
1449         ENTRY;
1450
1451         if (tgt->lut_bottom->dd_rdonly)
1452                 RETURN(0);
1453
1454         CLASSERT(offsetof(struct lsd_client_data, lcd_padding) +
1455                  sizeof(lcd->lcd_padding) == LR_CLIENT_SIZE);
1456
1457         OBD_ALLOC_PTR(lcd);
1458         if (lcd == NULL)
1459                 RETURN(-ENOMEM);
1460
1461         hash = cfs_hash_getref(tgt->lut_obd->obd_gen_hash);
1462         if (hash == NULL)
1463                 GOTO(err_out, rc = -ENODEV);
1464
1465         for (cl_idx = 0; off < last_size; cl_idx++) {
1466                 struct obd_export       *exp;
1467                 __u64                    last_transno;
1468
1469                 /* Don't assume off is incremented properly by
1470                  * read_record(), in case sizeof(*lcd)
1471                  * isn't the same as fsd->lsd_client_size.  */
1472                 off = lsd->lsd_client_start + cl_idx * lsd->lsd_client_size;
1473                 rc = tgt_client_data_read(env, tgt, lcd, &off, cl_idx);
1474                 if (rc) {
1475                         CERROR("%s: error reading last_rcvd %s idx %d off "
1476                                "%llu: rc = %d\n", tgt_name(tgt), LAST_RCVD,
1477                                cl_idx, off, rc);
1478                         rc = 0;
1479                         break; /* read error shouldn't cause startup to fail */
1480                 }
1481
1482                 if (lcd->lcd_uuid[0] == '\0') {
1483                         CDEBUG(D_INFO, "skipping zeroed client at offset %d\n",
1484                                cl_idx);
1485                         continue;
1486                 }
1487
1488                 last_transno = lcd_last_transno(lcd);
1489
1490                 /* These exports are cleaned up by disconnect, so they
1491                  * need to be set up like real exports as connect does.
1492                  */
1493                 CDEBUG(D_HA, "RCVRNG CLIENT uuid: %s idx: %d lr: %llu"
1494                        " srv lr: %llu lx: %llu gen %u\n", lcd->lcd_uuid,
1495                        cl_idx, last_transno, lsd->lsd_last_transno,
1496                        lcd_last_xid(lcd), lcd->lcd_generation);
1497
1498                 exp = class_new_export(obd, (struct obd_uuid *)lcd->lcd_uuid);
1499                 if (IS_ERR(exp)) {
1500                         if (PTR_ERR(exp) == -EALREADY) {
1501                                 /* export already exists, zero out this one */
1502                                 CERROR("%s: Duplicate export %s!\n",
1503                                        tgt_name(tgt), lcd->lcd_uuid);
1504                                 continue;
1505                         }
1506                         GOTO(err_out, rc = PTR_ERR(exp));
1507                 }
1508
1509                 ted = &exp->exp_target_data;
1510                 *ted->ted_lcd = *lcd;
1511
1512                 rc = tgt_client_add(env, exp, cl_idx);
1513                 LASSERTF(rc == 0, "rc = %d\n", rc); /* can't fail existing */
1514                 /* VBR: set export last committed version */
1515                 exp->exp_last_committed = last_transno;
1516                 spin_lock(&exp->exp_lock);
1517                 exp->exp_connecting = 0;
1518                 exp->exp_in_recovery = 0;
1519                 spin_unlock(&exp->exp_lock);
1520                 obd->obd_max_recoverable_clients++;
1521
1522                 if (tgt->lut_lsd.lsd_feature_incompat &
1523                     OBD_INCOMPAT_MULTI_RPCS &&
1524                     lcd->lcd_generation != 0) {
1525                         /* compute the highest valid client generation */
1526                         generation = max(generation, lcd->lcd_generation);
1527                         /* fill client_generation <-> export hash table */
1528                         rc = cfs_hash_add_unique(hash, &lcd->lcd_generation,
1529                                                  &exp->exp_gen_hash);
1530                         if (rc != 0) {
1531                                 CERROR("%s: duplicate export for client "
1532                                        "generation %u\n",
1533                                        tgt_name(tgt), lcd->lcd_generation);
1534                                 class_export_put(exp);
1535                                 GOTO(err_out, rc);
1536                         }
1537                 }
1538
1539                 class_export_put(exp);
1540
1541                 rc = rev_import_init(exp);
1542                 if (rc != 0) {
1543                         class_unlink_export(exp);
1544                         GOTO(err_out, rc);
1545                 }
1546
1547                 /* Need to check last_rcvd even for duplicated exports. */
1548                 CDEBUG(D_OTHER, "client at idx %d has last_transno = %llu\n",
1549                        cl_idx, last_transno);
1550
1551                 spin_lock(&tgt->lut_translock);
1552                 tgt->lut_last_transno = max(last_transno,
1553                                             tgt->lut_last_transno);
1554                 spin_unlock(&tgt->lut_translock);
1555         }
1556
1557         /* record highest valid client generation */
1558         atomic_set(&tgt->lut_client_generation, generation);
1559
1560 err_out:
1561         if (hash != NULL)
1562                 cfs_hash_putref(hash);
1563         OBD_FREE_PTR(lcd);
1564         RETURN(rc);
1565 }
1566
1567 struct server_compat_data {
1568         __u32 rocompat;
1569         __u32 incompat;
1570         __u32 rocinit;
1571         __u32 incinit;
1572 };
1573
1574 static struct server_compat_data tgt_scd[] = {
1575         [LDD_F_SV_TYPE_MDT] = {
1576                 .rocompat = OBD_ROCOMPAT_LOVOBJID,
1577                 .incompat = OBD_INCOMPAT_MDT | OBD_INCOMPAT_COMMON_LR |
1578                             OBD_INCOMPAT_FID | OBD_INCOMPAT_IAM_DIR |
1579                             OBD_INCOMPAT_LMM_VER | OBD_INCOMPAT_MULTI_OI |
1580                             OBD_INCOMPAT_MULTI_RPCS,
1581                 .rocinit = OBD_ROCOMPAT_LOVOBJID,
1582                 .incinit = OBD_INCOMPAT_MDT | OBD_INCOMPAT_COMMON_LR |
1583                            OBD_INCOMPAT_MULTI_OI,
1584         },
1585         [LDD_F_SV_TYPE_OST] = {
1586                 .rocompat = OBD_ROCOMPAT_IDX_IN_IDIF,
1587                 .incompat = OBD_INCOMPAT_OST | OBD_INCOMPAT_COMMON_LR |
1588                             OBD_INCOMPAT_FID,
1589                 .rocinit = OBD_ROCOMPAT_IDX_IN_IDIF,
1590                 .incinit = OBD_INCOMPAT_OST | OBD_INCOMPAT_COMMON_LR,
1591         }
1592 };
1593
1594 int tgt_server_data_init(const struct lu_env *env, struct lu_target *tgt)
1595 {
1596         struct tgt_thread_info          *tti = tgt_th_info(env);
1597         struct lr_server_data           *lsd = &tgt->lut_lsd;
1598         unsigned long                    last_rcvd_size;
1599         __u32                            index;
1600         int                              rc, type;
1601
1602         rc = dt_attr_get(env, tgt->lut_last_rcvd, &tti->tti_attr);
1603         if (rc)
1604                 RETURN(rc);
1605
1606         last_rcvd_size = (unsigned long)tti->tti_attr.la_size;
1607
1608         /* ensure padding in the struct is the correct size */
1609         CLASSERT(offsetof(struct lr_server_data, lsd_padding) +
1610                  sizeof(lsd->lsd_padding) == LR_SERVER_SIZE);
1611
1612         rc = server_name2index(tgt_name(tgt), &index, NULL);
1613         if (rc < 0) {
1614                 CERROR("%s: Can not get index from name: rc = %d\n",
1615                        tgt_name(tgt), rc);
1616                 RETURN(rc);
1617         }
1618         /* server_name2index() returns type */
1619         type = rc;
1620         if (type != LDD_F_SV_TYPE_MDT && type != LDD_F_SV_TYPE_OST) {
1621                 CERROR("%s: unknown target type %x\n", tgt_name(tgt), type);
1622                 RETURN(-EINVAL);
1623         }
1624
1625         /* last_rcvd on OST doesn't provide reconstruct support because there
1626          * may be up to 8 in-flight write requests per single slot in
1627          * last_rcvd client data
1628          */
1629         tgt->lut_no_reconstruct = (type == LDD_F_SV_TYPE_OST);
1630
1631         if (last_rcvd_size == 0) {
1632                 LCONSOLE_WARN("%s: new disk, initializing\n", tgt_name(tgt));
1633
1634                 memcpy(lsd->lsd_uuid, tgt->lut_obd->obd_uuid.uuid,
1635                        sizeof(lsd->lsd_uuid));
1636                 lsd->lsd_last_transno = 0;
1637                 lsd->lsd_mount_count = 0;
1638                 lsd->lsd_server_size = LR_SERVER_SIZE;
1639                 lsd->lsd_client_start = LR_CLIENT_START;
1640                 lsd->lsd_client_size = LR_CLIENT_SIZE;
1641                 lsd->lsd_subdir_count = OBJ_SUBDIR_COUNT;
1642                 lsd->lsd_osd_index = index;
1643                 lsd->lsd_feature_rocompat = tgt_scd[type].rocinit;
1644                 lsd->lsd_feature_incompat = tgt_scd[type].incinit;
1645         } else {
1646                 rc = tgt_server_data_read(env, tgt);
1647                 if (rc) {
1648                         CERROR("%s: error reading LAST_RCVD: rc= %d\n",
1649                                tgt_name(tgt), rc);
1650                         RETURN(rc);
1651                 }
1652                 if (strcmp(lsd->lsd_uuid, tgt->lut_obd->obd_uuid.uuid)) {
1653                         if (tgt->lut_bottom->dd_rdonly) {
1654                                 /* Such difference may be caused by mounting
1655                                  * up snapshot with new fsname under rd_only
1656                                  * mode. But even if it was NOT, it will not
1657                                  * damage the system because of "rd_only". */
1658                                 memcpy(lsd->lsd_uuid,
1659                                        tgt->lut_obd->obd_uuid.uuid,
1660                                        sizeof(lsd->lsd_uuid));
1661                         } else {
1662                                 LCONSOLE_ERROR_MSG(0x157, "Trying to start "
1663                                                    "OBD %s using the wrong "
1664                                                    "disk %s. Were the /dev/ "
1665                                                    "assignments rearranged?\n",
1666                                                    tgt->lut_obd->obd_uuid.uuid,
1667                                                    lsd->lsd_uuid);
1668                                 RETURN(-EINVAL);
1669                         }
1670                 }
1671
1672                 if (lsd->lsd_osd_index != index) {
1673                         LCONSOLE_ERROR_MSG(0x157, "%s: index %d in last rcvd "
1674                                            "is different with the index %d in"
1675                                            "config log, It might be disk"
1676                                            "corruption!\n", tgt_name(tgt),
1677                                            lsd->lsd_osd_index, index);
1678                         RETURN(-EINVAL);
1679                 }
1680         }
1681
1682         if (lsd->lsd_feature_incompat & ~tgt_scd[type].incompat) {
1683                 CERROR("%s: unsupported incompat filesystem feature(s) %x\n",
1684                        tgt_name(tgt),
1685                        lsd->lsd_feature_incompat & ~tgt_scd[type].incompat);
1686                 RETURN(-EINVAL);
1687         }
1688
1689         if (type == LDD_F_SV_TYPE_MDT)
1690                 lsd->lsd_feature_incompat |= OBD_INCOMPAT_FID;
1691
1692         if (lsd->lsd_feature_rocompat & ~tgt_scd[type].rocompat) {
1693                 CERROR("%s: unsupported read-only filesystem feature(s) %x\n",
1694                        tgt_name(tgt),
1695                        lsd->lsd_feature_rocompat & ~tgt_scd[type].rocompat);
1696                 RETURN(-EINVAL);
1697         }
1698         /** Interop: evict all clients at first boot with 1.8 last_rcvd */
1699         if (type == LDD_F_SV_TYPE_MDT &&
1700             !(lsd->lsd_feature_compat & OBD_COMPAT_20)) {
1701                 if (last_rcvd_size > lsd->lsd_client_start) {
1702                         LCONSOLE_WARN("%s: mounting at first time on 1.8 FS, "
1703                                       "remove all clients for interop needs\n",
1704                                       tgt_name(tgt));
1705                         rc = tgt_truncate_last_rcvd(env, tgt,
1706                                                     lsd->lsd_client_start);
1707                         if (rc)
1708                                 RETURN(rc);
1709                         last_rcvd_size = lsd->lsd_client_start;
1710                 }
1711                 /** set 2.0 flag to upgrade/downgrade between 1.8 and 2.0 */
1712                 lsd->lsd_feature_compat |= OBD_COMPAT_20;
1713         }
1714
1715         spin_lock(&tgt->lut_translock);
1716         tgt->lut_last_transno = lsd->lsd_last_transno;
1717         spin_unlock(&tgt->lut_translock);
1718
1719         lsd->lsd_mount_count++;
1720
1721         CDEBUG(D_INODE, "=======,=BEGIN DUMPING LAST_RCVD========\n");
1722         CDEBUG(D_INODE, "%s: server last_transno: %llu\n",
1723                tgt_name(tgt), tgt->lut_last_transno);
1724         CDEBUG(D_INODE, "%s: server mount_count: %llu\n",
1725                tgt_name(tgt), lsd->lsd_mount_count);
1726         CDEBUG(D_INODE, "%s: server data size: %u\n",
1727                tgt_name(tgt), lsd->lsd_server_size);
1728         CDEBUG(D_INODE, "%s: per-client data start: %u\n",
1729                tgt_name(tgt), lsd->lsd_client_start);
1730         CDEBUG(D_INODE, "%s: per-client data size: %u\n",
1731                tgt_name(tgt), lsd->lsd_client_size);
1732         CDEBUG(D_INODE, "%s: last_rcvd size: %lu\n",
1733                tgt_name(tgt), last_rcvd_size);
1734         CDEBUG(D_INODE, "%s: server subdir_count: %u\n",
1735                tgt_name(tgt), lsd->lsd_subdir_count);
1736         CDEBUG(D_INODE, "%s: last_rcvd clients: %lu\n", tgt_name(tgt),
1737                last_rcvd_size <= lsd->lsd_client_start ? 0 :
1738                (last_rcvd_size - lsd->lsd_client_start) /
1739                 lsd->lsd_client_size);
1740         CDEBUG(D_INODE, "========END DUMPING LAST_RCVD========\n");
1741
1742         if (lsd->lsd_server_size == 0 || lsd->lsd_client_start == 0 ||
1743             lsd->lsd_client_size == 0) {
1744                 CERROR("%s: bad last_rcvd contents!\n", tgt_name(tgt));
1745                 RETURN(-EINVAL);
1746         }
1747
1748         if (!tgt->lut_obd->obd_replayable)
1749                 CWARN("%s: recovery support OFF\n", tgt_name(tgt));
1750
1751         rc = tgt_clients_data_init(env, tgt, last_rcvd_size);
1752         if (rc < 0)
1753                 GOTO(err_client, rc);
1754
1755         spin_lock(&tgt->lut_translock);
1756         /* obd_last_committed is used for compatibility
1757          * with other lustre recovery code */
1758         tgt->lut_obd->obd_last_committed = tgt->lut_last_transno;
1759         spin_unlock(&tgt->lut_translock);
1760
1761         tgt->lut_obd->u.obt.obt_mount_count = lsd->lsd_mount_count;
1762         tgt->lut_obd->u.obt.obt_instance = (__u32)lsd->lsd_mount_count;
1763
1764         /* save it, so mount count and last_transno is current */
1765         rc = tgt_server_data_update(env, tgt, 0);
1766         if (rc < 0)
1767                 GOTO(err_client, rc);
1768
1769         RETURN(0);
1770
1771 err_client:
1772         class_disconnect_exports(tgt->lut_obd);
1773         return rc;
1774 }
1775
1776 /* add credits for last_rcvd update */
1777 int tgt_txn_start_cb(const struct lu_env *env, struct thandle *th,
1778                      void *cookie)
1779 {
1780         struct lu_target        *tgt = cookie;
1781         struct tgt_session_info *tsi;
1782         struct tgt_thread_info  *tti = tgt_th_info(env);
1783         struct dt_object        *dto;
1784         int                      rc;
1785
1786         /* For readonly case, the caller should have got failure
1787          * when start the transaction. If the logic comes here,
1788          * there must be something wrong. */
1789         if (unlikely(tgt->lut_bottom->dd_rdonly)) {
1790                 dump_stack();
1791                 LBUG();
1792         }
1793
1794         /* if there is no session, then this transaction is not result of
1795          * request processing but some local operation */
1796         if (env->le_ses == NULL)
1797                 return 0;
1798
1799         LASSERT(tgt->lut_last_rcvd);
1800         tsi = tgt_ses_info(env);
1801         /* OFD may start transaction without export assigned */
1802         if (tsi->tsi_exp == NULL)
1803                 return 0;
1804
1805         if (tgt_is_multimodrpcs_client(tsi->tsi_exp)) {
1806                 /*
1807                  * Use maximum possible file offset for declaration to ensure
1808                  * ZFS will reserve enough credits for a write anywhere in this
1809                  * file, since we don't know where in the file the write will be
1810                  * because a replay slot has not been assigned.  This should be
1811                  * replaced by dmu_tx_hold_append() when available.
1812                  */
1813                 tti->tti_buf.lb_buf = NULL;
1814                 tti->tti_buf.lb_len = sizeof(struct lsd_reply_data);
1815                 dto = dt_object_locate(tgt->lut_reply_data, th->th_dev);
1816                 rc = dt_declare_record_write(env, dto, &tti->tti_buf, -1, th);
1817                 if (rc)
1818                         return rc;
1819         } else {
1820                 dto = dt_object_locate(tgt->lut_last_rcvd, th->th_dev);
1821                 tti_buf_lcd(tti);
1822                 tti->tti_off = tsi->tsi_exp->exp_target_data.ted_lr_off;
1823                 rc = dt_declare_record_write(env, dto, &tti->tti_buf,
1824                                              tti->tti_off, th);
1825                 if (rc)
1826                         return rc;
1827         }
1828
1829         if (tsi->tsi_vbr_obj != NULL &&
1830             !lu_object_remote(&tsi->tsi_vbr_obj->do_lu)) {
1831                 dto = dt_object_locate(tsi->tsi_vbr_obj, th->th_dev);
1832                 rc = dt_declare_version_set(env, dto, th);
1833         }
1834
1835         return rc;
1836 }
1837
1838 /* Update last_rcvd records with latests transaction data */
1839 int tgt_txn_stop_cb(const struct lu_env *env, struct thandle *th,
1840                     void *cookie)
1841 {
1842         struct lu_target        *tgt = cookie;
1843         struct tgt_session_info *tsi;
1844         struct tgt_thread_info  *tti = tgt_th_info(env);
1845         struct dt_object        *obj = NULL;
1846         int                      rc;
1847         bool                     echo_client;
1848
1849         if (env->le_ses == NULL)
1850                 return 0;
1851
1852         tsi = tgt_ses_info(env);
1853         /* OFD may start transaction without export assigned */
1854         if (tsi->tsi_exp == NULL)
1855                 return 0;
1856
1857         echo_client = (tgt_ses_req(tsi) == NULL && tsi->tsi_xid == 0);
1858
1859         if (tti->tti_has_trans && !echo_client) {
1860                 if (tti->tti_mult_trans == 0) {
1861                         CDEBUG(D_HA, "More than one transaction %llu\n",
1862                                tti->tti_transno);
1863                         RETURN(0);
1864                 }
1865                 /* we need another transno to be assigned */
1866                 tti->tti_transno = 0;
1867         } else if (th->th_result == 0) {
1868                 tti->tti_has_trans = 1;
1869         }
1870
1871         if (tsi->tsi_vbr_obj != NULL &&
1872             !lu_object_remote(&tsi->tsi_vbr_obj->do_lu)) {
1873                 obj = tsi->tsi_vbr_obj;
1874         }
1875
1876         if (unlikely(echo_client)) /* echo client special case */
1877                 rc = tgt_last_rcvd_update_echo(env, tgt, obj, th,
1878                                                tsi->tsi_exp);
1879         else
1880                 rc = tgt_last_rcvd_update(env, tgt, obj, tsi->tsi_opdata, th,
1881                                           tgt_ses_req(tsi));
1882         return rc;
1883 }
1884
1885 int tgt_reply_data_init(const struct lu_env *env, struct lu_target *tgt)
1886 {
1887         struct tgt_thread_info  *tti = tgt_th_info(env);
1888         struct lsd_reply_data   *lrd = &tti->tti_lrd;
1889         unsigned long            reply_data_size;
1890         int                      rc;
1891         struct lsd_reply_header *lrh = NULL;
1892         struct lsd_client_data  *lcd = NULL;
1893         struct tg_reply_data    *trd = NULL;
1894         int                      idx;
1895         loff_t                   off;
1896         struct cfs_hash         *hash = NULL;
1897         struct obd_export       *exp;
1898         struct tg_export_data   *ted;
1899         int                      reply_data_recovered = 0;
1900
1901         rc = dt_attr_get(env, tgt->lut_reply_data, &tti->tti_attr);
1902         if (rc)
1903                 GOTO(out, rc);
1904         reply_data_size = (unsigned long)tti->tti_attr.la_size;
1905
1906         OBD_ALLOC_PTR(lrh);
1907         if (lrh == NULL)
1908                 GOTO(out, rc = -ENOMEM);
1909
1910         if (reply_data_size == 0) {
1911                 CDEBUG(D_INFO, "%s: new reply_data file, initializing\n",
1912                        tgt_name(tgt));
1913                 lrh->lrh_magic = LRH_MAGIC;
1914                 lrh->lrh_header_size = sizeof(struct lsd_reply_header);
1915                 lrh->lrh_reply_size = sizeof(struct lsd_reply_data);
1916                 rc = tgt_reply_header_write(env, tgt, lrh);
1917                 if (rc) {
1918                         CERROR("%s: error writing %s: rc = %d\n",
1919                                tgt_name(tgt), REPLY_DATA, rc);
1920                         GOTO(out, rc);
1921                 }
1922         } else {
1923                 rc = tgt_reply_header_read(env, tgt, lrh);
1924                 if (rc) {
1925                         CERROR("%s: error reading %s: rc = %d\n",
1926                                tgt_name(tgt), REPLY_DATA, rc);
1927                         GOTO(out, rc);
1928                 }
1929                 if (lrh->lrh_magic != LRH_MAGIC ||
1930                     lrh->lrh_header_size != sizeof(struct lsd_reply_header) ||
1931                     lrh->lrh_reply_size != sizeof(struct lsd_reply_data)) {
1932                         CERROR("%s: invalid header in %s\n",
1933                                tgt_name(tgt), REPLY_DATA);
1934                         GOTO(out, rc = -EINVAL);
1935                 }
1936
1937                 hash = cfs_hash_getref(tgt->lut_obd->obd_gen_hash);
1938                 if (hash == NULL)
1939                         GOTO(out, rc = -ENODEV);
1940
1941                 OBD_ALLOC_PTR(lcd);
1942                 if (lcd == NULL)
1943                         GOTO(out, rc = -ENOMEM);
1944
1945                 OBD_ALLOC_PTR(trd);
1946                 if (trd == NULL)
1947                         GOTO(out, rc = -ENOMEM);
1948
1949                 /* Load reply_data from disk */
1950                 for (idx = 0, off = sizeof(struct lsd_reply_header);
1951                      off < reply_data_size;
1952                      idx++, off += sizeof(struct lsd_reply_data)) {
1953                         rc = tgt_reply_data_read(env, tgt, lrd, off);
1954                         if (rc) {
1955                                 CERROR("%s: error reading %s: rc = %d\n",
1956                                        tgt_name(tgt), REPLY_DATA, rc);
1957                                 GOTO(out, rc);
1958                         }
1959
1960                         exp = cfs_hash_lookup(hash, &lrd->lrd_client_gen);
1961                         if (exp == NULL) {
1962                                 /* old reply data from a disconnected client */
1963                                 continue;
1964                         }
1965                         ted = &exp->exp_target_data;
1966                         mutex_lock(&ted->ted_lcd_lock);
1967
1968                         /* create in-memory reply_data and link it to
1969                          * target export's reply list */
1970                         rc = tgt_set_reply_slot(tgt, idx);
1971                         if (rc != 0) {
1972                                 mutex_unlock(&ted->ted_lcd_lock);
1973                                 GOTO(out, rc);
1974                         }
1975                         trd->trd_reply = *lrd;
1976                         trd->trd_pre_versions[0] = 0;
1977                         trd->trd_pre_versions[1] = 0;
1978                         trd->trd_pre_versions[2] = 0;
1979                         trd->trd_pre_versions[3] = 0;
1980                         trd->trd_index = idx;
1981                         trd->trd_tag = 0;
1982                         list_add(&trd->trd_list, &ted->ted_reply_list);
1983                         ted->ted_reply_cnt++;
1984                         if (ted->ted_reply_cnt > ted->ted_reply_max)
1985                                 ted->ted_reply_max = ted->ted_reply_cnt;
1986
1987                         CDEBUG(D_HA, "%s: restore reply %p: xid %llu, "
1988                                "transno %llu, client gen %u, slot idx %d\n",
1989                                tgt_name(tgt), trd, lrd->lrd_xid,
1990                                lrd->lrd_transno, lrd->lrd_client_gen,
1991                                trd->trd_index);
1992
1993                         /* update export last committed transation */
1994                         exp->exp_last_committed = max(exp->exp_last_committed,
1995                                                       lrd->lrd_transno);
1996
1997                         mutex_unlock(&ted->ted_lcd_lock);
1998                         class_export_put(exp);
1999
2000                         /* update target last committed transaction */
2001                         spin_lock(&tgt->lut_translock);
2002                         tgt->lut_last_transno = max(tgt->lut_last_transno,
2003                                                     lrd->lrd_transno);
2004                         spin_unlock(&tgt->lut_translock);
2005
2006                         reply_data_recovered++;
2007
2008                         OBD_ALLOC_PTR(trd);
2009                         if (trd == NULL)
2010                                 GOTO(out, rc = -ENOMEM);
2011                 }
2012                 CDEBUG(D_INFO, "%s: %d reply data have been recovered\n",
2013                        tgt_name(tgt), reply_data_recovered);
2014         }
2015
2016         spin_lock(&tgt->lut_translock);
2017         /* obd_last_committed is used for compatibility
2018          * with other lustre recovery code */
2019         tgt->lut_obd->obd_last_committed = tgt->lut_last_transno;
2020         spin_unlock(&tgt->lut_translock);
2021
2022         rc = 0;
2023
2024 out:
2025         if (hash != NULL)
2026                 cfs_hash_putref(hash);
2027         if (lcd != NULL)
2028                 OBD_FREE_PTR(lcd);
2029         if (trd != NULL)
2030                 OBD_FREE_PTR(trd);
2031         if (lrh != NULL)
2032                 OBD_FREE_PTR(lrh);
2033         return rc;
2034 }
2035
2036 struct tg_reply_data *tgt_lookup_reply_by_xid(struct tg_export_data *ted,
2037                                               __u64 xid)
2038 {
2039         struct tg_reply_data    *found = NULL;
2040         struct tg_reply_data    *reply;
2041
2042         mutex_lock(&ted->ted_lcd_lock);
2043         list_for_each_entry(reply, &ted->ted_reply_list, trd_list) {
2044                 if (reply->trd_reply.lrd_xid == xid) {
2045                         found = reply;
2046                         break;
2047                 }
2048         }
2049         mutex_unlock(&ted->ted_lcd_lock);
2050         return found;
2051 }
2052 EXPORT_SYMBOL(tgt_lookup_reply_by_xid);
2053
2054 /* Look for a reply data matching specified request @req
2055  * A copy is returned in @trd if the pointer is not NULL
2056  */
2057 bool tgt_lookup_reply(struct ptlrpc_request *req, struct tg_reply_data *trd)
2058 {
2059         struct tg_export_data   *ted = &req->rq_export->exp_target_data;
2060         struct tg_reply_data    *reply;
2061         bool                     found = false;
2062
2063         reply = tgt_lookup_reply_by_xid(ted, req->rq_xid);
2064         if (reply != NULL) {
2065                 found = true;
2066                 if (trd != NULL)
2067                         *trd = *reply;
2068         }
2069
2070         CDEBUG(D_TRACE, "%s: lookup reply xid %llu, found %d\n",
2071                tgt_name(class_exp2tgt(req->rq_export)), req->rq_xid,
2072                found ? 1 : 0);
2073
2074         return found;
2075 }
2076 EXPORT_SYMBOL(tgt_lookup_reply);
2077
2078 int tgt_handle_received_xid(struct obd_export *exp, __u64 rcvd_xid)
2079 {
2080         struct tg_export_data   *ted = &exp->exp_target_data;
2081         struct lu_target        *lut = class_exp2tgt(exp);
2082         struct tg_reply_data    *trd, *tmp;
2083
2084         mutex_lock(&ted->ted_lcd_lock);
2085         list_for_each_entry_safe(trd, tmp, &ted->ted_reply_list, trd_list) {
2086                 if (trd->trd_reply.lrd_xid > rcvd_xid)
2087                         continue;
2088                 ted->ted_release_xid++;
2089                 tgt_release_reply_data(lut, ted, trd);
2090         }
2091         mutex_unlock(&ted->ted_lcd_lock);
2092
2093         return 0;
2094 }
2095
2096 int tgt_handle_tag(struct obd_export *exp, __u16 tag)
2097 {
2098         struct tg_export_data   *ted = &exp->exp_target_data;
2099         struct lu_target        *lut = class_exp2tgt(exp);
2100         struct tg_reply_data    *trd, *tmp;
2101
2102         if (tag == 0)
2103                 return 0;
2104
2105         mutex_lock(&ted->ted_lcd_lock);
2106         list_for_each_entry_safe(trd, tmp, &ted->ted_reply_list, trd_list) {
2107                 if (trd->trd_tag != tag)
2108                         continue;
2109                 ted->ted_release_tag++;
2110                 tgt_release_reply_data(lut, ted, trd);
2111                 break;
2112         }
2113         mutex_unlock(&ted->ted_lcd_lock);
2114
2115         return 0;
2116 }
2117