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 int mdt_record_read(const struct lu_env *env,
82 struct dt_object *dt, struct lu_buf *buf, loff_t *pos)
86 LASSERTF(dt != NULL, "dt is NULL when we want to read record\n");
88 rc = dt->do_body_ops->dbo_read(env, dt, buf, pos, BYPASS_CAPA);
90 if (rc == buf->lb_len)
97 int mdt_record_write(const struct lu_env *env,
98 struct dt_object *dt, const struct lu_buf *buf,
99 loff_t *pos, struct thandle *th)
103 LASSERTF(dt != NULL, "dt is NULL when we want to write record\n");
105 rc = dt->do_body_ops->dbo_write(env, dt, buf, pos, th, BYPASS_CAPA, 1);
106 if (rc == buf->lb_len)
113 static inline int mdt_trans_credit_get(const struct lu_env *env,
114 struct mdt_device *mdt,
117 struct dt_device *dev = mdt->mdt_bottom;
120 case MDT_TXN_CAPA_KEYS_WRITE_OP:
121 case MDT_TXN_LAST_RCVD_WRITE_OP:
122 cr = dev->dd_ops->dt_credit_get(env,
132 void mdt_trans_credit_init(const struct lu_env *env,
133 struct mdt_device *mdt,
136 struct mdt_thread_info *mti;
140 mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
141 p = &mti->mti_txn_param;
143 cr = mdt_trans_credit_get(env, mdt, op);
144 txn_param_init(p, cr);
147 struct thandle* mdt_trans_start(const struct lu_env *env,
148 struct mdt_device *mdt)
150 struct mdt_thread_info *mti;
153 mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
154 p = &mti->mti_txn_param;
156 /* export can require sync operations */
157 if (mti->mti_exp != NULL)
158 p->tp_sync = mti->mti_exp->exp_need_sync;
160 return mdt->mdt_bottom->dd_ops->dt_trans_start(env, mdt->mdt_bottom, p);
163 void mdt_trans_stop(const struct lu_env *env,
164 struct mdt_device *mdt, struct thandle *th)
166 mdt->mdt_bottom->dd_ops->dt_trans_stop(env, th);
169 /* last_rcvd handling */
170 static inline void lsd_le_to_cpu(struct lr_server_data *buf,
171 struct lr_server_data *lsd)
173 memcpy(lsd->lsd_uuid, buf->lsd_uuid, sizeof (lsd->lsd_uuid));
174 lsd->lsd_last_transno = le64_to_cpu(buf->lsd_last_transno);
175 lsd->lsd_mount_count = le64_to_cpu(buf->lsd_mount_count);
176 lsd->lsd_feature_compat = le32_to_cpu(buf->lsd_feature_compat);
177 lsd->lsd_feature_rocompat = le32_to_cpu(buf->lsd_feature_rocompat);
178 lsd->lsd_feature_incompat = le32_to_cpu(buf->lsd_feature_incompat);
179 lsd->lsd_server_size = le32_to_cpu(buf->lsd_server_size);
180 lsd->lsd_client_start = le32_to_cpu(buf->lsd_client_start);
181 lsd->lsd_client_size = le16_to_cpu(buf->lsd_client_size);
184 static inline void lsd_cpu_to_le(struct lr_server_data *lsd,
185 struct lr_server_data *buf)
187 memcpy(buf->lsd_uuid, lsd->lsd_uuid, sizeof (lsd->lsd_uuid));
188 buf->lsd_last_transno = cpu_to_le64(lsd->lsd_last_transno);
189 buf->lsd_mount_count = cpu_to_le64(lsd->lsd_mount_count);
190 buf->lsd_feature_compat = cpu_to_le32(lsd->lsd_feature_compat);
191 buf->lsd_feature_rocompat = cpu_to_le32(lsd->lsd_feature_rocompat);
192 buf->lsd_feature_incompat = cpu_to_le32(lsd->lsd_feature_incompat);
193 buf->lsd_server_size = cpu_to_le32(lsd->lsd_server_size);
194 buf->lsd_client_start = cpu_to_le32(lsd->lsd_client_start);
195 buf->lsd_client_size = cpu_to_le16(lsd->lsd_client_size);
198 static inline void lcd_le_to_cpu(struct lsd_client_data *buf,
199 struct lsd_client_data *lcd)
201 memcpy(lcd->lcd_uuid, buf->lcd_uuid, sizeof (lcd->lcd_uuid));
202 lcd->lcd_last_transno = le64_to_cpu(buf->lcd_last_transno);
203 lcd->lcd_last_xid = le64_to_cpu(buf->lcd_last_xid);
204 lcd->lcd_last_result = le32_to_cpu(buf->lcd_last_result);
205 lcd->lcd_last_data = le32_to_cpu(buf->lcd_last_data);
206 lcd->lcd_last_close_transno = le64_to_cpu(buf->lcd_last_close_transno);
207 lcd->lcd_last_close_xid = le64_to_cpu(buf->lcd_last_close_xid);
208 lcd->lcd_last_close_result = le32_to_cpu(buf->lcd_last_close_result);
211 static inline void lcd_cpu_to_le(struct lsd_client_data *lcd,
212 struct lsd_client_data *buf)
214 memcpy(buf->lcd_uuid, lcd->lcd_uuid, sizeof (lcd->lcd_uuid));
215 buf->lcd_last_transno = cpu_to_le64(lcd->lcd_last_transno);
216 buf->lcd_last_xid = cpu_to_le64(lcd->lcd_last_xid);
217 buf->lcd_last_result = cpu_to_le32(lcd->lcd_last_result);
218 buf->lcd_last_data = cpu_to_le32(lcd->lcd_last_data);
219 buf->lcd_last_close_transno = cpu_to_le64(lcd->lcd_last_close_transno);
220 buf->lcd_last_close_xid = cpu_to_le64(lcd->lcd_last_close_xid);
221 buf->lcd_last_close_result = cpu_to_le32(lcd->lcd_last_close_result);
224 static inline int mdt_last_rcvd_header_read(const struct lu_env *env,
225 struct mdt_device *mdt)
227 struct mdt_thread_info *mti;
230 mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
233 rc = mdt_record_read(env, mdt->mdt_last_rcvd,
234 mdt_buf(env, &mti->mti_lsd, sizeof(mti->mti_lsd)),
237 lsd_le_to_cpu(&mti->mti_lsd, &mdt->mdt_lsd);
239 CDEBUG(D_INFO, "read last_rcvd header rc = %d:\n"
241 "last_transno = "LPU64"\n",
242 rc, mdt->mdt_lsd.lsd_uuid,
243 mdt->mdt_lsd.lsd_last_transno);
247 static void mdt_client_cb(const struct mdt_device *mdt, __u64 transno,
250 struct obd_device *obd = mdt2obd_dev(mdt);
251 target_client_add_cb(obd, transno, data, err);
254 static inline int mdt_last_rcvd_header_write(const struct lu_env *env,
255 struct mdt_device *mdt,
258 struct mdt_thread_info *mti;
263 mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
266 spin_lock(&mti->mti_exp->exp_lock);
267 mti->mti_exp->exp_need_sync = need_sync;
268 spin_unlock(&mti->mti_exp->exp_lock);
270 mdt_trans_credit_init(env, mdt, MDT_TXN_LAST_RCVD_WRITE_OP);
271 th = mdt_trans_start(env, mdt);
276 lsd_cpu_to_le(&mdt->mdt_lsd, &mti->mti_lsd);
278 if (need_sync && mti->mti_exp)
279 mdt_trans_add_cb(th, mdt_client_cb, mti->mti_exp);
281 rc = mdt_record_write(env, mdt->mdt_last_rcvd,
282 mdt_buf_const(env, &mti->mti_lsd,
283 sizeof(mti->mti_lsd)),
286 mdt_trans_stop(env, mdt, th);
288 CDEBUG(D_INFO, "write last_rcvd header rc = %d:\n"
289 "uuid = %s\nlast_transno = "LPU64"\n",
290 rc, mdt->mdt_lsd.lsd_uuid, mdt->mdt_lsd.lsd_last_transno);
295 static int mdt_last_rcvd_read(const struct lu_env *env,
296 struct mdt_device *mdt,
297 struct lsd_client_data *lcd, loff_t *off)
299 struct mdt_thread_info *mti;
300 struct lsd_client_data *tmp;
303 mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
305 rc = mdt_record_read(env, mdt->mdt_last_rcvd,
306 mdt_buf(env, tmp, sizeof(*tmp)), off);
308 lcd_le_to_cpu(tmp, lcd);
310 CDEBUG(D_INFO, "read lcd @%d rc = %d:\n"
312 "last_transno = "LPU64"\n"
313 "last_xid = "LPU64"\n"
316 "last_close_transno = "LPU64"\n"
317 "last_close_xid = "LPU64"\n"
318 "last_close_result = %u\n",
319 (int)(*off - sizeof(*tmp)),
322 lcd->lcd_last_transno,
324 lcd->lcd_last_result,
326 lcd->lcd_last_close_transno,
327 lcd->lcd_last_close_xid,
328 lcd->lcd_last_close_result);
332 static int mdt_last_rcvd_write(const struct lu_env *env,
333 struct mdt_device *mdt,
334 struct lsd_client_data *lcd,
335 loff_t *off, struct thandle *th)
337 struct mdt_thread_info *mti;
338 struct lsd_client_data *tmp;
342 mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
345 lcd_cpu_to_le(lcd, tmp);
347 rc = mdt_record_write(env, mdt->mdt_last_rcvd,
348 mdt_buf_const(env, tmp, sizeof(*tmp)), off, th);
350 CDEBUG(D_INFO, "write lcd @%d rc = %d:\n"
352 "last_transno = "LPU64"\n"
353 "last_xid = "LPU64"\n"
356 "last_close_transno = "LPU64"\n"
357 "last_close_xid = "LPU64"\n"
358 "last_close_result = %u\n",
359 (int)(*off - sizeof(*tmp)),
362 lcd->lcd_last_transno,
364 lcd->lcd_last_result,
366 lcd->lcd_last_close_transno,
367 lcd->lcd_last_close_xid,
368 lcd->lcd_last_close_result);
372 static int mdt_clients_data_init(const struct lu_env *env,
373 struct mdt_device *mdt,
374 unsigned long last_size)
376 struct lr_server_data *lsd = &mdt->mdt_lsd;
377 struct lsd_client_data *lcd = NULL;
378 struct obd_device *obd = mdt2obd_dev(mdt);
384 /* When we do a clean MDS shutdown, we save the last_transno into
385 * the header. If we find clients with higher last_transno values
386 * then those clients may need recovery done. */
387 LASSERT(atomic_read(&obd->obd_req_replay_clients) == 0);
388 for (cl_idx = 0, off = lsd->lsd_client_start;
389 off < last_size; cl_idx++) {
391 struct obd_export *exp;
399 off = lsd->lsd_client_start +
400 cl_idx * lsd->lsd_client_size;
402 rc = mdt_last_rcvd_read(env, mdt, lcd, &off);
404 CERROR("error reading MDS %s idx %d, off %llu: rc %d\n",
405 LAST_RCVD, cl_idx, off, rc);
407 break; /* read error shouldn't cause startup to fail */
410 if (lcd->lcd_uuid[0] == '\0') {
411 CDEBUG(D_INFO, "skipping zeroed client at offset %d\n",
416 last_transno = lcd_last_transno(lcd);
418 /* These exports are cleaned up by mdt_obd_disconnect(), so
419 * they need to be set up like real exports as
420 * mdt_obd_connect() does.
422 CDEBUG(D_HA, "RCVRNG CLIENT uuid: %s idx: %d lr: "LPU64
423 " srv lr: "LPU64" lx: "LPU64"\n", lcd->lcd_uuid, cl_idx,
424 last_transno, lsd->lsd_last_transno,
427 exp = class_new_export(obd, (struct obd_uuid *)lcd->lcd_uuid);
429 if (PTR_ERR(exp) == -EALREADY) {
430 /* export already exists, zero out this one */
431 lcd->lcd_uuid[0] = '\0';
433 GOTO(err_client, rc = PTR_ERR(exp));
435 struct mdt_thread_info *mti;
436 mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
437 LASSERT(mti != NULL);
439 exp->exp_mdt_data.med_lcd = lcd;
440 rc = mdt_client_add(env, mdt, cl_idx);
441 /* can't fail existing */
442 LASSERTF(rc == 0, "rc = %d\n", rc);
444 spin_lock(&exp->exp_lock);
445 exp->exp_connecting = 0;
446 exp->exp_in_recovery = 0;
447 spin_unlock(&exp->exp_lock);
448 obd->obd_max_recoverable_clients++;
449 class_export_put(exp);
452 CDEBUG(D_OTHER, "client at idx %d has last_transno="LPU64"\n",
453 cl_idx, last_transno);
454 /* protect __u64 value update */
455 spin_lock(&mdt->mdt_transno_lock);
456 mdt->mdt_last_transno = max(last_transno,
457 mdt->mdt_last_transno);
458 spin_unlock(&mdt->mdt_transno_lock);
467 static int mdt_server_data_init(const struct lu_env *env,
468 struct mdt_device *mdt,
469 struct lustre_sb_info *lsi)
471 struct lr_server_data *lsd = &mdt->mdt_lsd;
472 struct lsd_client_data *lcd = NULL;
473 struct obd_device *obd = mdt2obd_dev(mdt);
474 struct mdt_thread_info *mti;
475 struct dt_object *obj;
477 struct lustre_disk_data *ldd;
478 unsigned long last_rcvd_size;
483 /* ensure padding in the struct is the correct size */
484 CLASSERT(offsetof(struct lr_server_data, lsd_padding) +
485 sizeof(lsd->lsd_padding) == LR_SERVER_SIZE);
486 CLASSERT(offsetof(struct lsd_client_data, lcd_padding) +
487 sizeof(lcd->lcd_padding) == LR_CLIENT_SIZE);
489 mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
490 LASSERT(mti != NULL);
491 la = &mti->mti_attr.ma_attr;
493 obj = mdt->mdt_last_rcvd;
494 rc = obj->do_ops->do_attr_get(env, mdt->mdt_last_rcvd, la, BYPASS_CAPA);
498 last_rcvd_size = (unsigned long)la->la_size;
500 if (last_rcvd_size == 0) {
501 LCONSOLE_WARN("%s: new disk, initializing\n", obd->obd_name);
503 memcpy(lsd->lsd_uuid, obd->obd_uuid.uuid,
504 sizeof(lsd->lsd_uuid));
505 lsd->lsd_last_transno = 0;
506 lsd->lsd_mount_count = 0;
507 lsd->lsd_server_size = LR_SERVER_SIZE;
508 lsd->lsd_client_start = LR_CLIENT_START;
509 lsd->lsd_client_size = LR_CLIENT_SIZE;
510 lsd->lsd_feature_rocompat = OBD_ROCOMPAT_LOVOBJID;
511 lsd->lsd_feature_incompat = OBD_INCOMPAT_MDT |
512 OBD_INCOMPAT_COMMON_LR;
514 LCONSOLE_WARN("%s: used disk, loading\n", obd->obd_name);
515 rc = mdt_last_rcvd_header_read(env, mdt);
517 CERROR("error reading MDS %s: rc %d\n", LAST_RCVD, rc);
520 if (strcmp(lsd->lsd_uuid, obd->obd_uuid.uuid) != 0) {
521 LCONSOLE_ERROR_MSG(0x157, "Trying to start OBD %s using"
522 "the wrong disk %s. Were the /dev/ "
523 "assignments rearranged?\n",
524 obd->obd_uuid.uuid, lsd->lsd_uuid);
525 GOTO(out, rc = -EINVAL);
528 mount_count = lsd->lsd_mount_count;
532 if (ldd->ldd_flags & LDD_F_IAM_DIR)
533 lsd->lsd_feature_incompat |= OBD_INCOMPAT_IAM_DIR;
535 lsd->lsd_feature_compat = OBD_COMPAT_MDT;
536 lsd->lsd_feature_incompat |= OBD_INCOMPAT_FID;
538 spin_lock(&mdt->mdt_transno_lock);
539 mdt->mdt_last_transno = lsd->lsd_last_transno;
540 spin_unlock(&mdt->mdt_transno_lock);
542 CDEBUG(D_INODE, "========BEGIN DUMPING LAST_RCVD========\n");
543 CDEBUG(D_INODE, "%s: server last_transno: "LPU64"\n",
544 obd->obd_name, mdt->mdt_last_transno);
545 CDEBUG(D_INODE, "%s: server mount_count: "LPU64"\n",
546 obd->obd_name, mount_count + 1);
547 CDEBUG(D_INODE, "%s: server data size: %u\n",
548 obd->obd_name, lsd->lsd_server_size);
549 CDEBUG(D_INODE, "%s: per-client data start: %u\n",
550 obd->obd_name, lsd->lsd_client_start);
551 CDEBUG(D_INODE, "%s: per-client data size: %u\n",
552 obd->obd_name, lsd->lsd_client_size);
553 CDEBUG(D_INODE, "%s: last_rcvd size: %lu\n",
554 obd->obd_name, last_rcvd_size);
555 CDEBUG(D_INODE, "%s: last_rcvd clients: %lu\n", obd->obd_name,
556 last_rcvd_size <= lsd->lsd_client_start ? 0 :
557 (last_rcvd_size - lsd->lsd_client_start) /
558 lsd->lsd_client_size);
559 CDEBUG(D_INODE, "========END DUMPING LAST_RCVD========\n");
561 if (!lsd->lsd_server_size || !lsd->lsd_client_start ||
562 !lsd->lsd_client_size) {
563 CERROR("Bad last_rcvd contents!\n");
564 GOTO(out, rc = -EINVAL);
567 rc = mdt_clients_data_init(env, mdt, last_rcvd_size);
569 GOTO(err_client, rc);
571 spin_lock(&mdt->mdt_transno_lock);
572 /* obd_last_committed is used for compatibility
573 * with other lustre recovery code */
574 obd->obd_last_committed = mdt->mdt_last_transno;
575 spin_unlock(&mdt->mdt_transno_lock);
577 mdt->mdt_mount_count++;
578 lsd->lsd_mount_count = mdt->mdt_mount_count;
580 /* save it, so mount count and last_transno is current */
581 rc = mdt_server_data_update(env, mdt, (mti->mti_exp &&
582 mti->mti_exp->exp_need_sync));
584 GOTO(err_client, rc);
589 target_recovery_fini(obd);
594 static int mdt_server_data_update(const struct lu_env *env,
595 struct mdt_device *mdt,
601 CDEBUG(D_SUPER, "MDS mount_count is "LPU64", last_transno is "LPU64"\n",
602 mdt->mdt_mount_count, mdt->mdt_last_transno);
604 spin_lock(&mdt->mdt_transno_lock);
605 mdt->mdt_lsd.lsd_last_transno = mdt->mdt_last_transno;
606 spin_unlock(&mdt->mdt_transno_lock);
609 * This may be called from difficult reply handler and
610 * mdt->mdt_last_rcvd may be NULL that time.
612 if (mdt->mdt_last_rcvd != NULL)
613 rc = mdt_last_rcvd_header_write(env, mdt, need_sync);
617 int mdt_client_new(const struct lu_env *env, struct mdt_device *mdt)
619 unsigned long *bitmap = mdt->mdt_client_bitmap;
620 struct mdt_thread_info *mti;
621 struct mdt_export_data *med;
622 struct lsd_client_data *lcd;
623 struct lr_server_data *lsd = &mdt->mdt_lsd;
624 struct obd_device *obd = mdt2obd_dev(mdt);
631 mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
632 LASSERT(mti != NULL);
634 med = &mti->mti_exp->exp_mdt_data;
637 LASSERT(bitmap != NULL);
638 if (!strcmp(med->med_lcd->lcd_uuid, obd->obd_uuid.uuid))
641 /* the bitmap operations can handle cl_idx > sizeof(long) * 8, so
642 * there's no need for extra complication here
644 spin_lock(&mdt->mdt_client_bitmap_lock);
645 cl_idx = find_first_zero_bit(bitmap, LR_MAX_CLIENTS);
646 if (cl_idx >= LR_MAX_CLIENTS ||
647 OBD_FAIL_CHECK(OBD_FAIL_MDS_CLIENT_ADD)) {
648 CERROR("no room for %u clients - fix LR_MAX_CLIENTS\n",
650 spin_unlock(&mdt->mdt_client_bitmap_lock);
653 set_bit(cl_idx, bitmap);
654 spin_unlock(&mdt->mdt_client_bitmap_lock);
656 CDEBUG(D_INFO, "client at idx %d with UUID '%s' added\n",
657 cl_idx, med->med_lcd->lcd_uuid);
659 med->med_lr_idx = cl_idx;
660 med->med_lr_off = lsd->lsd_client_start +
661 (cl_idx * lsd->lsd_client_size);
662 init_mutex(&med->med_lcd_lock);
664 LASSERTF(med->med_lr_off > 0, "med_lr_off = %llu\n", med->med_lr_off);
666 /* Write new client data. */
667 off = med->med_lr_off;
668 mdt_trans_credit_init(env, mdt, MDT_TXN_LAST_RCVD_WRITE_OP);
670 th = mdt_trans_start(env, mdt);
675 * Until this operations will be committed the sync is needed
676 * for this export. This should be done _after_ starting the
677 * transaction so that many connecting clients will not bring
678 * server down with lots of sync writes.
680 mdt_trans_add_cb(th, mdt_client_cb, mti->mti_exp);
681 spin_lock(&mti->mti_exp->exp_lock);
682 mti->mti_exp->exp_need_sync = 1;
683 spin_unlock(&mti->mti_exp->exp_lock);
685 rc = mdt_last_rcvd_write(env, mdt, lcd, &off, th);
686 CDEBUG(D_INFO, "wrote client lcd at idx %u off %llu (len "LPSZ")\n",
687 cl_idx, med->med_lr_off, sizeof(*lcd));
688 mdt_trans_stop(env, mdt, th);
693 /* Add client data to the MDS. We use a bitmap to locate a free space
694 * in the last_rcvd file if cl_off is -1 (i.e. a new client).
695 * Otherwise, we just have to read the data from the last_rcvd file and
696 * we know its offset.
698 * It should not be possible to fail adding an existing client - otherwise
699 * mdt_init_server_data() callsite needs to be fixed.
701 int mdt_client_add(const struct lu_env *env,
702 struct mdt_device *mdt, int cl_idx)
704 struct mdt_thread_info *mti;
705 struct mdt_export_data *med;
706 unsigned long *bitmap = mdt->mdt_client_bitmap;
707 struct obd_device *obd = mdt2obd_dev(mdt);
708 struct lr_server_data *lsd = &mdt->mdt_lsd;
712 mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
713 LASSERT(mti != NULL);
715 med = &mti->mti_exp->exp_mdt_data;
717 LASSERT(bitmap != NULL);
718 LASSERTF(cl_idx >= 0, "%d\n", cl_idx);
720 if (!strcmp(med->med_lcd->lcd_uuid, obd->obd_uuid.uuid))
723 spin_lock(&mdt->mdt_client_bitmap_lock);
724 if (test_and_set_bit(cl_idx, bitmap)) {
725 CERROR("MDS client %d: bit already set in bitmap!!\n",
729 spin_unlock(&mdt->mdt_client_bitmap_lock);
731 CDEBUG(D_INFO, "client at idx %d with UUID '%s' added\n",
732 cl_idx, med->med_lcd->lcd_uuid);
734 med->med_lr_idx = cl_idx;
735 med->med_lr_off = lsd->lsd_client_start +
736 (cl_idx * lsd->lsd_client_size);
737 init_mutex(&med->med_lcd_lock);
739 LASSERTF(med->med_lr_off > 0, "med_lr_off = %llu\n", med->med_lr_off);
744 int mdt_client_del(const struct lu_env *env, struct mdt_device *mdt)
746 struct mdt_thread_info *mti;
747 struct mdt_export_data *med;
748 struct lsd_client_data *lcd;
749 struct obd_device *obd = mdt2obd_dev(mdt);
750 struct obd_export *exp;
757 mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
758 LASSERT(mti != NULL);
761 med = &exp->exp_mdt_data;
766 /* XXX: If lcd_uuid were a real obd_uuid, I could use obd_uuid_equals */
767 if (!strcmp(lcd->lcd_uuid, obd->obd_uuid.uuid))
770 CDEBUG(D_INFO, "freeing client at idx %u, offset %lld\n",
771 med->med_lr_idx, med->med_lr_off);
773 off = med->med_lr_off;
776 * Don't clear med_lr_idx here as it is likely also unset. At worst we
777 * leak a client slot that will be cleaned on the next recovery.
780 CERROR("client idx %d has offset %lld\n",
781 med->med_lr_idx, off);
782 GOTO(free, rc = -EINVAL);
786 * Clear the bit _after_ zeroing out the client so we don't race with
787 * mdt_client_add and zero out new clients.
789 if (!test_bit(med->med_lr_idx, mdt->mdt_client_bitmap)) {
790 CERROR("MDT client %u: bit already clear in bitmap!!\n",
795 /* Don't force sync on disconnect if aborting recovery,
796 * or it does num_clients * num_osts. b=17194 */
797 need_sync = (!exp->exp_libclient || exp->exp_need_sync) &&
798 !(exp->exp_flags & OBD_OPT_ABORT_RECOV);
801 * This may be called from difficult reply handler path and
802 * mdt->mdt_last_rcvd may be NULL that time.
804 if (mdt->mdt_last_rcvd != NULL) {
805 mdt_trans_credit_init(env, mdt, MDT_TXN_LAST_RCVD_WRITE_OP);
807 spin_lock(&exp->exp_lock);
808 exp->exp_need_sync = need_sync;
809 spin_unlock(&exp->exp_lock);
811 th = mdt_trans_start(env, mdt);
813 GOTO(free, rc = PTR_ERR(th));
817 * Until this operations will be committed the sync
818 * is needed for this export.
820 mdt_trans_add_cb(th, mdt_client_cb, exp);
823 mutex_down(&med->med_lcd_lock);
824 memset(lcd, 0, sizeof *lcd);
826 rc = mdt_last_rcvd_write(env, mdt, lcd, &off, th);
827 mutex_up(&med->med_lcd_lock);
828 mdt_trans_stop(env, mdt, th);
831 CDEBUG(rc == 0 ? D_INFO : D_ERROR, "Zeroing out client idx %u in "
832 "%s %ssync rc %d\n", med->med_lr_idx, LAST_RCVD,
833 need_sync ? "" : "a", rc);
835 spin_lock(&mdt->mdt_client_bitmap_lock);
836 clear_bit(med->med_lr_idx, mdt->mdt_client_bitmap);
837 spin_unlock(&mdt->mdt_client_bitmap_lock);
840 * Make sure the server's last_transno is up to date. Do this
841 * after the client is freed so we know all the client's
842 * transactions have been committed.
844 mdt_server_data_update(env, mdt, need_sync);
854 * last_rcvd & last_committed update callbacks
856 static int mdt_last_rcvd_update(struct mdt_thread_info *mti,
859 struct mdt_device *mdt = mti->mti_mdt;
860 struct ptlrpc_request *req = mdt_info_req(mti);
861 struct mdt_export_data *med;
862 struct lsd_client_data *lcd;
865 __s32 rc = th->th_result;
870 LASSERT(req->rq_export);
872 med = &req->rq_export->exp_mdt_data;
875 /* if the export has already been failed, we have no last_rcvd slot */
876 if (req->rq_export->exp_failed) {
877 CWARN("commit transaction for disconnected client %s: rc %d\n",
878 req->rq_export->exp_client_uuid.uuid, rc);
884 off = med->med_lr_off;
885 mutex_down(&med->med_lcd_lock);
886 if (lustre_msg_get_opc(req->rq_reqmsg) == MDS_CLOSE ||
887 lustre_msg_get_opc(req->rq_reqmsg) == MDS_DONE_WRITING) {
888 transno_p = &lcd->lcd_last_close_transno;
889 lcd->lcd_last_close_xid = req->rq_xid;
890 lcd->lcd_last_close_result = rc;
892 transno_p = &lcd->lcd_last_transno;
893 lcd->lcd_last_xid = req->rq_xid;
894 lcd->lcd_last_result = rc;
895 /*XXX: save intent_disposition in mdt_thread_info?
896 * also there is bug - intent_dispostion is __u64,
897 * see struct ldlm_reply->lock_policy_res1; */
898 lcd->lcd_last_data = mti->mti_opdata;
902 * When we store zero transno in lcd we can lost last transno value
903 * because lcd contains 0, but lsd is not yet written
904 * The server data should be updated also if the latest
905 * transno is rewritten by zero. See the bug 11125 for details.
907 if (mti->mti_transno == 0 &&
908 *transno_p == mdt->mdt_last_transno)
909 mdt_server_data_update(mti->mti_env, mdt,
911 mti->mti_exp->exp_need_sync));
913 *transno_p = mti->mti_transno;
916 CERROR("client idx %d has offset %lld\n", med->med_lr_idx, off);
919 err = mdt_last_rcvd_write(mti->mti_env, mdt, lcd, &off, th);
921 mutex_up(&med->med_lcd_lock);
925 extern struct lu_context_key mdt_thread_key;
927 /* add credits for last_rcvd update */
928 static int mdt_txn_start_cb(const struct lu_env *env,
929 struct txn_param *param, void *cookie)
931 struct mdt_device *mdt = cookie;
933 param->tp_credits += mdt_trans_credit_get(env, mdt,
934 MDT_TXN_LAST_RCVD_WRITE_OP);
938 /* Update last_rcvd records with latests transaction data */
939 static int mdt_txn_stop_cb(const struct lu_env *env,
940 struct thandle *txn, void *cookie)
942 struct mdt_device *mdt = cookie;
943 struct mdt_txn_info *txi;
944 struct mdt_thread_info *mti;
945 struct ptlrpc_request *req;
947 /* transno in two contexts - for commit_cb and for thread */
948 txi = lu_context_key_get(&txn->th_ctx, &mdt_txn_key);
949 mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
950 req = mdt_info_req(mti);
952 if (mti->mti_mdt == NULL || req == NULL || mti->mti_no_need_trans) {
953 txi->txi_transno = 0;
954 mti->mti_no_need_trans = 0;
958 if (mti->mti_has_trans) {
959 /* XXX: currently there are allowed cases, but the wrong cases
960 * are also possible, so better check is needed here */
961 CDEBUG(D_INFO, "More than one transaction "LPU64"\n",
966 mti->mti_has_trans = 1;
967 spin_lock(&mdt->mdt_transno_lock);
968 if (txn->th_result != 0) {
969 if (mti->mti_transno != 0) {
970 CERROR("Replay transno "LPU64" failed: rc %i\n",
971 mti->mti_transno, txn->th_result);
972 mti->mti_transno = 0;
974 } else if (mti->mti_transno == 0) {
975 mti->mti_transno = ++ mdt->mdt_last_transno;
977 /* should be replay */
978 if (mti->mti_transno > mdt->mdt_last_transno)
979 mdt->mdt_last_transno = mti->mti_transno;
982 /* sometimes the reply message has not been successfully packed */
983 LASSERT(req != NULL && req->rq_repmsg != NULL);
985 /* filling reply data */
986 CDEBUG(D_INODE, "transno = %llu, last_committed = %llu\n",
987 mti->mti_transno, req->rq_export->exp_obd->obd_last_committed);
989 req->rq_transno = mti->mti_transno;
990 lustre_msg_set_transno(req->rq_repmsg, mti->mti_transno);
991 lustre_msg_set_last_xid(req->rq_repmsg,
992 lcd_last_xid(req->rq_export->exp_mdt_data.med_lcd));
993 /* save transno for the commit callback */
994 txi->txi_transno = mti->mti_transno;
995 spin_unlock(&mdt->mdt_transno_lock);
997 return mdt_last_rcvd_update(mti, txn);
1000 /* commit callback, need to update last_committed value */
1001 static int mdt_txn_commit_cb(const struct lu_env *env,
1002 struct thandle *txn, void *cookie)
1004 struct mdt_device *mdt = cookie;
1005 struct obd_device *obd = mdt2obd_dev(mdt);
1006 struct mdt_txn_info *txi;
1009 txi = lu_context_key_get(&txn->th_ctx, &mdt_txn_key);
1011 /* copy of obd_transno_commit_cb() but with locking */
1012 spin_lock(&mdt->mdt_transno_lock);
1013 if (txi->txi_transno > obd->obd_last_committed) {
1014 obd->obd_last_committed = txi->txi_transno;
1015 spin_unlock(&mdt->mdt_transno_lock);
1016 ptlrpc_commit_replies(obd);
1018 spin_unlock(&mdt->mdt_transno_lock);
1020 if (txi->txi_transno)
1021 CDEBUG(D_HA, "%s: transno "LPD64" is committed\n",
1022 obd->obd_name, txi->txi_transno);
1024 /* iterate through all additional callbacks */
1025 for (i = 0; i < txi->txi_cb_count; i++) {
1026 txi->txi_cb[i].mdt_cb_func(mdt, txi->txi_transno,
1027 txi->txi_cb[i].mdt_cb_data, 0);
1032 int mdt_fs_setup(const struct lu_env *env, struct mdt_device *mdt,
1033 struct obd_device *obd,
1034 struct lustre_sb_info *lsi)
1037 struct dt_object *o;
1041 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_FS_SETUP))
1044 /* prepare transactions callbacks */
1045 mdt->mdt_txn_cb.dtc_txn_start = mdt_txn_start_cb;
1046 mdt->mdt_txn_cb.dtc_txn_stop = mdt_txn_stop_cb;
1047 mdt->mdt_txn_cb.dtc_txn_commit = mdt_txn_commit_cb;
1048 mdt->mdt_txn_cb.dtc_cookie = mdt;
1049 CFS_INIT_LIST_HEAD(&mdt->mdt_txn_cb.dtc_linkage);
1051 dt_txn_callback_add(mdt->mdt_bottom, &mdt->mdt_txn_cb);
1053 o = dt_store_open(env, mdt->mdt_bottom, "", LAST_RCVD, &fid);
1055 mdt->mdt_last_rcvd = o;
1056 rc = mdt_server_data_init(env, mdt, lsi);
1058 GOTO(put_last_rcvd, rc);
1061 CERROR("cannot open %s: rc = %d\n", LAST_RCVD, rc);
1065 o = dt_store_open(env, mdt->mdt_bottom, "", CAPA_KEYS, &fid);
1067 mdt->mdt_ck_obj = o;
1068 rc = mdt_capa_keys_init(env, mdt);
1070 GOTO(put_ck_object, rc);
1073 CERROR("cannot open %s: rc = %d\n", CAPA_KEYS, rc);
1074 GOTO(put_last_rcvd, rc);
1079 lu_object_put(env, &o->do_lu);
1080 mdt->mdt_ck_obj = NULL;
1082 lu_object_put(env, &mdt->mdt_last_rcvd->do_lu);
1083 mdt->mdt_last_rcvd = NULL;
1087 void mdt_fs_cleanup(const struct lu_env *env, struct mdt_device *mdt)
1091 /* Remove transaction callback */
1092 dt_txn_callback_del(mdt->mdt_bottom, &mdt->mdt_txn_cb);
1093 if (mdt->mdt_last_rcvd)
1094 lu_object_put(env, &mdt->mdt_last_rcvd->do_lu);
1095 mdt->mdt_last_rcvd = NULL;
1096 if (mdt->mdt_ck_obj)
1097 lu_object_put(env, &mdt->mdt_ck_obj->do_lu);
1098 mdt->mdt_ck_obj = NULL;
1102 /* reconstruction code */
1103 static void mdt_steal_ack_locks(struct ptlrpc_request *req)
1105 struct obd_export *exp = req->rq_export;
1106 struct list_head *tmp;
1107 struct ptlrpc_reply_state *oldrep;
1108 struct ptlrpc_service *svc;
1111 /* CAVEAT EMPTOR: spinlock order */
1112 spin_lock(&exp->exp_lock);
1113 list_for_each (tmp, &exp->exp_outstanding_replies) {
1114 oldrep = list_entry(tmp, struct ptlrpc_reply_state,rs_exp_list);
1116 if (oldrep->rs_xid != req->rq_xid)
1119 if (lustre_msg_get_opc(oldrep->rs_msg) !=
1120 lustre_msg_get_opc(req->rq_reqmsg))
1121 CERROR ("Resent req xid "LPX64" has mismatched opc: "
1122 "new %d old %d\n", req->rq_xid,
1123 lustre_msg_get_opc(req->rq_reqmsg),
1124 lustre_msg_get_opc(oldrep->rs_msg));
1126 svc = oldrep->rs_service;
1127 spin_lock (&svc->srv_lock);
1129 list_del_init (&oldrep->rs_exp_list);
1131 CWARN("Stealing %d locks from rs %p x"LPD64".t"LPD64
1133 oldrep->rs_nlocks, oldrep,
1134 oldrep->rs_xid, oldrep->rs_transno,
1135 lustre_msg_get_opc(oldrep->rs_msg),
1136 libcfs_nid2str(exp->exp_connection->c_peer.nid));
1138 for (i = 0; i < oldrep->rs_nlocks; i++)
1139 ptlrpc_save_lock(req, &oldrep->rs_locks[i],
1140 oldrep->rs_modes[i], 0);
1141 oldrep->rs_nlocks = 0;
1143 DEBUG_REQ(D_HA, req, "stole locks for");
1144 spin_lock(&oldrep->rs_lock);
1145 ptlrpc_schedule_difficult_reply (oldrep);
1146 spin_unlock(&oldrep->rs_lock);
1148 spin_unlock (&svc->srv_lock);
1151 spin_unlock(&exp->exp_lock);
1154 void mdt_req_from_lcd(struct ptlrpc_request *req,
1155 struct lsd_client_data *lcd)
1157 DEBUG_REQ(D_HA, req, "restoring transno "LPD64"/status %d",
1158 lcd->lcd_last_transno, lcd->lcd_last_result);
1160 if (lustre_msg_get_opc(req->rq_reqmsg) == MDS_CLOSE ||
1161 lustre_msg_get_opc(req->rq_repmsg) == MDS_DONE_WRITING) {
1162 req->rq_transno = lcd->lcd_last_close_transno;
1163 req->rq_status = lcd->lcd_last_close_result;
1164 lustre_msg_set_transno(req->rq_repmsg, req->rq_transno);
1165 lustre_msg_set_status(req->rq_repmsg, req->rq_status);
1167 req->rq_transno = lcd->lcd_last_transno;
1168 req->rq_status = lcd->lcd_last_result;
1169 lustre_msg_set_transno(req->rq_repmsg, req->rq_transno);
1170 lustre_msg_set_status(req->rq_repmsg, req->rq_status);
1172 mdt_steal_ack_locks(req);
1175 void mdt_reconstruct_generic(struct mdt_thread_info *mti,
1176 struct mdt_lock_handle *lhc)
1178 struct ptlrpc_request *req = mdt_info_req(mti);
1179 struct mdt_export_data *med = &req->rq_export->exp_mdt_data;
1181 return mdt_req_from_lcd(req, med->med_lcd);
1184 static void mdt_reconstruct_create(struct mdt_thread_info *mti,
1185 struct mdt_lock_handle *lhc)
1187 struct ptlrpc_request *req = mdt_info_req(mti);
1188 struct obd_export *exp = req->rq_export;
1189 struct mdt_export_data *med = &exp->exp_mdt_data;
1190 struct mdt_device *mdt = mti->mti_mdt;
1191 struct mdt_object *child;
1192 struct mdt_body *body;
1195 mdt_req_from_lcd(req, med->med_lcd);
1199 /* if no error, so child was created with requested fid */
1200 child = mdt_object_find(mti->mti_env, mdt, mti->mti_rr.rr_fid2);
1201 if (IS_ERR(child)) {
1202 rc = PTR_ERR(child);
1203 LCONSOLE_WARN("Child "DFID" lookup error %d."
1204 " Evicting client %s with export %s.\n",
1205 PFID(mdt_object_fid(child)), rc,
1206 obd_uuid2str(&exp->exp_client_uuid),
1207 obd_export_nid2str(exp));
1208 mdt_export_evict(exp);
1213 body = req_capsule_server_get(mti->mti_pill, &RMF_MDT_BODY);
1214 mti->mti_attr.ma_need = MA_INODE;
1215 mti->mti_attr.ma_valid = 0;
1216 rc = mo_attr_get(mti->mti_env, mdt_object_child(child), &mti->mti_attr);
1217 if (rc == -EREMOTE) {
1218 /* object was created on remote server */
1219 req->rq_status = rc;
1220 body->valid |= OBD_MD_MDS;
1222 mdt_pack_attr2body(mti, body, &mti->mti_attr.ma_attr,
1223 mdt_object_fid(child));
1224 mdt_object_put(mti->mti_env, child);
1227 static void mdt_reconstruct_setattr(struct mdt_thread_info *mti,
1228 struct mdt_lock_handle *lhc)
1230 struct ptlrpc_request *req = mdt_info_req(mti);
1231 struct obd_export *exp = req->rq_export;
1232 struct mdt_export_data *med = &exp->exp_mdt_data;
1233 struct mdt_device *mdt = mti->mti_mdt;
1234 struct mdt_object *obj;
1235 struct mdt_body *body;
1237 mdt_req_from_lcd(req, med->med_lcd);
1241 body = req_capsule_server_get(mti->mti_pill, &RMF_MDT_BODY);
1242 obj = mdt_object_find(mti->mti_env, mdt, mti->mti_rr.rr_fid1);
1244 int rc = PTR_ERR(obj);
1245 LCONSOLE_WARN(""DFID" lookup error %d."
1246 " Evicting client %s with export %s.\n",
1247 PFID(mdt_object_fid(obj)), rc,
1248 obd_uuid2str(&exp->exp_client_uuid),
1249 obd_export_nid2str(exp));
1250 mdt_export_evict(exp);
1254 mti->mti_attr.ma_need = MA_INODE;
1255 mti->mti_attr.ma_valid = 0;
1256 mo_attr_get(mti->mti_env, mdt_object_child(obj), &mti->mti_attr);
1257 mdt_pack_attr2body(mti, body, &mti->mti_attr.ma_attr,
1258 mdt_object_fid(obj));
1259 if (mti->mti_epoch && (mti->mti_epoch->flags & MF_EPOCH_OPEN)) {
1260 struct mdt_file_data *mfd;
1261 struct mdt_body *repbody;
1263 repbody = req_capsule_server_get(mti->mti_pill, &RMF_MDT_BODY);
1264 repbody->ioepoch = obj->mot_ioepoch;
1265 spin_lock(&med->med_open_lock);
1266 list_for_each_entry(mfd, &med->med_open_head, mfd_list) {
1267 if (mfd->mfd_xid == req->rq_xid)
1270 LASSERT(&mfd->mfd_list != &med->med_open_head);
1271 spin_unlock(&med->med_open_lock);
1272 repbody->handle.cookie = mfd->mfd_handle.h_cookie;
1275 mdt_object_put(mti->mti_env, obj);
1278 typedef void (*mdt_reconstructor)(struct mdt_thread_info *mti,
1279 struct mdt_lock_handle *lhc);
1281 static mdt_reconstructor reconstructors[REINT_MAX] = {
1282 [REINT_SETATTR] = mdt_reconstruct_setattr,
1283 [REINT_CREATE] = mdt_reconstruct_create,
1284 [REINT_LINK] = mdt_reconstruct_generic,
1285 [REINT_UNLINK] = mdt_reconstruct_generic,
1286 [REINT_RENAME] = mdt_reconstruct_generic,
1287 [REINT_OPEN] = mdt_reconstruct_open,
1288 [REINT_SETXATTR] = mdt_reconstruct_generic
1291 void mdt_reconstruct(struct mdt_thread_info *mti,
1292 struct mdt_lock_handle *lhc)
1295 reconstructors[mti->mti_rr.rr_opcode](mti, lhc);