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 (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
30 * Use is subject to license terms.
32 * Copyright (c) 2011, 2012, Whamcloud, Inc.
35 * This file is part of Lustre, http://www.lustre.org/
36 * Lustre is a trademark of Sun Microsystems, Inc.
40 # define EXPORT_SYMTAB
42 #define DEBUG_SUBSYSTEM S_LMV
44 #include <linux/slab.h>
45 #include <linux/module.h>
46 #include <linux/init.h>
47 #include <linux/slab.h>
48 #include <linux/pagemap.h>
50 #include <asm/div64.h>
51 #include <linux/seq_file.h>
52 #include <linux/namei.h>
54 #include <liblustre.h>
57 #include <lustre_log.h>
58 #include <obd_support.h>
59 #include <lustre_lib.h>
60 #include <lustre_net.h>
61 #include <obd_class.h>
62 #include <lprocfs_status.h>
63 #include <lustre_lite.h>
64 #include <lustre_fid.h>
65 #include "lmv_internal.h"
68 cfs_mem_cache_t *lmv_object_cache;
69 cfs_atomic_t lmv_object_count = CFS_ATOMIC_INIT(0);
71 static void lmv_activate_target(struct lmv_obd *lmv,
72 struct lmv_tgt_desc *tgt,
75 if (tgt->ltd_active == activate)
78 tgt->ltd_active = activate;
79 lmv->desc.ld_active_tgt_count += (activate ? 1 : -1);
85 * -EINVAL : UUID can't be found in the LMV's target list
86 * -ENOTCONN: The UUID is found, but the target connection is bad (!)
87 * -EBADF : The UUID is found, but the OBD of the wrong type (!)
89 static int lmv_set_mdc_active(struct lmv_obd *lmv, struct obd_uuid *uuid,
92 struct lmv_tgt_desc *tgt;
93 struct obd_device *obd;
98 CDEBUG(D_INFO, "Searching in lmv %p for uuid %s (activate=%d)\n",
99 lmv, uuid->uuid, activate);
101 cfs_spin_lock(&lmv->lmv_lock);
102 for (i = 0, tgt = lmv->tgts; i < lmv->desc.ld_tgt_count; i++, tgt++) {
103 if (tgt->ltd_exp == NULL)
106 CDEBUG(D_INFO, "Target idx %d is %s conn "LPX64"\n",
107 i, tgt->ltd_uuid.uuid, tgt->ltd_exp->exp_handle.h_cookie);
109 if (obd_uuid_equals(uuid, &tgt->ltd_uuid))
113 if (i == lmv->desc.ld_tgt_count)
114 GOTO(out_lmv_lock, rc = -EINVAL);
116 obd = class_exp2obd(tgt->ltd_exp);
118 GOTO(out_lmv_lock, rc = -ENOTCONN);
120 CDEBUG(D_INFO, "Found OBD %s=%s device %d (%p) type %s at LMV idx %d\n",
121 obd->obd_name, obd->obd_uuid.uuid, obd->obd_minor, obd,
122 obd->obd_type->typ_name, i);
123 LASSERT(strcmp(obd->obd_type->typ_name, LUSTRE_MDC_NAME) == 0);
125 if (tgt->ltd_active == activate) {
126 CDEBUG(D_INFO, "OBD %p already %sactive!\n", obd,
127 activate ? "" : "in");
128 GOTO(out_lmv_lock, rc);
131 CDEBUG(D_INFO, "Marking OBD %p %sactive\n", obd,
132 activate ? "" : "in");
133 lmv_activate_target(lmv, tgt, activate);
137 cfs_spin_unlock(&lmv->lmv_lock);
141 static int lmv_set_mdc_data(struct lmv_obd *lmv, struct obd_uuid *uuid,
142 struct obd_connect_data *data)
144 struct lmv_tgt_desc *tgt;
148 LASSERT(data != NULL);
150 cfs_spin_lock(&lmv->lmv_lock);
151 for (i = 0, tgt = lmv->tgts; i < lmv->desc.ld_tgt_count; i++, tgt++) {
152 if (tgt->ltd_exp == NULL)
155 if (obd_uuid_equals(uuid, &tgt->ltd_uuid)) {
156 lmv->datas[tgt->ltd_idx] = *data;
160 cfs_spin_unlock(&lmv->lmv_lock);
164 struct obd_uuid *lmv_get_uuid(struct obd_export *exp) {
165 struct obd_device *obd = exp->exp_obd;
166 struct lmv_obd *lmv = &obd->u.lmv;
167 return obd_get_uuid(lmv->tgts[0].ltd_exp);
170 static int lmv_notify(struct obd_device *obd, struct obd_device *watched,
171 enum obd_notify_event ev, void *data)
173 struct obd_connect_data *conn_data;
174 struct lmv_obd *lmv = &obd->u.lmv;
175 struct obd_uuid *uuid;
179 if (strcmp(watched->obd_type->typ_name, LUSTRE_MDC_NAME)) {
180 CERROR("unexpected notification of %s %s!\n",
181 watched->obd_type->typ_name,
186 uuid = &watched->u.cli.cl_target_uuid;
187 if (ev == OBD_NOTIFY_ACTIVE || ev == OBD_NOTIFY_INACTIVE) {
189 * Set MDC as active before notifying the observer, so the
190 * observer can use the MDC normally.
192 rc = lmv_set_mdc_active(lmv, uuid,
193 ev == OBD_NOTIFY_ACTIVE);
195 CERROR("%sactivation of %s failed: %d\n",
196 ev == OBD_NOTIFY_ACTIVE ? "" : "de",
200 } else if (ev == OBD_NOTIFY_OCD) {
201 conn_data = &watched->u.cli.cl_import->imp_connect_data;
204 * Set connect data to desired target, update exp_connect_flags.
206 rc = lmv_set_mdc_data(lmv, uuid, conn_data);
208 CERROR("can't set connect data to target %s, rc %d\n",
214 * XXX: Make sure that ocd_connect_flags from all targets are
215 * the same. Otherwise one of MDTs runs wrong version or
216 * something like this. --umka
218 obd->obd_self_export->exp_connect_flags =
219 conn_data->ocd_connect_flags;
222 else if (ev == OBD_NOTIFY_DISCON) {
224 * For disconnect event, flush fld cache for failout MDS case.
226 fld_client_flush(&lmv->lmv_fld);
230 * Pass the notification up the chain.
232 if (obd->obd_observer)
233 rc = obd_notify(obd->obd_observer, watched, ev, data);
239 * This is fake connect function. Its purpose is to initialize lmv and say
240 * caller that everything is okay. Real connection will be performed later.
242 static int lmv_connect(const struct lu_env *env,
243 struct obd_export **exp, struct obd_device *obd,
244 struct obd_uuid *cluuid, struct obd_connect_data *data,
248 struct proc_dir_entry *lmv_proc_dir;
250 struct lmv_obd *lmv = &obd->u.lmv;
251 struct lustre_handle conn = { 0 };
256 * We don't want to actually do the underlying connections more than
257 * once, so keep track.
260 if (lmv->refcount > 1) {
265 rc = class_connect(&conn, obd, cluuid);
267 CERROR("class_connection() returned %d\n", rc);
271 *exp = class_conn2export(&conn);
272 class_export_get(*exp);
276 lmv->cluuid = *cluuid;
279 lmv->conn_data = *data;
282 lmv_proc_dir = lprocfs_register("target_obds", obd->obd_proc_entry,
284 if (IS_ERR(lmv_proc_dir)) {
285 CERROR("could not register /proc/fs/lustre/%s/%s/target_obds.",
286 obd->obd_type->typ_name, obd->obd_name);
292 * All real clients should perform actual connection right away, because
293 * it is possible, that LMV will not have opportunity to connect targets
294 * and MDC stuff will be called directly, for instance while reading
295 * ../mdc/../kbytesfree procfs file, etc.
297 if (data->ocd_connect_flags & OBD_CONNECT_REAL)
298 rc = lmv_check_connect(obd);
303 lprocfs_remove(&lmv_proc_dir);
310 static void lmv_set_timeouts(struct obd_device *obd)
312 struct lmv_tgt_desc *tgts;
317 if (lmv->server_timeout == 0)
320 if (lmv->connected == 0)
323 for (i = 0, tgts = lmv->tgts; i < lmv->desc.ld_tgt_count; i++, tgts++) {
324 if (tgts->ltd_exp == NULL)
327 obd_set_info_async(tgts->ltd_exp, sizeof(KEY_INTERMDS),
328 KEY_INTERMDS, 0, NULL, NULL);
332 static int lmv_init_ea_size(struct obd_export *exp, int easize,
333 int def_easize, int cookiesize)
335 struct obd_device *obd = exp->exp_obd;
336 struct lmv_obd *lmv = &obd->u.lmv;
342 if (lmv->max_easize < easize) {
343 lmv->max_easize = easize;
346 if (lmv->max_def_easize < def_easize) {
347 lmv->max_def_easize = def_easize;
350 if (lmv->max_cookiesize < cookiesize) {
351 lmv->max_cookiesize = cookiesize;
357 if (lmv->connected == 0)
360 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
361 if (lmv->tgts[i].ltd_exp == NULL) {
362 CWARN("%s: NULL export for %d\n", obd->obd_name, i);
366 rc = md_init_ea_size(lmv->tgts[i].ltd_exp, easize, def_easize,
369 CERROR("obd_init_ea_size() failed on MDT target %d, "
370 "error %d.\n", i, rc);
377 #define MAX_STRING_SIZE 128
379 int lmv_connect_mdc(struct obd_device *obd, struct lmv_tgt_desc *tgt)
382 struct proc_dir_entry *lmv_proc_dir;
384 struct lmv_obd *lmv = &obd->u.lmv;
385 struct obd_uuid *cluuid = &lmv->cluuid;
386 struct obd_connect_data *mdc_data = NULL;
387 struct obd_uuid lmv_mdc_uuid = { "LMV_MDC_UUID" };
388 struct obd_device *mdc_obd;
389 struct obd_export *mdc_exp;
390 struct lu_fld_target target;
394 mdc_obd = class_find_client_obd(&tgt->ltd_uuid, LUSTRE_MDC_NAME,
397 CERROR("target %s not attached\n", tgt->ltd_uuid.uuid);
401 CDEBUG(D_CONFIG, "connect to %s(%s) - %s, %s FOR %s\n",
402 mdc_obd->obd_name, mdc_obd->obd_uuid.uuid,
403 tgt->ltd_uuid.uuid, obd->obd_uuid.uuid,
406 if (!mdc_obd->obd_set_up) {
407 CERROR("target %s is not set up\n", tgt->ltd_uuid.uuid);
411 rc = obd_connect(NULL, &mdc_exp, mdc_obd, &lmv_mdc_uuid,
412 &lmv->conn_data, NULL);
414 CERROR("target %s connect error %d\n", tgt->ltd_uuid.uuid, rc);
419 * Init fid sequence client for this mdc and add new fld target.
421 rc = obd_fid_init(mdc_exp);
425 target.ft_srv = NULL;
426 target.ft_exp = mdc_exp;
427 target.ft_idx = tgt->ltd_idx;
429 fld_client_add_target(&lmv->lmv_fld, &target);
431 mdc_data = &class_exp2cliimp(mdc_exp)->imp_connect_data;
433 rc = obd_register_observer(mdc_obd, obd);
435 obd_disconnect(mdc_exp);
436 CERROR("target %s register_observer error %d\n",
437 tgt->ltd_uuid.uuid, rc);
441 if (obd->obd_observer) {
443 * Tell the observer about the new target.
445 rc = obd_notify(obd->obd_observer, mdc_exp->exp_obd,
446 OBD_NOTIFY_ACTIVE, (void *)(tgt - lmv->tgts));
448 obd_disconnect(mdc_exp);
454 tgt->ltd_exp = mdc_exp;
455 lmv->desc.ld_active_tgt_count++;
458 * Copy connect data, it may be used later.
460 lmv->datas[tgt->ltd_idx] = *mdc_data;
462 md_init_ea_size(tgt->ltd_exp, lmv->max_easize,
463 lmv->max_def_easize, lmv->max_cookiesize);
465 CDEBUG(D_CONFIG, "Connected to %s(%s) successfully (%d)\n",
466 mdc_obd->obd_name, mdc_obd->obd_uuid.uuid,
467 cfs_atomic_read(&obd->obd_refcount));
470 lmv_proc_dir = lprocfs_srch(obd->obd_proc_entry, "target_obds");
472 struct proc_dir_entry *mdc_symlink;
474 LASSERT(mdc_obd->obd_type != NULL);
475 LASSERT(mdc_obd->obd_type->typ_name != NULL);
476 mdc_symlink = lprocfs_add_symlink(mdc_obd->obd_name,
479 mdc_obd->obd_type->typ_name,
481 if (mdc_symlink == NULL) {
482 CERROR("Could not register LMV target "
483 "/proc/fs/lustre/%s/%s/target_obds/%s.",
484 obd->obd_type->typ_name, obd->obd_name,
486 lprocfs_remove(&lmv_proc_dir);
494 int lmv_add_target(struct obd_device *obd, struct obd_uuid *tgt_uuid)
496 struct lmv_obd *lmv = &obd->u.lmv;
497 struct lmv_tgt_desc *tgt;
501 CDEBUG(D_CONFIG, "Target uuid: %s.\n", tgt_uuid->uuid);
505 if (lmv->desc.ld_active_tgt_count >= LMV_MAX_TGT_COUNT) {
506 lmv_init_unlock(lmv);
507 CERROR("Can't add %s, LMV module compiled for %d MDCs. "
508 "That many MDCs already configured.\n",
509 tgt_uuid->uuid, LMV_MAX_TGT_COUNT);
512 if (lmv->desc.ld_tgt_count == 0) {
513 struct obd_device *mdc_obd;
515 mdc_obd = class_find_client_obd(tgt_uuid, LUSTRE_MDC_NAME,
518 lmv_init_unlock(lmv);
519 CERROR("Target %s not attached\n", tgt_uuid->uuid);
523 rc = obd_llog_init(obd, &obd->obd_olg, mdc_obd, NULL);
525 lmv_init_unlock(lmv);
526 CERROR("lmv failed to setup llogging subsystems\n");
529 cfs_spin_lock(&lmv->lmv_lock);
530 tgt = lmv->tgts + lmv->desc.ld_tgt_count++;
531 tgt->ltd_uuid = *tgt_uuid;
532 cfs_spin_unlock(&lmv->lmv_lock);
534 if (lmv->connected) {
535 rc = lmv_connect_mdc(obd, tgt);
537 cfs_spin_lock(&lmv->lmv_lock);
538 lmv->desc.ld_tgt_count--;
539 memset(tgt, 0, sizeof(*tgt));
540 cfs_spin_unlock(&lmv->lmv_lock);
542 int easize = sizeof(struct lmv_stripe_md) +
543 lmv->desc.ld_tgt_count *
544 sizeof(struct lu_fid);
545 lmv_init_ea_size(obd->obd_self_export, easize, 0, 0);
549 lmv_init_unlock(lmv);
553 int lmv_check_connect(struct obd_device *obd)
555 struct lmv_obd *lmv = &obd->u.lmv;
556 struct lmv_tgt_desc *tgt;
566 if (lmv->connected) {
567 lmv_init_unlock(lmv);
571 if (lmv->desc.ld_tgt_count == 0) {
572 CERROR("%s: no targets configured.\n", obd->obd_name);
576 CDEBUG(D_CONFIG, "Time to connect %s to %s\n",
577 lmv->cluuid.uuid, obd->obd_name);
579 LASSERT(lmv->tgts != NULL);
581 for (i = 0, tgt = lmv->tgts; i < lmv->desc.ld_tgt_count; i++, tgt++) {
582 rc = lmv_connect_mdc(obd, tgt);
587 lmv_set_timeouts(obd);
588 class_export_put(lmv->exp);
590 easize = lmv_get_easize(lmv);
591 lmv_init_ea_size(obd->obd_self_export, easize, 0, 0);
592 lmv_init_unlock(lmv);
601 --lmv->desc.ld_active_tgt_count;
602 rc2 = obd_disconnect(tgt->ltd_exp);
604 CERROR("LMV target %s disconnect on "
605 "MDC idx %d: error %d\n",
606 tgt->ltd_uuid.uuid, i, rc2);
610 class_disconnect(lmv->exp);
611 lmv_init_unlock(lmv);
615 static int lmv_disconnect_mdc(struct obd_device *obd, struct lmv_tgt_desc *tgt)
618 struct proc_dir_entry *lmv_proc_dir;
620 struct lmv_obd *lmv = &obd->u.lmv;
621 struct obd_device *mdc_obd;
625 LASSERT(tgt != NULL);
626 LASSERT(obd != NULL);
628 mdc_obd = class_exp2obd(tgt->ltd_exp);
631 mdc_obd->obd_force = obd->obd_force;
632 mdc_obd->obd_fail = obd->obd_fail;
633 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 if ((index >= count))
748 if (!lmv->tgts[index].ltd_active)
751 mdc_obd = class_exp2obd(lmv->tgts[index].ltd_exp);
756 if (cfs_copy_to_user(data->ioc_pbuf2, obd2cli_tgt(mdc_obd),
757 min((int) data->ioc_plen2,
758 (int) sizeof(struct obd_uuid))))
761 rc = obd_statfs(mdc_obd, &stat_buf,
762 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
766 if (cfs_copy_to_user(data->ioc_pbuf1, &stat_buf,
767 min((int) data->ioc_plen1,
768 (int) sizeof(stat_buf))))
772 case OBD_IOC_QUOTACTL: {
773 struct if_quotactl *qctl = karg;
774 struct lmv_tgt_desc *tgt = NULL;
775 struct obd_quotactl *oqctl;
777 if (qctl->qc_valid == QC_MDTIDX) {
778 if (qctl->qc_idx < 0 || count <= qctl->qc_idx)
781 tgt = &lmv->tgts[qctl->qc_idx];
784 } else if (qctl->qc_valid == QC_UUID) {
785 for (i = 0; i < count; i++) {
787 if (!obd_uuid_equals(&tgt->ltd_uuid,
791 if (tgt->ltd_exp == NULL)
803 LASSERT(tgt && tgt->ltd_exp);
804 OBD_ALLOC_PTR(oqctl);
808 QCTL_COPY(oqctl, qctl);
809 rc = obd_quotactl(tgt->ltd_exp, oqctl);
811 QCTL_COPY(qctl, oqctl);
812 qctl->qc_valid = QC_MDTIDX;
813 qctl->obd_uuid = tgt->ltd_uuid;
818 case OBD_IOC_CHANGELOG_SEND:
819 case OBD_IOC_CHANGELOG_CLEAR: {
820 struct ioc_changelog *icc = karg;
822 if (icc->icc_mdtindex >= count)
825 rc = obd_iocontrol(cmd, lmv->tgts[icc->icc_mdtindex].ltd_exp,
826 sizeof(*icc), icc, NULL);
829 case LL_IOC_GET_CONNECT_FLAGS: {
830 rc = obd_iocontrol(cmd, lmv->tgts[0].ltd_exp, len, karg, uarg);
835 for (i = 0; i < count; i++) {
837 struct obd_device *mdc_obd;
839 if (lmv->tgts[i].ltd_exp == NULL)
841 /* ll_umount_begin() sets force flag but for lmv, not
842 * mdc. Let's pass it through */
843 mdc_obd = class_exp2obd(lmv->tgts[i].ltd_exp);
844 mdc_obd->obd_force = obddev->obd_force;
845 err = obd_iocontrol(cmd, lmv->tgts[i].ltd_exp, len,
847 if (err == -ENODATA && cmd == OBD_IOC_POLL_QUOTACHECK) {
850 if (lmv->tgts[i].ltd_active) {
851 CERROR("error: iocontrol MDC %s on MDT"
852 "idx %d cmd %x: err = %d\n",
853 lmv->tgts[i].ltd_uuid.uuid,
868 static int lmv_all_chars_policy(int count, const char *name,
879 static int lmv_nid_policy(struct lmv_obd *lmv)
881 struct obd_import *imp;
885 * XXX: To get nid we assume that underlying obd device is mdc.
887 imp = class_exp2cliimp(lmv->tgts[0].ltd_exp);
888 id = imp->imp_connection->c_self ^ (imp->imp_connection->c_self >> 32);
889 return id % lmv->desc.ld_tgt_count;
892 static int lmv_choose_mds(struct lmv_obd *lmv, struct md_op_data *op_data,
893 placement_policy_t placement)
896 case PLACEMENT_CHAR_POLICY:
897 return lmv_all_chars_policy(lmv->desc.ld_tgt_count,
899 op_data->op_namelen);
900 case PLACEMENT_NID_POLICY:
901 return lmv_nid_policy(lmv);
907 CERROR("Unsupported placement policy %x\n", placement);
912 * This is _inode_ placement policy function (not name).
914 static int lmv_placement_policy(struct obd_device *obd,
915 struct md_op_data *op_data,
918 struct lmv_obd *lmv = &obd->u.lmv;
919 struct lmv_object *obj;
923 LASSERT(mds != NULL);
925 if (lmv->desc.ld_tgt_count == 1) {
931 * Allocate new fid on target according to operation type and parent
934 obj = lmv_object_find(obd, &op_data->op_fid1);
935 if (obj != NULL || op_data->op_name == NULL ||
936 op_data->op_opc != LUSTRE_OPC_MKDIR) {
938 * Allocate fid for non-dir or for null name or for case parent
945 * If we have this flag turned on, and we see that
946 * parent dir is split, this means, that caller did not
947 * notice split yet. This is race and we would like to
948 * let caller know that.
950 if (op_data->op_bias & MDS_CHECK_SPLIT)
955 * Allocate new fid on same mds where parent fid is located and
956 * where operation will be sent. In case of split dir, ->op_fid1
957 * and ->op_mds here will contain fid and mds of slave directory
958 * object (assigned by caller).
960 *mds = op_data->op_mds;
964 * Parent directory is not split and we want to create a
965 * directory in it. Let's calculate where to place it according
966 * to operation data @op_data.
968 *mds = lmv_choose_mds(lmv, op_data, lmv->lmv_placement);
973 CERROR("Can't choose MDS, err = %d\n", rc);
975 LASSERT(*mds < lmv->desc.ld_tgt_count);
981 int __lmv_fid_alloc(struct lmv_obd *lmv, struct lu_fid *fid,
984 struct lmv_tgt_desc *tgt;
988 tgt = lmv_get_target(lmv, mds);
991 * New seq alloc and FLD setup should be atomic. Otherwise we may find
992 * on server that seq in new allocated fid is not yet known.
994 cfs_down(&tgt->ltd_fid_sem);
996 if (!tgt->ltd_active)
997 GOTO(out, rc = -ENODEV);
1000 * Asking underlaying tgt layer to allocate new fid.
1002 rc = obd_fid_alloc(tgt->ltd_exp, fid, NULL);
1004 LASSERT(fid_is_sane(fid));
1010 cfs_up(&tgt->ltd_fid_sem);
1014 int lmv_fid_alloc(struct obd_export *exp, struct lu_fid *fid,
1015 struct md_op_data *op_data)
1017 struct obd_device *obd = class_exp2obd(exp);
1018 struct lmv_obd *lmv = &obd->u.lmv;
1023 LASSERT(op_data != NULL);
1024 LASSERT(fid != NULL);
1026 rc = lmv_placement_policy(obd, op_data, &mds);
1028 CERROR("Can't get target for allocating fid, "
1033 rc = __lmv_fid_alloc(lmv, fid, mds);
1035 CERROR("Can't alloc new fid, rc %d\n", rc);
1042 static int lmv_fid_delete(struct obd_export *exp, const struct lu_fid *fid)
1045 LASSERT(exp != NULL && fid != NULL);
1046 if (lmv_object_delete(exp, fid)) {
1047 CDEBUG(D_INODE, "Object "DFID" is destroyed.\n",
1053 static int lmv_setup(struct obd_device *obd, struct lustre_cfg *lcfg)
1055 struct lmv_obd *lmv = &obd->u.lmv;
1056 struct lprocfs_static_vars lvars;
1057 struct lmv_desc *desc;
1062 if (LUSTRE_CFG_BUFLEN(lcfg, 1) < 1) {
1063 CERROR("LMV setup requires a descriptor\n");
1067 desc = (struct lmv_desc *)lustre_cfg_buf(lcfg, 1);
1068 if (sizeof(*desc) > LUSTRE_CFG_BUFLEN(lcfg, 1)) {
1069 CERROR("Lmv descriptor size wrong: %d > %d\n",
1070 (int)sizeof(*desc), LUSTRE_CFG_BUFLEN(lcfg, 1));
1074 lmv->tgts_size = LMV_MAX_TGT_COUNT * sizeof(struct lmv_tgt_desc);
1076 OBD_ALLOC(lmv->tgts, lmv->tgts_size);
1077 if (lmv->tgts == NULL)
1080 for (i = 0; i < LMV_MAX_TGT_COUNT; i++) {
1081 cfs_sema_init(&lmv->tgts[i].ltd_fid_sem, 1);
1082 lmv->tgts[i].ltd_idx = i;
1085 lmv->datas_size = LMV_MAX_TGT_COUNT * sizeof(struct obd_connect_data);
1087 OBD_ALLOC(lmv->datas, lmv->datas_size);
1088 if (lmv->datas == NULL)
1089 GOTO(out_free_tgts, rc = -ENOMEM);
1091 obd_str2uuid(&lmv->desc.ld_uuid, desc->ld_uuid.uuid);
1092 lmv->desc.ld_tgt_count = 0;
1093 lmv->desc.ld_active_tgt_count = 0;
1094 lmv->max_cookiesize = 0;
1095 lmv->max_def_easize = 0;
1096 lmv->max_easize = 0;
1097 lmv->lmv_placement = PLACEMENT_CHAR_POLICY;
1099 cfs_spin_lock_init(&lmv->lmv_lock);
1100 cfs_sema_init(&lmv->init_sem, 1);
1102 rc = lmv_object_setup(obd);
1104 CERROR("Can't setup LMV object manager, error %d.\n", rc);
1105 GOTO(out_free_datas, rc);
1108 lprocfs_lmv_init_vars(&lvars);
1109 lprocfs_obd_setup(obd, lvars.obd_vars);
1112 rc = lprocfs_seq_create(obd->obd_proc_entry, "target_obd",
1113 0444, &lmv_proc_target_fops, obd);
1115 CWARN("%s: error adding LMV target_obd file: rc = %d\n",
1119 rc = fld_client_init(&lmv->lmv_fld, obd->obd_name,
1120 LUSTRE_CLI_FLD_HASH_DHT);
1122 CERROR("Can't init FLD, err %d\n", rc);
1123 GOTO(out_free_datas, rc);
1129 OBD_FREE(lmv->datas, lmv->datas_size);
1132 OBD_FREE(lmv->tgts, lmv->tgts_size);
1137 static int lmv_cleanup(struct obd_device *obd)
1139 struct lmv_obd *lmv = &obd->u.lmv;
1142 fld_client_fini(&lmv->lmv_fld);
1143 lmv_object_cleanup(obd);
1144 OBD_FREE(lmv->datas, lmv->datas_size);
1145 OBD_FREE(lmv->tgts, lmv->tgts_size);
1150 static int lmv_process_config(struct obd_device *obd, obd_count len, void *buf)
1152 struct lustre_cfg *lcfg = buf;
1153 struct obd_uuid tgt_uuid;
1157 switch(lcfg->lcfg_command) {
1159 if (LUSTRE_CFG_BUFLEN(lcfg, 1) > sizeof(tgt_uuid.uuid))
1160 GOTO(out, rc = -EINVAL);
1162 obd_str2uuid(&tgt_uuid, lustre_cfg_string(lcfg, 1));
1163 rc = lmv_add_target(obd, &tgt_uuid);
1166 CERROR("Unknown command: %d\n", lcfg->lcfg_command);
1167 GOTO(out, rc = -EINVAL);
1174 static int lmv_statfs(struct obd_device *obd, struct obd_statfs *osfs,
1175 __u64 max_age, __u32 flags)
1177 struct lmv_obd *lmv = &obd->u.lmv;
1178 struct obd_statfs *temp;
1183 rc = lmv_check_connect(obd);
1187 OBD_ALLOC(temp, sizeof(*temp));
1191 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
1192 if (lmv->tgts[i].ltd_exp == NULL)
1195 rc = obd_statfs(lmv->tgts[i].ltd_exp->exp_obd, temp,
1198 CERROR("can't stat MDS #%d (%s), error %d\n", i,
1199 lmv->tgts[i].ltd_exp->exp_obd->obd_name,
1201 GOTO(out_free_temp, rc);
1206 osfs->os_bavail += temp->os_bavail;
1207 osfs->os_blocks += temp->os_blocks;
1208 osfs->os_ffree += temp->os_ffree;
1209 osfs->os_files += temp->os_files;
1215 OBD_FREE(temp, sizeof(*temp));
1219 static int lmv_getstatus(struct obd_export *exp,
1221 struct obd_capa **pc)
1223 struct obd_device *obd = exp->exp_obd;
1224 struct lmv_obd *lmv = &obd->u.lmv;
1228 rc = lmv_check_connect(obd);
1232 rc = md_getstatus(lmv->tgts[0].ltd_exp, fid, pc);
1236 static int lmv_getxattr(struct obd_export *exp, const struct lu_fid *fid,
1237 struct obd_capa *oc, obd_valid valid, const char *name,
1238 const char *input, int input_size, int output_size,
1239 int flags, struct ptlrpc_request **request)
1241 struct obd_device *obd = exp->exp_obd;
1242 struct lmv_obd *lmv = &obd->u.lmv;
1243 struct lmv_tgt_desc *tgt;
1247 rc = lmv_check_connect(obd);
1251 tgt = lmv_find_target(lmv, fid);
1253 RETURN(PTR_ERR(tgt));
1255 rc = md_getxattr(tgt->ltd_exp, fid, oc, valid, name, input,
1256 input_size, output_size, flags, request);
1261 static int lmv_setxattr(struct obd_export *exp, const struct lu_fid *fid,
1262 struct obd_capa *oc, obd_valid valid, const char *name,
1263 const char *input, int input_size, int output_size,
1264 int flags, __u32 suppgid,
1265 struct ptlrpc_request **request)
1267 struct obd_device *obd = exp->exp_obd;
1268 struct lmv_obd *lmv = &obd->u.lmv;
1269 struct lmv_tgt_desc *tgt;
1273 rc = lmv_check_connect(obd);
1277 tgt = lmv_find_target(lmv, fid);
1279 RETURN(PTR_ERR(tgt));
1281 rc = md_setxattr(tgt->ltd_exp, fid, oc, valid, name, input,
1282 input_size, output_size, flags, suppgid,
1288 static int lmv_getattr(struct obd_export *exp, struct md_op_data *op_data,
1289 struct ptlrpc_request **request)
1291 struct obd_device *obd = exp->exp_obd;
1292 struct lmv_obd *lmv = &obd->u.lmv;
1293 struct lmv_tgt_desc *tgt;
1294 struct lmv_object *obj;
1299 rc = lmv_check_connect(obd);
1303 tgt = lmv_find_target(lmv, &op_data->op_fid1);
1305 RETURN(PTR_ERR(tgt));
1307 if (op_data->op_valid & OBD_MD_MDTIDX) {
1308 op_data->op_mds = tgt->ltd_idx;
1312 rc = md_getattr(tgt->ltd_exp, op_data, request);
1316 obj = lmv_object_find_lock(obd, &op_data->op_fid1);
1318 CDEBUG(D_INODE, "GETATTR for "DFID" %s\n", PFID(&op_data->op_fid1),
1319 obj ? "(split)" : "");
1322 * If object is split, then we loop over all the slaves and gather size
1323 * attribute. In ideal world we would have to gather also mds field from
1324 * all slaves, as object is spread over the cluster and this is
1325 * definitely interesting information and it is not good to loss it,
1329 struct mdt_body *body;
1331 if (*request == NULL) {
1332 lmv_object_put(obj);
1336 body = req_capsule_server_get(&(*request)->rq_pill,
1338 LASSERT(body != NULL);
1340 for (i = 0; i < obj->lo_objcount; i++) {
1341 if (lmv->tgts[i].ltd_exp == NULL) {
1342 CWARN("%s: NULL export for %d\n",
1348 * Skip master object.
1350 if (lu_fid_eq(&obj->lo_fid, &obj->lo_stripes[i].ls_fid))
1353 body->size += obj->lo_stripes[i].ls_size;
1356 lmv_object_put_unlock(obj);
1362 static int lmv_change_cbdata(struct obd_export *exp, const struct lu_fid *fid,
1363 ldlm_iterator_t it, void *data)
1365 struct obd_device *obd = exp->exp_obd;
1366 struct lmv_obd *lmv = &obd->u.lmv;
1371 rc = lmv_check_connect(obd);
1375 CDEBUG(D_INODE, "CBDATA for "DFID"\n", PFID(fid));
1378 * With CMD every object can have two locks in different namespaces:
1379 * lookup lock in space of mds storing direntry and update/open lock in
1380 * space of mds storing inode.
1382 for (i = 0; i < lmv->desc.ld_tgt_count; i++)
1383 md_change_cbdata(lmv->tgts[i].ltd_exp, fid, it, data);
1388 static int lmv_find_cbdata(struct obd_export *exp, const struct lu_fid *fid,
1389 ldlm_iterator_t it, void *data)
1391 struct obd_device *obd = exp->exp_obd;
1392 struct lmv_obd *lmv = &obd->u.lmv;
1397 rc = lmv_check_connect(obd);
1401 CDEBUG(D_INODE, "CBDATA for "DFID"\n", PFID(fid));
1404 * With CMD every object can have two locks in different namespaces:
1405 * lookup lock in space of mds storing direntry and update/open lock in
1406 * space of mds storing inode.
1408 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
1409 rc = md_find_cbdata(lmv->tgts[i].ltd_exp, fid, it, data);
1418 static int lmv_close(struct obd_export *exp, struct md_op_data *op_data,
1419 struct md_open_data *mod, struct ptlrpc_request **request)
1421 struct obd_device *obd = exp->exp_obd;
1422 struct lmv_obd *lmv = &obd->u.lmv;
1423 struct lmv_tgt_desc *tgt;
1427 rc = lmv_check_connect(obd);
1431 tgt = lmv_find_target(lmv, &op_data->op_fid1);
1433 RETURN(PTR_ERR(tgt));
1435 CDEBUG(D_INODE, "CLOSE "DFID"\n", PFID(&op_data->op_fid1));
1436 rc = md_close(tgt->ltd_exp, op_data, mod, request);
1441 * Called in the case MDS returns -ERESTART on create on open, what means that
1442 * directory is split and its LMV presentation object has to be updated.
1444 int lmv_handle_split(struct obd_export *exp, const struct lu_fid *fid)
1446 struct obd_device *obd = exp->exp_obd;
1447 struct lmv_obd *lmv = &obd->u.lmv;
1448 struct ptlrpc_request *req = NULL;
1449 struct lmv_tgt_desc *tgt;
1450 struct lmv_object *obj;
1451 struct lustre_md md;
1452 struct md_op_data *op_data;
1459 mealen = lmv_get_easize(lmv);
1461 valid = OBD_MD_FLEASIZE | OBD_MD_FLDIREA | OBD_MD_MEA;
1463 tgt = lmv_find_target(lmv, fid);
1465 RETURN(PTR_ERR(tgt));
1468 * Time to update mea of parent fid.
1471 OBD_ALLOC_PTR(op_data);
1472 if (op_data == NULL)
1475 op_data->op_fid1 = *fid;
1476 op_data->op_mode = mealen;
1477 op_data->op_valid = valid;
1479 rc = md_getattr(tgt->ltd_exp, op_data, &req);
1480 OBD_FREE_PTR(op_data);
1482 CERROR("md_getattr() failed, error %d\n", rc);
1486 rc = md_get_lustre_md(tgt->ltd_exp, req, NULL, exp, &md);
1488 CERROR("md_get_lustre_md() failed, error %d\n", rc);
1493 GOTO(cleanup, rc = -ENODATA);
1495 obj = lmv_object_create(exp, fid, md.mea);
1499 lmv_object_put(obj);
1501 obd_free_memmd(exp, (void *)&md.mea);
1505 ptlrpc_req_finished(req);
1509 int lmv_create(struct obd_export *exp, struct md_op_data *op_data,
1510 const void *data, int datalen, int mode, __u32 uid,
1511 __u32 gid, cfs_cap_t cap_effective, __u64 rdev,
1512 struct ptlrpc_request **request)
1514 struct obd_device *obd = exp->exp_obd;
1515 struct lmv_obd *lmv = &obd->u.lmv;
1516 struct lmv_tgt_desc *tgt;
1517 struct lmv_object *obj;
1523 rc = lmv_check_connect(obd);
1527 if (!lmv->desc.ld_active_tgt_count)
1533 obj = lmv_object_find(obd, &op_data->op_fid1);
1535 sidx = raw_name2idx(obj->lo_hashtype, obj->lo_objcount,
1536 op_data->op_name, op_data->op_namelen);
1537 op_data->op_fid1 = obj->lo_stripes[sidx].ls_fid;
1538 op_data->op_bias &= ~MDS_CHECK_SPLIT;
1539 op_data->op_mds = obj->lo_stripes[sidx].ls_mds;
1540 tgt = lmv_get_target(lmv, op_data->op_mds);
1541 lmv_object_put(obj);
1543 tgt = lmv_find_target(lmv, &op_data->op_fid1);
1544 op_data->op_bias |= MDS_CHECK_SPLIT;
1545 op_data->op_mds = tgt->ltd_idx;
1549 RETURN(PTR_ERR(tgt));
1551 rc = lmv_fid_alloc(exp, &op_data->op_fid2, op_data);
1552 if (rc == -ERESTART)
1557 CDEBUG(D_INODE, "CREATE '%*s' on "DFID" -> mds #%x\n",
1558 op_data->op_namelen, op_data->op_name, PFID(&op_data->op_fid1),
1561 op_data->op_flags |= MF_MDC_CANCEL_FID1;
1562 rc = md_create(tgt->ltd_exp, op_data, data, datalen, mode, uid, gid,
1563 cap_effective, rdev, request);
1565 if (*request == NULL)
1567 CDEBUG(D_INODE, "Created - "DFID"\n", PFID(&op_data->op_fid2));
1568 } else if (rc == -ERESTART) {
1569 LASSERT(*request != NULL);
1570 DEBUG_REQ(D_WARNING|D_RPCTRACE, *request,
1571 "Got -ERESTART during create!\n");
1572 ptlrpc_req_finished(*request);
1576 * Directory got split. Time to update local object and repeat
1577 * the request with proper MDS.
1579 rc = lmv_handle_split(exp, &op_data->op_fid1);
1581 rc = lmv_allocate_slaves(obd, &op_data->op_fid1,
1582 op_data, &op_data->op_fid2);
1591 static int lmv_done_writing(struct obd_export *exp,
1592 struct md_op_data *op_data,
1593 struct md_open_data *mod)
1595 struct obd_device *obd = exp->exp_obd;
1596 struct lmv_obd *lmv = &obd->u.lmv;
1597 struct lmv_tgt_desc *tgt;
1601 rc = lmv_check_connect(obd);
1605 tgt = lmv_find_target(lmv, &op_data->op_fid1);
1607 RETURN(PTR_ERR(tgt));
1609 rc = md_done_writing(tgt->ltd_exp, op_data, mod);
1614 lmv_enqueue_slaves(struct obd_export *exp, struct ldlm_enqueue_info *einfo,
1615 struct lookup_intent *it, struct md_op_data *op_data,
1616 struct lustre_handle *lockh, void *lmm, int lmmsize)
1618 struct obd_device *obd = exp->exp_obd;
1619 struct lmv_obd *lmv = &obd->u.lmv;
1620 struct lmv_stripe_md *mea = op_data->op_mea1;
1621 struct md_op_data *op_data2;
1622 struct lmv_tgt_desc *tgt;
1627 OBD_ALLOC_PTR(op_data2);
1628 if (op_data2 == NULL)
1631 LASSERT(mea != NULL);
1632 for (i = 0; i < mea->mea_count; i++) {
1633 memset(op_data2, 0, sizeof(*op_data2));
1634 op_data2->op_fid1 = mea->mea_ids[i];
1635 op_data2->op_bias = 0;
1637 tgt = lmv_find_target(lmv, &op_data2->op_fid1);
1639 GOTO(cleanup, rc = PTR_ERR(tgt));
1641 if (tgt->ltd_exp == NULL)
1644 rc = md_enqueue(tgt->ltd_exp, einfo, it, op_data2,
1645 lockh + i, lmm, lmmsize, NULL, 0);
1647 CDEBUG(D_INODE, "Take lock on slave "DFID" -> %d/%d\n",
1648 PFID(&mea->mea_ids[i]), rc, it->d.lustre.it_status);
1653 if (it->d.lustre.it_data) {
1654 struct ptlrpc_request *req;
1655 req = (struct ptlrpc_request *)it->d.lustre.it_data;
1656 ptlrpc_req_finished(req);
1659 if (it->d.lustre.it_status)
1660 GOTO(cleanup, rc = it->d.lustre.it_status);
1665 OBD_FREE_PTR(op_data2);
1669 * Drop all taken locks.
1672 if (lockh[i].cookie)
1673 ldlm_lock_decref(lockh + i, einfo->ei_mode);
1674 lockh[i].cookie = 0;
1681 lmv_enqueue_remote(struct obd_export *exp, struct ldlm_enqueue_info *einfo,
1682 struct lookup_intent *it, struct md_op_data *op_data,
1683 struct lustre_handle *lockh, void *lmm, int lmmsize,
1684 int extra_lock_flags)
1686 struct ptlrpc_request *req = it->d.lustre.it_data;
1687 struct obd_device *obd = exp->exp_obd;
1688 struct lmv_obd *lmv = &obd->u.lmv;
1689 struct lustre_handle plock;
1690 struct lmv_tgt_desc *tgt;
1691 struct md_op_data *rdata;
1693 struct mdt_body *body;
1698 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
1699 LASSERT(body != NULL);
1701 if (!(body->valid & OBD_MD_MDS))
1704 CDEBUG(D_INODE, "REMOTE_ENQUEUE '%s' on "DFID" -> "DFID"\n",
1705 LL_IT2STR(it), PFID(&op_data->op_fid1), PFID(&body->fid1));
1708 * We got LOOKUP lock, but we really need attrs.
1710 pmode = it->d.lustre.it_lock_mode;
1711 LASSERT(pmode != 0);
1712 memcpy(&plock, lockh, sizeof(plock));
1713 it->d.lustre.it_lock_mode = 0;
1714 it->d.lustre.it_data = NULL;
1717 it->d.lustre.it_disposition &= ~DISP_ENQ_COMPLETE;
1718 ptlrpc_req_finished(req);
1720 tgt = lmv_find_target(lmv, &fid1);
1722 GOTO(out, rc = PTR_ERR(tgt));
1724 OBD_ALLOC_PTR(rdata);
1726 GOTO(out, rc = -ENOMEM);
1728 rdata->op_fid1 = fid1;
1729 rdata->op_bias = MDS_CROSS_REF;
1731 rc = md_enqueue(tgt->ltd_exp, einfo, it, rdata, lockh,
1732 lmm, lmmsize, NULL, extra_lock_flags);
1733 OBD_FREE_PTR(rdata);
1736 ldlm_lock_decref(&plock, pmode);
1741 lmv_enqueue(struct obd_export *exp, struct ldlm_enqueue_info *einfo,
1742 struct lookup_intent *it, struct md_op_data *op_data,
1743 struct lustre_handle *lockh, void *lmm, int lmmsize,
1744 struct ptlrpc_request **req, int extra_lock_flags)
1746 struct obd_device *obd = exp->exp_obd;
1747 struct lmv_obd *lmv = &obd->u.lmv;
1748 struct lmv_tgt_desc *tgt;
1749 struct lmv_object *obj;
1754 rc = lmv_check_connect(obd);
1758 CDEBUG(D_INODE, "ENQUEUE '%s' on "DFID"\n",
1759 LL_IT2STR(it), PFID(&op_data->op_fid1));
1761 if (op_data->op_mea1 && it && it->it_op == IT_UNLINK) {
1762 rc = lmv_enqueue_slaves(exp, einfo, it, op_data,
1763 lockh, lmm, lmmsize);
1767 obj = lmv_object_find(obd, &op_data->op_fid1);
1768 if (obj && op_data->op_namelen) {
1769 sidx = raw_name2idx(obj->lo_hashtype,
1771 (char *)op_data->op_name,
1772 op_data->op_namelen);
1773 op_data->op_fid1 = obj->lo_stripes[sidx].ls_fid;
1774 tgt = lmv_get_target(lmv, obj->lo_stripes[sidx].ls_mds);
1776 tgt = lmv_find_target(lmv, &op_data->op_fid1);
1779 lmv_object_put(obj);
1782 RETURN(PTR_ERR(tgt));
1784 CDEBUG(D_INODE, "ENQUEUE '%s' on "DFID" -> mds #%d\n",
1785 LL_IT2STR(it), PFID(&op_data->op_fid1), tgt->ltd_idx);
1787 rc = md_enqueue(tgt->ltd_exp, einfo, it, op_data, lockh,
1788 lmm, lmmsize, req, extra_lock_flags);
1790 if (rc == 0 && it && it->it_op == IT_OPEN) {
1791 rc = lmv_enqueue_remote(exp, einfo, it, op_data, lockh,
1792 lmm, lmmsize, extra_lock_flags);
1798 lmv_getattr_name(struct obd_export *exp,struct md_op_data *op_data,
1799 struct ptlrpc_request **request)
1801 struct ptlrpc_request *req = NULL;
1802 struct obd_device *obd = exp->exp_obd;
1803 struct lmv_obd *lmv = &obd->u.lmv;
1804 struct lu_fid rid = op_data->op_fid1;
1805 struct lmv_tgt_desc *tgt;
1806 struct mdt_body *body;
1807 struct lmv_object *obj;
1808 obd_valid valid = op_data->op_valid;
1814 rc = lmv_check_connect(obd);
1821 obj = lmv_object_find(obd, &rid);
1823 sidx = raw_name2idx(obj->lo_hashtype, obj->lo_objcount,
1824 op_data->op_name, op_data->op_namelen);
1825 rid = obj->lo_stripes[sidx].ls_fid;
1826 tgt = lmv_get_target(lmv, obj->lo_stripes[sidx].ls_mds);
1827 op_data->op_mds = obj->lo_stripes[sidx].ls_mds;
1828 valid &= ~OBD_MD_FLCKSPLIT;
1829 lmv_object_put(obj);
1831 tgt = lmv_find_target(lmv, &rid);
1832 valid |= OBD_MD_FLCKSPLIT;
1833 op_data->op_mds = tgt->ltd_idx;
1836 RETURN(PTR_ERR(tgt));
1838 CDEBUG(D_INODE, "GETATTR_NAME for %*s on "DFID" - "DFID" -> mds #%d\n",
1839 op_data->op_namelen, op_data->op_name, PFID(&op_data->op_fid1),
1840 PFID(&rid), tgt->ltd_idx);
1842 op_data->op_valid = valid;
1843 op_data->op_fid1 = rid;
1844 rc = md_getattr_name(tgt->ltd_exp, op_data, request);
1846 body = req_capsule_server_get(&(*request)->rq_pill,
1848 LASSERT(body != NULL);
1850 if (body->valid & OBD_MD_MDS) {
1852 CDEBUG(D_INODE, "Request attrs for "DFID"\n",
1855 tgt = lmv_find_target(lmv, &rid);
1857 ptlrpc_req_finished(*request);
1858 RETURN(PTR_ERR(tgt));
1861 op_data->op_fid1 = rid;
1862 op_data->op_valid |= OBD_MD_FLCROSSREF;
1863 op_data->op_namelen = 0;
1864 op_data->op_name = NULL;
1865 rc = md_getattr_name(tgt->ltd_exp, op_data, &req);
1866 ptlrpc_req_finished(*request);
1869 } else if (rc == -ERESTART) {
1870 LASSERT(*request != NULL);
1871 DEBUG_REQ(D_WARNING|D_RPCTRACE, *request,
1872 "Got -ERESTART during getattr!\n");
1873 ptlrpc_req_finished(*request);
1877 * Directory got split. Time to update local object and repeat
1878 * the request with proper MDS.
1880 rc = lmv_handle_split(exp, &rid);
1887 #define md_op_data_fid(op_data, fl) \
1888 (fl == MF_MDC_CANCEL_FID1 ? &op_data->op_fid1 : \
1889 fl == MF_MDC_CANCEL_FID2 ? &op_data->op_fid2 : \
1890 fl == MF_MDC_CANCEL_FID3 ? &op_data->op_fid3 : \
1891 fl == MF_MDC_CANCEL_FID4 ? &op_data->op_fid4 : \
1894 static int lmv_early_cancel_slaves(struct obd_export *exp,
1895 struct md_op_data *op_data, int op_tgt,
1896 ldlm_mode_t mode, int bits, int flag)
1898 struct obd_device *obd = exp->exp_obd;
1899 struct lmv_obd *lmv = &obd->u.lmv;
1900 ldlm_policy_data_t policy = {{0}};
1901 struct lu_fid *op_fid;
1902 struct lu_fid *st_fid;
1903 struct lmv_tgt_desc *tgt;
1904 struct lmv_object *obj;
1909 op_fid = md_op_data_fid(op_data, flag);
1910 if (!fid_is_sane(op_fid))
1913 obj = lmv_object_find(obd, op_fid);
1917 policy.l_inodebits.bits = bits;
1918 for (i = 0; i < obj->lo_objcount; i++) {
1919 tgt = lmv_get_target(lmv, obj->lo_stripes[i].ls_mds);
1920 st_fid = &obj->lo_stripes[i].ls_fid;
1921 if (op_tgt != tgt->ltd_idx) {
1922 CDEBUG(D_INODE, "EARLY_CANCEL slave "DFID" -> mds #%d\n",
1923 PFID(st_fid), tgt->ltd_idx);
1924 rc = md_cancel_unused(tgt->ltd_exp, st_fid, &policy,
1925 mode, LCF_ASYNC, NULL);
1927 GOTO(out_put_obj, rc);
1930 "EARLY_CANCEL skip operation target %d on "DFID"\n",
1931 op_tgt, PFID(st_fid));
1933 * Do not cancel locks for operation target, they will
1934 * be handled later in underlaying layer when calling
1935 * function we run on behalf of.
1938 op_data->op_flags |= flag;
1943 lmv_object_put(obj);
1947 static int lmv_early_cancel(struct obd_export *exp, struct md_op_data *op_data,
1948 int op_tgt, ldlm_mode_t mode, int bits, int flag)
1950 struct lu_fid *fid = md_op_data_fid(op_data, flag);
1951 struct obd_device *obd = exp->exp_obd;
1952 struct lmv_obd *lmv = &obd->u.lmv;
1953 struct lmv_tgt_desc *tgt;
1954 ldlm_policy_data_t policy = {{0}};
1955 struct lmv_object *obj;
1959 if (!fid_is_sane(fid))
1962 obj = lmv_object_find(obd, fid);
1964 rc = lmv_early_cancel_slaves(exp, op_data, op_tgt, mode,
1966 lmv_object_put(obj);
1968 tgt = lmv_find_target(lmv, fid);
1970 RETURN(PTR_ERR(tgt));
1972 if (tgt->ltd_idx != op_tgt) {
1973 CDEBUG(D_INODE, "EARLY_CANCEL on "DFID"\n", PFID(fid));
1974 policy.l_inodebits.bits = bits;
1975 rc = md_cancel_unused(tgt->ltd_exp, fid, &policy,
1976 mode, LCF_ASYNC, NULL);
1979 "EARLY_CANCEL skip operation target %d on "DFID"\n",
1981 op_data->op_flags |= flag;
1990 * llite passes fid of an target inode in op_data->op_fid1 and id of directory in
1993 static int lmv_link(struct obd_export *exp, struct md_op_data *op_data,
1994 struct ptlrpc_request **request)
1996 struct obd_device *obd = exp->exp_obd;
1997 struct lmv_obd *lmv = &obd->u.lmv;
1998 struct lmv_tgt_desc *tgt;
1999 struct lmv_object *obj;
2006 rc = lmv_check_connect(obd);
2013 LASSERT(op_data->op_namelen != 0);
2015 CDEBUG(D_INODE, "LINK "DFID":%*s to "DFID"\n",
2016 PFID(&op_data->op_fid2), op_data->op_namelen,
2017 op_data->op_name, PFID(&op_data->op_fid1));
2019 obj = lmv_object_find(obd, &op_data->op_fid2);
2021 sidx = raw_name2idx(obj->lo_hashtype,
2024 op_data->op_namelen);
2025 op_data->op_fid2 = obj->lo_stripes[sidx].ls_fid;
2026 mds = obj->lo_stripes[sidx].ls_mds;
2027 lmv_object_put(obj);
2029 rc = lmv_fld_lookup(lmv, &op_data->op_fid2, &mds);
2034 CDEBUG(D_INODE, "Forward to mds #%x ("DFID")\n",
2035 mds, PFID(&op_data->op_fid1));
2037 op_data->op_fsuid = cfs_curproc_fsuid();
2038 op_data->op_fsgid = cfs_curproc_fsgid();
2039 op_data->op_cap = cfs_curproc_cap_pack();
2040 tgt = lmv_get_target(lmv, mds);
2043 * Cancel UPDATE lock on child (fid1).
2045 op_data->op_flags |= MF_MDC_CANCEL_FID2;
2046 rc = lmv_early_cancel(exp, op_data, tgt->ltd_idx, LCK_EX,
2047 MDS_INODELOCK_UPDATE, MF_MDC_CANCEL_FID1);
2049 rc = md_link(tgt->ltd_exp, op_data, request);
2050 if (rc == -ERESTART) {
2051 LASSERT(*request != NULL);
2052 DEBUG_REQ(D_WARNING|D_RPCTRACE, *request,
2053 "Got -ERESTART during link!\n");
2054 ptlrpc_req_finished(*request);
2058 * Directory got split. Time to update local object and repeat
2059 * the request with proper MDS.
2061 rc = lmv_handle_split(exp, &op_data->op_fid2);
2069 static int lmv_rename(struct obd_export *exp, struct md_op_data *op_data,
2070 const char *old, int oldlen, const char *new, int newlen,
2071 struct ptlrpc_request **request)
2073 struct obd_device *obd = exp->exp_obd;
2074 struct lmv_obd *lmv = &obd->u.lmv;
2075 struct lmv_tgt_desc *src_tgt;
2079 struct lmv_object *obj;
2084 LASSERT(oldlen != 0);
2086 CDEBUG(D_INODE, "RENAME %*s in "DFID" to %*s in "DFID"\n",
2087 oldlen, old, PFID(&op_data->op_fid1),
2088 newlen, new, PFID(&op_data->op_fid2));
2090 rc = lmv_check_connect(obd);
2097 obj = lmv_object_find(obd, &op_data->op_fid1);
2099 sidx = raw_name2idx(obj->lo_hashtype, obj->lo_objcount,
2100 (char *)old, oldlen);
2101 op_data->op_fid1 = obj->lo_stripes[sidx].ls_fid;
2102 mds1 = obj->lo_stripes[sidx].ls_mds;
2103 CDEBUG(D_INODE, "Parent obj "DFID"\n", PFID(&op_data->op_fid1));
2104 lmv_object_put(obj);
2106 rc = lmv_fld_lookup(lmv, &op_data->op_fid1, &mds1);
2111 obj = lmv_object_find(obd, &op_data->op_fid2);
2114 * Directory is already split, so we have to forward request to
2117 sidx = raw_name2idx(obj->lo_hashtype, obj->lo_objcount,
2118 (char *)new, newlen);
2120 mds2 = obj->lo_stripes[sidx].ls_mds;
2121 op_data->op_fid2 = obj->lo_stripes[sidx].ls_fid;
2122 CDEBUG(D_INODE, "Parent obj "DFID"\n", PFID(&op_data->op_fid2));
2123 lmv_object_put(obj);
2125 rc = lmv_fld_lookup(lmv, &op_data->op_fid2, &mds2);
2130 op_data->op_fsuid = cfs_curproc_fsuid();
2131 op_data->op_fsgid = cfs_curproc_fsgid();
2132 op_data->op_cap = cfs_curproc_cap_pack();
2134 src_tgt = lmv_get_target(lmv, mds1);
2137 * LOOKUP lock on src child (fid3) should also be cancelled for
2138 * src_tgt in mdc_rename.
2140 op_data->op_flags |= MF_MDC_CANCEL_FID1 | MF_MDC_CANCEL_FID3;
2143 * Cancel UPDATE locks on tgt parent (fid2), tgt_tgt is its
2146 rc = lmv_early_cancel(exp, op_data, src_tgt->ltd_idx,
2147 LCK_EX, MDS_INODELOCK_UPDATE,
2148 MF_MDC_CANCEL_FID2);
2151 * Cancel LOOKUP locks on tgt child (fid4) for parent tgt_tgt.
2154 rc = lmv_early_cancel(exp, op_data, src_tgt->ltd_idx,
2155 LCK_EX, MDS_INODELOCK_LOOKUP,
2156 MF_MDC_CANCEL_FID4);
2160 * Cancel all the locks on tgt child (fid4).
2163 rc = lmv_early_cancel(exp, op_data, src_tgt->ltd_idx,
2164 LCK_EX, MDS_INODELOCK_FULL,
2165 MF_MDC_CANCEL_FID4);
2168 rc = md_rename(src_tgt->ltd_exp, op_data, old, oldlen,
2169 new, newlen, request);
2171 if (rc == -ERESTART) {
2172 LASSERT(*request != NULL);
2173 DEBUG_REQ(D_WARNING|D_RPCTRACE, *request,
2174 "Got -ERESTART during rename!\n");
2175 ptlrpc_req_finished(*request);
2179 * Directory got split. Time to update local object and repeat
2180 * the request with proper MDS.
2182 rc = lmv_handle_split(exp, &op_data->op_fid1);
2189 static int lmv_setattr(struct obd_export *exp, struct md_op_data *op_data,
2190 void *ea, int ealen, void *ea2, int ea2len,
2191 struct ptlrpc_request **request,
2192 struct md_open_data **mod)
2194 struct obd_device *obd = exp->exp_obd;
2195 struct lmv_obd *lmv = &obd->u.lmv;
2196 struct ptlrpc_request *req;
2197 struct lmv_tgt_desc *tgt;
2198 struct lmv_object *obj;
2203 rc = lmv_check_connect(obd);
2207 obj = lmv_object_find(obd, &op_data->op_fid1);
2209 CDEBUG(D_INODE, "SETATTR for "DFID", valid 0x%x%s\n",
2210 PFID(&op_data->op_fid1), op_data->op_attr.ia_valid,
2211 obj ? ", split" : "");
2213 op_data->op_flags |= MF_MDC_CANCEL_FID1;
2215 for (i = 0; i < obj->lo_objcount; i++) {
2216 op_data->op_fid1 = obj->lo_stripes[i].ls_fid;
2218 tgt = lmv_get_target(lmv, obj->lo_stripes[i].ls_mds);
2224 rc = md_setattr(tgt->ltd_exp, op_data, ea, ealen,
2225 ea2, ea2len, &req, mod);
2227 if (lu_fid_eq(&obj->lo_fid, &obj->lo_stripes[i].ls_fid)) {
2229 * This is master object and this request should
2230 * be returned back to llite.
2234 ptlrpc_req_finished(req);
2240 lmv_object_put(obj);
2242 tgt = lmv_find_target(lmv, &op_data->op_fid1);
2244 RETURN(PTR_ERR(tgt));
2246 rc = md_setattr(tgt->ltd_exp, op_data, ea, ealen, ea2,
2247 ea2len, request, mod);
2252 static int lmv_sync(struct obd_export *exp, const struct lu_fid *fid,
2253 struct obd_capa *oc, struct ptlrpc_request **request)
2255 struct obd_device *obd = exp->exp_obd;
2256 struct lmv_obd *lmv = &obd->u.lmv;
2257 struct lmv_tgt_desc *tgt;
2261 rc = lmv_check_connect(obd);
2265 tgt = lmv_find_target(lmv, fid);
2267 RETURN(PTR_ERR(tgt));
2269 rc = md_sync(tgt->ltd_exp, fid, oc, request);
2274 * Main purpose of LMV blocking ast is to remove split directory LMV
2275 * presentation object (struct lmv_object) attached to the lock being revoked.
2277 int lmv_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
2278 void *data, int flag)
2280 struct lustre_handle lockh;
2281 struct lmv_object *obj;
2286 case LDLM_CB_BLOCKING:
2287 ldlm_lock2handle(lock, &lockh);
2288 rc = ldlm_cli_cancel(&lockh);
2290 CDEBUG(D_INODE, "ldlm_cli_cancel: %d\n", rc);
2294 case LDLM_CB_CANCELING:
2296 * Time to drop cached attrs for split directory object
2298 obj = lock->l_ast_data;
2300 CDEBUG(D_INODE, "Cancel %s on "LPU64"/"LPU64
2301 ", master "DFID"\n",
2302 lock->l_resource->lr_name.name[3] == 1 ?
2303 "LOOKUP" : "UPDATE",
2304 lock->l_resource->lr_name.name[0],
2305 lock->l_resource->lr_name.name[1],
2306 PFID(&obj->lo_fid));
2307 lmv_object_put(obj);
2316 static void lmv_hash_adjust(__u64 *hash, __u64 hash_adj)
2320 val = le64_to_cpu(*hash);
2322 val += MAX_HASH_SIZE;
2323 if (val != MDS_DIR_END_OFF)
2324 *hash = cpu_to_le64(val - hash_adj);
2327 static __u32 lmv_node_rank(struct obd_export *exp, const struct lu_fid *fid)
2330 struct obd_import *imp;
2333 * XXX: to get nid we assume that underlying obd device is mdc.
2335 imp = class_exp2cliimp(exp);
2336 id = imp->imp_connection->c_self + fid_flatten(fid);
2338 CDEBUG(D_INODE, "Readpage node rank: "LPX64" "DFID" "LPX64" "LPX64"\n",
2339 imp->imp_connection->c_self, PFID(fid), id, id ^ (id >> 32));
2341 return id ^ (id >> 32);
2344 static int lmv_readpage(struct obd_export *exp, struct md_op_data *op_data,
2345 struct page **pages, struct ptlrpc_request **request)
2347 struct obd_device *obd = exp->exp_obd;
2348 struct lmv_obd *lmv = &obd->u.lmv;
2349 struct lmv_object *obj;
2350 struct lu_fid rid = op_data->op_fid1;
2351 __u64 offset = op_data->op_offset;
2361 /* number of pages read, in CFS_PAGE_SIZE */
2363 /* number of pages transferred in LU_PAGE_SIZE */
2365 struct lmv_stripe *los;
2366 struct lmv_tgt_desc *tgt;
2367 struct lu_dirpage *dp;
2368 struct lu_dirent *ent;
2371 rc = lmv_check_connect(obd);
2375 CDEBUG(D_INODE, "READPAGE at "LPX64" from "DFID"\n", offset, PFID(&rid));
2378 * This case handle directory lookup in clustered metadata case (i.e.
2379 * split directory is located on multiple md servers.)
2380 * each server keeps directory entries for certain range of hashes.
2381 * E.g. we have N server and suppose hash range is 0 to MAX_HASH.
2382 * first server will keep records with hashes [ 0 ... MAX_HASH / N - 1],
2383 * second one with hashes [MAX_HASH / N ... 2 * MAX_HASH / N] and
2385 * readdir can simply start reading entries from 0 - N server in
2386 * order but that will not scale well as all client will request dir in
2387 * to server in same order.
2388 * Following algorithm does optimization:
2389 * Instead of doing readdir in 1, 2, ...., N order, client with a
2390 * rank R does readdir in R, R + 1, ..., N, 1, ... R - 1 order.
2391 * (every client has rank R)
2392 * But ll_readdir() expect offset range [0 to MAX_HASH/N) but
2393 * since client ask dir from MDS{R} client has pages with offsets
2394 * [R*MAX_HASH/N ... (R + 1)*MAX_HASH/N] there for we do hash_adj
2395 * on hash values that we get.
2397 obj = lmv_object_find_lock(obd, &rid);
2399 nr = obj->lo_objcount;
2401 seg_size = MAX_HASH_SIZE;
2402 do_div(seg_size, nr);
2403 los = obj->lo_stripes;
2404 tgt = lmv_get_target(lmv, los[0].ls_mds);
2405 rank = lmv_node_rank(tgt->ltd_exp, &rid) % nr;
2407 do_div(tgt_tmp, seg_size);
2408 tgt0_idx = do_div(tgt_tmp, nr);
2409 tgt_idx = (tgt0_idx + rank) % nr;
2411 if (tgt_idx < tgt0_idx)
2415 * Last segment has unusual length due to division
2418 hash_adj = MAX_HASH_SIZE - seg_size * nr;
2422 hash_adj += rank * seg_size;
2424 CDEBUG(D_INODE, "Readpage hash adjustment: %x "LPX64" "
2425 LPX64"/%x -> "LPX64"/%x\n", rank, hash_adj,
2426 offset, tgt0_idx, offset + hash_adj, tgt_idx);
2428 offset = (offset + hash_adj) & MAX_HASH_SIZE;
2429 rid = obj->lo_stripes[tgt_idx].ls_fid;
2430 tgt = lmv_get_target(lmv, los[tgt_idx].ls_mds);
2432 CDEBUG(D_INODE, "Forward to "DFID" with offset %lu i %d\n",
2433 PFID(&rid), (unsigned long)offset, tgt_idx);
2435 tgt = lmv_find_target(lmv, &rid);
2438 GOTO(cleanup, rc = PTR_ERR(tgt));
2440 op_data->op_fid1 = rid;
2441 rc = md_readpage(tgt->ltd_exp, op_data, pages, request);
2445 nrdpgs = ((*request)->rq_bulk->bd_nob_transferred + CFS_PAGE_SIZE - 1)
2447 nlupgs = (*request)->rq_bulk->bd_nob_transferred >> LU_PAGE_SHIFT;
2448 LASSERT(!((*request)->rq_bulk->bd_nob_transferred & ~LU_PAGE_MASK));
2449 LASSERT(nrdpgs > 0 && nrdpgs <= op_data->op_npages);
2451 CDEBUG(D_INODE, "read %d(%d)/%d pages\n", nrdpgs, nlupgs,
2452 op_data->op_npages);
2454 for (i = 0; i < nrdpgs; i++) {
2455 #if CFS_PAGE_SIZE > LU_PAGE_SIZE
2456 struct lu_dirpage *first;
2460 struct lu_dirent *tmp = NULL;
2462 dp = cfs_kmap(pages[i]);
2464 lmv_hash_adjust(&dp->ldp_hash_start, hash_adj);
2465 lmv_hash_adjust(&dp->ldp_hash_end, hash_adj);
2466 LASSERT(le64_to_cpu(dp->ldp_hash_start) <=
2467 op_data->op_offset);
2469 if ((tgt0_idx != nr - 1) &&
2470 (le64_to_cpu(dp->ldp_hash_end) == MDS_DIR_END_OFF))
2472 dp->ldp_hash_end = cpu_to_le32(seg_size *
2475 ""DFID" reset end "LPX64" tgt %d\n",
2477 (__u64)le64_to_cpu(dp->ldp_hash_end),
2482 ent = lu_dirent_start(dp);
2483 #if CFS_PAGE_SIZE > LU_PAGE_SIZE
2485 hash_end = dp->ldp_hash_end;
2489 for (tmp = ent; ent != NULL;
2490 tmp = ent, ent = lu_dirent_next(ent)) {
2492 lmv_hash_adjust(&ent->lde_hash, hash_adj);
2495 #if CFS_PAGE_SIZE > LU_PAGE_SIZE
2496 dp = (struct lu_dirpage *)((char *)dp + LU_PAGE_SIZE);
2497 if (((unsigned long)dp & ~CFS_PAGE_MASK) && nlupgs > 0) {
2498 ent = lu_dirent_start(dp);
2501 lmv_hash_adjust(&dp->ldp_hash_end, hash_adj);
2502 if ((tgt0_idx != nr - 1) &&
2503 (le64_to_cpu(dp->ldp_hash_end) ==
2505 hash_end = cpu_to_le32(seg_size *
2508 ""DFID" reset end "LPX64" tgt %d\n",
2510 (__u64)le64_to_cpu(hash_end),
2514 hash_end = dp->ldp_hash_end;
2515 flags = dp->ldp_flags;
2518 /* enlarge the end entry lde_reclen from 0 to
2519 * first entry of next lu_dirpage, in this way
2520 * several lu_dirpages can be stored into one
2521 * client page on client. */
2522 tmp = ((void *)tmp) +
2523 le16_to_cpu(tmp->lde_reclen);
2525 cpu_to_le16((char *)(dp->ldp_entries) -
2530 first->ldp_hash_end = hash_end;
2531 first->ldp_flags &= ~cpu_to_le32(LDF_COLLIDE);
2532 first->ldp_flags |= flags & cpu_to_le32(LDF_COLLIDE);
2534 SET_BUT_UNUSED(tmp);
2536 cfs_kunmap(pages[i]);
2541 lmv_object_put_unlock(obj);
2545 static int lmv_unlink(struct obd_export *exp, struct md_op_data *op_data,
2546 struct ptlrpc_request **request)
2548 struct obd_device *obd = exp->exp_obd;
2549 struct lmv_obd *lmv = &obd->u.lmv;
2550 struct lmv_tgt_desc *tgt = NULL;
2551 struct lmv_object *obj;
2557 rc = lmv_check_connect(obd);
2564 LASSERT(op_data->op_namelen != 0);
2566 obj = lmv_object_find(obd, &op_data->op_fid1);
2568 sidx = raw_name2idx(obj->lo_hashtype,
2571 op_data->op_namelen);
2572 op_data->op_bias &= ~MDS_CHECK_SPLIT;
2573 op_data->op_fid1 = obj->lo_stripes[sidx].ls_fid;
2574 tgt = lmv_get_target(lmv,
2575 obj->lo_stripes[sidx].ls_mds);
2576 lmv_object_put(obj);
2577 CDEBUG(D_INODE, "UNLINK '%*s' in "DFID" -> %u\n",
2578 op_data->op_namelen, op_data->op_name,
2579 PFID(&op_data->op_fid1), sidx);
2583 tgt = lmv_find_target(lmv, &op_data->op_fid1);
2585 RETURN(PTR_ERR(tgt));
2586 op_data->op_bias |= MDS_CHECK_SPLIT;
2589 op_data->op_fsuid = cfs_curproc_fsuid();
2590 op_data->op_fsgid = cfs_curproc_fsgid();
2591 op_data->op_cap = cfs_curproc_cap_pack();
2594 * If child's fid is given, cancel unused locks for it if it is from
2595 * another export than parent.
2597 * LOOKUP lock for child (fid3) should also be cancelled on parent
2598 * tgt_tgt in mdc_unlink().
2600 op_data->op_flags |= MF_MDC_CANCEL_FID1 | MF_MDC_CANCEL_FID3;
2603 * Cancel FULL locks on child (fid3).
2605 rc = lmv_early_cancel(exp, op_data, tgt->ltd_idx, LCK_EX,
2606 MDS_INODELOCK_FULL, MF_MDC_CANCEL_FID3);
2609 rc = md_unlink(tgt->ltd_exp, op_data, request);
2611 if (rc == -ERESTART) {
2612 LASSERT(*request != NULL);
2613 DEBUG_REQ(D_WARNING|D_RPCTRACE, *request,
2614 "Got -ERESTART during unlink!\n");
2615 ptlrpc_req_finished(*request);
2619 * Directory got split. Time to update local object and repeat
2620 * the request with proper MDS.
2622 rc = lmv_handle_split(exp, &op_data->op_fid1);
2629 static int lmv_precleanup(struct obd_device *obd, enum obd_cleanup_stage stage)
2631 struct lmv_obd *lmv = &obd->u.lmv;
2635 case OBD_CLEANUP_EARLY:
2636 /* XXX: here should be calling obd_precleanup() down to
2639 case OBD_CLEANUP_EXPORTS:
2640 fld_client_proc_fini(&lmv->lmv_fld);
2641 lprocfs_obd_cleanup(obd);
2642 rc = obd_llog_finish(obd, 0);
2644 CERROR("failed to cleanup llogging subsystems\n");
2652 static int lmv_get_info(struct obd_export *exp, __u32 keylen,
2653 void *key, __u32 *vallen, void *val,
2654 struct lov_stripe_md *lsm)
2656 struct obd_device *obd;
2657 struct lmv_obd *lmv;
2661 obd = class_exp2obd(exp);
2663 CDEBUG(D_IOCTL, "Invalid client cookie "LPX64"\n",
2664 exp->exp_handle.h_cookie);
2669 if (keylen >= strlen("remote_flag") && !strcmp(key, "remote_flag")) {
2670 struct lmv_tgt_desc *tgts;
2673 rc = lmv_check_connect(obd);
2677 LASSERT(*vallen == sizeof(__u32));
2678 for (i = 0, tgts = lmv->tgts; i < lmv->desc.ld_tgt_count;
2682 * All tgts should be connected when this gets called.
2684 if (!tgts || !tgts->ltd_exp) {
2685 CERROR("target not setup?\n");
2689 if (!obd_get_info(tgts->ltd_exp, keylen, key,
2694 } else if (KEY_IS(KEY_MAX_EASIZE) || KEY_IS(KEY_CONN_DATA)) {
2695 rc = lmv_check_connect(obd);
2700 * Forwarding this request to first MDS, it should know LOV
2703 rc = obd_get_info(lmv->tgts[0].ltd_exp, keylen, key,
2705 if (!rc && KEY_IS(KEY_CONN_DATA)) {
2706 exp->exp_connect_flags =
2707 ((struct obd_connect_data *)val)->ocd_connect_flags;
2710 } else if (KEY_IS(KEY_TGT_COUNT)) {
2711 *((int *)val) = lmv->desc.ld_tgt_count;
2715 CDEBUG(D_IOCTL, "Invalid key\n");
2719 int lmv_set_info_async(struct obd_export *exp, obd_count keylen,
2720 void *key, obd_count vallen, void *val,
2721 struct ptlrpc_request_set *set)
2723 struct lmv_tgt_desc *tgt;
2724 struct obd_device *obd;
2725 struct lmv_obd *lmv;
2729 obd = class_exp2obd(exp);
2731 CDEBUG(D_IOCTL, "Invalid client cookie "LPX64"\n",
2732 exp->exp_handle.h_cookie);
2737 if (KEY_IS(KEY_READ_ONLY) || KEY_IS(KEY_FLUSH_CTX)) {
2740 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
2741 tgt = &lmv->tgts[i];
2746 err = obd_set_info_async(tgt->ltd_exp,
2747 keylen, key, vallen, val, set);
2758 int lmv_packmd(struct obd_export *exp, struct lov_mds_md **lmmp,
2759 struct lov_stripe_md *lsm)
2761 struct obd_device *obd = class_exp2obd(exp);
2762 struct lmv_obd *lmv = &obd->u.lmv;
2763 struct lmv_stripe_md *meap;
2764 struct lmv_stripe_md *lsmp;
2769 mea_size = lmv_get_easize(lmv);
2773 if (*lmmp && !lsm) {
2774 OBD_FREE_LARGE(*lmmp, mea_size);
2779 if (*lmmp == NULL) {
2780 OBD_ALLOC_LARGE(*lmmp, mea_size);
2788 lsmp = (struct lmv_stripe_md *)lsm;
2789 meap = (struct lmv_stripe_md *)*lmmp;
2791 if (lsmp->mea_magic != MEA_MAGIC_LAST_CHAR &&
2792 lsmp->mea_magic != MEA_MAGIC_ALL_CHARS)
2795 meap->mea_magic = cpu_to_le32(lsmp->mea_magic);
2796 meap->mea_count = cpu_to_le32(lsmp->mea_count);
2797 meap->mea_master = cpu_to_le32(lsmp->mea_master);
2799 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
2800 meap->mea_ids[i] = meap->mea_ids[i];
2801 fid_cpu_to_le(&meap->mea_ids[i], &meap->mea_ids[i]);
2807 int lmv_unpackmd(struct obd_export *exp, struct lov_stripe_md **lsmp,
2808 struct lov_mds_md *lmm, int lmm_size)
2810 struct obd_device *obd = class_exp2obd(exp);
2811 struct lmv_stripe_md **tmea = (struct lmv_stripe_md **)lsmp;
2812 struct lmv_stripe_md *mea = (struct lmv_stripe_md *)lmm;
2813 struct lmv_obd *lmv = &obd->u.lmv;
2819 mea_size = lmv_get_easize(lmv);
2823 if (*lsmp != NULL && lmm == NULL) {
2824 OBD_FREE_LARGE(*tmea, mea_size);
2829 LASSERT(mea_size == lmm_size);
2831 OBD_ALLOC_LARGE(*tmea, mea_size);
2838 if (mea->mea_magic == MEA_MAGIC_LAST_CHAR ||
2839 mea->mea_magic == MEA_MAGIC_ALL_CHARS ||
2840 mea->mea_magic == MEA_MAGIC_HASH_SEGMENT)
2842 magic = le32_to_cpu(mea->mea_magic);
2845 * Old mea is not handled here.
2847 CERROR("Old not supportable EA is found\n");
2851 (*tmea)->mea_magic = magic;
2852 (*tmea)->mea_count = le32_to_cpu(mea->mea_count);
2853 (*tmea)->mea_master = le32_to_cpu(mea->mea_master);
2855 for (i = 0; i < (*tmea)->mea_count; i++) {
2856 (*tmea)->mea_ids[i] = mea->mea_ids[i];
2857 fid_le_to_cpu(&(*tmea)->mea_ids[i], &(*tmea)->mea_ids[i]);
2862 static int lmv_cancel_unused(struct obd_export *exp, const struct lu_fid *fid,
2863 ldlm_policy_data_t *policy, ldlm_mode_t mode,
2864 ldlm_cancel_flags_t flags, void *opaque)
2866 struct obd_device *obd = exp->exp_obd;
2867 struct lmv_obd *lmv = &obd->u.lmv;
2873 LASSERT(fid != NULL);
2875 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
2876 if (!lmv->tgts[i].ltd_exp || !lmv->tgts[i].ltd_active)
2879 err = md_cancel_unused(lmv->tgts[i].ltd_exp, fid,
2880 policy, mode, flags, opaque);
2887 int lmv_set_lock_data(struct obd_export *exp, __u64 *lockh, void *data,
2890 struct obd_device *obd = exp->exp_obd;
2891 struct lmv_obd *lmv = &obd->u.lmv;
2895 rc = md_set_lock_data(lmv->tgts[0].ltd_exp, lockh, data, bits);
2899 ldlm_mode_t lmv_lock_match(struct obd_export *exp, int flags,
2900 const struct lu_fid *fid, ldlm_type_t type,
2901 ldlm_policy_data_t *policy, ldlm_mode_t mode,
2902 struct lustre_handle *lockh)
2904 struct obd_device *obd = exp->exp_obd;
2905 struct lmv_obd *lmv = &obd->u.lmv;
2910 CDEBUG(D_INODE, "Lock match for "DFID"\n", PFID(fid));
2913 * With CMD every object can have two locks in different namespaces:
2914 * lookup lock in space of mds storing direntry and update/open lock in
2915 * space of mds storing inode. Thus we check all targets, not only that
2916 * one fid was created in.
2918 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
2919 rc = md_lock_match(lmv->tgts[i].ltd_exp, flags, fid,
2920 type, policy, mode, lockh);
2928 int lmv_get_lustre_md(struct obd_export *exp, struct ptlrpc_request *req,
2929 struct obd_export *dt_exp, struct obd_export *md_exp,
2930 struct lustre_md *md)
2932 struct obd_device *obd = exp->exp_obd;
2933 struct lmv_obd *lmv = &obd->u.lmv;
2936 rc = md_get_lustre_md(lmv->tgts[0].ltd_exp, req, dt_exp, md_exp, md);
2940 int lmv_free_lustre_md(struct obd_export *exp, struct lustre_md *md)
2942 struct obd_device *obd = exp->exp_obd;
2943 struct lmv_obd *lmv = &obd->u.lmv;
2947 obd_free_memmd(exp, (void *)&md->mea);
2948 RETURN(md_free_lustre_md(lmv->tgts[0].ltd_exp, md));
2951 int lmv_set_open_replay_data(struct obd_export *exp,
2952 struct obd_client_handle *och,
2953 struct ptlrpc_request *open_req)
2955 struct obd_device *obd = exp->exp_obd;
2956 struct lmv_obd *lmv = &obd->u.lmv;
2957 struct lmv_tgt_desc *tgt;
2960 tgt = lmv_find_target(lmv, &och->och_fid);
2962 RETURN(PTR_ERR(tgt));
2964 RETURN(md_set_open_replay_data(tgt->ltd_exp, och, open_req));
2967 int lmv_clear_open_replay_data(struct obd_export *exp,
2968 struct obd_client_handle *och)
2970 struct obd_device *obd = exp->exp_obd;
2971 struct lmv_obd *lmv = &obd->u.lmv;
2972 struct lmv_tgt_desc *tgt;
2975 tgt = lmv_find_target(lmv, &och->och_fid);
2977 RETURN(PTR_ERR(tgt));
2979 RETURN(md_clear_open_replay_data(tgt->ltd_exp, och));
2982 static int lmv_get_remote_perm(struct obd_export *exp,
2983 const struct lu_fid *fid,
2984 struct obd_capa *oc, __u32 suppgid,
2985 struct ptlrpc_request **request)
2987 struct obd_device *obd = exp->exp_obd;
2988 struct lmv_obd *lmv = &obd->u.lmv;
2989 struct lmv_tgt_desc *tgt;
2993 rc = lmv_check_connect(obd);
2997 tgt = lmv_find_target(lmv, fid);
2999 RETURN(PTR_ERR(tgt));
3001 rc = md_get_remote_perm(tgt->ltd_exp, fid, oc, suppgid, request);
3005 static int lmv_renew_capa(struct obd_export *exp, struct obd_capa *oc,
3008 struct obd_device *obd = exp->exp_obd;
3009 struct lmv_obd *lmv = &obd->u.lmv;
3010 struct lmv_tgt_desc *tgt;
3014 rc = lmv_check_connect(obd);
3018 tgt = lmv_find_target(lmv, &oc->c_capa.lc_fid);
3020 RETURN(PTR_ERR(tgt));
3022 rc = md_renew_capa(tgt->ltd_exp, oc, cb);
3026 int lmv_unpack_capa(struct obd_export *exp, struct ptlrpc_request *req,
3027 const struct req_msg_field *field, struct obd_capa **oc)
3029 struct obd_device *obd = exp->exp_obd;
3030 struct lmv_obd *lmv = &obd->u.lmv;
3034 rc = md_unpack_capa(lmv->tgts[0].ltd_exp, req, field, oc);
3038 int lmv_intent_getattr_async(struct obd_export *exp,
3039 struct md_enqueue_info *minfo,
3040 struct ldlm_enqueue_info *einfo)
3042 struct md_op_data *op_data = &minfo->mi_data;
3043 struct obd_device *obd = exp->exp_obd;
3044 struct lmv_obd *lmv = &obd->u.lmv;
3045 struct lmv_object *obj;
3046 struct lmv_tgt_desc *tgt = NULL;
3051 rc = lmv_check_connect(obd);
3055 if (op_data->op_namelen) {
3056 obj = lmv_object_find(obd, &op_data->op_fid1);
3058 sidx = raw_name2idx(obj->lo_hashtype, obj->lo_objcount,
3059 (char *)op_data->op_name,
3060 op_data->op_namelen);
3061 op_data->op_fid1 = obj->lo_stripes[sidx].ls_fid;
3062 tgt = lmv_get_target(lmv, obj->lo_stripes[sidx].ls_mds);
3063 lmv_object_put(obj);
3068 tgt = lmv_find_target(lmv, &op_data->op_fid1);
3071 RETURN(PTR_ERR(tgt));
3073 rc = md_intent_getattr_async(tgt->ltd_exp, minfo, einfo);
3077 int lmv_revalidate_lock(struct obd_export *exp, struct lookup_intent *it,
3078 struct lu_fid *fid, __u64 *bits)
3080 struct obd_device *obd = exp->exp_obd;
3081 struct lmv_obd *lmv = &obd->u.lmv;
3082 struct lmv_tgt_desc *tgt;
3086 rc = lmv_check_connect(obd);
3090 tgt = lmv_find_target(lmv, fid);
3092 RETURN(PTR_ERR(tgt));
3094 rc = md_revalidate_lock(tgt->ltd_exp, it, fid, bits);
3099 * For lmv, only need to send request to master MDT, and the master MDT will
3100 * process with other slave MDTs. The only exception is Q_GETOQUOTA for which
3101 * we directly fetch data from the slave MDTs.
3103 int lmv_quotactl(struct obd_device *unused, struct obd_export *exp,
3104 struct obd_quotactl *oqctl)
3106 struct obd_device *obd = class_exp2obd(exp);
3107 struct lmv_obd *lmv = &obd->u.lmv;
3108 struct lmv_tgt_desc *tgt = &lmv->tgts[0];
3110 __u64 curspace, curinodes;
3113 if (!lmv->desc.ld_tgt_count || !tgt->ltd_active) {
3114 CERROR("master lmv inactive\n");
3118 if (oqctl->qc_cmd != Q_GETOQUOTA) {
3119 rc = obd_quotactl(tgt->ltd_exp, oqctl);
3123 curspace = curinodes = 0;
3124 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
3126 tgt = &lmv->tgts[i];
3128 if (tgt->ltd_exp == NULL)
3130 if (!tgt->ltd_active) {
3131 CDEBUG(D_HA, "mdt %d is inactive.\n", i);
3135 err = obd_quotactl(tgt->ltd_exp, oqctl);
3137 CERROR("getquota on mdt %d failed. %d\n", i, err);
3141 curspace += oqctl->qc_dqblk.dqb_curspace;
3142 curinodes += oqctl->qc_dqblk.dqb_curinodes;
3145 oqctl->qc_dqblk.dqb_curspace = curspace;
3146 oqctl->qc_dqblk.dqb_curinodes = curinodes;
3151 int lmv_quotacheck(struct obd_device *unused, struct obd_export *exp,
3152 struct obd_quotactl *oqctl)
3154 struct obd_device *obd = class_exp2obd(exp);
3155 struct lmv_obd *lmv = &obd->u.lmv;
3156 struct lmv_tgt_desc *tgt;
3160 for (i = 0, tgt = lmv->tgts; i < lmv->desc.ld_tgt_count; i++, tgt++) {
3163 if (!tgt->ltd_active) {
3164 CERROR("lmv idx %d inactive\n", i);
3168 err = obd_quotacheck(tgt->ltd_exp, oqctl);
3176 struct obd_ops lmv_obd_ops = {
3177 .o_owner = THIS_MODULE,
3178 .o_setup = lmv_setup,
3179 .o_cleanup = lmv_cleanup,
3180 .o_precleanup = lmv_precleanup,
3181 .o_process_config = lmv_process_config,
3182 .o_connect = lmv_connect,
3183 .o_disconnect = lmv_disconnect,
3184 .o_statfs = lmv_statfs,
3185 .o_get_info = lmv_get_info,
3186 .o_set_info_async = lmv_set_info_async,
3187 .o_packmd = lmv_packmd,
3188 .o_unpackmd = lmv_unpackmd,
3189 .o_notify = lmv_notify,
3190 .o_get_uuid = lmv_get_uuid,
3191 .o_iocontrol = lmv_iocontrol,
3192 .o_fid_delete = lmv_fid_delete,
3193 .o_quotacheck = lmv_quotacheck,
3194 .o_quotactl = lmv_quotactl
3197 struct md_ops lmv_md_ops = {
3198 .m_getstatus = lmv_getstatus,
3199 .m_change_cbdata = lmv_change_cbdata,
3200 .m_find_cbdata = lmv_find_cbdata,
3201 .m_close = lmv_close,
3202 .m_create = lmv_create,
3203 .m_done_writing = lmv_done_writing,
3204 .m_enqueue = lmv_enqueue,
3205 .m_getattr = lmv_getattr,
3206 .m_getxattr = lmv_getxattr,
3207 .m_getattr_name = lmv_getattr_name,
3208 .m_intent_lock = lmv_intent_lock,
3210 .m_rename = lmv_rename,
3211 .m_setattr = lmv_setattr,
3212 .m_setxattr = lmv_setxattr,
3214 .m_readpage = lmv_readpage,
3215 .m_unlink = lmv_unlink,
3216 .m_init_ea_size = lmv_init_ea_size,
3217 .m_cancel_unused = lmv_cancel_unused,
3218 .m_set_lock_data = lmv_set_lock_data,
3219 .m_lock_match = lmv_lock_match,
3220 .m_get_lustre_md = lmv_get_lustre_md,
3221 .m_free_lustre_md = lmv_free_lustre_md,
3222 .m_set_open_replay_data = lmv_set_open_replay_data,
3223 .m_clear_open_replay_data = lmv_clear_open_replay_data,
3224 .m_renew_capa = lmv_renew_capa,
3225 .m_unpack_capa = lmv_unpack_capa,
3226 .m_get_remote_perm = lmv_get_remote_perm,
3227 .m_intent_getattr_async = lmv_intent_getattr_async,
3228 .m_revalidate_lock = lmv_revalidate_lock
3231 int __init lmv_init(void)
3233 struct lprocfs_static_vars lvars;
3236 lmv_object_cache = cfs_mem_cache_create("lmv_objects",
3237 sizeof(struct lmv_object),
3239 if (!lmv_object_cache) {
3240 CERROR("Error allocating lmv objects cache\n");
3244 lprocfs_lmv_init_vars(&lvars);
3246 rc = class_register_type(&lmv_obd_ops, &lmv_md_ops,
3247 lvars.module_vars, LUSTRE_LMV_NAME, NULL);
3249 cfs_mem_cache_destroy(lmv_object_cache);
3255 static void lmv_exit(void)
3257 class_unregister_type(LUSTRE_LMV_NAME);
3259 LASSERTF(cfs_atomic_read(&lmv_object_count) == 0,
3260 "Can't free lmv objects cache, %d object(s) busy\n",
3261 cfs_atomic_read(&lmv_object_count));
3262 cfs_mem_cache_destroy(lmv_object_cache);
3265 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
3266 MODULE_DESCRIPTION("Lustre Logical Metadata Volume OBD driver");
3267 MODULE_LICENSE("GPL");
3269 module_init(lmv_init);
3270 module_exit(lmv_exit);