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 [sun.com URL with a
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.
38 # define EXPORT_SYMTAB
40 #define DEBUG_SUBSYSTEM S_LMV
42 #include <linux/slab.h>
43 #include <linux/module.h>
44 #include <linux/init.h>
45 #include <linux/slab.h>
46 #include <linux/pagemap.h>
48 #include <asm/div64.h>
49 #include <linux/seq_file.h>
50 #include <linux/namei.h>
52 #include <liblustre.h>
55 #include <linux/ext2_fs.h>
57 #include <lustre/lustre_idl.h>
58 #include <lustre_log.h>
59 #include <obd_support.h>
60 #include <lustre_lib.h>
61 #include <lustre_net.h>
62 #include <obd_class.h>
63 #include <lprocfs_status.h>
64 #include <lustre_lite.h>
65 #include <lustre_fid.h>
66 #include "lmv_internal.h"
68 /* not defined for liblustre building */
69 #if !defined(ATOMIC_INIT)
70 #define ATOMIC_INIT(val) { (val) }
74 cfs_mem_cache_t *obj_cache;
75 atomic_t obj_cache_count = ATOMIC_INIT(0);
77 static void lmv_activate_target(struct lmv_obd *lmv,
78 struct lmv_tgt_desc *tgt,
81 if (tgt->ltd_active == activate)
84 tgt->ltd_active = activate;
85 lmv->desc.ld_active_tgt_count += (activate ? 1 : -1);
90 * -EINVAL : UUID can't be found in the LMV's target list
91 * -ENOTCONN: The UUID is found, but the target connection is bad (!)
92 * -EBADF : The UUID is found, but the OBD of the wrong type (!)
94 static int lmv_set_mdc_active(struct lmv_obd *lmv, struct obd_uuid *uuid,
97 struct lmv_tgt_desc *tgt;
98 struct obd_device *obd;
102 CDEBUG(D_INFO, "Searching in lmv %p for uuid %s (activate=%d)\n",
103 lmv, uuid->uuid, activate);
105 spin_lock(&lmv->lmv_lock);
106 for (i = 0, tgt = lmv->tgts; i < lmv->desc.ld_tgt_count; i++, tgt++) {
107 if (tgt->ltd_exp == NULL)
110 CDEBUG(D_INFO, "lmv idx %d is %s conn "LPX64"\n",
111 i, tgt->ltd_uuid.uuid, tgt->ltd_exp->exp_handle.h_cookie);
113 if (obd_uuid_equals(uuid, &tgt->ltd_uuid))
117 if (i == lmv->desc.ld_tgt_count)
118 GOTO(out_lmv_lock, rc = -EINVAL);
120 obd = class_exp2obd(tgt->ltd_exp);
122 GOTO(out_lmv_lock, rc = -ENOTCONN);
124 CDEBUG(D_INFO, "Found OBD %s=%s device %d (%p) type %s at LMV idx %d\n",
125 obd->obd_name, obd->obd_uuid.uuid, obd->obd_minor, obd,
126 obd->obd_type->typ_name, i);
127 LASSERT(strcmp(obd->obd_type->typ_name, LUSTRE_MDC_NAME) == 0);
129 if (tgt->ltd_active == activate) {
130 CDEBUG(D_INFO, "OBD %p already %sactive!\n", obd,
131 activate ? "" : "in");
132 GOTO(out_lmv_lock, rc);
135 CDEBUG(D_INFO, "Marking OBD %p %sactive\n",
136 obd, activate ? "" : "in");
138 lmv_activate_target(lmv, tgt, activate);
143 spin_unlock(&lmv->lmv_lock);
147 static int lmv_set_mdc_data(struct lmv_obd *lmv, struct obd_uuid *uuid,
148 struct obd_connect_data *data)
150 struct lmv_tgt_desc *tgt;
154 LASSERT(data != NULL);
156 spin_lock(&lmv->lmv_lock);
157 for (i = 0, tgt = lmv->tgts; i < lmv->desc.ld_tgt_count; i++, tgt++) {
158 if (tgt->ltd_exp == NULL)
161 if (obd_uuid_equals(uuid, &tgt->ltd_uuid)) {
162 lmv->datas[tgt->ltd_idx] = *data;
166 spin_unlock(&lmv->lmv_lock);
170 static int lmv_notify(struct obd_device *obd, struct obd_device *watched,
171 enum obd_notify_event ev, void *data)
173 struct lmv_obd *lmv = &obd->u.lmv;
174 struct obd_uuid *uuid;
178 if (strcmp(watched->obd_type->typ_name, LUSTRE_MDC_NAME)) {
179 CERROR("unexpected notification of %s %s!\n",
180 watched->obd_type->typ_name,
185 uuid = &watched->u.cli.cl_target_uuid;
186 if (ev == OBD_NOTIFY_ACTIVE || ev == OBD_NOTIFY_INACTIVE) {
188 * Set MDC as active before notifying the observer, so the
189 * observer can use the MDC normally.
191 rc = lmv_set_mdc_active(lmv, uuid,
192 ev == OBD_NOTIFY_ACTIVE);
194 CERROR("%sactivation of %s failed: %d\n",
195 ev == OBD_NOTIFY_ACTIVE ? "" : "de",
199 } else if (ev == OBD_NOTIFY_OCD) {
200 struct obd_connect_data *conn_data =
201 &watched->u.cli.cl_import->imp_connect_data;
203 /* Set connect data to desired target, update
204 * exp_connect_flags. */
205 rc = lmv_set_mdc_data(lmv, uuid, conn_data);
207 CERROR("can't set connect data to target %s, rc %d\n",
213 * XXX: Make sure that ocd_connect_flags from all targets are
214 * the same. Otherwise one of MDTs runs wrong version or
215 * something like this. --umka
217 obd->obd_self_export->exp_connect_flags =
218 conn_data->ocd_connect_flags;
221 else if (ev == OBD_NOTIFY_DISCON) {
222 /* For disconnect event, flush fld cache for failout MDS case. */
223 fld_client_flush(&lmv->lmv_fld);
226 /* Pass the notification up the chain. */
227 if (obd->obd_observer)
228 rc = obd_notify(obd->obd_observer, watched, ev, data);
233 /* this is fake connect function. Its purpose is to initialize lmv and say
234 * caller that everything is okay. Real connection will be performed later. */
235 static int lmv_connect(const struct lu_env *env,
236 struct lustre_handle *conn, struct obd_device *obd,
237 struct obd_uuid *cluuid, struct obd_connect_data *data,
241 struct proc_dir_entry *lmv_proc_dir;
243 struct lmv_obd *lmv = &obd->u.lmv;
244 struct obd_export *exp;
248 rc = class_connect(conn, obd, cluuid);
250 CERROR("class_connection() returned %d\n", rc);
254 exp = class_conn2export(conn);
256 /* we don't want to actually do the underlying connections more than
257 * once, so keep track. */
259 if (lmv->refcount > 1) {
260 class_export_put(exp);
266 lmv->cluuid = *cluuid;
269 lmv->conn_data = *data;
272 lmv_proc_dir = lprocfs_register("target_obds", obd->obd_proc_entry,
274 if (IS_ERR(lmv_proc_dir)) {
275 CERROR("could not register /proc/fs/lustre/%s/%s/target_obds.",
276 obd->obd_type->typ_name, obd->obd_name);
281 /* all real clients should perform actual connection right away, because
282 * it is possible, that LMV will not have opportunity to connect targets
283 * and MDC stuff will be called directly, for instance while reading
284 * ../mdc/../kbytesfree procfs file, etc. */
285 if (data->ocd_connect_flags & OBD_CONNECT_REAL)
286 rc = lmv_check_connect(obd);
291 lprocfs_remove(&lmv_proc_dir);
298 static void lmv_set_timeouts(struct obd_device *obd)
300 struct lmv_tgt_desc *tgts;
305 if (lmv->server_timeout == 0)
308 if (lmv->connected == 0)
311 for (i = 0, tgts = lmv->tgts; i < lmv->desc.ld_tgt_count; i++, tgts++) {
312 if (tgts->ltd_exp == NULL)
315 obd_set_info_async(tgts->ltd_exp, sizeof(KEY_INTERMDS),
316 KEY_INTERMDS, 0, NULL, NULL);
320 static int lmv_init_ea_size(struct obd_export *exp, int easize,
321 int def_easize, int cookiesize)
323 struct obd_device *obd = exp->exp_obd;
324 struct lmv_obd *lmv = &obd->u.lmv;
325 int i, rc = 0, change = 0;
328 if (lmv->max_easize < easize) {
329 lmv->max_easize = easize;
332 if (lmv->max_def_easize < def_easize) {
333 lmv->max_def_easize = def_easize;
336 if (lmv->max_cookiesize < cookiesize) {
337 lmv->max_cookiesize = cookiesize;
343 if (lmv->connected == 0)
346 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
347 if (lmv->tgts[i].ltd_exp == NULL) {
348 CWARN("%s: NULL export for %d\n", obd->obd_name, i);
352 rc = md_init_ea_size(lmv->tgts[i].ltd_exp, easize, def_easize,
355 CERROR("obd_init_ea_size() failed on MDT target %d, "
356 "error %d.\n", i, rc);
363 #define MAX_STRING_SIZE 128
365 int lmv_connect_mdc(struct obd_device *obd, struct lmv_tgt_desc *tgt)
367 struct lmv_obd *lmv = &obd->u.lmv;
368 struct obd_uuid *cluuid = &lmv->cluuid;
369 struct obd_connect_data *mdc_data = NULL;
370 struct obd_uuid lmv_mdc_uuid = { "LMV_MDC_UUID" };
371 struct lustre_handle conn = {0, };
372 struct obd_device *mdc_obd;
373 struct obd_export *mdc_exp;
374 struct lu_fld_target target;
377 struct proc_dir_entry *lmv_proc_dir;
381 /* for MDS: don't connect to yourself */
382 if (obd_uuid_equals(&tgt->ltd_uuid, cluuid)) {
383 CDEBUG(D_CONFIG, "don't connect back to %s\n", cluuid->uuid);
384 /* XXX - the old code didn't increment active tgt count.
389 mdc_obd = class_find_client_obd(&tgt->ltd_uuid, LUSTRE_MDC_NAME,
392 CERROR("target %s not attached\n", tgt->ltd_uuid.uuid);
396 CDEBUG(D_CONFIG, "connect to %s(%s) - %s, %s FOR %s\n",
397 mdc_obd->obd_name, mdc_obd->obd_uuid.uuid,
398 tgt->ltd_uuid.uuid, obd->obd_uuid.uuid,
401 if (!mdc_obd->obd_set_up) {
402 CERROR("target %s is not set up\n", tgt->ltd_uuid.uuid);
406 rc = obd_connect(NULL, &conn, mdc_obd, &lmv_mdc_uuid,
407 &lmv->conn_data, NULL);
409 CERROR("target %s connect error %d\n", tgt->ltd_uuid.uuid, rc);
413 mdc_exp = class_conn2export(&conn);
415 /* Init fid sequence client for this mdc. */
416 rc = obd_fid_init(mdc_exp);
420 /* Add new FLD target. */
421 target.ft_srv = NULL;
422 target.ft_exp = mdc_exp;
423 target.ft_idx = tgt->ltd_idx;
425 fld_client_add_target(&lmv->lmv_fld, &target);
427 mdc_data = &class_exp2cliimp(mdc_exp)->imp_connect_data;
429 rc = obd_register_observer(mdc_obd, obd);
431 obd_disconnect(mdc_exp);
432 CERROR("target %s register_observer error %d\n",
433 tgt->ltd_uuid.uuid, rc);
437 if (obd->obd_observer) {
438 /* tell the mds_lmv about the new target */
439 rc = obd_notify(obd->obd_observer, mdc_exp->exp_obd,
440 OBD_NOTIFY_ACTIVE, (void *)(tgt - lmv->tgts));
442 obd_disconnect(mdc_exp);
448 tgt->ltd_exp = mdc_exp;
449 lmv->desc.ld_active_tgt_count++;
451 /* copy connect data, it may be used later */
452 lmv->datas[tgt->ltd_idx] = *mdc_data;
454 md_init_ea_size(tgt->ltd_exp, lmv->max_easize,
455 lmv->max_def_easize, lmv->max_cookiesize);
457 CDEBUG(D_CONFIG, "connected to %s(%s) successfully (%d)\n",
458 mdc_obd->obd_name, mdc_obd->obd_uuid.uuid,
459 atomic_read(&obd->obd_refcount));
462 lmv_proc_dir = lprocfs_srch(obd->obd_proc_entry, "target_obds");
464 struct proc_dir_entry *mdc_symlink;
465 char name[MAX_STRING_SIZE + 1];
467 LASSERT(mdc_obd->obd_type != NULL);
468 LASSERT(mdc_obd->obd_type->typ_name != NULL);
469 name[MAX_STRING_SIZE] = '\0';
470 snprintf(name, MAX_STRING_SIZE, "../../../%s/%s",
471 mdc_obd->obd_type->typ_name,
473 mdc_symlink = proc_symlink(mdc_obd->obd_name,
475 if (mdc_symlink == NULL) {
476 CERROR("could not register LMV target "
477 "/proc/fs/lustre/%s/%s/target_obds/%s.",
478 obd->obd_type->typ_name, obd->obd_name,
480 lprocfs_remove(&lmv_proc_dir);
488 int lmv_add_target(struct obd_device *obd, struct obd_uuid *tgt_uuid)
490 struct lmv_obd *lmv = &obd->u.lmv;
491 struct lmv_tgt_desc *tgt;
495 CDEBUG(D_CONFIG, "tgt_uuid: %s.\n", tgt_uuid->uuid);
499 if (lmv->desc.ld_active_tgt_count >= LMV_MAX_TGT_COUNT) {
500 lmv_init_unlock(lmv);
501 CERROR("can't add %s, LMV module compiled for %d MDCs. "
502 "That many MDCs already configured.\n",
503 tgt_uuid->uuid, LMV_MAX_TGT_COUNT);
506 if (lmv->desc.ld_tgt_count == 0) {
507 struct obd_device *mdc_obd;
509 mdc_obd = class_find_client_obd(tgt_uuid, LUSTRE_MDC_NAME,
512 lmv_init_unlock(lmv);
513 CERROR("Target %s not attached\n", tgt_uuid->uuid);
517 rc = obd_llog_init(obd, &obd->obd_olg, mdc_obd, 0, NULL, tgt_uuid);
519 lmv_init_unlock(lmv);
520 CERROR("lmv failed to setup llogging subsystems\n");
523 spin_lock(&lmv->lmv_lock);
524 tgt = lmv->tgts + lmv->desc.ld_tgt_count++;
525 tgt->ltd_uuid = *tgt_uuid;
526 spin_unlock(&lmv->lmv_lock);
528 if (lmv->connected) {
529 rc = lmv_connect_mdc(obd, tgt);
531 spin_lock(&lmv->lmv_lock);
532 lmv->desc.ld_tgt_count--;
533 memset(tgt, 0, sizeof(*tgt));
534 spin_unlock(&lmv->lmv_lock);
536 int easize = sizeof(struct lmv_stripe_md) +
537 lmv->desc.ld_tgt_count *
538 sizeof(struct lu_fid);
539 lmv_init_ea_size(obd->obd_self_export, easize, 0, 0);
543 lmv_init_unlock(lmv);
547 /* performs a check if passed obd is connected. If no - connect it. */
548 int lmv_check_connect(struct obd_device *obd)
550 struct lmv_obd *lmv = &obd->u.lmv;
551 struct lmv_tgt_desc *tgt;
559 if (lmv->connected) {
560 lmv_init_unlock(lmv);
564 if (lmv->desc.ld_tgt_count == 0) {
565 CERROR("%s: no targets configured.\n", obd->obd_name);
569 CDEBUG(D_CONFIG, "time to connect %s to %s\n",
570 lmv->cluuid.uuid, obd->obd_name);
572 LASSERT(lmv->tgts != NULL);
574 for (i = 0, tgt = lmv->tgts; i < lmv->desc.ld_tgt_count; i++, tgt++) {
575 rc = lmv_connect_mdc(obd, tgt);
580 lmv_set_timeouts(obd);
581 class_export_put(lmv->exp);
583 easize = lmv_get_easize(lmv);
584 lmv_init_ea_size(obd->obd_self_export, easize, 0, 0);
585 lmv_init_unlock(lmv);
594 --lmv->desc.ld_active_tgt_count;
595 rc2 = obd_disconnect(tgt->ltd_exp);
597 CERROR("error: LMV target %s disconnect on "
598 "MDC idx %d: error %d\n",
599 tgt->ltd_uuid.uuid, i, rc2);
603 class_disconnect(lmv->exp);
604 lmv_init_unlock(lmv);
608 static int lmv_disconnect_mdc(struct obd_device *obd, struct lmv_tgt_desc *tgt)
611 struct proc_dir_entry *lmv_proc_dir;
613 struct lmv_obd *lmv = &obd->u.lmv;
614 struct obd_device *mdc_obd;
618 LASSERT(tgt != NULL);
619 LASSERT(obd != NULL);
621 mdc_obd = class_exp2obd(tgt->ltd_exp);
624 mdc_obd->obd_no_recov = obd->obd_no_recov;
627 lmv_proc_dir = lprocfs_srch(obd->obd_proc_entry, "target_obds");
629 struct proc_dir_entry *mdc_symlink;
631 mdc_symlink = lprocfs_srch(lmv_proc_dir, mdc_obd->obd_name);
633 lprocfs_remove(&mdc_symlink);
635 CERROR("/proc/fs/lustre/%s/%s/target_obds/%s missing\n",
636 obd->obd_type->typ_name, obd->obd_name,
641 rc = obd_fid_fini(tgt->ltd_exp);
643 CERROR("Can't finanize fids factory\n");
645 CDEBUG(D_OTHER, "Disconnected from %s(%s) successfully\n",
646 tgt->ltd_exp->exp_obd->obd_name,
647 tgt->ltd_exp->exp_obd->obd_uuid.uuid);
649 obd_register_observer(tgt->ltd_exp->exp_obd, NULL);
650 rc = obd_disconnect(tgt->ltd_exp);
652 if (tgt->ltd_active) {
653 CERROR("Target %s disconnect error %d\n",
654 tgt->ltd_uuid.uuid, rc);
658 lmv_activate_target(lmv, tgt, 0);
663 static int lmv_disconnect(struct obd_export *exp)
665 struct obd_device *obd = class_exp2obd(exp);
667 struct proc_dir_entry *lmv_proc_dir;
669 struct lmv_obd *lmv = &obd->u.lmv;
676 /* Only disconnect the underlying layers on the final disconnect. */
678 if (lmv->refcount != 0)
681 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
682 if (lmv->tgts[i].ltd_exp == NULL)
684 lmv_disconnect_mdc(obd, &lmv->tgts[i]);
688 lmv_proc_dir = lprocfs_srch(obd->obd_proc_entry, "target_obds");
690 lprocfs_remove(&lmv_proc_dir);
692 CERROR("/proc/fs/lustre/%s/%s/target_obds missing\n",
693 obd->obd_type->typ_name, obd->obd_name);
699 * This is the case when no real connection is established by
700 * lmv_check_connect().
703 class_export_put(exp);
704 rc = class_disconnect(exp);
705 if (lmv->refcount == 0)
710 static int lmv_iocontrol(unsigned int cmd, struct obd_export *exp,
711 int len, void *karg, void *uarg)
713 struct obd_device *obddev = class_exp2obd(exp);
714 struct lmv_obd *lmv = &obddev->u.lmv;
715 int i, rc = 0, set = 0;
718 if (lmv->desc.ld_tgt_count == 0)
722 case IOC_OBD_STATFS: {
723 struct obd_ioctl_data *data = karg;
724 struct obd_device *mdc_obd;
725 struct obd_statfs stat_buf = {0};
728 memcpy(&index, data->ioc_inlbuf2, sizeof(__u32));
729 LASSERT(data->ioc_plen1 == sizeof(struct obd_statfs));
731 if ((index >= lmv->desc.ld_tgt_count))
734 if (!lmv->tgts[index].ltd_active)
737 mdc_obd = class_exp2obd(lmv->tgts[index].ltd_exp);
741 /* got statfs data */
742 rc = obd_statfs(mdc_obd, &stat_buf,
743 cfs_time_current_64() - HZ, 0);
746 if (copy_to_user(data->ioc_pbuf1, &stat_buf, data->ioc_plen1))
749 rc = copy_to_user(data->ioc_pbuf2, obd2cli_tgt(mdc_obd),
754 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
757 if (lmv->tgts[i].ltd_exp == NULL)
760 err = obd_iocontrol(cmd, lmv->tgts[i].ltd_exp, len,
763 if (lmv->tgts[i].ltd_active) {
764 CERROR("error: iocontrol MDC %s on MDT"
765 "idx %d cmd %x: err = %d\n",
766 lmv->tgts[i].ltd_uuid.uuid,
786 static int lmv_all_chars_policy(int count, const char *name,
797 static int lmv_nid_policy(struct lmv_obd *lmv)
799 struct obd_import *imp = class_exp2cliimp(lmv->tgts[0].ltd_exp);
802 * XXX Hack: to get nid we assume that underlying obd device is mdc.
804 id = imp->imp_connection->c_self ^ (imp->imp_connection->c_self >> 32);
805 return id % lmv->desc.ld_tgt_count;
808 static int lmv_choose_mds(struct lmv_obd *lmv, struct md_op_data *op_data,
813 return lmv_all_chars_policy(lmv->desc.ld_tgt_count,
815 op_data->op_namelen);
817 return lmv_nid_policy(lmv);
823 CERROR("unsupport type %d \n", type);
827 /* This is _inode_ placement policy function (not name). */
828 static int lmv_placement_policy(struct obd_device *obd,
829 struct md_op_data *op_data,
832 struct lmv_obd *lmv = &obd->u.lmv;
837 LASSERT(mds != NULL);
840 * Allocate new fid on target according to operation type and parent
843 obj = lmv_obj_grab(obd, &op_data->op_fid1);
844 if (obj != NULL || op_data->op_name == NULL ||
845 op_data->op_opc != LUSTRE_OPC_MKDIR) {
847 * Allocate fid for non-dir or for null name or for case parent
854 * If we have this flag turned on, and we see that
855 * parent dir is split, this means, that caller did not
856 * notice split yet. This is race and we would like to
857 * let caller know that.
859 if (op_data->op_bias & MDS_CHECK_SPLIT)
864 * Allocate new fid on same mds where parent fid is located and
865 * where operation will be sent. In case of split dir, ->op_fid1
866 * and ->op_mds here will contain fid and mds of slave directory
867 * object (assigned by caller).
869 *mds = op_data->op_mds;
873 /* XXX: This should be removed later wehn we sure it is not
875 rc = lmv_fld_lookup(lmv, &op_data->op_fid1, mds);
881 * Parent directory is not split and we want to create a
882 * directory in it. Let's calculate where to place it according
885 *mds = lmv_choose_mds(lmv, op_data, NID_TYPE);
893 CERROR("Can't choose MDS, err = %d\n", rc);
895 LASSERT(*mds < lmv->desc.ld_tgt_count);
901 int __lmv_fid_alloc(struct lmv_obd *lmv, struct lu_fid *fid,
904 struct lmv_tgt_desc *tgt = &lmv->tgts[mds];
908 /* New seq alloc and FLD setup should be atomic. */
909 down(&tgt->ltd_fid_sem);
911 /* Asking underlaying tgt layer to allocate new fid. */
912 rc = obd_fid_alloc(tgt->ltd_exp, fid, NULL);
914 LASSERT(fid_is_sane(fid));
916 /* Client switches to new sequence, setup FLD. */
917 rc = fld_client_create(&lmv->lmv_fld, fid_seq(fid),
920 CERROR("Can't create fld entry, rc %d\n", rc);
921 /* Delete just allocated fid sequence */
922 obd_fid_delete(tgt->ltd_exp, NULL);
925 up(&tgt->ltd_fid_sem);
929 int lmv_fid_alloc(struct obd_export *exp, struct lu_fid *fid,
930 struct md_op_data *op_data)
932 struct obd_device *obd = class_exp2obd(exp);
933 struct lmv_obd *lmv = &obd->u.lmv;
938 LASSERT(op_data != NULL);
939 LASSERT(fid != NULL);
941 rc = lmv_placement_policy(obd, op_data, &mds);
943 CERROR("Can't get target for allocating fid, "
948 rc = __lmv_fid_alloc(lmv, fid, mds);
950 CERROR("Can't alloc new fid, rc %d\n", rc);
957 static int lmv_fid_delete(struct obd_export *exp, const struct lu_fid *fid)
962 if (lmv_obj_delete(exp, fid)) {
963 CDEBUG(D_OTHER, "lmv object "DFID" is destroyed.\n",
969 static int lmv_setup(struct obd_device *obd, struct lustre_cfg *lcfg)
971 struct lmv_obd *lmv = &obd->u.lmv;
972 struct lprocfs_static_vars lvars;
973 struct lmv_desc *desc;
977 if (LUSTRE_CFG_BUFLEN(lcfg, 1) < 1) {
978 CERROR("LMV setup requires a descriptor\n");
982 desc = (struct lmv_desc *)lustre_cfg_buf(lcfg, 1);
983 if (sizeof(*desc) > LUSTRE_CFG_BUFLEN(lcfg, 1)) {
984 CERROR("descriptor size wrong: %d > %d\n",
985 (int)sizeof(*desc), LUSTRE_CFG_BUFLEN(lcfg, 1));
989 lmv->tgts_size = LMV_MAX_TGT_COUNT * sizeof(struct lmv_tgt_desc);
991 OBD_ALLOC(lmv->tgts, lmv->tgts_size);
992 if (lmv->tgts == NULL)
995 for (i = 0; i < LMV_MAX_TGT_COUNT; i++) {
996 sema_init(&lmv->tgts[i].ltd_fid_sem, 1);
997 lmv->tgts[i].ltd_idx = i;
1000 lmv->datas_size = LMV_MAX_TGT_COUNT * sizeof(struct obd_connect_data);
1002 OBD_ALLOC(lmv->datas, lmv->datas_size);
1003 if (lmv->datas == NULL)
1004 GOTO(out_free_tgts, rc = -ENOMEM);
1006 obd_str2uuid(&lmv->desc.ld_uuid, desc->ld_uuid.uuid);
1007 lmv->desc.ld_tgt_count = 0;
1008 lmv->desc.ld_active_tgt_count = 0;
1009 lmv->max_cookiesize = 0;
1010 lmv->max_def_easize = 0;
1011 lmv->max_easize = 0;
1013 spin_lock_init(&lmv->lmv_lock);
1014 sema_init(&lmv->init_sem, 1);
1016 rc = lmv_obj_setup(obd);
1018 CERROR("Can't setup LMV object manager, "
1020 GOTO(out_free_datas, rc);
1023 lprocfs_lmv_init_vars(&lvars);
1024 lprocfs_obd_setup(obd, lvars.obd_vars);
1027 rc = lprocfs_seq_create(obd->obd_proc_entry, "target_obd_status",
1028 0444, &lmv_proc_target_fops, obd);
1030 CWARN("Error adding the target_obd_status file\n");
1033 rc = fld_client_init(&lmv->lmv_fld, obd->obd_name,
1034 LUSTRE_CLI_FLD_HASH_DHT);
1036 CERROR("can't init FLD, err %d\n",
1038 GOTO(out_free_datas, rc);
1044 OBD_FREE(lmv->datas, lmv->datas_size);
1047 OBD_FREE(lmv->tgts, lmv->tgts_size);
1052 static int lmv_cleanup(struct obd_device *obd)
1054 struct lmv_obd *lmv = &obd->u.lmv;
1057 fld_client_fini(&lmv->lmv_fld);
1058 lprocfs_obd_cleanup(obd);
1059 lmv_obj_cleanup(obd);
1060 OBD_FREE(lmv->datas, lmv->datas_size);
1061 OBD_FREE(lmv->tgts, lmv->tgts_size);
1066 static int lmv_process_config(struct obd_device *obd, obd_count len, void *buf)
1068 struct lustre_cfg *lcfg = buf;
1069 struct obd_uuid tgt_uuid;
1073 switch(lcfg->lcfg_command) {
1075 if (LUSTRE_CFG_BUFLEN(lcfg, 1) > sizeof(tgt_uuid.uuid))
1076 GOTO(out, rc = -EINVAL);
1078 obd_str2uuid(&tgt_uuid, lustre_cfg_string(lcfg, 1));
1079 rc = lmv_add_target(obd, &tgt_uuid);
1082 CERROR("Unknown command: %d\n", lcfg->lcfg_command);
1083 GOTO(out, rc = -EINVAL);
1090 static int lmv_statfs(struct obd_device *obd, struct obd_statfs *osfs,
1091 __u64 max_age, __u32 flags)
1093 struct lmv_obd *lmv = &obd->u.lmv;
1094 struct obd_statfs *temp;
1098 rc = lmv_check_connect(obd);
1102 OBD_ALLOC(temp, sizeof(*temp));
1106 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
1107 if (lmv->tgts[i].ltd_exp == NULL)
1110 rc = obd_statfs(lmv->tgts[i].ltd_exp->exp_obd, temp,
1113 CERROR("can't stat MDS #%d (%s), error %d\n", i,
1114 lmv->tgts[i].ltd_exp->exp_obd->obd_name,
1116 GOTO(out_free_temp, rc);
1121 osfs->os_bavail += temp->os_bavail;
1122 osfs->os_blocks += temp->os_blocks;
1123 osfs->os_ffree += temp->os_ffree;
1124 osfs->os_files += temp->os_files;
1130 OBD_FREE(temp, sizeof(*temp));
1134 static int lmv_getstatus(struct obd_export *exp,
1136 struct obd_capa **pc)
1138 struct obd_device *obd = exp->exp_obd;
1139 struct lmv_obd *lmv = &obd->u.lmv;
1143 rc = lmv_check_connect(obd);
1147 rc = md_getstatus(lmv->tgts[0].ltd_exp, fid, pc);
1152 static int lmv_getxattr(struct obd_export *exp, const struct lu_fid *fid,
1153 struct obd_capa *oc, obd_valid valid, const char *name,
1154 const char *input, int input_size, int output_size,
1155 int flags, struct ptlrpc_request **request)
1157 struct obd_device *obd = exp->exp_obd;
1158 struct lmv_obd *lmv = &obd->u.lmv;
1159 struct obd_export *tgt_exp;
1163 rc = lmv_check_connect(obd);
1167 tgt_exp = lmv_find_export(lmv, fid);
1168 if (IS_ERR(tgt_exp))
1169 RETURN(PTR_ERR(tgt_exp));
1171 rc = md_getxattr(tgt_exp, fid, oc, valid, name, input, input_size,
1172 output_size, flags, request);
1177 static int lmv_setxattr(struct obd_export *exp, const struct lu_fid *fid,
1178 struct obd_capa *oc, obd_valid valid, const char *name,
1179 const char *input, int input_size, int output_size,
1180 int flags, __u32 suppgid,
1181 struct ptlrpc_request **request)
1183 struct obd_device *obd = exp->exp_obd;
1184 struct lmv_obd *lmv = &obd->u.lmv;
1185 struct obd_export *tgt_exp;
1189 rc = lmv_check_connect(obd);
1193 tgt_exp = lmv_find_export(lmv, fid);
1194 if (IS_ERR(tgt_exp))
1195 RETURN(PTR_ERR(tgt_exp));
1197 rc = md_setxattr(tgt_exp, fid, oc, valid, name,
1198 input, input_size, output_size, flags, suppgid,
1204 static int lmv_getattr(struct obd_export *exp, const struct lu_fid *fid,
1205 struct obd_capa *oc, obd_valid valid, int ea_size,
1206 struct ptlrpc_request **request)
1208 struct obd_device *obd = exp->exp_obd;
1209 struct lmv_obd *lmv = &obd->u.lmv;
1210 struct obd_export *tgt_exp;
1211 struct lmv_obj *obj;
1215 rc = lmv_check_connect(obd);
1219 tgt_exp = lmv_find_export(lmv, fid);
1220 if (IS_ERR(tgt_exp))
1221 RETURN(PTR_ERR(tgt_exp));
1223 rc = md_getattr(tgt_exp, fid, oc, valid, ea_size, request);
1227 obj = lmv_obj_grab(obd, fid);
1229 CDEBUG(D_OTHER, "GETATTR for "DFID" %s\n", PFID(fid),
1230 obj ? "(split)" : "");
1233 * If object is split, then we loop over all the slaves and gather size
1234 * attribute. In ideal world we would have to gather also mds field from
1235 * all slaves, as object is spread over the cluster and this is
1236 * definitely interesting information and it is not good to loss it,
1240 struct mdt_body *body;
1242 if (*request == NULL) {
1247 body = req_capsule_server_get(&(*request)->rq_pill,
1249 LASSERT(body != NULL);
1253 for (i = 0; i < obj->lo_objcount; i++) {
1254 if (lmv->tgts[i].ltd_exp == NULL) {
1255 CWARN("%s: NULL export for %d\n",
1260 /* skip master obj. */
1261 if (lu_fid_eq(&obj->lo_fid, &obj->lo_inodes[i].li_fid))
1264 lmv_update_body(body, &obj->lo_inodes[i]);
1267 lmv_obj_unlock(obj);
1274 static int lmv_change_cbdata(struct obd_export *exp, const struct lu_fid *fid,
1275 ldlm_iterator_t it, void *data)
1277 struct obd_device *obd = exp->exp_obd;
1278 struct lmv_obd *lmv = &obd->u.lmv;
1282 rc = lmv_check_connect(obd);
1286 CDEBUG(D_OTHER, "CBDATA for "DFID"\n", PFID(fid));
1289 * With CMD every object can have two locks in different namespaces:
1290 * lookup lock in space of mds storing direntry and update/open lock in
1291 * space of mds storing inode.
1293 for (i = 0; i < lmv->desc.ld_tgt_count; i++)
1294 md_change_cbdata(lmv->tgts[i].ltd_exp, fid, it, data);
1299 static int lmv_close(struct obd_export *exp,
1300 struct md_op_data *op_data,
1301 struct md_open_data *mod,
1302 struct ptlrpc_request **request)
1304 struct obd_device *obd = exp->exp_obd;
1305 struct lmv_obd *lmv = &obd->u.lmv;
1306 struct obd_export *tgt_exp;
1310 rc = lmv_check_connect(obd);
1314 tgt_exp = lmv_find_export(lmv, &op_data->op_fid1);
1315 if (IS_ERR(tgt_exp))
1316 RETURN(PTR_ERR(tgt_exp));
1318 CDEBUG(D_OTHER, "CLOSE "DFID"\n", PFID(&op_data->op_fid1));
1319 rc = md_close(tgt_exp, op_data, mod, request);
1324 * Called in the case MDS returns -ERESTART on create on open, what means that
1325 * directory is split and its LMV presentation object has to be updated.
1327 int lmv_handle_split(struct obd_export *exp, const struct lu_fid *fid)
1329 struct obd_device *obd = exp->exp_obd;
1330 struct lmv_obd *lmv = &obd->u.lmv;
1331 struct ptlrpc_request *req = NULL;
1332 struct obd_export *tgt_exp;
1333 struct lmv_obj *obj;
1334 struct lustre_md md;
1340 mealen = lmv_get_easize(lmv);
1342 valid = OBD_MD_FLEASIZE | OBD_MD_FLDIREA | OBD_MD_MEA;
1344 tgt_exp = lmv_find_export(lmv, fid);
1345 if (IS_ERR(tgt_exp))
1346 RETURN(PTR_ERR(tgt_exp));
1348 /* time to update mea of parent fid */
1349 rc = md_getattr(tgt_exp, fid, NULL, valid, mealen, &req);
1351 CERROR("md_getattr() failed, error %d\n", rc);
1355 rc = md_get_lustre_md(tgt_exp, req, NULL, exp, &md);
1357 CERROR("mdc_get_lustre_md() failed, error %d\n", rc);
1362 GOTO(cleanup, rc = -ENODATA);
1364 obj = lmv_obj_create(exp, fid, md.mea);
1370 /* XXX LOV STACKING */
1371 obd_free_memmd(exp, (void *)&md.mea);
1376 ptlrpc_req_finished(req);
1380 int lmv_create(struct obd_export *exp, struct md_op_data *op_data,
1381 const void *data, int datalen, int mode, __u32 uid,
1382 __u32 gid, __u32 cap_effective, __u64 rdev,
1383 struct ptlrpc_request **request)
1385 struct obd_device *obd = exp->exp_obd;
1386 struct lmv_obd *lmv = &obd->u.lmv;
1387 struct obd_export *tgt_exp;
1388 struct lmv_obj *obj;
1392 rc = lmv_check_connect(obd);
1396 if (!lmv->desc.ld_active_tgt_count)
1401 obj = lmv_obj_grab(obd, &op_data->op_fid1);
1405 mea_idx = raw_name2idx(obj->lo_hashtype, obj->lo_objcount,
1406 op_data->op_name, op_data->op_namelen);
1407 op_data->op_fid1 = obj->lo_inodes[mea_idx].li_fid;
1408 op_data->op_bias &= ~MDS_CHECK_SPLIT;
1409 op_data->op_mds = obj->lo_inodes[mea_idx].li_mds;
1410 tgt_exp = lmv_get_export(lmv, op_data->op_mds);
1413 struct lmv_tgt_desc *tgt;
1415 tgt = lmv_find_target(lmv, &op_data->op_fid1);
1416 op_data->op_bias |= MDS_CHECK_SPLIT;
1417 op_data->op_mds = tgt->ltd_idx;
1418 tgt_exp = tgt->ltd_exp;
1421 if (IS_ERR(tgt_exp))
1422 RETURN(PTR_ERR(tgt_exp));
1424 rc = lmv_fid_alloc(exp, &op_data->op_fid2, op_data);
1425 if (rc == -ERESTART)
1430 CDEBUG(D_OTHER, "CREATE '%*s' on "DFID"\n", op_data->op_namelen,
1431 op_data->op_name, PFID(&op_data->op_fid1));
1433 op_data->op_flags |= MF_MDC_CANCEL_FID1;
1434 rc = md_create(tgt_exp, op_data, data, datalen, mode, uid, gid,
1435 cap_effective, rdev, request);
1437 if (*request == NULL)
1439 CDEBUG(D_OTHER, "created - "DFID"\n", PFID(&op_data->op_fid1));
1440 } else if (rc == -ERESTART) {
1441 LASSERT(*request != NULL);
1442 DEBUG_REQ(D_WARNING|D_RPCTRACE, *request,
1443 "Got -ERESTART during create!\n");
1444 ptlrpc_req_finished(*request);
1448 * Directory got split. Time to update local object and repeat
1449 * the request with proper MDS.
1451 rc = lmv_handle_split(exp, &op_data->op_fid1);
1453 rc = lmv_alloc_slave_fids(obd, &op_data->op_fid1,
1454 op_data, &op_data->op_fid2);
1463 static int lmv_done_writing(struct obd_export *exp,
1464 struct md_op_data *op_data,
1465 struct md_open_data *mod)
1467 struct obd_device *obd = exp->exp_obd;
1468 struct lmv_obd *lmv = &obd->u.lmv;
1469 struct obd_export *tgt_exp;
1473 rc = lmv_check_connect(obd);
1477 tgt_exp = lmv_find_export(lmv, &op_data->op_fid1);
1478 if (IS_ERR(tgt_exp))
1479 RETURN(PTR_ERR(tgt_exp));
1481 rc = md_done_writing(tgt_exp, op_data, mod);
1486 lmv_enqueue_slaves(struct obd_export *exp, struct ldlm_enqueue_info *einfo,
1487 struct lookup_intent *it, struct md_op_data *op_data,
1488 struct lustre_handle *lockh, void *lmm, int lmmsize)
1490 struct obd_device *obd = exp->exp_obd;
1491 struct lmv_obd *lmv = &obd->u.lmv;
1492 struct lmv_stripe_md *mea = op_data->op_mea1;
1493 struct md_op_data *op_data2;
1494 struct obd_export *tgt_exp;
1498 OBD_ALLOC_PTR(op_data2);
1499 if (op_data2 == NULL)
1502 LASSERT(mea != NULL);
1503 for (i = 0; i < mea->mea_count; i++) {
1504 memset(op_data2, 0, sizeof(*op_data2));
1505 op_data2->op_fid1 = mea->mea_ids[i];
1506 op_data2->op_bias = 0;
1508 tgt_exp = lmv_find_export(lmv, &op_data2->op_fid1);
1509 if (IS_ERR(tgt_exp))
1510 GOTO(cleanup, rc = PTR_ERR(tgt_exp));
1512 if (tgt_exp == NULL)
1515 rc = md_enqueue(tgt_exp, einfo, it, op_data2,
1516 lockh + i, lmm, lmmsize, 0);
1518 CDEBUG(D_OTHER, "take lock on slave "DFID" -> %d/%d\n",
1519 PFID(&mea->mea_ids[i]), rc, it->d.lustre.it_status);
1524 if (it->d.lustre.it_data) {
1525 struct ptlrpc_request *req;
1526 req = (struct ptlrpc_request *)it->d.lustre.it_data;
1527 ptlrpc_req_finished(req);
1530 if (it->d.lustre.it_status)
1531 GOTO(cleanup, rc = it->d.lustre.it_status);
1536 OBD_FREE_PTR(op_data2);
1539 /* drop all taken locks */
1541 if (lockh[i].cookie)
1542 ldlm_lock_decref(lockh + i, einfo->ei_mode);
1543 lockh[i].cookie = 0;
1550 lmv_enqueue_remote(struct obd_export *exp, struct ldlm_enqueue_info *einfo,
1551 struct lookup_intent *it, struct md_op_data *op_data,
1552 struct lustre_handle *lockh, void *lmm, int lmmsize,
1553 int extra_lock_flags)
1555 struct ptlrpc_request *req = it->d.lustre.it_data;
1556 struct obd_device *obd = exp->exp_obd;
1557 struct lmv_obd *lmv = &obd->u.lmv;
1558 struct lustre_handle plock;
1559 struct obd_export *tgt_exp;
1560 struct md_op_data *rdata;
1561 struct lu_fid fid_copy;
1562 struct mdt_body *body;
1566 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
1567 LASSERT(body != NULL);
1569 if (!(body->valid & OBD_MD_MDS))
1572 CDEBUG(D_OTHER, "ENQUEUE '%s' on "DFID" -> "DFID"\n",
1573 LL_IT2STR(it), PFID(&op_data->op_fid1), PFID(&body->fid1));
1575 /* We got LOOKUP lock, but we really need attrs */
1576 pmode = it->d.lustre.it_lock_mode;
1577 LASSERT(pmode != 0);
1578 memcpy(&plock, lockh, sizeof(plock));
1579 it->d.lustre.it_lock_mode = 0;
1580 it->d.lustre.it_data = NULL;
1581 fid_copy = body->fid1;
1583 it->d.lustre.it_disposition &= ~DISP_ENQ_COMPLETE;
1584 ptlrpc_req_finished(req);
1586 tgt_exp = lmv_find_export(lmv, &fid_copy);
1587 if (IS_ERR(tgt_exp))
1588 GOTO(out, rc = PTR_ERR(tgt_exp));
1590 OBD_ALLOC_PTR(rdata);
1592 GOTO(out, rc = -ENOMEM);
1594 rdata->op_fid1 = fid_copy;
1595 rdata->op_bias = MDS_CROSS_REF;
1597 rc = md_enqueue(tgt_exp, einfo, it, rdata, lockh,
1598 lmm, lmmsize, extra_lock_flags);
1599 OBD_FREE_PTR(rdata);
1602 ldlm_lock_decref(&plock, pmode);
1607 lmv_enqueue(struct obd_export *exp, struct ldlm_enqueue_info *einfo,
1608 struct lookup_intent *it, struct md_op_data *op_data,
1609 struct lustre_handle *lockh, void *lmm, int lmmsize,
1610 int extra_lock_flags)
1612 struct obd_device *obd = exp->exp_obd;
1613 struct lmv_obd *lmv = &obd->u.lmv;
1614 struct obd_export *tgt_exp = NULL;
1615 struct lmv_obj *obj;
1619 rc = lmv_check_connect(obd);
1623 if (op_data->op_mea1 && it->it_op == IT_UNLINK) {
1624 rc = lmv_enqueue_slaves(exp, einfo, it, op_data,
1625 lockh, lmm, lmmsize);
1629 if (op_data->op_namelen) {
1630 obj = lmv_obj_grab(obd, &op_data->op_fid1);
1634 /* directory is split. look for right mds for this
1636 mea_idx = raw_name2idx(obj->lo_hashtype,
1638 (char *)op_data->op_name,
1639 op_data->op_namelen);
1640 op_data->op_fid1 = obj->lo_inodes[mea_idx].li_fid;
1641 tgt_exp = lmv_get_export(lmv, obj->lo_inodes[mea_idx].li_mds);
1646 if (tgt_exp == NULL)
1647 tgt_exp = lmv_find_export(lmv, &op_data->op_fid1);
1648 if (IS_ERR(tgt_exp))
1649 RETURN(PTR_ERR(tgt_exp));
1651 CDEBUG(D_OTHER, "ENQUEUE '%s' on "DFID"\n", LL_IT2STR(it),
1652 PFID(&op_data->op_fid1));
1654 rc = md_enqueue(tgt_exp, einfo, it, op_data, lockh,
1655 lmm, lmmsize, extra_lock_flags);
1657 if (rc == 0 && it->it_op == IT_OPEN)
1658 rc = lmv_enqueue_remote(exp, einfo, it, op_data, lockh,
1659 lmm, lmmsize, extra_lock_flags);
1664 lmv_getattr_name(struct obd_export *exp, const struct lu_fid *fid,
1665 struct obd_capa *oc, const char *filename, int namelen,
1666 obd_valid valid, int ea_size, __u32 suppgid,
1667 struct ptlrpc_request **request)
1669 struct obd_device *obd = exp->exp_obd;
1670 struct lmv_obd *lmv = &obd->u.lmv;
1671 struct lu_fid rid = *fid;
1672 struct obd_export *tgt_exp;
1673 struct mdt_body *body;
1674 struct lmv_obj *obj;
1678 rc = lmv_check_connect(obd);
1685 obj = lmv_obj_grab(obd, &rid);
1689 /* Directory is split. Look for right mds for this name */
1690 mea_idx = raw_name2idx(obj->lo_hashtype, obj->lo_objcount,
1691 filename, namelen - 1);
1692 rid = obj->lo_inodes[mea_idx].li_fid;
1693 tgt_exp = lmv_get_export(lmv, obj->lo_inodes[mea_idx].li_mds);
1695 valid &= ~OBD_MD_FLCKSPLIT;
1697 tgt_exp = lmv_find_export(lmv, &rid);
1698 valid |= OBD_MD_FLCKSPLIT;
1700 if (IS_ERR(tgt_exp))
1701 RETURN(PTR_ERR(tgt_exp));
1703 CDEBUG(D_OTHER, "getattr_name for %*s on "DFID" -> "DFID"\n",
1704 namelen, filename, PFID(fid), PFID(&rid));
1706 rc = md_getattr_name(tgt_exp, &rid, oc, filename, namelen, valid,
1707 ea_size, suppgid, request);
1709 body = req_capsule_server_get(&(*request)->rq_pill,
1711 LASSERT(body != NULL);
1713 if (body->valid & OBD_MD_MDS) {
1714 struct ptlrpc_request *req = NULL;
1717 CDEBUG(D_OTHER, "request attrs for "DFID"\n",
1720 tgt_exp = lmv_find_export(lmv, &rid);
1721 if (IS_ERR(tgt_exp)) {
1722 ptlrpc_req_finished(*request);
1723 RETURN(PTR_ERR(tgt_exp));
1726 rc = md_getattr_name(tgt_exp, &rid, NULL, NULL, 1,
1727 valid | OBD_MD_FLCROSSREF,
1728 ea_size, suppgid, &req);
1729 ptlrpc_req_finished(*request);
1732 } else if (rc == -ERESTART) {
1733 LASSERT(*request != NULL);
1734 DEBUG_REQ(D_WARNING|D_RPCTRACE, *request,
1735 "Got -ERESTART during getattr!\n");
1736 ptlrpc_req_finished(*request);
1740 * Directory got split. Time to update local object and repeat
1741 * the request with proper MDS.
1743 rc = lmv_handle_split(exp, &rid);
1750 #define md_op_data_fid(op_data, fl) \
1751 (fl == MF_MDC_CANCEL_FID1 ? &op_data->op_fid1 : \
1752 fl == MF_MDC_CANCEL_FID2 ? &op_data->op_fid2 : \
1753 fl == MF_MDC_CANCEL_FID3 ? &op_data->op_fid3 : \
1754 fl == MF_MDC_CANCEL_FID4 ? &op_data->op_fid4 : \
1757 /* @tgt_exp is the export the metadata request is sent.
1758 * @fid_exp is the export the cancel should be sent for the current fid.
1759 * if @fid_exp is NULL, the export is found for the current fid.
1760 * @op_data keeps the current fid, which is pointed through @flag.
1761 * @mode, @bits -- lock match parameters. */
1762 static int lmv_early_cancel(struct lmv_obd *lmv, struct obd_export *tgt_exp,
1763 struct obd_export *fid_exp,
1764 struct md_op_data *op_data,
1765 ldlm_mode_t mode, int bits, int flag)
1767 struct lu_fid *fid = md_op_data_fid(op_data, flag);
1768 ldlm_policy_data_t policy = {{0}};
1772 if (!fid_is_sane(fid))
1775 if (fid_exp == NULL)
1776 fid_exp = lmv_find_export(lmv, fid);
1778 if (tgt_exp == fid_exp) {
1779 /* The export is the same as on the target server, cancel
1780 * will be sent along with the main metadata operation. */
1781 op_data->op_flags |= flag;
1785 policy.l_inodebits.bits = bits;
1786 rc = md_cancel_unused(fid_exp, fid, &policy, mode, LDLM_FL_ASYNC, NULL);
1790 #ifdef EARLY_CANCEL_FOR_STRIPED_DIR_IS_READY
1791 /* Check if the fid in @op_data pointed to by flag is of the same export(s)
1792 * as @tgt_exp. Early cancels will be sent later by mdc code, otherwise, call
1793 * md_cancel_unused for child export(s). */
1794 static int lmv_early_cancel_stripes(struct obd_export *exp,
1795 struct obd_export *tgt_exp,
1796 struct md_op_data *op_data,
1797 ldlm_mode_t mode, int bits, int flag)
1799 struct lu_fid *fid = md_op_data_fid(op_data, flag);
1800 struct obd_device *obd = exp->exp_obd;
1801 struct lmv_obd *lmv = &obd->u.lmv;
1802 struct obd_export *st_exp;
1803 struct lmv_obj *obj;
1807 if (!fid_is_sane(fid))
1810 obj = lmv_obj_grab(obd, fid);
1812 ldlm_policy_data_t policy = {{0}};
1813 struct lu_fid *st_fid;
1816 policy.l_inodebits.bits = bits;
1817 for (i = 0; i < obj->lo_objcount; i++) {
1818 st_exp = lmv_get_export(lmv, obj->lo_inodes[i].li_mds);
1819 st_fid = &obj->lo_inodes[i].li_fid;
1820 if (tgt_exp != st_exp) {
1821 rc = md_cancel_unused(st_exp, st_fid, &policy,
1822 mode, LDLM_FL_ASYNC,
1827 /* Some export matches to @tgt_exp, do cancel
1828 * for its fid in mdc */
1830 op_data->op_flags |= flag;
1835 rc = lmv_early_cancel(lmv, tgt_exp, NULL, op_data,
1843 * llite passes fid of an target inode in op_data->op_fid1 and id of directory in
1846 static int lmv_link(struct obd_export *exp, struct md_op_data *op_data,
1847 struct ptlrpc_request **request)
1849 struct obd_device *obd = exp->exp_obd;
1850 struct lmv_obd *lmv = &obd->u.lmv;
1851 struct obd_export *tgt_exp;
1852 struct lmv_obj *obj;
1857 rc = lmv_check_connect(obd);
1864 if (op_data->op_namelen != 0) {
1867 /* Usual link request */
1868 obj = lmv_obj_grab(obd, &op_data->op_fid2);
1870 mea_idx = raw_name2idx(obj->lo_hashtype,
1873 op_data->op_namelen);
1874 op_data->op_fid2 = obj->lo_inodes[mea_idx].li_fid;
1875 mds = obj->lo_inodes[mea_idx].li_mds;
1878 rc = lmv_fld_lookup(lmv, &op_data->op_fid2, &mds);
1883 CDEBUG(D_OTHER,"link "DFID":%*s to "DFID"\n",
1884 PFID(&op_data->op_fid2), op_data->op_namelen,
1885 op_data->op_name, PFID(&op_data->op_fid1));
1887 rc = lmv_fld_lookup(lmv, &op_data->op_fid1, &mds);
1891 /* request from MDS to acquire i_links for inode by fid1 */
1892 CDEBUG(D_OTHER, "inc i_nlinks for "DFID"\n",
1893 PFID(&op_data->op_fid1));
1896 CDEBUG(D_OTHER, "forward to MDS #"LPU64" ("DFID")\n",
1897 mds, PFID(&op_data->op_fid1));
1899 op_data->op_fsuid = current->fsuid;
1900 op_data->op_fsgid = current->fsgid;
1901 op_data->op_cap = current->cap_effective;
1903 tgt_exp = lmv->tgts[mds].ltd_exp;
1904 if (op_data->op_namelen) {
1905 op_data->op_flags |= MF_MDC_CANCEL_FID2;
1906 /* Cancel UPDATE lock on child (fid1). */
1907 rc = lmv_early_cancel(lmv, tgt_exp, NULL, op_data, LCK_EX,
1908 MDS_INODELOCK_UPDATE, MF_MDC_CANCEL_FID1);
1911 rc = md_link(tgt_exp, op_data, request);
1912 if (rc == -ERESTART) {
1913 LASSERT(*request != NULL);
1914 DEBUG_REQ(D_WARNING|D_RPCTRACE, *request,
1915 "Got -ERESTART during link!\n");
1916 ptlrpc_req_finished(*request);
1920 * Directory got split. Time to update local object and repeat
1921 * the request with proper MDS.
1923 rc = lmv_handle_split(exp, &op_data->op_fid2);
1931 static int lmv_rename(struct obd_export *exp, struct md_op_data *op_data,
1932 const char *old, int oldlen, const char *new, int newlen,
1933 struct ptlrpc_request **request)
1935 struct obd_export *tgt_exp = NULL, *src_exp;
1936 struct obd_device *obd = exp->exp_obd;
1937 struct lmv_obd *lmv = &obd->u.lmv;
1938 int rc, mea_idx, loop = 0;
1939 struct lmv_obj *obj;
1943 CDEBUG(D_OTHER, "rename %*s in "DFID" to %*s in "DFID"\n",
1944 oldlen, old, PFID(&op_data->op_fid1),
1945 newlen, new, PFID(&op_data->op_fid2));
1947 rc = lmv_check_connect(obd);
1953 * MDS with old dir entry is asking another MDS to create name
1957 "create %*s(%d/%d) in "DFID" pointing "
1958 "to "DFID"\n", newlen, new, oldlen, newlen,
1959 PFID(&op_data->op_fid2), PFID(&op_data->op_fid1));
1961 rc = lmv_fld_lookup(lmv, &op_data->op_fid2, &mds1);
1966 * Target directory can be split, sowe should forward request to
1969 obj = lmv_obj_grab(obd, &op_data->op_fid2);
1971 mea_idx = raw_name2idx(obj->lo_hashtype,
1973 (char *)new, newlen);
1974 op_data->op_fid2 = obj->lo_inodes[mea_idx].li_fid;
1975 CDEBUG(D_OTHER, "Parent obj "DFID"\n",
1976 PFID(&op_data->op_fid2));
1985 obj = lmv_obj_grab(obd, &op_data->op_fid1);
1988 * directory is already split, so we have to forward request to
1991 mea_idx = raw_name2idx(obj->lo_hashtype, obj->lo_objcount,
1992 (char *)old, oldlen);
1993 op_data->op_fid1 = obj->lo_inodes[mea_idx].li_fid;
1994 mds1 = obj->lo_inodes[mea_idx].li_mds;
1995 CDEBUG(D_OTHER, "Parent obj "DFID"\n", PFID(&op_data->op_fid1));
1998 rc = lmv_fld_lookup(lmv, &op_data->op_fid1, &mds1);
2003 obj = lmv_obj_grab(obd, &op_data->op_fid2);
2006 * Directory is already split, so we have to forward request to
2009 mea_idx = raw_name2idx(obj->lo_hashtype, obj->lo_objcount,
2010 (char *)new, newlen);
2012 mds2 = obj->lo_inodes[mea_idx].li_mds;
2013 op_data->op_fid2 = obj->lo_inodes[mea_idx].li_fid;
2014 CDEBUG(D_OTHER, "Parent obj "DFID"\n", PFID(&op_data->op_fid2));
2017 rc = lmv_fld_lookup(lmv, &op_data->op_fid2, &mds2);
2023 op_data->op_fsuid = current->fsuid;
2024 op_data->op_fsgid = current->fsgid;
2025 op_data->op_cap = current->cap_effective;
2027 src_exp = lmv_get_export(lmv, mds1);
2028 tgt_exp = lmv_get_export(lmv, mds2);
2030 /* LOOKUP lock on src child (fid3) should also be cancelled for
2031 * src_exp in mdc_rename. */
2032 op_data->op_flags |= MF_MDC_CANCEL_FID1 | MF_MDC_CANCEL_FID3;
2034 /* Cancel UPDATE locks on tgt parent (fid2), tgt_exp is its
2036 rc = lmv_early_cancel(lmv, src_exp, tgt_exp, op_data, LCK_EX,
2037 MDS_INODELOCK_UPDATE, MF_MDC_CANCEL_FID2);
2039 /* Cancel LOOKUP locks on tgt child (fid4) for parent tgt_exp.*/
2041 rc = lmv_early_cancel(lmv, src_exp, tgt_exp, op_data,
2042 LCK_EX, MDS_INODELOCK_LOOKUP,
2043 MF_MDC_CANCEL_FID4);
2045 /* XXX: the case when child is a striped dir is not supported.
2046 * Only the master stripe has all locks cancelled early. */
2047 /* Cancel all the locks on tgt child (fid4). */
2049 rc = lmv_early_cancel(lmv, src_exp, NULL, op_data,
2050 LCK_EX, MDS_INODELOCK_FULL,
2051 MF_MDC_CANCEL_FID4);
2055 rc = md_rename(src_exp, op_data, old, oldlen,
2056 new, newlen, request);
2057 if (rc == -ERESTART) {
2058 LASSERT(*request != NULL);
2059 DEBUG_REQ(D_WARNING|D_RPCTRACE, *request,
2060 "Got -ERESTART during rename!\n");
2061 ptlrpc_req_finished(*request);
2065 * Directory got split. Time to update local object and repeat
2066 * the request with proper MDS.
2068 rc = lmv_handle_split(exp, &op_data->op_fid1);
2075 static int lmv_setattr(struct obd_export *exp, struct md_op_data *op_data,
2076 void *ea, int ealen, void *ea2, int ea2len,
2077 struct ptlrpc_request **request,
2078 struct md_open_data **mod)
2080 struct obd_device *obd = exp->exp_obd;
2081 struct lmv_obd *lmv = &obd->u.lmv;
2082 struct ptlrpc_request *req;
2083 struct obd_export *tgt_exp;
2084 struct lmv_obj *obj;
2088 rc = lmv_check_connect(obd);
2092 obj = lmv_obj_grab(obd, &op_data->op_fid1);
2094 CDEBUG(D_OTHER, "SETATTR for "DFID", valid 0x%x%s\n",
2095 PFID(&op_data->op_fid1), op_data->op_attr.ia_valid,
2096 obj ? ", split" : "");
2098 op_data->op_flags |= MF_MDC_CANCEL_FID1;
2100 for (i = 0; i < obj->lo_objcount; i++) {
2101 op_data->op_fid1 = obj->lo_inodes[i].li_fid;
2103 tgt_exp = lmv_get_export(lmv, obj->lo_inodes[i].li_mds);
2104 if (IS_ERR(tgt_exp)) {
2105 rc = PTR_ERR(tgt_exp);
2109 rc = md_setattr(tgt_exp, op_data, ea, ealen,
2110 ea2, ea2len, &req, mod);
2112 if (lu_fid_eq(&obj->lo_fid, &obj->lo_inodes[i].li_fid)) {
2114 * this is master object and this request should
2115 * be returned back to llite.
2119 ptlrpc_req_finished(req);
2127 tgt_exp = lmv_find_export(lmv, &op_data->op_fid1);
2128 if (IS_ERR(tgt_exp))
2129 RETURN(PTR_ERR(tgt_exp));
2131 rc = md_setattr(tgt_exp, op_data, ea, ealen, ea2,
2132 ea2len, request, mod);
2137 static int lmv_sync(struct obd_export *exp, const struct lu_fid *fid,
2138 struct obd_capa *oc, struct ptlrpc_request **request)
2140 struct obd_device *obd = exp->exp_obd;
2141 struct lmv_obd *lmv = &obd->u.lmv;
2142 struct obd_export *tgt_exp;
2146 rc = lmv_check_connect(obd);
2150 tgt_exp = lmv_find_export(lmv, fid);
2151 if (IS_ERR(tgt_exp))
2152 RETURN(PTR_ERR(tgt_exp));
2154 rc = md_sync(tgt_exp, fid, oc, request);
2158 /* main purpose of LMV blocking ast is to remove split directory LMV
2159 * presentation object (struct lmv_obj) attached to the lock being revoked. */
2160 int lmv_blocking_ast(struct ldlm_lock *lock,
2161 struct ldlm_lock_desc *desc,
2162 void *data, int flag)
2164 struct lustre_handle lockh;
2165 struct lmv_obj *obj;
2170 case LDLM_CB_BLOCKING:
2171 ldlm_lock2handle(lock, &lockh);
2172 rc = ldlm_cli_cancel(&lockh);
2174 CDEBUG(D_INODE, "ldlm_cli_cancel: %d\n", rc);
2178 case LDLM_CB_CANCELING:
2179 /* time to drop cached attrs for dirobj */
2180 obj = lock->l_ast_data;
2182 CDEBUG(D_OTHER, "cancel %s on "LPU64"/"LPU64
2183 ", master "DFID"\n",
2184 lock->l_resource->lr_name.name[3] == 1 ?
2185 "LOOKUP" : "UPDATE",
2186 lock->l_resource->lr_name.name[0],
2187 lock->l_resource->lr_name.name[1],
2188 PFID(&obj->lo_fid));
2198 static void lmv_hash_adjust(__u64 *hash, __u64 hash_adj)
2202 val = le64_to_cpu(*hash);
2204 val += MAX_HASH_SIZE;
2205 if (val != DIR_END_OFF)
2206 *hash = cpu_to_le64(val - hash_adj);
2209 static __u32 lmv_node_rank(struct obd_export *exp, const struct lu_fid *fid)
2212 struct obd_import *imp;
2215 * XXX Hack: to get nid we assume that underlying obd device is mdc.
2217 imp = class_exp2cliimp(exp);
2218 id = imp->imp_connection->c_self + fid_flatten(fid);
2220 CDEBUG(D_INFO, "node rank: "LPX64" "DFID" "LPX64" "LPX64"\n",
2221 imp->imp_connection->c_self, PFID(fid), id, id ^ (id >> 32));
2223 return id ^ (id >> 32);
2226 static int lmv_readpage(struct obd_export *exp, const struct lu_fid *fid,
2227 struct obd_capa *oc, __u64 offset64, struct page *page,
2228 struct ptlrpc_request **request)
2230 struct obd_device *obd = exp->exp_obd;
2231 struct lmv_obd *lmv = &obd->u.lmv;
2232 struct obd_export *tgt_exp;
2233 struct lu_fid rid = *fid;
2234 struct lmv_obj *obj;
2248 rc = lmv_check_connect(obd);
2252 CDEBUG(D_INFO, "READPAGE at "LPX64" from "DFID"\n", offset, PFID(&rid));
2254 obj = lmv_obj_grab(obd, fid);
2258 * This case handle directory lookup in clustered metadata case (i.e.
2259 * split directory is located on multiple md servers.)
2260 * each server keeps directory entries for certain range of hashes.
2261 * E.g. we have N server and suppose hash range is 0 to MAX_HASH.
2262 * first server will keep records with hashes [ 0 ... MAX_HASH / N - 1],
2263 * second one with hashes [MAX_HASH / N ... 2 * MAX_HASH / N] and
2265 * readdir can simply start reading entries from 0 - N server in
2266 * order but that will not scale well as all client will request dir in
2267 * to server in same order.
2268 * Following algorithm does optimization:
2269 * Instead of doing readdir in 1, 2, ...., N order, client with a
2270 * rank R does readdir in R, R + 1, ..., N, 1, ... R - 1 order.
2271 * (every client has rank R)
2272 * But ll_readdir() expect offset range [0 to MAX_HASH/N) but
2273 * since client ask dir from MDS{R} client has pages with offsets
2274 * [R*MAX_HASH/N ... (R + 1)*MAX_HASH/N] there for we do hash_adj
2275 * on hash values that we get.
2278 struct lmv_inode *loi;
2282 nr = obj->lo_objcount;
2284 seg_size = MAX_HASH_SIZE;
2285 do_div(seg_size, nr);
2286 loi = obj->lo_inodes;
2287 rank = lmv_node_rank(lmv_get_export(lmv, loi[0].li_mds),
2290 do_div(tgt_tmp, seg_size);
2291 tgt0 = do_div(tgt_tmp, nr);
2292 tgt = (tgt0 + rank) % nr;
2298 * Last segment has unusual length due to division
2301 hash_adj = MAX_HASH_SIZE - seg_size * nr;
2305 hash_adj += rank * seg_size;
2307 CDEBUG(D_INFO, "hash_adj: %x "LPX64" "LPX64"/%x -> "LPX64"/%x\n",
2308 rank, hash_adj, offset, tgt0, offset + hash_adj, tgt);
2310 offset = (offset + hash_adj) & MAX_HASH_SIZE;
2311 rid = obj->lo_inodes[tgt].li_fid;
2312 tgt_exp = lmv_get_export(lmv, loi[tgt].li_mds);
2314 CDEBUG(D_INFO, "forward to "DFID" with offset %lu i %d\n",
2315 PFID(&rid), (unsigned long)offset, tgt);
2317 tgt_exp = lmv_find_export(lmv, &rid);
2319 if (IS_ERR(tgt_exp))
2320 GOTO(cleanup, rc = PTR_ERR(tgt_exp));
2322 rc = md_readpage(tgt_exp, &rid, oc, offset, page, request);
2326 struct lu_dirpage *dp;
2327 struct lu_dirent *ent;
2329 dp = cfs_kmap(page);
2331 lmv_hash_adjust(&dp->ldp_hash_start, hash_adj);
2332 lmv_hash_adjust(&dp->ldp_hash_end, hash_adj);
2333 LASSERT(le64_to_cpu(dp->ldp_hash_start) <= offset64);
2335 for (ent = lu_dirent_start(dp); ent != NULL;
2336 ent = lu_dirent_next(ent))
2337 lmv_hash_adjust(&ent->lde_hash, hash_adj);
2339 if (tgt0 != nr - 1) {
2342 end = le64_to_cpu(dp->ldp_hash_end);
2343 if (end == DIR_END_OFF) {
2344 dp->ldp_hash_end = cpu_to_le32(seg_size *
2347 ""DFID" reset end "LPX64" tgt %d\n",
2349 le64_to_cpu(dp->ldp_hash_end), tgt);
2355 * Here we could remove "." and ".." from all pages which at not from
2356 * master. But MDS has only "." and ".." for master dir.
2361 lmv_obj_unlock(obj);
2367 static int lmv_unlink_slaves(struct obd_export *exp,
2368 struct md_op_data *op_data,
2369 struct ptlrpc_request **req)
2371 struct obd_device *obd = exp->exp_obd;
2372 struct lmv_obd *lmv = &obd->u.lmv;
2373 struct lmv_stripe_md *mea = op_data->op_mea1;
2374 struct md_op_data *op_data2;
2375 struct obd_export *tgt_exp;
2379 OBD_ALLOC_PTR(op_data2);
2380 if (op_data2 == NULL)
2383 op_data2->op_mode = S_IFDIR;
2384 op_data2->op_fsuid = current->fsuid;
2385 op_data2->op_fsgid = current->fsgid;
2386 op_data2->op_bias = 0;
2388 LASSERT(mea != NULL);
2389 for (i = 0; i < mea->mea_count; i++) {
2390 memset(op_data2, 0, sizeof(*op_data2));
2391 op_data2->op_fid1 = mea->mea_ids[i];
2392 tgt_exp = lmv_find_export(lmv, &op_data2->op_fid1);
2393 if (IS_ERR(tgt_exp))
2394 GOTO(out_free_op_data2, rc = PTR_ERR(tgt_exp));
2396 if (tgt_exp == NULL)
2399 rc = md_unlink(tgt_exp, op_data2, req);
2401 CDEBUG(D_OTHER, "unlink slave "DFID" -> %d\n",
2402 PFID(&mea->mea_ids[i]), rc);
2405 ptlrpc_req_finished(*req);
2409 GOTO(out_free_op_data2, rc);
2414 OBD_FREE_PTR(op_data2);
2418 static int lmv_unlink(struct obd_export *exp, struct md_op_data *op_data,
2419 struct ptlrpc_request **request)
2421 struct obd_device *obd = exp->exp_obd;
2422 struct lmv_obd *lmv = &obd->u.lmv;
2423 struct obd_export *tgt_exp = NULL;
2424 struct lmv_obj *obj;
2428 rc = lmv_check_connect(obd);
2432 if (op_data->op_namelen == 0 && op_data->op_mea1 != NULL) {
2433 /* mds asks to remove slave objects */
2434 rc = lmv_unlink_slaves(exp, op_data, request);
2441 if (op_data->op_namelen != 0) {
2444 obj = lmv_obj_grab(obd, &op_data->op_fid1);
2446 mea_idx = raw_name2idx(obj->lo_hashtype,
2449 op_data->op_namelen);
2450 op_data->op_bias &= ~MDS_CHECK_SPLIT;
2451 op_data->op_fid1 = obj->lo_inodes[mea_idx].li_fid;
2452 tgt_exp = lmv_get_export(lmv,
2453 obj->lo_inodes[mea_idx].li_mds);
2455 CDEBUG(D_OTHER, "unlink '%*s' in "DFID" -> %u\n",
2456 op_data->op_namelen, op_data->op_name,
2457 PFID(&op_data->op_fid1), mea_idx);
2460 CDEBUG(D_OTHER, "drop i_nlink on "DFID"\n",
2461 PFID(&op_data->op_fid1));
2463 if (tgt_exp == NULL) {
2464 tgt_exp = lmv_find_export(lmv, &op_data->op_fid1);
2465 if (IS_ERR(tgt_exp))
2466 RETURN(PTR_ERR(tgt_exp));
2467 op_data->op_bias |= MDS_CHECK_SPLIT;
2470 op_data->op_fsuid = current->fsuid;
2471 op_data->op_fsgid = current->fsgid;
2472 op_data->op_cap = current->cap_effective;
2474 /* If child's fid is given, cancel unused locks for it if it is from
2475 * another export than parent. */
2476 if (op_data->op_namelen) {
2477 /* LOOKUP lock for child (fid3) should also be cancelled on
2478 * parent tgt_exp in mdc_unlink(). */
2479 op_data->op_flags |= MF_MDC_CANCEL_FID1 | MF_MDC_CANCEL_FID3;
2481 /* XXX: the case when child is a striped dir is not supported.
2482 * Only the master stripe has all locks cancelled early. */
2483 /* Cancel FULL locks on child (fid3). */
2484 rc = lmv_early_cancel(lmv, tgt_exp, NULL, op_data, LCK_EX,
2485 MDS_INODELOCK_FULL, MF_MDC_CANCEL_FID3);
2488 rc = md_unlink(tgt_exp, op_data, request);
2489 if (rc == -ERESTART) {
2490 LASSERT(*request != NULL);
2491 DEBUG_REQ(D_WARNING|D_RPCTRACE, *request,
2492 "Got -ERESTART during unlink!\n");
2493 ptlrpc_req_finished(*request);
2497 * Directory got split. Time to update local object and repeat
2498 * the request with proper MDS.
2500 rc = lmv_handle_split(exp, &op_data->op_fid1);
2507 static int lmv_llog_init(struct obd_device *obd, struct obd_llog_group *olg,
2508 struct obd_device *tgt, int count,
2509 struct llog_catid *logid, struct obd_uuid *uuid)
2512 struct llog_ctxt *ctxt;
2516 LASSERT(group == OBD_LLOG_GROUP);
2517 rc = llog_setup(obd, &obd->obd_olg, LLOG_CONFIG_REPL_CTXT, tgt, 0, NULL,
2520 ctxt = llog_group_get_ctxt(&obd->obd_olg, LLOG_CONFIG_REPL_CTXT);
2521 llog_initiator_connect(ctxt, tgt);
2522 llog_ctxt_put(ctxt);
2530 static int lmv_llog_finish(struct obd_device *obd, int count)
2532 struct llog_ctxt *ctxt;
2536 ctxt = llog_get_context(obd, LLOG_CONFIG_REPL_CTXT);
2538 rc = llog_cleanup(ctxt);
2543 static int lmv_precleanup(struct obd_device *obd, enum obd_cleanup_stage stage)
2548 case OBD_CLEANUP_EARLY:
2549 /* XXX: here should be calling obd_precleanup() down to
2552 case OBD_CLEANUP_EXPORTS:
2553 rc = obd_llog_finish(obd, 0);
2555 CERROR("failed to cleanup llogging subsystems\n");
2563 static int lmv_get_info(struct obd_export *exp, __u32 keylen,
2564 void *key, __u32 *vallen, void *val, struct lov_stripe_md *lsm)
2566 struct obd_device *obd;
2567 struct lmv_obd *lmv;
2571 obd = class_exp2obd(exp);
2573 CDEBUG(D_IOCTL, "invalid client cookie "LPX64"\n",
2574 exp->exp_handle.h_cookie);
2579 if (keylen >= strlen("remote_flag") && !strcmp(key, "remote_flag")) {
2580 struct lmv_tgt_desc *tgts;
2583 rc = lmv_check_connect(obd);
2587 LASSERT(*vallen == sizeof(__u32));
2588 for (i = 0, tgts = lmv->tgts; i < lmv->desc.ld_tgt_count;
2591 /* all tgts should be connected when this get called. */
2592 if (!tgts || !tgts->ltd_exp) {
2593 CERROR("target not setup?\n");
2597 if (!obd_get_info(tgts->ltd_exp, keylen, key,
2602 } else if (KEY_IS(KEY_MAX_EASIZE) || KEY_IS(KEY_CONN_DATA)) {
2603 rc = lmv_check_connect(obd);
2607 /* forwarding this request to first MDS, it should know LOV
2609 rc = obd_get_info(lmv->tgts[0].ltd_exp, keylen, key,
2611 if (!rc && KEY_IS(KEY_CONN_DATA)) {
2612 exp->exp_connect_flags =
2613 ((struct obd_connect_data *)val)->ocd_connect_flags;
2618 CDEBUG(D_IOCTL, "invalid key\n");
2622 int lmv_set_info_async(struct obd_export *exp, obd_count keylen,
2623 void *key, obd_count vallen, void *val,
2624 struct ptlrpc_request_set *set)
2626 struct lmv_tgt_desc *tgt;
2627 struct obd_device *obd;
2628 struct lmv_obd *lmv;
2632 obd = class_exp2obd(exp);
2634 CDEBUG(D_IOCTL, "invalid client cookie "LPX64"\n",
2635 exp->exp_handle.h_cookie);
2640 if (KEY_IS(KEY_READ_ONLY) || KEY_IS(KEY_FLUSH_CTX) ||
2641 KEY_IS(KEY_INIT_RECOV_BACKUP)) {
2644 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
2645 tgt = &lmv->tgts[i];
2650 err = obd_set_info_async(tgt->ltd_exp,
2651 keylen, key, vallen, val, set);
2662 int lmv_packmd(struct obd_export *exp, struct lov_mds_md **lmmp,
2663 struct lov_stripe_md *lsm)
2665 struct obd_device *obd = class_exp2obd(exp);
2666 struct lmv_obd *lmv = &obd->u.lmv;
2667 struct lmv_stripe_md *meap, *lsmp;
2671 mea_size = lmv_get_easize(lmv);
2675 if (*lmmp && !lsm) {
2676 OBD_FREE(*lmmp, mea_size);
2681 if (*lmmp == NULL) {
2682 OBD_ALLOC(*lmmp, mea_size);
2690 lsmp = (struct lmv_stripe_md *)lsm;
2691 meap = (struct lmv_stripe_md *)*lmmp;
2693 if (lsmp->mea_magic != MEA_MAGIC_LAST_CHAR &&
2694 lsmp->mea_magic != MEA_MAGIC_ALL_CHARS)
2697 meap->mea_magic = cpu_to_le32(lsmp->mea_magic);
2698 meap->mea_count = cpu_to_le32(lsmp->mea_count);
2699 meap->mea_master = cpu_to_le32(lsmp->mea_master);
2701 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
2702 meap->mea_ids[i] = meap->mea_ids[i];
2703 fid_cpu_to_le(&meap->mea_ids[i], &meap->mea_ids[i]);
2709 int lmv_unpackmd(struct obd_export *exp, struct lov_stripe_md **lsmp,
2710 struct lov_mds_md *lmm, int lmm_size)
2712 struct obd_device *obd = class_exp2obd(exp);
2713 struct lmv_stripe_md **tmea = (struct lmv_stripe_md **)lsmp;
2714 struct lmv_stripe_md *mea = (struct lmv_stripe_md *)lmm;
2715 struct lmv_obd *lmv = &obd->u.lmv;
2720 mea_size = lmv_get_easize(lmv);
2724 if (*lsmp != NULL && lmm == NULL) {
2725 OBD_FREE(*tmea, mea_size);
2730 LASSERT(mea_size == lmm_size);
2732 OBD_ALLOC(*tmea, mea_size);
2739 if (mea->mea_magic == MEA_MAGIC_LAST_CHAR ||
2740 mea->mea_magic == MEA_MAGIC_ALL_CHARS ||
2741 mea->mea_magic == MEA_MAGIC_HASH_SEGMENT)
2743 magic = le32_to_cpu(mea->mea_magic);
2745 /* old mea is not handled here */
2749 (*tmea)->mea_magic = magic;
2750 (*tmea)->mea_count = le32_to_cpu(mea->mea_count);
2751 (*tmea)->mea_master = le32_to_cpu(mea->mea_master);
2753 for (i = 0; i < (*tmea)->mea_count; i++) {
2754 (*tmea)->mea_ids[i] = mea->mea_ids[i];
2755 fid_le_to_cpu(&(*tmea)->mea_ids[i], &(*tmea)->mea_ids[i]);
2760 static int lmv_cancel_unused(struct obd_export *exp,
2761 const struct lu_fid *fid,
2762 ldlm_policy_data_t *policy,
2763 ldlm_mode_t mode, int flags, void *opaque)
2765 struct obd_device *obd = exp->exp_obd;
2766 struct lmv_obd *lmv = &obd->u.lmv;
2770 LASSERT(fid != NULL);
2772 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
2773 if (!lmv->tgts[i].ltd_exp || !lmv->tgts[i].ltd_active)
2776 err = md_cancel_unused(lmv->tgts[i].ltd_exp, fid,
2777 policy, mode, flags, opaque);
2784 int lmv_set_lock_data(struct obd_export *exp, __u64 *lockh, void *data)
2786 struct obd_device *obd = exp->exp_obd;
2787 struct lmv_obd *lmv = &obd->u.lmv;
2790 RETURN(md_set_lock_data(lmv->tgts[0].ltd_exp, lockh, data));
2793 ldlm_mode_t lmv_lock_match(struct obd_export *exp, int flags,
2794 const struct lu_fid *fid, ldlm_type_t type,
2795 ldlm_policy_data_t *policy, ldlm_mode_t mode,
2796 struct lustre_handle *lockh)
2798 struct obd_device *obd = exp->exp_obd;
2799 struct lmv_obd *lmv = &obd->u.lmv;
2804 CDEBUG(D_OTHER, "lock match for "DFID"\n", PFID(fid));
2806 /* with CMD every object can have two locks in different namespaces:
2807 * lookup lock in space of mds storing direntry and update/open lock in
2808 * space of mds storing inode. Thus we check all targets, not only that
2809 * one fid was created in. */
2810 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
2811 rc = md_lock_match(lmv->tgts[i].ltd_exp, flags, fid,
2812 type, policy, mode, lockh);
2820 int lmv_get_lustre_md(struct obd_export *exp, struct ptlrpc_request *req,
2821 struct obd_export *dt_exp, struct obd_export *md_exp,
2822 struct lustre_md *md)
2824 struct obd_device *obd = exp->exp_obd;
2825 struct lmv_obd *lmv = &obd->u.lmv;
2829 rc = md_get_lustre_md(lmv->tgts[0].ltd_exp, req, dt_exp, md_exp, md);
2833 int lmv_free_lustre_md(struct obd_export *exp, struct lustre_md *md)
2835 struct obd_device *obd = exp->exp_obd;
2836 struct lmv_obd *lmv = &obd->u.lmv;
2840 /* XXX LOV STACKING */
2842 obd_free_memmd(exp, (void *)&md->mea);
2843 RETURN(md_free_lustre_md(lmv->tgts[0].ltd_exp, md));
2846 int lmv_set_open_replay_data(struct obd_export *exp,
2847 struct obd_client_handle *och,
2848 struct ptlrpc_request *open_req)
2850 struct obd_device *obd = exp->exp_obd;
2851 struct lmv_obd *lmv = &obd->u.lmv;
2852 struct obd_export *tgt_exp;
2856 tgt_exp = lmv_find_export(lmv, &och->och_fid);
2857 if (IS_ERR(tgt_exp))
2858 RETURN(PTR_ERR(tgt_exp));
2860 RETURN(md_set_open_replay_data(tgt_exp, och, open_req));
2863 int lmv_clear_open_replay_data(struct obd_export *exp,
2864 struct obd_client_handle *och)
2866 struct obd_device *obd = exp->exp_obd;
2867 struct lmv_obd *lmv = &obd->u.lmv;
2868 struct obd_export *tgt_exp;
2871 tgt_exp = lmv_find_export(lmv, &och->och_fid);
2872 if (IS_ERR(tgt_exp))
2873 RETURN(PTR_ERR(tgt_exp));
2875 RETURN(md_clear_open_replay_data(tgt_exp, och));
2878 static int lmv_get_remote_perm(struct obd_export *exp,
2879 const struct lu_fid *fid,
2880 struct obd_capa *oc, __u32 suppgid,
2881 struct ptlrpc_request **request)
2883 struct obd_device *obd = exp->exp_obd;
2884 struct lmv_obd *lmv = &obd->u.lmv;
2885 struct obd_export *tgt_exp;
2890 rc = lmv_check_connect(obd);
2894 tgt_exp = lmv_find_export(lmv, fid);
2895 if (IS_ERR(tgt_exp))
2896 RETURN(PTR_ERR(tgt_exp));
2898 rc = md_get_remote_perm(tgt_exp, fid, oc, suppgid, request);
2903 static int lmv_renew_capa(struct obd_export *exp, struct obd_capa *oc,
2906 struct obd_device *obd = exp->exp_obd;
2907 struct lmv_obd *lmv = &obd->u.lmv;
2908 struct obd_export *tgt_exp;
2912 rc = lmv_check_connect(obd);
2916 tgt_exp = lmv_find_export(lmv, &oc->c_capa.lc_fid);
2917 if (IS_ERR(tgt_exp))
2918 RETURN(PTR_ERR(tgt_exp));
2920 rc = md_renew_capa(tgt_exp, oc, cb);
2924 int lmv_intent_getattr_async(struct obd_export *exp,
2925 struct md_enqueue_info *minfo,
2926 struct ldlm_enqueue_info *einfo)
2928 struct obd_device *obd = exp->exp_obd;
2929 struct lmv_obd *lmv = &obd->u.lmv;
2930 struct obd_export *tgt_exp;
2934 rc = lmv_check_connect(obd);
2938 if (fid_is_zero(&minfo->mi_data.op_fid2))
2939 tgt_exp = lmv_find_export(lmv, &minfo->mi_data.op_fid1);
2941 tgt_exp = lmv_find_export(lmv, &minfo->mi_data.op_fid2);
2942 if (IS_ERR(tgt_exp))
2943 RETURN(PTR_ERR(tgt_exp));
2945 rc = md_intent_getattr_async(tgt_exp, minfo, einfo);
2949 int lmv_revalidate_lock(struct obd_export *exp,
2950 struct lookup_intent *it,
2953 struct obd_device *obd = exp->exp_obd;
2954 struct lmv_obd *lmv = &obd->u.lmv;
2955 struct obd_export *tgt_exp;
2959 rc = lmv_check_connect(obd);
2963 tgt_exp = lmv_find_export(lmv, fid);
2964 if (IS_ERR(tgt_exp))
2965 RETURN(PTR_ERR(tgt_exp));
2967 rc = md_revalidate_lock(tgt_exp, it, fid);
2972 struct obd_ops lmv_obd_ops = {
2973 .o_owner = THIS_MODULE,
2974 .o_setup = lmv_setup,
2975 .o_cleanup = lmv_cleanup,
2976 .o_precleanup = lmv_precleanup,
2977 .o_process_config = lmv_process_config,
2978 .o_connect = lmv_connect,
2979 .o_disconnect = lmv_disconnect,
2980 .o_statfs = lmv_statfs,
2981 .o_llog_init = lmv_llog_init,
2982 .o_llog_finish = lmv_llog_finish,
2983 .o_get_info = lmv_get_info,
2984 .o_set_info_async = lmv_set_info_async,
2985 .o_packmd = lmv_packmd,
2986 .o_unpackmd = lmv_unpackmd,
2987 .o_notify = lmv_notify,
2988 .o_iocontrol = lmv_iocontrol,
2989 .o_fid_delete = lmv_fid_delete
2992 struct md_ops lmv_md_ops = {
2993 .m_getstatus = lmv_getstatus,
2994 .m_change_cbdata = lmv_change_cbdata,
2995 .m_close = lmv_close,
2996 .m_create = lmv_create,
2997 .m_done_writing = lmv_done_writing,
2998 .m_enqueue = lmv_enqueue,
2999 .m_getattr = lmv_getattr,
3000 .m_getxattr = lmv_getxattr,
3001 .m_getattr_name = lmv_getattr_name,
3002 .m_intent_lock = lmv_intent_lock,
3004 .m_rename = lmv_rename,
3005 .m_setattr = lmv_setattr,
3006 .m_setxattr = lmv_setxattr,
3008 .m_readpage = lmv_readpage,
3009 .m_unlink = lmv_unlink,
3010 .m_init_ea_size = lmv_init_ea_size,
3011 .m_cancel_unused = lmv_cancel_unused,
3012 .m_set_lock_data = lmv_set_lock_data,
3013 .m_lock_match = lmv_lock_match,
3014 .m_get_lustre_md = lmv_get_lustre_md,
3015 .m_free_lustre_md = lmv_free_lustre_md,
3016 .m_set_open_replay_data = lmv_set_open_replay_data,
3017 .m_clear_open_replay_data = lmv_clear_open_replay_data,
3018 .m_renew_capa = lmv_renew_capa,
3019 .m_get_remote_perm = lmv_get_remote_perm,
3020 .m_intent_getattr_async = lmv_intent_getattr_async,
3021 .m_revalidate_lock = lmv_revalidate_lock
3024 int __init lmv_init(void)
3026 struct lprocfs_static_vars lvars;
3029 obj_cache = cfs_mem_cache_create("lmv_objects",
3030 sizeof(struct lmv_obj),
3033 CERROR("error allocating lmv objects cache\n");
3037 lprocfs_lmv_init_vars(&lvars);
3038 rc = class_register_type(&lmv_obd_ops, &lmv_md_ops,
3039 lvars.module_vars, LUSTRE_LMV_NAME, NULL);
3041 cfs_mem_cache_destroy(obj_cache);
3047 static void lmv_exit(void)
3051 class_unregister_type(LUSTRE_LMV_NAME);
3053 rc = cfs_mem_cache_destroy(obj_cache);
3055 "can't free lmv objects cache, %d object(s)"
3056 "still in use\n", atomic_read(&obj_cache_count));
3059 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
3060 MODULE_DESCRIPTION("Lustre Logical Metadata Volume OBD driver");
3061 MODULE_LICENSE("GPL");
3063 module_init(lmv_init);
3064 module_exit(lmv_exit);