1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/mdt/mdt_recovery.c
38 * Lustre Metadata Target (mdt) recovery-related methods
40 * Author: Huang Hua <huanghua@clusterfs.com>
41 * Author: Pershin Mike <tappro@clusterfs.com>
45 # define EXPORT_SYMTAB
47 #define DEBUG_SUBSYSTEM S_MDS
49 #include "mdt_internal.h"
51 static int mdt_server_data_update(const struct lu_env *env,
52 struct mdt_device *mdt);
54 struct lu_buf *mdt_buf(const struct lu_env *env, void *area, ssize_t len)
57 struct mdt_thread_info *mti;
59 mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
66 const struct lu_buf *mdt_buf_const(const struct lu_env *env,
67 const void *area, ssize_t len)
70 struct mdt_thread_info *mti;
72 mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
75 buf->lb_buf = (void *)area;
80 static inline int mdt_trans_credit_get(const struct lu_env *env,
81 struct mdt_device *mdt,
84 struct dt_device *dev = mdt->mdt_bottom;
87 case MDT_TXN_CAPA_KEYS_WRITE_OP:
88 case MDT_TXN_LAST_RCVD_WRITE_OP:
89 cr = dev->dd_ops->dt_credit_get(env,
99 void mdt_trans_credit_init(const struct lu_env *env,
100 struct mdt_device *mdt,
103 struct mdt_thread_info *mti;
107 mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
108 p = &mti->mti_txn_param;
110 cr = mdt_trans_credit_get(env, mdt, op);
111 txn_param_init(p, cr);
114 struct thandle* mdt_trans_start(const struct lu_env *env,
115 struct mdt_device *mdt)
117 struct mdt_thread_info *mti;
120 mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
121 p = &mti->mti_txn_param;
123 /* export can require sync operations */
124 if (mti->mti_exp != NULL)
125 p->tp_sync = mti->mti_exp->exp_need_sync;
127 return mdt->mdt_bottom->dd_ops->dt_trans_start(env, mdt->mdt_bottom, p);
130 void mdt_trans_stop(const struct lu_env *env,
131 struct mdt_device *mdt, struct thandle *th)
133 mdt->mdt_bottom->dd_ops->dt_trans_stop(env, th);
136 static inline int mdt_last_rcvd_header_read(const struct lu_env *env,
137 struct mdt_device *mdt)
139 struct mdt_thread_info *mti;
142 mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
145 rc = dt_record_read(env, mdt->mdt_last_rcvd,
146 mdt_buf(env, &mti->mti_lsd, sizeof(mti->mti_lsd)),
149 lsd_le_to_cpu(&mti->mti_lsd, &mdt->mdt_lsd);
151 CDEBUG(D_INFO, "read last_rcvd header rc = %d, uuid = %s, "
152 "last_transno = "LPU64"\n", rc, mdt->mdt_lsd.lsd_uuid,
153 mdt->mdt_lsd.lsd_last_transno);
157 static int mdt_last_rcvd_header_write(const struct lu_env *env,
158 struct mdt_device *mdt,
161 struct mdt_thread_info *mti;
165 mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
168 lsd_cpu_to_le(&mdt->mdt_lsd, &mti->mti_lsd);
170 rc = dt_record_write(env, mdt->mdt_last_rcvd,
171 mdt_buf_const(env, &mti->mti_lsd,
172 sizeof(mti->mti_lsd)),
175 CDEBUG(D_INFO, "write last_rcvd header rc = %d, uuid = %s, "
176 "last_transno = "LPU64"\n", rc, mdt->mdt_lsd.lsd_uuid,
177 mdt->mdt_lsd.lsd_last_transno);
182 static int mdt_last_rcvd_read(const struct lu_env *env,
183 struct mdt_device *mdt,
184 struct lsd_client_data *lcd, loff_t *off)
186 struct mdt_thread_info *mti;
187 struct lsd_client_data *tmp;
190 mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
192 rc = dt_record_read(env, mdt->mdt_last_rcvd,
193 mdt_buf(env, tmp, sizeof(*tmp)), off);
195 lcd_le_to_cpu(tmp, lcd);
197 CDEBUG(D_INFO, "read lcd @%d rc = %d, uuid = %s, last_transno = "LPU64
198 ", last_xid = "LPU64", last_result = %u, last_data = %u, "
199 "last_close_transno = "LPU64", last_close_xid = "LPU64", "
200 "last_close_result = %u\n", (int)(*off - sizeof(*tmp)),
201 rc, lcd->lcd_uuid, lcd->lcd_last_transno, lcd->lcd_last_xid,
202 lcd->lcd_last_result, lcd->lcd_last_data,
203 lcd->lcd_last_close_transno, lcd->lcd_last_close_xid,
204 lcd->lcd_last_close_result);
208 static int mdt_last_rcvd_write(const struct lu_env *env,
209 struct mdt_device *mdt,
210 struct lsd_client_data *lcd,
211 loff_t *off, struct thandle *th)
213 struct mdt_thread_info *mti;
214 struct lsd_client_data *tmp;
218 mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
221 lcd_cpu_to_le(lcd, tmp);
223 rc = dt_record_write(env, mdt->mdt_last_rcvd,
224 mdt_buf_const(env, tmp, sizeof(*tmp)), off, th);
226 CDEBUG(D_INFO, "write lcd @%d rc = %d, uuid = %s, last_transno = "LPU64
227 ", last_xid = "LPU64", last_result = %u, last_data = %u, "
228 "last_close_transno = "LPU64", last_close_xid = "LPU64" ,"
229 "last_close_result = %u\n", (int)(*off - sizeof(*tmp)),
230 rc, lcd->lcd_uuid, lcd->lcd_last_transno, lcd->lcd_last_xid,
231 lcd->lcd_last_result, lcd->lcd_last_data,
232 lcd->lcd_last_close_transno, lcd->lcd_last_close_xid,
233 lcd->lcd_last_close_result);
237 static int mdt_clients_data_init(const struct lu_env *env,
238 struct mdt_device *mdt,
239 unsigned long last_size)
241 struct lr_server_data *lsd = &mdt->mdt_lsd;
242 struct lsd_client_data *lcd = NULL;
243 struct obd_device *obd = mdt2obd_dev(mdt);
249 /* When we do a clean MDS shutdown, we save the last_transno into
250 * the header. If we find clients with higher last_transno values
251 * then those clients may need recovery done. */
252 LASSERT(atomic_read(&obd->obd_req_replay_clients) == 0);
253 for (cl_idx = 0, off = lsd->lsd_client_start;
254 off < last_size; cl_idx++) {
256 struct obd_export *exp;
264 off = lsd->lsd_client_start +
265 cl_idx * lsd->lsd_client_size;
267 rc = mdt_last_rcvd_read(env, mdt, lcd, &off);
269 CERROR("error reading MDS %s idx %d, off %llu: rc %d\n",
270 LAST_RCVD, cl_idx, off, rc);
272 break; /* read error shouldn't cause startup to fail */
275 if (lcd->lcd_uuid[0] == '\0') {
276 CDEBUG(D_INFO, "skipping zeroed client at offset %d\n",
281 last_transno = lcd_last_transno(lcd);
283 /* These exports are cleaned up by mdt_obd_disconnect(), so
284 * they need to be set up like real exports as
285 * mdt_obd_connect() does.
287 CDEBUG(D_HA, "RCVRNG CLIENT uuid: %s idx: %d lr: "LPU64
288 " srv lr: "LPU64" lx: "LPU64"\n", lcd->lcd_uuid, cl_idx,
289 last_transno, lsd->lsd_last_transno,
292 exp = class_new_export(obd, (struct obd_uuid *)lcd->lcd_uuid);
294 if (PTR_ERR(exp) == -EALREADY) {
295 /* export already exists, zero out this one */
296 lcd->lcd_uuid[0] = '\0';
298 GOTO(err_client, rc = PTR_ERR(exp));
301 struct mdt_thread_info *mti;
302 mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
303 LASSERT(mti != NULL);
305 exp->exp_mdt_data.med_lcd = lcd;
306 rc = mdt_client_add(env, mdt, cl_idx);
307 /* can't fail existing */
308 LASSERTF(rc == 0, "rc = %d\n", rc);
309 /* VBR: set export last committed version */
310 exp->exp_last_committed = last_transno;
312 spin_lock(&exp->exp_lock);
313 exp->exp_connecting = 0;
314 exp->exp_in_recovery = 0;
315 spin_unlock(&exp->exp_lock);
316 obd->obd_max_recoverable_clients++;
317 class_export_put(exp);
320 CDEBUG(D_OTHER, "client at idx %d has last_transno="LPU64"\n",
321 cl_idx, last_transno);
322 /* protect __u64 value update */
323 spin_lock(&mdt->mdt_transno_lock);
324 mdt->mdt_last_transno = max(last_transno,
325 mdt->mdt_last_transno);
326 spin_unlock(&mdt->mdt_transno_lock);
335 static int mdt_server_data_init(const struct lu_env *env,
336 struct mdt_device *mdt,
337 struct lustre_sb_info *lsi)
339 struct lr_server_data *lsd = &mdt->mdt_lsd;
340 struct lsd_client_data *lcd = NULL;
341 struct obd_device *obd = mdt2obd_dev(mdt);
342 struct mdt_thread_info *mti;
343 struct dt_object *obj;
345 struct lustre_disk_data *ldd;
346 unsigned long last_rcvd_size;
351 /* ensure padding in the struct is the correct size */
352 CLASSERT(offsetof(struct lr_server_data, lsd_padding) +
353 sizeof(lsd->lsd_padding) == LR_SERVER_SIZE);
354 CLASSERT(offsetof(struct lsd_client_data, lcd_padding) +
355 sizeof(lcd->lcd_padding) == LR_CLIENT_SIZE);
357 mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
358 LASSERT(mti != NULL);
359 la = &mti->mti_attr.ma_attr;
361 obj = mdt->mdt_last_rcvd;
362 rc = obj->do_ops->do_attr_get(env, mdt->mdt_last_rcvd, la, BYPASS_CAPA);
366 last_rcvd_size = (unsigned long)la->la_size;
368 if (last_rcvd_size == 0) {
369 LCONSOLE_WARN("%s: new disk, initializing\n", obd->obd_name);
371 memcpy(lsd->lsd_uuid, obd->obd_uuid.uuid,
372 sizeof(lsd->lsd_uuid));
373 lsd->lsd_last_transno = 0;
374 lsd->lsd_mount_count = 0;
375 lsd->lsd_server_size = LR_SERVER_SIZE;
376 lsd->lsd_client_start = LR_CLIENT_START;
377 lsd->lsd_client_size = LR_CLIENT_SIZE;
378 lsd->lsd_feature_compat = OBD_COMPAT_MDT;
379 lsd->lsd_feature_rocompat = OBD_ROCOMPAT_LOVOBJID;
380 lsd->lsd_feature_incompat = OBD_INCOMPAT_MDT |
381 OBD_INCOMPAT_COMMON_LR;
383 LCONSOLE_WARN("%s: used disk, loading\n", obd->obd_name);
384 rc = mdt_last_rcvd_header_read(env, mdt);
386 CERROR("error reading MDS %s: rc %d\n", LAST_RCVD, rc);
389 if (strcmp(lsd->lsd_uuid, obd->obd_uuid.uuid) != 0) {
390 LCONSOLE_ERROR_MSG(0x157, "Trying to start OBD %s using"
391 "the wrong disk %s. Were the /dev/ "
392 "assignments rearranged?\n",
393 obd->obd_uuid.uuid, lsd->lsd_uuid);
394 GOTO(out, rc = -EINVAL);
396 lsd->lsd_feature_compat |= OBD_COMPAT_MDT;
397 lsd->lsd_feature_incompat |= OBD_INCOMPAT_MDT |
398 OBD_INCOMPAT_COMMON_LR;
400 mount_count = lsd->lsd_mount_count;
404 if (lsd->lsd_feature_incompat & ~MDT_INCOMPAT_SUPP) {
405 CERROR("%s: unsupported incompat filesystem feature(s) %x\n",
407 lsd->lsd_feature_incompat & ~MDT_INCOMPAT_SUPP);
408 GOTO(out, rc = -EINVAL);
410 if (lsd->lsd_feature_rocompat & ~MDT_ROCOMPAT_SUPP) {
411 CERROR("%s: unsupported read-only filesystem feature(s) %x\n",
413 lsd->lsd_feature_rocompat & ~MDT_ROCOMPAT_SUPP);
414 /* XXX: Do something like remount filesystem read-only */
415 GOTO(out, rc = -EINVAL);
417 /** Interop: evict all clients at first boot with 1.8 last_rcvd */
418 if (!(lsd->lsd_feature_compat & OBD_COMPAT_20)) {
419 LCONSOLE_WARN("Mounting %s at first time on 1.8 FS, remove all"
420 " clients for interop needs\n", obd->obd_name);
421 simple_truncate(lsi->lsi_srv_mnt->mnt_sb->s_root,
422 lsi->lsi_srv_mnt, LAST_RCVD,
423 lsd->lsd_client_start);
424 last_rcvd_size = lsd->lsd_client_start;
425 /** set 2.0 flag to upgrade/downgrade between 1.8 and 2.0 */
426 lsd->lsd_feature_compat |= OBD_COMPAT_20;
429 if (ldd->ldd_flags & LDD_F_IAM_DIR)
430 lsd->lsd_feature_incompat |= OBD_INCOMPAT_IAM_DIR;
432 lsd->lsd_feature_incompat |= OBD_INCOMPAT_FID;
434 spin_lock(&mdt->mdt_transno_lock);
435 mdt->mdt_last_transno = lsd->lsd_last_transno;
436 spin_unlock(&mdt->mdt_transno_lock);
438 CDEBUG(D_INODE, "========BEGIN DUMPING LAST_RCVD========\n");
439 CDEBUG(D_INODE, "%s: server last_transno: "LPU64"\n",
440 obd->obd_name, mdt->mdt_last_transno);
441 CDEBUG(D_INODE, "%s: server mount_count: "LPU64"\n",
442 obd->obd_name, mount_count + 1);
443 CDEBUG(D_INODE, "%s: server data size: %u\n",
444 obd->obd_name, lsd->lsd_server_size);
445 CDEBUG(D_INODE, "%s: per-client data start: %u\n",
446 obd->obd_name, lsd->lsd_client_start);
447 CDEBUG(D_INODE, "%s: per-client data size: %u\n",
448 obd->obd_name, lsd->lsd_client_size);
449 CDEBUG(D_INODE, "%s: last_rcvd size: %lu\n",
450 obd->obd_name, last_rcvd_size);
451 CDEBUG(D_INODE, "%s: last_rcvd clients: %lu\n", obd->obd_name,
452 last_rcvd_size <= lsd->lsd_client_start ? 0 :
453 (last_rcvd_size - lsd->lsd_client_start) /
454 lsd->lsd_client_size);
455 CDEBUG(D_INODE, "========END DUMPING LAST_RCVD========\n");
457 if (!lsd->lsd_server_size || !lsd->lsd_client_start ||
458 !lsd->lsd_client_size) {
459 CERROR("Bad last_rcvd contents!\n");
460 GOTO(out, rc = -EINVAL);
463 rc = mdt_clients_data_init(env, mdt, last_rcvd_size);
465 GOTO(err_client, rc);
467 spin_lock(&mdt->mdt_transno_lock);
468 /* obd_last_committed is used for compatibility
469 * with other lustre recovery code */
470 obd->obd_last_committed = mdt->mdt_last_transno;
471 spin_unlock(&mdt->mdt_transno_lock);
473 mdt->mdt_mount_count = mount_count + 1;
474 lsd->lsd_mount_count = mdt->mdt_mount_count;
476 /* save it, so mount count and last_transno is current */
477 rc = mdt_server_data_update(env, mdt);
479 GOTO(err_client, rc);
484 class_disconnect_exports(obd);
489 static int mdt_server_data_update(const struct lu_env *env,
490 struct mdt_device *mdt)
492 struct mdt_thread_info *mti;
496 mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
498 mdt_trans_credit_init(env, mdt, MDT_TXN_LAST_RCVD_WRITE_OP);
499 th = mdt_trans_start(env, mdt);
503 CDEBUG(D_SUPER, "MDS mount_count is "LPU64", last_transno is "LPU64"\n",
504 mdt->mdt_mount_count, mdt->mdt_last_transno);
506 spin_lock(&mdt->mdt_transno_lock);
507 mdt->mdt_lsd.lsd_last_transno = mdt->mdt_last_transno;
508 spin_unlock(&mdt->mdt_transno_lock);
510 rc = mdt_last_rcvd_header_write(env, mdt, th);
511 mdt_trans_stop(env, mdt, th);
516 int mdt_client_new(const struct lu_env *env, struct mdt_device *mdt)
518 unsigned long *bitmap = mdt->mdt_client_bitmap;
519 struct mdt_thread_info *mti;
520 struct mdt_export_data *med;
521 struct lsd_client_data *lcd;
522 struct lr_server_data *lsd = &mdt->mdt_lsd;
523 struct obd_device *obd = mdt2obd_dev(mdt);
530 mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
531 LASSERT(mti != NULL);
533 med = &mti->mti_exp->exp_mdt_data;
536 LASSERT(bitmap != NULL);
537 if (!strcmp(med->med_lcd->lcd_uuid, obd->obd_uuid.uuid))
540 /* the bitmap operations can handle cl_idx > sizeof(long) * 8, so
541 * there's no need for extra complication here
543 spin_lock(&mdt->mdt_client_bitmap_lock);
544 cl_idx = find_first_zero_bit(bitmap, LR_MAX_CLIENTS);
545 if (cl_idx >= LR_MAX_CLIENTS ||
546 OBD_FAIL_CHECK(OBD_FAIL_MDS_CLIENT_ADD)) {
547 CERROR("no room for %u clients - fix LR_MAX_CLIENTS\n",
549 spin_unlock(&mdt->mdt_client_bitmap_lock);
552 set_bit(cl_idx, bitmap);
553 spin_unlock(&mdt->mdt_client_bitmap_lock);
555 CDEBUG(D_INFO, "client at idx %d with UUID '%s' added\n",
556 cl_idx, med->med_lcd->lcd_uuid);
558 med->med_lr_idx = cl_idx;
559 med->med_lr_off = lsd->lsd_client_start +
560 (cl_idx * lsd->lsd_client_size);
561 init_mutex(&med->med_lcd_lock);
563 LASSERTF(med->med_lr_off > 0, "med_lr_off = %llu\n", med->med_lr_off);
565 /* Write new client data. */
566 off = med->med_lr_off;
567 mdt_trans_credit_init(env, mdt, MDT_TXN_LAST_RCVD_WRITE_OP);
569 th = mdt_trans_start(env, mdt);
574 * Until this operations will be committed the sync is needed
575 * for this export. This should be done _after_ starting the
576 * transaction so that many connecting clients will not bring
577 * server down with lots of sync writes.
579 mdt_trans_add_cb(th, lut_cb_client, class_export_cb_get(mti->mti_exp));
580 spin_lock(&mti->mti_exp->exp_lock);
581 mti->mti_exp->exp_need_sync = 1;
582 spin_unlock(&mti->mti_exp->exp_lock);
584 rc = mdt_last_rcvd_write(env, mdt, lcd, &off, th);
585 CDEBUG(D_INFO, "wrote client lcd at idx %u off %llu (len %u)\n",
586 cl_idx, med->med_lr_off, (int)sizeof(*lcd));
587 mdt_trans_stop(env, mdt, th);
592 /* Add client data to the MDS. We use a bitmap to locate a free space
593 * in the last_rcvd file if cl_off is -1 (i.e. a new client).
594 * Otherwise, we just have to read the data from the last_rcvd file and
595 * we know its offset.
597 * It should not be possible to fail adding an existing client - otherwise
598 * mdt_init_server_data() callsite needs to be fixed.
600 int mdt_client_add(const struct lu_env *env,
601 struct mdt_device *mdt, int cl_idx)
603 struct mdt_thread_info *mti;
604 struct mdt_export_data *med;
605 unsigned long *bitmap = mdt->mdt_client_bitmap;
606 struct obd_device *obd = mdt2obd_dev(mdt);
607 struct lr_server_data *lsd = &mdt->mdt_lsd;
611 mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
612 LASSERT(mti != NULL);
614 med = &mti->mti_exp->exp_mdt_data;
616 LASSERT(bitmap != NULL);
617 LASSERTF(cl_idx >= 0, "%d\n", cl_idx);
619 if (!strcmp(med->med_lcd->lcd_uuid, obd->obd_uuid.uuid))
622 spin_lock(&mdt->mdt_client_bitmap_lock);
623 if (test_and_set_bit(cl_idx, bitmap)) {
624 CERROR("MDS client %d: bit already set in bitmap!!\n",
628 spin_unlock(&mdt->mdt_client_bitmap_lock);
630 CDEBUG(D_INFO, "client at idx %d with UUID '%s' added\n",
631 cl_idx, med->med_lcd->lcd_uuid);
633 med->med_lr_idx = cl_idx;
634 med->med_lr_off = lsd->lsd_client_start +
635 (cl_idx * lsd->lsd_client_size);
636 init_mutex(&med->med_lcd_lock);
638 LASSERTF(med->med_lr_off > 0, "med_lr_off = %llu\n", med->med_lr_off);
643 int mdt_client_del(const struct lu_env *env, struct mdt_device *mdt)
645 struct mdt_thread_info *mti;
646 struct mdt_export_data *med;
647 struct lsd_client_data *lcd;
648 struct obd_device *obd = mdt2obd_dev(mdt);
649 struct obd_export *exp;
655 mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
656 LASSERT(mti != NULL);
659 med = &exp->exp_mdt_data;
664 /* XXX: If lcd_uuid were a real obd_uuid, I could use obd_uuid_equals */
665 if (!strcmp(lcd->lcd_uuid, obd->obd_uuid.uuid))
668 CDEBUG(D_INFO, "freeing client at idx %u, offset %lld\n",
669 med->med_lr_idx, med->med_lr_off);
671 off = med->med_lr_off;
674 * Don't clear med_lr_idx here as it is likely also unset. At worst we
675 * leak a client slot that will be cleaned on the next recovery.
678 CERROR("client idx %d has offset %lld\n",
679 med->med_lr_idx, off);
680 GOTO(free, rc = -EINVAL);
684 * Clear the bit _after_ zeroing out the client so we don't race with
685 * mdt_client_add and zero out new clients.
687 if (!test_bit(med->med_lr_idx, mdt->mdt_client_bitmap)) {
688 CERROR("MDT client %u: bit already clear in bitmap!!\n",
693 /* Make sure the server's last_transno is up to date.
694 * This should be done before zeroing client slot so last_transno will
695 * be in server data or in client data in case of failure */
696 mdt_server_data_update(env, mdt);
698 mdt_trans_credit_init(env, mdt, MDT_TXN_LAST_RCVD_WRITE_OP);
699 th = mdt_trans_start(env, mdt);
701 GOTO(free, rc = PTR_ERR(th));
703 mutex_down(&med->med_lcd_lock);
704 memset(lcd, 0, sizeof *lcd);
705 rc = mdt_last_rcvd_write(env, mdt, lcd, &off, th);
707 mutex_up(&med->med_lcd_lock);
708 mdt_trans_stop(env, mdt, th);
710 spin_lock(&mdt->mdt_client_bitmap_lock);
711 clear_bit(med->med_lr_idx, mdt->mdt_client_bitmap);
712 spin_unlock(&mdt->mdt_client_bitmap_lock);
714 CDEBUG(rc == 0 ? D_INFO : D_ERROR, "Zeroing out client idx %u in "
715 "%s, rc %d\n", med->med_lr_idx, LAST_RCVD, rc);
719 mutex_down(&med->med_lcd_lock);
721 mutex_up(&med->med_lcd_lock);
727 * last_rcvd & last_committed update callbacks
729 static int mdt_last_rcvd_update(struct mdt_thread_info *mti,
732 struct mdt_device *mdt = mti->mti_mdt;
733 struct ptlrpc_request *req = mdt_info_req(mti);
734 struct mdt_export_data *med;
735 struct lsd_client_data *lcd;
738 __s32 rc = th->th_result;
742 LASSERT(req->rq_export);
744 med = &req->rq_export->exp_mdt_data;
747 mutex_down(&med->med_lcd_lock);
749 /* if the export has already been disconnected, we have no last_rcvd slot,
750 * update server data with latest transno then */
752 mutex_up(&med->med_lcd_lock);
753 CWARN("commit transaction for disconnected client %s: rc %d\n",
754 req->rq_export->exp_client_uuid.uuid, rc);
755 err = mdt_last_rcvd_header_write(mti->mti_env, mdt, th);
759 off = med->med_lr_off;
760 LASSERT(ergo(mti->mti_transno == 0, rc != 0));
761 if (lustre_msg_get_opc(req->rq_reqmsg) == MDS_CLOSE ||
762 lustre_msg_get_opc(req->rq_reqmsg) == MDS_DONE_WRITING) {
763 if (mti->mti_transno != 0)
764 lcd->lcd_last_close_transno = mti->mti_transno;
765 lcd->lcd_last_close_xid = req->rq_xid;
766 lcd->lcd_last_close_result = rc;
768 /* VBR: save versions in last_rcvd for reconstruct. */
769 __u64 *pre_versions = lustre_msg_get_versions(req->rq_repmsg);
771 lcd->lcd_pre_versions[0] = pre_versions[0];
772 lcd->lcd_pre_versions[1] = pre_versions[1];
773 lcd->lcd_pre_versions[2] = pre_versions[2];
774 lcd->lcd_pre_versions[3] = pre_versions[3];
776 if (mti->mti_transno != 0)
777 lcd->lcd_last_transno = mti->mti_transno;
778 lcd->lcd_last_xid = req->rq_xid;
779 lcd->lcd_last_result = rc;
780 /*XXX: save intent_disposition in mdt_thread_info?
781 * also there is bug - intent_dispostion is __u64,
782 * see struct ldlm_reply->lock_policy_res1; */
783 lcd->lcd_last_data = mti->mti_opdata;
787 CERROR("client idx %d has offset %lld\n", med->med_lr_idx, off);
790 err = mdt_last_rcvd_write(mti->mti_env, mdt, lcd, &off, th);
792 mutex_up(&med->med_lcd_lock);
796 extern struct lu_context_key mdt_thread_key;
798 /* add credits for last_rcvd update */
799 static int mdt_txn_start_cb(const struct lu_env *env,
800 struct txn_param *param, void *cookie)
802 struct mdt_device *mdt = cookie;
804 param->tp_credits += mdt_trans_credit_get(env, mdt,
805 MDT_TXN_LAST_RCVD_WRITE_OP);
809 /* Set new object versions */
810 static void mdt_versions_set(struct mdt_thread_info *info)
813 for (i = 0; i < PTLRPC_NUM_VERSIONS; i++)
814 if (info->mti_mos[i] != NULL)
815 mo_version_set(info->mti_env,
816 mdt_object_child(info->mti_mos[i]),
820 /* Update last_rcvd records with latests transaction data */
821 static int mdt_txn_stop_cb(const struct lu_env *env,
822 struct thandle *txn, void *cookie)
824 struct mdt_device *mdt = cookie;
825 struct mdt_txn_info *txi;
826 struct mdt_thread_info *mti;
827 struct ptlrpc_request *req;
829 /* transno in two contexts - for commit_cb and for thread */
830 txi = lu_context_key_get(&txn->th_ctx, &mdt_txn_key);
831 mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
832 req = mdt_info_req(mti);
834 if (mti->mti_mdt == NULL || req == NULL || mti->mti_no_need_trans) {
835 txi->txi_transno = 0;
836 mti->mti_no_need_trans = 0;
840 if (mti->mti_has_trans) {
841 /* XXX: currently there are allowed cases, but the wrong cases
842 * are also possible, so better check is needed here */
843 CDEBUG(D_INFO, "More than one transaction "LPU64"\n",
848 mti->mti_has_trans = 1;
849 spin_lock(&mdt->mdt_transno_lock);
850 if (txn->th_result != 0) {
851 if (mti->mti_transno != 0) {
852 CERROR("Replay transno "LPU64" failed: rc %i\n",
853 mti->mti_transno, txn->th_result);
855 } else if (mti->mti_transno == 0) {
856 mti->mti_transno = ++ mdt->mdt_last_transno;
858 /* should be replay */
859 if (mti->mti_transno > mdt->mdt_last_transno)
860 mdt->mdt_last_transno = mti->mti_transno;
862 spin_unlock(&mdt->mdt_transno_lock);
863 /* sometimes the reply message has not been successfully packed */
864 LASSERT(req != NULL && req->rq_repmsg != NULL);
866 /** VBR: set new versions */
867 if (txn->th_result == 0)
868 mdt_versions_set(mti);
870 /* filling reply data */
871 CDEBUG(D_INODE, "transno = %llu, last_committed = %llu\n",
872 mti->mti_transno, req->rq_export->exp_obd->obd_last_committed);
874 req->rq_transno = mti->mti_transno;
875 lustre_msg_set_transno(req->rq_repmsg, mti->mti_transno);
876 /* save transno for the commit callback */
877 txi->txi_transno = mti->mti_transno;
879 /* add separate commit callback for transaction handling because we need
880 * export as parameter */
881 mdt_trans_add_cb(txn, lut_cb_last_committed,
882 class_export_cb_get(mti->mti_exp));
884 return mdt_last_rcvd_update(mti, txn);
887 /* commit callback, need to update last_committed value */
888 static int mdt_txn_commit_cb(const struct lu_env *env,
889 struct thandle *txn, void *cookie)
891 struct mdt_device *mdt = cookie;
892 struct mdt_txn_info *txi;
895 txi = lu_context_key_get(&txn->th_ctx, &mdt_txn_key);
897 /* iterate through all additional callbacks */
898 for (i = 0; i < txi->txi_cb_count; i++) {
899 txi->txi_cb[i].lut_cb_func(&mdt->mdt_lut, txi->txi_transno,
900 txi->txi_cb[i].lut_cb_data, 0);
905 int mdt_fs_setup(const struct lu_env *env, struct mdt_device *mdt,
906 struct obd_device *obd,
907 struct lustre_sb_info *lsi)
914 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_FS_SETUP))
917 /* prepare transactions callbacks */
918 mdt->mdt_txn_cb.dtc_txn_start = mdt_txn_start_cb;
919 mdt->mdt_txn_cb.dtc_txn_stop = mdt_txn_stop_cb;
920 mdt->mdt_txn_cb.dtc_txn_commit = mdt_txn_commit_cb;
921 mdt->mdt_txn_cb.dtc_cookie = mdt;
922 mdt->mdt_txn_cb.dtc_tag = LCT_MD_THREAD;
923 CFS_INIT_LIST_HEAD(&mdt->mdt_txn_cb.dtc_linkage);
925 dt_txn_callback_add(mdt->mdt_bottom, &mdt->mdt_txn_cb);
927 rc = mdt_server_data_init(env, mdt, lsi);
931 o = dt_store_open(env, mdt->mdt_bottom, "", CAPA_KEYS, &fid);
934 rc = mdt_capa_keys_init(env, mdt);
936 GOTO(put_ck_object, rc);
939 CERROR("cannot open %s: rc = %d\n", CAPA_KEYS, rc);
940 GOTO(disconnect_exports, rc);
945 lu_object_put(env, &o->do_lu);
946 mdt->mdt_ck_obj = NULL;
948 class_disconnect_exports(obd);
952 void mdt_fs_cleanup(const struct lu_env *env, struct mdt_device *mdt)
956 /* Remove transaction callback */
957 dt_txn_callback_del(mdt->mdt_bottom, &mdt->mdt_txn_cb);
959 lu_object_put(env, &mdt->mdt_ck_obj->do_lu);
960 mdt->mdt_ck_obj = NULL;
964 /* reconstruction code */
965 static void mdt_steal_ack_locks(struct ptlrpc_request *req)
967 struct obd_export *exp = req->rq_export;
968 struct list_head *tmp;
969 struct ptlrpc_reply_state *oldrep;
970 struct ptlrpc_service *svc;
973 /* CAVEAT EMPTOR: spinlock order */
974 spin_lock(&exp->exp_lock);
975 list_for_each (tmp, &exp->exp_outstanding_replies) {
976 oldrep = list_entry(tmp, struct ptlrpc_reply_state,rs_exp_list);
978 if (oldrep->rs_xid != req->rq_xid)
981 if (oldrep->rs_opc != lustre_msg_get_opc(req->rq_reqmsg))
982 CERROR ("Resent req xid "LPU64" has mismatched opc: "
983 "new %d old %d\n", req->rq_xid,
984 lustre_msg_get_opc(req->rq_reqmsg),
987 svc = oldrep->rs_service;
988 spin_lock (&svc->srv_lock);
990 list_del_init (&oldrep->rs_exp_list);
992 CWARN("Stealing %d locks from rs %p x"LPD64".t"LPD64
994 oldrep->rs_nlocks, oldrep,
995 oldrep->rs_xid, oldrep->rs_transno, oldrep->rs_opc,
996 libcfs_nid2str(exp->exp_connection->c_peer.nid));
998 for (i = 0; i < oldrep->rs_nlocks; i++)
999 ptlrpc_save_lock(req, &oldrep->rs_locks[i],
1000 oldrep->rs_modes[i], 0);
1001 oldrep->rs_nlocks = 0;
1003 DEBUG_REQ(D_HA, req, "stole locks for");
1004 spin_lock(&oldrep->rs_lock);
1005 ptlrpc_schedule_difficult_reply (oldrep);
1006 spin_unlock(&oldrep->rs_lock);
1008 spin_unlock (&svc->srv_lock);
1011 spin_unlock(&exp->exp_lock);
1015 * VBR: restore versions
1017 void mdt_vbr_reconstruct(struct ptlrpc_request *req,
1018 struct lsd_client_data *lcd)
1020 __u64 pre_versions[4] = {0};
1021 pre_versions[0] = lcd->lcd_pre_versions[0];
1022 pre_versions[1] = lcd->lcd_pre_versions[1];
1023 pre_versions[2] = lcd->lcd_pre_versions[2];
1024 pre_versions[3] = lcd->lcd_pre_versions[3];
1025 lustre_msg_set_versions(req->rq_repmsg, pre_versions);
1028 void mdt_req_from_lcd(struct ptlrpc_request *req,
1029 struct lsd_client_data *lcd)
1031 DEBUG_REQ(D_HA, req, "restoring transno "LPD64"/status %d",
1032 lcd->lcd_last_transno, lcd->lcd_last_result);
1034 if (lustre_msg_get_opc(req->rq_reqmsg) == MDS_CLOSE ||
1035 lustre_msg_get_opc(req->rq_repmsg) == MDS_DONE_WRITING) {
1036 req->rq_transno = lcd->lcd_last_close_transno;
1037 req->rq_status = lcd->lcd_last_close_result;
1039 req->rq_transno = lcd->lcd_last_transno;
1040 req->rq_status = lcd->lcd_last_result;
1041 mdt_vbr_reconstruct(req, lcd);
1043 if (req->rq_status != 0)
1044 req->rq_transno = 0;
1045 lustre_msg_set_transno(req->rq_repmsg, req->rq_transno);
1046 lustre_msg_set_status(req->rq_repmsg, req->rq_status);
1047 DEBUG_REQ(D_RPCTRACE, req, "restoring transno "LPD64"/status %d",
1048 req->rq_transno, req->rq_status);
1050 mdt_steal_ack_locks(req);
1053 void mdt_reconstruct_generic(struct mdt_thread_info *mti,
1054 struct mdt_lock_handle *lhc)
1056 struct ptlrpc_request *req = mdt_info_req(mti);
1057 struct mdt_export_data *med = &req->rq_export->exp_mdt_data;
1059 return mdt_req_from_lcd(req, med->med_lcd);
1062 static void mdt_reconstruct_create(struct mdt_thread_info *mti,
1063 struct mdt_lock_handle *lhc)
1065 struct ptlrpc_request *req = mdt_info_req(mti);
1066 struct obd_export *exp = req->rq_export;
1067 struct mdt_export_data *med = &exp->exp_mdt_data;
1068 struct mdt_device *mdt = mti->mti_mdt;
1069 struct mdt_object *child;
1070 struct mdt_body *body;
1073 mdt_req_from_lcd(req, med->med_lcd);
1077 /* if no error, so child was created with requested fid */
1078 child = mdt_object_find(mti->mti_env, mdt, mti->mti_rr.rr_fid2);
1079 if (IS_ERR(child)) {
1080 rc = PTR_ERR(child);
1081 LCONSOLE_WARN("Child "DFID" lookup error %d."
1082 " Evicting client %s with export %s.\n",
1083 PFID(mdt_object_fid(child)), rc,
1084 obd_uuid2str(&exp->exp_client_uuid),
1085 obd_export_nid2str(exp));
1086 mdt_export_evict(exp);
1091 body = req_capsule_server_get(mti->mti_pill, &RMF_MDT_BODY);
1092 mti->mti_attr.ma_need = MA_INODE;
1093 mti->mti_attr.ma_valid = 0;
1094 rc = mo_attr_get(mti->mti_env, mdt_object_child(child), &mti->mti_attr);
1095 if (rc == -EREMOTE) {
1096 /* object was created on remote server */
1097 req->rq_status = rc;
1098 body->valid |= OBD_MD_MDS;
1100 mdt_pack_attr2body(mti, body, &mti->mti_attr.ma_attr,
1101 mdt_object_fid(child));
1102 mdt_object_put(mti->mti_env, child);
1105 static void mdt_reconstruct_setattr(struct mdt_thread_info *mti,
1106 struct mdt_lock_handle *lhc)
1108 struct ptlrpc_request *req = mdt_info_req(mti);
1109 struct obd_export *exp = req->rq_export;
1110 struct mdt_export_data *med = &exp->exp_mdt_data;
1111 struct mdt_device *mdt = mti->mti_mdt;
1112 struct mdt_object *obj;
1113 struct mdt_body *body;
1115 mdt_req_from_lcd(req, med->med_lcd);
1119 body = req_capsule_server_get(mti->mti_pill, &RMF_MDT_BODY);
1120 obj = mdt_object_find(mti->mti_env, mdt, mti->mti_rr.rr_fid1);
1122 int rc = PTR_ERR(obj);
1123 LCONSOLE_WARN(""DFID" lookup error %d."
1124 " Evicting client %s with export %s.\n",
1125 PFID(mdt_object_fid(obj)), rc,
1126 obd_uuid2str(&exp->exp_client_uuid),
1127 obd_export_nid2str(exp));
1128 mdt_export_evict(exp);
1132 mti->mti_attr.ma_need = MA_INODE;
1133 mti->mti_attr.ma_valid = 0;
1134 mo_attr_get(mti->mti_env, mdt_object_child(obj), &mti->mti_attr);
1135 mdt_pack_attr2body(mti, body, &mti->mti_attr.ma_attr,
1136 mdt_object_fid(obj));
1137 if (mti->mti_epoch && (mti->mti_epoch->flags & MF_EPOCH_OPEN)) {
1138 struct mdt_file_data *mfd;
1139 struct mdt_body *repbody;
1141 repbody = req_capsule_server_get(mti->mti_pill, &RMF_MDT_BODY);
1142 repbody->ioepoch = obj->mot_ioepoch;
1143 spin_lock(&med->med_open_lock);
1144 list_for_each_entry(mfd, &med->med_open_head, mfd_list) {
1145 if (mfd->mfd_xid == req->rq_xid)
1148 LASSERT(&mfd->mfd_list != &med->med_open_head);
1149 spin_unlock(&med->med_open_lock);
1150 repbody->handle.cookie = mfd->mfd_handle.h_cookie;
1153 mdt_object_put(mti->mti_env, obj);
1156 typedef void (*mdt_reconstructor)(struct mdt_thread_info *mti,
1157 struct mdt_lock_handle *lhc);
1159 static mdt_reconstructor reconstructors[REINT_MAX] = {
1160 [REINT_SETATTR] = mdt_reconstruct_setattr,
1161 [REINT_CREATE] = mdt_reconstruct_create,
1162 [REINT_LINK] = mdt_reconstruct_generic,
1163 [REINT_UNLINK] = mdt_reconstruct_generic,
1164 [REINT_RENAME] = mdt_reconstruct_generic,
1165 [REINT_OPEN] = mdt_reconstruct_open,
1166 [REINT_SETXATTR] = mdt_reconstruct_generic
1169 void mdt_reconstruct(struct mdt_thread_info *mti,
1170 struct mdt_lock_handle *lhc)
1173 reconstructors[mti->mti_rr.rr_opcode](mti, lhc);