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.
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 <lustre/lustre_idl.h>
56 #include <lustre_log.h>
57 #include <obd_support.h>
58 #include <lustre_lib.h>
59 #include <lustre_net.h>
60 #include <obd_class.h>
61 #include <lprocfs_status.h>
62 #include <lustre_lite.h>
63 #include <lustre_fid.h>
64 #include "lmv_internal.h"
67 cfs_mem_cache_t *lmv_object_cache;
68 atomic_t lmv_object_count = ATOMIC_INIT(0);
70 static void lmv_activate_target(struct lmv_obd *lmv,
71 struct lmv_tgt_desc *tgt,
74 if (tgt->ltd_active == activate)
77 tgt->ltd_active = activate;
78 lmv->desc.ld_active_tgt_count += (activate ? 1 : -1);
84 * -EINVAL : UUID can't be found in the LMV's target list
85 * -ENOTCONN: The UUID is found, but the target connection is bad (!)
86 * -EBADF : The UUID is found, but the OBD of the wrong type (!)
88 static int lmv_set_mdc_active(struct lmv_obd *lmv, struct obd_uuid *uuid,
91 struct lmv_tgt_desc *tgt;
92 struct obd_device *obd;
97 CDEBUG(D_INFO, "Searching in lmv %p for uuid %s (activate=%d)\n",
98 lmv, uuid->uuid, activate);
100 spin_lock(&lmv->lmv_lock);
101 for (i = 0, tgt = lmv->tgts; i < lmv->desc.ld_tgt_count; i++, tgt++) {
102 if (tgt->ltd_exp == NULL)
105 CDEBUG(D_INFO, "Target idx %d is %s conn "LPX64"\n",
106 i, tgt->ltd_uuid.uuid, tgt->ltd_exp->exp_handle.h_cookie);
108 if (obd_uuid_equals(uuid, &tgt->ltd_uuid))
112 if (i == lmv->desc.ld_tgt_count)
113 GOTO(out_lmv_lock, rc = -EINVAL);
115 obd = class_exp2obd(tgt->ltd_exp);
117 GOTO(out_lmv_lock, rc = -ENOTCONN);
119 CDEBUG(D_INFO, "Found OBD %s=%s device %d (%p) type %s at LMV idx %d\n",
120 obd->obd_name, obd->obd_uuid.uuid, obd->obd_minor, obd,
121 obd->obd_type->typ_name, i);
122 LASSERT(strcmp(obd->obd_type->typ_name, LUSTRE_MDC_NAME) == 0);
124 if (tgt->ltd_active == activate) {
125 CDEBUG(D_INFO, "OBD %p already %sactive!\n", obd,
126 activate ? "" : "in");
127 GOTO(out_lmv_lock, rc);
130 CDEBUG(D_INFO, "Marking OBD %p %sactive\n", obd,
131 activate ? "" : "in");
132 lmv_activate_target(lmv, tgt, activate);
136 spin_unlock(&lmv->lmv_lock);
140 static int lmv_set_mdc_data(struct lmv_obd *lmv, struct obd_uuid *uuid,
141 struct obd_connect_data *data)
143 struct lmv_tgt_desc *tgt;
147 LASSERT(data != NULL);
149 spin_lock(&lmv->lmv_lock);
150 for (i = 0, tgt = lmv->tgts; i < lmv->desc.ld_tgt_count; i++, tgt++) {
151 if (tgt->ltd_exp == NULL)
154 if (obd_uuid_equals(uuid, &tgt->ltd_uuid)) {
155 lmv->datas[tgt->ltd_idx] = *data;
159 spin_unlock(&lmv->lmv_lock);
163 struct obd_uuid *lmv_get_uuid(struct obd_export *exp) {
164 struct obd_device *obd = exp->exp_obd;
165 struct lmv_obd *lmv = &obd->u.lmv;
166 return obd_get_uuid(lmv->tgts[0].ltd_exp);
169 static int lmv_notify(struct obd_device *obd, struct obd_device *watched,
170 enum obd_notify_event ev, void *data)
172 struct obd_connect_data *conn_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 conn_data = &watched->u.cli.cl_import->imp_connect_data;
203 * Set connect data to desired target, update 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) {
223 * For disconnect event, flush fld cache for failout MDS case.
225 fld_client_flush(&lmv->lmv_fld);
229 * Pass the notification up the chain.
231 if (obd->obd_observer)
232 rc = obd_notify(obd->obd_observer, watched, ev, data);
238 * This is fake connect function. Its purpose is to initialize lmv and say
239 * caller that everything is okay. Real connection will be performed later.
241 static int lmv_connect(const struct lu_env *env,
242 struct lustre_handle *conn, struct obd_device *obd,
243 struct obd_uuid *cluuid, struct obd_connect_data *data,
247 struct proc_dir_entry *lmv_proc_dir;
249 struct lmv_obd *lmv = &obd->u.lmv;
250 struct obd_export *exp;
254 rc = class_connect(conn, obd, cluuid);
256 CERROR("class_connection() returned %d\n", rc);
260 exp = class_conn2export(conn);
263 * We don't want to actually do the underlying connections more than
264 * once, so keep track.
267 if (lmv->refcount > 1) {
268 class_export_put(exp);
274 lmv->cluuid = *cluuid;
277 lmv->conn_data = *data;
280 lmv_proc_dir = lprocfs_register("target_obds", obd->obd_proc_entry,
282 if (IS_ERR(lmv_proc_dir)) {
283 CERROR("could not register /proc/fs/lustre/%s/%s/target_obds.",
284 obd->obd_type->typ_name, obd->obd_name);
290 * All real clients should perform actual connection right away, because
291 * it is possible, that LMV will not have opportunity to connect targets
292 * and MDC stuff will be called directly, for instance while reading
293 * ../mdc/../kbytesfree procfs file, etc.
295 if (data->ocd_connect_flags & OBD_CONNECT_REAL)
296 rc = lmv_check_connect(obd);
301 lprocfs_remove(&lmv_proc_dir);
308 static void lmv_set_timeouts(struct obd_device *obd)
310 struct lmv_tgt_desc *tgts;
315 if (lmv->server_timeout == 0)
318 if (lmv->connected == 0)
321 for (i = 0, tgts = lmv->tgts; i < lmv->desc.ld_tgt_count; i++, tgts++) {
322 if (tgts->ltd_exp == NULL)
325 obd_set_info_async(tgts->ltd_exp, sizeof(KEY_INTERMDS),
326 KEY_INTERMDS, 0, NULL, NULL);
330 static int lmv_init_ea_size(struct obd_export *exp, int easize,
331 int def_easize, int cookiesize)
333 struct obd_device *obd = exp->exp_obd;
334 struct lmv_obd *lmv = &obd->u.lmv;
340 if (lmv->max_easize < easize) {
341 lmv->max_easize = easize;
344 if (lmv->max_def_easize < def_easize) {
345 lmv->max_def_easize = def_easize;
348 if (lmv->max_cookiesize < cookiesize) {
349 lmv->max_cookiesize = cookiesize;
355 if (lmv->connected == 0)
358 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
359 if (lmv->tgts[i].ltd_exp == NULL) {
360 CWARN("%s: NULL export for %d\n", obd->obd_name, i);
364 rc = md_init_ea_size(lmv->tgts[i].ltd_exp, easize, def_easize,
367 CERROR("obd_init_ea_size() failed on MDT target %d, "
368 "error %d.\n", i, rc);
375 #define MAX_STRING_SIZE 128
377 int lmv_connect_mdc(struct obd_device *obd, struct lmv_tgt_desc *tgt)
380 struct proc_dir_entry *lmv_proc_dir;
382 struct lmv_obd *lmv = &obd->u.lmv;
383 struct obd_uuid *cluuid = &lmv->cluuid;
384 struct obd_connect_data *mdc_data = NULL;
385 struct obd_uuid lmv_mdc_uuid = { "LMV_MDC_UUID" };
386 struct lustre_handle conn = {0, };
387 struct obd_device *mdc_obd;
388 struct obd_export *mdc_exp;
389 struct lu_fld_target target;
393 mdc_obd = class_find_client_obd(&tgt->ltd_uuid, LUSTRE_MDC_NAME,
396 CERROR("target %s not attached\n", tgt->ltd_uuid.uuid);
400 CDEBUG(D_CONFIG, "connect to %s(%s) - %s, %s FOR %s\n",
401 mdc_obd->obd_name, mdc_obd->obd_uuid.uuid,
402 tgt->ltd_uuid.uuid, obd->obd_uuid.uuid,
405 if (!mdc_obd->obd_set_up) {
406 CERROR("target %s is not set up\n", tgt->ltd_uuid.uuid);
410 rc = obd_connect(NULL, &conn, mdc_obd, &lmv_mdc_uuid,
411 &lmv->conn_data, NULL);
413 CERROR("target %s connect error %d\n", tgt->ltd_uuid.uuid, rc);
417 mdc_exp = class_conn2export(&conn);
420 * Init fid sequence client for this mdc and add new fld target.
422 rc = obd_fid_init(mdc_exp);
426 target.ft_srv = NULL;
427 target.ft_exp = mdc_exp;
428 target.ft_idx = tgt->ltd_idx;
430 fld_client_add_target(&lmv->lmv_fld, &target);
432 mdc_data = &class_exp2cliimp(mdc_exp)->imp_connect_data;
434 rc = obd_register_observer(mdc_obd, obd);
436 obd_disconnect(mdc_exp);
437 CERROR("target %s register_observer error %d\n",
438 tgt->ltd_uuid.uuid, rc);
442 if (obd->obd_observer) {
444 * Tell the observer about the new target.
446 rc = obd_notify(obd->obd_observer, mdc_exp->exp_obd,
447 OBD_NOTIFY_ACTIVE, (void *)(tgt - lmv->tgts));
449 obd_disconnect(mdc_exp);
455 tgt->ltd_exp = mdc_exp;
456 lmv->desc.ld_active_tgt_count++;
459 * Copy connect data, it may be used later.
461 lmv->datas[tgt->ltd_idx] = *mdc_data;
463 md_init_ea_size(tgt->ltd_exp, lmv->max_easize,
464 lmv->max_def_easize, lmv->max_cookiesize);
466 CDEBUG(D_CONFIG, "Connected to %s(%s) successfully (%d)\n",
467 mdc_obd->obd_name, mdc_obd->obd_uuid.uuid,
468 atomic_read(&obd->obd_refcount));
471 lmv_proc_dir = lprocfs_srch(obd->obd_proc_entry, "target_obds");
473 struct proc_dir_entry *mdc_symlink;
474 char name[MAX_STRING_SIZE + 1];
476 LASSERT(mdc_obd->obd_type != NULL);
477 LASSERT(mdc_obd->obd_type->typ_name != NULL);
478 name[MAX_STRING_SIZE] = '\0';
479 snprintf(name, MAX_STRING_SIZE, "../../../%s/%s",
480 mdc_obd->obd_type->typ_name,
482 mdc_symlink = proc_symlink(mdc_obd->obd_name,
484 if (mdc_symlink == NULL) {
485 CERROR("Could not register LMV target "
486 "/proc/fs/lustre/%s/%s/target_obds/%s.",
487 obd->obd_type->typ_name, obd->obd_name,
489 lprocfs_remove(&lmv_proc_dir);
497 int lmv_add_target(struct obd_device *obd, struct obd_uuid *tgt_uuid)
499 struct lmv_obd *lmv = &obd->u.lmv;
500 struct lmv_tgt_desc *tgt;
504 CDEBUG(D_CONFIG, "Target uuid: %s.\n", tgt_uuid->uuid);
508 if (lmv->desc.ld_active_tgt_count >= LMV_MAX_TGT_COUNT) {
509 lmv_init_unlock(lmv);
510 CERROR("Can't add %s, LMV module compiled for %d MDCs. "
511 "That many MDCs already configured.\n",
512 tgt_uuid->uuid, LMV_MAX_TGT_COUNT);
515 if (lmv->desc.ld_tgt_count == 0) {
516 struct obd_device *mdc_obd;
518 mdc_obd = class_find_client_obd(tgt_uuid, LUSTRE_MDC_NAME,
521 lmv_init_unlock(lmv);
522 CERROR("Target %s not attached\n", tgt_uuid->uuid);
526 rc = obd_llog_init(obd, &obd->obd_olg, mdc_obd, 0, NULL, tgt_uuid);
528 lmv_init_unlock(lmv);
529 CERROR("lmv failed to setup llogging subsystems\n");
532 spin_lock(&lmv->lmv_lock);
533 tgt = lmv->tgts + lmv->desc.ld_tgt_count++;
534 tgt->ltd_uuid = *tgt_uuid;
535 spin_unlock(&lmv->lmv_lock);
537 if (lmv->connected) {
538 rc = lmv_connect_mdc(obd, tgt);
540 spin_lock(&lmv->lmv_lock);
541 lmv->desc.ld_tgt_count--;
542 memset(tgt, 0, sizeof(*tgt));
543 spin_unlock(&lmv->lmv_lock);
545 int easize = sizeof(struct lmv_stripe_md) +
546 lmv->desc.ld_tgt_count *
547 sizeof(struct lu_fid);
548 lmv_init_ea_size(obd->obd_self_export, easize, 0, 0);
552 lmv_init_unlock(lmv);
556 int lmv_check_connect(struct obd_device *obd)
558 struct lmv_obd *lmv = &obd->u.lmv;
559 struct lmv_tgt_desc *tgt;
569 if (lmv->connected) {
570 lmv_init_unlock(lmv);
574 if (lmv->desc.ld_tgt_count == 0) {
575 CERROR("%s: no targets configured.\n", obd->obd_name);
579 CDEBUG(D_CONFIG, "Time to connect %s to %s\n",
580 lmv->cluuid.uuid, obd->obd_name);
582 LASSERT(lmv->tgts != NULL);
584 for (i = 0, tgt = lmv->tgts; i < lmv->desc.ld_tgt_count; i++, tgt++) {
585 rc = lmv_connect_mdc(obd, tgt);
590 lmv_set_timeouts(obd);
591 class_export_put(lmv->exp);
593 easize = lmv_get_easize(lmv);
594 lmv_init_ea_size(obd->obd_self_export, easize, 0, 0);
595 lmv_init_unlock(lmv);
604 --lmv->desc.ld_active_tgt_count;
605 rc2 = obd_disconnect(tgt->ltd_exp);
607 CERROR("LMV target %s disconnect on "
608 "MDC idx %d: error %d\n",
609 tgt->ltd_uuid.uuid, i, rc2);
613 class_disconnect(lmv->exp);
614 lmv_init_unlock(lmv);
618 static int lmv_disconnect_mdc(struct obd_device *obd, struct lmv_tgt_desc *tgt)
621 struct proc_dir_entry *lmv_proc_dir;
623 struct lmv_obd *lmv = &obd->u.lmv;
624 struct obd_device *mdc_obd;
628 LASSERT(tgt != NULL);
629 LASSERT(obd != NULL);
631 mdc_obd = class_exp2obd(tgt->ltd_exp);
634 mdc_obd->obd_no_recov = obd->obd_no_recov;
637 lmv_proc_dir = lprocfs_srch(obd->obd_proc_entry, "target_obds");
639 struct proc_dir_entry *mdc_symlink;
641 mdc_symlink = lprocfs_srch(lmv_proc_dir, mdc_obd->obd_name);
643 lprocfs_remove(&mdc_symlink);
645 CERROR("/proc/fs/lustre/%s/%s/target_obds/%s missing\n",
646 obd->obd_type->typ_name, obd->obd_name,
651 rc = obd_fid_fini(tgt->ltd_exp);
653 CERROR("Can't finanize fids factory\n");
655 CDEBUG(D_INFO, "Disconnected from %s(%s) successfully\n",
656 tgt->ltd_exp->exp_obd->obd_name,
657 tgt->ltd_exp->exp_obd->obd_uuid.uuid);
659 obd_register_observer(tgt->ltd_exp->exp_obd, NULL);
660 rc = obd_disconnect(tgt->ltd_exp);
662 if (tgt->ltd_active) {
663 CERROR("Target %s disconnect error %d\n",
664 tgt->ltd_uuid.uuid, rc);
668 lmv_activate_target(lmv, tgt, 0);
673 static int lmv_disconnect(struct obd_export *exp)
675 struct obd_device *obd = class_exp2obd(exp);
677 struct proc_dir_entry *lmv_proc_dir;
679 struct lmv_obd *lmv = &obd->u.lmv;
688 * Only disconnect the underlying layers on the final disconnect.
691 if (lmv->refcount != 0)
694 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
695 if (lmv->tgts[i].ltd_exp == NULL)
697 lmv_disconnect_mdc(obd, &lmv->tgts[i]);
701 lmv_proc_dir = lprocfs_srch(obd->obd_proc_entry, "target_obds");
703 lprocfs_remove(&lmv_proc_dir);
705 CERROR("/proc/fs/lustre/%s/%s/target_obds missing\n",
706 obd->obd_type->typ_name, obd->obd_name);
712 * This is the case when no real connection is established by
713 * lmv_check_connect().
716 class_export_put(exp);
717 rc = class_disconnect(exp);
718 if (lmv->refcount == 0)
723 static int lmv_iocontrol(unsigned int cmd, struct obd_export *exp,
724 int len, void *karg, void *uarg)
726 struct obd_device *obddev = class_exp2obd(exp);
727 struct lmv_obd *lmv = &obddev->u.lmv;
731 int count = lmv->desc.ld_tgt_count;
738 case IOC_OBD_STATFS: {
739 struct obd_ioctl_data *data = karg;
740 struct obd_device *mdc_obd;
741 struct obd_statfs stat_buf = {0};
744 memcpy(&index, data->ioc_inlbuf2, sizeof(__u32));
745 LASSERT(data->ioc_plen1 == sizeof(struct obd_statfs));
747 if ((index >= count))
750 if (!lmv->tgts[index].ltd_active)
753 mdc_obd = class_exp2obd(lmv->tgts[index].ltd_exp);
757 rc = obd_statfs(mdc_obd, &stat_buf,
758 cfs_time_current_64() - HZ, 0);
761 if (copy_to_user(data->ioc_pbuf1, &stat_buf, data->ioc_plen1))
763 if (copy_to_user(data->ioc_pbuf2, obd2cli_tgt(mdc_obd),
768 case OBD_IOC_QUOTACTL: {
769 struct if_quotactl *qctl = karg;
770 struct lmv_tgt_desc *tgt = NULL;
771 struct obd_quotactl *oqctl;
773 if (qctl->qc_valid == QC_MDTIDX) {
774 if (qctl->qc_idx < 0 || count <= qctl->qc_idx)
777 tgt = &lmv->tgts[qctl->qc_idx];
780 } else if (qctl->qc_valid == QC_UUID) {
781 for (i = 0; i < count; i++) {
783 if (!obd_uuid_equals(&tgt->ltd_uuid,
787 if (tgt->ltd_exp == NULL)
799 LASSERT(tgt && tgt->ltd_exp);
800 OBD_ALLOC_PTR(oqctl);
804 QCTL_COPY(oqctl, qctl);
805 rc = obd_quotactl(tgt->ltd_exp, oqctl);
807 QCTL_COPY(qctl, oqctl);
808 qctl->qc_valid = QC_MDTIDX;
809 qctl->obd_uuid = tgt->ltd_uuid;
815 for (i = 0; i < count; i++) {
818 if (lmv->tgts[i].ltd_exp == NULL)
821 err = obd_iocontrol(cmd, lmv->tgts[i].ltd_exp, len,
823 if (err == -ENODATA && cmd == OBD_IOC_POLL_QUOTACHECK) {
826 if (lmv->tgts[i].ltd_active) {
827 CERROR("error: iocontrol MDC %s on MDT"
828 "idx %d cmd %x: err = %d\n",
829 lmv->tgts[i].ltd_uuid.uuid,
844 static int lmv_all_chars_policy(int count, const char *name,
855 static int lmv_nid_policy(struct lmv_obd *lmv)
857 struct obd_import *imp;
861 * XXX: To get nid we assume that underlying obd device is mdc.
863 imp = class_exp2cliimp(lmv->tgts[0].ltd_exp);
864 id = imp->imp_connection->c_self ^ (imp->imp_connection->c_self >> 32);
865 return id % lmv->desc.ld_tgt_count;
868 static int lmv_choose_mds(struct lmv_obd *lmv, struct md_op_data *op_data,
869 placement_policy_t placement)
872 case PLACEMENT_CHAR_POLICY:
873 return lmv_all_chars_policy(lmv->desc.ld_tgt_count,
875 op_data->op_namelen);
876 case PLACEMENT_NID_POLICY:
877 return lmv_nid_policy(lmv);
883 CERROR("Unsupported placement policy %x\n", placement);
888 * This is _inode_ placement policy function (not name).
890 static int lmv_placement_policy(struct obd_device *obd,
891 struct md_op_data *op_data,
894 struct lmv_obd *lmv = &obd->u.lmv;
895 struct lmv_object *obj;
899 LASSERT(mds != NULL);
901 if (lmv->desc.ld_tgt_count == 1) {
907 * Allocate new fid on target according to operation type and parent
910 obj = lmv_object_find(obd, &op_data->op_fid1);
911 if (obj != NULL || op_data->op_name == NULL ||
912 op_data->op_opc != LUSTRE_OPC_MKDIR) {
914 * Allocate fid for non-dir or for null name or for case parent
921 * If we have this flag turned on, and we see that
922 * parent dir is split, this means, that caller did not
923 * notice split yet. This is race and we would like to
924 * let caller know that.
926 if (op_data->op_bias & MDS_CHECK_SPLIT)
931 * Allocate new fid on same mds where parent fid is located and
932 * where operation will be sent. In case of split dir, ->op_fid1
933 * and ->op_mds here will contain fid and mds of slave directory
934 * object (assigned by caller).
936 *mds = op_data->op_mds;
940 * Parent directory is not split and we want to create a
941 * directory in it. Let's calculate where to place it according
942 * to operation data @op_data.
944 *mds = lmv_choose_mds(lmv, op_data, lmv->lmv_placement);
949 CERROR("Can't choose MDS, err = %d\n", rc);
951 LASSERT(*mds < lmv->desc.ld_tgt_count);
957 int __lmv_fid_alloc(struct lmv_obd *lmv, struct lu_fid *fid,
960 struct lmv_tgt_desc *tgt;
964 tgt = lmv_get_target(lmv, mds);
967 * New seq alloc and FLD setup should be atomic. Otherwise we may find
968 * on server that seq in new allocated fid is not yet known.
970 down(&tgt->ltd_fid_sem);
972 if (!tgt->ltd_active)
973 GOTO(out, rc = -ENODEV);
976 * Asking underlaying tgt layer to allocate new fid.
978 rc = obd_fid_alloc(tgt->ltd_exp, fid, NULL);
980 LASSERT(fid_is_sane(fid));
983 * Client switches to new sequence, setup FLD.
985 rc = fld_client_create(&lmv->lmv_fld, fid_seq(fid),
989 * Delete just allocated fid sequence in case
992 CERROR("Can't create fld entry, rc %d\n", rc);
993 obd_fid_delete(tgt->ltd_exp, NULL);
999 up(&tgt->ltd_fid_sem);
1003 int lmv_fid_alloc(struct obd_export *exp, struct lu_fid *fid,
1004 struct md_op_data *op_data)
1006 struct obd_device *obd = class_exp2obd(exp);
1007 struct lmv_obd *lmv = &obd->u.lmv;
1012 LASSERT(op_data != NULL);
1013 LASSERT(fid != NULL);
1015 rc = lmv_placement_policy(obd, op_data, &mds);
1017 CERROR("Can't get target for allocating fid, "
1022 rc = __lmv_fid_alloc(lmv, fid, mds);
1024 CERROR("Can't alloc new fid, rc %d\n", rc);
1031 static int lmv_fid_delete(struct obd_export *exp, const struct lu_fid *fid)
1034 LASSERT(exp != NULL && fid != NULL);
1035 if (lmv_object_delete(exp, fid)) {
1036 CDEBUG(D_INODE, "Object "DFID" is destroyed.\n",
1042 static int lmv_setup(struct obd_device *obd, struct lustre_cfg *lcfg)
1044 struct lmv_obd *lmv = &obd->u.lmv;
1045 struct lprocfs_static_vars lvars;
1046 struct lmv_desc *desc;
1051 if (LUSTRE_CFG_BUFLEN(lcfg, 1) < 1) {
1052 CERROR("LMV setup requires a descriptor\n");
1056 desc = (struct lmv_desc *)lustre_cfg_buf(lcfg, 1);
1057 if (sizeof(*desc) > LUSTRE_CFG_BUFLEN(lcfg, 1)) {
1058 CERROR("Lmv descriptor size wrong: %d > %d\n",
1059 (int)sizeof(*desc), LUSTRE_CFG_BUFLEN(lcfg, 1));
1063 lmv->tgts_size = LMV_MAX_TGT_COUNT * sizeof(struct lmv_tgt_desc);
1065 OBD_ALLOC(lmv->tgts, lmv->tgts_size);
1066 if (lmv->tgts == NULL)
1069 for (i = 0; i < LMV_MAX_TGT_COUNT; i++) {
1070 sema_init(&lmv->tgts[i].ltd_fid_sem, 1);
1071 lmv->tgts[i].ltd_idx = i;
1074 lmv->datas_size = LMV_MAX_TGT_COUNT * sizeof(struct obd_connect_data);
1076 OBD_ALLOC(lmv->datas, lmv->datas_size);
1077 if (lmv->datas == NULL)
1078 GOTO(out_free_tgts, rc = -ENOMEM);
1080 obd_str2uuid(&lmv->desc.ld_uuid, desc->ld_uuid.uuid);
1081 lmv->desc.ld_tgt_count = 0;
1082 lmv->desc.ld_active_tgt_count = 0;
1083 lmv->max_cookiesize = 0;
1084 lmv->max_def_easize = 0;
1085 lmv->max_easize = 0;
1086 lmv->lmv_placement = PLACEMENT_CHAR_POLICY;
1088 spin_lock_init(&lmv->lmv_lock);
1089 sema_init(&lmv->init_sem, 1);
1091 rc = lmv_object_setup(obd);
1093 CERROR("Can't setup LMV object manager, error %d.\n", rc);
1094 GOTO(out_free_datas, rc);
1097 lprocfs_lmv_init_vars(&lvars);
1098 lprocfs_obd_setup(obd, lvars.obd_vars);
1101 rc = lprocfs_seq_create(obd->obd_proc_entry, "target_obd_status",
1102 0444, &lmv_proc_target_fops, obd);
1104 CWARN("Error adding target_obd_stats file (%d)\n", rc);
1107 rc = fld_client_init(&lmv->lmv_fld, obd->obd_name,
1108 LUSTRE_CLI_FLD_HASH_DHT);
1110 CERROR("Can't init FLD, err %d\n", rc);
1111 GOTO(out_free_datas, rc);
1117 OBD_FREE(lmv->datas, lmv->datas_size);
1120 OBD_FREE(lmv->tgts, lmv->tgts_size);
1125 static int lmv_cleanup(struct obd_device *obd)
1127 struct lmv_obd *lmv = &obd->u.lmv;
1130 fld_client_fini(&lmv->lmv_fld);
1131 lprocfs_obd_cleanup(obd);
1132 lmv_object_cleanup(obd);
1133 OBD_FREE(lmv->datas, lmv->datas_size);
1134 OBD_FREE(lmv->tgts, lmv->tgts_size);
1139 static int lmv_process_config(struct obd_device *obd, obd_count len, void *buf)
1141 struct lustre_cfg *lcfg = buf;
1142 struct obd_uuid tgt_uuid;
1146 switch(lcfg->lcfg_command) {
1148 if (LUSTRE_CFG_BUFLEN(lcfg, 1) > sizeof(tgt_uuid.uuid))
1149 GOTO(out, rc = -EINVAL);
1151 obd_str2uuid(&tgt_uuid, lustre_cfg_string(lcfg, 1));
1152 rc = lmv_add_target(obd, &tgt_uuid);
1155 CERROR("Unknown command: %d\n", lcfg->lcfg_command);
1156 GOTO(out, rc = -EINVAL);
1163 static int lmv_statfs(struct obd_device *obd, struct obd_statfs *osfs,
1164 __u64 max_age, __u32 flags)
1166 struct lmv_obd *lmv = &obd->u.lmv;
1167 struct obd_statfs *temp;
1172 rc = lmv_check_connect(obd);
1176 OBD_ALLOC(temp, sizeof(*temp));
1180 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
1181 if (lmv->tgts[i].ltd_exp == NULL)
1184 rc = obd_statfs(lmv->tgts[i].ltd_exp->exp_obd, temp,
1187 CERROR("can't stat MDS #%d (%s), error %d\n", i,
1188 lmv->tgts[i].ltd_exp->exp_obd->obd_name,
1190 GOTO(out_free_temp, rc);
1195 osfs->os_bavail += temp->os_bavail;
1196 osfs->os_blocks += temp->os_blocks;
1197 osfs->os_ffree += temp->os_ffree;
1198 osfs->os_files += temp->os_files;
1204 OBD_FREE(temp, sizeof(*temp));
1208 static int lmv_getstatus(struct obd_export *exp,
1210 struct obd_capa **pc)
1212 struct obd_device *obd = exp->exp_obd;
1213 struct lmv_obd *lmv = &obd->u.lmv;
1217 rc = lmv_check_connect(obd);
1221 rc = md_getstatus(lmv->tgts[0].ltd_exp, fid, pc);
1225 static int lmv_getxattr(struct obd_export *exp, const struct lu_fid *fid,
1226 struct obd_capa *oc, obd_valid valid, const char *name,
1227 const char *input, int input_size, int output_size,
1228 int flags, struct ptlrpc_request **request)
1230 struct obd_device *obd = exp->exp_obd;
1231 struct lmv_obd *lmv = &obd->u.lmv;
1232 struct lmv_tgt_desc *tgt;
1236 rc = lmv_check_connect(obd);
1240 tgt = lmv_find_target(lmv, fid);
1242 RETURN(PTR_ERR(tgt));
1244 rc = md_getxattr(tgt->ltd_exp, fid, oc, valid, name, input,
1245 input_size, output_size, flags, request);
1250 static int lmv_setxattr(struct obd_export *exp, const struct lu_fid *fid,
1251 struct obd_capa *oc, obd_valid valid, const char *name,
1252 const char *input, int input_size, int output_size,
1253 int flags, __u32 suppgid,
1254 struct ptlrpc_request **request)
1256 struct obd_device *obd = exp->exp_obd;
1257 struct lmv_obd *lmv = &obd->u.lmv;
1258 struct lmv_tgt_desc *tgt;
1262 rc = lmv_check_connect(obd);
1266 tgt = lmv_find_target(lmv, fid);
1268 RETURN(PTR_ERR(tgt));
1270 rc = md_setxattr(tgt->ltd_exp, fid, oc, valid, name, input,
1271 input_size, output_size, flags, suppgid,
1277 static int lmv_getattr(struct obd_export *exp, const struct lu_fid *fid,
1278 struct obd_capa *oc, obd_valid valid, int ea_size,
1279 struct ptlrpc_request **request)
1281 struct obd_device *obd = exp->exp_obd;
1282 struct lmv_obd *lmv = &obd->u.lmv;
1283 struct lmv_tgt_desc *tgt;
1284 struct lmv_object *obj;
1289 rc = lmv_check_connect(obd);
1293 tgt = lmv_find_target(lmv, fid);
1295 RETURN(PTR_ERR(tgt));
1297 rc = md_getattr(tgt->ltd_exp, fid, oc, valid, ea_size, request);
1301 obj = lmv_object_find_lock(obd, fid);
1303 CDEBUG(D_INODE, "GETATTR for "DFID" %s\n", PFID(fid),
1304 obj ? "(split)" : "");
1307 * If object is split, then we loop over all the slaves and gather size
1308 * attribute. In ideal world we would have to gather also mds field from
1309 * all slaves, as object is spread over the cluster and this is
1310 * definitely interesting information and it is not good to loss it,
1314 struct mdt_body *body;
1316 if (*request == NULL) {
1317 lmv_object_put(obj);
1321 body = req_capsule_server_get(&(*request)->rq_pill,
1323 LASSERT(body != NULL);
1325 for (i = 0; i < obj->lo_objcount; i++) {
1326 if (lmv->tgts[i].ltd_exp == NULL) {
1327 CWARN("%s: NULL export for %d\n",
1333 * Skip master object.
1335 if (lu_fid_eq(&obj->lo_fid, &obj->lo_stripes[i].ls_fid))
1338 body->size += obj->lo_stripes[i].ls_size;
1341 lmv_object_put_unlock(obj);
1347 static int lmv_change_cbdata(struct obd_export *exp, const struct lu_fid *fid,
1348 ldlm_iterator_t it, void *data)
1350 struct obd_device *obd = exp->exp_obd;
1351 struct lmv_obd *lmv = &obd->u.lmv;
1356 rc = lmv_check_connect(obd);
1360 CDEBUG(D_INODE, "CBDATA for "DFID"\n", PFID(fid));
1363 * With CMD every object can have two locks in different namespaces:
1364 * lookup lock in space of mds storing direntry and update/open lock in
1365 * space of mds storing inode.
1367 for (i = 0; i < lmv->desc.ld_tgt_count; i++)
1368 md_change_cbdata(lmv->tgts[i].ltd_exp, fid, it, data);
1373 static int lmv_close(struct obd_export *exp, struct md_op_data *op_data,
1374 struct md_open_data *mod, struct ptlrpc_request **request)
1376 struct obd_device *obd = exp->exp_obd;
1377 struct lmv_obd *lmv = &obd->u.lmv;
1378 struct lmv_tgt_desc *tgt;
1382 rc = lmv_check_connect(obd);
1386 tgt = lmv_find_target(lmv, &op_data->op_fid1);
1388 RETURN(PTR_ERR(tgt));
1390 CDEBUG(D_INODE, "CLOSE "DFID"\n", PFID(&op_data->op_fid1));
1391 rc = md_close(tgt->ltd_exp, op_data, mod, request);
1396 * Called in the case MDS returns -ERESTART on create on open, what means that
1397 * directory is split and its LMV presentation object has to be updated.
1399 int lmv_handle_split(struct obd_export *exp, const struct lu_fid *fid)
1401 struct obd_device *obd = exp->exp_obd;
1402 struct lmv_obd *lmv = &obd->u.lmv;
1403 struct ptlrpc_request *req = NULL;
1404 struct lmv_tgt_desc *tgt;
1405 struct lmv_object *obj;
1406 struct lustre_md md;
1413 mealen = lmv_get_easize(lmv);
1415 valid = OBD_MD_FLEASIZE | OBD_MD_FLDIREA | OBD_MD_MEA;
1417 tgt = lmv_find_target(lmv, fid);
1419 RETURN(PTR_ERR(tgt));
1422 * Time to update mea of parent fid.
1424 rc = md_getattr(tgt->ltd_exp, fid, NULL, valid, mealen, &req);
1426 CERROR("md_getattr() failed, error %d\n", rc);
1430 rc = md_get_lustre_md(tgt->ltd_exp, req, NULL, exp, &md);
1432 CERROR("md_get_lustre_md() failed, error %d\n", rc);
1437 GOTO(cleanup, rc = -ENODATA);
1439 obj = lmv_object_create(exp, fid, md.mea);
1443 lmv_object_put(obj);
1445 obd_free_memmd(exp, (void *)&md.mea);
1449 ptlrpc_req_finished(req);
1453 int lmv_create(struct obd_export *exp, struct md_op_data *op_data,
1454 const void *data, int datalen, int mode, __u32 uid,
1455 __u32 gid, cfs_cap_t cap_effective, __u64 rdev,
1456 struct ptlrpc_request **request)
1458 struct obd_device *obd = exp->exp_obd;
1459 struct lmv_obd *lmv = &obd->u.lmv;
1460 struct lmv_tgt_desc *tgt;
1461 struct lmv_object *obj;
1467 rc = lmv_check_connect(obd);
1471 if (!lmv->desc.ld_active_tgt_count)
1477 obj = lmv_object_find(obd, &op_data->op_fid1);
1479 sidx = raw_name2idx(obj->lo_hashtype, obj->lo_objcount,
1480 op_data->op_name, op_data->op_namelen);
1481 op_data->op_fid1 = obj->lo_stripes[sidx].ls_fid;
1482 op_data->op_bias &= ~MDS_CHECK_SPLIT;
1483 op_data->op_mds = obj->lo_stripes[sidx].ls_mds;
1484 tgt = lmv_get_target(lmv, op_data->op_mds);
1485 lmv_object_put(obj);
1487 tgt = lmv_find_target(lmv, &op_data->op_fid1);
1488 op_data->op_bias |= MDS_CHECK_SPLIT;
1489 op_data->op_mds = tgt->ltd_idx;
1493 RETURN(PTR_ERR(tgt));
1495 rc = lmv_fid_alloc(exp, &op_data->op_fid2, op_data);
1496 if (rc == -ERESTART)
1501 CDEBUG(D_INODE, "CREATE '%*s' on "DFID" -> mds #"LPU64"\n",
1502 op_data->op_namelen, op_data->op_name, PFID(&op_data->op_fid1),
1505 op_data->op_flags |= MF_MDC_CANCEL_FID1;
1506 rc = md_create(tgt->ltd_exp, op_data, data, datalen, mode, uid, gid,
1507 cap_effective, rdev, request);
1509 if (*request == NULL)
1511 CDEBUG(D_INODE, "Created - "DFID"\n", PFID(&op_data->op_fid2));
1512 } else if (rc == -ERESTART) {
1513 LASSERT(*request != NULL);
1514 DEBUG_REQ(D_WARNING|D_RPCTRACE, *request,
1515 "Got -ERESTART during create!\n");
1516 ptlrpc_req_finished(*request);
1520 * Directory got split. Time to update local object and repeat
1521 * the request with proper MDS.
1523 rc = lmv_handle_split(exp, &op_data->op_fid1);
1525 rc = lmv_allocate_slaves(obd, &op_data->op_fid1,
1526 op_data, &op_data->op_fid2);
1535 static int lmv_done_writing(struct obd_export *exp,
1536 struct md_op_data *op_data,
1537 struct md_open_data *mod)
1539 struct obd_device *obd = exp->exp_obd;
1540 struct lmv_obd *lmv = &obd->u.lmv;
1541 struct lmv_tgt_desc *tgt;
1545 rc = lmv_check_connect(obd);
1549 tgt = lmv_find_target(lmv, &op_data->op_fid1);
1551 RETURN(PTR_ERR(tgt));
1553 rc = md_done_writing(tgt->ltd_exp, op_data, mod);
1558 lmv_enqueue_slaves(struct obd_export *exp, struct ldlm_enqueue_info *einfo,
1559 struct lookup_intent *it, struct md_op_data *op_data,
1560 struct lustre_handle *lockh, void *lmm, int lmmsize)
1562 struct obd_device *obd = exp->exp_obd;
1563 struct lmv_obd *lmv = &obd->u.lmv;
1564 struct lmv_stripe_md *mea = op_data->op_mea1;
1565 struct md_op_data *op_data2;
1566 struct lmv_tgt_desc *tgt;
1571 OBD_ALLOC_PTR(op_data2);
1572 if (op_data2 == NULL)
1575 LASSERT(mea != NULL);
1576 for (i = 0; i < mea->mea_count; i++) {
1577 memset(op_data2, 0, sizeof(*op_data2));
1578 op_data2->op_fid1 = mea->mea_ids[i];
1579 op_data2->op_bias = 0;
1581 tgt = lmv_find_target(lmv, &op_data2->op_fid1);
1583 GOTO(cleanup, rc = PTR_ERR(tgt));
1585 if (tgt->ltd_exp == NULL)
1588 rc = md_enqueue(tgt->ltd_exp, einfo, it, op_data2,
1589 lockh + i, lmm, lmmsize, NULL, 0);
1591 CDEBUG(D_INODE, "Take lock on slave "DFID" -> %d/%d\n",
1592 PFID(&mea->mea_ids[i]), rc, it->d.lustre.it_status);
1597 if (it->d.lustre.it_data) {
1598 struct ptlrpc_request *req;
1599 req = (struct ptlrpc_request *)it->d.lustre.it_data;
1600 ptlrpc_req_finished(req);
1603 if (it->d.lustre.it_status)
1604 GOTO(cleanup, rc = it->d.lustre.it_status);
1609 OBD_FREE_PTR(op_data2);
1613 * Drop all taken locks.
1616 if (lockh[i].cookie)
1617 ldlm_lock_decref(lockh + i, einfo->ei_mode);
1618 lockh[i].cookie = 0;
1625 lmv_enqueue_remote(struct obd_export *exp, struct ldlm_enqueue_info *einfo,
1626 struct lookup_intent *it, struct md_op_data *op_data,
1627 struct lustre_handle *lockh, void *lmm, int lmmsize,
1628 int extra_lock_flags)
1630 struct ptlrpc_request *req = it->d.lustre.it_data;
1631 struct obd_device *obd = exp->exp_obd;
1632 struct lmv_obd *lmv = &obd->u.lmv;
1633 struct lustre_handle plock;
1634 struct lmv_tgt_desc *tgt;
1635 struct md_op_data *rdata;
1637 struct mdt_body *body;
1642 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
1643 LASSERT(body != NULL);
1645 if (!(body->valid & OBD_MD_MDS))
1648 CDEBUG(D_INODE, "REMOTE_ENQUEUE '%s' on "DFID" -> "DFID"\n",
1649 LL_IT2STR(it), PFID(&op_data->op_fid1), PFID(&body->fid1));
1652 * We got LOOKUP lock, but we really need attrs.
1654 pmode = it->d.lustre.it_lock_mode;
1655 LASSERT(pmode != 0);
1656 memcpy(&plock, lockh, sizeof(plock));
1657 it->d.lustre.it_lock_mode = 0;
1658 it->d.lustre.it_data = NULL;
1661 it->d.lustre.it_disposition &= ~DISP_ENQ_COMPLETE;
1662 ptlrpc_req_finished(req);
1664 tgt = lmv_find_target(lmv, &fid1);
1666 GOTO(out, rc = PTR_ERR(tgt));
1668 OBD_ALLOC_PTR(rdata);
1670 GOTO(out, rc = -ENOMEM);
1672 rdata->op_fid1 = fid1;
1673 rdata->op_bias = MDS_CROSS_REF;
1675 rc = md_enqueue(tgt->ltd_exp, einfo, it, rdata, lockh,
1676 lmm, lmmsize, NULL, extra_lock_flags);
1677 OBD_FREE_PTR(rdata);
1680 ldlm_lock_decref(&plock, pmode);
1685 lmv_enqueue(struct obd_export *exp, struct ldlm_enqueue_info *einfo,
1686 struct lookup_intent *it, struct md_op_data *op_data,
1687 struct lustre_handle *lockh, void *lmm, int lmmsize,
1688 struct ptlrpc_request **req, int extra_lock_flags)
1690 struct obd_device *obd = exp->exp_obd;
1691 struct lmv_obd *lmv = &obd->u.lmv;
1692 struct lmv_tgt_desc *tgt;
1693 struct lmv_object *obj;
1698 rc = lmv_check_connect(obd);
1702 CDEBUG(D_INODE, "ENQUEUE '%s' on "DFID"\n",
1703 LL_IT2STR(it), PFID(&op_data->op_fid1));
1705 if (op_data->op_mea1 && it && it->it_op == IT_UNLINK) {
1706 rc = lmv_enqueue_slaves(exp, einfo, it, op_data,
1707 lockh, lmm, lmmsize);
1711 obj = lmv_object_find(obd, &op_data->op_fid1);
1712 if (obj && op_data->op_namelen) {
1713 sidx = raw_name2idx(obj->lo_hashtype,
1715 (char *)op_data->op_name,
1716 op_data->op_namelen);
1717 op_data->op_fid1 = obj->lo_stripes[sidx].ls_fid;
1718 tgt = lmv_get_target(lmv, obj->lo_stripes[sidx].ls_mds);
1720 tgt = lmv_find_target(lmv, &op_data->op_fid1);
1723 lmv_object_put(obj);
1726 RETURN(PTR_ERR(tgt));
1728 CDEBUG(D_INODE, "ENQUEUE '%s' on "DFID" -> mds #%d\n",
1729 LL_IT2STR(it), PFID(&op_data->op_fid1), tgt->ltd_idx);
1731 rc = md_enqueue(tgt->ltd_exp, einfo, it, op_data, lockh,
1732 lmm, lmmsize, req, extra_lock_flags);
1734 if (rc == 0 && it && it->it_op == IT_OPEN) {
1735 rc = lmv_enqueue_remote(exp, einfo, it, op_data, lockh,
1736 lmm, lmmsize, extra_lock_flags);
1742 lmv_getattr_name(struct obd_export *exp, const struct lu_fid *fid,
1743 struct obd_capa *oc, const char *name, int namelen,
1744 obd_valid valid, int ea_size, __u32 suppgid,
1745 struct ptlrpc_request **request)
1747 struct ptlrpc_request *req = NULL;
1748 struct obd_device *obd = exp->exp_obd;
1749 struct lmv_obd *lmv = &obd->u.lmv;
1750 struct lu_fid rid = *fid;
1751 struct lmv_tgt_desc *tgt;
1752 struct mdt_body *body;
1753 struct lmv_object *obj;
1759 rc = lmv_check_connect(obd);
1766 obj = lmv_object_find(obd, &rid);
1768 sidx = raw_name2idx(obj->lo_hashtype, obj->lo_objcount,
1770 rid = obj->lo_stripes[sidx].ls_fid;
1771 tgt = lmv_get_target(lmv, obj->lo_stripes[sidx].ls_mds);
1772 valid &= ~OBD_MD_FLCKSPLIT;
1773 lmv_object_put(obj);
1775 tgt = lmv_find_target(lmv, &rid);
1776 valid |= OBD_MD_FLCKSPLIT;
1779 RETURN(PTR_ERR(tgt));
1781 CDEBUG(D_INODE, "GETATTR_NAME for %*s on "DFID" - "DFID" -> mds #%d\n",
1782 namelen, name, PFID(fid), PFID(&rid), tgt->ltd_idx);
1784 rc = md_getattr_name(tgt->ltd_exp, &rid, oc, name, namelen, valid,
1785 ea_size, suppgid, request);
1787 body = req_capsule_server_get(&(*request)->rq_pill,
1789 LASSERT(body != NULL);
1791 if (body->valid & OBD_MD_MDS) {
1793 CDEBUG(D_INODE, "Request attrs for "DFID"\n",
1796 tgt = lmv_find_target(lmv, &rid);
1798 ptlrpc_req_finished(*request);
1799 RETURN(PTR_ERR(tgt));
1802 rc = md_getattr_name(tgt->ltd_exp, &rid, NULL, NULL,
1803 1, valid | OBD_MD_FLCROSSREF,
1804 ea_size, suppgid, &req);
1805 ptlrpc_req_finished(*request);
1808 } else if (rc == -ERESTART) {
1809 LASSERT(*request != NULL);
1810 DEBUG_REQ(D_WARNING|D_RPCTRACE, *request,
1811 "Got -ERESTART during getattr!\n");
1812 ptlrpc_req_finished(*request);
1816 * Directory got split. Time to update local object and repeat
1817 * the request with proper MDS.
1819 rc = lmv_handle_split(exp, &rid);
1826 #define md_op_data_fid(op_data, fl) \
1827 (fl == MF_MDC_CANCEL_FID1 ? &op_data->op_fid1 : \
1828 fl == MF_MDC_CANCEL_FID2 ? &op_data->op_fid2 : \
1829 fl == MF_MDC_CANCEL_FID3 ? &op_data->op_fid3 : \
1830 fl == MF_MDC_CANCEL_FID4 ? &op_data->op_fid4 : \
1833 static int lmv_early_cancel_slaves(struct obd_export *exp,
1834 struct md_op_data *op_data, int op_tgt,
1835 ldlm_mode_t mode, int bits, int flag)
1837 struct obd_device *obd = exp->exp_obd;
1838 struct lmv_obd *lmv = &obd->u.lmv;
1839 ldlm_policy_data_t policy = {{0}};
1840 struct lu_fid *op_fid;
1841 struct lu_fid *st_fid;
1842 struct lmv_tgt_desc *tgt;
1843 struct lmv_object *obj;
1848 op_fid = md_op_data_fid(op_data, flag);
1849 if (!fid_is_sane(op_fid))
1852 obj = lmv_object_find(obd, op_fid);
1856 policy.l_inodebits.bits = bits;
1857 for (i = 0; i < obj->lo_objcount; i++) {
1858 tgt = lmv_get_target(lmv, obj->lo_stripes[i].ls_mds);
1859 st_fid = &obj->lo_stripes[i].ls_fid;
1860 if (op_tgt != tgt->ltd_idx) {
1861 CDEBUG(D_INODE, "EARLY_CANCEL slave "DFID" -> mds #%d\n",
1862 PFID(st_fid), tgt->ltd_idx);
1863 rc = md_cancel_unused(tgt->ltd_exp, st_fid, &policy,
1864 mode, LDLM_FL_ASYNC, NULL);
1866 GOTO(out_put_obj, rc);
1869 "EARLY_CANCEL skip operation target %d on "DFID"\n",
1870 op_tgt, PFID(st_fid));
1872 * Do not cancel locks for operation target, they will
1873 * be handled later in underlaying layer when calling
1874 * function we run on behalf of.
1877 op_data->op_flags |= flag;
1882 lmv_object_put(obj);
1886 static int lmv_early_cancel(struct obd_export *exp, struct md_op_data *op_data,
1887 int op_tgt, ldlm_mode_t mode, int bits, int flag)
1889 struct lu_fid *fid = md_op_data_fid(op_data, flag);
1890 struct obd_device *obd = exp->exp_obd;
1891 struct lmv_obd *lmv = &obd->u.lmv;
1892 struct lmv_tgt_desc *tgt;
1893 ldlm_policy_data_t policy = {{0}};
1894 struct lmv_object *obj;
1898 if (!fid_is_sane(fid))
1901 obj = lmv_object_find(obd, fid);
1903 rc = lmv_early_cancel_slaves(exp, op_data, op_tgt, mode,
1905 lmv_object_put(obj);
1907 tgt = lmv_find_target(lmv, fid);
1909 RETURN(PTR_ERR(tgt));
1911 if (tgt->ltd_idx != op_tgt) {
1912 CDEBUG(D_INODE, "EARLY_CANCEL on "DFID"\n", PFID(fid));
1913 policy.l_inodebits.bits = bits;
1914 rc = md_cancel_unused(tgt->ltd_exp, fid, &policy,
1915 mode, LDLM_FL_ASYNC, NULL);
1918 "EARLY_CANCEL skip operation target %d on "DFID"\n",
1920 op_data->op_flags |= flag;
1929 * llite passes fid of an target inode in op_data->op_fid1 and id of directory in
1932 static int lmv_link(struct obd_export *exp, struct md_op_data *op_data,
1933 struct ptlrpc_request **request)
1935 struct obd_device *obd = exp->exp_obd;
1936 struct lmv_obd *lmv = &obd->u.lmv;
1937 struct lmv_tgt_desc *tgt;
1938 struct lmv_object *obj;
1945 rc = lmv_check_connect(obd);
1952 LASSERT(op_data->op_namelen != 0);
1954 CDEBUG(D_INODE, "LINK "DFID":%*s to "DFID"\n",
1955 PFID(&op_data->op_fid2), op_data->op_namelen,
1956 op_data->op_name, PFID(&op_data->op_fid1));
1958 obj = lmv_object_find(obd, &op_data->op_fid2);
1960 sidx = raw_name2idx(obj->lo_hashtype,
1963 op_data->op_namelen);
1964 op_data->op_fid2 = obj->lo_stripes[sidx].ls_fid;
1965 mds = obj->lo_stripes[sidx].ls_mds;
1966 lmv_object_put(obj);
1968 rc = lmv_fld_lookup(lmv, &op_data->op_fid2, &mds);
1973 CDEBUG(D_INODE, "Forward to mds #"LPU64" ("DFID")\n",
1974 mds, PFID(&op_data->op_fid1));
1976 op_data->op_fsuid = current->fsuid;
1977 op_data->op_fsgid = current->fsgid;
1978 op_data->op_cap = cfs_curproc_cap_pack();
1979 tgt = lmv_get_target(lmv, mds);
1982 * Cancel UPDATE lock on child (fid1).
1984 op_data->op_flags |= MF_MDC_CANCEL_FID2;
1985 rc = lmv_early_cancel(exp, op_data, tgt->ltd_idx, LCK_EX,
1986 MDS_INODELOCK_UPDATE, MF_MDC_CANCEL_FID1);
1988 rc = md_link(tgt->ltd_exp, op_data, request);
1989 if (rc == -ERESTART) {
1990 LASSERT(*request != NULL);
1991 DEBUG_REQ(D_WARNING|D_RPCTRACE, *request,
1992 "Got -ERESTART during link!\n");
1993 ptlrpc_req_finished(*request);
1997 * Directory got split. Time to update local object and repeat
1998 * the request with proper MDS.
2000 rc = lmv_handle_split(exp, &op_data->op_fid2);
2008 static int lmv_rename(struct obd_export *exp, struct md_op_data *op_data,
2009 const char *old, int oldlen, const char *new, int newlen,
2010 struct ptlrpc_request **request)
2012 struct obd_device *obd = exp->exp_obd;
2013 struct lmv_obd *lmv = &obd->u.lmv;
2014 struct lmv_tgt_desc *src_tgt;
2015 struct lmv_tgt_desc *tgt_tgt;
2019 struct lmv_object *obj;
2024 LASSERT(oldlen != 0);
2026 CDEBUG(D_INODE, "RENAME %*s in "DFID" to %*s in "DFID"\n",
2027 oldlen, old, PFID(&op_data->op_fid1),
2028 newlen, new, PFID(&op_data->op_fid2));
2030 rc = lmv_check_connect(obd);
2037 obj = lmv_object_find(obd, &op_data->op_fid1);
2039 sidx = raw_name2idx(obj->lo_hashtype, obj->lo_objcount,
2040 (char *)old, oldlen);
2041 op_data->op_fid1 = obj->lo_stripes[sidx].ls_fid;
2042 mds1 = obj->lo_stripes[sidx].ls_mds;
2043 CDEBUG(D_INODE, "Parent obj "DFID"\n", PFID(&op_data->op_fid1));
2044 lmv_object_put(obj);
2046 rc = lmv_fld_lookup(lmv, &op_data->op_fid1, &mds1);
2051 obj = lmv_object_find(obd, &op_data->op_fid2);
2054 * Directory is already split, so we have to forward request to
2057 sidx = raw_name2idx(obj->lo_hashtype, obj->lo_objcount,
2058 (char *)new, newlen);
2060 mds2 = obj->lo_stripes[sidx].ls_mds;
2061 op_data->op_fid2 = obj->lo_stripes[sidx].ls_fid;
2062 CDEBUG(D_INODE, "Parent obj "DFID"\n", PFID(&op_data->op_fid2));
2063 lmv_object_put(obj);
2065 rc = lmv_fld_lookup(lmv, &op_data->op_fid2, &mds2);
2070 op_data->op_fsuid = current->fsuid;
2071 op_data->op_fsgid = current->fsgid;
2072 op_data->op_cap = cfs_curproc_cap_pack();
2074 src_tgt = lmv_get_target(lmv, mds1);
2075 tgt_tgt = lmv_get_target(lmv, mds2);
2078 * LOOKUP lock on src child (fid3) should also be cancelled for
2079 * src_tgt in mdc_rename.
2081 op_data->op_flags |= MF_MDC_CANCEL_FID1 | MF_MDC_CANCEL_FID3;
2084 * Cancel UPDATE locks on tgt parent (fid2), tgt_tgt is its
2087 rc = lmv_early_cancel(exp, op_data, src_tgt->ltd_idx,
2088 LCK_EX, MDS_INODELOCK_UPDATE,
2089 MF_MDC_CANCEL_FID2);
2092 * Cancel LOOKUP locks on tgt child (fid4) for parent tgt_tgt.
2095 rc = lmv_early_cancel(exp, op_data, src_tgt->ltd_idx,
2096 LCK_EX, MDS_INODELOCK_LOOKUP,
2097 MF_MDC_CANCEL_FID4);
2101 * Cancel all the locks on tgt child (fid4).
2104 rc = lmv_early_cancel(exp, op_data, src_tgt->ltd_idx,
2105 LCK_EX, MDS_INODELOCK_FULL,
2106 MF_MDC_CANCEL_FID4);
2109 rc = md_rename(src_tgt->ltd_exp, op_data, old, oldlen,
2110 new, newlen, request);
2112 if (rc == -ERESTART) {
2113 LASSERT(*request != NULL);
2114 DEBUG_REQ(D_WARNING|D_RPCTRACE, *request,
2115 "Got -ERESTART during rename!\n");
2116 ptlrpc_req_finished(*request);
2120 * Directory got split. Time to update local object and repeat
2121 * the request with proper MDS.
2123 rc = lmv_handle_split(exp, &op_data->op_fid1);
2130 static int lmv_setattr(struct obd_export *exp, struct md_op_data *op_data,
2131 void *ea, int ealen, void *ea2, int ea2len,
2132 struct ptlrpc_request **request,
2133 struct md_open_data **mod)
2135 struct obd_device *obd = exp->exp_obd;
2136 struct lmv_obd *lmv = &obd->u.lmv;
2137 struct ptlrpc_request *req;
2138 struct lmv_tgt_desc *tgt;
2139 struct lmv_object *obj;
2144 rc = lmv_check_connect(obd);
2148 obj = lmv_object_find(obd, &op_data->op_fid1);
2150 CDEBUG(D_INODE, "SETATTR for "DFID", valid 0x%x%s\n",
2151 PFID(&op_data->op_fid1), op_data->op_attr.ia_valid,
2152 obj ? ", split" : "");
2154 op_data->op_flags |= MF_MDC_CANCEL_FID1;
2156 for (i = 0; i < obj->lo_objcount; i++) {
2157 op_data->op_fid1 = obj->lo_stripes[i].ls_fid;
2159 tgt = lmv_get_target(lmv, obj->lo_stripes[i].ls_mds);
2165 rc = md_setattr(tgt->ltd_exp, op_data, ea, ealen,
2166 ea2, ea2len, &req, mod);
2168 if (lu_fid_eq(&obj->lo_fid, &obj->lo_stripes[i].ls_fid)) {
2170 * This is master object and this request should
2171 * be returned back to llite.
2175 ptlrpc_req_finished(req);
2181 lmv_object_put(obj);
2183 tgt = lmv_find_target(lmv, &op_data->op_fid1);
2185 RETURN(PTR_ERR(tgt));
2187 rc = md_setattr(tgt->ltd_exp, op_data, ea, ealen, ea2,
2188 ea2len, request, mod);
2193 static int lmv_sync(struct obd_export *exp, const struct lu_fid *fid,
2194 struct obd_capa *oc, struct ptlrpc_request **request)
2196 struct obd_device *obd = exp->exp_obd;
2197 struct lmv_obd *lmv = &obd->u.lmv;
2198 struct lmv_tgt_desc *tgt;
2202 rc = lmv_check_connect(obd);
2206 tgt = lmv_find_target(lmv, fid);
2208 RETURN(PTR_ERR(tgt));
2210 rc = md_sync(tgt->ltd_exp, fid, oc, request);
2215 * Main purpose of LMV blocking ast is to remove split directory LMV
2216 * presentation object (struct lmv_object) attached to the lock being revoked.
2218 int lmv_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
2219 void *data, int flag)
2221 struct lustre_handle lockh;
2222 struct lmv_object *obj;
2227 case LDLM_CB_BLOCKING:
2228 ldlm_lock2handle(lock, &lockh);
2229 rc = ldlm_cli_cancel(&lockh);
2231 CDEBUG(D_INODE, "ldlm_cli_cancel: %d\n", rc);
2235 case LDLM_CB_CANCELING:
2237 * Time to drop cached attrs for split directory object
2239 obj = lock->l_ast_data;
2241 CDEBUG(D_INODE, "Cancel %s on "LPU64"/"LPU64
2242 ", master "DFID"\n",
2243 lock->l_resource->lr_name.name[3] == 1 ?
2244 "LOOKUP" : "UPDATE",
2245 lock->l_resource->lr_name.name[0],
2246 lock->l_resource->lr_name.name[1],
2247 PFID(&obj->lo_fid));
2248 lmv_object_put(obj);
2257 static void lmv_hash_adjust(__u64 *hash, __u64 hash_adj)
2261 val = le64_to_cpu(*hash);
2263 val += MAX_HASH_SIZE;
2264 if (val != DIR_END_OFF)
2265 *hash = cpu_to_le64(val - hash_adj);
2268 static __u32 lmv_node_rank(struct obd_export *exp, const struct lu_fid *fid)
2271 struct obd_import *imp;
2274 * XXX: to get nid we assume that underlying obd device is mdc.
2276 imp = class_exp2cliimp(exp);
2277 id = imp->imp_connection->c_self + fid_flatten(fid);
2279 CDEBUG(D_INODE, "Readpage node rank: "LPX64" "DFID" "LPX64" "LPX64"\n",
2280 imp->imp_connection->c_self, PFID(fid), id, id ^ (id >> 32));
2282 return id ^ (id >> 32);
2285 static int lmv_readpage(struct obd_export *exp, const struct lu_fid *fid,
2286 struct obd_capa *oc, __u64 offset64, struct page *page,
2287 struct ptlrpc_request **request)
2289 struct obd_device *obd = exp->exp_obd;
2290 struct lmv_obd *lmv = &obd->u.lmv;
2291 struct lu_fid rid = *fid;
2292 struct lmv_object *obj;
2302 struct lmv_stripe *los;
2303 struct lmv_tgt_desc *tgt;
2304 struct lu_dirpage *dp;
2305 struct lu_dirent *ent;
2310 rc = lmv_check_connect(obd);
2314 CDEBUG(D_INODE, "READPAGE at "LPX64" from "DFID"\n", offset, PFID(&rid));
2317 * This case handle directory lookup in clustered metadata case (i.e.
2318 * split directory is located on multiple md servers.)
2319 * each server keeps directory entries for certain range of hashes.
2320 * E.g. we have N server and suppose hash range is 0 to MAX_HASH.
2321 * first server will keep records with hashes [ 0 ... MAX_HASH / N - 1],
2322 * second one with hashes [MAX_HASH / N ... 2 * MAX_HASH / N] and
2324 * readdir can simply start reading entries from 0 - N server in
2325 * order but that will not scale well as all client will request dir in
2326 * to server in same order.
2327 * Following algorithm does optimization:
2328 * Instead of doing readdir in 1, 2, ...., N order, client with a
2329 * rank R does readdir in R, R + 1, ..., N, 1, ... R - 1 order.
2330 * (every client has rank R)
2331 * But ll_readdir() expect offset range [0 to MAX_HASH/N) but
2332 * since client ask dir from MDS{R} client has pages with offsets
2333 * [R*MAX_HASH/N ... (R + 1)*MAX_HASH/N] there for we do hash_adj
2334 * on hash values that we get.
2336 obj = lmv_object_find_lock(obd, fid);
2338 nr = obj->lo_objcount;
2340 seg_size = MAX_HASH_SIZE;
2341 do_div(seg_size, nr);
2342 los = obj->lo_stripes;
2343 tgt = lmv_get_target(lmv, los[0].ls_mds);
2344 rank = lmv_node_rank(tgt->ltd_exp, fid) % nr;
2346 do_div(tgt_tmp, seg_size);
2347 tgt0_idx = do_div(tgt_tmp, nr);
2348 tgt_idx = (tgt0_idx + rank) % nr;
2350 if (tgt_idx < tgt0_idx)
2354 * Last segment has unusual length due to division
2357 hash_adj = MAX_HASH_SIZE - seg_size * nr;
2361 hash_adj += rank * seg_size;
2363 CDEBUG(D_INODE, "Readpage hash adjustment: %x "LPX64" "
2364 LPX64"/%x -> "LPX64"/%x\n", rank, hash_adj,
2365 offset, tgt0_idx, offset + hash_adj, tgt_idx);
2367 offset = (offset + hash_adj) & MAX_HASH_SIZE;
2368 rid = obj->lo_stripes[tgt_idx].ls_fid;
2369 tgt = lmv_get_target(lmv, los[tgt_idx].ls_mds);
2371 CDEBUG(D_INODE, "Forward to "DFID" with offset %lu i %d\n",
2372 PFID(&rid), (unsigned long)offset, tgt_idx);
2374 tgt = lmv_find_target(lmv, &rid);
2377 GOTO(cleanup, rc = PTR_ERR(tgt));
2379 rc = md_readpage(tgt->ltd_exp, &rid, oc, offset, page, request);
2383 dp = cfs_kmap(page);
2385 lmv_hash_adjust(&dp->ldp_hash_start, hash_adj);
2386 lmv_hash_adjust(&dp->ldp_hash_end, hash_adj);
2387 LASSERT(le64_to_cpu(dp->ldp_hash_start) <= offset64);
2389 for (ent = lu_dirent_start(dp); ent != NULL;
2390 ent = lu_dirent_next(ent))
2391 lmv_hash_adjust(&ent->lde_hash, hash_adj);
2393 if (tgt0_idx != nr - 1) {
2396 end = le64_to_cpu(dp->ldp_hash_end);
2397 if (end == DIR_END_OFF) {
2398 dp->ldp_hash_end = cpu_to_le32(seg_size *
2401 ""DFID" reset end "LPX64" tgt %d\n",
2403 le64_to_cpu(dp->ldp_hash_end), tgt_idx);
2411 lmv_object_put_unlock(obj);
2415 static int lmv_unlink(struct obd_export *exp, struct md_op_data *op_data,
2416 struct ptlrpc_request **request)
2418 struct obd_device *obd = exp->exp_obd;
2419 struct lmv_obd *lmv = &obd->u.lmv;
2420 struct lmv_tgt_desc *tgt = NULL;
2421 struct lmv_object *obj;
2427 rc = lmv_check_connect(obd);
2434 LASSERT(op_data->op_namelen != 0);
2436 obj = lmv_object_find(obd, &op_data->op_fid1);
2438 sidx = raw_name2idx(obj->lo_hashtype,
2441 op_data->op_namelen);
2442 op_data->op_bias &= ~MDS_CHECK_SPLIT;
2443 op_data->op_fid1 = obj->lo_stripes[sidx].ls_fid;
2444 tgt = lmv_get_target(lmv,
2445 obj->lo_stripes[sidx].ls_mds);
2446 lmv_object_put(obj);
2447 CDEBUG(D_INODE, "UNLINK '%*s' in "DFID" -> %u\n",
2448 op_data->op_namelen, op_data->op_name,
2449 PFID(&op_data->op_fid1), sidx);
2453 tgt = lmv_find_target(lmv, &op_data->op_fid1);
2455 RETURN(PTR_ERR(tgt));
2456 op_data->op_bias |= MDS_CHECK_SPLIT;
2459 op_data->op_fsuid = current->fsuid;
2460 op_data->op_fsgid = current->fsgid;
2461 op_data->op_cap = cfs_curproc_cap_pack();
2464 * If child's fid is given, cancel unused locks for it if it is from
2465 * another export than parent.
2467 * LOOKUP lock for child (fid3) should also be cancelled on parent
2468 * tgt_tgt in mdc_unlink().
2470 op_data->op_flags |= MF_MDC_CANCEL_FID1 | MF_MDC_CANCEL_FID3;
2473 * Cancel FULL locks on child (fid3).
2475 rc = lmv_early_cancel(exp, op_data, tgt->ltd_idx, LCK_EX,
2476 MDS_INODELOCK_FULL, MF_MDC_CANCEL_FID3);
2479 rc = md_unlink(tgt->ltd_exp, op_data, request);
2481 if (rc == -ERESTART) {
2482 LASSERT(*request != NULL);
2483 DEBUG_REQ(D_WARNING|D_RPCTRACE, *request,
2484 "Got -ERESTART during unlink!\n");
2485 ptlrpc_req_finished(*request);
2489 * Directory got split. Time to update local object and repeat
2490 * the request with proper MDS.
2492 rc = lmv_handle_split(exp, &op_data->op_fid1);
2499 static int lmv_precleanup(struct obd_device *obd, enum obd_cleanup_stage stage)
2504 case OBD_CLEANUP_EARLY:
2505 /* XXX: here should be calling obd_precleanup() down to
2508 case OBD_CLEANUP_EXPORTS:
2509 rc = obd_llog_finish(obd, 0);
2511 CERROR("failed to cleanup llogging subsystems\n");
2519 static int lmv_get_info(struct obd_export *exp, __u32 keylen,
2520 void *key, __u32 *vallen, void *val,
2521 struct lov_stripe_md *lsm)
2523 struct obd_device *obd;
2524 struct lmv_obd *lmv;
2528 obd = class_exp2obd(exp);
2530 CDEBUG(D_IOCTL, "Invalid client cookie "LPX64"\n",
2531 exp->exp_handle.h_cookie);
2536 if (keylen >= strlen("remote_flag") && !strcmp(key, "remote_flag")) {
2537 struct lmv_tgt_desc *tgts;
2540 rc = lmv_check_connect(obd);
2544 LASSERT(*vallen == sizeof(__u32));
2545 for (i = 0, tgts = lmv->tgts; i < lmv->desc.ld_tgt_count;
2549 * All tgts should be connected when this gets called.
2551 if (!tgts || !tgts->ltd_exp) {
2552 CERROR("target not setup?\n");
2556 if (!obd_get_info(tgts->ltd_exp, keylen, key,
2561 } else if (KEY_IS(KEY_MAX_EASIZE) || KEY_IS(KEY_CONN_DATA)) {
2562 rc = lmv_check_connect(obd);
2567 * Forwarding this request to first MDS, it should know LOV
2570 rc = obd_get_info(lmv->tgts[0].ltd_exp, keylen, key,
2572 if (!rc && KEY_IS(KEY_CONN_DATA)) {
2573 exp->exp_connect_flags =
2574 ((struct obd_connect_data *)val)->ocd_connect_flags;
2579 CDEBUG(D_IOCTL, "Invalid key\n");
2583 int lmv_set_info_async(struct obd_export *exp, obd_count keylen,
2584 void *key, obd_count vallen, void *val,
2585 struct ptlrpc_request_set *set)
2587 struct lmv_tgt_desc *tgt;
2588 struct obd_device *obd;
2589 struct lmv_obd *lmv;
2593 obd = class_exp2obd(exp);
2595 CDEBUG(D_IOCTL, "Invalid client cookie "LPX64"\n",
2596 exp->exp_handle.h_cookie);
2601 if (KEY_IS(KEY_READ_ONLY) || KEY_IS(KEY_FLUSH_CTX) ||
2602 KEY_IS(KEY_INIT_RECOV_BACKUP)) {
2605 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
2606 tgt = &lmv->tgts[i];
2611 err = obd_set_info_async(tgt->ltd_exp,
2612 keylen, key, vallen, val, set);
2623 int lmv_packmd(struct obd_export *exp, struct lov_mds_md **lmmp,
2624 struct lov_stripe_md *lsm)
2626 struct obd_device *obd = class_exp2obd(exp);
2627 struct lmv_obd *lmv = &obd->u.lmv;
2628 struct lmv_stripe_md *meap;
2629 struct lmv_stripe_md *lsmp;
2634 mea_size = lmv_get_easize(lmv);
2638 if (*lmmp && !lsm) {
2639 OBD_FREE(*lmmp, mea_size);
2644 if (*lmmp == NULL) {
2645 OBD_ALLOC(*lmmp, mea_size);
2653 lsmp = (struct lmv_stripe_md *)lsm;
2654 meap = (struct lmv_stripe_md *)*lmmp;
2656 if (lsmp->mea_magic != MEA_MAGIC_LAST_CHAR &&
2657 lsmp->mea_magic != MEA_MAGIC_ALL_CHARS)
2660 meap->mea_magic = cpu_to_le32(lsmp->mea_magic);
2661 meap->mea_count = cpu_to_le32(lsmp->mea_count);
2662 meap->mea_master = cpu_to_le32(lsmp->mea_master);
2664 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
2665 meap->mea_ids[i] = meap->mea_ids[i];
2666 fid_cpu_to_le(&meap->mea_ids[i], &meap->mea_ids[i]);
2672 int lmv_unpackmd(struct obd_export *exp, struct lov_stripe_md **lsmp,
2673 struct lov_mds_md *lmm, int lmm_size)
2675 struct obd_device *obd = class_exp2obd(exp);
2676 struct lmv_stripe_md **tmea = (struct lmv_stripe_md **)lsmp;
2677 struct lmv_stripe_md *mea = (struct lmv_stripe_md *)lmm;
2678 struct lmv_obd *lmv = &obd->u.lmv;
2684 mea_size = lmv_get_easize(lmv);
2688 if (*lsmp != NULL && lmm == NULL) {
2689 OBD_FREE(*tmea, mea_size);
2694 LASSERT(mea_size == lmm_size);
2696 OBD_ALLOC(*tmea, mea_size);
2703 if (mea->mea_magic == MEA_MAGIC_LAST_CHAR ||
2704 mea->mea_magic == MEA_MAGIC_ALL_CHARS ||
2705 mea->mea_magic == MEA_MAGIC_HASH_SEGMENT)
2707 magic = le32_to_cpu(mea->mea_magic);
2710 * Old mea is not handled here.
2712 CERROR("Old not supportable EA is found\n");
2716 (*tmea)->mea_magic = magic;
2717 (*tmea)->mea_count = le32_to_cpu(mea->mea_count);
2718 (*tmea)->mea_master = le32_to_cpu(mea->mea_master);
2720 for (i = 0; i < (*tmea)->mea_count; i++) {
2721 (*tmea)->mea_ids[i] = mea->mea_ids[i];
2722 fid_le_to_cpu(&(*tmea)->mea_ids[i], &(*tmea)->mea_ids[i]);
2727 static int lmv_cancel_unused(struct obd_export *exp, const struct lu_fid *fid,
2728 ldlm_policy_data_t *policy, ldlm_mode_t mode,
2729 int flags, void *opaque)
2731 struct obd_device *obd = exp->exp_obd;
2732 struct lmv_obd *lmv = &obd->u.lmv;
2738 LASSERT(fid != NULL);
2740 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
2741 if (!lmv->tgts[i].ltd_exp || !lmv->tgts[i].ltd_active)
2744 err = md_cancel_unused(lmv->tgts[i].ltd_exp, fid,
2745 policy, mode, flags, opaque);
2752 int lmv_set_lock_data(struct obd_export *exp, __u64 *lockh, void *data)
2754 struct obd_device *obd = exp->exp_obd;
2755 struct lmv_obd *lmv = &obd->u.lmv;
2758 rc = md_set_lock_data(lmv->tgts[0].ltd_exp, lockh, data);
2762 ldlm_mode_t lmv_lock_match(struct obd_export *exp, int flags,
2763 const struct lu_fid *fid, ldlm_type_t type,
2764 ldlm_policy_data_t *policy, ldlm_mode_t mode,
2765 struct lustre_handle *lockh)
2767 struct obd_device *obd = exp->exp_obd;
2768 struct lmv_obd *lmv = &obd->u.lmv;
2773 CDEBUG(D_INODE, "Lock match for "DFID"\n", PFID(fid));
2776 * With CMD every object can have two locks in different namespaces:
2777 * lookup lock in space of mds storing direntry and update/open lock in
2778 * space of mds storing inode. Thus we check all targets, not only that
2779 * one fid was created in.
2781 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
2782 rc = md_lock_match(lmv->tgts[i].ltd_exp, flags, fid,
2783 type, policy, mode, lockh);
2791 int lmv_get_lustre_md(struct obd_export *exp, struct ptlrpc_request *req,
2792 struct obd_export *dt_exp, struct obd_export *md_exp,
2793 struct lustre_md *md)
2795 struct obd_device *obd = exp->exp_obd;
2796 struct lmv_obd *lmv = &obd->u.lmv;
2799 rc = md_get_lustre_md(lmv->tgts[0].ltd_exp, req, dt_exp, md_exp, md);
2803 int lmv_free_lustre_md(struct obd_export *exp, struct lustre_md *md)
2805 struct obd_device *obd = exp->exp_obd;
2806 struct lmv_obd *lmv = &obd->u.lmv;
2810 obd_free_memmd(exp, (void *)&md->mea);
2811 RETURN(md_free_lustre_md(lmv->tgts[0].ltd_exp, md));
2814 int lmv_set_open_replay_data(struct obd_export *exp,
2815 struct obd_client_handle *och,
2816 struct ptlrpc_request *open_req)
2818 struct obd_device *obd = exp->exp_obd;
2819 struct lmv_obd *lmv = &obd->u.lmv;
2820 struct lmv_tgt_desc *tgt;
2823 tgt = lmv_find_target(lmv, &och->och_fid);
2825 RETURN(PTR_ERR(tgt));
2827 RETURN(md_set_open_replay_data(tgt->ltd_exp, och, open_req));
2830 int lmv_clear_open_replay_data(struct obd_export *exp,
2831 struct obd_client_handle *och)
2833 struct obd_device *obd = exp->exp_obd;
2834 struct lmv_obd *lmv = &obd->u.lmv;
2835 struct lmv_tgt_desc *tgt;
2838 tgt = lmv_find_target(lmv, &och->och_fid);
2840 RETURN(PTR_ERR(tgt));
2842 RETURN(md_clear_open_replay_data(tgt->ltd_exp, och));
2845 static int lmv_get_remote_perm(struct obd_export *exp,
2846 const struct lu_fid *fid,
2847 struct obd_capa *oc, __u32 suppgid,
2848 struct ptlrpc_request **request)
2850 struct obd_device *obd = exp->exp_obd;
2851 struct lmv_obd *lmv = &obd->u.lmv;
2852 struct lmv_tgt_desc *tgt;
2856 rc = lmv_check_connect(obd);
2860 tgt = lmv_find_target(lmv, fid);
2862 RETURN(PTR_ERR(tgt));
2864 rc = md_get_remote_perm(tgt->ltd_exp, fid, oc, suppgid, request);
2868 static int lmv_renew_capa(struct obd_export *exp, struct obd_capa *oc,
2871 struct obd_device *obd = exp->exp_obd;
2872 struct lmv_obd *lmv = &obd->u.lmv;
2873 struct lmv_tgt_desc *tgt;
2877 rc = lmv_check_connect(obd);
2881 tgt = lmv_find_target(lmv, &oc->c_capa.lc_fid);
2883 RETURN(PTR_ERR(tgt));
2885 rc = md_renew_capa(tgt->ltd_exp, oc, cb);
2889 int lmv_unpack_capa(struct obd_export *exp, struct ptlrpc_request *req,
2890 const struct req_msg_field *field, struct obd_capa **oc)
2892 struct obd_device *obd = exp->exp_obd;
2893 struct lmv_obd *lmv = &obd->u.lmv;
2897 rc = md_unpack_capa(lmv->tgts[0].ltd_exp, req, field, oc);
2901 int lmv_intent_getattr_async(struct obd_export *exp,
2902 struct md_enqueue_info *minfo,
2903 struct ldlm_enqueue_info *einfo)
2905 struct md_op_data *op_data = &minfo->mi_data;
2906 struct obd_device *obd = exp->exp_obd;
2907 struct lmv_obd *lmv = &obd->u.lmv;
2908 struct lmv_object *obj;
2909 struct lmv_tgt_desc *tgt;
2914 rc = lmv_check_connect(obd);
2918 if (!fid_is_sane(&op_data->op_fid2)) {
2919 obj = lmv_object_find(obd, &op_data->op_fid1);
2920 if (obj && op_data->op_namelen) {
2921 sidx = raw_name2idx(obj->lo_hashtype,
2923 (char *)op_data->op_name,
2924 op_data->op_namelen);
2925 op_data->op_fid1 = obj->lo_stripes[sidx].ls_fid;
2926 tgt = lmv_get_target(lmv,
2927 obj->lo_stripes[sidx].ls_mds);
2929 "Choose slave dir ("DFID") -> mds #%d\n",
2930 PFID(&op_data->op_fid1), tgt->ltd_idx);
2932 tgt = lmv_find_target(lmv, &op_data->op_fid1);
2935 lmv_object_put(obj);
2937 op_data->op_fid1 = op_data->op_fid2;
2938 tgt = lmv_find_target(lmv, &op_data->op_fid2);
2939 op_data->op_bias = MDS_CROSS_REF;
2941 * Unfortunately, we have to lie to MDC/MDS to retrieve
2942 * attributes llite needs.
2944 if (minfo->mi_it.it_op & IT_LOOKUP)
2945 minfo->mi_it.it_op = IT_GETATTR;
2949 RETURN(PTR_ERR(tgt));
2951 rc = md_intent_getattr_async(tgt->ltd_exp, minfo, einfo);
2955 int lmv_revalidate_lock(struct obd_export *exp,
2956 struct lookup_intent *it,
2959 struct obd_device *obd = exp->exp_obd;
2960 struct lmv_obd *lmv = &obd->u.lmv;
2961 struct lmv_tgt_desc *tgt;
2965 rc = lmv_check_connect(obd);
2969 tgt = lmv_find_target(lmv, fid);
2971 RETURN(PTR_ERR(tgt));
2973 rc = md_revalidate_lock(tgt->ltd_exp, it, fid);
2978 struct obd_ops lmv_obd_ops = {
2979 .o_owner = THIS_MODULE,
2980 .o_setup = lmv_setup,
2981 .o_cleanup = lmv_cleanup,
2982 .o_precleanup = lmv_precleanup,
2983 .o_process_config = lmv_process_config,
2984 .o_connect = lmv_connect,
2985 .o_disconnect = lmv_disconnect,
2986 .o_statfs = lmv_statfs,
2987 .o_get_info = lmv_get_info,
2988 .o_set_info_async = lmv_set_info_async,
2989 .o_packmd = lmv_packmd,
2990 .o_unpackmd = lmv_unpackmd,
2991 .o_notify = lmv_notify,
2992 .o_get_uuid = lmv_get_uuid,
2993 .o_iocontrol = lmv_iocontrol,
2994 .o_fid_delete = lmv_fid_delete
2997 struct md_ops lmv_md_ops = {
2998 .m_getstatus = lmv_getstatus,
2999 .m_change_cbdata = lmv_change_cbdata,
3000 .m_close = lmv_close,
3001 .m_create = lmv_create,
3002 .m_done_writing = lmv_done_writing,
3003 .m_enqueue = lmv_enqueue,
3004 .m_getattr = lmv_getattr,
3005 .m_getxattr = lmv_getxattr,
3006 .m_getattr_name = lmv_getattr_name,
3007 .m_intent_lock = lmv_intent_lock,
3009 .m_rename = lmv_rename,
3010 .m_setattr = lmv_setattr,
3011 .m_setxattr = lmv_setxattr,
3013 .m_readpage = lmv_readpage,
3014 .m_unlink = lmv_unlink,
3015 .m_init_ea_size = lmv_init_ea_size,
3016 .m_cancel_unused = lmv_cancel_unused,
3017 .m_set_lock_data = lmv_set_lock_data,
3018 .m_lock_match = lmv_lock_match,
3019 .m_get_lustre_md = lmv_get_lustre_md,
3020 .m_free_lustre_md = lmv_free_lustre_md,
3021 .m_set_open_replay_data = lmv_set_open_replay_data,
3022 .m_clear_open_replay_data = lmv_clear_open_replay_data,
3023 .m_renew_capa = lmv_renew_capa,
3024 .m_unpack_capa = lmv_unpack_capa,
3025 .m_get_remote_perm = lmv_get_remote_perm,
3026 .m_intent_getattr_async = lmv_intent_getattr_async,
3027 .m_revalidate_lock = lmv_revalidate_lock
3030 static quota_interface_t *quota_interface;
3031 extern quota_interface_t lmv_quota_interface;
3033 int __init lmv_init(void)
3035 struct lprocfs_static_vars lvars;
3038 lmv_object_cache = cfs_mem_cache_create("lmv_objects",
3039 sizeof(struct lmv_object),
3041 if (!lmv_object_cache) {
3042 CERROR("Error allocating lmv objects cache\n");
3046 lprocfs_lmv_init_vars(&lvars);
3048 request_module("lquota");
3049 quota_interface = PORTAL_SYMBOL_GET(lmv_quota_interface);
3050 init_obd_quota_ops(quota_interface, &lmv_obd_ops);
3052 rc = class_register_type(&lmv_obd_ops, &lmv_md_ops,
3053 lvars.module_vars, LUSTRE_LMV_NAME, NULL);
3055 if (quota_interface)
3056 PORTAL_SYMBOL_PUT(lmv_quota_interface);
3057 cfs_mem_cache_destroy(lmv_object_cache);
3064 static void lmv_exit(void)
3066 if (quota_interface)
3067 PORTAL_SYMBOL_PUT(lmv_quota_interface);
3069 class_unregister_type(LUSTRE_LMV_NAME);
3071 LASSERTF(atomic_read(&lmv_object_count) == 0,
3072 "Can't free lmv objects cache, %d object(s) busy\n",
3073 atomic_read(&lmv_object_count));
3074 cfs_mem_cache_destroy(lmv_object_cache);
3077 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
3078 MODULE_DESCRIPTION("Lustre Logical Metadata Volume OBD driver");
3079 MODULE_LICENSE("GPL");
3081 module_init(lmv_init);
3082 module_exit(lmv_exit);