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 int mdt_record_read(const struct lu_env *env,
81 struct dt_object *dt, struct lu_buf *buf, loff_t *pos)
85 LASSERTF(dt != NULL, "dt is NULL when we want to read record\n");
87 rc = dt->do_body_ops->dbo_read(env, dt, buf, pos, BYPASS_CAPA);
89 if (rc == buf->lb_len)
96 int mdt_record_write(const struct lu_env *env,
97 struct dt_object *dt, const struct lu_buf *buf,
98 loff_t *pos, struct thandle *th)
102 LASSERTF(dt != NULL, "dt is NULL when we want to write record\n");
104 rc = dt->do_body_ops->dbo_write(env, dt, buf, pos, th, BYPASS_CAPA);
105 if (rc == buf->lb_len)
111 /* only one record write */
114 MDT_TXN_LAST_RCVD_WRITE_CREDITS = 3
117 struct thandle* mdt_trans_start(const struct lu_env *env,
118 struct mdt_device *mdt, int credits)
120 struct mdt_thread_info *mti;
123 mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
124 p = &mti->mti_txn_param;
125 txn_param_init(p, credits);
127 /* export can require sync operations */
128 if (mti->mti_exp != NULL)
129 p->tp_sync = mti->mti_exp->exp_need_sync;
131 return mdt->mdt_bottom->dd_ops->dt_trans_start(env, mdt->mdt_bottom, p);
134 void mdt_trans_stop(const struct lu_env *env,
135 struct mdt_device *mdt, struct thandle *th)
137 mdt->mdt_bottom->dd_ops->dt_trans_stop(env, th);
140 /* last_rcvd handling */
141 static inline void lsd_le_to_cpu(struct lr_server_data *buf,
142 struct lr_server_data *lsd)
144 memcpy(lsd->lsd_uuid, buf->lsd_uuid, sizeof (lsd->lsd_uuid));
145 lsd->lsd_last_transno = le64_to_cpu(buf->lsd_last_transno);
146 lsd->lsd_mount_count = le64_to_cpu(buf->lsd_mount_count);
147 lsd->lsd_feature_compat = le32_to_cpu(buf->lsd_feature_compat);
148 lsd->lsd_feature_rocompat = le32_to_cpu(buf->lsd_feature_rocompat);
149 lsd->lsd_feature_incompat = le32_to_cpu(buf->lsd_feature_incompat);
150 lsd->lsd_server_size = le32_to_cpu(buf->lsd_server_size);
151 lsd->lsd_client_start = le32_to_cpu(buf->lsd_client_start);
152 lsd->lsd_client_size = le16_to_cpu(buf->lsd_client_size);
155 static inline void lsd_cpu_to_le(struct lr_server_data *lsd,
156 struct lr_server_data *buf)
158 memcpy(buf->lsd_uuid, lsd->lsd_uuid, sizeof (lsd->lsd_uuid));
159 buf->lsd_last_transno = cpu_to_le64(lsd->lsd_last_transno);
160 buf->lsd_mount_count = cpu_to_le64(lsd->lsd_mount_count);
161 buf->lsd_feature_compat = cpu_to_le32(lsd->lsd_feature_compat);
162 buf->lsd_feature_rocompat = cpu_to_le32(lsd->lsd_feature_rocompat);
163 buf->lsd_feature_incompat = cpu_to_le32(lsd->lsd_feature_incompat);
164 buf->lsd_server_size = cpu_to_le32(lsd->lsd_server_size);
165 buf->lsd_client_start = cpu_to_le32(lsd->lsd_client_start);
166 buf->lsd_client_size = cpu_to_le16(lsd->lsd_client_size);
169 static inline void lcd_le_to_cpu(struct lsd_client_data *buf,
170 struct lsd_client_data *lcd)
172 memcpy(lcd->lcd_uuid, buf->lcd_uuid, sizeof (lcd->lcd_uuid));
173 lcd->lcd_last_transno = le64_to_cpu(buf->lcd_last_transno);
174 lcd->lcd_last_xid = le64_to_cpu(buf->lcd_last_xid);
175 lcd->lcd_last_result = le32_to_cpu(buf->lcd_last_result);
176 lcd->lcd_last_data = le32_to_cpu(buf->lcd_last_data);
177 lcd->lcd_last_close_transno = le64_to_cpu(buf->lcd_last_close_transno);
178 lcd->lcd_last_close_xid = le64_to_cpu(buf->lcd_last_close_xid);
179 lcd->lcd_last_close_result = le32_to_cpu(buf->lcd_last_close_result);
182 static inline void lcd_cpu_to_le(struct lsd_client_data *lcd,
183 struct lsd_client_data *buf)
185 memcpy(buf->lcd_uuid, lcd->lcd_uuid, sizeof (lcd->lcd_uuid));
186 buf->lcd_last_transno = cpu_to_le64(lcd->lcd_last_transno);
187 buf->lcd_last_xid = cpu_to_le64(lcd->lcd_last_xid);
188 buf->lcd_last_result = cpu_to_le32(lcd->lcd_last_result);
189 buf->lcd_last_data = cpu_to_le32(lcd->lcd_last_data);
190 buf->lcd_last_close_transno = cpu_to_le64(lcd->lcd_last_close_transno);
191 buf->lcd_last_close_xid = cpu_to_le64(lcd->lcd_last_close_xid);
192 buf->lcd_last_close_result = cpu_to_le32(lcd->lcd_last_close_result);
195 static inline int mdt_last_rcvd_header_read(const struct lu_env *env,
196 struct mdt_device *mdt)
198 struct mdt_thread_info *mti;
201 mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
204 rc = mdt_record_read(env, mdt->mdt_last_rcvd,
205 mdt_buf(env, &mti->mti_lsd, sizeof(mti->mti_lsd)),
208 lsd_le_to_cpu(&mti->mti_lsd, &mdt->mdt_lsd);
210 CDEBUG(D_INFO, "read last_rcvd header rc = %d:\n"
212 "last_transno = "LPU64"\n",
213 rc, mdt->mdt_lsd.lsd_uuid,
214 mdt->mdt_lsd.lsd_last_transno);
218 static inline int mdt_last_rcvd_header_write(const struct lu_env *env,
219 struct mdt_device *mdt)
221 struct mdt_thread_info *mti;
226 mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
228 th = mdt_trans_start(env, mdt, MDT_TXN_LAST_RCVD_WRITE_CREDITS);
233 lsd_cpu_to_le(&mdt->mdt_lsd, &mti->mti_lsd);
235 rc = mdt_record_write(env, mdt->mdt_last_rcvd,
236 mdt_buf_const(env, &mti->mti_lsd,
237 sizeof(mti->mti_lsd)),
240 mdt_trans_stop(env, mdt, th);
242 CDEBUG(D_INFO, "write last_rcvd header rc = %d:\n"
243 "uuid = %s\nlast_transno = "LPU64"\n",
244 rc, mdt->mdt_lsd.lsd_uuid, mdt->mdt_lsd.lsd_last_transno);
249 static int mdt_last_rcvd_read(const struct lu_env *env,
250 struct mdt_device *mdt,
251 struct lsd_client_data *lcd, loff_t *off)
253 struct mdt_thread_info *mti;
254 struct lsd_client_data *tmp;
257 mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
259 rc = mdt_record_read(env, mdt->mdt_last_rcvd,
260 mdt_buf(env, tmp, sizeof(*tmp)), off);
262 lcd_le_to_cpu(tmp, lcd);
264 CDEBUG(D_INFO, "read lcd @%d rc = %d:\n"
266 "last_transno = "LPU64"\n"
267 "last_xid = "LPU64"\n"
270 "last_close_transno = "LPU64"\n"
271 "last_close_xid = "LPU64"\n"
272 "last_close_result = %u\n",
273 (int)(*off - sizeof(*tmp)),
276 lcd->lcd_last_transno,
278 lcd->lcd_last_result,
280 lcd->lcd_last_close_transno,
281 lcd->lcd_last_close_xid,
282 lcd->lcd_last_close_result);
286 static int mdt_last_rcvd_write(const struct lu_env *env,
287 struct mdt_device *mdt,
288 struct lsd_client_data *lcd,
289 loff_t *off, struct thandle *th)
291 struct mdt_thread_info *mti;
292 struct lsd_client_data *tmp;
296 mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
299 lcd_cpu_to_le(lcd, tmp);
301 rc = mdt_record_write(env, mdt->mdt_last_rcvd,
302 mdt_buf_const(env, tmp, sizeof(*tmp)), off, th);
304 CDEBUG(D_INFO, "write lcd @%d rc = %d:\n"
306 "last_transno = "LPU64"\n"
307 "last_xid = "LPU64"\n"
310 "last_close_transno = "LPU64"\n"
311 "last_close_xid = "LPU64"\n"
312 "last_close_result = %u\n",
313 (int)(*off - sizeof(*tmp)),
316 lcd->lcd_last_transno,
318 lcd->lcd_last_result,
320 lcd->lcd_last_close_transno,
321 lcd->lcd_last_close_xid,
322 lcd->lcd_last_close_result);
326 static int mdt_clients_data_init(const struct lu_env *env,
327 struct mdt_device *mdt,
328 unsigned long last_size)
330 struct lr_server_data *lsd = &mdt->mdt_lsd;
331 struct lsd_client_data *lcd = NULL;
332 struct obd_device *obd = mdt->mdt_md_dev.md_lu_dev.ld_obd;
338 /* When we do a clean MDS shutdown, we save the last_transno into
339 * the header. If we find clients with higher last_transno values
340 * then those clients may need recovery done. */
341 LASSERT(atomic_read(&obd->obd_req_replay_clients) == 0);
342 for (cl_idx = 0, off = lsd->lsd_client_start;
343 off < last_size; cl_idx++) {
345 struct obd_export *exp;
353 off = lsd->lsd_client_start +
354 cl_idx * lsd->lsd_client_size;
356 rc = mdt_last_rcvd_read(env, mdt, lcd, &off);
358 CERROR("error reading MDS %s idx %d, off %llu: rc %d\n",
359 LAST_RCVD, cl_idx, off, rc);
361 break; /* read error shouldn't cause startup to fail */
364 if (lcd->lcd_uuid[0] == '\0') {
365 CDEBUG(D_INFO, "skipping zeroed client at offset %d\n",
370 last_transno = lcd_last_transno(lcd);
372 /* These exports are cleaned up by mdt_obd_disconnect(), so
373 * they need to be set up like real exports as
374 * mdt_obd_connect() does.
376 CDEBUG(D_HA, "RCVRNG CLIENT uuid: %s idx: %d lr: "LPU64
377 " srv lr: "LPU64" lx: "LPU64"\n", lcd->lcd_uuid, cl_idx,
378 last_transno, lsd->lsd_last_transno,
381 exp = class_new_export(obd, (struct obd_uuid *)lcd->lcd_uuid);
383 if (PTR_ERR(exp) == -EALREADY) {
384 /* export already exists, zero out this one */
385 lcd->lcd_uuid[0] = '\0';
387 GOTO(err_client, rc = PTR_ERR(exp));
389 struct mdt_thread_info *mti;
390 mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
391 LASSERT(mti != NULL);
393 exp->exp_mdt_data.med_lcd = lcd;
394 rc = mdt_client_add(env, mdt, cl_idx);
395 /* can't fail existing */
396 LASSERTF(rc == 0, "rc = %d\n", rc);
398 spin_lock(&exp->exp_lock);
399 exp->exp_connecting = 0;
400 exp->exp_in_recovery = 0;
401 spin_unlock(&exp->exp_lock);
402 obd->obd_max_recoverable_clients++;
403 class_export_put(exp);
406 CDEBUG(D_OTHER, "client at idx %d has last_transno="LPU64"\n",
407 cl_idx, last_transno);
408 /* protect __u64 value update */
409 spin_lock(&mdt->mdt_transno_lock);
410 mdt->mdt_last_transno = max(last_transno,
411 mdt->mdt_last_transno);
412 spin_unlock(&mdt->mdt_transno_lock);
421 static int mdt_server_data_init(const struct lu_env *env,
422 struct mdt_device *mdt)
424 struct lr_server_data *lsd = &mdt->mdt_lsd;
425 struct lsd_client_data *lcd = NULL;
426 struct obd_device *obd = mdt->mdt_md_dev.md_lu_dev.ld_obd;
427 struct mdt_thread_info *mti;
428 struct dt_object *obj;
430 unsigned long last_rcvd_size;
435 /* ensure padding in the struct is the correct size */
436 CLASSERT(offsetof(struct lr_server_data, lsd_padding) +
437 sizeof(lsd->lsd_padding) == LR_SERVER_SIZE);
438 CLASSERT(offsetof(struct lsd_client_data, lcd_padding) +
439 sizeof(lcd->lcd_padding) == LR_CLIENT_SIZE);
441 mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
442 LASSERT(mti != NULL);
443 la = &mti->mti_attr.ma_attr;
445 obj = mdt->mdt_last_rcvd;
446 rc = obj->do_ops->do_attr_get(env, mdt->mdt_last_rcvd, la, BYPASS_CAPA);
450 last_rcvd_size = (unsigned long)la->la_size;
452 if (last_rcvd_size == 0) {
453 LCONSOLE_WARN("%s: new disk, initializing\n", obd->obd_name);
455 memcpy(lsd->lsd_uuid, obd->obd_uuid.uuid,
456 sizeof(lsd->lsd_uuid));
457 lsd->lsd_last_transno = 0;
458 lsd->lsd_mount_count = 0;
459 lsd->lsd_server_size = LR_SERVER_SIZE;
460 lsd->lsd_client_start = LR_CLIENT_START;
461 lsd->lsd_client_size = LR_CLIENT_SIZE;
462 lsd->lsd_feature_rocompat = OBD_ROCOMPAT_LOVOBJID;
463 lsd->lsd_feature_incompat = OBD_INCOMPAT_MDT |
464 OBD_INCOMPAT_COMMON_LR;
466 LCONSOLE_WARN("%s: used disk, loading\n", obd->obd_name);
467 rc = mdt_last_rcvd_header_read(env, mdt);
469 CERROR("error reading MDS %s: rc %d\n", LAST_RCVD, rc);
472 if (strcmp(lsd->lsd_uuid, obd->obd_uuid.uuid) != 0) {
473 LCONSOLE_ERROR_MSG(0x157, "Trying to start OBD %s using"
474 "the wrong disk %s. Were the /dev/ "
475 "assignments rearranged?\n",
476 obd->obd_uuid.uuid, lsd->lsd_uuid);
477 GOTO(out, rc = -EINVAL);
480 mount_count = lsd->lsd_mount_count;
482 lsd->lsd_feature_compat = OBD_COMPAT_MDT;
484 spin_lock(&mdt->mdt_transno_lock);
485 mdt->mdt_last_transno = lsd->lsd_last_transno;
486 spin_unlock(&mdt->mdt_transno_lock);
488 CDEBUG(D_INODE, "========BEGIN DUMPING LAST_RCVD========\n");
489 CDEBUG(D_INODE, "%s: server last_transno: "LPU64"\n",
490 obd->obd_name, mdt->mdt_last_transno);
491 CDEBUG(D_INODE, "%s: server mount_count: "LPU64"\n",
492 obd->obd_name, mount_count + 1);
493 CDEBUG(D_INODE, "%s: server data size: %u\n",
494 obd->obd_name, lsd->lsd_server_size);
495 CDEBUG(D_INODE, "%s: per-client data start: %u\n",
496 obd->obd_name, lsd->lsd_client_start);
497 CDEBUG(D_INODE, "%s: per-client data size: %u\n",
498 obd->obd_name, lsd->lsd_client_size);
499 CDEBUG(D_INODE, "%s: last_rcvd size: %lu\n",
500 obd->obd_name, last_rcvd_size);
501 CDEBUG(D_INODE, "%s: last_rcvd clients: %lu\n", obd->obd_name,
502 last_rcvd_size <= lsd->lsd_client_start ? 0 :
503 (last_rcvd_size - lsd->lsd_client_start) /
504 lsd->lsd_client_size);
505 CDEBUG(D_INODE, "========END DUMPING LAST_RCVD========\n");
507 if (!lsd->lsd_server_size || !lsd->lsd_client_start ||
508 !lsd->lsd_client_size) {
509 CERROR("Bad last_rcvd contents!\n");
510 GOTO(out, rc = -EINVAL);
513 rc = mdt_clients_data_init(env, mdt, last_rcvd_size);
515 GOTO(err_client, rc);
517 spin_lock(&mdt->mdt_transno_lock);
518 /* obd_last_committed is used for compatibility
519 * with other lustre recovery code */
520 obd->obd_last_committed = mdt->mdt_last_transno;
521 spin_unlock(&mdt->mdt_transno_lock);
523 mdt->mdt_mount_count++;
524 lsd->lsd_mount_count = mdt->mdt_mount_count;
526 /* save it, so mount count and last_transno is current */
527 rc = mdt_server_data_update(env, mdt);
529 GOTO(err_client, rc);
534 target_recovery_fini(obd);
539 static int mdt_server_data_update(const struct lu_env *env,
540 struct mdt_device *mdt)
545 CDEBUG(D_SUPER, "MDS mount_count is "LPU64", last_transno is "LPU64"\n",
546 mdt->mdt_mount_count, mdt->mdt_last_transno);
548 spin_lock(&mdt->mdt_transno_lock);
549 mdt->mdt_lsd.lsd_last_transno = mdt->mdt_last_transno;
550 spin_unlock(&mdt->mdt_transno_lock);
553 * This may be called from difficult reply handler and
554 * mdt->mdt_last_rcvd may be NULL that time.
556 if (mdt->mdt_last_rcvd != NULL)
557 rc = mdt_last_rcvd_header_write(env, mdt);
561 void mdt_cb_new_client(const struct mdt_device *mdt, __u64 transno,
564 struct obd_device *obd = mdt->mdt_md_dev.md_lu_dev.ld_obd;
566 target_client_add_cb(obd, transno, data, err);
569 int mdt_client_new(const struct lu_env *env, struct mdt_device *mdt)
571 unsigned long *bitmap = mdt->mdt_client_bitmap;
572 struct mdt_thread_info *mti;
573 struct mdt_export_data *med;
574 struct lsd_client_data *lcd;
575 struct lr_server_data *lsd = &mdt->mdt_lsd;
576 struct obd_device *obd = mdt->mdt_md_dev.md_lu_dev.ld_obd;
583 mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
584 LASSERT(mti != NULL);
586 med = &mti->mti_exp->exp_mdt_data;
589 LASSERT(bitmap != NULL);
590 if (!strcmp(med->med_lcd->lcd_uuid, obd->obd_uuid.uuid))
593 /* the bitmap operations can handle cl_idx > sizeof(long) * 8, so
594 * there's no need for extra complication here
596 spin_lock(&mdt->mdt_client_bitmap_lock);
597 cl_idx = find_first_zero_bit(bitmap, LR_MAX_CLIENTS);
598 if (cl_idx >= LR_MAX_CLIENTS ||
599 OBD_FAIL_CHECK(OBD_FAIL_MDS_CLIENT_ADD)) {
600 CERROR("no room for %u clients - fix LR_MAX_CLIENTS\n",
602 spin_unlock(&mdt->mdt_client_bitmap_lock);
605 set_bit(cl_idx, bitmap);
606 spin_unlock(&mdt->mdt_client_bitmap_lock);
608 CDEBUG(D_INFO, "client at idx %d with UUID '%s' added\n",
609 cl_idx, med->med_lcd->lcd_uuid);
611 med->med_lr_idx = cl_idx;
612 med->med_lr_off = lsd->lsd_client_start +
613 (cl_idx * lsd->lsd_client_size);
614 init_mutex(&med->med_lcd_lock);
616 LASSERTF(med->med_lr_off > 0, "med_lr_off = %llu\n", med->med_lr_off);
617 /* write new client data */
618 off = med->med_lr_off;
619 th = mdt_trans_start(env, mdt, MDT_TXN_LAST_RCVD_WRITE_CREDITS);
623 /* until this operations will be committed the sync is needed for this
625 mdt_trans_add_cb(th, mdt_cb_new_client, mti->mti_exp);
626 spin_lock(&mti->mti_exp->exp_lock);
627 mti->mti_exp->exp_need_sync = 1;
628 spin_unlock(&mti->mti_exp->exp_lock);
630 rc = mdt_last_rcvd_write(env, mdt, lcd, &off, th);
631 CDEBUG(D_INFO, "wrote client lcd at idx %u off %llu (len "LPSZ")\n",
632 cl_idx, med->med_lr_off, sizeof(*lcd));
633 mdt_trans_stop(env, mdt, th);
638 /* Add client data to the MDS. We use a bitmap to locate a free space
639 * in the last_rcvd file if cl_off is -1 (i.e. a new client).
640 * Otherwise, we just have to read the data from the last_rcvd file and
641 * we know its offset.
643 * It should not be possible to fail adding an existing client - otherwise
644 * mdt_init_server_data() callsite needs to be fixed.
646 int mdt_client_add(const struct lu_env *env,
647 struct mdt_device *mdt, int cl_idx)
649 struct mdt_thread_info *mti;
650 struct mdt_export_data *med;
651 unsigned long *bitmap = mdt->mdt_client_bitmap;
652 struct obd_device *obd = mdt->mdt_md_dev.md_lu_dev.ld_obd;
653 struct lr_server_data *lsd = &mdt->mdt_lsd;
657 mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
658 LASSERT(mti != NULL);
660 med = &mti->mti_exp->exp_mdt_data;
662 LASSERT(bitmap != NULL);
663 LASSERTF(cl_idx >= 0, "%d\n", cl_idx);
665 if (!strcmp(med->med_lcd->lcd_uuid, obd->obd_uuid.uuid))
668 spin_lock(&mdt->mdt_client_bitmap_lock);
669 if (test_and_set_bit(cl_idx, bitmap)) {
670 CERROR("MDS client %d: bit already set in bitmap!!\n",
674 spin_unlock(&mdt->mdt_client_bitmap_lock);
676 CDEBUG(D_INFO, "client at idx %d with UUID '%s' added\n",
677 cl_idx, med->med_lcd->lcd_uuid);
679 med->med_lr_idx = cl_idx;
680 med->med_lr_off = lsd->lsd_client_start +
681 (cl_idx * lsd->lsd_client_size);
682 init_mutex(&med->med_lcd_lock);
684 LASSERTF(med->med_lr_off > 0, "med_lr_off = %llu\n", med->med_lr_off);
689 int mdt_client_del(const struct lu_env *env, struct mdt_device *mdt)
691 struct mdt_thread_info *mti;
692 struct mdt_export_data *med;
693 struct lsd_client_data *lcd;
694 struct obd_device *obd = mdt->mdt_md_dev.md_lu_dev.ld_obd;
700 mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
701 LASSERT(mti != NULL);
703 med = &mti->mti_exp->exp_mdt_data;
708 /* XXX: If lcd_uuid were a real obd_uuid, I could use obd_uuid_equals */
709 if (!strcmp(med->med_lcd->lcd_uuid, obd->obd_uuid.uuid))
712 CDEBUG(D_INFO, "freeing client at idx %u, offset %lld\n",
713 med->med_lr_idx, med->med_lr_off);
715 off = med->med_lr_off;
718 * Don't clear med_lr_idx here as it is likely also unset. At worst we
719 * leak a client slot that will be cleaned on the next recovery.
722 CERROR("client idx %d has offset %lld\n",
723 med->med_lr_idx, off);
724 GOTO(free, rc = -EINVAL);
728 * Clear the bit _after_ zeroing out the client so we don't race with
729 * mdt_client_add and zero out new clients.
731 if (!test_bit(med->med_lr_idx, mdt->mdt_client_bitmap)) {
732 CERROR("MDT client %u: bit already clear in bitmap!!\n",
738 * This may be called from difficult reply handler path and
739 * mdt->mdt_last_rcvd may be NULL that time.
741 if (mdt->mdt_last_rcvd != NULL) {
742 th = mdt_trans_start(env, mdt, MDT_TXN_LAST_RCVD_WRITE_CREDITS);
744 GOTO(free, rc = PTR_ERR(th));
746 mutex_down(&med->med_lcd_lock);
747 memset(lcd, 0, sizeof *lcd);
749 rc = mdt_last_rcvd_write(env, mdt, lcd, &off, th);
750 mutex_up(&med->med_lcd_lock);
751 mdt_trans_stop(env, mdt, th);
754 CDEBUG(rc == 0 ? D_INFO : D_ERROR, "Zeroing out client idx %u in "
755 "%s rc %d\n", med->med_lr_idx, LAST_RCVD, rc);
757 spin_lock(&mdt->mdt_client_bitmap_lock);
758 clear_bit(med->med_lr_idx, mdt->mdt_client_bitmap);
759 spin_unlock(&mdt->mdt_client_bitmap_lock);
762 * Make sure the server's last_transno is up to date. Do this after the
763 * client is freed so we know all the client's transactions have been
766 mdt_server_data_update(env, mdt);
775 * last_rcvd & last_committed update callbacks
777 static int mdt_last_rcvd_update(struct mdt_thread_info *mti,
780 struct mdt_device *mdt = mti->mti_mdt;
781 struct ptlrpc_request *req = mdt_info_req(mti);
782 struct mdt_export_data *med;
783 struct lsd_client_data *lcd;
786 __s32 rc = th->th_result;
791 LASSERT(req->rq_export);
793 med = &req->rq_export->exp_mdt_data;
796 /* if the export has already been failed, we have no last_rcvd slot */
797 if (req->rq_export->exp_failed) {
798 CWARN("commit transaction for disconnected client %s: rc %d\n",
799 req->rq_export->exp_client_uuid.uuid, rc);
805 off = med->med_lr_off;
806 mutex_down(&med->med_lcd_lock);
807 if (lustre_msg_get_opc(req->rq_reqmsg) == MDS_CLOSE ||
808 lustre_msg_get_opc(req->rq_reqmsg) == MDS_DONE_WRITING) {
809 transno_p = &lcd->lcd_last_close_transno;
810 lcd->lcd_last_close_xid = req->rq_xid;
811 lcd->lcd_last_close_result = rc;
813 transno_p = &lcd->lcd_last_transno;
814 lcd->lcd_last_xid = req->rq_xid;
815 lcd->lcd_last_result = rc;
816 /*XXX: save intent_disposition in mdt_thread_info?
817 * also there is bug - intent_dispostion is __u64,
818 * see struct ldlm_reply->lock_policy_res1; */
819 lcd->lcd_last_data = mti->mti_opdata;
823 * When we store zero transno in lcd we can lost last transno value
824 * because lcd contains 0, but lsd is not yet written
825 * The server data should be updated also if the latest
826 * transno is rewritten by zero. See the bug 11125 for details.
828 if (mti->mti_transno == 0 &&
829 *transno_p == mdt->mdt_last_transno)
830 mdt_server_data_update(mti->mti_env, mdt);
832 *transno_p = mti->mti_transno;
835 CERROR("client idx %d has offset %lld\n", med->med_lr_idx, off);
838 err = mdt_last_rcvd_write(mti->mti_env, mdt, lcd, &off, th);
840 mutex_up(&med->med_lcd_lock);
844 extern struct lu_context_key mdt_thread_key;
846 /* add credits for last_rcvd update */
847 static int mdt_txn_start_cb(const struct lu_env *env,
848 struct txn_param *param, void *cookie)
850 param->tp_credits += MDT_TXN_LAST_RCVD_WRITE_CREDITS;
854 /* Update last_rcvd records with latests transaction data */
855 static int mdt_txn_stop_cb(const struct lu_env *env,
856 struct thandle *txn, void *cookie)
858 struct mdt_device *mdt = cookie;
859 struct mdt_txn_info *txi;
860 struct mdt_thread_info *mti;
861 struct ptlrpc_request *req;
863 /* transno in two contexts - for commit_cb and for thread */
864 txi = lu_context_key_get(&txn->th_ctx, &mdt_txn_key);
865 mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
866 req = mdt_info_req(mti);
868 if (mti->mti_mdt == NULL || req == NULL || mti->mti_no_need_trans) {
869 txi->txi_transno = 0;
870 mti->mti_no_need_trans = 0;
874 if (mti->mti_has_trans) {
875 /* XXX: currently there are allowed cases, but the wrong cases
876 * are also possible, so better check is needed here */
877 CDEBUG(D_INFO, "More than one transaction "LPU64"\n",
882 mti->mti_has_trans = 1;
883 spin_lock(&mdt->mdt_transno_lock);
884 if (txn->th_result != 0) {
885 if (mti->mti_transno != 0) {
886 CERROR("Replay transno "LPU64" failed: rc %i\n",
887 mti->mti_transno, txn->th_result);
888 mti->mti_transno = 0;
890 } else if (mti->mti_transno == 0) {
891 mti->mti_transno = ++ mdt->mdt_last_transno;
893 /* should be replay */
894 if (mti->mti_transno > mdt->mdt_last_transno)
895 mdt->mdt_last_transno = mti->mti_transno;
898 /* sometimes the reply message has not been successfully packed */
899 LASSERT(req != NULL && req->rq_repmsg != NULL);
901 /* filling reply data */
902 CDEBUG(D_INODE, "transno = %llu, last_committed = %llu\n",
903 mti->mti_transno, req->rq_export->exp_obd->obd_last_committed);
905 req->rq_transno = mti->mti_transno;
906 lustre_msg_set_transno(req->rq_repmsg, mti->mti_transno);
907 lustre_msg_set_last_xid(req->rq_repmsg,
908 lcd_last_xid(req->rq_export->exp_mdt_data.med_lcd));
909 /* save transno for the commit callback */
910 txi->txi_transno = mti->mti_transno;
911 spin_unlock(&mdt->mdt_transno_lock);
913 return mdt_last_rcvd_update(mti, txn);
916 /* commit callback, need to update last_committed value */
917 static int mdt_txn_commit_cb(const struct lu_env *env,
918 struct thandle *txn, void *cookie)
920 struct mdt_device *mdt = cookie;
921 struct obd_device *obd = md2lu_dev(&mdt->mdt_md_dev)->ld_obd;
922 struct mdt_txn_info *txi;
925 txi = lu_context_key_get(&txn->th_ctx, &mdt_txn_key);
927 /* copy of obd_transno_commit_cb() but with locking */
928 spin_lock(&mdt->mdt_transno_lock);
929 if (txi->txi_transno > obd->obd_last_committed) {
930 obd->obd_last_committed = txi->txi_transno;
931 spin_unlock(&mdt->mdt_transno_lock);
932 ptlrpc_commit_replies(obd);
934 spin_unlock(&mdt->mdt_transno_lock);
936 if (txi->txi_transno)
937 CDEBUG(D_HA, "%s: transno "LPD64" is committed\n",
938 obd->obd_name, txi->txi_transno);
940 /* iterate through all additional callbacks */
941 for (i = 0; i < txi->txi_cb_count; i++) {
942 txi->txi_cb[i].mdt_cb_func(mdt, txi->txi_transno,
943 txi->txi_cb[i].mdt_cb_data, 0);
948 int mdt_fs_setup(const struct lu_env *env, struct mdt_device *mdt,
949 struct obd_device *obd)
956 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_FS_SETUP))
959 /* prepare transactions callbacks */
960 mdt->mdt_txn_cb.dtc_txn_start = mdt_txn_start_cb;
961 mdt->mdt_txn_cb.dtc_txn_stop = mdt_txn_stop_cb;
962 mdt->mdt_txn_cb.dtc_txn_commit = mdt_txn_commit_cb;
963 mdt->mdt_txn_cb.dtc_cookie = mdt;
964 CFS_INIT_LIST_HEAD(&mdt->mdt_txn_cb.dtc_linkage);
966 dt_txn_callback_add(mdt->mdt_bottom, &mdt->mdt_txn_cb);
968 o = dt_store_open(env, mdt->mdt_bottom, LAST_RCVD, &fid);
970 mdt->mdt_last_rcvd = o;
971 rc = mdt_server_data_init(env, mdt);
973 GOTO(put_last_rcvd, rc);
976 CERROR("cannot open %s: rc = %d\n", LAST_RCVD, rc);
980 o = dt_store_open(env, mdt->mdt_bottom, CAPA_KEYS, &fid);
983 rc = mdt_capa_keys_init(env, mdt);
985 GOTO(put_ck_object, rc);
988 CERROR("cannot open %s: rc = %d\n", CAPA_KEYS, rc);
989 GOTO(put_last_rcvd, rc);
994 lu_object_put(env, &o->do_lu);
995 mdt->mdt_ck_obj = NULL;
997 lu_object_put(env, &mdt->mdt_last_rcvd->do_lu);
998 mdt->mdt_last_rcvd = NULL;
1002 void mdt_fs_cleanup(const struct lu_env *env, struct mdt_device *mdt)
1006 /* Remove transaction callback */
1007 dt_txn_callback_del(mdt->mdt_bottom, &mdt->mdt_txn_cb);
1008 if (mdt->mdt_last_rcvd)
1009 lu_object_put(env, &mdt->mdt_last_rcvd->do_lu);
1010 mdt->mdt_last_rcvd = NULL;
1011 if (mdt->mdt_ck_obj)
1012 lu_object_put(env, &mdt->mdt_ck_obj->do_lu);
1013 mdt->mdt_ck_obj = NULL;
1017 /* reconstruction code */
1018 static void mdt_steal_ack_locks(struct ptlrpc_request *req)
1020 struct obd_export *exp = req->rq_export;
1021 struct list_head *tmp;
1022 struct ptlrpc_reply_state *oldrep;
1023 struct ptlrpc_service *svc;
1026 /* CAVEAT EMPTOR: spinlock order */
1027 spin_lock(&exp->exp_lock);
1028 list_for_each (tmp, &exp->exp_outstanding_replies) {
1029 oldrep = list_entry(tmp, struct ptlrpc_reply_state,rs_exp_list);
1031 if (oldrep->rs_xid != req->rq_xid)
1034 if (lustre_msg_get_opc(oldrep->rs_msg) !=
1035 lustre_msg_get_opc(req->rq_reqmsg))
1036 CERROR ("Resent req xid "LPX64" has mismatched opc: "
1037 "new %d old %d\n", req->rq_xid,
1038 lustre_msg_get_opc(req->rq_reqmsg),
1039 lustre_msg_get_opc(oldrep->rs_msg));
1041 svc = oldrep->rs_service;
1042 spin_lock (&svc->srv_lock);
1044 list_del_init (&oldrep->rs_exp_list);
1046 CWARN("Stealing %d locks from rs %p x"LPD64".t"LPD64
1048 oldrep->rs_nlocks, oldrep,
1049 oldrep->rs_xid, oldrep->rs_transno,
1050 lustre_msg_get_opc(oldrep->rs_msg),
1051 libcfs_nid2str(exp->exp_connection->c_peer.nid));
1053 for (i = 0; i < oldrep->rs_nlocks; i++)
1054 ptlrpc_save_lock(req, &oldrep->rs_locks[i],
1055 oldrep->rs_modes[i], 0);
1056 oldrep->rs_nlocks = 0;
1058 DEBUG_REQ(D_HA, req, "stole locks for");
1059 ptlrpc_schedule_difficult_reply (oldrep);
1061 spin_unlock (&svc->srv_lock);
1064 spin_unlock(&exp->exp_lock);
1067 void mdt_req_from_lcd(struct ptlrpc_request *req,
1068 struct lsd_client_data *lcd)
1070 DEBUG_REQ(D_HA, req, "restoring transno "LPD64"/status %d",
1071 lcd->lcd_last_transno, lcd->lcd_last_result);
1073 if (lustre_msg_get_opc(req->rq_reqmsg) == MDS_CLOSE ||
1074 lustre_msg_get_opc(req->rq_repmsg) == MDS_DONE_WRITING) {
1075 req->rq_transno = lcd->lcd_last_close_transno;
1076 req->rq_status = lcd->lcd_last_close_result;
1077 lustre_msg_set_transno(req->rq_repmsg, req->rq_transno);
1078 lustre_msg_set_status(req->rq_repmsg, req->rq_status);
1080 req->rq_transno = lcd->lcd_last_transno;
1081 req->rq_status = lcd->lcd_last_result;
1082 lustre_msg_set_transno(req->rq_repmsg, req->rq_transno);
1083 lustre_msg_set_status(req->rq_repmsg, req->rq_status);
1085 mdt_steal_ack_locks(req);
1088 void mdt_reconstruct_generic(struct mdt_thread_info *mti,
1089 struct mdt_lock_handle *lhc)
1091 struct ptlrpc_request *req = mdt_info_req(mti);
1092 struct mdt_export_data *med = &req->rq_export->exp_mdt_data;
1094 return mdt_req_from_lcd(req, med->med_lcd);
1097 static void mdt_reconstruct_create(struct mdt_thread_info *mti,
1098 struct mdt_lock_handle *lhc)
1100 struct ptlrpc_request *req = mdt_info_req(mti);
1101 struct obd_export *exp = req->rq_export;
1102 struct mdt_export_data *med = &exp->exp_mdt_data;
1103 struct mdt_device *mdt = mti->mti_mdt;
1104 struct mdt_object *child;
1105 struct mdt_body *body;
1108 mdt_req_from_lcd(req, med->med_lcd);
1112 /* if no error, so child was created with requested fid */
1113 child = mdt_object_find(mti->mti_env, mdt, mti->mti_rr.rr_fid2);
1114 if (IS_ERR(child)) {
1115 rc = PTR_ERR(child);
1116 LCONSOLE_WARN("Child "DFID" lookup error %d."
1117 " Evicting client %s with export %s.\n",
1118 PFID(mdt_object_fid(child)), rc,
1119 obd_uuid2str(&exp->exp_client_uuid),
1120 obd_export_nid2str(exp));
1121 mdt_export_evict(exp);
1126 body = req_capsule_server_get(mti->mti_pill, &RMF_MDT_BODY);
1127 mti->mti_attr.ma_need = MA_INODE;
1128 mti->mti_attr.ma_valid = 0;
1129 rc = mo_attr_get(mti->mti_env, mdt_object_child(child), &mti->mti_attr);
1130 if (rc == -EREMOTE) {
1131 /* object was created on remote server */
1132 req->rq_status = rc;
1133 body->valid |= OBD_MD_MDS;
1135 mdt_pack_attr2body(mti, body, &mti->mti_attr.ma_attr,
1136 mdt_object_fid(child));
1137 mdt_object_put(mti->mti_env, child);
1140 static void mdt_reconstruct_setattr(struct mdt_thread_info *mti,
1141 struct mdt_lock_handle *lhc)
1143 struct ptlrpc_request *req = mdt_info_req(mti);
1144 struct obd_export *exp = req->rq_export;
1145 struct mdt_export_data *med = &exp->exp_mdt_data;
1146 struct mdt_device *mdt = mti->mti_mdt;
1147 struct mdt_object *obj;
1148 struct mdt_body *body;
1150 mdt_req_from_lcd(req, med->med_lcd);
1154 body = req_capsule_server_get(mti->mti_pill, &RMF_MDT_BODY);
1155 obj = mdt_object_find(mti->mti_env, mdt, mti->mti_rr.rr_fid1);
1157 int rc = PTR_ERR(obj);
1158 LCONSOLE_WARN(""DFID" lookup error %d."
1159 " Evicting client %s with export %s.\n",
1160 PFID(mdt_object_fid(obj)), rc,
1161 obd_uuid2str(&exp->exp_client_uuid),
1162 obd_export_nid2str(exp));
1163 mdt_export_evict(exp);
1167 mti->mti_attr.ma_need = MA_INODE;
1168 mti->mti_attr.ma_valid = 0;
1169 mo_attr_get(mti->mti_env, mdt_object_child(obj), &mti->mti_attr);
1170 mdt_pack_attr2body(mti, body, &mti->mti_attr.ma_attr,
1171 mdt_object_fid(obj));
1172 if (mti->mti_epoch && (mti->mti_epoch->flags & MF_EPOCH_OPEN)) {
1173 struct mdt_file_data *mfd;
1174 struct mdt_body *repbody;
1176 repbody = req_capsule_server_get(mti->mti_pill, &RMF_MDT_BODY);
1177 repbody->ioepoch = obj->mot_ioepoch;
1178 spin_lock(&med->med_open_lock);
1179 list_for_each_entry(mfd, &med->med_open_head, mfd_list) {
1180 if (mfd->mfd_xid == req->rq_xid)
1183 LASSERT(&mfd->mfd_list != &med->med_open_head);
1184 spin_unlock(&med->med_open_lock);
1185 repbody->handle.cookie = mfd->mfd_handle.h_cookie;
1188 mdt_object_put(mti->mti_env, obj);
1191 typedef void (*mdt_reconstructor)(struct mdt_thread_info *mti,
1192 struct mdt_lock_handle *lhc);
1194 static mdt_reconstructor reconstructors[REINT_MAX] = {
1195 [REINT_SETATTR] = mdt_reconstruct_setattr,
1196 [REINT_CREATE] = mdt_reconstruct_create,
1197 [REINT_LINK] = mdt_reconstruct_generic,
1198 [REINT_UNLINK] = mdt_reconstruct_generic,
1199 [REINT_RENAME] = mdt_reconstruct_generic,
1200 [REINT_OPEN] = mdt_reconstruct_open,
1201 [REINT_SETXATTR] = mdt_reconstruct_generic
1204 void mdt_reconstruct(struct mdt_thread_info *mti,
1205 struct mdt_lock_handle *lhc)
1208 reconstructors[mti->mti_rr.rr_opcode](mti, lhc);