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.
32 * Copyright (c) 2011, 2012, Whamcloud, Inc.
35 * This file is part of Lustre, http://www.lustre.org/
36 * Lustre is a trademark of Sun Microsystems, Inc.
38 * lustre/mdt/mdt_recovery.c
40 * Lustre Metadata Target (mdt) recovery-related methods
42 * Author: Huang Hua <huanghua@clusterfs.com>
43 * Author: Pershin Mike <tappro@clusterfs.com>
47 # define EXPORT_SYMTAB
49 #define DEBUG_SUBSYSTEM S_MDS
51 #include "mdt_internal.h"
53 static int mdt_server_data_update(const struct lu_env *env,
54 struct mdt_device *mdt);
56 struct lu_buf *mdt_buf(const struct lu_env *env, void *area, ssize_t len)
59 struct mdt_thread_info *mti;
61 mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
68 const struct lu_buf *mdt_buf_const(const struct lu_env *env,
69 const void *area, ssize_t len)
72 struct mdt_thread_info *mti;
74 mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
77 buf->lb_buf = (void *)area;
82 struct thandle *mdt_trans_create(const struct lu_env *env,
83 struct mdt_device *mdt)
85 return mdt->mdt_bottom->dd_ops->dt_trans_create(env, mdt->mdt_bottom);
88 int mdt_trans_start(const struct lu_env *env, struct mdt_device *mdt,
91 return dt_trans_start(env, mdt->mdt_bottom, th);
94 void mdt_trans_stop(const struct lu_env *env,
95 struct mdt_device *mdt, struct thandle *th)
97 struct mdt_thread_info *mti;
99 mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
100 /* export can require sync operations */
101 if (mti->mti_exp != NULL)
102 th->th_sync |= mti->mti_exp->exp_need_sync;
103 dt_trans_stop(env, mdt->mdt_bottom, th);
106 static inline int mdt_last_rcvd_header_read(const struct lu_env *env,
107 struct mdt_device *mdt)
109 struct mdt_thread_info *mti;
112 mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
115 rc = dt_record_read(env, mdt->mdt_lut.lut_last_rcvd,
116 mdt_buf(env, &mti->mti_lsd, sizeof(mti->mti_lsd)),
119 lsd_le_to_cpu(&mti->mti_lsd, &mdt->mdt_lut.lut_lsd);
121 CDEBUG(D_INFO, "read last_rcvd header rc = %d, uuid = %s, "
122 "last_transno = "LPU64"\n", rc, mdt->mdt_lut.lut_lsd.lsd_uuid,
123 mdt->mdt_lut.lut_lsd.lsd_last_transno);
127 static int mdt_declare_last_rcvd_header_write(const struct lu_env *env,
128 struct mdt_device *mdt,
131 struct mdt_thread_info *mti;
133 mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
135 return dt_declare_record_write(env, mdt->mdt_lut.lut_last_rcvd,
136 sizeof(mti->mti_lsd), 0, th);
139 static int mdt_last_rcvd_header_write(const struct lu_env *env,
140 struct mdt_device *mdt,
143 struct mdt_thread_info *mti;
147 mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
150 lsd_cpu_to_le(&mdt->mdt_lut.lut_lsd, &mti->mti_lsd);
152 rc = dt_record_write(env, mdt->mdt_lut.lut_last_rcvd,
153 mdt_buf_const(env, &mti->mti_lsd,
154 sizeof(mti->mti_lsd)),
157 CDEBUG(D_INFO, "write last_rcvd header rc = %d, uuid = %s, "
158 "last_transno = "LPU64"\n", rc, mdt->mdt_lut.lut_lsd.lsd_uuid,
159 mdt->mdt_lut.lut_lsd.lsd_last_transno);
164 static int mdt_last_rcvd_read(const struct lu_env *env, struct mdt_device *mdt,
165 struct lsd_client_data *lcd, loff_t *off,
168 struct mdt_thread_info *mti;
169 struct lsd_client_data *tmp;
172 mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
174 rc = dt_record_read(env, mdt->mdt_lut.lut_last_rcvd,
175 mdt_buf(env, tmp, sizeof(*tmp)), off);
177 check_lcd(mdt2obd_dev(mdt)->obd_name, index, tmp);
178 lcd_le_to_cpu(tmp, lcd);
181 CDEBUG(D_INFO, "read lcd @%d rc = %d, uuid = %s, last_transno = "LPU64
182 ", last_xid = "LPU64", last_result = %u, last_data = %u, "
183 "last_close_transno = "LPU64", last_close_xid = "LPU64", "
184 "last_close_result = %u\n", (int)(*off - sizeof(*tmp)),
185 rc, lcd->lcd_uuid, lcd->lcd_last_transno, lcd->lcd_last_xid,
186 lcd->lcd_last_result, lcd->lcd_last_data,
187 lcd->lcd_last_close_transno, lcd->lcd_last_close_xid,
188 lcd->lcd_last_close_result);
192 static int mdt_declare_last_rcvd_write(const struct lu_env *env,
193 struct mdt_device *mdt,
194 loff_t off, struct thandle *th)
196 return dt_declare_record_write(env, mdt->mdt_lut.lut_last_rcvd,
197 sizeof(struct lsd_client_data), off, th);
200 static int mdt_last_rcvd_write(const struct lu_env *env,
201 struct mdt_device *mdt,
202 struct lsd_client_data *lcd,
203 loff_t *off, struct thandle *th)
205 struct mdt_thread_info *mti;
206 struct lsd_client_data *tmp;
210 mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
213 lcd_cpu_to_le(lcd, tmp);
215 rc = dt_record_write(env, mdt->mdt_lut.lut_last_rcvd,
216 mdt_buf_const(env, tmp, sizeof(*tmp)), off, th);
218 CDEBUG(D_INFO, "write lcd @%d rc = %d, uuid = %s, last_transno = "LPU64
219 ", last_xid = "LPU64", last_result = %u, last_data = %u, "
220 "last_close_transno = "LPU64", last_close_xid = "LPU64" ,"
221 "last_close_result = %u\n", (int)(*off - sizeof(*tmp)),
222 rc, lcd->lcd_uuid, lcd->lcd_last_transno, lcd->lcd_last_xid,
223 lcd->lcd_last_result, lcd->lcd_last_data,
224 lcd->lcd_last_close_transno, lcd->lcd_last_close_xid,
225 lcd->lcd_last_close_result);
229 static int mdt_clients_data_init(const struct lu_env *env,
230 struct mdt_device *mdt,
231 unsigned long last_size)
233 struct lr_server_data *lsd = &mdt->mdt_lut.lut_lsd;
234 struct lsd_client_data *lcd;
235 struct obd_device *obd = mdt2obd_dev(mdt);
245 /* When we do a clean MDS shutdown, we save the last_transno into
246 * the header. If we find clients with higher last_transno values
247 * then those clients may need recovery done. */
248 LASSERT(cfs_atomic_read(&obd->obd_req_replay_clients) == 0);
249 for (cl_idx = 0, off = lsd->lsd_client_start;
250 off < last_size; cl_idx++) {
252 struct obd_export *exp;
253 struct mdt_thread_info *mti;
255 off = lsd->lsd_client_start +
256 cl_idx * lsd->lsd_client_size;
258 rc = mdt_last_rcvd_read(env, mdt, lcd, &off, cl_idx);
260 CERROR("error reading MDS %s idx %d, off %llu: rc %d\n",
261 LAST_RCVD, cl_idx, off, rc);
263 break; /* read error shouldn't cause startup to fail */
266 if (lcd->lcd_uuid[0] == '\0') {
267 CDEBUG(D_INFO, "skipping zeroed client at offset %d\n",
272 last_transno = lcd_last_transno(lcd);
274 /* These exports are cleaned up by mdt_obd_disconnect(), so
275 * they need to be set up like real exports as
276 * mdt_obd_connect() does.
278 CDEBUG(D_HA, "RCVRNG CLIENT uuid: %s idx: %d lr: "LPU64
279 " srv lr: "LPU64" lx: "LPU64"\n", lcd->lcd_uuid, cl_idx,
280 last_transno, lsd->lsd_last_transno,
283 exp = class_new_export(obd, (struct obd_uuid *)lcd->lcd_uuid);
285 if (PTR_ERR(exp) == -EALREADY) {
286 /* export already exists, zero out this one */
287 CERROR("Duplicate export %s!\n", lcd->lcd_uuid);
290 GOTO(err_client, rc = PTR_ERR(exp));
293 mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
294 LASSERT(mti != NULL);
296 /* copy on-disk lcd to the export */
297 *exp->exp_target_data.ted_lcd = *lcd;
298 rc = mdt_client_add(env, mdt, cl_idx);
299 /* can't fail existing */
300 LASSERTF(rc == 0, "rc = %d\n", rc);
301 /* VBR: set export last committed version */
302 exp->exp_last_committed = last_transno;
303 cfs_spin_lock(&exp->exp_lock);
304 exp->exp_connecting = 0;
305 exp->exp_in_recovery = 0;
306 cfs_spin_unlock(&exp->exp_lock);
307 obd->obd_max_recoverable_clients++;
308 class_export_put(exp);
310 CDEBUG(D_OTHER, "client at idx %d has last_transno="LPU64"\n",
311 cl_idx, last_transno);
312 /* protect __u64 value update */
313 cfs_spin_lock(&mdt->mdt_lut.lut_translock);
314 mdt->mdt_lut.lut_last_transno = max(last_transno,
315 mdt->mdt_lut.lut_last_transno);
316 cfs_spin_unlock(&mdt->mdt_lut.lut_translock);
324 static int mdt_server_data_init(const struct lu_env *env,
325 struct mdt_device *mdt,
326 struct lustre_sb_info *lsi)
328 struct lr_server_data *lsd = &mdt->mdt_lut.lut_lsd;
329 struct lsd_client_data *lcd = NULL;
330 struct obd_device *obd = mdt2obd_dev(mdt);
331 struct mdt_thread_info *mti;
332 struct dt_object *obj;
334 struct lustre_disk_data *ldd;
335 unsigned long last_rcvd_size;
340 /* ensure padding in the struct is the correct size */
341 CLASSERT(offsetof(struct lr_server_data, lsd_padding) +
342 sizeof(lsd->lsd_padding) == LR_SERVER_SIZE);
343 CLASSERT(offsetof(struct lsd_client_data, lcd_padding) +
344 sizeof(lcd->lcd_padding) == LR_CLIENT_SIZE);
346 mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
347 LASSERT(mti != NULL);
348 la = &mti->mti_attr.ma_attr;
350 obj = mdt->mdt_lut.lut_last_rcvd;
351 rc = obj->do_ops->do_attr_get(env, obj, la, BYPASS_CAPA);
355 last_rcvd_size = (unsigned long)la->la_size;
357 if (last_rcvd_size == 0) {
358 LCONSOLE_WARN("%s: new disk, initializing\n", obd->obd_name);
360 memcpy(lsd->lsd_uuid, obd->obd_uuid.uuid,
361 sizeof(lsd->lsd_uuid));
362 lsd->lsd_last_transno = 0;
363 lsd->lsd_mount_count = 0;
364 lsd->lsd_server_size = LR_SERVER_SIZE;
365 lsd->lsd_client_start = LR_CLIENT_START;
366 lsd->lsd_client_size = LR_CLIENT_SIZE;
367 lsd->lsd_feature_compat = OBD_COMPAT_MDT;
368 lsd->lsd_feature_rocompat = OBD_ROCOMPAT_LOVOBJID;
369 lsd->lsd_feature_incompat = OBD_INCOMPAT_MDT |
370 OBD_INCOMPAT_COMMON_LR |
371 OBD_INCOMPAT_MULTI_OI;
373 LCONSOLE_WARN("%s: used disk, loading\n", obd->obd_name);
374 rc = mdt_last_rcvd_header_read(env, mdt);
376 CERROR("error reading MDS %s: rc %d\n", LAST_RCVD, rc);
379 if (strcmp(lsd->lsd_uuid, obd->obd_uuid.uuid) != 0) {
380 LCONSOLE_ERROR_MSG(0x157, "Trying to start OBD %s using"
381 "the wrong disk %s. Were the /dev/ "
382 "assignments rearranged?\n",
383 obd->obd_uuid.uuid, lsd->lsd_uuid);
384 GOTO(out, rc = -EINVAL);
386 lsd->lsd_feature_compat |= OBD_COMPAT_MDT;
387 lsd->lsd_feature_incompat |= OBD_INCOMPAT_MDT |
388 OBD_INCOMPAT_COMMON_LR;
390 mount_count = lsd->lsd_mount_count;
394 if (lsd->lsd_feature_incompat & ~MDT_INCOMPAT_SUPP) {
395 CERROR("%s: unsupported incompat filesystem feature(s) %x\n",
397 lsd->lsd_feature_incompat & ~MDT_INCOMPAT_SUPP);
398 GOTO(out, rc = -EINVAL);
400 if (lsd->lsd_feature_rocompat & ~MDT_ROCOMPAT_SUPP) {
401 CERROR("%s: unsupported read-only filesystem feature(s) %x\n",
403 lsd->lsd_feature_rocompat & ~MDT_ROCOMPAT_SUPP);
404 /* XXX: Do something like remount filesystem read-only */
405 GOTO(out, rc = -EINVAL);
407 /** Interop: evict all clients at first boot with 1.8 last_rcvd */
408 if (!(lsd->lsd_feature_compat & OBD_COMPAT_20)) {
409 if (last_rcvd_size > lsd->lsd_client_start) {
410 LCONSOLE_WARN("Mounting %s at first time on 1.8 FS, "
411 "remove all clients for interop needs\n",
413 simple_truncate(lsi->lsi_srv_mnt->mnt_sb->s_root,
414 lsi->lsi_srv_mnt, LAST_RCVD,
415 lsd->lsd_client_start);
416 last_rcvd_size = lsd->lsd_client_start;
418 /** set 2.0 flag to upgrade/downgrade between 1.8 and 2.0 */
419 lsd->lsd_feature_compat |= OBD_COMPAT_20;
422 if (ldd->ldd_flags & LDD_F_IAM_DIR)
423 lsd->lsd_feature_incompat |= OBD_INCOMPAT_IAM_DIR;
425 lsd->lsd_feature_incompat |= OBD_INCOMPAT_FID;
427 cfs_spin_lock(&mdt->mdt_lut.lut_translock);
428 mdt->mdt_lut.lut_last_transno = lsd->lsd_last_transno;
429 cfs_spin_unlock(&mdt->mdt_lut.lut_translock);
431 CDEBUG(D_INODE, "========BEGIN DUMPING LAST_RCVD========\n");
432 CDEBUG(D_INODE, "%s: server last_transno: "LPU64"\n",
433 obd->obd_name, mdt->mdt_lut.lut_last_transno);
434 CDEBUG(D_INODE, "%s: server mount_count: "LPU64"\n",
435 obd->obd_name, mount_count + 1);
436 CDEBUG(D_INODE, "%s: server data size: %u\n",
437 obd->obd_name, lsd->lsd_server_size);
438 CDEBUG(D_INODE, "%s: per-client data start: %u\n",
439 obd->obd_name, lsd->lsd_client_start);
440 CDEBUG(D_INODE, "%s: per-client data size: %u\n",
441 obd->obd_name, lsd->lsd_client_size);
442 CDEBUG(D_INODE, "%s: last_rcvd size: %lu\n",
443 obd->obd_name, last_rcvd_size);
444 CDEBUG(D_INODE, "%s: last_rcvd clients: %lu\n", obd->obd_name,
445 last_rcvd_size <= lsd->lsd_client_start ? 0 :
446 (last_rcvd_size - lsd->lsd_client_start) /
447 lsd->lsd_client_size);
448 CDEBUG(D_INODE, "========END DUMPING LAST_RCVD========\n");
450 if (!lsd->lsd_server_size || !lsd->lsd_client_start ||
451 !lsd->lsd_client_size) {
452 CERROR("Bad last_rcvd contents!\n");
453 GOTO(out, rc = -EINVAL);
456 rc = mdt_clients_data_init(env, mdt, last_rcvd_size);
458 GOTO(err_client, rc);
460 cfs_spin_lock(&mdt->mdt_lut.lut_translock);
461 /* obd_last_committed is used for compatibility
462 * with other lustre recovery code */
463 obd->obd_last_committed = mdt->mdt_lut.lut_last_transno;
464 cfs_spin_unlock(&mdt->mdt_lut.lut_translock);
466 obd->u.obt.obt_mount_count = mount_count + 1;
467 obd->u.obt.obt_instance = (__u32)obd->u.obt.obt_mount_count;
468 lsd->lsd_mount_count = obd->u.obt.obt_mount_count;
470 /* save it, so mount count and last_transno is current */
471 rc = mdt_server_data_update(env, mdt);
473 GOTO(err_client, rc);
478 class_disconnect_exports(obd);
483 static int mdt_server_data_update(const struct lu_env *env,
484 struct mdt_device *mdt)
486 struct mdt_thread_info *mti;
490 mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
492 th = mdt_trans_create(env, mdt);
496 rc = mdt_declare_last_rcvd_header_write(env, mdt, th);
500 rc = mdt_trans_start(env, mdt, th);
504 CDEBUG(D_SUPER, "MDS mount_count is "LPU64", last_transno is "LPU64"\n",
505 mdt->mdt_lut.lut_obd->u.obt.obt_mount_count,
506 mdt->mdt_lut.lut_last_transno);
508 cfs_spin_lock(&mdt->mdt_lut.lut_translock);
509 mdt->mdt_lut.lut_lsd.lsd_last_transno = mdt->mdt_lut.lut_last_transno;
510 cfs_spin_unlock(&mdt->mdt_lut.lut_translock);
512 rc = mdt_last_rcvd_header_write(env, mdt, th);
515 mdt_trans_stop(env, mdt, th);
520 int mdt_client_new(const struct lu_env *env, struct mdt_device *mdt)
522 unsigned long *bitmap = mdt->mdt_lut.lut_client_bitmap;
523 struct mdt_thread_info *mti;
524 struct tg_export_data *ted;
525 struct lr_server_data *lsd = &mdt->mdt_lut.lut_lsd;
526 struct obd_device *obd = mdt2obd_dev(mdt);
533 mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
534 LASSERT(mti != NULL);
536 ted = &mti->mti_exp->exp_target_data;
538 LASSERT(bitmap != NULL);
539 if (!strcmp(ted->ted_lcd->lcd_uuid, obd->obd_uuid.uuid))
542 /* the bitmap operations can handle cl_idx > sizeof(long) * 8, so
543 * there's no need for extra complication here
545 cfs_spin_lock(&mdt->mdt_lut.lut_client_bitmap_lock);
546 cl_idx = cfs_find_first_zero_bit(bitmap, LR_MAX_CLIENTS);
547 if (cl_idx >= LR_MAX_CLIENTS ||
548 OBD_FAIL_CHECK(OBD_FAIL_MDS_CLIENT_ADD)) {
549 CERROR("no room for %u clients - fix LR_MAX_CLIENTS\n",
551 cfs_spin_unlock(&mdt->mdt_lut.lut_client_bitmap_lock);
554 cfs_set_bit(cl_idx, bitmap);
555 cfs_spin_unlock(&mdt->mdt_lut.lut_client_bitmap_lock);
557 CDEBUG(D_INFO, "client at idx %d with UUID '%s' added\n",
558 cl_idx, ted->ted_lcd->lcd_uuid);
560 ted->ted_lr_idx = cl_idx;
561 ted->ted_lr_off = lsd->lsd_client_start +
562 (cl_idx * lsd->lsd_client_size);
563 cfs_mutex_init(&ted->ted_lcd_lock);
565 LASSERTF(ted->ted_lr_off > 0, "ted_lr_off = %llu\n", ted->ted_lr_off);
567 /* Write new client data. */
568 off = ted->ted_lr_off;
570 if (OBD_FAIL_CHECK(OBD_FAIL_TGT_CLIENT_ADD))
573 th = mdt_trans_create(env, mdt);
577 rc = mdt_declare_last_rcvd_write(env, mdt, off, th);
581 rc = mdt_trans_start(env, mdt, th);
586 * Until this operations will be committed the sync is needed
587 * for this export. This should be done _after_ starting the
588 * transaction so that many connecting clients will not bring
589 * server down with lots of sync writes.
591 rc = lut_new_client_cb_add(th, mti->mti_exp);
593 /* can't add callback, do sync now */
596 cfs_spin_lock(&mti->mti_exp->exp_lock);
597 mti->mti_exp->exp_need_sync = 1;
598 cfs_spin_unlock(&mti->mti_exp->exp_lock);
601 rc = mdt_last_rcvd_write(env, mdt, ted->ted_lcd, &off, th);
602 CDEBUG(D_INFO, "wrote client lcd at idx %u off %llu (len %u)\n",
603 cl_idx, ted->ted_lr_off, (int)sizeof(*(ted->ted_lcd)));
606 mdt_trans_stop(env, mdt, th);
611 /* Add client data to the MDS. We use a bitmap to locate a free space
612 * in the last_rcvd file if cl_off is -1 (i.e. a new client).
613 * Otherwise, we just have to read the data from the last_rcvd file and
614 * we know its offset.
616 * It should not be possible to fail adding an existing client - otherwise
617 * mdt_init_server_data() callsite needs to be fixed.
619 int mdt_client_add(const struct lu_env *env,
620 struct mdt_device *mdt, int cl_idx)
622 struct mdt_thread_info *mti;
623 struct tg_export_data *ted;
624 unsigned long *bitmap = mdt->mdt_lut.lut_client_bitmap;
625 struct obd_device *obd = mdt2obd_dev(mdt);
626 struct lr_server_data *lsd = &mdt->mdt_lut.lut_lsd;
630 mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
631 LASSERT(mti != NULL);
633 ted = &mti->mti_exp->exp_target_data;
635 LASSERT(bitmap != NULL);
636 LASSERTF(cl_idx >= 0, "%d\n", cl_idx);
638 if (!strcmp(ted->ted_lcd->lcd_uuid, obd->obd_uuid.uuid))
641 cfs_spin_lock(&mdt->mdt_lut.lut_client_bitmap_lock);
642 if (cfs_test_and_set_bit(cl_idx, bitmap)) {
643 CERROR("MDS client %d: bit already set in bitmap!!\n",
647 cfs_spin_unlock(&mdt->mdt_lut.lut_client_bitmap_lock);
649 CDEBUG(D_INFO, "client at idx %d with UUID '%s' added\n",
650 cl_idx, ted->ted_lcd->lcd_uuid);
652 ted->ted_lr_idx = cl_idx;
653 ted->ted_lr_off = lsd->lsd_client_start +
654 (cl_idx * lsd->lsd_client_size);
655 cfs_mutex_init(&ted->ted_lcd_lock);
657 LASSERTF(ted->ted_lr_off > 0, "ted_lr_off = %llu\n", ted->ted_lr_off);
662 int mdt_client_del(const struct lu_env *env, struct mdt_device *mdt)
664 struct mdt_thread_info *mti;
665 struct tg_export_data *ted;
666 struct obd_device *obd = mdt2obd_dev(mdt);
667 struct obd_export *exp;
673 mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
674 LASSERT(mti != NULL);
677 ted = &exp->exp_target_data;
681 /* XXX: If lcd_uuid were a real obd_uuid, I could use obd_uuid_equals */
682 if (!strcmp(ted->ted_lcd->lcd_uuid, obd->obd_uuid.uuid))
685 CDEBUG(D_INFO, "freeing client at idx %u, offset %lld\n",
686 ted->ted_lr_idx, ted->ted_lr_off);
688 off = ted->ted_lr_off;
691 * Don't clear ted_lr_idx here as it is likely also unset. At worst we
692 * leak a client slot that will be cleaned on the next recovery.
695 CERROR("client idx %d has offset %lld\n",
696 ted->ted_lr_idx, off);
697 GOTO(free, rc = -EINVAL);
701 * Clear the bit _after_ zeroing out the client so we don't race with
702 * mdt_client_add and zero out new clients.
704 if (!cfs_test_bit(ted->ted_lr_idx, mdt->mdt_lut.lut_client_bitmap)) {
705 CERROR("MDT client %u: bit already clear in bitmap!!\n",
710 /* Make sure the server's last_transno is up to date.
711 * This should be done before zeroing client slot so last_transno will
712 * be in server data or in client data in case of failure */
713 mdt_server_data_update(env, mdt);
715 th = mdt_trans_create(env, mdt);
717 GOTO(free, rc = PTR_ERR(th));
719 rc = mdt_declare_last_rcvd_write(env, mdt, off, th);
723 rc = mdt_trans_start(env, mdt, th);
727 cfs_mutex_lock(&ted->ted_lcd_lock);
728 memset(ted->ted_lcd->lcd_uuid, 0, sizeof ted->ted_lcd->lcd_uuid);
729 rc = mdt_last_rcvd_write(env, mdt, ted->ted_lcd, &off, th);
730 cfs_mutex_unlock(&ted->ted_lcd_lock);
733 mdt_trans_stop(env, mdt, th);
735 CDEBUG(rc == 0 ? D_INFO : D_ERROR, "Zeroing out client idx %u in "
736 "%s, rc %d\n", ted->ted_lr_idx, LAST_RCVD, rc);
743 * last_rcvd & last_committed update callbacks
745 static int mdt_last_rcvd_update(struct mdt_thread_info *mti,
748 struct mdt_device *mdt = mti->mti_mdt;
749 struct ptlrpc_request *req = mdt_info_req(mti);
750 struct tg_export_data *ted;
751 struct lsd_client_data *lcd;
754 __s32 rc = th->th_result;
758 LASSERT(req->rq_export);
760 ted = &req->rq_export->exp_target_data;
763 cfs_mutex_lock(&ted->ted_lcd_lock);
765 /* if the export has already been disconnected, we have no last_rcvd slot,
766 * update server data with latest transno then */
768 cfs_mutex_unlock(&ted->ted_lcd_lock);
769 CWARN("commit transaction for disconnected client %s: rc %d\n",
770 req->rq_export->exp_client_uuid.uuid, rc);
771 err = mdt_last_rcvd_header_write(mti->mti_env, mdt, th);
775 off = ted->ted_lr_off;
776 LASSERT(ergo(mti->mti_transno == 0, rc != 0));
777 if (lustre_msg_get_opc(req->rq_reqmsg) == MDS_CLOSE ||
778 lustre_msg_get_opc(req->rq_reqmsg) == MDS_DONE_WRITING) {
779 if (mti->mti_transno != 0) {
780 if (lcd->lcd_last_close_transno > mti->mti_transno) {
781 CERROR("Trying to overwrite bigger transno:"
782 "on-disk: "LPU64", new: "LPU64" "
783 "replay: %d. see LU-617.\n",
784 lcd->lcd_last_close_transno,
785 mti->mti_transno, req_is_replay(req));
786 if (req_is_replay(req)) {
787 cfs_spin_lock(&req->rq_export->exp_lock);
788 req->rq_export->exp_vbr_failed = 1;
789 cfs_spin_unlock(&req->rq_export->exp_lock);
791 cfs_mutex_unlock(&ted->ted_lcd_lock);
792 RETURN(req_is_replay(req) ? -EOVERFLOW : 0);
794 lcd->lcd_last_close_transno = mti->mti_transno;
796 lcd->lcd_last_close_xid = req->rq_xid;
797 lcd->lcd_last_close_result = rc;
799 /* VBR: save versions in last_rcvd for reconstruct. */
800 __u64 *pre_versions = lustre_msg_get_versions(req->rq_repmsg);
802 lcd->lcd_pre_versions[0] = pre_versions[0];
803 lcd->lcd_pre_versions[1] = pre_versions[1];
804 lcd->lcd_pre_versions[2] = pre_versions[2];
805 lcd->lcd_pre_versions[3] = pre_versions[3];
807 if (mti->mti_transno != 0) {
808 if (lcd->lcd_last_transno > mti->mti_transno) {
809 CERROR("Trying to overwrite bigger transno:"
810 "on-disk: "LPU64", new: "LPU64" "
811 "replay: %d. see LU-617.\n",
812 lcd->lcd_last_transno,
813 mti->mti_transno, req_is_replay(req));
814 if (req_is_replay(req)) {
815 cfs_spin_lock(&req->rq_export->exp_lock);
816 req->rq_export->exp_vbr_failed = 1;
817 cfs_spin_unlock(&req->rq_export->exp_lock);
819 cfs_mutex_unlock(&ted->ted_lcd_lock);
820 RETURN(req_is_replay(req) ? -EOVERFLOW : 0);
822 lcd->lcd_last_transno = mti->mti_transno;
824 lcd->lcd_last_xid = req->rq_xid;
825 lcd->lcd_last_result = rc;
826 /*XXX: save intent_disposition in mdt_thread_info?
827 * also there is bug - intent_dispostion is __u64,
828 * see struct ldlm_reply->lock_policy_res1; */
829 lcd->lcd_last_data = mti->mti_opdata;
833 CERROR("client idx %d has offset %lld\n", ted->ted_lr_idx, off);
836 err = mdt_last_rcvd_write(mti->mti_env, mdt, lcd, &off, th);
838 cfs_mutex_unlock(&ted->ted_lcd_lock);
842 extern struct lu_context_key mdt_thread_key;
844 /* add credits for last_rcvd update */
845 static int mdt_txn_start_cb(const struct lu_env *env,
846 struct thandle *th, void *cookie)
848 struct mdt_device *mdt = cookie;
850 /* XXX: later we'll be declaring this at specific offset */
851 return dt_declare_record_write(env, mdt->mdt_lut.lut_last_rcvd,
852 sizeof(struct lsd_client_data), 0, th);
855 /* Update last_rcvd records with latests transaction data */
856 static int mdt_txn_stop_cb(const struct lu_env *env,
857 struct thandle *txn, void *cookie)
859 struct mdt_device *mdt = cookie;
860 struct mdt_thread_info *mti;
861 struct ptlrpc_request *req;
863 mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
864 req = mdt_info_req(mti);
866 if (mti->mti_mdt == NULL || req == NULL || mti->mti_no_need_trans) {
867 mti->mti_no_need_trans = 0;
871 if (mti->mti_has_trans) {
872 /* XXX: currently there are allowed cases, but the wrong cases
873 * are also possible, so better check is needed here */
874 CDEBUG(D_INFO, "More than one transaction "LPU64"\n",
879 mti->mti_has_trans = 1;
880 cfs_spin_lock(&mdt->mdt_lut.lut_translock);
881 if (txn->th_result != 0) {
882 if (mti->mti_transno != 0) {
883 CERROR("Replay transno "LPU64" failed: rc %d\n",
884 mti->mti_transno, txn->th_result);
886 } else if (mti->mti_transno == 0) {
887 mti->mti_transno = ++ mdt->mdt_lut.lut_last_transno;
889 /* should be replay */
890 if (mti->mti_transno > mdt->mdt_lut.lut_last_transno)
891 mdt->mdt_lut.lut_last_transno = mti->mti_transno;
893 cfs_spin_unlock(&mdt->mdt_lut.lut_translock);
894 /* sometimes the reply message has not been successfully packed */
895 LASSERT(req != NULL && req->rq_repmsg != NULL);
897 /** VBR: set new versions */
898 if (txn->th_result == 0 && mti->mti_mos != NULL) {
899 dt_version_set(env, mdt_obj2dt(mti->mti_mos),
900 mti->mti_transno, txn);
904 /* filling reply data */
905 CDEBUG(D_INODE, "transno = "LPU64", last_committed = "LPU64"\n",
906 mti->mti_transno, req->rq_export->exp_obd->obd_last_committed);
908 req->rq_transno = mti->mti_transno;
909 lustre_msg_set_transno(req->rq_repmsg, mti->mti_transno);
910 /* if can't add callback, do sync write */
911 txn->th_sync = !!lut_last_commit_cb_add(txn, &mdt->mdt_lut,
914 return mdt_last_rcvd_update(mti, txn);
917 int mdt_fs_setup(const struct lu_env *env, struct mdt_device *mdt,
918 struct obd_device *obd,
919 struct lustre_sb_info *lsi)
926 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_FS_SETUP))
929 /* prepare transactions callbacks */
930 mdt->mdt_txn_cb.dtc_txn_start = mdt_txn_start_cb;
931 mdt->mdt_txn_cb.dtc_txn_stop = mdt_txn_stop_cb;
932 mdt->mdt_txn_cb.dtc_txn_commit = NULL;
933 mdt->mdt_txn_cb.dtc_cookie = mdt;
934 mdt->mdt_txn_cb.dtc_tag = LCT_MD_THREAD;
935 CFS_INIT_LIST_HEAD(&mdt->mdt_txn_cb.dtc_linkage);
937 dt_txn_callback_add(mdt->mdt_bottom, &mdt->mdt_txn_cb);
939 rc = mdt_server_data_init(env, mdt, lsi);
943 o = dt_store_open(env, mdt->mdt_bottom, "", CAPA_KEYS, &fid);
946 rc = mdt_capa_keys_init(env, mdt);
948 GOTO(put_ck_object, rc);
951 CERROR("cannot open %s: rc = %d\n", CAPA_KEYS, rc);
952 GOTO(disconnect_exports, rc);
957 lu_object_put(env, &o->do_lu);
958 mdt->mdt_ck_obj = NULL;
960 class_disconnect_exports(obd);
964 void mdt_fs_cleanup(const struct lu_env *env, struct mdt_device *mdt)
968 /* Remove transaction callback */
969 dt_txn_callback_del(mdt->mdt_bottom, &mdt->mdt_txn_cb);
971 lu_object_put(env, &mdt->mdt_ck_obj->do_lu);
972 mdt->mdt_ck_obj = NULL;
976 /* reconstruction code */
977 static void mdt_steal_ack_locks(struct ptlrpc_request *req)
979 struct obd_export *exp = req->rq_export;
981 struct ptlrpc_reply_state *oldrep;
982 struct ptlrpc_service *svc;
985 /* CAVEAT EMPTOR: spinlock order */
986 cfs_spin_lock(&exp->exp_lock);
987 cfs_list_for_each (tmp, &exp->exp_outstanding_replies) {
988 oldrep = cfs_list_entry(tmp, struct ptlrpc_reply_state,
991 if (oldrep->rs_xid != req->rq_xid)
994 if (oldrep->rs_opc != lustre_msg_get_opc(req->rq_reqmsg))
995 CERROR ("Resent req xid "LPU64" has mismatched opc: "
996 "new %d old %d\n", req->rq_xid,
997 lustre_msg_get_opc(req->rq_reqmsg),
1000 svc = oldrep->rs_service;
1001 cfs_spin_lock (&svc->srv_rs_lock);
1003 cfs_list_del_init (&oldrep->rs_exp_list);
1005 CWARN("Stealing %d locks from rs %p x"LPD64".t"LPD64
1007 oldrep->rs_nlocks, oldrep,
1008 oldrep->rs_xid, oldrep->rs_transno, oldrep->rs_opc,
1009 libcfs_nid2str(exp->exp_connection->c_peer.nid));
1011 for (i = 0; i < oldrep->rs_nlocks; i++)
1012 ptlrpc_save_lock(req, &oldrep->rs_locks[i],
1013 oldrep->rs_modes[i], 0);
1014 oldrep->rs_nlocks = 0;
1016 DEBUG_REQ(D_HA, req, "stole locks for");
1017 cfs_spin_lock(&oldrep->rs_lock);
1018 ptlrpc_schedule_difficult_reply (oldrep);
1019 cfs_spin_unlock(&oldrep->rs_lock);
1021 cfs_spin_unlock (&svc->srv_rs_lock);
1024 cfs_spin_unlock(&exp->exp_lock);
1028 * VBR: restore versions
1030 void mdt_vbr_reconstruct(struct ptlrpc_request *req,
1031 struct lsd_client_data *lcd)
1033 __u64 pre_versions[4] = {0};
1034 pre_versions[0] = lcd->lcd_pre_versions[0];
1035 pre_versions[1] = lcd->lcd_pre_versions[1];
1036 pre_versions[2] = lcd->lcd_pre_versions[2];
1037 pre_versions[3] = lcd->lcd_pre_versions[3];
1038 lustre_msg_set_versions(req->rq_repmsg, pre_versions);
1041 void mdt_req_from_lcd(struct ptlrpc_request *req,
1042 struct lsd_client_data *lcd)
1044 DEBUG_REQ(D_HA, req, "restoring transno "LPD64"/status %d",
1045 lcd->lcd_last_transno, lcd->lcd_last_result);
1047 if (lustre_msg_get_opc(req->rq_reqmsg) == MDS_CLOSE ||
1048 lustre_msg_get_opc(req->rq_repmsg) == MDS_DONE_WRITING) {
1049 req->rq_transno = lcd->lcd_last_close_transno;
1050 req->rq_status = lcd->lcd_last_close_result;
1052 req->rq_transno = lcd->lcd_last_transno;
1053 req->rq_status = lcd->lcd_last_result;
1054 mdt_vbr_reconstruct(req, lcd);
1056 if (req->rq_status != 0)
1057 req->rq_transno = 0;
1058 lustre_msg_set_transno(req->rq_repmsg, req->rq_transno);
1059 lustre_msg_set_status(req->rq_repmsg, req->rq_status);
1060 DEBUG_REQ(D_RPCTRACE, req, "restoring transno "LPD64"/status %d",
1061 req->rq_transno, req->rq_status);
1063 mdt_steal_ack_locks(req);
1066 void mdt_reconstruct_generic(struct mdt_thread_info *mti,
1067 struct mdt_lock_handle *lhc)
1069 struct ptlrpc_request *req = mdt_info_req(mti);
1070 struct tg_export_data *ted = &req->rq_export->exp_target_data;
1072 return mdt_req_from_lcd(req, ted->ted_lcd);
1075 static void mdt_reconstruct_create(struct mdt_thread_info *mti,
1076 struct mdt_lock_handle *lhc)
1078 struct ptlrpc_request *req = mdt_info_req(mti);
1079 struct obd_export *exp = req->rq_export;
1080 struct tg_export_data *ted = &exp->exp_target_data;
1081 struct mdt_device *mdt = mti->mti_mdt;
1082 struct mdt_object *child;
1083 struct mdt_body *body;
1086 mdt_req_from_lcd(req, ted->ted_lcd);
1090 /* if no error, so child was created with requested fid */
1091 child = mdt_object_find(mti->mti_env, mdt, mti->mti_rr.rr_fid2);
1092 if (IS_ERR(child)) {
1093 rc = PTR_ERR(child);
1094 LCONSOLE_WARN("Child "DFID" lookup error %d."
1095 " Evicting client %s with export %s.\n",
1096 PFID(mdt_object_fid(child)), rc,
1097 obd_uuid2str(&exp->exp_client_uuid),
1098 obd_export_nid2str(exp));
1099 mdt_export_evict(exp);
1104 body = req_capsule_server_get(mti->mti_pill, &RMF_MDT_BODY);
1105 mti->mti_attr.ma_need = MA_INODE;
1106 mti->mti_attr.ma_valid = 0;
1107 rc = mo_attr_get(mti->mti_env, mdt_object_child(child), &mti->mti_attr);
1108 if (rc == -EREMOTE) {
1109 /* object was created on remote server */
1110 req->rq_status = rc;
1111 body->valid |= OBD_MD_MDS;
1113 mdt_pack_attr2body(mti, body, &mti->mti_attr.ma_attr,
1114 mdt_object_fid(child));
1115 mdt_object_put(mti->mti_env, child);
1118 static void mdt_reconstruct_setattr(struct mdt_thread_info *mti,
1119 struct mdt_lock_handle *lhc)
1121 struct ptlrpc_request *req = mdt_info_req(mti);
1122 struct obd_export *exp = req->rq_export;
1123 struct mdt_export_data *med = &exp->exp_mdt_data;
1124 struct mdt_device *mdt = mti->mti_mdt;
1125 struct mdt_object *obj;
1126 struct mdt_body *body;
1128 mdt_req_from_lcd(req, med->med_ted.ted_lcd);
1132 body = req_capsule_server_get(mti->mti_pill, &RMF_MDT_BODY);
1133 obj = mdt_object_find(mti->mti_env, mdt, mti->mti_rr.rr_fid1);
1135 int rc = PTR_ERR(obj);
1136 LCONSOLE_WARN(""DFID" lookup error %d."
1137 " Evicting client %s with export %s.\n",
1138 PFID(mdt_object_fid(obj)), rc,
1139 obd_uuid2str(&exp->exp_client_uuid),
1140 obd_export_nid2str(exp));
1141 mdt_export_evict(exp);
1145 mti->mti_attr.ma_need = MA_INODE;
1146 mti->mti_attr.ma_valid = 0;
1147 mo_attr_get(mti->mti_env, mdt_object_child(obj), &mti->mti_attr);
1148 mdt_pack_attr2body(mti, body, &mti->mti_attr.ma_attr,
1149 mdt_object_fid(obj));
1150 if (mti->mti_ioepoch && (mti->mti_ioepoch->flags & MF_EPOCH_OPEN)) {
1151 struct mdt_file_data *mfd;
1152 struct mdt_body *repbody;
1154 repbody = req_capsule_server_get(mti->mti_pill, &RMF_MDT_BODY);
1155 repbody->ioepoch = obj->mot_ioepoch;
1156 cfs_spin_lock(&med->med_open_lock);
1157 cfs_list_for_each_entry(mfd, &med->med_open_head, mfd_list) {
1158 if (mfd->mfd_xid == req->rq_xid)
1161 LASSERT(&mfd->mfd_list != &med->med_open_head);
1162 cfs_spin_unlock(&med->med_open_lock);
1163 repbody->handle.cookie = mfd->mfd_handle.h_cookie;
1166 mdt_object_put(mti->mti_env, obj);
1169 typedef void (*mdt_reconstructor)(struct mdt_thread_info *mti,
1170 struct mdt_lock_handle *lhc);
1172 static mdt_reconstructor reconstructors[REINT_MAX] = {
1173 [REINT_SETATTR] = mdt_reconstruct_setattr,
1174 [REINT_CREATE] = mdt_reconstruct_create,
1175 [REINT_LINK] = mdt_reconstruct_generic,
1176 [REINT_UNLINK] = mdt_reconstruct_generic,
1177 [REINT_RENAME] = mdt_reconstruct_generic,
1178 [REINT_OPEN] = mdt_reconstruct_open,
1179 [REINT_SETXATTR] = mdt_reconstruct_generic
1182 void mdt_reconstruct(struct mdt_thread_info *mti,
1183 struct mdt_lock_handle *lhc)
1186 reconstructors[mti->mti_rr.rr_opcode](mti, lhc);