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,
55 struct lu_buf *mdt_buf(const struct lu_env *env, void *area, ssize_t len)
58 struct mdt_thread_info *mti;
60 mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
67 const struct lu_buf *mdt_buf_const(const struct lu_env *env,
68 const void *area, ssize_t len)
71 struct mdt_thread_info *mti;
73 mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
76 buf->lb_buf = (void *)area;
81 static inline int mdt_trans_credit_get(const struct lu_env *env,
82 struct mdt_device *mdt,
85 struct dt_device *dev = mdt->mdt_bottom;
88 case MDT_TXN_CAPA_KEYS_WRITE_OP:
89 case MDT_TXN_LAST_RCVD_WRITE_OP:
90 cr = dev->dd_ops->dt_credit_get(env,
100 void mdt_trans_credit_init(const struct lu_env *env,
101 struct mdt_device *mdt,
104 struct mdt_thread_info *mti;
108 mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
109 p = &mti->mti_txn_param;
111 cr = mdt_trans_credit_get(env, mdt, op);
112 txn_param_init(p, cr);
115 struct thandle* mdt_trans_start(const struct lu_env *env,
116 struct mdt_device *mdt)
118 struct mdt_thread_info *mti;
121 mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
122 p = &mti->mti_txn_param;
124 /* export can require sync operations */
125 if (mti->mti_exp != NULL)
126 p->tp_sync = mti->mti_exp->exp_need_sync;
128 return mdt->mdt_bottom->dd_ops->dt_trans_start(env, mdt->mdt_bottom, p);
131 void mdt_trans_stop(const struct lu_env *env,
132 struct mdt_device *mdt, struct thandle *th)
134 mdt->mdt_bottom->dd_ops->dt_trans_stop(env, th);
137 static inline int mdt_last_rcvd_header_read(const struct lu_env *env,
138 struct mdt_device *mdt)
140 struct mdt_thread_info *mti;
143 mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
146 rc = dt_record_read(env, mdt->mdt_last_rcvd,
147 mdt_buf(env, &mti->mti_lsd, sizeof(mti->mti_lsd)),
150 lsd_le_to_cpu(&mti->mti_lsd, &mdt->mdt_lsd);
152 CDEBUG(D_INFO, "read last_rcvd header rc = %d:\n"
154 "last_transno = "LPU64"\n",
155 rc, mdt->mdt_lsd.lsd_uuid,
156 mdt->mdt_lsd.lsd_last_transno);
160 static inline int mdt_last_rcvd_header_write(const struct lu_env *env,
161 struct mdt_device *mdt,
164 struct mdt_thread_info *mti;
169 mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
172 spin_lock(&mti->mti_exp->exp_lock);
173 mti->mti_exp->exp_need_sync = need_sync;
174 spin_unlock(&mti->mti_exp->exp_lock);
176 mdt_trans_credit_init(env, mdt, MDT_TXN_LAST_RCVD_WRITE_OP);
177 th = mdt_trans_start(env, mdt);
182 lsd_cpu_to_le(&mdt->mdt_lsd, &mti->mti_lsd);
184 if (need_sync && mti->mti_exp)
185 mdt_trans_add_cb(th, lut_cb_client, mti->mti_exp);
187 rc = dt_record_write(env, mdt->mdt_last_rcvd,
188 mdt_buf_const(env, &mti->mti_lsd,
189 sizeof(mti->mti_lsd)),
192 mdt_trans_stop(env, mdt, th);
194 CDEBUG(D_INFO, "write last_rcvd header rc = %d:\n"
195 "uuid = %s\nlast_transno = "LPU64"\n",
196 rc, mdt->mdt_lsd.lsd_uuid, mdt->mdt_lsd.lsd_last_transno);
201 static int mdt_last_rcvd_read(const struct lu_env *env,
202 struct mdt_device *mdt,
203 struct lsd_client_data *lcd, loff_t *off)
205 struct mdt_thread_info *mti;
206 struct lsd_client_data *tmp;
209 mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
211 rc = dt_record_read(env, mdt->mdt_last_rcvd,
212 mdt_buf(env, tmp, sizeof(*tmp)), off);
214 lcd_le_to_cpu(tmp, lcd);
216 CDEBUG(D_INFO, "read lcd @%d rc = %d:\n"
218 "last_transno = "LPU64"\n"
219 "last_xid = "LPU64"\n"
222 "last_close_transno = "LPU64"\n"
223 "last_close_xid = "LPU64"\n"
224 "last_close_result = %u\n",
225 (int)(*off - sizeof(*tmp)),
228 lcd->lcd_last_transno,
230 lcd->lcd_last_result,
232 lcd->lcd_last_close_transno,
233 lcd->lcd_last_close_xid,
234 lcd->lcd_last_close_result);
238 static int mdt_last_rcvd_write(const struct lu_env *env,
239 struct mdt_device *mdt,
240 struct lsd_client_data *lcd,
241 loff_t *off, struct thandle *th)
243 struct mdt_thread_info *mti;
244 struct lsd_client_data *tmp;
248 mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
251 lcd_cpu_to_le(lcd, tmp);
253 rc = dt_record_write(env, mdt->mdt_last_rcvd,
254 mdt_buf_const(env, tmp, sizeof(*tmp)), off, th);
256 CDEBUG(D_INFO, "write lcd @%d rc = %d:\n"
258 "last_transno = "LPU64"\n"
259 "last_xid = "LPU64"\n"
262 "last_close_transno = "LPU64"\n"
263 "last_close_xid = "LPU64"\n"
264 "last_close_result = %u\n",
265 (int)(*off - sizeof(*tmp)),
268 lcd->lcd_last_transno,
270 lcd->lcd_last_result,
272 lcd->lcd_last_close_transno,
273 lcd->lcd_last_close_xid,
274 lcd->lcd_last_close_result);
278 static int mdt_clients_data_init(const struct lu_env *env,
279 struct mdt_device *mdt,
280 unsigned long last_size)
282 struct lr_server_data *lsd = &mdt->mdt_lsd;
283 struct lsd_client_data *lcd = NULL;
284 struct obd_device *obd = mdt2obd_dev(mdt);
290 /* When we do a clean MDS shutdown, we save the last_transno into
291 * the header. If we find clients with higher last_transno values
292 * then those clients may need recovery done. */
293 LASSERT(atomic_read(&obd->obd_req_replay_clients) == 0);
294 for (cl_idx = 0, off = lsd->lsd_client_start;
295 off < last_size; cl_idx++) {
297 struct obd_export *exp;
305 off = lsd->lsd_client_start +
306 cl_idx * lsd->lsd_client_size;
308 rc = mdt_last_rcvd_read(env, mdt, lcd, &off);
310 CERROR("error reading MDS %s idx %d, off %llu: rc %d\n",
311 LAST_RCVD, cl_idx, off, rc);
313 break; /* read error shouldn't cause startup to fail */
316 if (lcd->lcd_uuid[0] == '\0') {
317 CDEBUG(D_INFO, "skipping zeroed client at offset %d\n",
322 last_transno = lcd_last_transno(lcd);
324 /* These exports are cleaned up by mdt_obd_disconnect(), so
325 * they need to be set up like real exports as
326 * mdt_obd_connect() does.
328 CDEBUG(D_HA, "RCVRNG CLIENT uuid: %s idx: %d lr: "LPU64
329 " srv lr: "LPU64" lx: "LPU64"\n", lcd->lcd_uuid, cl_idx,
330 last_transno, lsd->lsd_last_transno,
333 exp = class_new_export(obd, (struct obd_uuid *)lcd->lcd_uuid);
335 if (PTR_ERR(exp) == -EALREADY) {
336 /* export already exists, zero out this one */
337 lcd->lcd_uuid[0] = '\0';
339 GOTO(err_client, rc = PTR_ERR(exp));
341 struct mdt_thread_info *mti;
342 mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
343 LASSERT(mti != NULL);
345 exp->exp_mdt_data.med_lcd = lcd;
346 rc = mdt_client_add(env, mdt, cl_idx);
347 /* can't fail existing */
348 LASSERTF(rc == 0, "rc = %d\n", rc);
349 /* VBR: set export last committed version */
350 exp->exp_last_committed = last_transno;
352 spin_lock(&exp->exp_lock);
353 exp->exp_connecting = 0;
354 exp->exp_in_recovery = 0;
355 spin_unlock(&exp->exp_lock);
356 obd->obd_max_recoverable_clients++;
357 class_export_put(exp);
360 CDEBUG(D_OTHER, "client at idx %d has last_transno="LPU64"\n",
361 cl_idx, last_transno);
362 /* protect __u64 value update */
363 spin_lock(&mdt->mdt_transno_lock);
364 mdt->mdt_last_transno = max(last_transno,
365 mdt->mdt_last_transno);
366 spin_unlock(&mdt->mdt_transno_lock);
375 static int mdt_server_data_init(const struct lu_env *env,
376 struct mdt_device *mdt,
377 struct lustre_sb_info *lsi)
379 struct lr_server_data *lsd = &mdt->mdt_lsd;
380 struct lsd_client_data *lcd = NULL;
381 struct obd_device *obd = mdt2obd_dev(mdt);
382 struct mdt_thread_info *mti;
383 struct dt_object *obj;
385 struct lustre_disk_data *ldd;
386 unsigned long last_rcvd_size;
391 /* ensure padding in the struct is the correct size */
392 CLASSERT(offsetof(struct lr_server_data, lsd_padding) +
393 sizeof(lsd->lsd_padding) == LR_SERVER_SIZE);
394 CLASSERT(offsetof(struct lsd_client_data, lcd_padding) +
395 sizeof(lcd->lcd_padding) == LR_CLIENT_SIZE);
397 mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
398 LASSERT(mti != NULL);
399 la = &mti->mti_attr.ma_attr;
401 obj = mdt->mdt_last_rcvd;
402 rc = obj->do_ops->do_attr_get(env, mdt->mdt_last_rcvd, la, BYPASS_CAPA);
406 last_rcvd_size = (unsigned long)la->la_size;
408 if (last_rcvd_size == 0) {
409 LCONSOLE_WARN("%s: new disk, initializing\n", obd->obd_name);
411 memcpy(lsd->lsd_uuid, obd->obd_uuid.uuid,
412 sizeof(lsd->lsd_uuid));
413 lsd->lsd_last_transno = 0;
414 lsd->lsd_mount_count = 0;
415 lsd->lsd_server_size = LR_SERVER_SIZE;
416 lsd->lsd_client_start = LR_CLIENT_START;
417 lsd->lsd_client_size = LR_CLIENT_SIZE;
418 lsd->lsd_feature_rocompat = OBD_ROCOMPAT_LOVOBJID;
419 lsd->lsd_feature_incompat = OBD_INCOMPAT_MDT |
420 OBD_INCOMPAT_COMMON_LR;
422 LCONSOLE_WARN("%s: used disk, loading\n", obd->obd_name);
423 rc = mdt_last_rcvd_header_read(env, mdt);
425 CERROR("error reading MDS %s: rc %d\n", LAST_RCVD, rc);
428 if (strcmp(lsd->lsd_uuid, obd->obd_uuid.uuid) != 0) {
429 LCONSOLE_ERROR_MSG(0x157, "Trying to start OBD %s using"
430 "the wrong disk %s. Were the /dev/ "
431 "assignments rearranged?\n",
432 obd->obd_uuid.uuid, lsd->lsd_uuid);
433 GOTO(out, rc = -EINVAL);
436 /** evict all clients as it is first boot with old last_rcvd */
437 if (!(lsd->lsd_feature_incompat & OBD_INCOMPAT_20)) {
438 LCONSOLE_WARN("Mounting %s at first time on old FS, "
439 "remove all clients for interop needs\n",
441 simple_truncate(lsi->lsi_srv_mnt->mnt_sb->s_root,
442 lsi->lsi_srv_mnt, LAST_RCVD,
443 lsd->lsd_client_start);
444 last_rcvd_size = lsd->lsd_client_start;
447 mount_count = lsd->lsd_mount_count;
451 if (ldd->ldd_flags & LDD_F_IAM_DIR)
452 lsd->lsd_feature_incompat |= OBD_INCOMPAT_IAM_DIR;
454 lsd->lsd_feature_compat = 0;
455 lsd->lsd_feature_incompat |= OBD_INCOMPAT_FID | OBD_INCOMPAT_20;
457 spin_lock(&mdt->mdt_transno_lock);
458 mdt->mdt_last_transno = lsd->lsd_last_transno;
459 spin_unlock(&mdt->mdt_transno_lock);
461 CDEBUG(D_INODE, "========BEGIN DUMPING LAST_RCVD========\n");
462 CDEBUG(D_INODE, "%s: server last_transno: "LPU64"\n",
463 obd->obd_name, mdt->mdt_last_transno);
464 CDEBUG(D_INODE, "%s: server mount_count: "LPU64"\n",
465 obd->obd_name, mount_count + 1);
466 CDEBUG(D_INODE, "%s: server data size: %u\n",
467 obd->obd_name, lsd->lsd_server_size);
468 CDEBUG(D_INODE, "%s: per-client data start: %u\n",
469 obd->obd_name, lsd->lsd_client_start);
470 CDEBUG(D_INODE, "%s: per-client data size: %u\n",
471 obd->obd_name, lsd->lsd_client_size);
472 CDEBUG(D_INODE, "%s: last_rcvd size: %lu\n",
473 obd->obd_name, last_rcvd_size);
474 CDEBUG(D_INODE, "%s: last_rcvd clients: %lu\n", obd->obd_name,
475 last_rcvd_size <= lsd->lsd_client_start ? 0 :
476 (last_rcvd_size - lsd->lsd_client_start) /
477 lsd->lsd_client_size);
478 CDEBUG(D_INODE, "========END DUMPING LAST_RCVD========\n");
480 if (!lsd->lsd_server_size || !lsd->lsd_client_start ||
481 !lsd->lsd_client_size) {
482 CERROR("Bad last_rcvd contents!\n");
483 GOTO(out, rc = -EINVAL);
486 rc = mdt_clients_data_init(env, mdt, last_rcvd_size);
488 GOTO(err_client, rc);
490 spin_lock(&mdt->mdt_transno_lock);
491 /* obd_last_committed is used for compatibility
492 * with other lustre recovery code */
493 obd->obd_last_committed = mdt->mdt_last_transno;
494 spin_unlock(&mdt->mdt_transno_lock);
496 mdt->mdt_mount_count = mount_count + 1;
497 lsd->lsd_mount_count = mdt->mdt_mount_count;
499 /* save it, so mount count and last_transno is current */
500 rc = mdt_server_data_update(env, mdt, (mti->mti_exp &&
501 mti->mti_exp->exp_need_sync));
503 GOTO(err_client, rc);
508 class_disconnect_exports(obd);
513 static int mdt_server_data_update(const struct lu_env *env,
514 struct mdt_device *mdt,
520 CDEBUG(D_SUPER, "MDS mount_count is "LPU64", last_transno is "LPU64"\n",
521 mdt->mdt_mount_count, mdt->mdt_last_transno);
523 spin_lock(&mdt->mdt_transno_lock);
524 mdt->mdt_lsd.lsd_last_transno = mdt->mdt_last_transno;
525 spin_unlock(&mdt->mdt_transno_lock);
528 * This may be called from difficult reply handler and
529 * mdt->mdt_last_rcvd may be NULL that time.
531 if (mdt->mdt_last_rcvd != NULL)
532 rc = mdt_last_rcvd_header_write(env, mdt, need_sync);
536 int mdt_client_new(const struct lu_env *env, struct mdt_device *mdt)
538 unsigned long *bitmap = mdt->mdt_client_bitmap;
539 struct mdt_thread_info *mti;
540 struct mdt_export_data *med;
541 struct lsd_client_data *lcd;
542 struct lr_server_data *lsd = &mdt->mdt_lsd;
543 struct obd_device *obd = mdt2obd_dev(mdt);
550 mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
551 LASSERT(mti != NULL);
553 med = &mti->mti_exp->exp_mdt_data;
556 LASSERT(bitmap != NULL);
557 if (!strcmp(med->med_lcd->lcd_uuid, obd->obd_uuid.uuid))
560 /* the bitmap operations can handle cl_idx > sizeof(long) * 8, so
561 * there's no need for extra complication here
563 spin_lock(&mdt->mdt_client_bitmap_lock);
564 cl_idx = find_first_zero_bit(bitmap, LR_MAX_CLIENTS);
565 if (cl_idx >= LR_MAX_CLIENTS ||
566 OBD_FAIL_CHECK(OBD_FAIL_MDS_CLIENT_ADD)) {
567 CERROR("no room for %u clients - fix LR_MAX_CLIENTS\n",
569 spin_unlock(&mdt->mdt_client_bitmap_lock);
572 set_bit(cl_idx, bitmap);
573 spin_unlock(&mdt->mdt_client_bitmap_lock);
575 CDEBUG(D_INFO, "client at idx %d with UUID '%s' added\n",
576 cl_idx, med->med_lcd->lcd_uuid);
578 med->med_lr_idx = cl_idx;
579 med->med_lr_off = lsd->lsd_client_start +
580 (cl_idx * lsd->lsd_client_size);
581 init_mutex(&med->med_lcd_lock);
583 LASSERTF(med->med_lr_off > 0, "med_lr_off = %llu\n", med->med_lr_off);
585 /* Write new client data. */
586 off = med->med_lr_off;
587 mdt_trans_credit_init(env, mdt, MDT_TXN_LAST_RCVD_WRITE_OP);
589 th = mdt_trans_start(env, mdt);
594 * Until this operations will be committed the sync is needed
595 * for this export. This should be done _after_ starting the
596 * transaction so that many connecting clients will not bring
597 * server down with lots of sync writes.
599 mdt_trans_add_cb(th, lut_cb_client, mti->mti_exp);
600 spin_lock(&mti->mti_exp->exp_lock);
601 mti->mti_exp->exp_need_sync = 1;
602 spin_unlock(&mti->mti_exp->exp_lock);
604 rc = mdt_last_rcvd_write(env, mdt, lcd, &off, th);
605 CDEBUG(D_INFO, "wrote client lcd at idx %u off %llu (len "LPSZ")\n",
606 cl_idx, med->med_lr_off, sizeof(*lcd));
607 mdt_trans_stop(env, mdt, th);
612 /* Add client data to the MDS. We use a bitmap to locate a free space
613 * in the last_rcvd file if cl_off is -1 (i.e. a new client).
614 * Otherwise, we just have to read the data from the last_rcvd file and
615 * we know its offset.
617 * It should not be possible to fail adding an existing client - otherwise
618 * mdt_init_server_data() callsite needs to be fixed.
620 int mdt_client_add(const struct lu_env *env,
621 struct mdt_device *mdt, int cl_idx)
623 struct mdt_thread_info *mti;
624 struct mdt_export_data *med;
625 unsigned long *bitmap = mdt->mdt_client_bitmap;
626 struct obd_device *obd = mdt2obd_dev(mdt);
627 struct lr_server_data *lsd = &mdt->mdt_lsd;
631 mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
632 LASSERT(mti != NULL);
634 med = &mti->mti_exp->exp_mdt_data;
636 LASSERT(bitmap != NULL);
637 LASSERTF(cl_idx >= 0, "%d\n", cl_idx);
639 if (!strcmp(med->med_lcd->lcd_uuid, obd->obd_uuid.uuid))
642 spin_lock(&mdt->mdt_client_bitmap_lock);
643 if (test_and_set_bit(cl_idx, bitmap)) {
644 CERROR("MDS client %d: bit already set in bitmap!!\n",
648 spin_unlock(&mdt->mdt_client_bitmap_lock);
650 CDEBUG(D_INFO, "client at idx %d with UUID '%s' added\n",
651 cl_idx, med->med_lcd->lcd_uuid);
653 med->med_lr_idx = cl_idx;
654 med->med_lr_off = lsd->lsd_client_start +
655 (cl_idx * lsd->lsd_client_size);
656 init_mutex(&med->med_lcd_lock);
658 LASSERTF(med->med_lr_off > 0, "med_lr_off = %llu\n", med->med_lr_off);
663 int mdt_client_del(const struct lu_env *env, struct mdt_device *mdt)
665 struct mdt_thread_info *mti;
666 struct mdt_export_data *med;
667 struct lsd_client_data *lcd;
668 struct obd_device *obd = mdt2obd_dev(mdt);
669 struct obd_export *exp;
676 mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
677 LASSERT(mti != NULL);
680 med = &exp->exp_mdt_data;
685 /* XXX: If lcd_uuid were a real obd_uuid, I could use obd_uuid_equals */
686 if (!strcmp(lcd->lcd_uuid, obd->obd_uuid.uuid))
689 CDEBUG(D_INFO, "freeing client at idx %u, offset %lld\n",
690 med->med_lr_idx, med->med_lr_off);
692 off = med->med_lr_off;
695 * Don't clear med_lr_idx here as it is likely also unset. At worst we
696 * leak a client slot that will be cleaned on the next recovery.
699 CERROR("client idx %d has offset %lld\n",
700 med->med_lr_idx, off);
701 GOTO(free, rc = -EINVAL);
705 * Clear the bit _after_ zeroing out the client so we don't race with
706 * mdt_client_add and zero out new clients.
708 if (!test_bit(med->med_lr_idx, mdt->mdt_client_bitmap)) {
709 CERROR("MDT client %u: bit already clear in bitmap!!\n",
714 /* Don't force sync on disconnect if aborting recovery,
715 * or it does num_clients * num_osts. b=17194 */
716 need_sync = (!exp->exp_libclient || exp->exp_need_sync) &&
717 !(exp->exp_flags & OBD_OPT_ABORT_RECOV);
720 * This may be called from difficult reply handler path and
721 * mdt->mdt_last_rcvd may be NULL that time.
723 if (mdt->mdt_last_rcvd != NULL) {
724 mdt_trans_credit_init(env, mdt, MDT_TXN_LAST_RCVD_WRITE_OP);
726 spin_lock(&exp->exp_lock);
727 exp->exp_need_sync = need_sync;
728 spin_unlock(&exp->exp_lock);
730 th = mdt_trans_start(env, mdt);
732 GOTO(free, rc = PTR_ERR(th));
736 * Until this operations will be committed the sync
737 * is needed for this export.
739 mdt_trans_add_cb(th, lut_cb_client, exp);
742 mutex_down(&med->med_lcd_lock);
743 memset(lcd, 0, sizeof *lcd);
745 rc = mdt_last_rcvd_write(env, mdt, lcd, &off, th);
746 mutex_up(&med->med_lcd_lock);
747 mdt_trans_stop(env, mdt, th);
750 CDEBUG(rc == 0 ? D_INFO : D_ERROR, "Zeroing out client idx %u in "
751 "%s %ssync rc %d\n", med->med_lr_idx, LAST_RCVD,
752 need_sync ? "" : "a", rc);
754 spin_lock(&mdt->mdt_client_bitmap_lock);
755 clear_bit(med->med_lr_idx, mdt->mdt_client_bitmap);
756 spin_unlock(&mdt->mdt_client_bitmap_lock);
759 * Make sure the server's last_transno is up to date. Do this
760 * after the client is freed so we know all the client's
761 * transactions have been committed.
763 mdt_server_data_update(env, mdt, need_sync);
773 * last_rcvd & last_committed update callbacks
775 static int mdt_last_rcvd_update(struct mdt_thread_info *mti,
778 struct mdt_device *mdt = mti->mti_mdt;
779 struct ptlrpc_request *req = mdt_info_req(mti);
780 struct mdt_export_data *med;
781 struct lsd_client_data *lcd;
784 __s32 rc = th->th_result;
788 LASSERT(req->rq_export);
790 med = &req->rq_export->exp_mdt_data;
793 /* if the export has already been failed, we have no last_rcvd slot */
794 if (req->rq_export->exp_failed) {
795 CWARN("commit transaction for disconnected client %s: rc %d\n",
796 req->rq_export->exp_client_uuid.uuid, rc);
802 off = med->med_lr_off;
803 LASSERT(ergo(mti->mti_transno == 0, rc != 0));
804 mutex_down(&med->med_lcd_lock);
805 if (lustre_msg_get_opc(req->rq_reqmsg) == MDS_CLOSE ||
806 lustre_msg_get_opc(req->rq_reqmsg) == MDS_DONE_WRITING) {
807 if (mti->mti_transno != 0)
808 lcd->lcd_last_close_transno = mti->mti_transno;
809 lcd->lcd_last_close_xid = req->rq_xid;
810 lcd->lcd_last_close_result = rc;
812 /* VBR: save versions in last_rcvd for reconstruct. */
813 __u64 *pre_versions = lustre_msg_get_versions(req->rq_repmsg);
815 lcd->lcd_pre_versions[0] = pre_versions[0];
816 lcd->lcd_pre_versions[1] = pre_versions[1];
817 lcd->lcd_pre_versions[2] = pre_versions[2];
818 lcd->lcd_pre_versions[3] = pre_versions[3];
820 if (mti->mti_transno != 0)
821 lcd->lcd_last_transno = mti->mti_transno;
822 lcd->lcd_last_xid = req->rq_xid;
823 lcd->lcd_last_result = rc;
824 /*XXX: save intent_disposition in mdt_thread_info?
825 * also there is bug - intent_dispostion is __u64,
826 * see struct ldlm_reply->lock_policy_res1; */
827 lcd->lcd_last_data = mti->mti_opdata;
831 CERROR("client idx %d has offset %lld\n", med->med_lr_idx, off);
834 err = mdt_last_rcvd_write(mti->mti_env, mdt, lcd, &off, th);
836 mutex_up(&med->med_lcd_lock);
840 extern struct lu_context_key mdt_thread_key;
842 /* add credits for last_rcvd update */
843 static int mdt_txn_start_cb(const struct lu_env *env,
844 struct txn_param *param, void *cookie)
846 struct mdt_device *mdt = cookie;
848 param->tp_credits += mdt_trans_credit_get(env, mdt,
849 MDT_TXN_LAST_RCVD_WRITE_OP);
853 /* Set new object versions */
854 static void mdt_versions_set(struct mdt_thread_info *info)
857 for (i = 0; i < PTLRPC_NUM_VERSIONS; i++)
858 if (info->mti_mos[i] != NULL)
859 mo_version_set(info->mti_env,
860 mdt_object_child(info->mti_mos[i]),
864 /* Update last_rcvd records with latests transaction data */
865 static int mdt_txn_stop_cb(const struct lu_env *env,
866 struct thandle *txn, void *cookie)
868 struct mdt_device *mdt = cookie;
869 struct mdt_txn_info *txi;
870 struct mdt_thread_info *mti;
871 struct ptlrpc_request *req;
873 /* transno in two contexts - for commit_cb and for thread */
874 txi = lu_context_key_get(&txn->th_ctx, &mdt_txn_key);
875 mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
876 req = mdt_info_req(mti);
878 if (mti->mti_mdt == NULL || req == NULL || mti->mti_no_need_trans) {
879 txi->txi_transno = 0;
880 mti->mti_no_need_trans = 0;
884 if (mti->mti_has_trans) {
885 /* XXX: currently there are allowed cases, but the wrong cases
886 * are also possible, so better check is needed here */
887 CDEBUG(D_INFO, "More than one transaction "LPU64"\n",
892 mti->mti_has_trans = 1;
893 spin_lock(&mdt->mdt_transno_lock);
894 if (txn->th_result != 0) {
895 if (mti->mti_transno != 0) {
896 CERROR("Replay transno "LPU64" failed: rc %i\n",
897 mti->mti_transno, txn->th_result);
899 } else if (mti->mti_transno == 0) {
900 mti->mti_transno = ++ mdt->mdt_last_transno;
902 /* should be replay */
903 if (mti->mti_transno > mdt->mdt_last_transno)
904 mdt->mdt_last_transno = mti->mti_transno;
906 spin_unlock(&mdt->mdt_transno_lock);
907 /* sometimes the reply message has not been successfully packed */
908 LASSERT(req != NULL && req->rq_repmsg != NULL);
910 /** VBR: set new versions */
911 if (txn->th_result == 0)
912 mdt_versions_set(mti);
914 /* filling reply data */
915 CDEBUG(D_INODE, "transno = %llu, last_committed = %llu\n",
916 mti->mti_transno, req->rq_export->exp_obd->obd_last_committed);
918 req->rq_transno = mti->mti_transno;
919 lustre_msg_set_transno(req->rq_repmsg, mti->mti_transno);
920 lustre_msg_set_last_xid(req->rq_repmsg,
921 lcd_last_xid(req->rq_export->exp_mdt_data.med_lcd));
922 /* save transno for the commit callback */
923 txi->txi_transno = mti->mti_transno;
925 /* add separate commit callback for transaction handling because we need
926 * export as parameter */
927 mdt_trans_add_cb(txn, lut_cb_last_committed,
928 class_export_get(mti->mti_exp));
929 atomic_inc(&mti->mti_exp->exp_cb_count);
931 return mdt_last_rcvd_update(mti, txn);
934 /* commit callback, need to update last_committed value */
935 static int mdt_txn_commit_cb(const struct lu_env *env,
936 struct thandle *txn, void *cookie)
938 struct mdt_device *mdt = cookie;
939 struct mdt_txn_info *txi;
942 txi = lu_context_key_get(&txn->th_ctx, &mdt_txn_key);
944 /* iterate through all additional callbacks */
945 for (i = 0; i < txi->txi_cb_count; i++) {
946 txi->txi_cb[i].lut_cb_func(&mdt->mdt_lut, txi->txi_transno,
947 txi->txi_cb[i].lut_cb_data, 0);
952 int mdt_fs_setup(const struct lu_env *env, struct mdt_device *mdt,
953 struct obd_device *obd,
954 struct lustre_sb_info *lsi)
961 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_FS_SETUP))
964 /* prepare transactions callbacks */
965 mdt->mdt_txn_cb.dtc_txn_start = mdt_txn_start_cb;
966 mdt->mdt_txn_cb.dtc_txn_stop = mdt_txn_stop_cb;
967 mdt->mdt_txn_cb.dtc_txn_commit = mdt_txn_commit_cb;
968 mdt->mdt_txn_cb.dtc_cookie = mdt;
969 mdt->mdt_txn_cb.dtc_tag = LCT_MD_THREAD;
970 CFS_INIT_LIST_HEAD(&mdt->mdt_txn_cb.dtc_linkage);
972 dt_txn_callback_add(mdt->mdt_bottom, &mdt->mdt_txn_cb);
974 rc = mdt_server_data_init(env, mdt, lsi);
978 o = dt_store_open(env, mdt->mdt_bottom, "", CAPA_KEYS, &fid);
981 rc = mdt_capa_keys_init(env, mdt);
983 GOTO(put_ck_object, rc);
986 CERROR("cannot open %s: rc = %d\n", CAPA_KEYS, rc);
987 GOTO(disconnect_exports, rc);
992 lu_object_put(env, &o->do_lu);
993 mdt->mdt_ck_obj = NULL;
995 class_disconnect_exports(obd);
999 void mdt_fs_cleanup(const struct lu_env *env, struct mdt_device *mdt)
1003 /* Remove transaction callback */
1004 dt_txn_callback_del(mdt->mdt_bottom, &mdt->mdt_txn_cb);
1005 if (mdt->mdt_ck_obj)
1006 lu_object_put(env, &mdt->mdt_ck_obj->do_lu);
1007 mdt->mdt_ck_obj = NULL;
1011 /* reconstruction code */
1012 static void mdt_steal_ack_locks(struct ptlrpc_request *req)
1014 struct obd_export *exp = req->rq_export;
1015 struct list_head *tmp;
1016 struct ptlrpc_reply_state *oldrep;
1017 struct ptlrpc_service *svc;
1020 /* CAVEAT EMPTOR: spinlock order */
1021 spin_lock(&exp->exp_lock);
1022 list_for_each (tmp, &exp->exp_outstanding_replies) {
1023 oldrep = list_entry(tmp, struct ptlrpc_reply_state,rs_exp_list);
1025 if (oldrep->rs_xid != req->rq_xid)
1028 if (oldrep->rs_opc != lustre_msg_get_opc(req->rq_reqmsg))
1029 CERROR ("Resent req xid "LPU64" has mismatched opc: "
1030 "new %d old %d\n", req->rq_xid,
1031 lustre_msg_get_opc(req->rq_reqmsg),
1034 svc = oldrep->rs_service;
1035 spin_lock (&svc->srv_lock);
1037 list_del_init (&oldrep->rs_exp_list);
1039 CWARN("Stealing %d locks from rs %p x"LPD64".t"LPD64
1041 oldrep->rs_nlocks, oldrep,
1042 oldrep->rs_xid, oldrep->rs_transno, oldrep->rs_opc,
1043 libcfs_nid2str(exp->exp_connection->c_peer.nid));
1045 for (i = 0; i < oldrep->rs_nlocks; i++)
1046 ptlrpc_save_lock(req, &oldrep->rs_locks[i],
1047 oldrep->rs_modes[i], 0);
1048 oldrep->rs_nlocks = 0;
1050 DEBUG_REQ(D_HA, req, "stole locks for");
1051 spin_lock(&oldrep->rs_lock);
1052 ptlrpc_schedule_difficult_reply (oldrep);
1053 spin_unlock(&oldrep->rs_lock);
1055 spin_unlock (&svc->srv_lock);
1058 spin_unlock(&exp->exp_lock);
1062 * VBR: restore versions
1064 void mdt_vbr_reconstruct(struct ptlrpc_request *req,
1065 struct lsd_client_data *lcd)
1067 __u64 pre_versions[4] = {0};
1068 pre_versions[0] = lcd->lcd_pre_versions[0];
1069 pre_versions[1] = lcd->lcd_pre_versions[1];
1070 pre_versions[2] = lcd->lcd_pre_versions[2];
1071 pre_versions[3] = lcd->lcd_pre_versions[3];
1072 lustre_msg_set_versions(req->rq_repmsg, pre_versions);
1075 void mdt_req_from_lcd(struct ptlrpc_request *req,
1076 struct lsd_client_data *lcd)
1078 DEBUG_REQ(D_HA, req, "restoring transno "LPD64"/status %d",
1079 lcd->lcd_last_transno, lcd->lcd_last_result);
1081 if (lustre_msg_get_opc(req->rq_reqmsg) == MDS_CLOSE ||
1082 lustre_msg_get_opc(req->rq_repmsg) == MDS_DONE_WRITING) {
1083 req->rq_transno = lcd->lcd_last_close_transno;
1084 req->rq_status = lcd->lcd_last_close_result;
1086 req->rq_transno = lcd->lcd_last_transno;
1087 req->rq_status = lcd->lcd_last_result;
1088 mdt_vbr_reconstruct(req, lcd);
1090 if (req->rq_status != 0)
1091 req->rq_transno = 0;
1092 lustre_msg_set_transno(req->rq_repmsg, req->rq_transno);
1093 lustre_msg_set_status(req->rq_repmsg, req->rq_status);
1094 DEBUG_REQ(D_RPCTRACE, req, "restoring transno "LPD64"/status %d",
1095 req->rq_transno, req->rq_status);
1097 mdt_steal_ack_locks(req);
1100 void mdt_reconstruct_generic(struct mdt_thread_info *mti,
1101 struct mdt_lock_handle *lhc)
1103 struct ptlrpc_request *req = mdt_info_req(mti);
1104 struct mdt_export_data *med = &req->rq_export->exp_mdt_data;
1106 return mdt_req_from_lcd(req, med->med_lcd);
1109 static void mdt_reconstruct_create(struct mdt_thread_info *mti,
1110 struct mdt_lock_handle *lhc)
1112 struct ptlrpc_request *req = mdt_info_req(mti);
1113 struct obd_export *exp = req->rq_export;
1114 struct mdt_export_data *med = &exp->exp_mdt_data;
1115 struct mdt_device *mdt = mti->mti_mdt;
1116 struct mdt_object *child;
1117 struct mdt_body *body;
1120 mdt_req_from_lcd(req, med->med_lcd);
1124 /* if no error, so child was created with requested fid */
1125 child = mdt_object_find(mti->mti_env, mdt, mti->mti_rr.rr_fid2);
1126 if (IS_ERR(child)) {
1127 rc = PTR_ERR(child);
1128 LCONSOLE_WARN("Child "DFID" lookup error %d."
1129 " Evicting client %s with export %s.\n",
1130 PFID(mdt_object_fid(child)), rc,
1131 obd_uuid2str(&exp->exp_client_uuid),
1132 obd_export_nid2str(exp));
1133 mdt_export_evict(exp);
1138 body = req_capsule_server_get(mti->mti_pill, &RMF_MDT_BODY);
1139 mti->mti_attr.ma_need = MA_INODE;
1140 mti->mti_attr.ma_valid = 0;
1141 rc = mo_attr_get(mti->mti_env, mdt_object_child(child), &mti->mti_attr);
1142 if (rc == -EREMOTE) {
1143 /* object was created on remote server */
1144 req->rq_status = rc;
1145 body->valid |= OBD_MD_MDS;
1147 mdt_pack_attr2body(mti, body, &mti->mti_attr.ma_attr,
1148 mdt_object_fid(child));
1149 mdt_object_put(mti->mti_env, child);
1152 static void mdt_reconstruct_setattr(struct mdt_thread_info *mti,
1153 struct mdt_lock_handle *lhc)
1155 struct ptlrpc_request *req = mdt_info_req(mti);
1156 struct obd_export *exp = req->rq_export;
1157 struct mdt_export_data *med = &exp->exp_mdt_data;
1158 struct mdt_device *mdt = mti->mti_mdt;
1159 struct mdt_object *obj;
1160 struct mdt_body *body;
1162 mdt_req_from_lcd(req, med->med_lcd);
1166 body = req_capsule_server_get(mti->mti_pill, &RMF_MDT_BODY);
1167 obj = mdt_object_find(mti->mti_env, mdt, mti->mti_rr.rr_fid1);
1169 int rc = PTR_ERR(obj);
1170 LCONSOLE_WARN(""DFID" lookup error %d."
1171 " Evicting client %s with export %s.\n",
1172 PFID(mdt_object_fid(obj)), rc,
1173 obd_uuid2str(&exp->exp_client_uuid),
1174 obd_export_nid2str(exp));
1175 mdt_export_evict(exp);
1179 mti->mti_attr.ma_need = MA_INODE;
1180 mti->mti_attr.ma_valid = 0;
1181 mo_attr_get(mti->mti_env, mdt_object_child(obj), &mti->mti_attr);
1182 mdt_pack_attr2body(mti, body, &mti->mti_attr.ma_attr,
1183 mdt_object_fid(obj));
1184 if (mti->mti_epoch && (mti->mti_epoch->flags & MF_EPOCH_OPEN)) {
1185 struct mdt_file_data *mfd;
1186 struct mdt_body *repbody;
1188 repbody = req_capsule_server_get(mti->mti_pill, &RMF_MDT_BODY);
1189 repbody->ioepoch = obj->mot_ioepoch;
1190 spin_lock(&med->med_open_lock);
1191 list_for_each_entry(mfd, &med->med_open_head, mfd_list) {
1192 if (mfd->mfd_xid == req->rq_xid)
1195 LASSERT(&mfd->mfd_list != &med->med_open_head);
1196 spin_unlock(&med->med_open_lock);
1197 repbody->handle.cookie = mfd->mfd_handle.h_cookie;
1200 mdt_object_put(mti->mti_env, obj);
1203 typedef void (*mdt_reconstructor)(struct mdt_thread_info *mti,
1204 struct mdt_lock_handle *lhc);
1206 static mdt_reconstructor reconstructors[REINT_MAX] = {
1207 [REINT_SETATTR] = mdt_reconstruct_setattr,
1208 [REINT_CREATE] = mdt_reconstruct_create,
1209 [REINT_LINK] = mdt_reconstruct_generic,
1210 [REINT_UNLINK] = mdt_reconstruct_generic,
1211 [REINT_RENAME] = mdt_reconstruct_generic,
1212 [REINT_OPEN] = mdt_reconstruct_open,
1213 [REINT_SETXATTR] = mdt_reconstruct_generic
1216 void mdt_reconstruct(struct mdt_thread_info *mti,
1217 struct mdt_lock_handle *lhc)
1220 reconstructors[mti->mti_rr.rr_opcode](mti, lhc);