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 obd_export **exp, 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 lustre_handle conn = { 0 };
255 * We don't want to actually do the underlying connections more than
256 * once, so keep track.
259 if (lmv->refcount > 1) {
264 rc = class_connect(&conn, obd, cluuid);
266 CERROR("class_connection() returned %d\n", rc);
270 *exp = class_conn2export(&conn);
271 class_export_get(*exp);
275 lmv->cluuid = *cluuid;
278 lmv->conn_data = *data;
281 lmv_proc_dir = lprocfs_register("target_obds", obd->obd_proc_entry,
283 if (IS_ERR(lmv_proc_dir)) {
284 CERROR("could not register /proc/fs/lustre/%s/%s/target_obds.",
285 obd->obd_type->typ_name, obd->obd_name);
291 * All real clients should perform actual connection right away, because
292 * it is possible, that LMV will not have opportunity to connect targets
293 * and MDC stuff will be called directly, for instance while reading
294 * ../mdc/../kbytesfree procfs file, etc.
296 if (data->ocd_connect_flags & OBD_CONNECT_REAL)
297 rc = lmv_check_connect(obd);
302 lprocfs_remove(&lmv_proc_dir);
309 static void lmv_set_timeouts(struct obd_device *obd)
311 struct lmv_tgt_desc *tgts;
316 if (lmv->server_timeout == 0)
319 if (lmv->connected == 0)
322 for (i = 0, tgts = lmv->tgts; i < lmv->desc.ld_tgt_count; i++, tgts++) {
323 if (tgts->ltd_exp == NULL)
326 obd_set_info_async(tgts->ltd_exp, sizeof(KEY_INTERMDS),
327 KEY_INTERMDS, 0, NULL, NULL);
331 static int lmv_init_ea_size(struct obd_export *exp, int easize,
332 int def_easize, int cookiesize)
334 struct obd_device *obd = exp->exp_obd;
335 struct lmv_obd *lmv = &obd->u.lmv;
341 if (lmv->max_easize < easize) {
342 lmv->max_easize = easize;
345 if (lmv->max_def_easize < def_easize) {
346 lmv->max_def_easize = def_easize;
349 if (lmv->max_cookiesize < cookiesize) {
350 lmv->max_cookiesize = cookiesize;
356 if (lmv->connected == 0)
359 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
360 if (lmv->tgts[i].ltd_exp == NULL) {
361 CWARN("%s: NULL export for %d\n", obd->obd_name, i);
365 rc = md_init_ea_size(lmv->tgts[i].ltd_exp, easize, def_easize,
368 CERROR("obd_init_ea_size() failed on MDT target %d, "
369 "error %d.\n", i, rc);
376 #define MAX_STRING_SIZE 128
378 int lmv_connect_mdc(struct obd_device *obd, struct lmv_tgt_desc *tgt)
381 struct proc_dir_entry *lmv_proc_dir;
383 struct lmv_obd *lmv = &obd->u.lmv;
384 struct obd_uuid *cluuid = &lmv->cluuid;
385 struct obd_connect_data *mdc_data = NULL;
386 struct obd_uuid lmv_mdc_uuid = { "LMV_MDC_UUID" };
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, &mdc_exp, mdc_obd, &lmv_mdc_uuid,
411 &lmv->conn_data, NULL);
413 CERROR("target %s connect error %d\n", tgt->ltd_uuid.uuid, rc);
418 * Init fid sequence client for this mdc and add new fld target.
420 rc = obd_fid_init(mdc_exp);
424 target.ft_srv = NULL;
425 target.ft_exp = mdc_exp;
426 target.ft_idx = tgt->ltd_idx;
428 fld_client_add_target(&lmv->lmv_fld, &target);
430 mdc_data = &class_exp2cliimp(mdc_exp)->imp_connect_data;
432 rc = obd_register_observer(mdc_obd, obd);
434 obd_disconnect(mdc_exp);
435 CERROR("target %s register_observer error %d\n",
436 tgt->ltd_uuid.uuid, rc);
440 if (obd->obd_observer) {
442 * Tell the observer about the new target.
444 rc = obd_notify(obd->obd_observer, mdc_exp->exp_obd,
445 OBD_NOTIFY_ACTIVE, (void *)(tgt - lmv->tgts));
447 obd_disconnect(mdc_exp);
453 tgt->ltd_exp = mdc_exp;
454 lmv->desc.ld_active_tgt_count++;
457 * Copy connect data, it may be used later.
459 lmv->datas[tgt->ltd_idx] = *mdc_data;
461 md_init_ea_size(tgt->ltd_exp, lmv->max_easize,
462 lmv->max_def_easize, lmv->max_cookiesize);
464 CDEBUG(D_CONFIG, "Connected to %s(%s) successfully (%d)\n",
465 mdc_obd->obd_name, mdc_obd->obd_uuid.uuid,
466 atomic_read(&obd->obd_refcount));
469 lmv_proc_dir = lprocfs_srch(obd->obd_proc_entry, "target_obds");
471 struct proc_dir_entry *mdc_symlink;
473 LASSERT(mdc_obd->obd_type != NULL);
474 LASSERT(mdc_obd->obd_type->typ_name != NULL);
475 mdc_symlink = lprocfs_add_symlink(mdc_obd->obd_name,
478 mdc_obd->obd_type->typ_name,
480 if (mdc_symlink == NULL) {
481 CERROR("Could not register LMV target "
482 "/proc/fs/lustre/%s/%s/target_obds/%s.",
483 obd->obd_type->typ_name, obd->obd_name,
485 lprocfs_remove(&lmv_proc_dir);
493 int lmv_add_target(struct obd_device *obd, struct obd_uuid *tgt_uuid)
495 struct lmv_obd *lmv = &obd->u.lmv;
496 struct lmv_tgt_desc *tgt;
500 CDEBUG(D_CONFIG, "Target uuid: %s.\n", tgt_uuid->uuid);
504 if (lmv->desc.ld_active_tgt_count >= LMV_MAX_TGT_COUNT) {
505 lmv_init_unlock(lmv);
506 CERROR("Can't add %s, LMV module compiled for %d MDCs. "
507 "That many MDCs already configured.\n",
508 tgt_uuid->uuid, LMV_MAX_TGT_COUNT);
511 if (lmv->desc.ld_tgt_count == 0) {
512 struct obd_device *mdc_obd;
514 mdc_obd = class_find_client_obd(tgt_uuid, LUSTRE_MDC_NAME,
517 lmv_init_unlock(lmv);
518 CERROR("Target %s not attached\n", tgt_uuid->uuid);
522 rc = obd_llog_init(obd, &obd->obd_olg, mdc_obd, NULL);
524 lmv_init_unlock(lmv);
525 CERROR("lmv failed to setup llogging subsystems\n");
528 spin_lock(&lmv->lmv_lock);
529 tgt = lmv->tgts + lmv->desc.ld_tgt_count++;
530 tgt->ltd_uuid = *tgt_uuid;
531 spin_unlock(&lmv->lmv_lock);
533 if (lmv->connected) {
534 rc = lmv_connect_mdc(obd, tgt);
536 spin_lock(&lmv->lmv_lock);
537 lmv->desc.ld_tgt_count--;
538 memset(tgt, 0, sizeof(*tgt));
539 spin_unlock(&lmv->lmv_lock);
541 int easize = sizeof(struct lmv_stripe_md) +
542 lmv->desc.ld_tgt_count *
543 sizeof(struct lu_fid);
544 lmv_init_ea_size(obd->obd_self_export, easize, 0, 0);
548 lmv_init_unlock(lmv);
552 int lmv_check_connect(struct obd_device *obd)
554 struct lmv_obd *lmv = &obd->u.lmv;
555 struct lmv_tgt_desc *tgt;
565 if (lmv->connected) {
566 lmv_init_unlock(lmv);
570 if (lmv->desc.ld_tgt_count == 0) {
571 CERROR("%s: no targets configured.\n", obd->obd_name);
575 CDEBUG(D_CONFIG, "Time to connect %s to %s\n",
576 lmv->cluuid.uuid, obd->obd_name);
578 LASSERT(lmv->tgts != NULL);
580 for (i = 0, tgt = lmv->tgts; i < lmv->desc.ld_tgt_count; i++, tgt++) {
581 rc = lmv_connect_mdc(obd, tgt);
586 lmv_set_timeouts(obd);
587 class_export_put(lmv->exp);
589 easize = lmv_get_easize(lmv);
590 lmv_init_ea_size(obd->obd_self_export, easize, 0, 0);
591 lmv_init_unlock(lmv);
600 --lmv->desc.ld_active_tgt_count;
601 rc2 = obd_disconnect(tgt->ltd_exp);
603 CERROR("LMV target %s disconnect on "
604 "MDC idx %d: error %d\n",
605 tgt->ltd_uuid.uuid, i, rc2);
609 class_disconnect(lmv->exp);
610 lmv_init_unlock(lmv);
614 static int lmv_disconnect_mdc(struct obd_device *obd, struct lmv_tgt_desc *tgt)
617 struct proc_dir_entry *lmv_proc_dir;
619 struct lmv_obd *lmv = &obd->u.lmv;
620 struct obd_device *mdc_obd;
624 LASSERT(tgt != NULL);
625 LASSERT(obd != NULL);
627 mdc_obd = class_exp2obd(tgt->ltd_exp);
630 mdc_obd->obd_no_recov = obd->obd_no_recov;
633 lmv_proc_dir = lprocfs_srch(obd->obd_proc_entry, "target_obds");
635 struct proc_dir_entry *mdc_symlink;
637 mdc_symlink = lprocfs_srch(lmv_proc_dir, mdc_obd->obd_name);
639 lprocfs_remove(&mdc_symlink);
641 CERROR("/proc/fs/lustre/%s/%s/target_obds/%s missing\n",
642 obd->obd_type->typ_name, obd->obd_name,
647 rc = obd_fid_fini(tgt->ltd_exp);
649 CERROR("Can't finanize fids factory\n");
651 CDEBUG(D_INFO, "Disconnected from %s(%s) successfully\n",
652 tgt->ltd_exp->exp_obd->obd_name,
653 tgt->ltd_exp->exp_obd->obd_uuid.uuid);
655 obd_register_observer(tgt->ltd_exp->exp_obd, NULL);
656 rc = obd_disconnect(tgt->ltd_exp);
658 if (tgt->ltd_active) {
659 CERROR("Target %s disconnect error %d\n",
660 tgt->ltd_uuid.uuid, rc);
664 lmv_activate_target(lmv, tgt, 0);
669 static int lmv_disconnect(struct obd_export *exp)
671 struct obd_device *obd = class_exp2obd(exp);
673 struct proc_dir_entry *lmv_proc_dir;
675 struct lmv_obd *lmv = &obd->u.lmv;
684 * Only disconnect the underlying layers on the final disconnect.
687 if (lmv->refcount != 0)
690 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
691 if (lmv->tgts[i].ltd_exp == NULL)
693 lmv_disconnect_mdc(obd, &lmv->tgts[i]);
697 lmv_proc_dir = lprocfs_srch(obd->obd_proc_entry, "target_obds");
699 lprocfs_remove(&lmv_proc_dir);
701 CERROR("/proc/fs/lustre/%s/%s/target_obds missing\n",
702 obd->obd_type->typ_name, obd->obd_name);
708 * This is the case when no real connection is established by
709 * lmv_check_connect().
712 class_export_put(exp);
713 rc = class_disconnect(exp);
714 if (lmv->refcount == 0)
719 static int lmv_iocontrol(unsigned int cmd, struct obd_export *exp,
720 int len, void *karg, void *uarg)
722 struct obd_device *obddev = class_exp2obd(exp);
723 struct lmv_obd *lmv = &obddev->u.lmv;
727 int count = lmv->desc.ld_tgt_count;
734 case IOC_OBD_STATFS: {
735 struct obd_ioctl_data *data = karg;
736 struct obd_device *mdc_obd;
737 struct obd_statfs stat_buf = {0};
740 memcpy(&index, data->ioc_inlbuf2, sizeof(__u32));
741 LASSERT(data->ioc_plen1 == sizeof(struct obd_statfs));
743 if ((index >= count))
746 if (!lmv->tgts[index].ltd_active)
749 mdc_obd = class_exp2obd(lmv->tgts[index].ltd_exp);
753 rc = obd_statfs(mdc_obd, &stat_buf,
754 cfs_time_current_64() - HZ, 0);
757 if (copy_to_user(data->ioc_pbuf1, &stat_buf, data->ioc_plen1))
759 if (copy_to_user(data->ioc_pbuf2, obd2cli_tgt(mdc_obd),
764 case OBD_IOC_QUOTACTL: {
765 struct if_quotactl *qctl = karg;
766 struct lmv_tgt_desc *tgt = NULL;
767 struct obd_quotactl *oqctl;
769 if (qctl->qc_valid == QC_MDTIDX) {
770 if (qctl->qc_idx < 0 || count <= qctl->qc_idx)
773 tgt = &lmv->tgts[qctl->qc_idx];
776 } else if (qctl->qc_valid == QC_UUID) {
777 for (i = 0; i < count; i++) {
779 if (!obd_uuid_equals(&tgt->ltd_uuid,
783 if (tgt->ltd_exp == NULL)
795 LASSERT(tgt && tgt->ltd_exp);
796 OBD_ALLOC_PTR(oqctl);
800 QCTL_COPY(oqctl, qctl);
801 rc = obd_quotactl(tgt->ltd_exp, oqctl);
803 QCTL_COPY(qctl, oqctl);
804 qctl->qc_valid = QC_MDTIDX;
805 qctl->obd_uuid = tgt->ltd_uuid;
810 case OBD_IOC_CHANGELOG_CLEAR: {
811 struct ioc_changelog_clear *icc = karg;
813 if (icc->icc_mdtindex >= count)
816 rc = obd_iocontrol(cmd, lmv->tgts[icc->icc_mdtindex].ltd_exp,
817 sizeof(*icc), icc, NULL);
822 for (i = 0; i < count; i++) {
825 if (lmv->tgts[i].ltd_exp == NULL)
828 err = obd_iocontrol(cmd, lmv->tgts[i].ltd_exp, len,
830 if (err == -ENODATA && cmd == OBD_IOC_POLL_QUOTACHECK) {
833 if (lmv->tgts[i].ltd_active) {
834 CERROR("error: iocontrol MDC %s on MDT"
835 "idx %d cmd %x: err = %d\n",
836 lmv->tgts[i].ltd_uuid.uuid,
851 static int lmv_all_chars_policy(int count, const char *name,
862 static int lmv_nid_policy(struct lmv_obd *lmv)
864 struct obd_import *imp;
868 * XXX: To get nid we assume that underlying obd device is mdc.
870 imp = class_exp2cliimp(lmv->tgts[0].ltd_exp);
871 id = imp->imp_connection->c_self ^ (imp->imp_connection->c_self >> 32);
872 return id % lmv->desc.ld_tgt_count;
875 static int lmv_choose_mds(struct lmv_obd *lmv, struct md_op_data *op_data,
876 placement_policy_t placement)
879 case PLACEMENT_CHAR_POLICY:
880 return lmv_all_chars_policy(lmv->desc.ld_tgt_count,
882 op_data->op_namelen);
883 case PLACEMENT_NID_POLICY:
884 return lmv_nid_policy(lmv);
890 CERROR("Unsupported placement policy %x\n", placement);
895 * This is _inode_ placement policy function (not name).
897 static int lmv_placement_policy(struct obd_device *obd,
898 struct md_op_data *op_data,
901 struct lmv_obd *lmv = &obd->u.lmv;
902 struct lmv_object *obj;
906 LASSERT(mds != NULL);
908 if (lmv->desc.ld_tgt_count == 1) {
914 * Allocate new fid on target according to operation type and parent
917 obj = lmv_object_find(obd, &op_data->op_fid1);
918 if (obj != NULL || op_data->op_name == NULL ||
919 op_data->op_opc != LUSTRE_OPC_MKDIR) {
921 * Allocate fid for non-dir or for null name or for case parent
928 * If we have this flag turned on, and we see that
929 * parent dir is split, this means, that caller did not
930 * notice split yet. This is race and we would like to
931 * let caller know that.
933 if (op_data->op_bias & MDS_CHECK_SPLIT)
938 * Allocate new fid on same mds where parent fid is located and
939 * where operation will be sent. In case of split dir, ->op_fid1
940 * and ->op_mds here will contain fid and mds of slave directory
941 * object (assigned by caller).
943 *mds = op_data->op_mds;
947 * Parent directory is not split and we want to create a
948 * directory in it. Let's calculate where to place it according
949 * to operation data @op_data.
951 *mds = lmv_choose_mds(lmv, op_data, lmv->lmv_placement);
956 CERROR("Can't choose MDS, err = %d\n", rc);
958 LASSERT(*mds < lmv->desc.ld_tgt_count);
964 int __lmv_fid_alloc(struct lmv_obd *lmv, struct lu_fid *fid,
967 struct lmv_tgt_desc *tgt;
971 tgt = lmv_get_target(lmv, mds);
974 * New seq alloc and FLD setup should be atomic. Otherwise we may find
975 * on server that seq in new allocated fid is not yet known.
977 down(&tgt->ltd_fid_sem);
979 if (!tgt->ltd_active)
980 GOTO(out, rc = -ENODEV);
983 * Asking underlaying tgt layer to allocate new fid.
985 rc = obd_fid_alloc(tgt->ltd_exp, fid, NULL);
987 LASSERT(fid_is_sane(fid));
993 up(&tgt->ltd_fid_sem);
997 int lmv_fid_alloc(struct obd_export *exp, struct lu_fid *fid,
998 struct md_op_data *op_data)
1000 struct obd_device *obd = class_exp2obd(exp);
1001 struct lmv_obd *lmv = &obd->u.lmv;
1006 LASSERT(op_data != NULL);
1007 LASSERT(fid != NULL);
1009 rc = lmv_placement_policy(obd, op_data, &mds);
1011 CERROR("Can't get target for allocating fid, "
1016 rc = __lmv_fid_alloc(lmv, fid, mds);
1018 CERROR("Can't alloc new fid, rc %d\n", rc);
1025 static int lmv_fid_delete(struct obd_export *exp, const struct lu_fid *fid)
1028 LASSERT(exp != NULL && fid != NULL);
1029 if (lmv_object_delete(exp, fid)) {
1030 CDEBUG(D_INODE, "Object "DFID" is destroyed.\n",
1036 static int lmv_setup(struct obd_device *obd, struct lustre_cfg *lcfg)
1038 struct lmv_obd *lmv = &obd->u.lmv;
1039 struct lprocfs_static_vars lvars;
1040 struct lmv_desc *desc;
1045 if (LUSTRE_CFG_BUFLEN(lcfg, 1) < 1) {
1046 CERROR("LMV setup requires a descriptor\n");
1050 desc = (struct lmv_desc *)lustre_cfg_buf(lcfg, 1);
1051 if (sizeof(*desc) > LUSTRE_CFG_BUFLEN(lcfg, 1)) {
1052 CERROR("Lmv descriptor size wrong: %d > %d\n",
1053 (int)sizeof(*desc), LUSTRE_CFG_BUFLEN(lcfg, 1));
1057 lmv->tgts_size = LMV_MAX_TGT_COUNT * sizeof(struct lmv_tgt_desc);
1059 OBD_ALLOC(lmv->tgts, lmv->tgts_size);
1060 if (lmv->tgts == NULL)
1063 for (i = 0; i < LMV_MAX_TGT_COUNT; i++) {
1064 sema_init(&lmv->tgts[i].ltd_fid_sem, 1);
1065 lmv->tgts[i].ltd_idx = i;
1068 lmv->datas_size = LMV_MAX_TGT_COUNT * sizeof(struct obd_connect_data);
1070 OBD_ALLOC(lmv->datas, lmv->datas_size);
1071 if (lmv->datas == NULL)
1072 GOTO(out_free_tgts, rc = -ENOMEM);
1074 obd_str2uuid(&lmv->desc.ld_uuid, desc->ld_uuid.uuid);
1075 lmv->desc.ld_tgt_count = 0;
1076 lmv->desc.ld_active_tgt_count = 0;
1077 lmv->max_cookiesize = 0;
1078 lmv->max_def_easize = 0;
1079 lmv->max_easize = 0;
1080 lmv->lmv_placement = PLACEMENT_CHAR_POLICY;
1082 spin_lock_init(&lmv->lmv_lock);
1083 sema_init(&lmv->init_sem, 1);
1085 rc = lmv_object_setup(obd);
1087 CERROR("Can't setup LMV object manager, error %d.\n", rc);
1088 GOTO(out_free_datas, rc);
1091 lprocfs_lmv_init_vars(&lvars);
1092 lprocfs_obd_setup(obd, lvars.obd_vars);
1095 rc = lprocfs_seq_create(obd->obd_proc_entry, "target_obd_status",
1096 0444, &lmv_proc_target_fops, obd);
1098 CWARN("Error adding target_obd_stats file (%d)\n", rc);
1101 rc = fld_client_init(&lmv->lmv_fld, obd->obd_name,
1102 LUSTRE_CLI_FLD_HASH_DHT);
1104 CERROR("Can't init FLD, err %d\n", rc);
1105 GOTO(out_free_datas, rc);
1111 OBD_FREE(lmv->datas, lmv->datas_size);
1114 OBD_FREE(lmv->tgts, lmv->tgts_size);
1119 static int lmv_cleanup(struct obd_device *obd)
1121 struct lmv_obd *lmv = &obd->u.lmv;
1124 fld_client_fini(&lmv->lmv_fld);
1125 lprocfs_obd_cleanup(obd);
1126 lmv_object_cleanup(obd);
1127 OBD_FREE(lmv->datas, lmv->datas_size);
1128 OBD_FREE(lmv->tgts, lmv->tgts_size);
1133 static int lmv_process_config(struct obd_device *obd, obd_count len, void *buf)
1135 struct lustre_cfg *lcfg = buf;
1136 struct obd_uuid tgt_uuid;
1140 switch(lcfg->lcfg_command) {
1142 if (LUSTRE_CFG_BUFLEN(lcfg, 1) > sizeof(tgt_uuid.uuid))
1143 GOTO(out, rc = -EINVAL);
1145 obd_str2uuid(&tgt_uuid, lustre_cfg_string(lcfg, 1));
1146 rc = lmv_add_target(obd, &tgt_uuid);
1149 CERROR("Unknown command: %d\n", lcfg->lcfg_command);
1150 GOTO(out, rc = -EINVAL);
1157 static int lmv_statfs(struct obd_device *obd, struct obd_statfs *osfs,
1158 __u64 max_age, __u32 flags)
1160 struct lmv_obd *lmv = &obd->u.lmv;
1161 struct obd_statfs *temp;
1166 rc = lmv_check_connect(obd);
1170 OBD_ALLOC(temp, sizeof(*temp));
1174 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
1175 if (lmv->tgts[i].ltd_exp == NULL)
1178 rc = obd_statfs(lmv->tgts[i].ltd_exp->exp_obd, temp,
1181 CERROR("can't stat MDS #%d (%s), error %d\n", i,
1182 lmv->tgts[i].ltd_exp->exp_obd->obd_name,
1184 GOTO(out_free_temp, rc);
1189 osfs->os_bavail += temp->os_bavail;
1190 osfs->os_blocks += temp->os_blocks;
1191 osfs->os_ffree += temp->os_ffree;
1192 osfs->os_files += temp->os_files;
1198 OBD_FREE(temp, sizeof(*temp));
1202 static int lmv_getstatus(struct obd_export *exp,
1204 struct obd_capa **pc)
1206 struct obd_device *obd = exp->exp_obd;
1207 struct lmv_obd *lmv = &obd->u.lmv;
1211 rc = lmv_check_connect(obd);
1215 rc = md_getstatus(lmv->tgts[0].ltd_exp, fid, pc);
1219 static int lmv_getxattr(struct obd_export *exp, const struct lu_fid *fid,
1220 struct obd_capa *oc, obd_valid valid, const char *name,
1221 const char *input, int input_size, int output_size,
1222 int flags, struct ptlrpc_request **request)
1224 struct obd_device *obd = exp->exp_obd;
1225 struct lmv_obd *lmv = &obd->u.lmv;
1226 struct lmv_tgt_desc *tgt;
1230 rc = lmv_check_connect(obd);
1234 tgt = lmv_find_target(lmv, fid);
1236 RETURN(PTR_ERR(tgt));
1238 rc = md_getxattr(tgt->ltd_exp, fid, oc, valid, name, input,
1239 input_size, output_size, flags, request);
1244 static int lmv_setxattr(struct obd_export *exp, const struct lu_fid *fid,
1245 struct obd_capa *oc, obd_valid valid, const char *name,
1246 const char *input, int input_size, int output_size,
1247 int flags, __u32 suppgid,
1248 struct ptlrpc_request **request)
1250 struct obd_device *obd = exp->exp_obd;
1251 struct lmv_obd *lmv = &obd->u.lmv;
1252 struct lmv_tgt_desc *tgt;
1256 rc = lmv_check_connect(obd);
1260 tgt = lmv_find_target(lmv, fid);
1262 RETURN(PTR_ERR(tgt));
1264 rc = md_setxattr(tgt->ltd_exp, fid, oc, valid, name, input,
1265 input_size, output_size, flags, suppgid,
1271 static int lmv_getattr(struct obd_export *exp, const struct lu_fid *fid,
1272 struct obd_capa *oc, obd_valid valid, int ea_size,
1273 struct ptlrpc_request **request)
1275 struct obd_device *obd = exp->exp_obd;
1276 struct lmv_obd *lmv = &obd->u.lmv;
1277 struct lmv_tgt_desc *tgt;
1278 struct lmv_object *obj;
1283 rc = lmv_check_connect(obd);
1287 tgt = lmv_find_target(lmv, fid);
1289 RETURN(PTR_ERR(tgt));
1291 rc = md_getattr(tgt->ltd_exp, fid, oc, valid, ea_size, request);
1295 obj = lmv_object_find_lock(obd, fid);
1297 CDEBUG(D_INODE, "GETATTR for "DFID" %s\n", PFID(fid),
1298 obj ? "(split)" : "");
1301 * If object is split, then we loop over all the slaves and gather size
1302 * attribute. In ideal world we would have to gather also mds field from
1303 * all slaves, as object is spread over the cluster and this is
1304 * definitely interesting information and it is not good to loss it,
1308 struct mdt_body *body;
1310 if (*request == NULL) {
1311 lmv_object_put(obj);
1315 body = req_capsule_server_get(&(*request)->rq_pill,
1317 LASSERT(body != NULL);
1319 for (i = 0; i < obj->lo_objcount; i++) {
1320 if (lmv->tgts[i].ltd_exp == NULL) {
1321 CWARN("%s: NULL export for %d\n",
1327 * Skip master object.
1329 if (lu_fid_eq(&obj->lo_fid, &obj->lo_stripes[i].ls_fid))
1332 body->size += obj->lo_stripes[i].ls_size;
1335 lmv_object_put_unlock(obj);
1341 static int lmv_change_cbdata(struct obd_export *exp, const struct lu_fid *fid,
1342 ldlm_iterator_t it, void *data)
1344 struct obd_device *obd = exp->exp_obd;
1345 struct lmv_obd *lmv = &obd->u.lmv;
1350 rc = lmv_check_connect(obd);
1354 CDEBUG(D_INODE, "CBDATA for "DFID"\n", PFID(fid));
1357 * With CMD every object can have two locks in different namespaces:
1358 * lookup lock in space of mds storing direntry and update/open lock in
1359 * space of mds storing inode.
1361 for (i = 0; i < lmv->desc.ld_tgt_count; i++)
1362 md_change_cbdata(lmv->tgts[i].ltd_exp, fid, it, data);
1367 static int lmv_close(struct obd_export *exp, struct md_op_data *op_data,
1368 struct md_open_data *mod, struct ptlrpc_request **request)
1370 struct obd_device *obd = exp->exp_obd;
1371 struct lmv_obd *lmv = &obd->u.lmv;
1372 struct lmv_tgt_desc *tgt;
1376 rc = lmv_check_connect(obd);
1380 tgt = lmv_find_target(lmv, &op_data->op_fid1);
1382 RETURN(PTR_ERR(tgt));
1384 CDEBUG(D_INODE, "CLOSE "DFID"\n", PFID(&op_data->op_fid1));
1385 rc = md_close(tgt->ltd_exp, op_data, mod, request);
1390 * Called in the case MDS returns -ERESTART on create on open, what means that
1391 * directory is split and its LMV presentation object has to be updated.
1393 int lmv_handle_split(struct obd_export *exp, const struct lu_fid *fid)
1395 struct obd_device *obd = exp->exp_obd;
1396 struct lmv_obd *lmv = &obd->u.lmv;
1397 struct ptlrpc_request *req = NULL;
1398 struct lmv_tgt_desc *tgt;
1399 struct lmv_object *obj;
1400 struct lustre_md md;
1407 mealen = lmv_get_easize(lmv);
1409 valid = OBD_MD_FLEASIZE | OBD_MD_FLDIREA | OBD_MD_MEA;
1411 tgt = lmv_find_target(lmv, fid);
1413 RETURN(PTR_ERR(tgt));
1416 * Time to update mea of parent fid.
1418 rc = md_getattr(tgt->ltd_exp, fid, NULL, valid, mealen, &req);
1420 CERROR("md_getattr() failed, error %d\n", rc);
1424 rc = md_get_lustre_md(tgt->ltd_exp, req, NULL, exp, &md);
1426 CERROR("md_get_lustre_md() failed, error %d\n", rc);
1431 GOTO(cleanup, rc = -ENODATA);
1433 obj = lmv_object_create(exp, fid, md.mea);
1437 lmv_object_put(obj);
1439 obd_free_memmd(exp, (void *)&md.mea);
1443 ptlrpc_req_finished(req);
1447 int lmv_create(struct obd_export *exp, struct md_op_data *op_data,
1448 const void *data, int datalen, int mode, __u32 uid,
1449 __u32 gid, cfs_cap_t cap_effective, __u64 rdev,
1450 struct ptlrpc_request **request)
1452 struct obd_device *obd = exp->exp_obd;
1453 struct lmv_obd *lmv = &obd->u.lmv;
1454 struct lmv_tgt_desc *tgt;
1455 struct lmv_object *obj;
1461 rc = lmv_check_connect(obd);
1465 if (!lmv->desc.ld_active_tgt_count)
1471 obj = lmv_object_find(obd, &op_data->op_fid1);
1473 sidx = raw_name2idx(obj->lo_hashtype, obj->lo_objcount,
1474 op_data->op_name, op_data->op_namelen);
1475 op_data->op_fid1 = obj->lo_stripes[sidx].ls_fid;
1476 op_data->op_bias &= ~MDS_CHECK_SPLIT;
1477 op_data->op_mds = obj->lo_stripes[sidx].ls_mds;
1478 tgt = lmv_get_target(lmv, op_data->op_mds);
1479 lmv_object_put(obj);
1481 tgt = lmv_find_target(lmv, &op_data->op_fid1);
1482 op_data->op_bias |= MDS_CHECK_SPLIT;
1483 op_data->op_mds = tgt->ltd_idx;
1487 RETURN(PTR_ERR(tgt));
1489 rc = lmv_fid_alloc(exp, &op_data->op_fid2, op_data);
1490 if (rc == -ERESTART)
1495 CDEBUG(D_INODE, "CREATE '%*s' on "DFID" -> mds #%x\n",
1496 op_data->op_namelen, op_data->op_name, PFID(&op_data->op_fid1),
1499 op_data->op_flags |= MF_MDC_CANCEL_FID1;
1500 rc = md_create(tgt->ltd_exp, op_data, data, datalen, mode, uid, gid,
1501 cap_effective, rdev, request);
1503 if (*request == NULL)
1505 CDEBUG(D_INODE, "Created - "DFID"\n", PFID(&op_data->op_fid2));
1506 } else if (rc == -ERESTART) {
1507 LASSERT(*request != NULL);
1508 DEBUG_REQ(D_WARNING|D_RPCTRACE, *request,
1509 "Got -ERESTART during create!\n");
1510 ptlrpc_req_finished(*request);
1514 * Directory got split. Time to update local object and repeat
1515 * the request with proper MDS.
1517 rc = lmv_handle_split(exp, &op_data->op_fid1);
1519 rc = lmv_allocate_slaves(obd, &op_data->op_fid1,
1520 op_data, &op_data->op_fid2);
1529 static int lmv_done_writing(struct obd_export *exp,
1530 struct md_op_data *op_data,
1531 struct md_open_data *mod)
1533 struct obd_device *obd = exp->exp_obd;
1534 struct lmv_obd *lmv = &obd->u.lmv;
1535 struct lmv_tgt_desc *tgt;
1539 rc = lmv_check_connect(obd);
1543 tgt = lmv_find_target(lmv, &op_data->op_fid1);
1545 RETURN(PTR_ERR(tgt));
1547 rc = md_done_writing(tgt->ltd_exp, op_data, mod);
1552 lmv_enqueue_slaves(struct obd_export *exp, struct ldlm_enqueue_info *einfo,
1553 struct lookup_intent *it, struct md_op_data *op_data,
1554 struct lustre_handle *lockh, void *lmm, int lmmsize)
1556 struct obd_device *obd = exp->exp_obd;
1557 struct lmv_obd *lmv = &obd->u.lmv;
1558 struct lmv_stripe_md *mea = op_data->op_mea1;
1559 struct md_op_data *op_data2;
1560 struct lmv_tgt_desc *tgt;
1565 OBD_ALLOC_PTR(op_data2);
1566 if (op_data2 == NULL)
1569 LASSERT(mea != NULL);
1570 for (i = 0; i < mea->mea_count; i++) {
1571 memset(op_data2, 0, sizeof(*op_data2));
1572 op_data2->op_fid1 = mea->mea_ids[i];
1573 op_data2->op_bias = 0;
1575 tgt = lmv_find_target(lmv, &op_data2->op_fid1);
1577 GOTO(cleanup, rc = PTR_ERR(tgt));
1579 if (tgt->ltd_exp == NULL)
1582 rc = md_enqueue(tgt->ltd_exp, einfo, it, op_data2,
1583 lockh + i, lmm, lmmsize, NULL, 0);
1585 CDEBUG(D_INODE, "Take lock on slave "DFID" -> %d/%d\n",
1586 PFID(&mea->mea_ids[i]), rc, it->d.lustre.it_status);
1591 if (it->d.lustre.it_data) {
1592 struct ptlrpc_request *req;
1593 req = (struct ptlrpc_request *)it->d.lustre.it_data;
1594 ptlrpc_req_finished(req);
1597 if (it->d.lustre.it_status)
1598 GOTO(cleanup, rc = it->d.lustre.it_status);
1603 OBD_FREE_PTR(op_data2);
1607 * Drop all taken locks.
1610 if (lockh[i].cookie)
1611 ldlm_lock_decref(lockh + i, einfo->ei_mode);
1612 lockh[i].cookie = 0;
1619 lmv_enqueue_remote(struct obd_export *exp, struct ldlm_enqueue_info *einfo,
1620 struct lookup_intent *it, struct md_op_data *op_data,
1621 struct lustre_handle *lockh, void *lmm, int lmmsize,
1622 int extra_lock_flags)
1624 struct ptlrpc_request *req = it->d.lustre.it_data;
1625 struct obd_device *obd = exp->exp_obd;
1626 struct lmv_obd *lmv = &obd->u.lmv;
1627 struct lustre_handle plock;
1628 struct lmv_tgt_desc *tgt;
1629 struct md_op_data *rdata;
1631 struct mdt_body *body;
1636 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
1637 LASSERT(body != NULL);
1639 if (!(body->valid & OBD_MD_MDS))
1642 CDEBUG(D_INODE, "REMOTE_ENQUEUE '%s' on "DFID" -> "DFID"\n",
1643 LL_IT2STR(it), PFID(&op_data->op_fid1), PFID(&body->fid1));
1646 * We got LOOKUP lock, but we really need attrs.
1648 pmode = it->d.lustre.it_lock_mode;
1649 LASSERT(pmode != 0);
1650 memcpy(&plock, lockh, sizeof(plock));
1651 it->d.lustre.it_lock_mode = 0;
1652 it->d.lustre.it_data = NULL;
1655 it->d.lustre.it_disposition &= ~DISP_ENQ_COMPLETE;
1656 ptlrpc_req_finished(req);
1658 tgt = lmv_find_target(lmv, &fid1);
1660 GOTO(out, rc = PTR_ERR(tgt));
1662 OBD_ALLOC_PTR(rdata);
1664 GOTO(out, rc = -ENOMEM);
1666 rdata->op_fid1 = fid1;
1667 rdata->op_bias = MDS_CROSS_REF;
1669 rc = md_enqueue(tgt->ltd_exp, einfo, it, rdata, lockh,
1670 lmm, lmmsize, NULL, extra_lock_flags);
1671 OBD_FREE_PTR(rdata);
1674 ldlm_lock_decref(&plock, pmode);
1679 lmv_enqueue(struct obd_export *exp, struct ldlm_enqueue_info *einfo,
1680 struct lookup_intent *it, struct md_op_data *op_data,
1681 struct lustre_handle *lockh, void *lmm, int lmmsize,
1682 struct ptlrpc_request **req, int extra_lock_flags)
1684 struct obd_device *obd = exp->exp_obd;
1685 struct lmv_obd *lmv = &obd->u.lmv;
1686 struct lmv_tgt_desc *tgt;
1687 struct lmv_object *obj;
1692 rc = lmv_check_connect(obd);
1696 CDEBUG(D_INODE, "ENQUEUE '%s' on "DFID"\n",
1697 LL_IT2STR(it), PFID(&op_data->op_fid1));
1699 if (op_data->op_mea1 && it && it->it_op == IT_UNLINK) {
1700 rc = lmv_enqueue_slaves(exp, einfo, it, op_data,
1701 lockh, lmm, lmmsize);
1705 obj = lmv_object_find(obd, &op_data->op_fid1);
1706 if (obj && op_data->op_namelen) {
1707 sidx = raw_name2idx(obj->lo_hashtype,
1709 (char *)op_data->op_name,
1710 op_data->op_namelen);
1711 op_data->op_fid1 = obj->lo_stripes[sidx].ls_fid;
1712 tgt = lmv_get_target(lmv, obj->lo_stripes[sidx].ls_mds);
1714 tgt = lmv_find_target(lmv, &op_data->op_fid1);
1717 lmv_object_put(obj);
1720 RETURN(PTR_ERR(tgt));
1722 CDEBUG(D_INODE, "ENQUEUE '%s' on "DFID" -> mds #%d\n",
1723 LL_IT2STR(it), PFID(&op_data->op_fid1), tgt->ltd_idx);
1725 rc = md_enqueue(tgt->ltd_exp, einfo, it, op_data, lockh,
1726 lmm, lmmsize, req, extra_lock_flags);
1728 if (rc == 0 && it && it->it_op == IT_OPEN) {
1729 rc = lmv_enqueue_remote(exp, einfo, it, op_data, lockh,
1730 lmm, lmmsize, extra_lock_flags);
1736 lmv_getattr_name(struct obd_export *exp, const struct lu_fid *fid,
1737 struct obd_capa *oc, const char *name, int namelen,
1738 obd_valid valid, int ea_size, __u32 suppgid,
1739 struct ptlrpc_request **request)
1741 struct ptlrpc_request *req = NULL;
1742 struct obd_device *obd = exp->exp_obd;
1743 struct lmv_obd *lmv = &obd->u.lmv;
1744 struct lu_fid rid = *fid;
1745 struct lmv_tgt_desc *tgt;
1746 struct mdt_body *body;
1747 struct lmv_object *obj;
1753 rc = lmv_check_connect(obd);
1760 obj = lmv_object_find(obd, &rid);
1762 sidx = raw_name2idx(obj->lo_hashtype, obj->lo_objcount,
1764 rid = obj->lo_stripes[sidx].ls_fid;
1765 tgt = lmv_get_target(lmv, obj->lo_stripes[sidx].ls_mds);
1766 valid &= ~OBD_MD_FLCKSPLIT;
1767 lmv_object_put(obj);
1769 tgt = lmv_find_target(lmv, &rid);
1770 valid |= OBD_MD_FLCKSPLIT;
1773 RETURN(PTR_ERR(tgt));
1775 CDEBUG(D_INODE, "GETATTR_NAME for %*s on "DFID" - "DFID" -> mds #%d\n",
1776 namelen, name, PFID(fid), PFID(&rid), tgt->ltd_idx);
1778 rc = md_getattr_name(tgt->ltd_exp, &rid, oc, name, namelen, valid,
1779 ea_size, suppgid, request);
1781 body = req_capsule_server_get(&(*request)->rq_pill,
1783 LASSERT(body != NULL);
1785 if (body->valid & OBD_MD_MDS) {
1787 CDEBUG(D_INODE, "Request attrs for "DFID"\n",
1790 tgt = lmv_find_target(lmv, &rid);
1792 ptlrpc_req_finished(*request);
1793 RETURN(PTR_ERR(tgt));
1796 rc = md_getattr_name(tgt->ltd_exp, &rid, NULL, NULL,
1797 1, valid | OBD_MD_FLCROSSREF,
1798 ea_size, suppgid, &req);
1799 ptlrpc_req_finished(*request);
1802 } else if (rc == -ERESTART) {
1803 LASSERT(*request != NULL);
1804 DEBUG_REQ(D_WARNING|D_RPCTRACE, *request,
1805 "Got -ERESTART during getattr!\n");
1806 ptlrpc_req_finished(*request);
1810 * Directory got split. Time to update local object and repeat
1811 * the request with proper MDS.
1813 rc = lmv_handle_split(exp, &rid);
1820 #define md_op_data_fid(op_data, fl) \
1821 (fl == MF_MDC_CANCEL_FID1 ? &op_data->op_fid1 : \
1822 fl == MF_MDC_CANCEL_FID2 ? &op_data->op_fid2 : \
1823 fl == MF_MDC_CANCEL_FID3 ? &op_data->op_fid3 : \
1824 fl == MF_MDC_CANCEL_FID4 ? &op_data->op_fid4 : \
1827 static int lmv_early_cancel_slaves(struct obd_export *exp,
1828 struct md_op_data *op_data, int op_tgt,
1829 ldlm_mode_t mode, int bits, int flag)
1831 struct obd_device *obd = exp->exp_obd;
1832 struct lmv_obd *lmv = &obd->u.lmv;
1833 ldlm_policy_data_t policy = {{0}};
1834 struct lu_fid *op_fid;
1835 struct lu_fid *st_fid;
1836 struct lmv_tgt_desc *tgt;
1837 struct lmv_object *obj;
1842 op_fid = md_op_data_fid(op_data, flag);
1843 if (!fid_is_sane(op_fid))
1846 obj = lmv_object_find(obd, op_fid);
1850 policy.l_inodebits.bits = bits;
1851 for (i = 0; i < obj->lo_objcount; i++) {
1852 tgt = lmv_get_target(lmv, obj->lo_stripes[i].ls_mds);
1853 st_fid = &obj->lo_stripes[i].ls_fid;
1854 if (op_tgt != tgt->ltd_idx) {
1855 CDEBUG(D_INODE, "EARLY_CANCEL slave "DFID" -> mds #%d\n",
1856 PFID(st_fid), tgt->ltd_idx);
1857 rc = md_cancel_unused(tgt->ltd_exp, st_fid, &policy,
1858 mode, LDLM_FL_ASYNC, NULL);
1860 GOTO(out_put_obj, rc);
1863 "EARLY_CANCEL skip operation target %d on "DFID"\n",
1864 op_tgt, PFID(st_fid));
1866 * Do not cancel locks for operation target, they will
1867 * be handled later in underlaying layer when calling
1868 * function we run on behalf of.
1871 op_data->op_flags |= flag;
1876 lmv_object_put(obj);
1880 static int lmv_early_cancel(struct obd_export *exp, struct md_op_data *op_data,
1881 int op_tgt, ldlm_mode_t mode, int bits, int flag)
1883 struct lu_fid *fid = md_op_data_fid(op_data, flag);
1884 struct obd_device *obd = exp->exp_obd;
1885 struct lmv_obd *lmv = &obd->u.lmv;
1886 struct lmv_tgt_desc *tgt;
1887 ldlm_policy_data_t policy = {{0}};
1888 struct lmv_object *obj;
1892 if (!fid_is_sane(fid))
1895 obj = lmv_object_find(obd, fid);
1897 rc = lmv_early_cancel_slaves(exp, op_data, op_tgt, mode,
1899 lmv_object_put(obj);
1901 tgt = lmv_find_target(lmv, fid);
1903 RETURN(PTR_ERR(tgt));
1905 if (tgt->ltd_idx != op_tgt) {
1906 CDEBUG(D_INODE, "EARLY_CANCEL on "DFID"\n", PFID(fid));
1907 policy.l_inodebits.bits = bits;
1908 rc = md_cancel_unused(tgt->ltd_exp, fid, &policy,
1909 mode, LDLM_FL_ASYNC, NULL);
1912 "EARLY_CANCEL skip operation target %d on "DFID"\n",
1914 op_data->op_flags |= flag;
1923 * llite passes fid of an target inode in op_data->op_fid1 and id of directory in
1926 static int lmv_link(struct obd_export *exp, struct md_op_data *op_data,
1927 struct ptlrpc_request **request)
1929 struct obd_device *obd = exp->exp_obd;
1930 struct lmv_obd *lmv = &obd->u.lmv;
1931 struct lmv_tgt_desc *tgt;
1932 struct lmv_object *obj;
1939 rc = lmv_check_connect(obd);
1946 LASSERT(op_data->op_namelen != 0);
1948 CDEBUG(D_INODE, "LINK "DFID":%*s to "DFID"\n",
1949 PFID(&op_data->op_fid2), op_data->op_namelen,
1950 op_data->op_name, PFID(&op_data->op_fid1));
1952 obj = lmv_object_find(obd, &op_data->op_fid2);
1954 sidx = raw_name2idx(obj->lo_hashtype,
1957 op_data->op_namelen);
1958 op_data->op_fid2 = obj->lo_stripes[sidx].ls_fid;
1959 mds = obj->lo_stripes[sidx].ls_mds;
1960 lmv_object_put(obj);
1962 rc = lmv_fld_lookup(lmv, &op_data->op_fid2, &mds);
1967 CDEBUG(D_INODE, "Forward to mds #%x ("DFID")\n",
1968 mds, PFID(&op_data->op_fid1));
1970 op_data->op_fsuid = cfs_curproc_fsuid();
1971 op_data->op_fsgid = cfs_curproc_fsgid();
1972 op_data->op_cap = cfs_curproc_cap_pack();
1973 tgt = lmv_get_target(lmv, mds);
1976 * Cancel UPDATE lock on child (fid1).
1978 op_data->op_flags |= MF_MDC_CANCEL_FID2;
1979 rc = lmv_early_cancel(exp, op_data, tgt->ltd_idx, LCK_EX,
1980 MDS_INODELOCK_UPDATE, MF_MDC_CANCEL_FID1);
1982 rc = md_link(tgt->ltd_exp, op_data, request);
1983 if (rc == -ERESTART) {
1984 LASSERT(*request != NULL);
1985 DEBUG_REQ(D_WARNING|D_RPCTRACE, *request,
1986 "Got -ERESTART during link!\n");
1987 ptlrpc_req_finished(*request);
1991 * Directory got split. Time to update local object and repeat
1992 * the request with proper MDS.
1994 rc = lmv_handle_split(exp, &op_data->op_fid2);
2002 static int lmv_rename(struct obd_export *exp, struct md_op_data *op_data,
2003 const char *old, int oldlen, const char *new, int newlen,
2004 struct ptlrpc_request **request)
2006 struct obd_device *obd = exp->exp_obd;
2007 struct lmv_obd *lmv = &obd->u.lmv;
2008 struct lmv_tgt_desc *src_tgt;
2009 struct lmv_tgt_desc *tgt_tgt;
2013 struct lmv_object *obj;
2018 LASSERT(oldlen != 0);
2020 CDEBUG(D_INODE, "RENAME %*s in "DFID" to %*s in "DFID"\n",
2021 oldlen, old, PFID(&op_data->op_fid1),
2022 newlen, new, PFID(&op_data->op_fid2));
2024 rc = lmv_check_connect(obd);
2031 obj = lmv_object_find(obd, &op_data->op_fid1);
2033 sidx = raw_name2idx(obj->lo_hashtype, obj->lo_objcount,
2034 (char *)old, oldlen);
2035 op_data->op_fid1 = obj->lo_stripes[sidx].ls_fid;
2036 mds1 = obj->lo_stripes[sidx].ls_mds;
2037 CDEBUG(D_INODE, "Parent obj "DFID"\n", PFID(&op_data->op_fid1));
2038 lmv_object_put(obj);
2040 rc = lmv_fld_lookup(lmv, &op_data->op_fid1, &mds1);
2045 obj = lmv_object_find(obd, &op_data->op_fid2);
2048 * Directory is already split, so we have to forward request to
2051 sidx = raw_name2idx(obj->lo_hashtype, obj->lo_objcount,
2052 (char *)new, newlen);
2054 mds2 = obj->lo_stripes[sidx].ls_mds;
2055 op_data->op_fid2 = obj->lo_stripes[sidx].ls_fid;
2056 CDEBUG(D_INODE, "Parent obj "DFID"\n", PFID(&op_data->op_fid2));
2057 lmv_object_put(obj);
2059 rc = lmv_fld_lookup(lmv, &op_data->op_fid2, &mds2);
2064 op_data->op_fsuid = cfs_curproc_fsuid();
2065 op_data->op_fsgid = cfs_curproc_fsgid();
2066 op_data->op_cap = cfs_curproc_cap_pack();
2068 src_tgt = lmv_get_target(lmv, mds1);
2069 tgt_tgt = lmv_get_target(lmv, mds2);
2072 * LOOKUP lock on src child (fid3) should also be cancelled for
2073 * src_tgt in mdc_rename.
2075 op_data->op_flags |= MF_MDC_CANCEL_FID1 | MF_MDC_CANCEL_FID3;
2078 * Cancel UPDATE locks on tgt parent (fid2), tgt_tgt is its
2081 rc = lmv_early_cancel(exp, op_data, src_tgt->ltd_idx,
2082 LCK_EX, MDS_INODELOCK_UPDATE,
2083 MF_MDC_CANCEL_FID2);
2086 * Cancel LOOKUP locks on tgt child (fid4) for parent tgt_tgt.
2089 rc = lmv_early_cancel(exp, op_data, src_tgt->ltd_idx,
2090 LCK_EX, MDS_INODELOCK_LOOKUP,
2091 MF_MDC_CANCEL_FID4);
2095 * Cancel all the locks on tgt child (fid4).
2098 rc = lmv_early_cancel(exp, op_data, src_tgt->ltd_idx,
2099 LCK_EX, MDS_INODELOCK_FULL,
2100 MF_MDC_CANCEL_FID4);
2103 rc = md_rename(src_tgt->ltd_exp, op_data, old, oldlen,
2104 new, newlen, request);
2106 if (rc == -ERESTART) {
2107 LASSERT(*request != NULL);
2108 DEBUG_REQ(D_WARNING|D_RPCTRACE, *request,
2109 "Got -ERESTART during rename!\n");
2110 ptlrpc_req_finished(*request);
2114 * Directory got split. Time to update local object and repeat
2115 * the request with proper MDS.
2117 rc = lmv_handle_split(exp, &op_data->op_fid1);
2124 static int lmv_setattr(struct obd_export *exp, struct md_op_data *op_data,
2125 void *ea, int ealen, void *ea2, int ea2len,
2126 struct ptlrpc_request **request,
2127 struct md_open_data **mod)
2129 struct obd_device *obd = exp->exp_obd;
2130 struct lmv_obd *lmv = &obd->u.lmv;
2131 struct ptlrpc_request *req;
2132 struct lmv_tgt_desc *tgt;
2133 struct lmv_object *obj;
2138 rc = lmv_check_connect(obd);
2142 obj = lmv_object_find(obd, &op_data->op_fid1);
2144 CDEBUG(D_INODE, "SETATTR for "DFID", valid 0x%x%s\n",
2145 PFID(&op_data->op_fid1), op_data->op_attr.ia_valid,
2146 obj ? ", split" : "");
2148 op_data->op_flags |= MF_MDC_CANCEL_FID1;
2150 for (i = 0; i < obj->lo_objcount; i++) {
2151 op_data->op_fid1 = obj->lo_stripes[i].ls_fid;
2153 tgt = lmv_get_target(lmv, obj->lo_stripes[i].ls_mds);
2159 rc = md_setattr(tgt->ltd_exp, op_data, ea, ealen,
2160 ea2, ea2len, &req, mod);
2162 if (lu_fid_eq(&obj->lo_fid, &obj->lo_stripes[i].ls_fid)) {
2164 * This is master object and this request should
2165 * be returned back to llite.
2169 ptlrpc_req_finished(req);
2175 lmv_object_put(obj);
2177 tgt = lmv_find_target(lmv, &op_data->op_fid1);
2179 RETURN(PTR_ERR(tgt));
2181 rc = md_setattr(tgt->ltd_exp, op_data, ea, ealen, ea2,
2182 ea2len, request, mod);
2187 static int lmv_sync(struct obd_export *exp, const struct lu_fid *fid,
2188 struct obd_capa *oc, struct ptlrpc_request **request)
2190 struct obd_device *obd = exp->exp_obd;
2191 struct lmv_obd *lmv = &obd->u.lmv;
2192 struct lmv_tgt_desc *tgt;
2196 rc = lmv_check_connect(obd);
2200 tgt = lmv_find_target(lmv, fid);
2202 RETURN(PTR_ERR(tgt));
2204 rc = md_sync(tgt->ltd_exp, fid, oc, request);
2209 * Main purpose of LMV blocking ast is to remove split directory LMV
2210 * presentation object (struct lmv_object) attached to the lock being revoked.
2212 int lmv_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
2213 void *data, int flag)
2215 struct lustre_handle lockh;
2216 struct lmv_object *obj;
2221 case LDLM_CB_BLOCKING:
2222 ldlm_lock2handle(lock, &lockh);
2223 rc = ldlm_cli_cancel(&lockh);
2225 CDEBUG(D_INODE, "ldlm_cli_cancel: %d\n", rc);
2229 case LDLM_CB_CANCELING:
2231 * Time to drop cached attrs for split directory object
2233 obj = lock->l_ast_data;
2235 CDEBUG(D_INODE, "Cancel %s on "LPU64"/"LPU64
2236 ", master "DFID"\n",
2237 lock->l_resource->lr_name.name[3] == 1 ?
2238 "LOOKUP" : "UPDATE",
2239 lock->l_resource->lr_name.name[0],
2240 lock->l_resource->lr_name.name[1],
2241 PFID(&obj->lo_fid));
2242 lmv_object_put(obj);
2251 static void lmv_hash_adjust(__u64 *hash, __u64 hash_adj)
2255 val = le64_to_cpu(*hash);
2257 val += MAX_HASH_SIZE;
2258 if (val != DIR_END_OFF)
2259 *hash = cpu_to_le64(val - hash_adj);
2262 static __u32 lmv_node_rank(struct obd_export *exp, const struct lu_fid *fid)
2265 struct obd_import *imp;
2268 * XXX: to get nid we assume that underlying obd device is mdc.
2270 imp = class_exp2cliimp(exp);
2271 id = imp->imp_connection->c_self + fid_flatten(fid);
2273 CDEBUG(D_INODE, "Readpage node rank: "LPX64" "DFID" "LPX64" "LPX64"\n",
2274 imp->imp_connection->c_self, PFID(fid), id, id ^ (id >> 32));
2276 return id ^ (id >> 32);
2279 static int lmv_readpage(struct obd_export *exp, const struct lu_fid *fid,
2280 struct obd_capa *oc, __u64 offset64, struct page *page,
2281 struct ptlrpc_request **request)
2283 struct obd_device *obd = exp->exp_obd;
2284 struct lmv_obd *lmv = &obd->u.lmv;
2285 struct lu_fid rid = *fid;
2286 struct lmv_object *obj;
2296 struct lmv_stripe *los;
2297 struct lmv_tgt_desc *tgt;
2298 struct lu_dirpage *dp;
2299 struct lu_dirent *ent;
2304 rc = lmv_check_connect(obd);
2308 CDEBUG(D_INODE, "READPAGE at "LPX64" from "DFID"\n", offset, PFID(&rid));
2311 * This case handle directory lookup in clustered metadata case (i.e.
2312 * split directory is located on multiple md servers.)
2313 * each server keeps directory entries for certain range of hashes.
2314 * E.g. we have N server and suppose hash range is 0 to MAX_HASH.
2315 * first server will keep records with hashes [ 0 ... MAX_HASH / N - 1],
2316 * second one with hashes [MAX_HASH / N ... 2 * MAX_HASH / N] and
2318 * readdir can simply start reading entries from 0 - N server in
2319 * order but that will not scale well as all client will request dir in
2320 * to server in same order.
2321 * Following algorithm does optimization:
2322 * Instead of doing readdir in 1, 2, ...., N order, client with a
2323 * rank R does readdir in R, R + 1, ..., N, 1, ... R - 1 order.
2324 * (every client has rank R)
2325 * But ll_readdir() expect offset range [0 to MAX_HASH/N) but
2326 * since client ask dir from MDS{R} client has pages with offsets
2327 * [R*MAX_HASH/N ... (R + 1)*MAX_HASH/N] there for we do hash_adj
2328 * on hash values that we get.
2330 obj = lmv_object_find_lock(obd, fid);
2332 nr = obj->lo_objcount;
2334 seg_size = MAX_HASH_SIZE;
2335 do_div(seg_size, nr);
2336 los = obj->lo_stripes;
2337 tgt = lmv_get_target(lmv, los[0].ls_mds);
2338 rank = lmv_node_rank(tgt->ltd_exp, fid) % nr;
2340 do_div(tgt_tmp, seg_size);
2341 tgt0_idx = do_div(tgt_tmp, nr);
2342 tgt_idx = (tgt0_idx + rank) % nr;
2344 if (tgt_idx < tgt0_idx)
2348 * Last segment has unusual length due to division
2351 hash_adj = MAX_HASH_SIZE - seg_size * nr;
2355 hash_adj += rank * seg_size;
2357 CDEBUG(D_INODE, "Readpage hash adjustment: %x "LPX64" "
2358 LPX64"/%x -> "LPX64"/%x\n", rank, hash_adj,
2359 offset, tgt0_idx, offset + hash_adj, tgt_idx);
2361 offset = (offset + hash_adj) & MAX_HASH_SIZE;
2362 rid = obj->lo_stripes[tgt_idx].ls_fid;
2363 tgt = lmv_get_target(lmv, los[tgt_idx].ls_mds);
2365 CDEBUG(D_INODE, "Forward to "DFID" with offset %lu i %d\n",
2366 PFID(&rid), (unsigned long)offset, tgt_idx);
2368 tgt = lmv_find_target(lmv, &rid);
2371 GOTO(cleanup, rc = PTR_ERR(tgt));
2373 rc = md_readpage(tgt->ltd_exp, &rid, oc, offset, page, request);
2377 dp = cfs_kmap(page);
2379 lmv_hash_adjust(&dp->ldp_hash_start, hash_adj);
2380 lmv_hash_adjust(&dp->ldp_hash_end, hash_adj);
2381 LASSERT(le64_to_cpu(dp->ldp_hash_start) <= offset64);
2383 for (ent = lu_dirent_start(dp); ent != NULL;
2384 ent = lu_dirent_next(ent))
2385 lmv_hash_adjust(&ent->lde_hash, hash_adj);
2387 if (tgt0_idx != nr - 1) {
2390 end = le64_to_cpu(dp->ldp_hash_end);
2391 if (end == DIR_END_OFF) {
2392 dp->ldp_hash_end = cpu_to_le32(seg_size *
2395 ""DFID" reset end "LPX64" tgt %d\n",
2397 le64_to_cpu(dp->ldp_hash_end), tgt_idx);
2405 lmv_object_put_unlock(obj);
2409 static int lmv_unlink(struct obd_export *exp, struct md_op_data *op_data,
2410 struct ptlrpc_request **request)
2412 struct obd_device *obd = exp->exp_obd;
2413 struct lmv_obd *lmv = &obd->u.lmv;
2414 struct lmv_tgt_desc *tgt = NULL;
2415 struct lmv_object *obj;
2421 rc = lmv_check_connect(obd);
2428 LASSERT(op_data->op_namelen != 0);
2430 obj = lmv_object_find(obd, &op_data->op_fid1);
2432 sidx = raw_name2idx(obj->lo_hashtype,
2435 op_data->op_namelen);
2436 op_data->op_bias &= ~MDS_CHECK_SPLIT;
2437 op_data->op_fid1 = obj->lo_stripes[sidx].ls_fid;
2438 tgt = lmv_get_target(lmv,
2439 obj->lo_stripes[sidx].ls_mds);
2440 lmv_object_put(obj);
2441 CDEBUG(D_INODE, "UNLINK '%*s' in "DFID" -> %u\n",
2442 op_data->op_namelen, op_data->op_name,
2443 PFID(&op_data->op_fid1), sidx);
2447 tgt = lmv_find_target(lmv, &op_data->op_fid1);
2449 RETURN(PTR_ERR(tgt));
2450 op_data->op_bias |= MDS_CHECK_SPLIT;
2453 op_data->op_fsuid = cfs_curproc_fsuid();
2454 op_data->op_fsgid = cfs_curproc_fsgid();
2455 op_data->op_cap = cfs_curproc_cap_pack();
2458 * If child's fid is given, cancel unused locks for it if it is from
2459 * another export than parent.
2461 * LOOKUP lock for child (fid3) should also be cancelled on parent
2462 * tgt_tgt in mdc_unlink().
2464 op_data->op_flags |= MF_MDC_CANCEL_FID1 | MF_MDC_CANCEL_FID3;
2467 * Cancel FULL locks on child (fid3).
2469 rc = lmv_early_cancel(exp, op_data, tgt->ltd_idx, LCK_EX,
2470 MDS_INODELOCK_FULL, MF_MDC_CANCEL_FID3);
2473 rc = md_unlink(tgt->ltd_exp, op_data, request);
2475 if (rc == -ERESTART) {
2476 LASSERT(*request != NULL);
2477 DEBUG_REQ(D_WARNING|D_RPCTRACE, *request,
2478 "Got -ERESTART during unlink!\n");
2479 ptlrpc_req_finished(*request);
2483 * Directory got split. Time to update local object and repeat
2484 * the request with proper MDS.
2486 rc = lmv_handle_split(exp, &op_data->op_fid1);
2493 static int lmv_precleanup(struct obd_device *obd, enum obd_cleanup_stage stage)
2498 case OBD_CLEANUP_EARLY:
2499 /* XXX: here should be calling obd_precleanup() down to
2502 case OBD_CLEANUP_EXPORTS:
2503 rc = obd_llog_finish(obd, 0);
2505 CERROR("failed to cleanup llogging subsystems\n");
2513 static int lmv_get_info(struct obd_export *exp, __u32 keylen,
2514 void *key, __u32 *vallen, void *val,
2515 struct lov_stripe_md *lsm)
2517 struct obd_device *obd;
2518 struct lmv_obd *lmv;
2522 obd = class_exp2obd(exp);
2524 CDEBUG(D_IOCTL, "Invalid client cookie "LPX64"\n",
2525 exp->exp_handle.h_cookie);
2530 if (keylen >= strlen("remote_flag") && !strcmp(key, "remote_flag")) {
2531 struct lmv_tgt_desc *tgts;
2534 rc = lmv_check_connect(obd);
2538 LASSERT(*vallen == sizeof(__u32));
2539 for (i = 0, tgts = lmv->tgts; i < lmv->desc.ld_tgt_count;
2543 * All tgts should be connected when this gets called.
2545 if (!tgts || !tgts->ltd_exp) {
2546 CERROR("target not setup?\n");
2550 if (!obd_get_info(tgts->ltd_exp, keylen, key,
2555 } else if (KEY_IS(KEY_MAX_EASIZE) || KEY_IS(KEY_CONN_DATA)) {
2556 rc = lmv_check_connect(obd);
2561 * Forwarding this request to first MDS, it should know LOV
2564 rc = obd_get_info(lmv->tgts[0].ltd_exp, keylen, key,
2566 if (!rc && KEY_IS(KEY_CONN_DATA)) {
2567 exp->exp_connect_flags =
2568 ((struct obd_connect_data *)val)->ocd_connect_flags;
2573 CDEBUG(D_IOCTL, "Invalid key\n");
2577 int lmv_set_info_async(struct obd_export *exp, obd_count keylen,
2578 void *key, obd_count vallen, void *val,
2579 struct ptlrpc_request_set *set)
2581 struct lmv_tgt_desc *tgt;
2582 struct obd_device *obd;
2583 struct lmv_obd *lmv;
2587 obd = class_exp2obd(exp);
2589 CDEBUG(D_IOCTL, "Invalid client cookie "LPX64"\n",
2590 exp->exp_handle.h_cookie);
2595 if (KEY_IS(KEY_READ_ONLY) || KEY_IS(KEY_FLUSH_CTX) ||
2596 KEY_IS(KEY_INIT_RECOV_BACKUP)) {
2599 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
2600 tgt = &lmv->tgts[i];
2605 err = obd_set_info_async(tgt->ltd_exp,
2606 keylen, key, vallen, val, set);
2617 int lmv_packmd(struct obd_export *exp, struct lov_mds_md **lmmp,
2618 struct lov_stripe_md *lsm)
2620 struct obd_device *obd = class_exp2obd(exp);
2621 struct lmv_obd *lmv = &obd->u.lmv;
2622 struct lmv_stripe_md *meap;
2623 struct lmv_stripe_md *lsmp;
2628 mea_size = lmv_get_easize(lmv);
2632 if (*lmmp && !lsm) {
2633 OBD_FREE(*lmmp, mea_size);
2638 if (*lmmp == NULL) {
2639 OBD_ALLOC(*lmmp, mea_size);
2647 lsmp = (struct lmv_stripe_md *)lsm;
2648 meap = (struct lmv_stripe_md *)*lmmp;
2650 if (lsmp->mea_magic != MEA_MAGIC_LAST_CHAR &&
2651 lsmp->mea_magic != MEA_MAGIC_ALL_CHARS)
2654 meap->mea_magic = cpu_to_le32(lsmp->mea_magic);
2655 meap->mea_count = cpu_to_le32(lsmp->mea_count);
2656 meap->mea_master = cpu_to_le32(lsmp->mea_master);
2658 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
2659 meap->mea_ids[i] = meap->mea_ids[i];
2660 fid_cpu_to_le(&meap->mea_ids[i], &meap->mea_ids[i]);
2666 int lmv_unpackmd(struct obd_export *exp, struct lov_stripe_md **lsmp,
2667 struct lov_mds_md *lmm, int lmm_size)
2669 struct obd_device *obd = class_exp2obd(exp);
2670 struct lmv_stripe_md **tmea = (struct lmv_stripe_md **)lsmp;
2671 struct lmv_stripe_md *mea = (struct lmv_stripe_md *)lmm;
2672 struct lmv_obd *lmv = &obd->u.lmv;
2678 mea_size = lmv_get_easize(lmv);
2682 if (*lsmp != NULL && lmm == NULL) {
2683 OBD_FREE(*tmea, mea_size);
2688 LASSERT(mea_size == lmm_size);
2690 OBD_ALLOC(*tmea, mea_size);
2697 if (mea->mea_magic == MEA_MAGIC_LAST_CHAR ||
2698 mea->mea_magic == MEA_MAGIC_ALL_CHARS ||
2699 mea->mea_magic == MEA_MAGIC_HASH_SEGMENT)
2701 magic = le32_to_cpu(mea->mea_magic);
2704 * Old mea is not handled here.
2706 CERROR("Old not supportable EA is found\n");
2710 (*tmea)->mea_magic = magic;
2711 (*tmea)->mea_count = le32_to_cpu(mea->mea_count);
2712 (*tmea)->mea_master = le32_to_cpu(mea->mea_master);
2714 for (i = 0; i < (*tmea)->mea_count; i++) {
2715 (*tmea)->mea_ids[i] = mea->mea_ids[i];
2716 fid_le_to_cpu(&(*tmea)->mea_ids[i], &(*tmea)->mea_ids[i]);
2721 static int lmv_cancel_unused(struct obd_export *exp, const struct lu_fid *fid,
2722 ldlm_policy_data_t *policy, ldlm_mode_t mode,
2723 int flags, void *opaque)
2725 struct obd_device *obd = exp->exp_obd;
2726 struct lmv_obd *lmv = &obd->u.lmv;
2732 LASSERT(fid != NULL);
2734 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
2735 if (!lmv->tgts[i].ltd_exp || !lmv->tgts[i].ltd_active)
2738 err = md_cancel_unused(lmv->tgts[i].ltd_exp, fid,
2739 policy, mode, flags, opaque);
2746 int lmv_set_lock_data(struct obd_export *exp, __u64 *lockh, void *data,
2749 struct obd_device *obd = exp->exp_obd;
2750 struct lmv_obd *lmv = &obd->u.lmv;
2754 rc = md_set_lock_data(lmv->tgts[0].ltd_exp, lockh, data, bits);
2758 ldlm_mode_t lmv_lock_match(struct obd_export *exp, int flags,
2759 const struct lu_fid *fid, ldlm_type_t type,
2760 ldlm_policy_data_t *policy, ldlm_mode_t mode,
2761 struct lustre_handle *lockh)
2763 struct obd_device *obd = exp->exp_obd;
2764 struct lmv_obd *lmv = &obd->u.lmv;
2769 CDEBUG(D_INODE, "Lock match for "DFID"\n", PFID(fid));
2772 * With CMD every object can have two locks in different namespaces:
2773 * lookup lock in space of mds storing direntry and update/open lock in
2774 * space of mds storing inode. Thus we check all targets, not only that
2775 * one fid was created in.
2777 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
2778 rc = md_lock_match(lmv->tgts[i].ltd_exp, flags, fid,
2779 type, policy, mode, lockh);
2787 int lmv_get_lustre_md(struct obd_export *exp, struct ptlrpc_request *req,
2788 struct obd_export *dt_exp, struct obd_export *md_exp,
2789 struct lustre_md *md)
2791 struct obd_device *obd = exp->exp_obd;
2792 struct lmv_obd *lmv = &obd->u.lmv;
2795 rc = md_get_lustre_md(lmv->tgts[0].ltd_exp, req, dt_exp, md_exp, md);
2799 int lmv_free_lustre_md(struct obd_export *exp, struct lustre_md *md)
2801 struct obd_device *obd = exp->exp_obd;
2802 struct lmv_obd *lmv = &obd->u.lmv;
2806 obd_free_memmd(exp, (void *)&md->mea);
2807 RETURN(md_free_lustre_md(lmv->tgts[0].ltd_exp, md));
2810 int lmv_set_open_replay_data(struct obd_export *exp,
2811 struct obd_client_handle *och,
2812 struct ptlrpc_request *open_req)
2814 struct obd_device *obd = exp->exp_obd;
2815 struct lmv_obd *lmv = &obd->u.lmv;
2816 struct lmv_tgt_desc *tgt;
2819 tgt = lmv_find_target(lmv, &och->och_fid);
2821 RETURN(PTR_ERR(tgt));
2823 RETURN(md_set_open_replay_data(tgt->ltd_exp, och, open_req));
2826 int lmv_clear_open_replay_data(struct obd_export *exp,
2827 struct obd_client_handle *och)
2829 struct obd_device *obd = exp->exp_obd;
2830 struct lmv_obd *lmv = &obd->u.lmv;
2831 struct lmv_tgt_desc *tgt;
2834 tgt = lmv_find_target(lmv, &och->och_fid);
2836 RETURN(PTR_ERR(tgt));
2838 RETURN(md_clear_open_replay_data(tgt->ltd_exp, och));
2841 static int lmv_get_remote_perm(struct obd_export *exp,
2842 const struct lu_fid *fid,
2843 struct obd_capa *oc, __u32 suppgid,
2844 struct ptlrpc_request **request)
2846 struct obd_device *obd = exp->exp_obd;
2847 struct lmv_obd *lmv = &obd->u.lmv;
2848 struct lmv_tgt_desc *tgt;
2852 rc = lmv_check_connect(obd);
2856 tgt = lmv_find_target(lmv, fid);
2858 RETURN(PTR_ERR(tgt));
2860 rc = md_get_remote_perm(tgt->ltd_exp, fid, oc, suppgid, request);
2864 static int lmv_renew_capa(struct obd_export *exp, struct obd_capa *oc,
2867 struct obd_device *obd = exp->exp_obd;
2868 struct lmv_obd *lmv = &obd->u.lmv;
2869 struct lmv_tgt_desc *tgt;
2873 rc = lmv_check_connect(obd);
2877 tgt = lmv_find_target(lmv, &oc->c_capa.lc_fid);
2879 RETURN(PTR_ERR(tgt));
2881 rc = md_renew_capa(tgt->ltd_exp, oc, cb);
2885 int lmv_unpack_capa(struct obd_export *exp, struct ptlrpc_request *req,
2886 const struct req_msg_field *field, struct obd_capa **oc)
2888 struct obd_device *obd = exp->exp_obd;
2889 struct lmv_obd *lmv = &obd->u.lmv;
2893 rc = md_unpack_capa(lmv->tgts[0].ltd_exp, req, field, oc);
2897 int lmv_intent_getattr_async(struct obd_export *exp,
2898 struct md_enqueue_info *minfo,
2899 struct ldlm_enqueue_info *einfo)
2901 struct md_op_data *op_data = &minfo->mi_data;
2902 struct obd_device *obd = exp->exp_obd;
2903 struct lmv_obd *lmv = &obd->u.lmv;
2904 struct lmv_object *obj;
2905 struct lmv_tgt_desc *tgt;
2910 rc = lmv_check_connect(obd);
2914 if (!fid_is_sane(&op_data->op_fid2)) {
2915 obj = lmv_object_find(obd, &op_data->op_fid1);
2916 if (obj && op_data->op_namelen) {
2917 sidx = raw_name2idx(obj->lo_hashtype,
2919 (char *)op_data->op_name,
2920 op_data->op_namelen);
2921 op_data->op_fid1 = obj->lo_stripes[sidx].ls_fid;
2922 tgt = lmv_get_target(lmv,
2923 obj->lo_stripes[sidx].ls_mds);
2925 "Choose slave dir ("DFID") -> mds #%d\n",
2926 PFID(&op_data->op_fid1), tgt->ltd_idx);
2928 tgt = lmv_find_target(lmv, &op_data->op_fid1);
2931 lmv_object_put(obj);
2933 op_data->op_fid1 = op_data->op_fid2;
2934 tgt = lmv_find_target(lmv, &op_data->op_fid2);
2935 op_data->op_bias = MDS_CROSS_REF;
2937 * Unfortunately, we have to lie to MDC/MDS to retrieve
2938 * attributes llite needs.
2940 if (minfo->mi_it.it_op & IT_LOOKUP)
2941 minfo->mi_it.it_op = IT_GETATTR;
2945 RETURN(PTR_ERR(tgt));
2947 rc = md_intent_getattr_async(tgt->ltd_exp, minfo, einfo);
2951 int lmv_revalidate_lock(struct obd_export *exp,
2952 struct lookup_intent *it,
2955 struct obd_device *obd = exp->exp_obd;
2956 struct lmv_obd *lmv = &obd->u.lmv;
2957 struct lmv_tgt_desc *tgt;
2961 rc = lmv_check_connect(obd);
2965 tgt = lmv_find_target(lmv, fid);
2967 RETURN(PTR_ERR(tgt));
2969 rc = md_revalidate_lock(tgt->ltd_exp, it, fid);
2974 struct obd_ops lmv_obd_ops = {
2975 .o_owner = THIS_MODULE,
2976 .o_setup = lmv_setup,
2977 .o_cleanup = lmv_cleanup,
2978 .o_precleanup = lmv_precleanup,
2979 .o_process_config = lmv_process_config,
2980 .o_connect = lmv_connect,
2981 .o_disconnect = lmv_disconnect,
2982 .o_statfs = lmv_statfs,
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_get_uuid = lmv_get_uuid,
2989 .o_iocontrol = lmv_iocontrol,
2990 .o_fid_delete = lmv_fid_delete
2993 struct md_ops lmv_md_ops = {
2994 .m_getstatus = lmv_getstatus,
2995 .m_change_cbdata = lmv_change_cbdata,
2996 .m_close = lmv_close,
2997 .m_create = lmv_create,
2998 .m_done_writing = lmv_done_writing,
2999 .m_enqueue = lmv_enqueue,
3000 .m_getattr = lmv_getattr,
3001 .m_getxattr = lmv_getxattr,
3002 .m_getattr_name = lmv_getattr_name,
3003 .m_intent_lock = lmv_intent_lock,
3005 .m_rename = lmv_rename,
3006 .m_setattr = lmv_setattr,
3007 .m_setxattr = lmv_setxattr,
3009 .m_readpage = lmv_readpage,
3010 .m_unlink = lmv_unlink,
3011 .m_init_ea_size = lmv_init_ea_size,
3012 .m_cancel_unused = lmv_cancel_unused,
3013 .m_set_lock_data = lmv_set_lock_data,
3014 .m_lock_match = lmv_lock_match,
3015 .m_get_lustre_md = lmv_get_lustre_md,
3016 .m_free_lustre_md = lmv_free_lustre_md,
3017 .m_set_open_replay_data = lmv_set_open_replay_data,
3018 .m_clear_open_replay_data = lmv_clear_open_replay_data,
3019 .m_renew_capa = lmv_renew_capa,
3020 .m_unpack_capa = lmv_unpack_capa,
3021 .m_get_remote_perm = lmv_get_remote_perm,
3022 .m_intent_getattr_async = lmv_intent_getattr_async,
3023 .m_revalidate_lock = lmv_revalidate_lock
3026 static quota_interface_t *quota_interface;
3027 extern quota_interface_t lmv_quota_interface;
3029 int __init lmv_init(void)
3031 struct lprocfs_static_vars lvars;
3034 lmv_object_cache = cfs_mem_cache_create("lmv_objects",
3035 sizeof(struct lmv_object),
3037 if (!lmv_object_cache) {
3038 CERROR("Error allocating lmv objects cache\n");
3042 lprocfs_lmv_init_vars(&lvars);
3044 request_module("lquota");
3045 quota_interface = PORTAL_SYMBOL_GET(lmv_quota_interface);
3046 init_obd_quota_ops(quota_interface, &lmv_obd_ops);
3048 rc = class_register_type(&lmv_obd_ops, &lmv_md_ops,
3049 lvars.module_vars, LUSTRE_LMV_NAME, NULL);
3051 if (quota_interface)
3052 PORTAL_SYMBOL_PUT(lmv_quota_interface);
3053 cfs_mem_cache_destroy(lmv_object_cache);
3060 static void lmv_exit(void)
3062 if (quota_interface)
3063 PORTAL_SYMBOL_PUT(lmv_quota_interface);
3065 class_unregister_type(LUSTRE_LMV_NAME);
3067 LASSERTF(atomic_read(&lmv_object_count) == 0,
3068 "Can't free lmv objects cache, %d object(s) busy\n",
3069 atomic_read(&lmv_object_count));
3070 cfs_mem_cache_destroy(lmv_object_cache);
3073 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
3074 MODULE_DESCRIPTION("Lustre Logical Metadata Volume OBD driver");
3075 MODULE_LICENSE("GPL");
3077 module_init(lmv_init);
3078 module_exit(lmv_exit);