1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/mdt/mdt_recovery.c
38 * Lustre Metadata Target (mdt) recovery-related methods
40 * Author: Huang Hua <huanghua@clusterfs.com>
41 * Author: Pershin Mike <tappro@clusterfs.com>
45 # define EXPORT_SYMTAB
47 #define DEBUG_SUBSYSTEM S_MDS
49 #include "mdt_internal.h"
51 static int mdt_server_data_update(const struct lu_env *env,
52 struct mdt_device *mdt);
54 struct lu_buf *mdt_buf(const struct lu_env *env, void *area, ssize_t len)
57 struct mdt_thread_info *mti;
59 mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
66 const struct lu_buf *mdt_buf_const(const struct lu_env *env,
67 const void *area, ssize_t len)
70 struct mdt_thread_info *mti;
72 mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
75 buf->lb_buf = (void *)area;
80 int mdt_record_read(const struct lu_env *env,
81 struct dt_object *dt, struct lu_buf *buf, loff_t *pos)
85 LASSERTF(dt != NULL, "dt is NULL when we want to read record\n");
87 rc = dt->do_body_ops->dbo_read(env, dt, buf, pos, BYPASS_CAPA);
89 if (rc == buf->lb_len)
96 int mdt_record_write(const struct lu_env *env,
97 struct dt_object *dt, const struct lu_buf *buf,
98 loff_t *pos, struct thandle *th)
102 LASSERTF(dt != NULL, "dt is NULL when we want to write record\n");
104 rc = dt->do_body_ops->dbo_write(env, dt, buf, pos, th, BYPASS_CAPA, 1);
105 if (rc == buf->lb_len)
112 static inline int mdt_trans_credit_get(const struct lu_env *env,
113 struct mdt_device *mdt,
116 struct dt_device *dev = mdt->mdt_bottom;
119 case MDT_TXN_CAPA_KEYS_WRITE_OP:
120 case MDT_TXN_LAST_RCVD_WRITE_OP:
121 cr = dev->dd_ops->dt_credit_get(env,
131 void mdt_trans_credit_init(const struct lu_env *env,
132 struct mdt_device *mdt,
135 struct mdt_thread_info *mti;
139 mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
140 p = &mti->mti_txn_param;
142 cr = mdt_trans_credit_get(env, mdt, op);
143 txn_param_init(p, cr);
146 struct thandle* mdt_trans_start(const struct lu_env *env,
147 struct mdt_device *mdt)
149 struct mdt_thread_info *mti;
152 mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
153 p = &mti->mti_txn_param;
155 /* export can require sync operations */
156 if (mti->mti_exp != NULL)
157 p->tp_sync = mti->mti_exp->exp_need_sync;
159 return mdt->mdt_bottom->dd_ops->dt_trans_start(env, mdt->mdt_bottom, p);
162 void mdt_trans_stop(const struct lu_env *env,
163 struct mdt_device *mdt, struct thandle *th)
165 mdt->mdt_bottom->dd_ops->dt_trans_stop(env, th);
168 /* last_rcvd handling */
169 static inline void lsd_le_to_cpu(struct lr_server_data *buf,
170 struct lr_server_data *lsd)
172 memcpy(lsd->lsd_uuid, buf->lsd_uuid, sizeof (lsd->lsd_uuid));
173 lsd->lsd_last_transno = le64_to_cpu(buf->lsd_last_transno);
174 lsd->lsd_mount_count = le64_to_cpu(buf->lsd_mount_count);
175 lsd->lsd_feature_compat = le32_to_cpu(buf->lsd_feature_compat);
176 lsd->lsd_feature_rocompat = le32_to_cpu(buf->lsd_feature_rocompat);
177 lsd->lsd_feature_incompat = le32_to_cpu(buf->lsd_feature_incompat);
178 lsd->lsd_server_size = le32_to_cpu(buf->lsd_server_size);
179 lsd->lsd_client_start = le32_to_cpu(buf->lsd_client_start);
180 lsd->lsd_client_size = le16_to_cpu(buf->lsd_client_size);
183 static inline void lsd_cpu_to_le(struct lr_server_data *lsd,
184 struct lr_server_data *buf)
186 memcpy(buf->lsd_uuid, lsd->lsd_uuid, sizeof (lsd->lsd_uuid));
187 buf->lsd_last_transno = cpu_to_le64(lsd->lsd_last_transno);
188 buf->lsd_mount_count = cpu_to_le64(lsd->lsd_mount_count);
189 buf->lsd_feature_compat = cpu_to_le32(lsd->lsd_feature_compat);
190 buf->lsd_feature_rocompat = cpu_to_le32(lsd->lsd_feature_rocompat);
191 buf->lsd_feature_incompat = cpu_to_le32(lsd->lsd_feature_incompat);
192 buf->lsd_server_size = cpu_to_le32(lsd->lsd_server_size);
193 buf->lsd_client_start = cpu_to_le32(lsd->lsd_client_start);
194 buf->lsd_client_size = cpu_to_le16(lsd->lsd_client_size);
197 static inline void lcd_le_to_cpu(struct lsd_client_data *buf,
198 struct lsd_client_data *lcd)
200 memcpy(lcd->lcd_uuid, buf->lcd_uuid, sizeof (lcd->lcd_uuid));
201 lcd->lcd_last_transno = le64_to_cpu(buf->lcd_last_transno);
202 lcd->lcd_last_xid = le64_to_cpu(buf->lcd_last_xid);
203 lcd->lcd_last_result = le32_to_cpu(buf->lcd_last_result);
204 lcd->lcd_last_data = le32_to_cpu(buf->lcd_last_data);
205 lcd->lcd_last_close_transno = le64_to_cpu(buf->lcd_last_close_transno);
206 lcd->lcd_last_close_xid = le64_to_cpu(buf->lcd_last_close_xid);
207 lcd->lcd_last_close_result = le32_to_cpu(buf->lcd_last_close_result);
210 static inline void lcd_cpu_to_le(struct lsd_client_data *lcd,
211 struct lsd_client_data *buf)
213 memcpy(buf->lcd_uuid, lcd->lcd_uuid, sizeof (lcd->lcd_uuid));
214 buf->lcd_last_transno = cpu_to_le64(lcd->lcd_last_transno);
215 buf->lcd_last_xid = cpu_to_le64(lcd->lcd_last_xid);
216 buf->lcd_last_result = cpu_to_le32(lcd->lcd_last_result);
217 buf->lcd_last_data = cpu_to_le32(lcd->lcd_last_data);
218 buf->lcd_last_close_transno = cpu_to_le64(lcd->lcd_last_close_transno);
219 buf->lcd_last_close_xid = cpu_to_le64(lcd->lcd_last_close_xid);
220 buf->lcd_last_close_result = cpu_to_le32(lcd->lcd_last_close_result);
223 static inline int mdt_last_rcvd_header_read(const struct lu_env *env,
224 struct mdt_device *mdt)
226 struct mdt_thread_info *mti;
229 mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
232 rc = mdt_record_read(env, mdt->mdt_last_rcvd,
233 mdt_buf(env, &mti->mti_lsd, sizeof(mti->mti_lsd)),
236 lsd_le_to_cpu(&mti->mti_lsd, &mdt->mdt_lsd);
238 CDEBUG(D_INFO, "read last_rcvd header rc = %d:\n"
240 "last_transno = "LPU64"\n",
241 rc, mdt->mdt_lsd.lsd_uuid,
242 mdt->mdt_lsd.lsd_last_transno);
246 static inline int mdt_last_rcvd_header_write(const struct lu_env *env,
247 struct mdt_device *mdt)
249 struct mdt_thread_info *mti;
254 mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
256 mdt_trans_credit_init(env, mdt, MDT_TXN_LAST_RCVD_WRITE_OP);
257 th = mdt_trans_start(env, mdt);
262 lsd_cpu_to_le(&mdt->mdt_lsd, &mti->mti_lsd);
264 rc = mdt_record_write(env, mdt->mdt_last_rcvd,
265 mdt_buf_const(env, &mti->mti_lsd,
266 sizeof(mti->mti_lsd)),
269 mdt_trans_stop(env, mdt, th);
271 CDEBUG(D_INFO, "write last_rcvd header rc = %d:\n"
272 "uuid = %s\nlast_transno = "LPU64"\n",
273 rc, mdt->mdt_lsd.lsd_uuid, mdt->mdt_lsd.lsd_last_transno);
278 static int mdt_last_rcvd_read(const struct lu_env *env,
279 struct mdt_device *mdt,
280 struct lsd_client_data *lcd, loff_t *off)
282 struct mdt_thread_info *mti;
283 struct lsd_client_data *tmp;
286 mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
288 rc = mdt_record_read(env, mdt->mdt_last_rcvd,
289 mdt_buf(env, tmp, sizeof(*tmp)), off);
291 lcd_le_to_cpu(tmp, lcd);
293 CDEBUG(D_INFO, "read lcd @%d rc = %d:\n"
295 "last_transno = "LPU64"\n"
296 "last_xid = "LPU64"\n"
299 "last_close_transno = "LPU64"\n"
300 "last_close_xid = "LPU64"\n"
301 "last_close_result = %u\n",
302 (int)(*off - sizeof(*tmp)),
305 lcd->lcd_last_transno,
307 lcd->lcd_last_result,
309 lcd->lcd_last_close_transno,
310 lcd->lcd_last_close_xid,
311 lcd->lcd_last_close_result);
315 static int mdt_last_rcvd_write(const struct lu_env *env,
316 struct mdt_device *mdt,
317 struct lsd_client_data *lcd,
318 loff_t *off, struct thandle *th)
320 struct mdt_thread_info *mti;
321 struct lsd_client_data *tmp;
325 mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
328 lcd_cpu_to_le(lcd, tmp);
330 rc = mdt_record_write(env, mdt->mdt_last_rcvd,
331 mdt_buf_const(env, tmp, sizeof(*tmp)), off, th);
333 CDEBUG(D_INFO, "write lcd @%d rc = %d:\n"
335 "last_transno = "LPU64"\n"
336 "last_xid = "LPU64"\n"
339 "last_close_transno = "LPU64"\n"
340 "last_close_xid = "LPU64"\n"
341 "last_close_result = %u\n",
342 (int)(*off - sizeof(*tmp)),
345 lcd->lcd_last_transno,
347 lcd->lcd_last_result,
349 lcd->lcd_last_close_transno,
350 lcd->lcd_last_close_xid,
351 lcd->lcd_last_close_result);
355 static int mdt_clients_data_init(const struct lu_env *env,
356 struct mdt_device *mdt,
357 unsigned long last_size)
359 struct lr_server_data *lsd = &mdt->mdt_lsd;
360 struct lsd_client_data *lcd = NULL;
361 struct obd_device *obd = mdt2obd_dev(mdt);
367 /* When we do a clean MDS shutdown, we save the last_transno into
368 * the header. If we find clients with higher last_transno values
369 * then those clients may need recovery done. */
370 LASSERT(atomic_read(&obd->obd_req_replay_clients) == 0);
371 for (cl_idx = 0, off = lsd->lsd_client_start;
372 off < last_size; cl_idx++) {
374 struct obd_export *exp;
382 off = lsd->lsd_client_start +
383 cl_idx * lsd->lsd_client_size;
385 rc = mdt_last_rcvd_read(env, mdt, lcd, &off);
387 CERROR("error reading MDS %s idx %d, off %llu: rc %d\n",
388 LAST_RCVD, cl_idx, off, rc);
390 break; /* read error shouldn't cause startup to fail */
393 if (lcd->lcd_uuid[0] == '\0') {
394 CDEBUG(D_INFO, "skipping zeroed client at offset %d\n",
399 last_transno = lcd_last_transno(lcd);
401 /* These exports are cleaned up by mdt_obd_disconnect(), so
402 * they need to be set up like real exports as
403 * mdt_obd_connect() does.
405 CDEBUG(D_HA, "RCVRNG CLIENT uuid: %s idx: %d lr: "LPU64
406 " srv lr: "LPU64" lx: "LPU64"\n", lcd->lcd_uuid, cl_idx,
407 last_transno, lsd->lsd_last_transno,
410 exp = class_new_export(obd, (struct obd_uuid *)lcd->lcd_uuid);
412 if (PTR_ERR(exp) == -EALREADY) {
413 /* export already exists, zero out this one */
414 lcd->lcd_uuid[0] = '\0';
416 GOTO(err_client, rc = PTR_ERR(exp));
418 struct mdt_thread_info *mti;
419 mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
420 LASSERT(mti != NULL);
422 exp->exp_mdt_data.med_lcd = lcd;
423 rc = mdt_client_add(env, mdt, cl_idx);
424 /* can't fail existing */
425 LASSERTF(rc == 0, "rc = %d\n", rc);
427 spin_lock(&exp->exp_lock);
428 exp->exp_connecting = 0;
429 exp->exp_in_recovery = 0;
430 spin_unlock(&exp->exp_lock);
431 obd->obd_max_recoverable_clients++;
432 class_export_put(exp);
435 CDEBUG(D_OTHER, "client at idx %d has last_transno="LPU64"\n",
436 cl_idx, last_transno);
437 /* protect __u64 value update */
438 spin_lock(&mdt->mdt_transno_lock);
439 mdt->mdt_last_transno = max(last_transno,
440 mdt->mdt_last_transno);
441 spin_unlock(&mdt->mdt_transno_lock);
450 static int mdt_server_data_init(const struct lu_env *env,
451 struct mdt_device *mdt,
452 struct lustre_sb_info *lsi)
454 struct lr_server_data *lsd = &mdt->mdt_lsd;
455 struct lsd_client_data *lcd = NULL;
456 struct obd_device *obd = mdt2obd_dev(mdt);
457 struct mdt_thread_info *mti;
458 struct dt_object *obj;
460 struct lustre_disk_data *ldd;
461 unsigned long last_rcvd_size;
466 /* ensure padding in the struct is the correct size */
467 CLASSERT(offsetof(struct lr_server_data, lsd_padding) +
468 sizeof(lsd->lsd_padding) == LR_SERVER_SIZE);
469 CLASSERT(offsetof(struct lsd_client_data, lcd_padding) +
470 sizeof(lcd->lcd_padding) == LR_CLIENT_SIZE);
472 mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
473 LASSERT(mti != NULL);
474 la = &mti->mti_attr.ma_attr;
476 obj = mdt->mdt_last_rcvd;
477 rc = obj->do_ops->do_attr_get(env, mdt->mdt_last_rcvd, la, BYPASS_CAPA);
481 last_rcvd_size = (unsigned long)la->la_size;
483 if (last_rcvd_size == 0) {
484 LCONSOLE_WARN("%s: new disk, initializing\n", obd->obd_name);
486 memcpy(lsd->lsd_uuid, obd->obd_uuid.uuid,
487 sizeof(lsd->lsd_uuid));
488 lsd->lsd_last_transno = 0;
489 lsd->lsd_mount_count = 0;
490 lsd->lsd_server_size = LR_SERVER_SIZE;
491 lsd->lsd_client_start = LR_CLIENT_START;
492 lsd->lsd_client_size = LR_CLIENT_SIZE;
493 lsd->lsd_feature_rocompat = OBD_ROCOMPAT_LOVOBJID;
494 lsd->lsd_feature_incompat = OBD_INCOMPAT_MDT |
495 OBD_INCOMPAT_COMMON_LR;
497 LCONSOLE_WARN("%s: used disk, loading\n", obd->obd_name);
498 rc = mdt_last_rcvd_header_read(env, mdt);
500 CERROR("error reading MDS %s: rc %d\n", LAST_RCVD, rc);
503 if (strcmp(lsd->lsd_uuid, obd->obd_uuid.uuid) != 0) {
504 LCONSOLE_ERROR_MSG(0x157, "Trying to start OBD %s using"
505 "the wrong disk %s. Were the /dev/ "
506 "assignments rearranged?\n",
507 obd->obd_uuid.uuid, lsd->lsd_uuid);
508 GOTO(out, rc = -EINVAL);
511 mount_count = lsd->lsd_mount_count;
515 if (ldd->ldd_flags & LDD_F_IAM_DIR)
516 lsd->lsd_feature_incompat |= OBD_INCOMPAT_IAM_DIR;
518 lsd->lsd_feature_compat = OBD_COMPAT_MDT;
519 lsd->lsd_feature_incompat |= OBD_INCOMPAT_FID;
521 spin_lock(&mdt->mdt_transno_lock);
522 mdt->mdt_last_transno = lsd->lsd_last_transno;
523 spin_unlock(&mdt->mdt_transno_lock);
525 CDEBUG(D_INODE, "========BEGIN DUMPING LAST_RCVD========\n");
526 CDEBUG(D_INODE, "%s: server last_transno: "LPU64"\n",
527 obd->obd_name, mdt->mdt_last_transno);
528 CDEBUG(D_INODE, "%s: server mount_count: "LPU64"\n",
529 obd->obd_name, mount_count + 1);
530 CDEBUG(D_INODE, "%s: server data size: %u\n",
531 obd->obd_name, lsd->lsd_server_size);
532 CDEBUG(D_INODE, "%s: per-client data start: %u\n",
533 obd->obd_name, lsd->lsd_client_start);
534 CDEBUG(D_INODE, "%s: per-client data size: %u\n",
535 obd->obd_name, lsd->lsd_client_size);
536 CDEBUG(D_INODE, "%s: last_rcvd size: %lu\n",
537 obd->obd_name, last_rcvd_size);
538 CDEBUG(D_INODE, "%s: last_rcvd clients: %lu\n", obd->obd_name,
539 last_rcvd_size <= lsd->lsd_client_start ? 0 :
540 (last_rcvd_size - lsd->lsd_client_start) /
541 lsd->lsd_client_size);
542 CDEBUG(D_INODE, "========END DUMPING LAST_RCVD========\n");
544 if (!lsd->lsd_server_size || !lsd->lsd_client_start ||
545 !lsd->lsd_client_size) {
546 CERROR("Bad last_rcvd contents!\n");
547 GOTO(out, rc = -EINVAL);
550 rc = mdt_clients_data_init(env, mdt, last_rcvd_size);
552 GOTO(err_client, rc);
554 spin_lock(&mdt->mdt_transno_lock);
555 /* obd_last_committed is used for compatibility
556 * with other lustre recovery code */
557 obd->obd_last_committed = mdt->mdt_last_transno;
558 spin_unlock(&mdt->mdt_transno_lock);
560 mdt->mdt_mount_count++;
561 lsd->lsd_mount_count = mdt->mdt_mount_count;
563 /* save it, so mount count and last_transno is current */
564 rc = mdt_server_data_update(env, mdt);
566 GOTO(err_client, rc);
571 target_recovery_fini(obd);
576 static int mdt_server_data_update(const struct lu_env *env,
577 struct mdt_device *mdt)
582 CDEBUG(D_SUPER, "MDS mount_count is "LPU64", last_transno is "LPU64"\n",
583 mdt->mdt_mount_count, mdt->mdt_last_transno);
585 spin_lock(&mdt->mdt_transno_lock);
586 mdt->mdt_lsd.lsd_last_transno = mdt->mdt_last_transno;
587 spin_unlock(&mdt->mdt_transno_lock);
590 * This may be called from difficult reply handler and
591 * mdt->mdt_last_rcvd may be NULL that time.
593 if (mdt->mdt_last_rcvd != NULL)
594 rc = mdt_last_rcvd_header_write(env, mdt);
598 void mdt_cb_new_client(const struct mdt_device *mdt, __u64 transno,
601 struct obd_device *obd = mdt2obd_dev(mdt);
603 target_client_add_cb(obd, transno, data, err);
606 int mdt_client_new(const struct lu_env *env, struct mdt_device *mdt)
608 unsigned long *bitmap = mdt->mdt_client_bitmap;
609 struct mdt_thread_info *mti;
610 struct mdt_export_data *med;
611 struct lsd_client_data *lcd;
612 struct lr_server_data *lsd = &mdt->mdt_lsd;
613 struct obd_device *obd = mdt2obd_dev(mdt);
620 mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
621 LASSERT(mti != NULL);
623 med = &mti->mti_exp->exp_mdt_data;
626 LASSERT(bitmap != NULL);
627 if (!strcmp(med->med_lcd->lcd_uuid, obd->obd_uuid.uuid))
630 /* the bitmap operations can handle cl_idx > sizeof(long) * 8, so
631 * there's no need for extra complication here
633 spin_lock(&mdt->mdt_client_bitmap_lock);
634 cl_idx = find_first_zero_bit(bitmap, LR_MAX_CLIENTS);
635 if (cl_idx >= LR_MAX_CLIENTS ||
636 OBD_FAIL_CHECK(OBD_FAIL_MDS_CLIENT_ADD)) {
637 CERROR("no room for %u clients - fix LR_MAX_CLIENTS\n",
639 spin_unlock(&mdt->mdt_client_bitmap_lock);
642 set_bit(cl_idx, bitmap);
643 spin_unlock(&mdt->mdt_client_bitmap_lock);
645 CDEBUG(D_INFO, "client at idx %d with UUID '%s' added\n",
646 cl_idx, med->med_lcd->lcd_uuid);
648 med->med_lr_idx = cl_idx;
649 med->med_lr_off = lsd->lsd_client_start +
650 (cl_idx * lsd->lsd_client_size);
651 init_mutex(&med->med_lcd_lock);
653 LASSERTF(med->med_lr_off > 0, "med_lr_off = %llu\n", med->med_lr_off);
654 /* write new client data */
655 off = med->med_lr_off;
656 mdt_trans_credit_init(env, mdt, MDT_TXN_LAST_RCVD_WRITE_OP);
657 th = mdt_trans_start(env, mdt);
661 /* until this operations will be committed the sync is needed for this
663 mdt_trans_add_cb(th, mdt_cb_new_client, mti->mti_exp);
664 spin_lock(&mti->mti_exp->exp_lock);
665 mti->mti_exp->exp_need_sync = 1;
666 spin_unlock(&mti->mti_exp->exp_lock);
668 rc = mdt_last_rcvd_write(env, mdt, lcd, &off, th);
669 CDEBUG(D_INFO, "wrote client lcd at idx %u off %llu (len "LPSZ")\n",
670 cl_idx, med->med_lr_off, sizeof(*lcd));
671 mdt_trans_stop(env, mdt, th);
676 /* Add client data to the MDS. We use a bitmap to locate a free space
677 * in the last_rcvd file if cl_off is -1 (i.e. a new client).
678 * Otherwise, we just have to read the data from the last_rcvd file and
679 * we know its offset.
681 * It should not be possible to fail adding an existing client - otherwise
682 * mdt_init_server_data() callsite needs to be fixed.
684 int mdt_client_add(const struct lu_env *env,
685 struct mdt_device *mdt, int cl_idx)
687 struct mdt_thread_info *mti;
688 struct mdt_export_data *med;
689 unsigned long *bitmap = mdt->mdt_client_bitmap;
690 struct obd_device *obd = mdt2obd_dev(mdt);
691 struct lr_server_data *lsd = &mdt->mdt_lsd;
695 mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
696 LASSERT(mti != NULL);
698 med = &mti->mti_exp->exp_mdt_data;
700 LASSERT(bitmap != NULL);
701 LASSERTF(cl_idx >= 0, "%d\n", cl_idx);
703 if (!strcmp(med->med_lcd->lcd_uuid, obd->obd_uuid.uuid))
706 spin_lock(&mdt->mdt_client_bitmap_lock);
707 if (test_and_set_bit(cl_idx, bitmap)) {
708 CERROR("MDS client %d: bit already set in bitmap!!\n",
712 spin_unlock(&mdt->mdt_client_bitmap_lock);
714 CDEBUG(D_INFO, "client at idx %d with UUID '%s' added\n",
715 cl_idx, med->med_lcd->lcd_uuid);
717 med->med_lr_idx = cl_idx;
718 med->med_lr_off = lsd->lsd_client_start +
719 (cl_idx * lsd->lsd_client_size);
720 init_mutex(&med->med_lcd_lock);
722 LASSERTF(med->med_lr_off > 0, "med_lr_off = %llu\n", med->med_lr_off);
727 int mdt_client_del(const struct lu_env *env, struct mdt_device *mdt)
729 struct mdt_thread_info *mti;
730 struct mdt_export_data *med;
731 struct lsd_client_data *lcd;
732 struct obd_device *obd = mdt2obd_dev(mdt);
738 mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
739 LASSERT(mti != NULL);
741 med = &mti->mti_exp->exp_mdt_data;
746 /* XXX: If lcd_uuid were a real obd_uuid, I could use obd_uuid_equals */
747 if (!strcmp(med->med_lcd->lcd_uuid, obd->obd_uuid.uuid))
750 CDEBUG(D_INFO, "freeing client at idx %u, offset %lld\n",
751 med->med_lr_idx, med->med_lr_off);
753 off = med->med_lr_off;
756 * Don't clear med_lr_idx here as it is likely also unset. At worst we
757 * leak a client slot that will be cleaned on the next recovery.
760 CERROR("client idx %d has offset %lld\n",
761 med->med_lr_idx, off);
762 GOTO(free, rc = -EINVAL);
766 * Clear the bit _after_ zeroing out the client so we don't race with
767 * mdt_client_add and zero out new clients.
769 if (!test_bit(med->med_lr_idx, mdt->mdt_client_bitmap)) {
770 CERROR("MDT client %u: bit already clear in bitmap!!\n",
776 * This may be called from difficult reply handler path and
777 * mdt->mdt_last_rcvd may be NULL that time.
779 if (mdt->mdt_last_rcvd != NULL) {
780 mdt_trans_credit_init(env, mdt, MDT_TXN_LAST_RCVD_WRITE_OP);
781 th = mdt_trans_start(env, mdt);
783 GOTO(free, rc = PTR_ERR(th));
785 mutex_down(&med->med_lcd_lock);
786 memset(lcd, 0, sizeof *lcd);
788 rc = mdt_last_rcvd_write(env, mdt, lcd, &off, th);
789 mutex_up(&med->med_lcd_lock);
790 mdt_trans_stop(env, mdt, th);
793 CDEBUG(rc == 0 ? D_INFO : D_ERROR, "Zeroing out client idx %u in "
794 "%s rc %d\n", med->med_lr_idx, LAST_RCVD, rc);
796 spin_lock(&mdt->mdt_client_bitmap_lock);
797 clear_bit(med->med_lr_idx, mdt->mdt_client_bitmap);
798 spin_unlock(&mdt->mdt_client_bitmap_lock);
801 * Make sure the server's last_transno is up to date. Do this after the
802 * client is freed so we know all the client's transactions have been
805 mdt_server_data_update(env, mdt);
814 * last_rcvd & last_committed update callbacks
816 static int mdt_last_rcvd_update(struct mdt_thread_info *mti,
819 struct mdt_device *mdt = mti->mti_mdt;
820 struct ptlrpc_request *req = mdt_info_req(mti);
821 struct mdt_export_data *med;
822 struct lsd_client_data *lcd;
825 __s32 rc = th->th_result;
830 LASSERT(req->rq_export);
832 med = &req->rq_export->exp_mdt_data;
835 /* if the export has already been failed, we have no last_rcvd slot */
836 if (req->rq_export->exp_failed) {
837 CWARN("commit transaction for disconnected client %s: rc %d\n",
838 req->rq_export->exp_client_uuid.uuid, rc);
844 off = med->med_lr_off;
845 mutex_down(&med->med_lcd_lock);
846 if (lustre_msg_get_opc(req->rq_reqmsg) == MDS_CLOSE ||
847 lustre_msg_get_opc(req->rq_reqmsg) == MDS_DONE_WRITING) {
848 transno_p = &lcd->lcd_last_close_transno;
849 lcd->lcd_last_close_xid = req->rq_xid;
850 lcd->lcd_last_close_result = rc;
852 transno_p = &lcd->lcd_last_transno;
853 lcd->lcd_last_xid = req->rq_xid;
854 lcd->lcd_last_result = rc;
855 /*XXX: save intent_disposition in mdt_thread_info?
856 * also there is bug - intent_dispostion is __u64,
857 * see struct ldlm_reply->lock_policy_res1; */
858 lcd->lcd_last_data = mti->mti_opdata;
862 * When we store zero transno in lcd we can lost last transno value
863 * because lcd contains 0, but lsd is not yet written
864 * The server data should be updated also if the latest
865 * transno is rewritten by zero. See the bug 11125 for details.
867 if (mti->mti_transno == 0 &&
868 *transno_p == mdt->mdt_last_transno)
869 mdt_server_data_update(mti->mti_env, mdt);
871 *transno_p = mti->mti_transno;
874 CERROR("client idx %d has offset %lld\n", med->med_lr_idx, off);
877 err = mdt_last_rcvd_write(mti->mti_env, mdt, lcd, &off, th);
879 mutex_up(&med->med_lcd_lock);
883 extern struct lu_context_key mdt_thread_key;
885 /* add credits for last_rcvd update */
886 static int mdt_txn_start_cb(const struct lu_env *env,
887 struct txn_param *param, void *cookie)
889 struct mdt_device *mdt = cookie;
891 param->tp_credits += mdt_trans_credit_get(env, mdt,
892 MDT_TXN_LAST_RCVD_WRITE_OP);
896 /* Update last_rcvd records with latests transaction data */
897 static int mdt_txn_stop_cb(const struct lu_env *env,
898 struct thandle *txn, void *cookie)
900 struct mdt_device *mdt = cookie;
901 struct mdt_txn_info *txi;
902 struct mdt_thread_info *mti;
903 struct ptlrpc_request *req;
905 /* transno in two contexts - for commit_cb and for thread */
906 txi = lu_context_key_get(&txn->th_ctx, &mdt_txn_key);
907 mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
908 req = mdt_info_req(mti);
910 if (mti->mti_mdt == NULL || req == NULL || mti->mti_no_need_trans) {
911 txi->txi_transno = 0;
912 mti->mti_no_need_trans = 0;
916 if (mti->mti_has_trans) {
917 /* XXX: currently there are allowed cases, but the wrong cases
918 * are also possible, so better check is needed here */
919 CDEBUG(D_INFO, "More than one transaction "LPU64"\n",
924 mti->mti_has_trans = 1;
925 spin_lock(&mdt->mdt_transno_lock);
926 if (txn->th_result != 0) {
927 if (mti->mti_transno != 0) {
928 CERROR("Replay transno "LPU64" failed: rc %i\n",
929 mti->mti_transno, txn->th_result);
930 mti->mti_transno = 0;
932 } else if (mti->mti_transno == 0) {
933 mti->mti_transno = ++ mdt->mdt_last_transno;
935 /* should be replay */
936 if (mti->mti_transno > mdt->mdt_last_transno)
937 mdt->mdt_last_transno = mti->mti_transno;
940 /* sometimes the reply message has not been successfully packed */
941 LASSERT(req != NULL && req->rq_repmsg != NULL);
943 /* filling reply data */
944 CDEBUG(D_INODE, "transno = %llu, last_committed = %llu\n",
945 mti->mti_transno, req->rq_export->exp_obd->obd_last_committed);
947 req->rq_transno = mti->mti_transno;
948 lustre_msg_set_transno(req->rq_repmsg, mti->mti_transno);
949 lustre_msg_set_last_xid(req->rq_repmsg,
950 lcd_last_xid(req->rq_export->exp_mdt_data.med_lcd));
951 /* save transno for the commit callback */
952 txi->txi_transno = mti->mti_transno;
953 spin_unlock(&mdt->mdt_transno_lock);
955 return mdt_last_rcvd_update(mti, txn);
958 /* commit callback, need to update last_committed value */
959 static int mdt_txn_commit_cb(const struct lu_env *env,
960 struct thandle *txn, void *cookie)
962 struct mdt_device *mdt = cookie;
963 struct obd_device *obd = mdt2obd_dev(mdt);
964 struct mdt_txn_info *txi;
967 txi = lu_context_key_get(&txn->th_ctx, &mdt_txn_key);
969 /* copy of obd_transno_commit_cb() but with locking */
970 spin_lock(&mdt->mdt_transno_lock);
971 if (txi->txi_transno > obd->obd_last_committed) {
972 obd->obd_last_committed = txi->txi_transno;
973 spin_unlock(&mdt->mdt_transno_lock);
974 ptlrpc_commit_replies(obd);
976 spin_unlock(&mdt->mdt_transno_lock);
978 if (txi->txi_transno)
979 CDEBUG(D_HA, "%s: transno "LPD64" is committed\n",
980 obd->obd_name, txi->txi_transno);
982 /* iterate through all additional callbacks */
983 for (i = 0; i < txi->txi_cb_count; i++) {
984 txi->txi_cb[i].mdt_cb_func(mdt, txi->txi_transno,
985 txi->txi_cb[i].mdt_cb_data, 0);
990 int mdt_fs_setup(const struct lu_env *env, struct mdt_device *mdt,
991 struct obd_device *obd,
992 struct lustre_sb_info *lsi)
999 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_FS_SETUP))
1002 /* prepare transactions callbacks */
1003 mdt->mdt_txn_cb.dtc_txn_start = mdt_txn_start_cb;
1004 mdt->mdt_txn_cb.dtc_txn_stop = mdt_txn_stop_cb;
1005 mdt->mdt_txn_cb.dtc_txn_commit = mdt_txn_commit_cb;
1006 mdt->mdt_txn_cb.dtc_cookie = mdt;
1007 CFS_INIT_LIST_HEAD(&mdt->mdt_txn_cb.dtc_linkage);
1009 dt_txn_callback_add(mdt->mdt_bottom, &mdt->mdt_txn_cb);
1011 o = dt_store_open(env, mdt->mdt_bottom, "", LAST_RCVD, &fid);
1013 mdt->mdt_last_rcvd = o;
1014 rc = mdt_server_data_init(env, mdt, lsi);
1016 GOTO(put_last_rcvd, rc);
1019 CERROR("cannot open %s: rc = %d\n", LAST_RCVD, rc);
1023 o = dt_store_open(env, mdt->mdt_bottom, "", CAPA_KEYS, &fid);
1025 mdt->mdt_ck_obj = o;
1026 rc = mdt_capa_keys_init(env, mdt);
1028 GOTO(put_ck_object, rc);
1031 CERROR("cannot open %s: rc = %d\n", CAPA_KEYS, rc);
1032 GOTO(put_last_rcvd, rc);
1037 lu_object_put(env, &o->do_lu);
1038 mdt->mdt_ck_obj = NULL;
1040 lu_object_put(env, &mdt->mdt_last_rcvd->do_lu);
1041 mdt->mdt_last_rcvd = NULL;
1045 void mdt_fs_cleanup(const struct lu_env *env, struct mdt_device *mdt)
1049 /* Remove transaction callback */
1050 dt_txn_callback_del(mdt->mdt_bottom, &mdt->mdt_txn_cb);
1051 if (mdt->mdt_last_rcvd)
1052 lu_object_put(env, &mdt->mdt_last_rcvd->do_lu);
1053 mdt->mdt_last_rcvd = NULL;
1054 if (mdt->mdt_ck_obj)
1055 lu_object_put(env, &mdt->mdt_ck_obj->do_lu);
1056 mdt->mdt_ck_obj = NULL;
1060 /* reconstruction code */
1061 static void mdt_steal_ack_locks(struct ptlrpc_request *req)
1063 struct obd_export *exp = req->rq_export;
1064 struct list_head *tmp;
1065 struct ptlrpc_reply_state *oldrep;
1066 struct ptlrpc_service *svc;
1069 /* CAVEAT EMPTOR: spinlock order */
1070 spin_lock(&exp->exp_lock);
1071 list_for_each (tmp, &exp->exp_outstanding_replies) {
1072 oldrep = list_entry(tmp, struct ptlrpc_reply_state,rs_exp_list);
1074 if (oldrep->rs_xid != req->rq_xid)
1077 if (lustre_msg_get_opc(oldrep->rs_msg) !=
1078 lustre_msg_get_opc(req->rq_reqmsg))
1079 CERROR ("Resent req xid "LPX64" has mismatched opc: "
1080 "new %d old %d\n", req->rq_xid,
1081 lustre_msg_get_opc(req->rq_reqmsg),
1082 lustre_msg_get_opc(oldrep->rs_msg));
1084 svc = oldrep->rs_service;
1085 spin_lock (&svc->srv_lock);
1087 list_del_init (&oldrep->rs_exp_list);
1089 CWARN("Stealing %d locks from rs %p x"LPD64".t"LPD64
1091 oldrep->rs_nlocks, oldrep,
1092 oldrep->rs_xid, oldrep->rs_transno,
1093 lustre_msg_get_opc(oldrep->rs_msg),
1094 libcfs_nid2str(exp->exp_connection->c_peer.nid));
1096 for (i = 0; i < oldrep->rs_nlocks; i++)
1097 ptlrpc_save_lock(req, &oldrep->rs_locks[i],
1098 oldrep->rs_modes[i], 0);
1099 oldrep->rs_nlocks = 0;
1101 DEBUG_REQ(D_HA, req, "stole locks for");
1102 ptlrpc_schedule_difficult_reply (oldrep);
1104 spin_unlock (&svc->srv_lock);
1107 spin_unlock(&exp->exp_lock);
1110 void mdt_req_from_lcd(struct ptlrpc_request *req,
1111 struct lsd_client_data *lcd)
1113 DEBUG_REQ(D_HA, req, "restoring transno "LPD64"/status %d",
1114 lcd->lcd_last_transno, lcd->lcd_last_result);
1116 if (lustre_msg_get_opc(req->rq_reqmsg) == MDS_CLOSE ||
1117 lustre_msg_get_opc(req->rq_repmsg) == MDS_DONE_WRITING) {
1118 req->rq_transno = lcd->lcd_last_close_transno;
1119 req->rq_status = lcd->lcd_last_close_result;
1120 lustre_msg_set_transno(req->rq_repmsg, req->rq_transno);
1121 lustre_msg_set_status(req->rq_repmsg, req->rq_status);
1123 req->rq_transno = lcd->lcd_last_transno;
1124 req->rq_status = lcd->lcd_last_result;
1125 lustre_msg_set_transno(req->rq_repmsg, req->rq_transno);
1126 lustre_msg_set_status(req->rq_repmsg, req->rq_status);
1128 mdt_steal_ack_locks(req);
1131 void mdt_reconstruct_generic(struct mdt_thread_info *mti,
1132 struct mdt_lock_handle *lhc)
1134 struct ptlrpc_request *req = mdt_info_req(mti);
1135 struct mdt_export_data *med = &req->rq_export->exp_mdt_data;
1137 return mdt_req_from_lcd(req, med->med_lcd);
1140 static void mdt_reconstruct_create(struct mdt_thread_info *mti,
1141 struct mdt_lock_handle *lhc)
1143 struct ptlrpc_request *req = mdt_info_req(mti);
1144 struct obd_export *exp = req->rq_export;
1145 struct mdt_export_data *med = &exp->exp_mdt_data;
1146 struct mdt_device *mdt = mti->mti_mdt;
1147 struct mdt_object *child;
1148 struct mdt_body *body;
1151 mdt_req_from_lcd(req, med->med_lcd);
1155 /* if no error, so child was created with requested fid */
1156 child = mdt_object_find(mti->mti_env, mdt, mti->mti_rr.rr_fid2);
1157 if (IS_ERR(child)) {
1158 rc = PTR_ERR(child);
1159 LCONSOLE_WARN("Child "DFID" lookup error %d."
1160 " Evicting client %s with export %s.\n",
1161 PFID(mdt_object_fid(child)), rc,
1162 obd_uuid2str(&exp->exp_client_uuid),
1163 obd_export_nid2str(exp));
1164 mdt_export_evict(exp);
1169 body = req_capsule_server_get(mti->mti_pill, &RMF_MDT_BODY);
1170 mti->mti_attr.ma_need = MA_INODE;
1171 mti->mti_attr.ma_valid = 0;
1172 rc = mo_attr_get(mti->mti_env, mdt_object_child(child), &mti->mti_attr);
1173 if (rc == -EREMOTE) {
1174 /* object was created on remote server */
1175 req->rq_status = rc;
1176 body->valid |= OBD_MD_MDS;
1178 mdt_pack_attr2body(mti, body, &mti->mti_attr.ma_attr,
1179 mdt_object_fid(child));
1180 mdt_object_put(mti->mti_env, child);
1183 static void mdt_reconstruct_setattr(struct mdt_thread_info *mti,
1184 struct mdt_lock_handle *lhc)
1186 struct ptlrpc_request *req = mdt_info_req(mti);
1187 struct obd_export *exp = req->rq_export;
1188 struct mdt_export_data *med = &exp->exp_mdt_data;
1189 struct mdt_device *mdt = mti->mti_mdt;
1190 struct mdt_object *obj;
1191 struct mdt_body *body;
1193 mdt_req_from_lcd(req, med->med_lcd);
1197 body = req_capsule_server_get(mti->mti_pill, &RMF_MDT_BODY);
1198 obj = mdt_object_find(mti->mti_env, mdt, mti->mti_rr.rr_fid1);
1200 int rc = PTR_ERR(obj);
1201 LCONSOLE_WARN(""DFID" lookup error %d."
1202 " Evicting client %s with export %s.\n",
1203 PFID(mdt_object_fid(obj)), rc,
1204 obd_uuid2str(&exp->exp_client_uuid),
1205 obd_export_nid2str(exp));
1206 mdt_export_evict(exp);
1210 mti->mti_attr.ma_need = MA_INODE;
1211 mti->mti_attr.ma_valid = 0;
1212 mo_attr_get(mti->mti_env, mdt_object_child(obj), &mti->mti_attr);
1213 mdt_pack_attr2body(mti, body, &mti->mti_attr.ma_attr,
1214 mdt_object_fid(obj));
1215 if (mti->mti_epoch && (mti->mti_epoch->flags & MF_EPOCH_OPEN)) {
1216 struct mdt_file_data *mfd;
1217 struct mdt_body *repbody;
1219 repbody = req_capsule_server_get(mti->mti_pill, &RMF_MDT_BODY);
1220 repbody->ioepoch = obj->mot_ioepoch;
1221 spin_lock(&med->med_open_lock);
1222 list_for_each_entry(mfd, &med->med_open_head, mfd_list) {
1223 if (mfd->mfd_xid == req->rq_xid)
1226 LASSERT(&mfd->mfd_list != &med->med_open_head);
1227 spin_unlock(&med->med_open_lock);
1228 repbody->handle.cookie = mfd->mfd_handle.h_cookie;
1231 mdt_object_put(mti->mti_env, obj);
1234 typedef void (*mdt_reconstructor)(struct mdt_thread_info *mti,
1235 struct mdt_lock_handle *lhc);
1237 static mdt_reconstructor reconstructors[REINT_MAX] = {
1238 [REINT_SETATTR] = mdt_reconstruct_setattr,
1239 [REINT_CREATE] = mdt_reconstruct_create,
1240 [REINT_LINK] = mdt_reconstruct_generic,
1241 [REINT_UNLINK] = mdt_reconstruct_generic,
1242 [REINT_RENAME] = mdt_reconstruct_generic,
1243 [REINT_OPEN] = mdt_reconstruct_open,
1244 [REINT_SETXATTR] = mdt_reconstruct_generic
1247 void mdt_reconstruct(struct mdt_thread_info *mti,
1248 struct mdt_lock_handle *lhc)
1251 reconstructors[mti->mti_rr.rr_opcode](mti, lhc);