1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * linux/mdt/mdt_recovery.c
5 * Lustre Metadata Target (mdt) recovery-related methods
7 * Copyright (C) 2002-2006 Cluster File Systems, Inc.
8 * Author: Huang Hua <huanghua@clusterfs.com>
9 * Author: Pershin Mike <tappro@clusterfs.com>
11 * This file is part of the Lustre file system, http://www.lustre.org
12 * Lustre is a trademark of Cluster File Systems, Inc.
14 * You may have signed or agreed to another license before downloading
15 * this software. If so, you are bound by the terms and conditions
16 * of that agreement, and the following does not apply to you. See the
17 * LICENSE file included with this distribution for more information.
19 * If you did not agree to a different license, then this copy of Lustre
20 * is open source software; you can redistribute it and/or modify it
21 * under the terms of version 2 of the GNU General Public License as
22 * published by the Free Software Foundation.
24 * In either case, Lustre is distributed in the hope that it will be
25 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
26 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * license text for more details.
30 # define EXPORT_SYMTAB
32 #define DEBUG_SUBSYSTEM S_MDS
34 #include "mdt_internal.h"
36 static int mdt_server_data_update(const struct lu_env *env,
37 struct mdt_device *mdt);
39 struct lu_buf *mdt_buf(const struct lu_env *env, void *area, ssize_t len)
42 struct mdt_thread_info *mti;
44 mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
51 const struct lu_buf *mdt_buf_const(const struct lu_env *env,
52 const void *area, ssize_t len)
55 struct mdt_thread_info *mti;
57 mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
60 buf->lb_buf = (void *)area;
65 int mdt_record_read(const struct lu_env *env,
66 struct dt_object *dt, struct lu_buf *buf, loff_t *pos)
70 LASSERTF(dt != NULL, "dt is NULL when we want to read record\n");
72 rc = dt->do_body_ops->dbo_read(env, dt, buf, pos, BYPASS_CAPA);
74 if (rc == buf->lb_len)
81 int mdt_record_write(const struct lu_env *env,
82 struct dt_object *dt, const struct lu_buf *buf,
83 loff_t *pos, struct thandle *th)
87 LASSERTF(dt != NULL, "dt is NULL when we want to write record\n");
89 rc = dt->do_body_ops->dbo_write(env, dt, buf, pos, th, BYPASS_CAPA);
90 if (rc == buf->lb_len)
96 /* only one record write */
99 MDT_TXN_LAST_RCVD_WRITE_CREDITS = 3
102 struct thandle* mdt_trans_start(const struct lu_env *env,
103 struct mdt_device *mdt, int credits)
105 struct mdt_thread_info *mti;
108 mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
109 p = &mti->mti_txn_param;
110 txn_param_init(p, credits);
112 /* export can require sync operations */
113 if (mti->mti_exp != NULL)
114 p->tp_sync = mti->mti_exp->exp_need_sync;
116 return mdt->mdt_bottom->dd_ops->dt_trans_start(env, mdt->mdt_bottom, p);
119 void mdt_trans_stop(const struct lu_env *env,
120 struct mdt_device *mdt, struct thandle *th)
122 mdt->mdt_bottom->dd_ops->dt_trans_stop(env, th);
125 /* last_rcvd handling */
126 static inline void msd_le_to_cpu(struct mdt_server_data *buf,
127 struct mdt_server_data *msd)
129 memcpy(msd->msd_uuid, buf->msd_uuid, sizeof (msd->msd_uuid));
130 msd->msd_last_transno = le64_to_cpu(buf->msd_last_transno);
131 msd->msd_mount_count = le64_to_cpu(buf->msd_mount_count);
132 msd->msd_feature_compat = le32_to_cpu(buf->msd_feature_compat);
133 msd->msd_feature_rocompat = le32_to_cpu(buf->msd_feature_rocompat);
134 msd->msd_feature_incompat = le32_to_cpu(buf->msd_feature_incompat);
135 msd->msd_server_size = le32_to_cpu(buf->msd_server_size);
136 msd->msd_client_start = le32_to_cpu(buf->msd_client_start);
137 msd->msd_client_size = le16_to_cpu(buf->msd_client_size);
140 static inline void msd_cpu_to_le(struct mdt_server_data *msd,
141 struct mdt_server_data *buf)
143 memcpy(buf->msd_uuid, msd->msd_uuid, sizeof (msd->msd_uuid));
144 buf->msd_last_transno = cpu_to_le64(msd->msd_last_transno);
145 buf->msd_mount_count = cpu_to_le64(msd->msd_mount_count);
146 buf->msd_feature_compat = cpu_to_le32(msd->msd_feature_compat);
147 buf->msd_feature_rocompat = cpu_to_le32(msd->msd_feature_rocompat);
148 buf->msd_feature_incompat = cpu_to_le32(msd->msd_feature_incompat);
149 buf->msd_server_size = cpu_to_le32(msd->msd_server_size);
150 buf->msd_client_start = cpu_to_le32(msd->msd_client_start);
151 buf->msd_client_size = cpu_to_le16(msd->msd_client_size);
154 static inline void mcd_le_to_cpu(struct mdt_client_data *buf,
155 struct mdt_client_data *mcd)
157 memcpy(mcd->mcd_uuid, buf->mcd_uuid, sizeof (mcd->mcd_uuid));
158 mcd->mcd_last_transno = le64_to_cpu(buf->mcd_last_transno);
159 mcd->mcd_last_xid = le64_to_cpu(buf->mcd_last_xid);
160 mcd->mcd_last_result = le32_to_cpu(buf->mcd_last_result);
161 mcd->mcd_last_data = le32_to_cpu(buf->mcd_last_data);
162 mcd->mcd_last_close_transno = le64_to_cpu(buf->mcd_last_close_transno);
163 mcd->mcd_last_close_xid = le64_to_cpu(buf->mcd_last_close_xid);
164 mcd->mcd_last_close_result = le32_to_cpu(buf->mcd_last_close_result);
167 static inline void mcd_cpu_to_le(struct mdt_client_data *mcd,
168 struct mdt_client_data *buf)
170 memcpy(buf->mcd_uuid, mcd->mcd_uuid, sizeof (mcd->mcd_uuid));
171 buf->mcd_last_transno = cpu_to_le64(mcd->mcd_last_transno);
172 buf->mcd_last_xid = cpu_to_le64(mcd->mcd_last_xid);
173 buf->mcd_last_result = cpu_to_le32(mcd->mcd_last_result);
174 buf->mcd_last_data = cpu_to_le32(mcd->mcd_last_data);
175 buf->mcd_last_close_transno = cpu_to_le64(mcd->mcd_last_close_transno);
176 buf->mcd_last_close_xid = cpu_to_le64(mcd->mcd_last_close_xid);
177 buf->mcd_last_close_result = cpu_to_le32(mcd->mcd_last_close_result);
180 static inline int mdt_last_rcvd_header_read(const struct lu_env *env,
181 struct mdt_device *mdt)
183 struct mdt_thread_info *mti;
186 mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
189 rc = mdt_record_read(env, mdt->mdt_last_rcvd,
190 mdt_buf(env, &mti->mti_msd, sizeof(mti->mti_msd)),
193 msd_le_to_cpu(&mti->mti_msd, &mdt->mdt_msd);
195 CDEBUG(D_INFO, "read last_rcvd header rc = %d:\n"
197 "last_transno = "LPU64"\n",
198 rc, mdt->mdt_msd.msd_uuid,
199 mdt->mdt_msd.msd_last_transno);
203 static inline int mdt_last_rcvd_header_write(const struct lu_env *env,
204 struct mdt_device *mdt)
206 struct mdt_thread_info *mti;
211 mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
213 th = mdt_trans_start(env, mdt, MDT_TXN_LAST_RCVD_WRITE_CREDITS);
218 msd_cpu_to_le(&mdt->mdt_msd, &mti->mti_msd);
220 rc = mdt_record_write(env, mdt->mdt_last_rcvd,
221 mdt_buf_const(env, &mti->mti_msd, sizeof(mti->mti_msd)),
224 mdt_trans_stop(env, mdt, th);
226 CDEBUG(D_INFO, "write last_rcvd header rc = %d:\n"
227 "uuid = %s\nlast_transno = "LPU64"\n",
228 rc, mdt->mdt_msd.msd_uuid, mdt->mdt_msd.msd_last_transno);
233 static int mdt_last_rcvd_read(const struct lu_env *env,
234 struct mdt_device *mdt,
235 struct mdt_client_data *mcd, loff_t *off)
237 struct mdt_thread_info *mti;
238 struct mdt_client_data *tmp;
241 mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
243 rc = mdt_record_read(env, mdt->mdt_last_rcvd,
244 mdt_buf(env, tmp, sizeof(*tmp)), off);
246 mcd_le_to_cpu(tmp, mcd);
248 CDEBUG(D_INFO, "read mcd @%d rc = %d:\n"
250 "last_transno = "LPU64"\n"
251 "last_xid = "LPU64"\n"
254 "last_close_transno = "LPU64"\n"
255 "last_close_xid = "LPU64"\n"
256 "last_close_result = %d\n",
257 (int)*off - sizeof(*tmp),
260 mcd->mcd_last_transno,
262 mcd->mcd_last_result,
264 mcd->mcd_last_close_transno,
265 mcd->mcd_last_close_xid,
266 mcd->mcd_last_close_result);
270 static int mdt_last_rcvd_write(const struct lu_env *env,
271 struct mdt_device *mdt,
272 struct mdt_client_data *mcd,
273 loff_t *off, struct thandle *th)
275 struct mdt_thread_info *mti;
276 struct mdt_client_data *tmp;
280 mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
283 mcd_cpu_to_le(mcd, tmp);
285 rc = mdt_record_write(env, mdt->mdt_last_rcvd,
286 mdt_buf_const(env, tmp, sizeof(*tmp)), off, th);
288 CDEBUG(D_INFO, "write mcd @%d rc = %d:\n"
290 "last_transno = "LPU64"\n"
291 "last_xid = "LPU64"\n"
294 "last_close_transno = "LPU64"\n"
295 "last_close_xid = "LPU64"\n"
296 "last_close_result = %d\n",
297 (int)*off - sizeof(*tmp),
300 mcd->mcd_last_transno,
302 mcd->mcd_last_result,
304 mcd->mcd_last_close_transno,
305 mcd->mcd_last_close_xid,
306 mcd->mcd_last_close_result);
311 static int mdt_clients_data_init(const struct lu_env *env,
312 struct mdt_device *mdt,
313 unsigned long last_size)
315 struct mdt_server_data *msd = &mdt->mdt_msd;
316 struct mdt_client_data *mcd = NULL;
317 struct obd_device *obd = mdt->mdt_md_dev.md_lu_dev.ld_obd;
323 /* When we do a clean MDS shutdown, we save the last_transno into
324 * the header. If we find clients with higher last_transno values
325 * then those clients may need recovery done. */
326 LASSERT(atomic_read(&obd->obd_req_replay_clients) == 0);
327 for (cl_idx = 0, off = msd->msd_client_start;
328 off < last_size; cl_idx++) {
330 struct obd_export *exp;
338 off = msd->msd_client_start +
339 cl_idx * msd->msd_client_size;
341 rc = mdt_last_rcvd_read(env, mdt, mcd, &off);
343 CERROR("error reading MDS %s idx %d, off %llu: rc %d\n",
344 LAST_RCVD, cl_idx, off, rc);
346 break; /* read error shouldn't cause startup to fail */
349 if (mcd->mcd_uuid[0] == '\0') {
350 CDEBUG(D_INFO, "skipping zeroed client at offset %d\n",
355 last_transno = mcd_last_transno(mcd);
357 /* These exports are cleaned up by mdt_obd_disconnect(), so
358 * they need to be set up like real exports as
359 * mdt_obd_connect() does.
361 CDEBUG(D_HA, "RCVRNG CLIENT uuid: %s idx: %d lr: "LPU64
362 " srv lr: "LPU64" lx: "LPU64"\n", mcd->mcd_uuid, cl_idx,
363 last_transno, msd->msd_last_transno,
366 exp = class_new_export(obd, (struct obd_uuid *)mcd->mcd_uuid);
368 if (PTR_ERR(exp) == -EALREADY) {
369 /* export already exists, zero out this one */
370 mcd->mcd_uuid[0] = '\0';
372 GOTO(err_client, rc = PTR_ERR(exp));
374 struct mdt_thread_info *mti;
375 mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
376 LASSERT(mti != NULL);
378 exp->exp_mdt_data.med_mcd = mcd;
379 rc = mdt_client_add(env, mdt, cl_idx);
380 /* can't fail existing */
381 LASSERTF(rc == 0, "rc = %d\n", rc);
383 spin_lock(&exp->exp_lock);
384 exp->exp_connecting = 0;
385 exp->exp_in_recovery = 0;
386 spin_unlock(&exp->exp_lock);
387 obd->obd_max_recoverable_clients++;
388 class_export_put(exp);
391 CDEBUG(D_OTHER, "client at idx %d has last_transno="LPU64"\n",
392 cl_idx, last_transno);
393 /* protect __u64 value update */
394 spin_lock(&mdt->mdt_transno_lock);
395 mdt->mdt_last_transno = max(last_transno,
396 mdt->mdt_last_transno);
397 spin_unlock(&mdt->mdt_transno_lock);
406 static int mdt_server_data_init(const struct lu_env *env,
407 struct mdt_device *mdt)
409 struct mdt_server_data *msd = &mdt->mdt_msd;
410 struct mdt_client_data *mcd = NULL;
411 struct obd_device *obd = mdt->mdt_md_dev.md_lu_dev.ld_obd;
412 struct mdt_thread_info *mti;
413 struct dt_object *obj;
415 unsigned long last_rcvd_size;
420 /* ensure padding in the struct is the correct size */
421 CLASSERT(offsetof(struct mdt_server_data, msd_padding) +
422 sizeof(msd->msd_padding) == LR_SERVER_SIZE);
423 CLASSERT(offsetof(struct mdt_client_data, mcd_padding) +
424 sizeof(mcd->mcd_padding) == LR_CLIENT_SIZE);
426 mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
427 LASSERT(mti != NULL);
428 la = &mti->mti_attr.ma_attr;
430 obj = mdt->mdt_last_rcvd;
431 rc = obj->do_ops->do_attr_get(env, mdt->mdt_last_rcvd, la, BYPASS_CAPA);
435 last_rcvd_size = (unsigned long)la->la_size;
437 if (last_rcvd_size == 0) {
438 LCONSOLE_WARN("%s: new disk, initializing\n", obd->obd_name);
440 memcpy(msd->msd_uuid, obd->obd_uuid.uuid,
441 sizeof(msd->msd_uuid));
442 msd->msd_last_transno = 0;
443 msd->msd_mount_count = 0;
444 msd->msd_server_size = LR_SERVER_SIZE;
445 msd->msd_client_start = LR_CLIENT_START;
446 msd->msd_client_size = LR_CLIENT_SIZE;
447 msd->msd_feature_rocompat = OBD_ROCOMPAT_LOVOBJID;
448 msd->msd_feature_incompat = OBD_INCOMPAT_MDT |
449 OBD_INCOMPAT_COMMON_LR;
451 LCONSOLE_WARN("%s: used disk, loading\n", obd->obd_name);
452 rc = mdt_last_rcvd_header_read(env, mdt);
454 CERROR("error reading MDS %s: rc %d\n", LAST_RCVD, rc);
457 if (strcmp(msd->msd_uuid, obd->obd_uuid.uuid) != 0) {
458 LCONSOLE_ERROR_MSG(0x157, "Trying to start OBD %s using"
459 "the wrong disk %s. Were the /dev/ "
460 "assignments rearranged?\n",
461 obd->obd_uuid.uuid, msd->msd_uuid);
462 GOTO(out, rc = -EINVAL);
465 mount_count = msd->msd_mount_count;
467 msd->msd_feature_compat = OBD_COMPAT_MDT;
469 spin_lock(&mdt->mdt_transno_lock);
470 mdt->mdt_last_transno = msd->msd_last_transno;
471 spin_unlock(&mdt->mdt_transno_lock);
473 CDEBUG(D_INODE, "========BEGIN DUMPING LAST_RCVD========\n");
474 CDEBUG(D_INODE, "%s: server last_transno: "LPU64"\n",
475 obd->obd_name, mdt->mdt_last_transno);
476 CDEBUG(D_INODE, "%s: server mount_count: "LPU64"\n",
477 obd->obd_name, mount_count + 1);
478 CDEBUG(D_INODE, "%s: server data size: %u\n",
479 obd->obd_name, msd->msd_server_size);
480 CDEBUG(D_INODE, "%s: per-client data start: %u\n",
481 obd->obd_name, msd->msd_client_start);
482 CDEBUG(D_INODE, "%s: per-client data size: %u\n",
483 obd->obd_name, msd->msd_client_size);
484 CDEBUG(D_INODE, "%s: last_rcvd size: %lu\n",
485 obd->obd_name, last_rcvd_size);
486 CDEBUG(D_INODE, "%s: last_rcvd clients: %lu\n", obd->obd_name,
487 last_rcvd_size <= msd->msd_client_start ? 0 :
488 (last_rcvd_size - msd->msd_client_start) /
489 msd->msd_client_size);
490 CDEBUG(D_INODE, "========END DUMPING LAST_RCVD========\n");
492 if (!msd->msd_server_size || !msd->msd_client_start ||
493 !msd->msd_client_size) {
494 CERROR("Bad last_rcvd contents!\n");
495 GOTO(out, rc = -EINVAL);
498 rc = mdt_clients_data_init(env, mdt, last_rcvd_size);
500 GOTO(err_client, rc);
502 spin_lock(&mdt->mdt_transno_lock);
503 /* obd_last_committed is used for compatibility
504 * with other lustre recovery code */
505 obd->obd_last_committed = mdt->mdt_last_transno;
506 spin_unlock(&mdt->mdt_transno_lock);
508 mdt->mdt_mount_count++;
509 msd->msd_mount_count = mdt->mdt_mount_count;
511 /* save it, so mount count and last_transno is current */
512 rc = mdt_server_data_update(env, mdt);
514 GOTO(err_client, rc);
519 target_recovery_fini(obd);
524 static int mdt_server_data_update(const struct lu_env *env,
525 struct mdt_device *mdt)
530 CDEBUG(D_SUPER, "MDS mount_count is "LPU64", last_transno is "LPU64"\n",
531 mdt->mdt_mount_count, mdt->mdt_last_transno);
533 spin_lock(&mdt->mdt_transno_lock);
534 mdt->mdt_msd.msd_last_transno = mdt->mdt_last_transno;
535 spin_unlock(&mdt->mdt_transno_lock);
538 * This may be called from difficult reply handler and
539 * mdt->mdt_last_rcvd may be NULL that time.
541 if (mdt->mdt_last_rcvd != NULL)
542 rc = mdt_last_rcvd_header_write(env, mdt);
546 void mdt_cb_new_client(const struct mdt_device *mdt, __u64 transno,
549 struct obd_device *obd = mdt->mdt_md_dev.md_lu_dev.ld_obd;
551 target_client_add_cb(obd, transno, data, err);
554 int mdt_client_new(const struct lu_env *env, struct mdt_device *mdt)
556 unsigned long *bitmap = mdt->mdt_client_bitmap;
557 struct mdt_thread_info *mti;
558 struct mdt_export_data *med;
559 struct mdt_client_data *mcd;
560 struct mdt_server_data *msd = &mdt->mdt_msd;
561 struct obd_device *obd = mdt->mdt_md_dev.md_lu_dev.ld_obd;
568 mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
569 LASSERT(mti != NULL);
571 med = &mti->mti_exp->exp_mdt_data;
574 LASSERT(bitmap != NULL);
575 if (!strcmp(med->med_mcd->mcd_uuid, obd->obd_uuid.uuid))
578 /* the bitmap operations can handle cl_idx > sizeof(long) * 8, so
579 * there's no need for extra complication here
581 spin_lock(&mdt->mdt_client_bitmap_lock);
582 cl_idx = find_first_zero_bit(bitmap, LR_MAX_CLIENTS);
583 if (cl_idx >= LR_MAX_CLIENTS ||
584 OBD_FAIL_CHECK(OBD_FAIL_MDS_CLIENT_ADD)) {
585 CERROR("no room for %u clients - fix LR_MAX_CLIENTS\n",
587 spin_unlock(&mdt->mdt_client_bitmap_lock);
590 set_bit(cl_idx, bitmap);
591 spin_unlock(&mdt->mdt_client_bitmap_lock);
593 CDEBUG(D_INFO, "client at idx %d with UUID '%s' added\n",
594 cl_idx, med->med_mcd->mcd_uuid);
596 med->med_lr_idx = cl_idx;
597 med->med_lr_off = msd->msd_client_start +
598 (cl_idx * msd->msd_client_size);
599 init_mutex(&med->med_mcd_lock);
601 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 th = mdt_trans_start(env, mdt, MDT_TXN_LAST_RCVD_WRITE_CREDITS);
608 /* until this operations will be committed the sync is needed for this
610 mdt_trans_add_cb(th, mdt_cb_new_client, mti->mti_exp);
611 spin_lock(&mti->mti_exp->exp_lock);
612 mti->mti_exp->exp_need_sync = 1;
613 spin_unlock(&mti->mti_exp->exp_lock);
615 rc = mdt_last_rcvd_write(env, mdt, mcd, &off, th);
616 CDEBUG(D_INFO, "wrote client mcd at idx %u off %llu (len %u)\n",
617 cl_idx, med->med_lr_off, sizeof(*mcd));
618 mdt_trans_stop(env, mdt, th);
623 /* Add client data to the MDS. We use a bitmap to locate a free space
624 * in the last_rcvd file if cl_off is -1 (i.e. a new client).
625 * Otherwise, we just have to read the data from the last_rcvd file and
626 * we know its offset.
628 * It should not be possible to fail adding an existing client - otherwise
629 * mdt_init_server_data() callsite needs to be fixed.
631 int mdt_client_add(const struct lu_env *env,
632 struct mdt_device *mdt, int cl_idx)
634 struct mdt_thread_info *mti;
635 struct mdt_export_data *med;
636 unsigned long *bitmap = mdt->mdt_client_bitmap;
637 struct obd_device *obd = mdt->mdt_md_dev.md_lu_dev.ld_obd;
638 struct mdt_server_data *msd = &mdt->mdt_msd;
642 mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
643 LASSERT(mti != NULL);
645 med = &mti->mti_exp->exp_mdt_data;
647 LASSERT(bitmap != NULL);
648 LASSERTF(cl_idx >= 0, "%d\n", cl_idx);
650 if (!strcmp(med->med_mcd->mcd_uuid, obd->obd_uuid.uuid))
653 spin_lock(&mdt->mdt_client_bitmap_lock);
654 if (test_and_set_bit(cl_idx, bitmap)) {
655 CERROR("MDS client %d: bit already set in bitmap!!\n",
659 spin_unlock(&mdt->mdt_client_bitmap_lock);
661 CDEBUG(D_INFO, "client at idx %d with UUID '%s' added\n",
662 cl_idx, med->med_mcd->mcd_uuid);
664 med->med_lr_idx = cl_idx;
665 med->med_lr_off = msd->msd_client_start +
666 (cl_idx * msd->msd_client_size);
667 init_mutex(&med->med_mcd_lock);
669 LASSERTF(med->med_lr_off > 0, "med_lr_off = %llu\n", med->med_lr_off);
674 int mdt_client_del(const struct lu_env *env, struct mdt_device *mdt)
676 struct mdt_thread_info *mti;
677 struct mdt_export_data *med;
678 struct mdt_client_data *mcd;
679 struct obd_device *obd = mdt->mdt_md_dev.md_lu_dev.ld_obd;
685 mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
686 LASSERT(mti != NULL);
688 med = &mti->mti_exp->exp_mdt_data;
693 /* XXX: If mcd_uuid were a real obd_uuid, I could use obd_uuid_equals */
694 if (!strcmp(med->med_mcd->mcd_uuid, obd->obd_uuid.uuid))
697 CDEBUG(D_INFO, "freeing client at idx %u, offset %lld\n",
698 med->med_lr_idx, med->med_lr_off);
700 off = med->med_lr_off;
703 * Don't clear med_lr_idx here as it is likely also unset. At worst we
704 * leak a client slot that will be cleaned on the next recovery.
707 CERROR("client idx %d has offset %lld\n",
708 med->med_lr_idx, off);
709 GOTO(free, rc = -EINVAL);
713 * Clear the bit _after_ zeroing out the client so we don't race with
714 * mdt_client_add and zero out new clients.
716 if (!test_bit(med->med_lr_idx, mdt->mdt_client_bitmap)) {
717 CERROR("MDT client %u: bit already clear in bitmap!!\n",
723 * This may be called from difficult reply handler path and
724 * mdt->mdt_last_rcvd may be NULL that time.
726 if (mdt->mdt_last_rcvd != NULL) {
727 th = mdt_trans_start(env, mdt, MDT_TXN_LAST_RCVD_WRITE_CREDITS);
729 GOTO(free, rc = PTR_ERR(th));
731 mutex_down(&med->med_mcd_lock);
732 memset(mcd, 0, sizeof *mcd);
734 rc = mdt_last_rcvd_write(env, mdt, mcd, &off, th);
735 mutex_up(&med->med_mcd_lock);
736 mdt_trans_stop(env, mdt, th);
739 CDEBUG(rc == 0 ? D_INFO : D_ERROR, "Zeroing out client idx %u in "
740 "%s rc %d\n", med->med_lr_idx, LAST_RCVD, rc);
742 spin_lock(&mdt->mdt_client_bitmap_lock);
743 clear_bit(med->med_lr_idx, mdt->mdt_client_bitmap);
744 spin_unlock(&mdt->mdt_client_bitmap_lock);
747 * Make sure the server's last_transno is up to date. Do this after the
748 * client is freed so we know all the client's transactions have been
751 mdt_server_data_update(env, mdt);
760 * last_rcvd & last_committed update callbacks
762 static int mdt_last_rcvd_update(struct mdt_thread_info *mti,
765 struct mdt_device *mdt = mti->mti_mdt;
766 struct ptlrpc_request *req = mdt_info_req(mti);
767 struct mdt_export_data *med;
768 struct mdt_client_data *mcd;
771 __s32 rc = th->th_result;
776 LASSERT(req->rq_export);
778 med = &req->rq_export->exp_mdt_data;
781 /* if the export has already been failed, we have no last_rcvd slot */
782 if (req->rq_export->exp_failed) {
783 CWARN("commit transaction for disconnected client %s: rc %d\n",
784 req->rq_export->exp_client_uuid.uuid, rc);
790 off = med->med_lr_off;
791 mutex_down(&med->med_mcd_lock);
792 if (lustre_msg_get_opc(req->rq_reqmsg) == MDS_CLOSE ||
793 lustre_msg_get_opc(req->rq_reqmsg) == MDS_DONE_WRITING) {
794 transno_p = &mcd->mcd_last_close_transno;
795 mcd->mcd_last_close_xid = req->rq_xid;
796 mcd->mcd_last_close_result = rc;
798 transno_p = &mcd->mcd_last_transno;
799 mcd->mcd_last_xid = req->rq_xid;
800 mcd->mcd_last_result = rc;
801 /*XXX: save intent_disposition in mdt_thread_info?
802 * also there is bug - intent_dispostion is __u64,
803 * see struct ldlm_reply->lock_policy_res1; */
804 mcd->mcd_last_data = mti->mti_opdata;
808 * When we store zero transno in mcd we can lost last transno value
809 * because mcd contains 0, but msd is not yet written
810 * The server data should be updated also if the latest
811 * transno is rewritten by zero. See the bug 11125 for details.
813 if (mti->mti_transno == 0 &&
814 *transno_p == mdt->mdt_last_transno)
815 mdt_server_data_update(mti->mti_env, mdt);
817 *transno_p = mti->mti_transno;
820 CERROR("client idx %d has offset %lld\n", med->med_lr_idx, off);
823 err = mdt_last_rcvd_write(mti->mti_env, mdt, mcd, &off, th);
825 mutex_up(&med->med_mcd_lock);
829 extern struct lu_context_key mdt_thread_key;
831 /* add credits for last_rcvd update */
832 static int mdt_txn_start_cb(const struct lu_env *env,
833 struct txn_param *param, void *cookie)
835 param->tp_credits += MDT_TXN_LAST_RCVD_WRITE_CREDITS;
839 /* Update last_rcvd records with latests transaction data */
840 static int mdt_txn_stop_cb(const struct lu_env *env,
841 struct thandle *txn, void *cookie)
843 struct mdt_device *mdt = cookie;
844 struct mdt_txn_info *txi;
845 struct mdt_thread_info *mti;
846 struct ptlrpc_request *req;
848 /* transno in two contexts - for commit_cb and for thread */
849 txi = lu_context_key_get(&txn->th_ctx, &mdt_txn_key);
850 mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
851 req = mdt_info_req(mti);
853 if (mti->mti_mdt == NULL || req == NULL || mti->mti_no_need_trans) {
854 txi->txi_transno = 0;
855 mti->mti_no_need_trans = 0;
859 if (mti->mti_has_trans) {
860 /* XXX: currently there are allowed cases, but the wrong cases
861 * are also possible, so better check is needed here */
862 CDEBUG(D_INFO, "More than one transaction "LPU64"\n", mti->mti_transno);
866 mti->mti_has_trans = 1;
867 spin_lock(&mdt->mdt_transno_lock);
868 if (txn->th_result != 0) {
869 if (mti->mti_transno != 0) {
870 CERROR("Replay transno "LPU64" failed: rc %i\n",
871 mti->mti_transno, txn->th_result);
872 mti->mti_transno = 0;
874 } else if (mti->mti_transno == 0) {
875 mti->mti_transno = ++ mdt->mdt_last_transno;
877 /* should be replay */
878 if (mti->mti_transno > mdt->mdt_last_transno)
879 mdt->mdt_last_transno = mti->mti_transno;
882 /* sometimes the reply message has not been successfully packed */
883 LASSERT(req != NULL && req->rq_repmsg != NULL);
885 /* filling reply data */
886 CDEBUG(D_INODE, "transno = %llu, last_committed = %llu\n",
887 mti->mti_transno, req->rq_export->exp_obd->obd_last_committed);
889 req->rq_transno = mti->mti_transno;
890 lustre_msg_set_transno(req->rq_repmsg, mti->mti_transno);
891 lustre_msg_set_last_xid(req->rq_repmsg,
892 mcd_last_xid(req->rq_export->exp_mdt_data.med_mcd));
893 /* save transno for the commit callback */
894 txi->txi_transno = mti->mti_transno;
895 spin_unlock(&mdt->mdt_transno_lock);
897 return mdt_last_rcvd_update(mti, txn);
900 /* commit callback, need to update last_commited value */
901 static int mdt_txn_commit_cb(const struct lu_env *env,
902 struct thandle *txn, void *cookie)
904 struct mdt_device *mdt = cookie;
905 struct obd_device *obd = md2lu_dev(&mdt->mdt_md_dev)->ld_obd;
906 struct mdt_txn_info *txi;
909 txi = lu_context_key_get(&txn->th_ctx, &mdt_txn_key);
911 /* copy of obd_transno_commit_cb() but with locking */
912 spin_lock(&mdt->mdt_transno_lock);
913 if (txi->txi_transno > obd->obd_last_committed) {
914 obd->obd_last_committed = txi->txi_transno;
915 spin_unlock(&mdt->mdt_transno_lock);
916 ptlrpc_commit_replies(obd);
918 spin_unlock(&mdt->mdt_transno_lock);
920 if (txi->txi_transno)
921 CDEBUG(D_HA, "%s: transno "LPD64" is committed\n",
922 obd->obd_name, txi->txi_transno);
924 /* iterate through all additional callbacks */
925 for (i = 0; i < txi->txi_cb_count; i++) {
926 txi->txi_cb[i].mdt_cb_func(mdt, txi->txi_transno,
927 txi->txi_cb[i].mdt_cb_data, 0);
932 int mdt_fs_setup(const struct lu_env *env, struct mdt_device *mdt,
933 struct obd_device *obd)
940 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_FS_SETUP))
943 /* prepare transactions callbacks */
944 mdt->mdt_txn_cb.dtc_txn_start = mdt_txn_start_cb;
945 mdt->mdt_txn_cb.dtc_txn_stop = mdt_txn_stop_cb;
946 mdt->mdt_txn_cb.dtc_txn_commit = mdt_txn_commit_cb;
947 mdt->mdt_txn_cb.dtc_cookie = mdt;
948 CFS_INIT_LIST_HEAD(&mdt->mdt_txn_cb.dtc_linkage);
950 dt_txn_callback_add(mdt->mdt_bottom, &mdt->mdt_txn_cb);
952 o = dt_store_open(env, mdt->mdt_bottom, LAST_RCVD, &fid);
954 mdt->mdt_last_rcvd = o;
955 rc = mdt_server_data_init(env, mdt);
957 GOTO(put_last_rcvd, rc);
960 CERROR("cannot open %s: rc = %d\n", LAST_RCVD, rc);
964 o = dt_store_open(env, mdt->mdt_bottom, CAPA_KEYS, &fid);
967 rc = mdt_capa_keys_init(env, mdt);
969 GOTO(put_ck_object, rc);
972 CERROR("cannot open %s: rc = %d\n", CAPA_KEYS, rc);
973 GOTO(put_last_rcvd, rc);
978 lu_object_put(env, &o->do_lu);
979 mdt->mdt_ck_obj = NULL;
981 lu_object_put(env, &mdt->mdt_last_rcvd->do_lu);
982 mdt->mdt_last_rcvd = NULL;
987 void mdt_fs_cleanup(const struct lu_env *env, struct mdt_device *mdt)
991 /* Remove transaction callback */
992 dt_txn_callback_del(mdt->mdt_bottom, &mdt->mdt_txn_cb);
993 if (mdt->mdt_last_rcvd)
994 lu_object_put(env, &mdt->mdt_last_rcvd->do_lu);
995 mdt->mdt_last_rcvd = NULL;
997 lu_object_put(env, &mdt->mdt_ck_obj->do_lu);
998 mdt->mdt_ck_obj = NULL;
1002 /* reconstruction code */
1003 static void mdt_steal_ack_locks(struct ptlrpc_request *req)
1005 struct obd_export *exp = req->rq_export;
1006 struct list_head *tmp;
1007 struct ptlrpc_reply_state *oldrep;
1008 struct ptlrpc_service *svc;
1011 /* CAVEAT EMPTOR: spinlock order */
1012 spin_lock(&exp->exp_lock);
1013 list_for_each (tmp, &exp->exp_outstanding_replies) {
1014 oldrep = list_entry(tmp, struct ptlrpc_reply_state,rs_exp_list);
1016 if (oldrep->rs_xid != req->rq_xid)
1019 if (lustre_msg_get_opc(oldrep->rs_msg) !=
1020 lustre_msg_get_opc(req->rq_reqmsg))
1021 CERROR ("Resent req xid "LPX64" has mismatched opc: "
1022 "new %d old %d\n", req->rq_xid,
1023 lustre_msg_get_opc(req->rq_reqmsg),
1024 lustre_msg_get_opc(oldrep->rs_msg));
1026 svc = oldrep->rs_service;
1027 spin_lock (&svc->srv_lock);
1029 list_del_init (&oldrep->rs_exp_list);
1031 CWARN("Stealing %d locks from rs %p x"LPD64".t"LPD64
1033 oldrep->rs_nlocks, oldrep,
1034 oldrep->rs_xid, oldrep->rs_transno,
1035 lustre_msg_get_opc(oldrep->rs_msg),
1036 libcfs_nid2str(exp->exp_connection->c_peer.nid));
1038 for (i = 0; i < oldrep->rs_nlocks; i++)
1039 ptlrpc_save_lock(req,
1040 &oldrep->rs_locks[i],
1041 oldrep->rs_modes[i]);
1042 oldrep->rs_nlocks = 0;
1044 DEBUG_REQ(D_HA, req, "stole locks for");
1045 ptlrpc_schedule_difficult_reply (oldrep);
1047 spin_unlock (&svc->srv_lock);
1050 spin_unlock(&exp->exp_lock);
1053 void mdt_req_from_mcd(struct ptlrpc_request *req,
1054 struct mdt_client_data *mcd)
1056 DEBUG_REQ(D_HA, req, "restoring transno "LPD64"/status %d",
1057 mcd->mcd_last_transno, mcd->mcd_last_result);
1059 if (lustre_msg_get_opc(req->rq_reqmsg) == MDS_CLOSE ||
1060 lustre_msg_get_opc(req->rq_repmsg) == MDS_DONE_WRITING) {
1061 req->rq_transno = mcd->mcd_last_close_transno;
1062 req->rq_status = mcd->mcd_last_close_result;
1063 lustre_msg_set_transno(req->rq_repmsg, req->rq_transno);
1064 lustre_msg_set_status(req->rq_repmsg, req->rq_status);
1066 req->rq_transno = mcd->mcd_last_transno;
1067 req->rq_status = mcd->mcd_last_result;
1068 lustre_msg_set_transno(req->rq_repmsg, req->rq_transno);
1069 lustre_msg_set_status(req->rq_repmsg, req->rq_status);
1071 mdt_steal_ack_locks(req);
1074 void mdt_reconstruct_generic(struct mdt_thread_info *mti,
1075 struct mdt_lock_handle *lhc)
1077 struct ptlrpc_request *req = mdt_info_req(mti);
1078 struct mdt_export_data *med = &req->rq_export->exp_mdt_data;
1080 return mdt_req_from_mcd(req, med->med_mcd);
1083 static void mdt_reconstruct_create(struct mdt_thread_info *mti,
1084 struct mdt_lock_handle *lhc)
1086 struct ptlrpc_request *req = mdt_info_req(mti);
1087 struct mdt_export_data *med = &req->rq_export->exp_mdt_data;
1088 struct mdt_device *mdt = mti->mti_mdt;
1089 struct mdt_object *child;
1090 struct mdt_body *body;
1093 mdt_req_from_mcd(req, med->med_mcd);
1097 /* if no error, so child was created with requested fid */
1098 child = mdt_object_find(mti->mti_env, mdt, mti->mti_rr.rr_fid2);
1099 LASSERT(!IS_ERR(child));
1101 body = req_capsule_server_get(mti->mti_pill, &RMF_MDT_BODY);
1102 rc = mo_attr_get(mti->mti_env, mdt_object_child(child), &mti->mti_attr);
1103 if (rc == -EREMOTE) {
1104 /* object was created on remote server */
1105 req->rq_status = rc;
1106 body->valid |= OBD_MD_MDS;
1108 mdt_pack_attr2body(mti, body, &mti->mti_attr.ma_attr, mdt_object_fid(child));
1109 mdt_object_put(mti->mti_env, child);
1112 static void mdt_reconstruct_setattr(struct mdt_thread_info *mti,
1113 struct mdt_lock_handle *lhc)
1115 struct ptlrpc_request *req = mdt_info_req(mti);
1116 struct mdt_export_data *med = &req->rq_export->exp_mdt_data;
1117 struct mdt_device *mdt = mti->mti_mdt;
1118 struct mdt_object *obj;
1119 struct mdt_body *body;
1121 mdt_req_from_mcd(req, med->med_mcd);
1125 body = req_capsule_server_get(mti->mti_pill, &RMF_MDT_BODY);
1126 obj = mdt_object_find(mti->mti_env, mdt, mti->mti_rr.rr_fid1);
1127 LASSERT(!IS_ERR(obj));
1128 mo_attr_get(mti->mti_env, mdt_object_child(obj), &mti->mti_attr);
1129 mdt_pack_attr2body(mti, body, &mti->mti_attr.ma_attr,
1130 mdt_object_fid(obj));
1131 if (mti->mti_epoch && (mti->mti_epoch->flags & MF_EPOCH_OPEN)) {
1132 struct mdt_file_data *mfd;
1133 struct mdt_body *repbody;
1135 repbody = req_capsule_server_get(mti->mti_pill, &RMF_MDT_BODY);
1136 repbody->ioepoch = obj->mot_ioepoch;
1137 spin_lock(&med->med_open_lock);
1138 list_for_each_entry(mfd, &med->med_open_head, mfd_list) {
1139 if (mfd->mfd_xid == req->rq_xid)
1142 LASSERT(&mfd->mfd_list != &med->med_open_head);
1143 spin_unlock(&med->med_open_lock);
1144 repbody->handle.cookie = mfd->mfd_handle.h_cookie;
1147 mdt_object_put(mti->mti_env, obj);
1150 static void mdt_reconstruct_setxattr(struct mdt_thread_info *mti,
1151 struct mdt_lock_handle *lhc)
1154 req_capsule_shrink(mti->mti_pill, &RMF_EADATA, 0, RCL_SERVER);
1157 typedef void (*mdt_reconstructor)(struct mdt_thread_info *mti,
1158 struct mdt_lock_handle *lhc);
1160 static mdt_reconstructor reconstructors[REINT_MAX] = {
1161 [REINT_SETATTR] = mdt_reconstruct_setattr,
1162 [REINT_CREATE] = mdt_reconstruct_create,
1163 [REINT_LINK] = mdt_reconstruct_generic,
1164 [REINT_UNLINK] = mdt_reconstruct_generic,
1165 [REINT_RENAME] = mdt_reconstruct_generic,
1166 [REINT_OPEN] = mdt_reconstruct_open,
1167 [REINT_SETXATTR] = mdt_reconstruct_setxattr
1170 void mdt_reconstruct(struct mdt_thread_info *mti,
1171 struct mdt_lock_handle *lhc)
1174 reconstructors[mti->mti_rr.rr_opcode](mti, lhc);