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 (c) 2007, 2010, Oracle and/or its affiliates. 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 static inline int mdt_trans_credit_get(const struct lu_env *env,
81 struct mdt_device *mdt,
84 struct dt_device *dev = mdt->mdt_bottom;
87 case MDT_TXN_CAPA_KEYS_WRITE_OP:
88 case MDT_TXN_LAST_RCVD_WRITE_OP:
89 cr = dev->dd_ops->dt_credit_get(env,
99 void mdt_trans_credit_init(const struct lu_env *env,
100 struct mdt_device *mdt,
103 struct mdt_thread_info *mti;
107 mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
108 p = &mti->mti_txn_param;
110 cr = mdt_trans_credit_get(env, mdt, op);
111 txn_param_init(p, cr);
114 struct thandle* mdt_trans_start(const struct lu_env *env,
115 struct mdt_device *mdt)
117 struct mdt_thread_info *mti;
120 mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
121 p = &mti->mti_txn_param;
123 /* export can require sync operations */
124 if (mti->mti_exp != NULL)
125 p->tp_sync = mti->mti_exp->exp_need_sync;
127 return mdt->mdt_bottom->dd_ops->dt_trans_start(env, mdt->mdt_bottom, p);
130 void mdt_trans_stop(const struct lu_env *env,
131 struct mdt_device *mdt, struct thandle *th)
133 mdt->mdt_bottom->dd_ops->dt_trans_stop(env, th);
136 static inline int mdt_last_rcvd_header_read(const struct lu_env *env,
137 struct mdt_device *mdt)
139 struct mdt_thread_info *mti;
142 mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
145 rc = dt_record_read(env, mdt->mdt_lut.lut_last_rcvd,
146 mdt_buf(env, &mti->mti_lsd, sizeof(mti->mti_lsd)),
149 lsd_le_to_cpu(&mti->mti_lsd, &mdt->mdt_lut.lut_lsd);
151 CDEBUG(D_INFO, "read last_rcvd header rc = %d, uuid = %s, "
152 "last_transno = "LPU64"\n", rc, mdt->mdt_lut.lut_lsd.lsd_uuid,
153 mdt->mdt_lut.lut_lsd.lsd_last_transno);
157 static int mdt_last_rcvd_header_write(const struct lu_env *env,
158 struct mdt_device *mdt,
161 struct mdt_thread_info *mti;
165 mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
168 lsd_cpu_to_le(&mdt->mdt_lut.lut_lsd, &mti->mti_lsd);
170 rc = dt_record_write(env, mdt->mdt_lut.lut_last_rcvd,
171 mdt_buf_const(env, &mti->mti_lsd,
172 sizeof(mti->mti_lsd)),
175 CDEBUG(D_INFO, "write last_rcvd header rc = %d, uuid = %s, "
176 "last_transno = "LPU64"\n", rc, mdt->mdt_lut.lut_lsd.lsd_uuid,
177 mdt->mdt_lut.lut_lsd.lsd_last_transno);
182 static int mdt_last_rcvd_read(const struct lu_env *env, struct mdt_device *mdt,
183 struct lsd_client_data *lcd, loff_t *off,
186 struct mdt_thread_info *mti;
187 struct lsd_client_data *tmp;
190 mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
192 rc = dt_record_read(env, mdt->mdt_lut.lut_last_rcvd,
193 mdt_buf(env, tmp, sizeof(*tmp)), off);
195 check_lcd(mdt2obd_dev(mdt)->obd_name, index, tmp);
196 lcd_le_to_cpu(tmp, lcd);
199 CDEBUG(D_INFO, "read lcd @%d rc = %d, uuid = %s, last_transno = "LPU64
200 ", last_xid = "LPU64", last_result = %u, last_data = %u, "
201 "last_close_transno = "LPU64", last_close_xid = "LPU64", "
202 "last_close_result = %u\n", (int)(*off - sizeof(*tmp)),
203 rc, lcd->lcd_uuid, lcd->lcd_last_transno, lcd->lcd_last_xid,
204 lcd->lcd_last_result, lcd->lcd_last_data,
205 lcd->lcd_last_close_transno, lcd->lcd_last_close_xid,
206 lcd->lcd_last_close_result);
210 static int mdt_last_rcvd_write(const struct lu_env *env,
211 struct mdt_device *mdt,
212 struct lsd_client_data *lcd,
213 loff_t *off, struct thandle *th)
215 struct mdt_thread_info *mti;
216 struct lsd_client_data *tmp;
220 mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
223 lcd_cpu_to_le(lcd, tmp);
225 rc = dt_record_write(env, mdt->mdt_lut.lut_last_rcvd,
226 mdt_buf_const(env, tmp, sizeof(*tmp)), off, th);
228 CDEBUG(D_INFO, "write lcd @%d rc = %d, uuid = %s, last_transno = "LPU64
229 ", last_xid = "LPU64", last_result = %u, last_data = %u, "
230 "last_close_transno = "LPU64", last_close_xid = "LPU64" ,"
231 "last_close_result = %u\n", (int)(*off - sizeof(*tmp)),
232 rc, lcd->lcd_uuid, lcd->lcd_last_transno, lcd->lcd_last_xid,
233 lcd->lcd_last_result, lcd->lcd_last_data,
234 lcd->lcd_last_close_transno, lcd->lcd_last_close_xid,
235 lcd->lcd_last_close_result);
239 static int mdt_clients_data_init(const struct lu_env *env,
240 struct mdt_device *mdt,
241 unsigned long last_size)
243 struct lr_server_data *lsd = &mdt->mdt_lut.lut_lsd;
244 struct lsd_client_data *lcd;
245 struct obd_device *obd = mdt2obd_dev(mdt);
255 /* When we do a clean MDS shutdown, we save the last_transno into
256 * the header. If we find clients with higher last_transno values
257 * then those clients may need recovery done. */
258 LASSERT(cfs_atomic_read(&obd->obd_req_replay_clients) == 0);
259 for (cl_idx = 0, off = lsd->lsd_client_start;
260 off < last_size; cl_idx++) {
262 struct obd_export *exp;
263 struct mdt_thread_info *mti;
265 off = lsd->lsd_client_start +
266 cl_idx * lsd->lsd_client_size;
268 rc = mdt_last_rcvd_read(env, mdt, lcd, &off, cl_idx);
270 CERROR("error reading MDS %s idx %d, off %llu: rc %d\n",
271 LAST_RCVD, cl_idx, off, rc);
273 break; /* read error shouldn't cause startup to fail */
276 if (lcd->lcd_uuid[0] == '\0') {
277 CDEBUG(D_INFO, "skipping zeroed client at offset %d\n",
282 last_transno = lcd_last_transno(lcd);
284 /* These exports are cleaned up by mdt_obd_disconnect(), so
285 * they need to be set up like real exports as
286 * mdt_obd_connect() does.
288 CDEBUG(D_HA, "RCVRNG CLIENT uuid: %s idx: %d lr: "LPU64
289 " srv lr: "LPU64" lx: "LPU64"\n", lcd->lcd_uuid, cl_idx,
290 last_transno, lsd->lsd_last_transno,
293 exp = class_new_export(obd, (struct obd_uuid *)lcd->lcd_uuid);
295 if (PTR_ERR(exp) == -EALREADY) {
296 /* export already exists, zero out this one */
297 CERROR("Duplicate export %s!\n", lcd->lcd_uuid);
300 GOTO(err_client, rc = PTR_ERR(exp));
303 mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
304 LASSERT(mti != NULL);
306 /* copy on-disk lcd to the export */
307 *exp->exp_target_data.ted_lcd = *lcd;
308 rc = mdt_client_add(env, mdt, cl_idx);
309 /* can't fail existing */
310 LASSERTF(rc == 0, "rc = %d\n", rc);
311 /* VBR: set export last committed version */
312 exp->exp_last_committed = last_transno;
313 cfs_spin_lock(&exp->exp_lock);
314 exp->exp_connecting = 0;
315 exp->exp_in_recovery = 0;
316 cfs_spin_unlock(&exp->exp_lock);
317 obd->obd_max_recoverable_clients++;
318 class_export_put(exp);
320 CDEBUG(D_OTHER, "client at idx %d has last_transno="LPU64"\n",
321 cl_idx, last_transno);
322 /* protect __u64 value update */
323 cfs_spin_lock(&mdt->mdt_lut.lut_translock);
324 mdt->mdt_lut.lut_last_transno = max(last_transno,
325 mdt->mdt_lut.lut_last_transno);
326 cfs_spin_unlock(&mdt->mdt_lut.lut_translock);
334 static int mdt_server_data_init(const struct lu_env *env,
335 struct mdt_device *mdt,
336 struct lustre_sb_info *lsi)
338 struct lr_server_data *lsd = &mdt->mdt_lut.lut_lsd;
339 struct lsd_client_data *lcd = NULL;
340 struct obd_device *obd = mdt2obd_dev(mdt);
341 struct mdt_thread_info *mti;
342 struct dt_object *obj;
344 struct lustre_disk_data *ldd;
345 unsigned long last_rcvd_size;
350 /* ensure padding in the struct is the correct size */
351 CLASSERT(offsetof(struct lr_server_data, lsd_padding) +
352 sizeof(lsd->lsd_padding) == LR_SERVER_SIZE);
353 CLASSERT(offsetof(struct lsd_client_data, lcd_padding) +
354 sizeof(lcd->lcd_padding) == LR_CLIENT_SIZE);
356 mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
357 LASSERT(mti != NULL);
358 la = &mti->mti_attr.ma_attr;
360 obj = mdt->mdt_lut.lut_last_rcvd;
361 rc = obj->do_ops->do_attr_get(env, obj, la, BYPASS_CAPA);
365 last_rcvd_size = (unsigned long)la->la_size;
367 if (last_rcvd_size == 0) {
368 LCONSOLE_WARN("%s: new disk, initializing\n", obd->obd_name);
370 memcpy(lsd->lsd_uuid, obd->obd_uuid.uuid,
371 sizeof(lsd->lsd_uuid));
372 lsd->lsd_last_transno = 0;
373 lsd->lsd_mount_count = 0;
374 lsd->lsd_server_size = LR_SERVER_SIZE;
375 lsd->lsd_client_start = LR_CLIENT_START;
376 lsd->lsd_client_size = LR_CLIENT_SIZE;
377 lsd->lsd_feature_compat = OBD_COMPAT_MDT;
378 lsd->lsd_feature_rocompat = OBD_ROCOMPAT_LOVOBJID;
379 lsd->lsd_feature_incompat = OBD_INCOMPAT_MDT |
380 OBD_INCOMPAT_COMMON_LR;
382 LCONSOLE_WARN("%s: used disk, loading\n", obd->obd_name);
383 rc = mdt_last_rcvd_header_read(env, mdt);
385 CERROR("error reading MDS %s: rc %d\n", LAST_RCVD, rc);
388 if (strcmp(lsd->lsd_uuid, obd->obd_uuid.uuid) != 0) {
389 LCONSOLE_ERROR_MSG(0x157, "Trying to start OBD %s using"
390 "the wrong disk %s. Were the /dev/ "
391 "assignments rearranged?\n",
392 obd->obd_uuid.uuid, lsd->lsd_uuid);
393 GOTO(out, rc = -EINVAL);
395 lsd->lsd_feature_compat |= OBD_COMPAT_MDT;
396 lsd->lsd_feature_incompat |= OBD_INCOMPAT_MDT |
397 OBD_INCOMPAT_COMMON_LR;
399 mount_count = lsd->lsd_mount_count;
403 if (lsd->lsd_feature_incompat & ~MDT_INCOMPAT_SUPP) {
404 CERROR("%s: unsupported incompat filesystem feature(s) %x\n",
406 lsd->lsd_feature_incompat & ~MDT_INCOMPAT_SUPP);
407 GOTO(out, rc = -EINVAL);
409 if (lsd->lsd_feature_rocompat & ~MDT_ROCOMPAT_SUPP) {
410 CERROR("%s: unsupported read-only filesystem feature(s) %x\n",
412 lsd->lsd_feature_rocompat & ~MDT_ROCOMPAT_SUPP);
413 /* XXX: Do something like remount filesystem read-only */
414 GOTO(out, rc = -EINVAL);
416 /** Interop: evict all clients at first boot with 1.8 last_rcvd */
417 if (!(lsd->lsd_feature_compat & OBD_COMPAT_20)) {
418 if (last_rcvd_size > lsd->lsd_client_start) {
419 LCONSOLE_WARN("Mounting %s at first time on 1.8 FS, "
420 "remove all clients for interop needs\n",
422 simple_truncate(lsi->lsi_srv_mnt->mnt_sb->s_root,
423 lsi->lsi_srv_mnt, LAST_RCVD,
424 lsd->lsd_client_start);
425 last_rcvd_size = lsd->lsd_client_start;
427 /** set 2.0 flag to upgrade/downgrade between 1.8 and 2.0 */
428 lsd->lsd_feature_compat |= OBD_COMPAT_20;
431 if (ldd->ldd_flags & LDD_F_IAM_DIR)
432 lsd->lsd_feature_incompat |= OBD_INCOMPAT_IAM_DIR;
434 lsd->lsd_feature_incompat |= OBD_INCOMPAT_FID;
436 cfs_spin_lock(&mdt->mdt_lut.lut_translock);
437 mdt->mdt_lut.lut_last_transno = lsd->lsd_last_transno;
438 cfs_spin_unlock(&mdt->mdt_lut.lut_translock);
440 CDEBUG(D_INODE, "========BEGIN DUMPING LAST_RCVD========\n");
441 CDEBUG(D_INODE, "%s: server last_transno: "LPU64"\n",
442 obd->obd_name, mdt->mdt_lut.lut_last_transno);
443 CDEBUG(D_INODE, "%s: server mount_count: "LPU64"\n",
444 obd->obd_name, mount_count + 1);
445 CDEBUG(D_INODE, "%s: server data size: %u\n",
446 obd->obd_name, lsd->lsd_server_size);
447 CDEBUG(D_INODE, "%s: per-client data start: %u\n",
448 obd->obd_name, lsd->lsd_client_start);
449 CDEBUG(D_INODE, "%s: per-client data size: %u\n",
450 obd->obd_name, lsd->lsd_client_size);
451 CDEBUG(D_INODE, "%s: last_rcvd size: %lu\n",
452 obd->obd_name, last_rcvd_size);
453 CDEBUG(D_INODE, "%s: last_rcvd clients: %lu\n", obd->obd_name,
454 last_rcvd_size <= lsd->lsd_client_start ? 0 :
455 (last_rcvd_size - lsd->lsd_client_start) /
456 lsd->lsd_client_size);
457 CDEBUG(D_INODE, "========END DUMPING LAST_RCVD========\n");
459 if (!lsd->lsd_server_size || !lsd->lsd_client_start ||
460 !lsd->lsd_client_size) {
461 CERROR("Bad last_rcvd contents!\n");
462 GOTO(out, rc = -EINVAL);
465 rc = mdt_clients_data_init(env, mdt, last_rcvd_size);
467 GOTO(err_client, rc);
469 cfs_spin_lock(&mdt->mdt_lut.lut_translock);
470 /* obd_last_committed is used for compatibility
471 * with other lustre recovery code */
472 obd->obd_last_committed = mdt->mdt_lut.lut_last_transno;
473 cfs_spin_unlock(&mdt->mdt_lut.lut_translock);
475 obd->u.obt.obt_mount_count = mount_count + 1;
476 lsd->lsd_mount_count = obd->u.obt.obt_mount_count;
478 /* save it, so mount count and last_transno is current */
479 rc = mdt_server_data_update(env, mdt);
481 GOTO(err_client, rc);
486 class_disconnect_exports(obd);
491 static int mdt_server_data_update(const struct lu_env *env,
492 struct mdt_device *mdt)
494 struct mdt_thread_info *mti;
498 mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
500 mdt_trans_credit_init(env, mdt, MDT_TXN_LAST_RCVD_WRITE_OP);
501 th = mdt_trans_start(env, mdt);
505 CDEBUG(D_SUPER, "MDS mount_count is "LPU64", last_transno is "LPU64"\n",
506 mdt->mdt_lut.lut_obd->u.obt.obt_mount_count,
507 mdt->mdt_lut.lut_last_transno);
509 cfs_spin_lock(&mdt->mdt_lut.lut_translock);
510 mdt->mdt_lut.lut_lsd.lsd_last_transno = mdt->mdt_lut.lut_last_transno;
511 cfs_spin_unlock(&mdt->mdt_lut.lut_translock);
513 rc = mdt_last_rcvd_header_write(env, mdt, th);
514 mdt_trans_stop(env, mdt, th);
519 int mdt_client_new(const struct lu_env *env, struct mdt_device *mdt)
521 unsigned long *bitmap = mdt->mdt_lut.lut_client_bitmap;
522 struct mdt_thread_info *mti;
523 struct tg_export_data *ted;
524 struct lr_server_data *lsd = &mdt->mdt_lut.lut_lsd;
525 struct obd_device *obd = mdt2obd_dev(mdt);
532 mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
533 LASSERT(mti != NULL);
535 ted = &mti->mti_exp->exp_target_data;
537 LASSERT(bitmap != NULL);
538 if (!strcmp(ted->ted_lcd->lcd_uuid, obd->obd_uuid.uuid))
541 /* the bitmap operations can handle cl_idx > sizeof(long) * 8, so
542 * there's no need for extra complication here
544 cfs_spin_lock(&mdt->mdt_lut.lut_client_bitmap_lock);
545 cl_idx = cfs_find_first_zero_bit(bitmap, LR_MAX_CLIENTS);
546 if (cl_idx >= LR_MAX_CLIENTS ||
547 OBD_FAIL_CHECK(OBD_FAIL_MDS_CLIENT_ADD)) {
548 CERROR("no room for %u clients - fix LR_MAX_CLIENTS\n",
550 cfs_spin_unlock(&mdt->mdt_lut.lut_client_bitmap_lock);
553 cfs_set_bit(cl_idx, bitmap);
554 cfs_spin_unlock(&mdt->mdt_lut.lut_client_bitmap_lock);
556 CDEBUG(D_INFO, "client at idx %d with UUID '%s' added\n",
557 cl_idx, ted->ted_lcd->lcd_uuid);
559 ted->ted_lr_idx = cl_idx;
560 ted->ted_lr_off = lsd->lsd_client_start +
561 (cl_idx * lsd->lsd_client_size);
562 cfs_init_mutex(&ted->ted_lcd_lock);
564 LASSERTF(ted->ted_lr_off > 0, "ted_lr_off = %llu\n", ted->ted_lr_off);
566 /* Write new client data. */
567 off = ted->ted_lr_off;
569 if (OBD_FAIL_CHECK(OBD_FAIL_TGT_CLIENT_ADD))
572 mdt_trans_credit_init(env, mdt, MDT_TXN_LAST_RCVD_WRITE_OP);
574 th = mdt_trans_start(env, mdt);
579 * Until this operations will be committed the sync is needed
580 * for this export. This should be done _after_ starting the
581 * transaction so that many connecting clients will not bring
582 * server down with lots of sync writes.
584 mdt_trans_add_cb(th, lut_cb_client, class_export_cb_get(mti->mti_exp));
585 cfs_spin_lock(&mti->mti_exp->exp_lock);
586 mti->mti_exp->exp_need_sync = 1;
587 cfs_spin_unlock(&mti->mti_exp->exp_lock);
589 rc = mdt_last_rcvd_write(env, mdt, ted->ted_lcd, &off, th);
590 CDEBUG(D_INFO, "wrote client lcd at idx %u off %llu (len %u)\n",
591 cl_idx, ted->ted_lr_off, (int)sizeof(*(ted->ted_lcd)));
592 mdt_trans_stop(env, mdt, th);
597 /* Add client data to the MDS. We use a bitmap to locate a free space
598 * in the last_rcvd file if cl_off is -1 (i.e. a new client).
599 * Otherwise, we just have to read the data from the last_rcvd file and
600 * we know its offset.
602 * It should not be possible to fail adding an existing client - otherwise
603 * mdt_init_server_data() callsite needs to be fixed.
605 int mdt_client_add(const struct lu_env *env,
606 struct mdt_device *mdt, int cl_idx)
608 struct mdt_thread_info *mti;
609 struct tg_export_data *ted;
610 unsigned long *bitmap = mdt->mdt_lut.lut_client_bitmap;
611 struct obd_device *obd = mdt2obd_dev(mdt);
612 struct lr_server_data *lsd = &mdt->mdt_lut.lut_lsd;
616 mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
617 LASSERT(mti != NULL);
619 ted = &mti->mti_exp->exp_target_data;
621 LASSERT(bitmap != NULL);
622 LASSERTF(cl_idx >= 0, "%d\n", cl_idx);
624 if (!strcmp(ted->ted_lcd->lcd_uuid, obd->obd_uuid.uuid))
627 cfs_spin_lock(&mdt->mdt_lut.lut_client_bitmap_lock);
628 if (cfs_test_and_set_bit(cl_idx, bitmap)) {
629 CERROR("MDS client %d: bit already set in bitmap!!\n",
633 cfs_spin_unlock(&mdt->mdt_lut.lut_client_bitmap_lock);
635 CDEBUG(D_INFO, "client at idx %d with UUID '%s' added\n",
636 cl_idx, ted->ted_lcd->lcd_uuid);
638 ted->ted_lr_idx = cl_idx;
639 ted->ted_lr_off = lsd->lsd_client_start +
640 (cl_idx * lsd->lsd_client_size);
641 cfs_init_mutex(&ted->ted_lcd_lock);
643 LASSERTF(ted->ted_lr_off > 0, "ted_lr_off = %llu\n", ted->ted_lr_off);
648 int mdt_client_del(const struct lu_env *env, struct mdt_device *mdt)
650 struct mdt_thread_info *mti;
651 struct tg_export_data *ted;
652 struct obd_device *obd = mdt2obd_dev(mdt);
653 struct obd_export *exp;
659 mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
660 LASSERT(mti != NULL);
663 ted = &exp->exp_target_data;
667 /* XXX: If lcd_uuid were a real obd_uuid, I could use obd_uuid_equals */
668 if (!strcmp(ted->ted_lcd->lcd_uuid, obd->obd_uuid.uuid))
671 CDEBUG(D_INFO, "freeing client at idx %u, offset %lld\n",
672 ted->ted_lr_idx, ted->ted_lr_off);
674 off = ted->ted_lr_off;
677 * Don't clear ted_lr_idx here as it is likely also unset. At worst we
678 * leak a client slot that will be cleaned on the next recovery.
681 CERROR("client idx %d has offset %lld\n",
682 ted->ted_lr_idx, off);
683 GOTO(free, rc = -EINVAL);
687 * Clear the bit _after_ zeroing out the client so we don't race with
688 * mdt_client_add and zero out new clients.
690 if (!cfs_test_bit(ted->ted_lr_idx, mdt->mdt_lut.lut_client_bitmap)) {
691 CERROR("MDT client %u: bit already clear in bitmap!!\n",
696 /* Make sure the server's last_transno is up to date.
697 * This should be done before zeroing client slot so last_transno will
698 * be in server data or in client data in case of failure */
699 mdt_server_data_update(env, mdt);
701 mdt_trans_credit_init(env, mdt, MDT_TXN_LAST_RCVD_WRITE_OP);
702 th = mdt_trans_start(env, mdt);
704 GOTO(free, rc = PTR_ERR(th));
706 cfs_mutex_down(&ted->ted_lcd_lock);
707 memset(ted->ted_lcd->lcd_uuid, 0, sizeof ted->ted_lcd->lcd_uuid);
708 rc = mdt_last_rcvd_write(env, mdt, ted->ted_lcd, &off, th);
709 cfs_mutex_up(&ted->ted_lcd_lock);
710 mdt_trans_stop(env, mdt, th);
712 CDEBUG(rc == 0 ? D_INFO : D_ERROR, "Zeroing out client idx %u in "
713 "%s, rc %d\n", ted->ted_lr_idx, LAST_RCVD, rc);
720 * last_rcvd & last_committed update callbacks
722 static int mdt_last_rcvd_update(struct mdt_thread_info *mti,
725 struct mdt_device *mdt = mti->mti_mdt;
726 struct ptlrpc_request *req = mdt_info_req(mti);
727 struct tg_export_data *ted;
728 struct lsd_client_data *lcd;
731 __s32 rc = th->th_result;
735 LASSERT(req->rq_export);
737 ted = &req->rq_export->exp_target_data;
740 cfs_mutex_down(&ted->ted_lcd_lock);
742 /* if the export has already been disconnected, we have no last_rcvd slot,
743 * update server data with latest transno then */
745 cfs_mutex_up(&ted->ted_lcd_lock);
746 CWARN("commit transaction for disconnected client %s: rc %d\n",
747 req->rq_export->exp_client_uuid.uuid, rc);
748 err = mdt_last_rcvd_header_write(mti->mti_env, mdt, th);
752 off = ted->ted_lr_off;
753 LASSERT(ergo(mti->mti_transno == 0, rc != 0));
754 if (lustre_msg_get_opc(req->rq_reqmsg) == MDS_CLOSE ||
755 lustre_msg_get_opc(req->rq_reqmsg) == MDS_DONE_WRITING) {
756 if (mti->mti_transno != 0)
757 lcd->lcd_last_close_transno = mti->mti_transno;
758 lcd->lcd_last_close_xid = req->rq_xid;
759 lcd->lcd_last_close_result = rc;
761 /* VBR: save versions in last_rcvd for reconstruct. */
762 __u64 *pre_versions = lustre_msg_get_versions(req->rq_repmsg);
764 lcd->lcd_pre_versions[0] = pre_versions[0];
765 lcd->lcd_pre_versions[1] = pre_versions[1];
766 lcd->lcd_pre_versions[2] = pre_versions[2];
767 lcd->lcd_pre_versions[3] = pre_versions[3];
769 if (mti->mti_transno != 0)
770 lcd->lcd_last_transno = mti->mti_transno;
771 lcd->lcd_last_xid = req->rq_xid;
772 lcd->lcd_last_result = rc;
773 /*XXX: save intent_disposition in mdt_thread_info?
774 * also there is bug - intent_dispostion is __u64,
775 * see struct ldlm_reply->lock_policy_res1; */
776 lcd->lcd_last_data = mti->mti_opdata;
780 CERROR("client idx %d has offset %lld\n", ted->ted_lr_idx, off);
783 err = mdt_last_rcvd_write(mti->mti_env, mdt, lcd, &off, th);
785 cfs_mutex_up(&ted->ted_lcd_lock);
789 extern struct lu_context_key mdt_thread_key;
791 /* add credits for last_rcvd update */
792 static int mdt_txn_start_cb(const struct lu_env *env,
793 struct txn_param *param, void *cookie)
795 struct mdt_device *mdt = cookie;
797 param->tp_credits += mdt_trans_credit_get(env, mdt,
798 MDT_TXN_LAST_RCVD_WRITE_OP);
802 /* Set new object versions */
803 static void mdt_version_set(struct mdt_thread_info *info)
805 if (info->mti_mos != NULL) {
806 mo_version_set(info->mti_env, mdt_object_child(info->mti_mos),
808 info->mti_mos = NULL;
812 /* Update last_rcvd records with latests transaction data */
813 static int mdt_txn_stop_cb(const struct lu_env *env,
814 struct thandle *txn, void *cookie)
816 struct mdt_device *mdt = cookie;
817 struct mdt_txn_info *txi;
818 struct mdt_thread_info *mti;
819 struct ptlrpc_request *req;
821 /* transno in two contexts - for commit_cb and for thread */
822 txi = lu_context_key_get(&txn->th_ctx, &mdt_txn_key);
823 mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
824 req = mdt_info_req(mti);
826 if (mti->mti_mdt == NULL || req == NULL || mti->mti_no_need_trans) {
827 txi->txi_transno = 0;
828 mti->mti_no_need_trans = 0;
832 if (mti->mti_has_trans) {
833 /* XXX: currently there are allowed cases, but the wrong cases
834 * are also possible, so better check is needed here */
835 CDEBUG(D_INFO, "More than one transaction "LPU64"\n",
840 mti->mti_has_trans = 1;
841 cfs_spin_lock(&mdt->mdt_lut.lut_translock);
842 if (txn->th_result != 0) {
843 if (mti->mti_transno != 0) {
844 CERROR("Replay transno "LPU64" failed: rc %d\n",
845 mti->mti_transno, txn->th_result);
847 } else if (mti->mti_transno == 0) {
848 mti->mti_transno = ++ mdt->mdt_lut.lut_last_transno;
850 /* should be replay */
851 if (mti->mti_transno > mdt->mdt_lut.lut_last_transno)
852 mdt->mdt_lut.lut_last_transno = mti->mti_transno;
854 cfs_spin_unlock(&mdt->mdt_lut.lut_translock);
855 /* sometimes the reply message has not been successfully packed */
856 LASSERT(req != NULL && req->rq_repmsg != NULL);
858 /** VBR: set new versions */
859 if (txn->th_result == 0)
860 mdt_version_set(mti);
862 /* filling reply data */
863 CDEBUG(D_INODE, "transno = "LPU64", last_committed = "LPU64"\n",
864 mti->mti_transno, req->rq_export->exp_obd->obd_last_committed);
866 req->rq_transno = mti->mti_transno;
867 lustre_msg_set_transno(req->rq_repmsg, mti->mti_transno);
868 /* save transno for the commit callback */
869 txi->txi_transno = mti->mti_transno;
871 /* add separate commit callback for transaction handling because we need
872 * export as parameter */
873 mdt_trans_add_cb(txn, lut_cb_last_committed,
874 class_export_cb_get(mti->mti_exp));
876 return mdt_last_rcvd_update(mti, txn);
879 /* commit callback, need to update last_committed value */
880 static int mdt_txn_commit_cb(const struct lu_env *env,
881 struct thandle *txn, void *cookie)
883 struct mdt_device *mdt = cookie;
884 struct mdt_txn_info *txi;
887 txi = lu_context_key_get(&txn->th_ctx, &mdt_txn_key);
889 /* iterate through all additional callbacks */
890 for (i = 0; i < txi->txi_cb_count; i++) {
891 txi->txi_cb[i].lut_cb_func(&mdt->mdt_lut, txi->txi_transno,
892 txi->txi_cb[i].lut_cb_data, 0);
897 int mdt_fs_setup(const struct lu_env *env, struct mdt_device *mdt,
898 struct obd_device *obd,
899 struct lustre_sb_info *lsi)
906 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_FS_SETUP))
909 /* prepare transactions callbacks */
910 mdt->mdt_txn_cb.dtc_txn_start = mdt_txn_start_cb;
911 mdt->mdt_txn_cb.dtc_txn_stop = mdt_txn_stop_cb;
912 mdt->mdt_txn_cb.dtc_txn_commit = mdt_txn_commit_cb;
913 mdt->mdt_txn_cb.dtc_cookie = mdt;
914 mdt->mdt_txn_cb.dtc_tag = LCT_MD_THREAD;
915 CFS_INIT_LIST_HEAD(&mdt->mdt_txn_cb.dtc_linkage);
917 dt_txn_callback_add(mdt->mdt_bottom, &mdt->mdt_txn_cb);
919 rc = mdt_server_data_init(env, mdt, lsi);
923 o = dt_store_open(env, mdt->mdt_bottom, "", CAPA_KEYS, &fid);
926 rc = mdt_capa_keys_init(env, mdt);
928 GOTO(put_ck_object, rc);
931 CERROR("cannot open %s: rc = %d\n", CAPA_KEYS, rc);
932 GOTO(disconnect_exports, rc);
937 lu_object_put(env, &o->do_lu);
938 mdt->mdt_ck_obj = NULL;
940 class_disconnect_exports(obd);
944 void mdt_fs_cleanup(const struct lu_env *env, struct mdt_device *mdt)
948 /* Remove transaction callback */
949 dt_txn_callback_del(mdt->mdt_bottom, &mdt->mdt_txn_cb);
951 lu_object_put(env, &mdt->mdt_ck_obj->do_lu);
952 mdt->mdt_ck_obj = NULL;
956 /* reconstruction code */
957 static void mdt_steal_ack_locks(struct ptlrpc_request *req)
959 struct obd_export *exp = req->rq_export;
961 struct ptlrpc_reply_state *oldrep;
962 struct ptlrpc_service *svc;
965 /* CAVEAT EMPTOR: spinlock order */
966 cfs_spin_lock(&exp->exp_lock);
967 cfs_list_for_each (tmp, &exp->exp_outstanding_replies) {
968 oldrep = cfs_list_entry(tmp, struct ptlrpc_reply_state,
971 if (oldrep->rs_xid != req->rq_xid)
974 if (oldrep->rs_opc != lustre_msg_get_opc(req->rq_reqmsg))
975 CERROR ("Resent req xid "LPU64" has mismatched opc: "
976 "new %d old %d\n", req->rq_xid,
977 lustre_msg_get_opc(req->rq_reqmsg),
980 svc = oldrep->rs_service;
981 cfs_spin_lock (&svc->srv_rs_lock);
983 cfs_list_del_init (&oldrep->rs_exp_list);
985 CWARN("Stealing %d locks from rs %p x"LPD64".t"LPD64
987 oldrep->rs_nlocks, oldrep,
988 oldrep->rs_xid, oldrep->rs_transno, oldrep->rs_opc,
989 libcfs_nid2str(exp->exp_connection->c_peer.nid));
991 for (i = 0; i < oldrep->rs_nlocks; i++)
992 ptlrpc_save_lock(req, &oldrep->rs_locks[i],
993 oldrep->rs_modes[i], 0);
994 oldrep->rs_nlocks = 0;
996 DEBUG_REQ(D_HA, req, "stole locks for");
997 cfs_spin_lock(&oldrep->rs_lock);
998 ptlrpc_schedule_difficult_reply (oldrep);
999 cfs_spin_unlock(&oldrep->rs_lock);
1001 cfs_spin_unlock (&svc->srv_rs_lock);
1004 cfs_spin_unlock(&exp->exp_lock);
1008 * VBR: restore versions
1010 void mdt_vbr_reconstruct(struct ptlrpc_request *req,
1011 struct lsd_client_data *lcd)
1013 __u64 pre_versions[4] = {0};
1014 pre_versions[0] = lcd->lcd_pre_versions[0];
1015 pre_versions[1] = lcd->lcd_pre_versions[1];
1016 pre_versions[2] = lcd->lcd_pre_versions[2];
1017 pre_versions[3] = lcd->lcd_pre_versions[3];
1018 lustre_msg_set_versions(req->rq_repmsg, pre_versions);
1021 void mdt_req_from_lcd(struct ptlrpc_request *req,
1022 struct lsd_client_data *lcd)
1024 DEBUG_REQ(D_HA, req, "restoring transno "LPD64"/status %d",
1025 lcd->lcd_last_transno, lcd->lcd_last_result);
1027 if (lustre_msg_get_opc(req->rq_reqmsg) == MDS_CLOSE ||
1028 lustre_msg_get_opc(req->rq_repmsg) == MDS_DONE_WRITING) {
1029 req->rq_transno = lcd->lcd_last_close_transno;
1030 req->rq_status = lcd->lcd_last_close_result;
1032 req->rq_transno = lcd->lcd_last_transno;
1033 req->rq_status = lcd->lcd_last_result;
1034 mdt_vbr_reconstruct(req, lcd);
1036 if (req->rq_status != 0)
1037 req->rq_transno = 0;
1038 lustre_msg_set_transno(req->rq_repmsg, req->rq_transno);
1039 lustre_msg_set_status(req->rq_repmsg, req->rq_status);
1040 DEBUG_REQ(D_RPCTRACE, req, "restoring transno "LPD64"/status %d",
1041 req->rq_transno, req->rq_status);
1043 mdt_steal_ack_locks(req);
1046 void mdt_reconstruct_generic(struct mdt_thread_info *mti,
1047 struct mdt_lock_handle *lhc)
1049 struct ptlrpc_request *req = mdt_info_req(mti);
1050 struct tg_export_data *ted = &req->rq_export->exp_target_data;
1052 return mdt_req_from_lcd(req, ted->ted_lcd);
1055 static void mdt_reconstruct_create(struct mdt_thread_info *mti,
1056 struct mdt_lock_handle *lhc)
1058 struct ptlrpc_request *req = mdt_info_req(mti);
1059 struct obd_export *exp = req->rq_export;
1060 struct tg_export_data *ted = &exp->exp_target_data;
1061 struct mdt_device *mdt = mti->mti_mdt;
1062 struct mdt_object *child;
1063 struct mdt_body *body;
1066 mdt_req_from_lcd(req, ted->ted_lcd);
1070 /* if no error, so child was created with requested fid */
1071 child = mdt_object_find(mti->mti_env, mdt, mti->mti_rr.rr_fid2);
1072 if (IS_ERR(child)) {
1073 rc = PTR_ERR(child);
1074 LCONSOLE_WARN("Child "DFID" lookup error %d."
1075 " Evicting client %s with export %s.\n",
1076 PFID(mdt_object_fid(child)), rc,
1077 obd_uuid2str(&exp->exp_client_uuid),
1078 obd_export_nid2str(exp));
1079 mdt_export_evict(exp);
1084 body = req_capsule_server_get(mti->mti_pill, &RMF_MDT_BODY);
1085 mti->mti_attr.ma_need = MA_INODE;
1086 mti->mti_attr.ma_valid = 0;
1087 rc = mo_attr_get(mti->mti_env, mdt_object_child(child), &mti->mti_attr);
1088 if (rc == -EREMOTE) {
1089 /* object was created on remote server */
1090 req->rq_status = rc;
1091 body->valid |= OBD_MD_MDS;
1093 mdt_pack_attr2body(mti, body, &mti->mti_attr.ma_attr,
1094 mdt_object_fid(child));
1095 mdt_object_put(mti->mti_env, child);
1098 static void mdt_reconstruct_setattr(struct mdt_thread_info *mti,
1099 struct mdt_lock_handle *lhc)
1101 struct ptlrpc_request *req = mdt_info_req(mti);
1102 struct obd_export *exp = req->rq_export;
1103 struct mdt_export_data *med = &exp->exp_mdt_data;
1104 struct mdt_device *mdt = mti->mti_mdt;
1105 struct mdt_object *obj;
1106 struct mdt_body *body;
1108 mdt_req_from_lcd(req, med->med_ted.ted_lcd);
1112 body = req_capsule_server_get(mti->mti_pill, &RMF_MDT_BODY);
1113 obj = mdt_object_find(mti->mti_env, mdt, mti->mti_rr.rr_fid1);
1115 int rc = PTR_ERR(obj);
1116 LCONSOLE_WARN(""DFID" lookup error %d."
1117 " Evicting client %s with export %s.\n",
1118 PFID(mdt_object_fid(obj)), rc,
1119 obd_uuid2str(&exp->exp_client_uuid),
1120 obd_export_nid2str(exp));
1121 mdt_export_evict(exp);
1125 mti->mti_attr.ma_need = MA_INODE;
1126 mti->mti_attr.ma_valid = 0;
1127 mo_attr_get(mti->mti_env, mdt_object_child(obj), &mti->mti_attr);
1128 mdt_pack_attr2body(mti, body, &mti->mti_attr.ma_attr,
1129 mdt_object_fid(obj));
1130 if (mti->mti_ioepoch && (mti->mti_ioepoch->flags & MF_EPOCH_OPEN)) {
1131 struct mdt_file_data *mfd;
1132 struct mdt_body *repbody;
1134 repbody = req_capsule_server_get(mti->mti_pill, &RMF_MDT_BODY);
1135 repbody->ioepoch = obj->mot_ioepoch;
1136 cfs_spin_lock(&med->med_open_lock);
1137 cfs_list_for_each_entry(mfd, &med->med_open_head, mfd_list) {
1138 if (mfd->mfd_xid == req->rq_xid)
1141 LASSERT(&mfd->mfd_list != &med->med_open_head);
1142 cfs_spin_unlock(&med->med_open_lock);
1143 repbody->handle.cookie = mfd->mfd_handle.h_cookie;
1146 mdt_object_put(mti->mti_env, obj);
1149 typedef void (*mdt_reconstructor)(struct mdt_thread_info *mti,
1150 struct mdt_lock_handle *lhc);
1152 static mdt_reconstructor reconstructors[REINT_MAX] = {
1153 [REINT_SETATTR] = mdt_reconstruct_setattr,
1154 [REINT_CREATE] = mdt_reconstruct_create,
1155 [REINT_LINK] = mdt_reconstruct_generic,
1156 [REINT_UNLINK] = mdt_reconstruct_generic,
1157 [REINT_RENAME] = mdt_reconstruct_generic,
1158 [REINT_OPEN] = mdt_reconstruct_open,
1159 [REINT_SETXATTR] = mdt_reconstruct_generic
1162 void mdt_reconstruct(struct mdt_thread_info *mti,
1163 struct mdt_lock_handle *lhc)
1166 reconstructors[mti->mti_rr.rr_opcode](mti, lhc);