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_compat = OBD_COMPAT_MDT;
419 lsd->lsd_feature_rocompat = OBD_ROCOMPAT_LOVOBJID;
420 lsd->lsd_feature_incompat = OBD_INCOMPAT_MDT |
421 OBD_INCOMPAT_COMMON_LR;
423 LCONSOLE_WARN("%s: used disk, loading\n", obd->obd_name);
424 rc = mdt_last_rcvd_header_read(env, mdt);
426 CERROR("error reading MDS %s: rc %d\n", LAST_RCVD, rc);
429 if (strcmp(lsd->lsd_uuid, obd->obd_uuid.uuid) != 0) {
430 LCONSOLE_ERROR_MSG(0x157, "Trying to start OBD %s using"
431 "the wrong disk %s. Were the /dev/ "
432 "assignments rearranged?\n",
433 obd->obd_uuid.uuid, lsd->lsd_uuid);
434 GOTO(out, rc = -EINVAL);
436 lsd->lsd_feature_compat |= OBD_COMPAT_MDT;
437 lsd->lsd_feature_incompat |= OBD_INCOMPAT_MDT |
438 OBD_INCOMPAT_COMMON_LR;
440 mount_count = lsd->lsd_mount_count;
444 if (lsd->lsd_feature_incompat & ~MDT_INCOMPAT_SUPP) {
445 CERROR("%s: unsupported incompat filesystem feature(s) %x\n",
447 lsd->lsd_feature_incompat & ~MDT_INCOMPAT_SUPP);
448 GOTO(out, rc = -EINVAL);
450 if (lsd->lsd_feature_rocompat & ~MDT_ROCOMPAT_SUPP) {
451 CERROR("%s: unsupported read-only filesystem feature(s) %x\n",
453 lsd->lsd_feature_rocompat & ~MDT_ROCOMPAT_SUPP);
454 /* XXX: Do something like remount filesystem read-only */
455 GOTO(out, rc = -EINVAL);
457 /** Interop: evict all clients at first boot with 1.8 last_rcvd */
458 if (!(lsd->lsd_feature_compat & OBD_COMPAT_20)) {
459 LCONSOLE_WARN("Mounting %s at first time on 1.8 FS, remove all"
460 " clients for interop needs\n", obd->obd_name);
461 simple_truncate(lsi->lsi_srv_mnt->mnt_sb->s_root,
462 lsi->lsi_srv_mnt, LAST_RCVD,
463 lsd->lsd_client_start);
464 last_rcvd_size = lsd->lsd_client_start;
465 /** set 2.0 flag to upgrade/downgrade between 1.8 and 2.0 */
466 lsd->lsd_feature_compat |= OBD_COMPAT_20;
469 if (ldd->ldd_flags & LDD_F_IAM_DIR)
470 lsd->lsd_feature_incompat |= OBD_INCOMPAT_IAM_DIR;
472 lsd->lsd_feature_incompat |= OBD_INCOMPAT_FID;
474 spin_lock(&mdt->mdt_transno_lock);
475 mdt->mdt_last_transno = lsd->lsd_last_transno;
476 spin_unlock(&mdt->mdt_transno_lock);
478 CDEBUG(D_INODE, "========BEGIN DUMPING LAST_RCVD========\n");
479 CDEBUG(D_INODE, "%s: server last_transno: "LPU64"\n",
480 obd->obd_name, mdt->mdt_last_transno);
481 CDEBUG(D_INODE, "%s: server mount_count: "LPU64"\n",
482 obd->obd_name, mount_count + 1);
483 CDEBUG(D_INODE, "%s: server data size: %u\n",
484 obd->obd_name, lsd->lsd_server_size);
485 CDEBUG(D_INODE, "%s: per-client data start: %u\n",
486 obd->obd_name, lsd->lsd_client_start);
487 CDEBUG(D_INODE, "%s: per-client data size: %u\n",
488 obd->obd_name, lsd->lsd_client_size);
489 CDEBUG(D_INODE, "%s: last_rcvd size: %lu\n",
490 obd->obd_name, last_rcvd_size);
491 CDEBUG(D_INODE, "%s: last_rcvd clients: %lu\n", obd->obd_name,
492 last_rcvd_size <= lsd->lsd_client_start ? 0 :
493 (last_rcvd_size - lsd->lsd_client_start) /
494 lsd->lsd_client_size);
495 CDEBUG(D_INODE, "========END DUMPING LAST_RCVD========\n");
497 if (!lsd->lsd_server_size || !lsd->lsd_client_start ||
498 !lsd->lsd_client_size) {
499 CERROR("Bad last_rcvd contents!\n");
500 GOTO(out, rc = -EINVAL);
503 rc = mdt_clients_data_init(env, mdt, last_rcvd_size);
505 GOTO(err_client, rc);
507 spin_lock(&mdt->mdt_transno_lock);
508 /* obd_last_committed is used for compatibility
509 * with other lustre recovery code */
510 obd->obd_last_committed = mdt->mdt_last_transno;
511 spin_unlock(&mdt->mdt_transno_lock);
513 mdt->mdt_mount_count = mount_count + 1;
514 lsd->lsd_mount_count = mdt->mdt_mount_count;
516 /* save it, so mount count and last_transno is current */
517 rc = mdt_server_data_update(env, mdt, (mti->mti_exp &&
518 mti->mti_exp->exp_need_sync));
520 GOTO(err_client, rc);
525 class_disconnect_exports(obd);
530 static int mdt_server_data_update(const struct lu_env *env,
531 struct mdt_device *mdt,
537 CDEBUG(D_SUPER, "MDS mount_count is "LPU64", last_transno is "LPU64"\n",
538 mdt->mdt_mount_count, mdt->mdt_last_transno);
540 spin_lock(&mdt->mdt_transno_lock);
541 mdt->mdt_lsd.lsd_last_transno = mdt->mdt_last_transno;
542 spin_unlock(&mdt->mdt_transno_lock);
545 * This may be called from difficult reply handler and
546 * mdt->mdt_last_rcvd may be NULL that time.
548 if (mdt->mdt_last_rcvd != NULL)
549 rc = mdt_last_rcvd_header_write(env, mdt, need_sync);
553 int mdt_client_new(const struct lu_env *env, struct mdt_device *mdt)
555 unsigned long *bitmap = mdt->mdt_client_bitmap;
556 struct mdt_thread_info *mti;
557 struct mdt_export_data *med;
558 struct lsd_client_data *lcd;
559 struct lr_server_data *lsd = &mdt->mdt_lsd;
560 struct obd_device *obd = mdt2obd_dev(mdt);
567 mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
568 LASSERT(mti != NULL);
570 med = &mti->mti_exp->exp_mdt_data;
573 LASSERT(bitmap != NULL);
574 if (!strcmp(med->med_lcd->lcd_uuid, obd->obd_uuid.uuid))
577 /* the bitmap operations can handle cl_idx > sizeof(long) * 8, so
578 * there's no need for extra complication here
580 spin_lock(&mdt->mdt_client_bitmap_lock);
581 cl_idx = find_first_zero_bit(bitmap, LR_MAX_CLIENTS);
582 if (cl_idx >= LR_MAX_CLIENTS ||
583 OBD_FAIL_CHECK(OBD_FAIL_MDS_CLIENT_ADD)) {
584 CERROR("no room for %u clients - fix LR_MAX_CLIENTS\n",
586 spin_unlock(&mdt->mdt_client_bitmap_lock);
589 set_bit(cl_idx, bitmap);
590 spin_unlock(&mdt->mdt_client_bitmap_lock);
592 CDEBUG(D_INFO, "client at idx %d with UUID '%s' added\n",
593 cl_idx, med->med_lcd->lcd_uuid);
595 med->med_lr_idx = cl_idx;
596 med->med_lr_off = lsd->lsd_client_start +
597 (cl_idx * lsd->lsd_client_size);
598 init_mutex(&med->med_lcd_lock);
600 LASSERTF(med->med_lr_off > 0, "med_lr_off = %llu\n", med->med_lr_off);
602 /* Write new client data. */
603 off = med->med_lr_off;
604 mdt_trans_credit_init(env, mdt, MDT_TXN_LAST_RCVD_WRITE_OP);
606 th = mdt_trans_start(env, mdt);
611 * Until this operations will be committed the sync is needed
612 * for this export. This should be done _after_ starting the
613 * transaction so that many connecting clients will not bring
614 * server down with lots of sync writes.
616 mdt_trans_add_cb(th, lut_cb_client, mti->mti_exp);
617 spin_lock(&mti->mti_exp->exp_lock);
618 mti->mti_exp->exp_need_sync = 1;
619 spin_unlock(&mti->mti_exp->exp_lock);
621 rc = mdt_last_rcvd_write(env, mdt, lcd, &off, th);
622 CDEBUG(D_INFO, "wrote client lcd at idx %u off %llu (len "LPSZ")\n",
623 cl_idx, med->med_lr_off, sizeof(*lcd));
624 mdt_trans_stop(env, mdt, th);
629 /* Add client data to the MDS. We use a bitmap to locate a free space
630 * in the last_rcvd file if cl_off is -1 (i.e. a new client).
631 * Otherwise, we just have to read the data from the last_rcvd file and
632 * we know its offset.
634 * It should not be possible to fail adding an existing client - otherwise
635 * mdt_init_server_data() callsite needs to be fixed.
637 int mdt_client_add(const struct lu_env *env,
638 struct mdt_device *mdt, int cl_idx)
640 struct mdt_thread_info *mti;
641 struct mdt_export_data *med;
642 unsigned long *bitmap = mdt->mdt_client_bitmap;
643 struct obd_device *obd = mdt2obd_dev(mdt);
644 struct lr_server_data *lsd = &mdt->mdt_lsd;
648 mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
649 LASSERT(mti != NULL);
651 med = &mti->mti_exp->exp_mdt_data;
653 LASSERT(bitmap != NULL);
654 LASSERTF(cl_idx >= 0, "%d\n", cl_idx);
656 if (!strcmp(med->med_lcd->lcd_uuid, obd->obd_uuid.uuid))
659 spin_lock(&mdt->mdt_client_bitmap_lock);
660 if (test_and_set_bit(cl_idx, bitmap)) {
661 CERROR("MDS client %d: bit already set in bitmap!!\n",
665 spin_unlock(&mdt->mdt_client_bitmap_lock);
667 CDEBUG(D_INFO, "client at idx %d with UUID '%s' added\n",
668 cl_idx, med->med_lcd->lcd_uuid);
670 med->med_lr_idx = cl_idx;
671 med->med_lr_off = lsd->lsd_client_start +
672 (cl_idx * lsd->lsd_client_size);
673 init_mutex(&med->med_lcd_lock);
675 LASSERTF(med->med_lr_off > 0, "med_lr_off = %llu\n", med->med_lr_off);
680 int mdt_client_del(const struct lu_env *env, struct mdt_device *mdt)
682 struct mdt_thread_info *mti;
683 struct mdt_export_data *med;
684 struct lsd_client_data *lcd;
685 struct obd_device *obd = mdt2obd_dev(mdt);
686 struct obd_export *exp;
693 mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
694 LASSERT(mti != NULL);
697 med = &exp->exp_mdt_data;
702 /* XXX: If lcd_uuid were a real obd_uuid, I could use obd_uuid_equals */
703 if (!strcmp(lcd->lcd_uuid, obd->obd_uuid.uuid))
706 CDEBUG(D_INFO, "freeing client at idx %u, offset %lld\n",
707 med->med_lr_idx, med->med_lr_off);
709 off = med->med_lr_off;
712 * Don't clear med_lr_idx here as it is likely also unset. At worst we
713 * leak a client slot that will be cleaned on the next recovery.
716 CERROR("client idx %d has offset %lld\n",
717 med->med_lr_idx, off);
718 GOTO(free, rc = -EINVAL);
722 * Clear the bit _after_ zeroing out the client so we don't race with
723 * mdt_client_add and zero out new clients.
725 if (!test_bit(med->med_lr_idx, mdt->mdt_client_bitmap)) {
726 CERROR("MDT client %u: bit already clear in bitmap!!\n",
731 /* Don't force sync on disconnect if aborting recovery,
732 * or it does num_clients * num_osts. b=17194 */
733 need_sync = (!exp->exp_libclient || exp->exp_need_sync) &&
734 !(exp->exp_flags & OBD_OPT_ABORT_RECOV);
737 * This may be called from difficult reply handler path and
738 * mdt->mdt_last_rcvd may be NULL that time.
740 if (mdt->mdt_last_rcvd != NULL) {
741 mdt_trans_credit_init(env, mdt, MDT_TXN_LAST_RCVD_WRITE_OP);
743 spin_lock(&exp->exp_lock);
744 exp->exp_need_sync = need_sync;
745 spin_unlock(&exp->exp_lock);
747 th = mdt_trans_start(env, mdt);
749 GOTO(free, rc = PTR_ERR(th));
753 * Until this operations will be committed the sync
754 * is needed for this export.
756 mdt_trans_add_cb(th, lut_cb_client, exp);
759 mutex_down(&med->med_lcd_lock);
760 memset(lcd, 0, sizeof *lcd);
762 rc = mdt_last_rcvd_write(env, mdt, lcd, &off, th);
763 mutex_up(&med->med_lcd_lock);
764 mdt_trans_stop(env, mdt, th);
767 CDEBUG(rc == 0 ? D_INFO : D_ERROR, "Zeroing out client idx %u in "
768 "%s %ssync rc %d\n", med->med_lr_idx, LAST_RCVD,
769 need_sync ? "" : "a", rc);
771 spin_lock(&mdt->mdt_client_bitmap_lock);
772 clear_bit(med->med_lr_idx, mdt->mdt_client_bitmap);
773 spin_unlock(&mdt->mdt_client_bitmap_lock);
776 * Make sure the server's last_transno is up to date. Do this
777 * after the client is freed so we know all the client's
778 * transactions have been committed.
780 mdt_server_data_update(env, mdt, need_sync);
790 * last_rcvd & last_committed update callbacks
792 static int mdt_last_rcvd_update(struct mdt_thread_info *mti,
795 struct mdt_device *mdt = mti->mti_mdt;
796 struct ptlrpc_request *req = mdt_info_req(mti);
797 struct mdt_export_data *med;
798 struct lsd_client_data *lcd;
801 __s32 rc = th->th_result;
805 LASSERT(req->rq_export);
807 med = &req->rq_export->exp_mdt_data;
810 /* if the export has already been failed, we have no last_rcvd slot */
811 if (req->rq_export->exp_failed) {
812 CWARN("commit transaction for disconnected client %s: rc %d\n",
813 req->rq_export->exp_client_uuid.uuid, rc);
819 off = med->med_lr_off;
820 LASSERT(ergo(mti->mti_transno == 0, rc != 0));
821 mutex_down(&med->med_lcd_lock);
822 if (lustre_msg_get_opc(req->rq_reqmsg) == MDS_CLOSE ||
823 lustre_msg_get_opc(req->rq_reqmsg) == MDS_DONE_WRITING) {
824 if (mti->mti_transno != 0)
825 lcd->lcd_last_close_transno = mti->mti_transno;
826 lcd->lcd_last_close_xid = req->rq_xid;
827 lcd->lcd_last_close_result = rc;
829 /* VBR: save versions in last_rcvd for reconstruct. */
830 __u64 *pre_versions = lustre_msg_get_versions(req->rq_repmsg);
832 lcd->lcd_pre_versions[0] = pre_versions[0];
833 lcd->lcd_pre_versions[1] = pre_versions[1];
834 lcd->lcd_pre_versions[2] = pre_versions[2];
835 lcd->lcd_pre_versions[3] = pre_versions[3];
837 if (mti->mti_transno != 0)
838 lcd->lcd_last_transno = mti->mti_transno;
839 lcd->lcd_last_xid = req->rq_xid;
840 lcd->lcd_last_result = rc;
841 /*XXX: save intent_disposition in mdt_thread_info?
842 * also there is bug - intent_dispostion is __u64,
843 * see struct ldlm_reply->lock_policy_res1; */
844 lcd->lcd_last_data = mti->mti_opdata;
848 CERROR("client idx %d has offset %lld\n", med->med_lr_idx, off);
851 err = mdt_last_rcvd_write(mti->mti_env, mdt, lcd, &off, th);
853 mutex_up(&med->med_lcd_lock);
857 extern struct lu_context_key mdt_thread_key;
859 /* add credits for last_rcvd update */
860 static int mdt_txn_start_cb(const struct lu_env *env,
861 struct txn_param *param, void *cookie)
863 struct mdt_device *mdt = cookie;
865 param->tp_credits += mdt_trans_credit_get(env, mdt,
866 MDT_TXN_LAST_RCVD_WRITE_OP);
870 /* Set new object versions */
871 static void mdt_versions_set(struct mdt_thread_info *info)
874 for (i = 0; i < PTLRPC_NUM_VERSIONS; i++)
875 if (info->mti_mos[i] != NULL)
876 mo_version_set(info->mti_env,
877 mdt_object_child(info->mti_mos[i]),
881 /* Update last_rcvd records with latests transaction data */
882 static int mdt_txn_stop_cb(const struct lu_env *env,
883 struct thandle *txn, void *cookie)
885 struct mdt_device *mdt = cookie;
886 struct mdt_txn_info *txi;
887 struct mdt_thread_info *mti;
888 struct ptlrpc_request *req;
890 /* transno in two contexts - for commit_cb and for thread */
891 txi = lu_context_key_get(&txn->th_ctx, &mdt_txn_key);
892 mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
893 req = mdt_info_req(mti);
895 if (mti->mti_mdt == NULL || req == NULL || mti->mti_no_need_trans) {
896 txi->txi_transno = 0;
897 mti->mti_no_need_trans = 0;
901 if (mti->mti_has_trans) {
902 /* XXX: currently there are allowed cases, but the wrong cases
903 * are also possible, so better check is needed here */
904 CDEBUG(D_INFO, "More than one transaction "LPU64"\n",
909 mti->mti_has_trans = 1;
910 spin_lock(&mdt->mdt_transno_lock);
911 if (txn->th_result != 0) {
912 if (mti->mti_transno != 0) {
913 CERROR("Replay transno "LPU64" failed: rc %i\n",
914 mti->mti_transno, txn->th_result);
916 } else if (mti->mti_transno == 0) {
917 mti->mti_transno = ++ mdt->mdt_last_transno;
919 /* should be replay */
920 if (mti->mti_transno > mdt->mdt_last_transno)
921 mdt->mdt_last_transno = mti->mti_transno;
923 spin_unlock(&mdt->mdt_transno_lock);
924 /* sometimes the reply message has not been successfully packed */
925 LASSERT(req != NULL && req->rq_repmsg != NULL);
927 /** VBR: set new versions */
928 if (txn->th_result == 0)
929 mdt_versions_set(mti);
931 /* filling reply data */
932 CDEBUG(D_INODE, "transno = %llu, last_committed = %llu\n",
933 mti->mti_transno, req->rq_export->exp_obd->obd_last_committed);
935 req->rq_transno = mti->mti_transno;
936 lustre_msg_set_transno(req->rq_repmsg, mti->mti_transno);
937 lustre_msg_set_last_xid(req->rq_repmsg,
938 lcd_last_xid(req->rq_export->exp_mdt_data.med_lcd));
939 /* save transno for the commit callback */
940 txi->txi_transno = mti->mti_transno;
942 /* add separate commit callback for transaction handling because we need
943 * export as parameter */
944 mdt_trans_add_cb(txn, lut_cb_last_committed,
945 class_export_cb_get(mti->mti_exp));
947 return mdt_last_rcvd_update(mti, txn);
950 /* commit callback, need to update last_committed value */
951 static int mdt_txn_commit_cb(const struct lu_env *env,
952 struct thandle *txn, void *cookie)
954 struct mdt_device *mdt = cookie;
955 struct mdt_txn_info *txi;
958 txi = lu_context_key_get(&txn->th_ctx, &mdt_txn_key);
960 /* iterate through all additional callbacks */
961 for (i = 0; i < txi->txi_cb_count; i++) {
962 txi->txi_cb[i].lut_cb_func(&mdt->mdt_lut, txi->txi_transno,
963 txi->txi_cb[i].lut_cb_data, 0);
968 int mdt_fs_setup(const struct lu_env *env, struct mdt_device *mdt,
969 struct obd_device *obd,
970 struct lustre_sb_info *lsi)
977 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_FS_SETUP))
980 /* prepare transactions callbacks */
981 mdt->mdt_txn_cb.dtc_txn_start = mdt_txn_start_cb;
982 mdt->mdt_txn_cb.dtc_txn_stop = mdt_txn_stop_cb;
983 mdt->mdt_txn_cb.dtc_txn_commit = mdt_txn_commit_cb;
984 mdt->mdt_txn_cb.dtc_cookie = mdt;
985 mdt->mdt_txn_cb.dtc_tag = LCT_MD_THREAD;
986 CFS_INIT_LIST_HEAD(&mdt->mdt_txn_cb.dtc_linkage);
988 dt_txn_callback_add(mdt->mdt_bottom, &mdt->mdt_txn_cb);
990 rc = mdt_server_data_init(env, mdt, lsi);
994 o = dt_store_open(env, mdt->mdt_bottom, "", CAPA_KEYS, &fid);
997 rc = mdt_capa_keys_init(env, mdt);
999 GOTO(put_ck_object, rc);
1002 CERROR("cannot open %s: rc = %d\n", CAPA_KEYS, rc);
1003 GOTO(disconnect_exports, rc);
1008 lu_object_put(env, &o->do_lu);
1009 mdt->mdt_ck_obj = NULL;
1011 class_disconnect_exports(obd);
1015 void mdt_fs_cleanup(const struct lu_env *env, struct mdt_device *mdt)
1019 /* Remove transaction callback */
1020 dt_txn_callback_del(mdt->mdt_bottom, &mdt->mdt_txn_cb);
1021 if (mdt->mdt_ck_obj)
1022 lu_object_put(env, &mdt->mdt_ck_obj->do_lu);
1023 mdt->mdt_ck_obj = NULL;
1027 /* reconstruction code */
1028 static void mdt_steal_ack_locks(struct ptlrpc_request *req)
1030 struct obd_export *exp = req->rq_export;
1031 struct list_head *tmp;
1032 struct ptlrpc_reply_state *oldrep;
1033 struct ptlrpc_service *svc;
1036 /* CAVEAT EMPTOR: spinlock order */
1037 spin_lock(&exp->exp_lock);
1038 list_for_each (tmp, &exp->exp_outstanding_replies) {
1039 oldrep = list_entry(tmp, struct ptlrpc_reply_state,rs_exp_list);
1041 if (oldrep->rs_xid != req->rq_xid)
1044 if (oldrep->rs_opc != lustre_msg_get_opc(req->rq_reqmsg))
1045 CERROR ("Resent req xid "LPU64" has mismatched opc: "
1046 "new %d old %d\n", req->rq_xid,
1047 lustre_msg_get_opc(req->rq_reqmsg),
1050 svc = oldrep->rs_service;
1051 spin_lock (&svc->srv_lock);
1053 list_del_init (&oldrep->rs_exp_list);
1055 CWARN("Stealing %d locks from rs %p x"LPD64".t"LPD64
1057 oldrep->rs_nlocks, oldrep,
1058 oldrep->rs_xid, oldrep->rs_transno, oldrep->rs_opc,
1059 libcfs_nid2str(exp->exp_connection->c_peer.nid));
1061 for (i = 0; i < oldrep->rs_nlocks; i++)
1062 ptlrpc_save_lock(req, &oldrep->rs_locks[i],
1063 oldrep->rs_modes[i], 0);
1064 oldrep->rs_nlocks = 0;
1066 DEBUG_REQ(D_HA, req, "stole locks for");
1067 spin_lock(&oldrep->rs_lock);
1068 ptlrpc_schedule_difficult_reply (oldrep);
1069 spin_unlock(&oldrep->rs_lock);
1071 spin_unlock (&svc->srv_lock);
1074 spin_unlock(&exp->exp_lock);
1078 * VBR: restore versions
1080 void mdt_vbr_reconstruct(struct ptlrpc_request *req,
1081 struct lsd_client_data *lcd)
1083 __u64 pre_versions[4] = {0};
1084 pre_versions[0] = lcd->lcd_pre_versions[0];
1085 pre_versions[1] = lcd->lcd_pre_versions[1];
1086 pre_versions[2] = lcd->lcd_pre_versions[2];
1087 pre_versions[3] = lcd->lcd_pre_versions[3];
1088 lustre_msg_set_versions(req->rq_repmsg, pre_versions);
1091 void mdt_req_from_lcd(struct ptlrpc_request *req,
1092 struct lsd_client_data *lcd)
1094 DEBUG_REQ(D_HA, req, "restoring transno "LPD64"/status %d",
1095 lcd->lcd_last_transno, lcd->lcd_last_result);
1097 if (lustre_msg_get_opc(req->rq_reqmsg) == MDS_CLOSE ||
1098 lustre_msg_get_opc(req->rq_repmsg) == MDS_DONE_WRITING) {
1099 req->rq_transno = lcd->lcd_last_close_transno;
1100 req->rq_status = lcd->lcd_last_close_result;
1102 req->rq_transno = lcd->lcd_last_transno;
1103 req->rq_status = lcd->lcd_last_result;
1104 mdt_vbr_reconstruct(req, lcd);
1106 if (req->rq_status != 0)
1107 req->rq_transno = 0;
1108 lustre_msg_set_transno(req->rq_repmsg, req->rq_transno);
1109 lustre_msg_set_status(req->rq_repmsg, req->rq_status);
1110 DEBUG_REQ(D_RPCTRACE, req, "restoring transno "LPD64"/status %d",
1111 req->rq_transno, req->rq_status);
1113 mdt_steal_ack_locks(req);
1116 void mdt_reconstruct_generic(struct mdt_thread_info *mti,
1117 struct mdt_lock_handle *lhc)
1119 struct ptlrpc_request *req = mdt_info_req(mti);
1120 struct mdt_export_data *med = &req->rq_export->exp_mdt_data;
1122 return mdt_req_from_lcd(req, med->med_lcd);
1125 static void mdt_reconstruct_create(struct mdt_thread_info *mti,
1126 struct mdt_lock_handle *lhc)
1128 struct ptlrpc_request *req = mdt_info_req(mti);
1129 struct obd_export *exp = req->rq_export;
1130 struct mdt_export_data *med = &exp->exp_mdt_data;
1131 struct mdt_device *mdt = mti->mti_mdt;
1132 struct mdt_object *child;
1133 struct mdt_body *body;
1136 mdt_req_from_lcd(req, med->med_lcd);
1140 /* if no error, so child was created with requested fid */
1141 child = mdt_object_find(mti->mti_env, mdt, mti->mti_rr.rr_fid2);
1142 if (IS_ERR(child)) {
1143 rc = PTR_ERR(child);
1144 LCONSOLE_WARN("Child "DFID" lookup error %d."
1145 " Evicting client %s with export %s.\n",
1146 PFID(mdt_object_fid(child)), rc,
1147 obd_uuid2str(&exp->exp_client_uuid),
1148 obd_export_nid2str(exp));
1149 mdt_export_evict(exp);
1154 body = req_capsule_server_get(mti->mti_pill, &RMF_MDT_BODY);
1155 mti->mti_attr.ma_need = MA_INODE;
1156 mti->mti_attr.ma_valid = 0;
1157 rc = mo_attr_get(mti->mti_env, mdt_object_child(child), &mti->mti_attr);
1158 if (rc == -EREMOTE) {
1159 /* object was created on remote server */
1160 req->rq_status = rc;
1161 body->valid |= OBD_MD_MDS;
1163 mdt_pack_attr2body(mti, body, &mti->mti_attr.ma_attr,
1164 mdt_object_fid(child));
1165 mdt_object_put(mti->mti_env, child);
1168 static void mdt_reconstruct_setattr(struct mdt_thread_info *mti,
1169 struct mdt_lock_handle *lhc)
1171 struct ptlrpc_request *req = mdt_info_req(mti);
1172 struct obd_export *exp = req->rq_export;
1173 struct mdt_export_data *med = &exp->exp_mdt_data;
1174 struct mdt_device *mdt = mti->mti_mdt;
1175 struct mdt_object *obj;
1176 struct mdt_body *body;
1178 mdt_req_from_lcd(req, med->med_lcd);
1182 body = req_capsule_server_get(mti->mti_pill, &RMF_MDT_BODY);
1183 obj = mdt_object_find(mti->mti_env, mdt, mti->mti_rr.rr_fid1);
1185 int rc = PTR_ERR(obj);
1186 LCONSOLE_WARN(""DFID" lookup error %d."
1187 " Evicting client %s with export %s.\n",
1188 PFID(mdt_object_fid(obj)), rc,
1189 obd_uuid2str(&exp->exp_client_uuid),
1190 obd_export_nid2str(exp));
1191 mdt_export_evict(exp);
1195 mti->mti_attr.ma_need = MA_INODE;
1196 mti->mti_attr.ma_valid = 0;
1197 mo_attr_get(mti->mti_env, mdt_object_child(obj), &mti->mti_attr);
1198 mdt_pack_attr2body(mti, body, &mti->mti_attr.ma_attr,
1199 mdt_object_fid(obj));
1200 if (mti->mti_epoch && (mti->mti_epoch->flags & MF_EPOCH_OPEN)) {
1201 struct mdt_file_data *mfd;
1202 struct mdt_body *repbody;
1204 repbody = req_capsule_server_get(mti->mti_pill, &RMF_MDT_BODY);
1205 repbody->ioepoch = obj->mot_ioepoch;
1206 spin_lock(&med->med_open_lock);
1207 list_for_each_entry(mfd, &med->med_open_head, mfd_list) {
1208 if (mfd->mfd_xid == req->rq_xid)
1211 LASSERT(&mfd->mfd_list != &med->med_open_head);
1212 spin_unlock(&med->med_open_lock);
1213 repbody->handle.cookie = mfd->mfd_handle.h_cookie;
1216 mdt_object_put(mti->mti_env, obj);
1219 typedef void (*mdt_reconstructor)(struct mdt_thread_info *mti,
1220 struct mdt_lock_handle *lhc);
1222 static mdt_reconstructor reconstructors[REINT_MAX] = {
1223 [REINT_SETATTR] = mdt_reconstruct_setattr,
1224 [REINT_CREATE] = mdt_reconstruct_create,
1225 [REINT_LINK] = mdt_reconstruct_generic,
1226 [REINT_UNLINK] = mdt_reconstruct_generic,
1227 [REINT_RENAME] = mdt_reconstruct_generic,
1228 [REINT_OPEN] = mdt_reconstruct_open,
1229 [REINT_SETXATTR] = mdt_reconstruct_generic
1232 void mdt_reconstruct(struct mdt_thread_info *mti,
1233 struct mdt_lock_handle *lhc)
1236 reconstructors[mti->mti_rr.rr_opcode](mti, lhc);