4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2012, Whamcloud, Inc.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
37 #define DEBUG_SUBSYSTEM S_LMV
39 #include <linux/slab.h>
40 #include <linux/module.h>
41 #include <linux/init.h>
42 #include <linux/slab.h>
43 #include <linux/pagemap.h>
45 #include <asm/div64.h>
46 #include <linux/seq_file.h>
47 #include <linux/namei.h>
49 #include <liblustre.h>
52 #include <obd_support.h>
53 #include <lustre_lib.h>
54 #include <lustre_net.h>
55 #include <obd_class.h>
56 #include <lprocfs_status.h>
57 #include <lustre_lite.h>
58 #include <lustre_fid.h>
59 #include "lmv_internal.h"
62 cfs_mem_cache_t *lmv_object_cache;
63 cfs_atomic_t lmv_object_count = CFS_ATOMIC_INIT(0);
65 static void lmv_activate_target(struct lmv_obd *lmv,
66 struct lmv_tgt_desc *tgt,
69 if (tgt->ltd_active == activate)
72 tgt->ltd_active = activate;
73 lmv->desc.ld_active_tgt_count += (activate ? 1 : -1);
79 * -EINVAL : UUID can't be found in the LMV's target list
80 * -ENOTCONN: The UUID is found, but the target connection is bad (!)
81 * -EBADF : The UUID is found, but the OBD of the wrong type (!)
83 static int lmv_set_mdc_active(struct lmv_obd *lmv, struct obd_uuid *uuid,
86 struct lmv_tgt_desc *tgt;
87 struct obd_device *obd;
92 CDEBUG(D_INFO, "Searching in lmv %p for uuid %s (activate=%d)\n",
93 lmv, uuid->uuid, activate);
95 cfs_spin_lock(&lmv->lmv_lock);
96 for (i = 0, tgt = lmv->tgts; i < lmv->desc.ld_tgt_count; i++, tgt++) {
97 if (tgt->ltd_exp == NULL)
100 CDEBUG(D_INFO, "Target idx %d is %s conn "LPX64"\n",
101 i, tgt->ltd_uuid.uuid, tgt->ltd_exp->exp_handle.h_cookie);
103 if (obd_uuid_equals(uuid, &tgt->ltd_uuid))
107 if (i == lmv->desc.ld_tgt_count)
108 GOTO(out_lmv_lock, rc = -EINVAL);
110 obd = class_exp2obd(tgt->ltd_exp);
112 GOTO(out_lmv_lock, rc = -ENOTCONN);
114 CDEBUG(D_INFO, "Found OBD %s=%s device %d (%p) type %s at LMV idx %d\n",
115 obd->obd_name, obd->obd_uuid.uuid, obd->obd_minor, obd,
116 obd->obd_type->typ_name, i);
117 LASSERT(strcmp(obd->obd_type->typ_name, LUSTRE_MDC_NAME) == 0);
119 if (tgt->ltd_active == activate) {
120 CDEBUG(D_INFO, "OBD %p already %sactive!\n", obd,
121 activate ? "" : "in");
122 GOTO(out_lmv_lock, rc);
125 CDEBUG(D_INFO, "Marking OBD %p %sactive\n", obd,
126 activate ? "" : "in");
127 lmv_activate_target(lmv, tgt, activate);
131 cfs_spin_unlock(&lmv->lmv_lock);
135 static int lmv_set_mdc_data(struct lmv_obd *lmv, struct obd_uuid *uuid,
136 struct obd_connect_data *data)
138 struct lmv_tgt_desc *tgt;
142 LASSERT(data != NULL);
144 cfs_spin_lock(&lmv->lmv_lock);
145 for (i = 0, tgt = lmv->tgts; i < lmv->desc.ld_tgt_count; i++, tgt++) {
146 if (tgt->ltd_exp == NULL)
149 if (obd_uuid_equals(uuid, &tgt->ltd_uuid)) {
150 lmv->datas[tgt->ltd_idx] = *data;
154 cfs_spin_unlock(&lmv->lmv_lock);
158 struct obd_uuid *lmv_get_uuid(struct obd_export *exp) {
159 struct obd_device *obd = exp->exp_obd;
160 struct lmv_obd *lmv = &obd->u.lmv;
161 return obd_get_uuid(lmv->tgts[0].ltd_exp);
164 static int lmv_notify(struct obd_device *obd, struct obd_device *watched,
165 enum obd_notify_event ev, void *data)
167 struct obd_connect_data *conn_data;
168 struct lmv_obd *lmv = &obd->u.lmv;
169 struct obd_uuid *uuid;
173 if (strcmp(watched->obd_type->typ_name, LUSTRE_MDC_NAME)) {
174 CERROR("unexpected notification of %s %s!\n",
175 watched->obd_type->typ_name,
180 uuid = &watched->u.cli.cl_target_uuid;
181 if (ev == OBD_NOTIFY_ACTIVE || ev == OBD_NOTIFY_INACTIVE) {
183 * Set MDC as active before notifying the observer, so the
184 * observer can use the MDC normally.
186 rc = lmv_set_mdc_active(lmv, uuid,
187 ev == OBD_NOTIFY_ACTIVE);
189 CERROR("%sactivation of %s failed: %d\n",
190 ev == OBD_NOTIFY_ACTIVE ? "" : "de",
194 } else if (ev == OBD_NOTIFY_OCD) {
195 conn_data = &watched->u.cli.cl_import->imp_connect_data;
198 * Set connect data to desired target, update exp_connect_flags.
200 rc = lmv_set_mdc_data(lmv, uuid, conn_data);
202 CERROR("can't set connect data to target %s, rc %d\n",
208 * XXX: Make sure that ocd_connect_flags from all targets are
209 * the same. Otherwise one of MDTs runs wrong version or
210 * something like this. --umka
212 obd->obd_self_export->exp_connect_flags =
213 conn_data->ocd_connect_flags;
216 else if (ev == OBD_NOTIFY_DISCON) {
218 * For disconnect event, flush fld cache for failout MDS case.
220 fld_client_flush(&lmv->lmv_fld);
224 * Pass the notification up the chain.
226 if (obd->obd_observer)
227 rc = obd_notify(obd->obd_observer, watched, ev, data);
233 * This is fake connect function. Its purpose is to initialize lmv and say
234 * caller that everything is okay. Real connection will be performed later.
236 static int lmv_connect(const struct lu_env *env,
237 struct obd_export **exp, struct obd_device *obd,
238 struct obd_uuid *cluuid, struct obd_connect_data *data,
242 struct proc_dir_entry *lmv_proc_dir;
244 struct lmv_obd *lmv = &obd->u.lmv;
245 struct lustre_handle conn = { 0 };
250 * We don't want to actually do the underlying connections more than
251 * once, so keep track.
254 if (lmv->refcount > 1) {
259 rc = class_connect(&conn, obd, cluuid);
261 CERROR("class_connection() returned %d\n", rc);
265 *exp = class_conn2export(&conn);
266 class_export_get(*exp);
270 lmv->cluuid = *cluuid;
273 lmv->conn_data = *data;
276 lmv_proc_dir = lprocfs_register("target_obds", obd->obd_proc_entry,
278 if (IS_ERR(lmv_proc_dir)) {
279 CERROR("could not register /proc/fs/lustre/%s/%s/target_obds.",
280 obd->obd_type->typ_name, obd->obd_name);
286 * All real clients should perform actual connection right away, because
287 * it is possible, that LMV will not have opportunity to connect targets
288 * and MDC stuff will be called directly, for instance while reading
289 * ../mdc/../kbytesfree procfs file, etc.
291 if (data->ocd_connect_flags & OBD_CONNECT_REAL)
292 rc = lmv_check_connect(obd);
297 lprocfs_remove(&lmv_proc_dir);
304 static void lmv_set_timeouts(struct obd_device *obd)
306 struct lmv_tgt_desc *tgts;
311 if (lmv->server_timeout == 0)
314 if (lmv->connected == 0)
317 for (i = 0, tgts = lmv->tgts; i < lmv->desc.ld_tgt_count; i++, tgts++) {
318 if (tgts->ltd_exp == NULL)
321 obd_set_info_async(NULL, tgts->ltd_exp, sizeof(KEY_INTERMDS),
322 KEY_INTERMDS, 0, NULL, NULL);
326 static int lmv_init_ea_size(struct obd_export *exp, int easize,
327 int def_easize, int cookiesize)
329 struct obd_device *obd = exp->exp_obd;
330 struct lmv_obd *lmv = &obd->u.lmv;
336 if (lmv->max_easize < easize) {
337 lmv->max_easize = easize;
340 if (lmv->max_def_easize < def_easize) {
341 lmv->max_def_easize = def_easize;
344 if (lmv->max_cookiesize < cookiesize) {
345 lmv->max_cookiesize = cookiesize;
351 if (lmv->connected == 0)
354 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
355 if (lmv->tgts[i].ltd_exp == NULL) {
356 CWARN("%s: NULL export for %d\n", obd->obd_name, i);
360 rc = md_init_ea_size(lmv->tgts[i].ltd_exp, easize, def_easize,
363 CERROR("obd_init_ea_size() failed on MDT target %d, "
364 "error %d.\n", i, rc);
371 #define MAX_STRING_SIZE 128
373 int lmv_connect_mdc(struct obd_device *obd, struct lmv_tgt_desc *tgt)
376 struct proc_dir_entry *lmv_proc_dir;
378 struct lmv_obd *lmv = &obd->u.lmv;
379 struct obd_uuid *cluuid = &lmv->cluuid;
380 struct obd_connect_data *mdc_data = NULL;
381 struct obd_uuid lmv_mdc_uuid = { "LMV_MDC_UUID" };
382 struct obd_device *mdc_obd;
383 struct obd_export *mdc_exp;
384 struct lu_fld_target target;
388 mdc_obd = class_find_client_obd(&tgt->ltd_uuid, LUSTRE_MDC_NAME,
391 CERROR("target %s not attached\n", tgt->ltd_uuid.uuid);
395 CDEBUG(D_CONFIG, "connect to %s(%s) - %s, %s FOR %s\n",
396 mdc_obd->obd_name, mdc_obd->obd_uuid.uuid,
397 tgt->ltd_uuid.uuid, obd->obd_uuid.uuid,
400 if (!mdc_obd->obd_set_up) {
401 CERROR("target %s is not set up\n", tgt->ltd_uuid.uuid);
405 rc = obd_connect(NULL, &mdc_exp, mdc_obd, &lmv_mdc_uuid,
406 &lmv->conn_data, NULL);
408 CERROR("target %s connect error %d\n", tgt->ltd_uuid.uuid, rc);
413 * Init fid sequence client for this mdc and add new fld target.
415 rc = obd_fid_init(mdc_exp);
419 target.ft_srv = NULL;
420 target.ft_exp = mdc_exp;
421 target.ft_idx = tgt->ltd_idx;
423 fld_client_add_target(&lmv->lmv_fld, &target);
425 mdc_data = &class_exp2cliimp(mdc_exp)->imp_connect_data;
427 rc = obd_register_observer(mdc_obd, obd);
429 obd_disconnect(mdc_exp);
430 CERROR("target %s register_observer error %d\n",
431 tgt->ltd_uuid.uuid, rc);
435 if (obd->obd_observer) {
437 * Tell the observer about the new target.
439 rc = obd_notify(obd->obd_observer, mdc_exp->exp_obd,
440 OBD_NOTIFY_ACTIVE, (void *)(tgt - lmv->tgts));
442 obd_disconnect(mdc_exp);
448 tgt->ltd_exp = mdc_exp;
449 lmv->desc.ld_active_tgt_count++;
452 * Copy connect data, it may be used later.
454 lmv->datas[tgt->ltd_idx] = *mdc_data;
456 md_init_ea_size(tgt->ltd_exp, lmv->max_easize,
457 lmv->max_def_easize, lmv->max_cookiesize);
459 CDEBUG(D_CONFIG, "Connected to %s(%s) successfully (%d)\n",
460 mdc_obd->obd_name, mdc_obd->obd_uuid.uuid,
461 cfs_atomic_read(&obd->obd_refcount));
464 lmv_proc_dir = lprocfs_srch(obd->obd_proc_entry, "target_obds");
466 struct proc_dir_entry *mdc_symlink;
468 LASSERT(mdc_obd->obd_type != NULL);
469 LASSERT(mdc_obd->obd_type->typ_name != NULL);
470 mdc_symlink = lprocfs_add_symlink(mdc_obd->obd_name,
473 mdc_obd->obd_type->typ_name,
475 if (mdc_symlink == NULL) {
476 CERROR("Could not register LMV target "
477 "/proc/fs/lustre/%s/%s/target_obds/%s.",
478 obd->obd_type->typ_name, obd->obd_name,
480 lprocfs_remove(&lmv_proc_dir);
488 int lmv_add_target(struct obd_device *obd, struct obd_uuid *tgt_uuid)
490 struct lmv_obd *lmv = &obd->u.lmv;
491 struct lmv_tgt_desc *tgt;
495 CDEBUG(D_CONFIG, "Target uuid: %s.\n", tgt_uuid->uuid);
499 if (lmv->desc.ld_active_tgt_count >= LMV_MAX_TGT_COUNT) {
500 lmv_init_unlock(lmv);
501 CERROR("Can't add %s, LMV module compiled for %d MDCs. "
502 "That many MDCs already configured.\n",
503 tgt_uuid->uuid, LMV_MAX_TGT_COUNT);
506 if (lmv->desc.ld_tgt_count == 0) {
507 struct obd_device *mdc_obd;
509 mdc_obd = class_find_client_obd(tgt_uuid, LUSTRE_MDC_NAME,
512 lmv_init_unlock(lmv);
513 CERROR("Target %s not attached\n", tgt_uuid->uuid);
517 cfs_spin_lock(&lmv->lmv_lock);
518 tgt = lmv->tgts + lmv->desc.ld_tgt_count++;
519 tgt->ltd_uuid = *tgt_uuid;
520 cfs_spin_unlock(&lmv->lmv_lock);
522 if (lmv->connected) {
523 rc = lmv_connect_mdc(obd, tgt);
525 cfs_spin_lock(&lmv->lmv_lock);
526 lmv->desc.ld_tgt_count--;
527 memset(tgt, 0, sizeof(*tgt));
528 cfs_spin_unlock(&lmv->lmv_lock);
530 int easize = sizeof(struct lmv_stripe_md) +
531 lmv->desc.ld_tgt_count *
532 sizeof(struct lu_fid);
533 lmv_init_ea_size(obd->obd_self_export, easize, 0, 0);
537 lmv_init_unlock(lmv);
541 int lmv_check_connect(struct obd_device *obd)
543 struct lmv_obd *lmv = &obd->u.lmv;
544 struct lmv_tgt_desc *tgt;
554 if (lmv->connected) {
555 lmv_init_unlock(lmv);
559 if (lmv->desc.ld_tgt_count == 0) {
560 lmv_init_unlock(lmv);
561 CERROR("%s: no targets configured.\n", obd->obd_name);
565 CDEBUG(D_CONFIG, "Time to connect %s to %s\n",
566 lmv->cluuid.uuid, obd->obd_name);
568 LASSERT(lmv->tgts != NULL);
570 for (i = 0, tgt = lmv->tgts; i < lmv->desc.ld_tgt_count; i++, tgt++) {
571 rc = lmv_connect_mdc(obd, tgt);
576 lmv_set_timeouts(obd);
577 class_export_put(lmv->exp);
579 easize = lmv_get_easize(lmv);
580 lmv_init_ea_size(obd->obd_self_export, easize, 0, 0);
581 lmv_init_unlock(lmv);
590 --lmv->desc.ld_active_tgt_count;
591 rc2 = obd_disconnect(tgt->ltd_exp);
593 CERROR("LMV target %s disconnect on "
594 "MDC idx %d: error %d\n",
595 tgt->ltd_uuid.uuid, i, rc2);
599 class_disconnect(lmv->exp);
600 lmv_init_unlock(lmv);
604 static int lmv_disconnect_mdc(struct obd_device *obd, struct lmv_tgt_desc *tgt)
607 struct proc_dir_entry *lmv_proc_dir;
609 struct lmv_obd *lmv = &obd->u.lmv;
610 struct obd_device *mdc_obd;
614 LASSERT(tgt != NULL);
615 LASSERT(obd != NULL);
617 mdc_obd = class_exp2obd(tgt->ltd_exp);
620 mdc_obd->obd_force = obd->obd_force;
621 mdc_obd->obd_fail = obd->obd_fail;
622 mdc_obd->obd_no_recov = obd->obd_no_recov;
626 lmv_proc_dir = lprocfs_srch(obd->obd_proc_entry, "target_obds");
628 struct proc_dir_entry *mdc_symlink;
630 mdc_symlink = lprocfs_srch(lmv_proc_dir, mdc_obd->obd_name);
632 lprocfs_remove(&mdc_symlink);
634 CERROR("/proc/fs/lustre/%s/%s/target_obds/%s missing\n",
635 obd->obd_type->typ_name, obd->obd_name,
640 rc = obd_fid_fini(tgt->ltd_exp);
642 CERROR("Can't finanize fids factory\n");
644 CDEBUG(D_INFO, "Disconnected from %s(%s) successfully\n",
645 tgt->ltd_exp->exp_obd->obd_name,
646 tgt->ltd_exp->exp_obd->obd_uuid.uuid);
648 obd_register_observer(tgt->ltd_exp->exp_obd, NULL);
649 rc = obd_disconnect(tgt->ltd_exp);
651 if (tgt->ltd_active) {
652 CERROR("Target %s disconnect error %d\n",
653 tgt->ltd_uuid.uuid, rc);
657 lmv_activate_target(lmv, tgt, 0);
662 static int lmv_disconnect(struct obd_export *exp)
664 struct obd_device *obd = class_exp2obd(exp);
666 struct proc_dir_entry *lmv_proc_dir;
668 struct lmv_obd *lmv = &obd->u.lmv;
677 * Only disconnect the underlying layers on the final disconnect.
680 if (lmv->refcount != 0)
683 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
684 if (lmv->tgts[i].ltd_exp == NULL)
686 lmv_disconnect_mdc(obd, &lmv->tgts[i]);
690 lmv_proc_dir = lprocfs_srch(obd->obd_proc_entry, "target_obds");
692 lprocfs_remove(&lmv_proc_dir);
694 CERROR("/proc/fs/lustre/%s/%s/target_obds missing\n",
695 obd->obd_type->typ_name, obd->obd_name);
701 * This is the case when no real connection is established by
702 * lmv_check_connect().
705 class_export_put(exp);
706 rc = class_disconnect(exp);
707 if (lmv->refcount == 0)
712 static int lmv_iocontrol(unsigned int cmd, struct obd_export *exp,
713 int len, void *karg, void *uarg)
715 struct obd_device *obddev = class_exp2obd(exp);
716 struct lmv_obd *lmv = &obddev->u.lmv;
720 int count = lmv->desc.ld_tgt_count;
727 case IOC_OBD_STATFS: {
728 struct obd_ioctl_data *data = karg;
729 struct obd_device *mdc_obd;
730 struct obd_statfs stat_buf = {0};
733 memcpy(&index, data->ioc_inlbuf2, sizeof(__u32));
734 if ((index >= count))
737 if (!lmv->tgts[index].ltd_active)
740 mdc_obd = class_exp2obd(lmv->tgts[index].ltd_exp);
745 if (cfs_copy_to_user(data->ioc_pbuf2, obd2cli_tgt(mdc_obd),
746 min((int) data->ioc_plen2,
747 (int) sizeof(struct obd_uuid))))
750 rc = obd_statfs(NULL, lmv->tgts[index].ltd_exp, &stat_buf,
751 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
755 if (cfs_copy_to_user(data->ioc_pbuf1, &stat_buf,
756 min((int) data->ioc_plen1,
757 (int) sizeof(stat_buf))))
761 case OBD_IOC_QUOTACTL: {
762 struct if_quotactl *qctl = karg;
763 struct lmv_tgt_desc *tgt = NULL;
764 struct obd_quotactl *oqctl;
766 if (qctl->qc_valid == QC_MDTIDX) {
767 if (qctl->qc_idx < 0 || count <= qctl->qc_idx)
770 tgt = &lmv->tgts[qctl->qc_idx];
773 } else if (qctl->qc_valid == QC_UUID) {
774 for (i = 0; i < count; i++) {
776 if (!obd_uuid_equals(&tgt->ltd_uuid,
780 if (tgt->ltd_exp == NULL)
792 LASSERT(tgt && tgt->ltd_exp);
793 OBD_ALLOC_PTR(oqctl);
797 QCTL_COPY(oqctl, qctl);
798 rc = obd_quotactl(tgt->ltd_exp, oqctl);
800 QCTL_COPY(qctl, oqctl);
801 qctl->qc_valid = QC_MDTIDX;
802 qctl->obd_uuid = tgt->ltd_uuid;
807 case OBD_IOC_CHANGELOG_SEND:
808 case OBD_IOC_CHANGELOG_CLEAR: {
809 struct ioc_changelog *icc = karg;
811 if (icc->icc_mdtindex >= count)
814 rc = obd_iocontrol(cmd, lmv->tgts[icc->icc_mdtindex].ltd_exp,
815 sizeof(*icc), icc, NULL);
818 case LL_IOC_GET_CONNECT_FLAGS: {
819 rc = obd_iocontrol(cmd, lmv->tgts[0].ltd_exp, len, karg, uarg);
824 for (i = 0; i < count; i++) {
826 struct obd_device *mdc_obd;
828 if (lmv->tgts[i].ltd_exp == NULL)
830 /* ll_umount_begin() sets force flag but for lmv, not
831 * mdc. Let's pass it through */
832 mdc_obd = class_exp2obd(lmv->tgts[i].ltd_exp);
833 mdc_obd->obd_force = obddev->obd_force;
834 err = obd_iocontrol(cmd, lmv->tgts[i].ltd_exp, len,
836 if (err == -ENODATA && cmd == OBD_IOC_POLL_QUOTACHECK) {
839 if (lmv->tgts[i].ltd_active) {
840 CERROR("error: iocontrol MDC %s on MDT"
841 "idx %d cmd %x: err = %d\n",
842 lmv->tgts[i].ltd_uuid.uuid,
857 static int lmv_all_chars_policy(int count, const char *name,
868 static int lmv_nid_policy(struct lmv_obd *lmv)
870 struct obd_import *imp;
874 * XXX: To get nid we assume that underlying obd device is mdc.
876 imp = class_exp2cliimp(lmv->tgts[0].ltd_exp);
877 id = imp->imp_connection->c_self ^ (imp->imp_connection->c_self >> 32);
878 return id % lmv->desc.ld_tgt_count;
881 static int lmv_choose_mds(struct lmv_obd *lmv, struct md_op_data *op_data,
882 placement_policy_t placement)
885 case PLACEMENT_CHAR_POLICY:
886 return lmv_all_chars_policy(lmv->desc.ld_tgt_count,
888 op_data->op_namelen);
889 case PLACEMENT_NID_POLICY:
890 return lmv_nid_policy(lmv);
896 CERROR("Unsupported placement policy %x\n", placement);
901 * This is _inode_ placement policy function (not name).
903 static int lmv_placement_policy(struct obd_device *obd,
904 struct md_op_data *op_data,
907 struct lmv_obd *lmv = &obd->u.lmv;
908 struct lmv_object *obj;
912 LASSERT(mds != NULL);
914 if (lmv->desc.ld_tgt_count == 1) {
920 * Allocate new fid on target according to operation type and parent
923 obj = lmv_object_find(obd, &op_data->op_fid1);
924 if (obj != NULL || op_data->op_name == NULL ||
925 op_data->op_opc != LUSTRE_OPC_MKDIR) {
927 * Allocate fid for non-dir or for null name or for case parent
934 * If we have this flag turned on, and we see that
935 * parent dir is split, this means, that caller did not
936 * notice split yet. This is race and we would like to
937 * let caller know that.
939 if (op_data->op_bias & MDS_CHECK_SPLIT)
944 * Allocate new fid on same mds where parent fid is located and
945 * where operation will be sent. In case of split dir, ->op_fid1
946 * and ->op_mds here will contain fid and mds of slave directory
947 * object (assigned by caller).
949 *mds = op_data->op_mds;
953 * Parent directory is not split and we want to create a
954 * directory in it. Let's calculate where to place it according
955 * to operation data @op_data.
957 *mds = lmv_choose_mds(lmv, op_data, lmv->lmv_placement);
962 CERROR("Can't choose MDS, err = %d\n", rc);
964 LASSERT(*mds < lmv->desc.ld_tgt_count);
970 int __lmv_fid_alloc(struct lmv_obd *lmv, struct lu_fid *fid,
973 struct lmv_tgt_desc *tgt;
977 tgt = lmv_get_target(lmv, mds);
980 * New seq alloc and FLD setup should be atomic. Otherwise we may find
981 * on server that seq in new allocated fid is not yet known.
983 cfs_mutex_lock(&tgt->ltd_fid_mutex);
985 if (!tgt->ltd_active)
986 GOTO(out, rc = -ENODEV);
989 * Asking underlaying tgt layer to allocate new fid.
991 rc = obd_fid_alloc(tgt->ltd_exp, fid, NULL);
993 LASSERT(fid_is_sane(fid));
999 cfs_mutex_unlock(&tgt->ltd_fid_mutex);
1003 int lmv_fid_alloc(struct obd_export *exp, struct lu_fid *fid,
1004 struct md_op_data *op_data)
1006 struct obd_device *obd = class_exp2obd(exp);
1007 struct lmv_obd *lmv = &obd->u.lmv;
1012 LASSERT(op_data != NULL);
1013 LASSERT(fid != NULL);
1015 rc = lmv_placement_policy(obd, op_data, &mds);
1017 CERROR("Can't get target for allocating fid, "
1022 rc = __lmv_fid_alloc(lmv, fid, mds);
1024 CERROR("Can't alloc new fid, rc %d\n", rc);
1031 static int lmv_fid_delete(struct obd_export *exp, const struct lu_fid *fid)
1034 LASSERT(exp != NULL && fid != NULL);
1035 if (lmv_object_delete(exp, fid)) {
1036 CDEBUG(D_INODE, "Object "DFID" is destroyed.\n",
1042 static int lmv_setup(struct obd_device *obd, struct lustre_cfg *lcfg)
1044 struct lmv_obd *lmv = &obd->u.lmv;
1045 struct lprocfs_static_vars lvars;
1046 struct lmv_desc *desc;
1051 if (LUSTRE_CFG_BUFLEN(lcfg, 1) < 1) {
1052 CERROR("LMV setup requires a descriptor\n");
1056 desc = (struct lmv_desc *)lustre_cfg_buf(lcfg, 1);
1057 if (sizeof(*desc) > LUSTRE_CFG_BUFLEN(lcfg, 1)) {
1058 CERROR("Lmv descriptor size wrong: %d > %d\n",
1059 (int)sizeof(*desc), LUSTRE_CFG_BUFLEN(lcfg, 1));
1063 lmv->tgts_size = LMV_MAX_TGT_COUNT * sizeof(struct lmv_tgt_desc);
1065 OBD_ALLOC(lmv->tgts, lmv->tgts_size);
1066 if (lmv->tgts == NULL)
1069 for (i = 0; i < LMV_MAX_TGT_COUNT; i++) {
1070 cfs_mutex_init(&lmv->tgts[i].ltd_fid_mutex);
1071 lmv->tgts[i].ltd_idx = i;
1074 lmv->datas_size = LMV_MAX_TGT_COUNT * sizeof(struct obd_connect_data);
1076 OBD_ALLOC(lmv->datas, lmv->datas_size);
1077 if (lmv->datas == NULL)
1078 GOTO(out_free_tgts, rc = -ENOMEM);
1080 obd_str2uuid(&lmv->desc.ld_uuid, desc->ld_uuid.uuid);
1081 lmv->desc.ld_tgt_count = 0;
1082 lmv->desc.ld_active_tgt_count = 0;
1083 lmv->max_cookiesize = 0;
1084 lmv->max_def_easize = 0;
1085 lmv->max_easize = 0;
1086 lmv->lmv_placement = PLACEMENT_CHAR_POLICY;
1088 cfs_spin_lock_init(&lmv->lmv_lock);
1089 cfs_mutex_init(&lmv->init_mutex);
1091 rc = lmv_object_setup(obd);
1093 CERROR("Can't setup LMV object manager, error %d.\n", rc);
1094 GOTO(out_free_datas, rc);
1097 lprocfs_lmv_init_vars(&lvars);
1098 lprocfs_obd_setup(obd, lvars.obd_vars);
1101 rc = lprocfs_seq_create(obd->obd_proc_entry, "target_obd",
1102 0444, &lmv_proc_target_fops, obd);
1104 CWARN("%s: error adding LMV target_obd file: rc = %d\n",
1108 rc = fld_client_init(&lmv->lmv_fld, obd->obd_name,
1109 LUSTRE_CLI_FLD_HASH_DHT);
1111 CERROR("Can't init FLD, err %d\n", rc);
1112 GOTO(out_free_datas, rc);
1118 OBD_FREE(lmv->datas, lmv->datas_size);
1121 OBD_FREE(lmv->tgts, lmv->tgts_size);
1126 static int lmv_cleanup(struct obd_device *obd)
1128 struct lmv_obd *lmv = &obd->u.lmv;
1131 fld_client_fini(&lmv->lmv_fld);
1132 lmv_object_cleanup(obd);
1133 OBD_FREE(lmv->datas, lmv->datas_size);
1134 OBD_FREE(lmv->tgts, lmv->tgts_size);
1139 static int lmv_process_config(struct obd_device *obd, obd_count len, void *buf)
1141 struct lustre_cfg *lcfg = buf;
1142 struct obd_uuid tgt_uuid;
1146 switch(lcfg->lcfg_command) {
1148 if (LUSTRE_CFG_BUFLEN(lcfg, 1) > sizeof(tgt_uuid.uuid))
1149 GOTO(out, rc = -EINVAL);
1151 obd_str2uuid(&tgt_uuid, lustre_cfg_string(lcfg, 1));
1152 rc = lmv_add_target(obd, &tgt_uuid);
1155 CERROR("Unknown command: %d\n", lcfg->lcfg_command);
1156 GOTO(out, rc = -EINVAL);
1163 static int lmv_statfs(const struct lu_env *env, struct obd_export *exp,
1164 struct obd_statfs *osfs, __u64 max_age, __u32 flags)
1166 struct obd_device *obd = class_exp2obd(exp);
1167 struct lmv_obd *lmv = &obd->u.lmv;
1168 struct obd_statfs *temp;
1173 rc = lmv_check_connect(obd);
1177 OBD_ALLOC(temp, sizeof(*temp));
1181 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
1182 if (lmv->tgts[i].ltd_exp == NULL)
1185 rc = obd_statfs(env, lmv->tgts[i].ltd_exp, temp,
1188 CERROR("can't stat MDS #%d (%s), error %d\n", i,
1189 lmv->tgts[i].ltd_exp->exp_obd->obd_name,
1191 GOTO(out_free_temp, rc);
1196 osfs->os_bavail += temp->os_bavail;
1197 osfs->os_blocks += temp->os_blocks;
1198 osfs->os_ffree += temp->os_ffree;
1199 osfs->os_files += temp->os_files;
1205 OBD_FREE(temp, sizeof(*temp));
1209 static int lmv_getstatus(struct obd_export *exp,
1211 struct obd_capa **pc)
1213 struct obd_device *obd = exp->exp_obd;
1214 struct lmv_obd *lmv = &obd->u.lmv;
1218 rc = lmv_check_connect(obd);
1222 rc = md_getstatus(lmv->tgts[0].ltd_exp, fid, pc);
1226 static int lmv_getxattr(struct obd_export *exp, const struct lu_fid *fid,
1227 struct obd_capa *oc, obd_valid valid, const char *name,
1228 const char *input, int input_size, int output_size,
1229 int flags, struct ptlrpc_request **request)
1231 struct obd_device *obd = exp->exp_obd;
1232 struct lmv_obd *lmv = &obd->u.lmv;
1233 struct lmv_tgt_desc *tgt;
1237 rc = lmv_check_connect(obd);
1241 tgt = lmv_find_target(lmv, fid);
1243 RETURN(PTR_ERR(tgt));
1245 rc = md_getxattr(tgt->ltd_exp, fid, oc, valid, name, input,
1246 input_size, output_size, flags, request);
1251 static int lmv_setxattr(struct obd_export *exp, const struct lu_fid *fid,
1252 struct obd_capa *oc, obd_valid valid, const char *name,
1253 const char *input, int input_size, int output_size,
1254 int flags, __u32 suppgid,
1255 struct ptlrpc_request **request)
1257 struct obd_device *obd = exp->exp_obd;
1258 struct lmv_obd *lmv = &obd->u.lmv;
1259 struct lmv_tgt_desc *tgt;
1263 rc = lmv_check_connect(obd);
1267 tgt = lmv_find_target(lmv, fid);
1269 RETURN(PTR_ERR(tgt));
1271 rc = md_setxattr(tgt->ltd_exp, fid, oc, valid, name, input,
1272 input_size, output_size, flags, suppgid,
1278 static int lmv_getattr(struct obd_export *exp, struct md_op_data *op_data,
1279 struct ptlrpc_request **request)
1281 struct obd_device *obd = exp->exp_obd;
1282 struct lmv_obd *lmv = &obd->u.lmv;
1283 struct lmv_tgt_desc *tgt;
1284 struct lmv_object *obj;
1289 rc = lmv_check_connect(obd);
1293 tgt = lmv_find_target(lmv, &op_data->op_fid1);
1295 RETURN(PTR_ERR(tgt));
1297 if (op_data->op_valid & OBD_MD_MDTIDX) {
1298 op_data->op_mds = tgt->ltd_idx;
1302 rc = md_getattr(tgt->ltd_exp, op_data, request);
1306 obj = lmv_object_find_lock(obd, &op_data->op_fid1);
1308 CDEBUG(D_INODE, "GETATTR for "DFID" %s\n", PFID(&op_data->op_fid1),
1309 obj ? "(split)" : "");
1312 * If object is split, then we loop over all the slaves and gather size
1313 * attribute. In ideal world we would have to gather also mds field from
1314 * all slaves, as object is spread over the cluster and this is
1315 * definitely interesting information and it is not good to loss it,
1319 struct mdt_body *body;
1321 if (*request == NULL) {
1322 lmv_object_put(obj);
1326 body = req_capsule_server_get(&(*request)->rq_pill,
1328 LASSERT(body != NULL);
1330 for (i = 0; i < obj->lo_objcount; i++) {
1331 if (lmv->tgts[i].ltd_exp == NULL) {
1332 CWARN("%s: NULL export for %d\n",
1338 * Skip master object.
1340 if (lu_fid_eq(&obj->lo_fid, &obj->lo_stripes[i].ls_fid))
1343 body->size += obj->lo_stripes[i].ls_size;
1346 lmv_object_put_unlock(obj);
1352 static int lmv_change_cbdata(struct obd_export *exp, const struct lu_fid *fid,
1353 ldlm_iterator_t it, void *data)
1355 struct obd_device *obd = exp->exp_obd;
1356 struct lmv_obd *lmv = &obd->u.lmv;
1361 rc = lmv_check_connect(obd);
1365 CDEBUG(D_INODE, "CBDATA for "DFID"\n", PFID(fid));
1368 * With CMD every object can have two locks in different namespaces:
1369 * lookup lock in space of mds storing direntry and update/open lock in
1370 * space of mds storing inode.
1372 for (i = 0; i < lmv->desc.ld_tgt_count; i++)
1373 md_change_cbdata(lmv->tgts[i].ltd_exp, fid, it, data);
1378 static int lmv_find_cbdata(struct obd_export *exp, const struct lu_fid *fid,
1379 ldlm_iterator_t it, void *data)
1381 struct obd_device *obd = exp->exp_obd;
1382 struct lmv_obd *lmv = &obd->u.lmv;
1387 rc = lmv_check_connect(obd);
1391 CDEBUG(D_INODE, "CBDATA for "DFID"\n", PFID(fid));
1394 * With CMD every object can have two locks in different namespaces:
1395 * lookup lock in space of mds storing direntry and update/open lock in
1396 * space of mds storing inode.
1398 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
1399 rc = md_find_cbdata(lmv->tgts[i].ltd_exp, fid, it, data);
1408 static int lmv_close(struct obd_export *exp, struct md_op_data *op_data,
1409 struct md_open_data *mod, struct ptlrpc_request **request)
1411 struct obd_device *obd = exp->exp_obd;
1412 struct lmv_obd *lmv = &obd->u.lmv;
1413 struct lmv_tgt_desc *tgt;
1417 rc = lmv_check_connect(obd);
1421 tgt = lmv_find_target(lmv, &op_data->op_fid1);
1423 RETURN(PTR_ERR(tgt));
1425 CDEBUG(D_INODE, "CLOSE "DFID"\n", PFID(&op_data->op_fid1));
1426 rc = md_close(tgt->ltd_exp, op_data, mod, request);
1431 * Called in the case MDS returns -ERESTART on create on open, what means that
1432 * directory is split and its LMV presentation object has to be updated.
1434 int lmv_handle_split(struct obd_export *exp, const struct lu_fid *fid)
1436 struct obd_device *obd = exp->exp_obd;
1437 struct lmv_obd *lmv = &obd->u.lmv;
1438 struct ptlrpc_request *req = NULL;
1439 struct lmv_tgt_desc *tgt;
1440 struct lmv_object *obj;
1441 struct lustre_md md;
1442 struct md_op_data *op_data;
1449 mealen = lmv_get_easize(lmv);
1451 valid = OBD_MD_FLEASIZE | OBD_MD_FLDIREA | OBD_MD_MEA;
1453 tgt = lmv_find_target(lmv, fid);
1455 RETURN(PTR_ERR(tgt));
1458 * Time to update mea of parent fid.
1461 OBD_ALLOC_PTR(op_data);
1462 if (op_data == NULL)
1465 op_data->op_fid1 = *fid;
1466 op_data->op_mode = mealen;
1467 op_data->op_valid = valid;
1469 rc = md_getattr(tgt->ltd_exp, op_data, &req);
1470 OBD_FREE_PTR(op_data);
1472 CERROR("md_getattr() failed, error %d\n", rc);
1476 rc = md_get_lustre_md(tgt->ltd_exp, req, NULL, exp, &md);
1478 CERROR("md_get_lustre_md() failed, error %d\n", rc);
1483 GOTO(cleanup, rc = -ENODATA);
1485 obj = lmv_object_create(exp, fid, md.mea);
1489 lmv_object_put(obj);
1491 obd_free_memmd(exp, (void *)&md.mea);
1495 ptlrpc_req_finished(req);
1499 int lmv_create(struct obd_export *exp, struct md_op_data *op_data,
1500 const void *data, int datalen, int mode, __u32 uid,
1501 __u32 gid, cfs_cap_t cap_effective, __u64 rdev,
1502 struct ptlrpc_request **request)
1504 struct obd_device *obd = exp->exp_obd;
1505 struct lmv_obd *lmv = &obd->u.lmv;
1506 struct lmv_tgt_desc *tgt;
1507 struct lmv_object *obj;
1513 rc = lmv_check_connect(obd);
1517 if (!lmv->desc.ld_active_tgt_count)
1523 obj = lmv_object_find(obd, &op_data->op_fid1);
1525 sidx = raw_name2idx(obj->lo_hashtype, obj->lo_objcount,
1526 op_data->op_name, op_data->op_namelen);
1527 op_data->op_fid1 = obj->lo_stripes[sidx].ls_fid;
1528 op_data->op_bias &= ~MDS_CHECK_SPLIT;
1529 op_data->op_mds = obj->lo_stripes[sidx].ls_mds;
1530 tgt = lmv_get_target(lmv, op_data->op_mds);
1531 lmv_object_put(obj);
1533 tgt = lmv_find_target(lmv, &op_data->op_fid1);
1534 op_data->op_bias |= MDS_CHECK_SPLIT;
1535 op_data->op_mds = tgt->ltd_idx;
1539 RETURN(PTR_ERR(tgt));
1541 rc = lmv_fid_alloc(exp, &op_data->op_fid2, op_data);
1542 if (rc == -ERESTART)
1547 CDEBUG(D_INODE, "CREATE '%*s' on "DFID" -> mds #%x\n",
1548 op_data->op_namelen, op_data->op_name, PFID(&op_data->op_fid1),
1551 op_data->op_flags |= MF_MDC_CANCEL_FID1;
1552 rc = md_create(tgt->ltd_exp, op_data, data, datalen, mode, uid, gid,
1553 cap_effective, rdev, request);
1555 if (*request == NULL)
1557 CDEBUG(D_INODE, "Created - "DFID"\n", PFID(&op_data->op_fid2));
1558 } else if (rc == -ERESTART) {
1559 LASSERT(*request != NULL);
1560 DEBUG_REQ(D_WARNING|D_RPCTRACE, *request,
1561 "Got -ERESTART during create!\n");
1562 ptlrpc_req_finished(*request);
1566 * Directory got split. Time to update local object and repeat
1567 * the request with proper MDS.
1569 rc = lmv_handle_split(exp, &op_data->op_fid1);
1571 rc = lmv_allocate_slaves(obd, &op_data->op_fid1,
1572 op_data, &op_data->op_fid2);
1581 static int lmv_done_writing(struct obd_export *exp,
1582 struct md_op_data *op_data,
1583 struct md_open_data *mod)
1585 struct obd_device *obd = exp->exp_obd;
1586 struct lmv_obd *lmv = &obd->u.lmv;
1587 struct lmv_tgt_desc *tgt;
1591 rc = lmv_check_connect(obd);
1595 tgt = lmv_find_target(lmv, &op_data->op_fid1);
1597 RETURN(PTR_ERR(tgt));
1599 rc = md_done_writing(tgt->ltd_exp, op_data, mod);
1604 lmv_enqueue_slaves(struct obd_export *exp, struct ldlm_enqueue_info *einfo,
1605 struct lookup_intent *it, struct md_op_data *op_data,
1606 struct lustre_handle *lockh, void *lmm, int lmmsize)
1608 struct obd_device *obd = exp->exp_obd;
1609 struct lmv_obd *lmv = &obd->u.lmv;
1610 struct lmv_stripe_md *mea = op_data->op_mea1;
1611 struct md_op_data *op_data2;
1612 struct lmv_tgt_desc *tgt;
1617 OBD_ALLOC_PTR(op_data2);
1618 if (op_data2 == NULL)
1621 LASSERT(mea != NULL);
1622 for (i = 0; i < mea->mea_count; i++) {
1623 memset(op_data2, 0, sizeof(*op_data2));
1624 op_data2->op_fid1 = mea->mea_ids[i];
1625 op_data2->op_bias = 0;
1627 tgt = lmv_find_target(lmv, &op_data2->op_fid1);
1629 GOTO(cleanup, rc = PTR_ERR(tgt));
1631 if (tgt->ltd_exp == NULL)
1634 rc = md_enqueue(tgt->ltd_exp, einfo, it, op_data2,
1635 lockh + i, lmm, lmmsize, NULL, 0);
1637 CDEBUG(D_INODE, "Take lock on slave "DFID" -> %d/%d\n",
1638 PFID(&mea->mea_ids[i]), rc, it->d.lustre.it_status);
1643 if (it->d.lustre.it_data) {
1644 struct ptlrpc_request *req;
1645 req = (struct ptlrpc_request *)it->d.lustre.it_data;
1646 ptlrpc_req_finished(req);
1649 if (it->d.lustre.it_status)
1650 GOTO(cleanup, rc = it->d.lustre.it_status);
1655 OBD_FREE_PTR(op_data2);
1659 * Drop all taken locks.
1662 if (lockh[i].cookie)
1663 ldlm_lock_decref(lockh + i, einfo->ei_mode);
1664 lockh[i].cookie = 0;
1671 lmv_enqueue_remote(struct obd_export *exp, struct ldlm_enqueue_info *einfo,
1672 struct lookup_intent *it, struct md_op_data *op_data,
1673 struct lustre_handle *lockh, void *lmm, int lmmsize,
1674 int extra_lock_flags)
1676 struct ptlrpc_request *req = it->d.lustre.it_data;
1677 struct obd_device *obd = exp->exp_obd;
1678 struct lmv_obd *lmv = &obd->u.lmv;
1679 struct lustre_handle plock;
1680 struct lmv_tgt_desc *tgt;
1681 struct md_op_data *rdata;
1683 struct mdt_body *body;
1688 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
1689 LASSERT(body != NULL);
1691 if (!(body->valid & OBD_MD_MDS))
1694 CDEBUG(D_INODE, "REMOTE_ENQUEUE '%s' on "DFID" -> "DFID"\n",
1695 LL_IT2STR(it), PFID(&op_data->op_fid1), PFID(&body->fid1));
1698 * We got LOOKUP lock, but we really need attrs.
1700 pmode = it->d.lustre.it_lock_mode;
1701 LASSERT(pmode != 0);
1702 memcpy(&plock, lockh, sizeof(plock));
1703 it->d.lustre.it_lock_mode = 0;
1704 it->d.lustre.it_data = NULL;
1707 it->d.lustre.it_disposition &= ~DISP_ENQ_COMPLETE;
1708 ptlrpc_req_finished(req);
1710 tgt = lmv_find_target(lmv, &fid1);
1712 GOTO(out, rc = PTR_ERR(tgt));
1714 OBD_ALLOC_PTR(rdata);
1716 GOTO(out, rc = -ENOMEM);
1718 rdata->op_fid1 = fid1;
1719 rdata->op_bias = MDS_CROSS_REF;
1721 rc = md_enqueue(tgt->ltd_exp, einfo, it, rdata, lockh,
1722 lmm, lmmsize, NULL, extra_lock_flags);
1723 OBD_FREE_PTR(rdata);
1726 ldlm_lock_decref(&plock, pmode);
1731 lmv_enqueue(struct obd_export *exp, struct ldlm_enqueue_info *einfo,
1732 struct lookup_intent *it, struct md_op_data *op_data,
1733 struct lustre_handle *lockh, void *lmm, int lmmsize,
1734 struct ptlrpc_request **req, int extra_lock_flags)
1736 struct obd_device *obd = exp->exp_obd;
1737 struct lmv_obd *lmv = &obd->u.lmv;
1738 struct lmv_tgt_desc *tgt;
1739 struct lmv_object *obj;
1744 rc = lmv_check_connect(obd);
1748 CDEBUG(D_INODE, "ENQUEUE '%s' on "DFID"\n",
1749 LL_IT2STR(it), PFID(&op_data->op_fid1));
1751 if (op_data->op_mea1 && it && it->it_op == IT_UNLINK) {
1752 rc = lmv_enqueue_slaves(exp, einfo, it, op_data,
1753 lockh, lmm, lmmsize);
1757 obj = lmv_object_find(obd, &op_data->op_fid1);
1758 if (obj && op_data->op_namelen) {
1759 sidx = raw_name2idx(obj->lo_hashtype,
1761 (char *)op_data->op_name,
1762 op_data->op_namelen);
1763 op_data->op_fid1 = obj->lo_stripes[sidx].ls_fid;
1764 tgt = lmv_get_target(lmv, obj->lo_stripes[sidx].ls_mds);
1766 tgt = lmv_find_target(lmv, &op_data->op_fid1);
1769 lmv_object_put(obj);
1772 RETURN(PTR_ERR(tgt));
1774 CDEBUG(D_INODE, "ENQUEUE '%s' on "DFID" -> mds #%d\n",
1775 LL_IT2STR(it), PFID(&op_data->op_fid1), tgt->ltd_idx);
1777 rc = md_enqueue(tgt->ltd_exp, einfo, it, op_data, lockh,
1778 lmm, lmmsize, req, extra_lock_flags);
1780 if (rc == 0 && it && it->it_op == IT_OPEN) {
1781 rc = lmv_enqueue_remote(exp, einfo, it, op_data, lockh,
1782 lmm, lmmsize, extra_lock_flags);
1788 lmv_getattr_name(struct obd_export *exp,struct md_op_data *op_data,
1789 struct ptlrpc_request **request)
1791 struct ptlrpc_request *req = NULL;
1792 struct obd_device *obd = exp->exp_obd;
1793 struct lmv_obd *lmv = &obd->u.lmv;
1794 struct lu_fid rid = op_data->op_fid1;
1795 struct lmv_tgt_desc *tgt;
1796 struct mdt_body *body;
1797 struct lmv_object *obj;
1798 obd_valid valid = op_data->op_valid;
1804 rc = lmv_check_connect(obd);
1811 obj = lmv_object_find(obd, &rid);
1813 sidx = raw_name2idx(obj->lo_hashtype, obj->lo_objcount,
1814 op_data->op_name, op_data->op_namelen);
1815 rid = obj->lo_stripes[sidx].ls_fid;
1816 tgt = lmv_get_target(lmv, obj->lo_stripes[sidx].ls_mds);
1817 op_data->op_mds = obj->lo_stripes[sidx].ls_mds;
1818 valid &= ~OBD_MD_FLCKSPLIT;
1819 lmv_object_put(obj);
1821 tgt = lmv_find_target(lmv, &rid);
1822 valid |= OBD_MD_FLCKSPLIT;
1823 op_data->op_mds = tgt->ltd_idx;
1826 RETURN(PTR_ERR(tgt));
1828 CDEBUG(D_INODE, "GETATTR_NAME for %*s on "DFID" - "DFID" -> mds #%d\n",
1829 op_data->op_namelen, op_data->op_name, PFID(&op_data->op_fid1),
1830 PFID(&rid), tgt->ltd_idx);
1832 op_data->op_valid = valid;
1833 op_data->op_fid1 = rid;
1834 rc = md_getattr_name(tgt->ltd_exp, op_data, request);
1836 body = req_capsule_server_get(&(*request)->rq_pill,
1838 LASSERT(body != NULL);
1840 if (body->valid & OBD_MD_MDS) {
1842 CDEBUG(D_INODE, "Request attrs for "DFID"\n",
1845 tgt = lmv_find_target(lmv, &rid);
1847 ptlrpc_req_finished(*request);
1848 RETURN(PTR_ERR(tgt));
1851 op_data->op_fid1 = rid;
1852 op_data->op_valid |= OBD_MD_FLCROSSREF;
1853 op_data->op_namelen = 0;
1854 op_data->op_name = NULL;
1855 rc = md_getattr_name(tgt->ltd_exp, op_data, &req);
1856 ptlrpc_req_finished(*request);
1859 } else if (rc == -ERESTART) {
1860 LASSERT(*request != NULL);
1861 DEBUG_REQ(D_WARNING|D_RPCTRACE, *request,
1862 "Got -ERESTART during getattr!\n");
1863 ptlrpc_req_finished(*request);
1867 * Directory got split. Time to update local object and repeat
1868 * the request with proper MDS.
1870 rc = lmv_handle_split(exp, &rid);
1877 #define md_op_data_fid(op_data, fl) \
1878 (fl == MF_MDC_CANCEL_FID1 ? &op_data->op_fid1 : \
1879 fl == MF_MDC_CANCEL_FID2 ? &op_data->op_fid2 : \
1880 fl == MF_MDC_CANCEL_FID3 ? &op_data->op_fid3 : \
1881 fl == MF_MDC_CANCEL_FID4 ? &op_data->op_fid4 : \
1884 static int lmv_early_cancel_slaves(struct obd_export *exp,
1885 struct md_op_data *op_data, int op_tgt,
1886 ldlm_mode_t mode, int bits, int flag)
1888 struct obd_device *obd = exp->exp_obd;
1889 struct lmv_obd *lmv = &obd->u.lmv;
1890 ldlm_policy_data_t policy = {{0}};
1891 struct lu_fid *op_fid;
1892 struct lu_fid *st_fid;
1893 struct lmv_tgt_desc *tgt;
1894 struct lmv_object *obj;
1899 op_fid = md_op_data_fid(op_data, flag);
1900 if (!fid_is_sane(op_fid))
1903 obj = lmv_object_find(obd, op_fid);
1907 policy.l_inodebits.bits = bits;
1908 for (i = 0; i < obj->lo_objcount; i++) {
1909 tgt = lmv_get_target(lmv, obj->lo_stripes[i].ls_mds);
1910 st_fid = &obj->lo_stripes[i].ls_fid;
1911 if (op_tgt != tgt->ltd_idx) {
1912 CDEBUG(D_INODE, "EARLY_CANCEL slave "DFID" -> mds #%d\n",
1913 PFID(st_fid), tgt->ltd_idx);
1914 rc = md_cancel_unused(tgt->ltd_exp, st_fid, &policy,
1915 mode, LCF_ASYNC, NULL);
1917 GOTO(out_put_obj, rc);
1920 "EARLY_CANCEL skip operation target %d on "DFID"\n",
1921 op_tgt, PFID(st_fid));
1923 * Do not cancel locks for operation target, they will
1924 * be handled later in underlaying layer when calling
1925 * function we run on behalf of.
1928 op_data->op_flags |= flag;
1933 lmv_object_put(obj);
1937 static int lmv_early_cancel(struct obd_export *exp, struct md_op_data *op_data,
1938 int op_tgt, ldlm_mode_t mode, int bits, int flag)
1940 struct lu_fid *fid = md_op_data_fid(op_data, flag);
1941 struct obd_device *obd = exp->exp_obd;
1942 struct lmv_obd *lmv = &obd->u.lmv;
1943 struct lmv_tgt_desc *tgt;
1944 ldlm_policy_data_t policy = {{0}};
1945 struct lmv_object *obj;
1949 if (!fid_is_sane(fid))
1952 obj = lmv_object_find(obd, fid);
1954 rc = lmv_early_cancel_slaves(exp, op_data, op_tgt, mode,
1956 lmv_object_put(obj);
1958 tgt = lmv_find_target(lmv, fid);
1960 RETURN(PTR_ERR(tgt));
1962 if (tgt->ltd_idx != op_tgt) {
1963 CDEBUG(D_INODE, "EARLY_CANCEL on "DFID"\n", PFID(fid));
1964 policy.l_inodebits.bits = bits;
1965 rc = md_cancel_unused(tgt->ltd_exp, fid, &policy,
1966 mode, LCF_ASYNC, NULL);
1969 "EARLY_CANCEL skip operation target %d on "DFID"\n",
1971 op_data->op_flags |= flag;
1980 * llite passes fid of an target inode in op_data->op_fid1 and id of directory in
1983 static int lmv_link(struct obd_export *exp, struct md_op_data *op_data,
1984 struct ptlrpc_request **request)
1986 struct obd_device *obd = exp->exp_obd;
1987 struct lmv_obd *lmv = &obd->u.lmv;
1988 struct lmv_tgt_desc *tgt;
1989 struct lmv_object *obj;
1996 rc = lmv_check_connect(obd);
2003 LASSERT(op_data->op_namelen != 0);
2005 CDEBUG(D_INODE, "LINK "DFID":%*s to "DFID"\n",
2006 PFID(&op_data->op_fid2), op_data->op_namelen,
2007 op_data->op_name, PFID(&op_data->op_fid1));
2009 obj = lmv_object_find(obd, &op_data->op_fid2);
2011 sidx = raw_name2idx(obj->lo_hashtype,
2014 op_data->op_namelen);
2015 op_data->op_fid2 = obj->lo_stripes[sidx].ls_fid;
2016 mds = obj->lo_stripes[sidx].ls_mds;
2017 lmv_object_put(obj);
2019 rc = lmv_fld_lookup(lmv, &op_data->op_fid2, &mds);
2024 CDEBUG(D_INODE, "Forward to mds #%x ("DFID")\n",
2025 mds, PFID(&op_data->op_fid1));
2027 op_data->op_fsuid = cfs_curproc_fsuid();
2028 op_data->op_fsgid = cfs_curproc_fsgid();
2029 op_data->op_cap = cfs_curproc_cap_pack();
2030 tgt = lmv_get_target(lmv, mds);
2033 * Cancel UPDATE lock on child (fid1).
2035 op_data->op_flags |= MF_MDC_CANCEL_FID2;
2036 rc = lmv_early_cancel(exp, op_data, tgt->ltd_idx, LCK_EX,
2037 MDS_INODELOCK_UPDATE, MF_MDC_CANCEL_FID1);
2039 rc = md_link(tgt->ltd_exp, op_data, request);
2040 if (rc == -ERESTART) {
2041 LASSERT(*request != NULL);
2042 DEBUG_REQ(D_WARNING|D_RPCTRACE, *request,
2043 "Got -ERESTART during link!\n");
2044 ptlrpc_req_finished(*request);
2048 * Directory got split. Time to update local object and repeat
2049 * the request with proper MDS.
2051 rc = lmv_handle_split(exp, &op_data->op_fid2);
2059 static int lmv_rename(struct obd_export *exp, struct md_op_data *op_data,
2060 const char *old, int oldlen, const char *new, int newlen,
2061 struct ptlrpc_request **request)
2063 struct obd_device *obd = exp->exp_obd;
2064 struct lmv_obd *lmv = &obd->u.lmv;
2065 struct lmv_tgt_desc *src_tgt;
2069 struct lmv_object *obj;
2074 LASSERT(oldlen != 0);
2076 CDEBUG(D_INODE, "RENAME %*s in "DFID" to %*s in "DFID"\n",
2077 oldlen, old, PFID(&op_data->op_fid1),
2078 newlen, new, PFID(&op_data->op_fid2));
2080 rc = lmv_check_connect(obd);
2087 obj = lmv_object_find(obd, &op_data->op_fid1);
2089 sidx = raw_name2idx(obj->lo_hashtype, obj->lo_objcount,
2090 (char *)old, oldlen);
2091 op_data->op_fid1 = obj->lo_stripes[sidx].ls_fid;
2092 mds1 = obj->lo_stripes[sidx].ls_mds;
2093 CDEBUG(D_INODE, "Parent obj "DFID"\n", PFID(&op_data->op_fid1));
2094 lmv_object_put(obj);
2096 rc = lmv_fld_lookup(lmv, &op_data->op_fid1, &mds1);
2101 obj = lmv_object_find(obd, &op_data->op_fid2);
2104 * Directory is already split, so we have to forward request to
2107 sidx = raw_name2idx(obj->lo_hashtype, obj->lo_objcount,
2108 (char *)new, newlen);
2110 mds2 = obj->lo_stripes[sidx].ls_mds;
2111 op_data->op_fid2 = obj->lo_stripes[sidx].ls_fid;
2112 CDEBUG(D_INODE, "Parent obj "DFID"\n", PFID(&op_data->op_fid2));
2113 lmv_object_put(obj);
2115 rc = lmv_fld_lookup(lmv, &op_data->op_fid2, &mds2);
2120 op_data->op_fsuid = cfs_curproc_fsuid();
2121 op_data->op_fsgid = cfs_curproc_fsgid();
2122 op_data->op_cap = cfs_curproc_cap_pack();
2124 src_tgt = lmv_get_target(lmv, mds1);
2127 * LOOKUP lock on src child (fid3) should also be cancelled for
2128 * src_tgt in mdc_rename.
2130 op_data->op_flags |= MF_MDC_CANCEL_FID1 | MF_MDC_CANCEL_FID3;
2133 * Cancel UPDATE locks on tgt parent (fid2), tgt_tgt is its
2136 rc = lmv_early_cancel(exp, op_data, src_tgt->ltd_idx,
2137 LCK_EX, MDS_INODELOCK_UPDATE,
2138 MF_MDC_CANCEL_FID2);
2141 * Cancel LOOKUP locks on tgt child (fid4) for parent tgt_tgt.
2144 rc = lmv_early_cancel(exp, op_data, src_tgt->ltd_idx,
2145 LCK_EX, MDS_INODELOCK_LOOKUP,
2146 MF_MDC_CANCEL_FID4);
2150 * Cancel all the locks on tgt child (fid4).
2153 rc = lmv_early_cancel(exp, op_data, src_tgt->ltd_idx,
2154 LCK_EX, MDS_INODELOCK_FULL,
2155 MF_MDC_CANCEL_FID4);
2158 rc = md_rename(src_tgt->ltd_exp, op_data, old, oldlen,
2159 new, newlen, request);
2161 if (rc == -ERESTART) {
2162 LASSERT(*request != NULL);
2163 DEBUG_REQ(D_WARNING|D_RPCTRACE, *request,
2164 "Got -ERESTART during rename!\n");
2165 ptlrpc_req_finished(*request);
2169 * Directory got split. Time to update local object and repeat
2170 * the request with proper MDS.
2172 rc = lmv_handle_split(exp, &op_data->op_fid1);
2179 static int lmv_setattr(struct obd_export *exp, struct md_op_data *op_data,
2180 void *ea, int ealen, void *ea2, int ea2len,
2181 struct ptlrpc_request **request,
2182 struct md_open_data **mod)
2184 struct obd_device *obd = exp->exp_obd;
2185 struct lmv_obd *lmv = &obd->u.lmv;
2186 struct ptlrpc_request *req;
2187 struct lmv_tgt_desc *tgt;
2188 struct lmv_object *obj;
2193 rc = lmv_check_connect(obd);
2197 obj = lmv_object_find(obd, &op_data->op_fid1);
2199 CDEBUG(D_INODE, "SETATTR for "DFID", valid 0x%x%s\n",
2200 PFID(&op_data->op_fid1), op_data->op_attr.ia_valid,
2201 obj ? ", split" : "");
2203 op_data->op_flags |= MF_MDC_CANCEL_FID1;
2205 for (i = 0; i < obj->lo_objcount; i++) {
2206 op_data->op_fid1 = obj->lo_stripes[i].ls_fid;
2208 tgt = lmv_get_target(lmv, obj->lo_stripes[i].ls_mds);
2214 rc = md_setattr(tgt->ltd_exp, op_data, ea, ealen,
2215 ea2, ea2len, &req, mod);
2217 if (lu_fid_eq(&obj->lo_fid, &obj->lo_stripes[i].ls_fid)) {
2219 * This is master object and this request should
2220 * be returned back to llite.
2224 ptlrpc_req_finished(req);
2230 lmv_object_put(obj);
2232 tgt = lmv_find_target(lmv, &op_data->op_fid1);
2234 RETURN(PTR_ERR(tgt));
2236 rc = md_setattr(tgt->ltd_exp, op_data, ea, ealen, ea2,
2237 ea2len, request, mod);
2242 static int lmv_sync(struct obd_export *exp, const struct lu_fid *fid,
2243 struct obd_capa *oc, struct ptlrpc_request **request)
2245 struct obd_device *obd = exp->exp_obd;
2246 struct lmv_obd *lmv = &obd->u.lmv;
2247 struct lmv_tgt_desc *tgt;
2251 rc = lmv_check_connect(obd);
2255 tgt = lmv_find_target(lmv, fid);
2257 RETURN(PTR_ERR(tgt));
2259 rc = md_sync(tgt->ltd_exp, fid, oc, request);
2264 * Main purpose of LMV blocking ast is to remove split directory LMV
2265 * presentation object (struct lmv_object) attached to the lock being revoked.
2267 int lmv_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
2268 void *data, int flag)
2270 struct lustre_handle lockh;
2271 struct lmv_object *obj;
2276 case LDLM_CB_BLOCKING:
2277 ldlm_lock2handle(lock, &lockh);
2278 rc = ldlm_cli_cancel(&lockh);
2280 CDEBUG(D_INODE, "ldlm_cli_cancel: %d\n", rc);
2284 case LDLM_CB_CANCELING:
2286 * Time to drop cached attrs for split directory object
2288 obj = lock->l_ast_data;
2290 CDEBUG(D_INODE, "Cancel %s on "LPU64"/"LPU64
2291 ", master "DFID"\n",
2292 lock->l_resource->lr_name.name[3] == 1 ?
2293 "LOOKUP" : "UPDATE",
2294 lock->l_resource->lr_name.name[0],
2295 lock->l_resource->lr_name.name[1],
2296 PFID(&obj->lo_fid));
2297 lmv_object_put(obj);
2306 static void lmv_hash_adjust(__u64 *hash, __u64 hash_adj)
2310 val = le64_to_cpu(*hash);
2312 val += MAX_HASH_SIZE;
2313 if (val != MDS_DIR_END_OFF)
2314 *hash = cpu_to_le64(val - hash_adj);
2317 static __u32 lmv_node_rank(struct obd_export *exp, const struct lu_fid *fid)
2320 struct obd_import *imp;
2323 * XXX: to get nid we assume that underlying obd device is mdc.
2325 imp = class_exp2cliimp(exp);
2326 id = imp->imp_connection->c_self + fid_flatten(fid);
2328 CDEBUG(D_INODE, "Readpage node rank: "LPX64" "DFID" "LPX64" "LPX64"\n",
2329 imp->imp_connection->c_self, PFID(fid), id, id ^ (id >> 32));
2331 return id ^ (id >> 32);
2334 static int lmv_readpage(struct obd_export *exp, struct md_op_data *op_data,
2335 struct page **pages, struct ptlrpc_request **request)
2337 struct obd_device *obd = exp->exp_obd;
2338 struct lmv_obd *lmv = &obd->u.lmv;
2339 struct lmv_object *obj;
2340 struct lu_fid rid = op_data->op_fid1;
2341 __u64 offset = op_data->op_offset;
2351 /* number of pages read, in CFS_PAGE_SIZE */
2353 /* number of pages transferred in LU_PAGE_SIZE */
2355 struct lmv_stripe *los;
2356 struct lmv_tgt_desc *tgt;
2357 struct lu_dirpage *dp;
2358 struct lu_dirent *ent;
2361 rc = lmv_check_connect(obd);
2365 CDEBUG(D_INODE, "READPAGE at "LPX64" from "DFID"\n", offset, PFID(&rid));
2368 * This case handle directory lookup in clustered metadata case (i.e.
2369 * split directory is located on multiple md servers.)
2370 * each server keeps directory entries for certain range of hashes.
2371 * E.g. we have N server and suppose hash range is 0 to MAX_HASH.
2372 * first server will keep records with hashes [ 0 ... MAX_HASH / N - 1],
2373 * second one with hashes [MAX_HASH / N ... 2 * MAX_HASH / N] and
2375 * readdir can simply start reading entries from 0 - N server in
2376 * order but that will not scale well as all client will request dir in
2377 * to server in same order.
2378 * Following algorithm does optimization:
2379 * Instead of doing readdir in 1, 2, ...., N order, client with a
2380 * rank R does readdir in R, R + 1, ..., N, 1, ... R - 1 order.
2381 * (every client has rank R)
2382 * But ll_readdir() expect offset range [0 to MAX_HASH/N) but
2383 * since client ask dir from MDS{R} client has pages with offsets
2384 * [R*MAX_HASH/N ... (R + 1)*MAX_HASH/N] there for we do hash_adj
2385 * on hash values that we get.
2387 obj = lmv_object_find_lock(obd, &rid);
2389 nr = obj->lo_objcount;
2391 seg_size = MAX_HASH_SIZE;
2392 do_div(seg_size, nr);
2393 los = obj->lo_stripes;
2394 tgt = lmv_get_target(lmv, los[0].ls_mds);
2395 rank = lmv_node_rank(tgt->ltd_exp, &rid) % nr;
2397 do_div(tgt_tmp, seg_size);
2398 tgt0_idx = do_div(tgt_tmp, nr);
2399 tgt_idx = (tgt0_idx + rank) % nr;
2401 if (tgt_idx < tgt0_idx)
2405 * Last segment has unusual length due to division
2408 hash_adj = MAX_HASH_SIZE - seg_size * nr;
2412 hash_adj += rank * seg_size;
2414 CDEBUG(D_INODE, "Readpage hash adjustment: %x "LPX64" "
2415 LPX64"/%x -> "LPX64"/%x\n", rank, hash_adj,
2416 offset, tgt0_idx, offset + hash_adj, tgt_idx);
2418 offset = (offset + hash_adj) & MAX_HASH_SIZE;
2419 rid = obj->lo_stripes[tgt_idx].ls_fid;
2420 tgt = lmv_get_target(lmv, los[tgt_idx].ls_mds);
2422 CDEBUG(D_INODE, "Forward to "DFID" with offset %lu i %d\n",
2423 PFID(&rid), (unsigned long)offset, tgt_idx);
2425 tgt = lmv_find_target(lmv, &rid);
2428 GOTO(cleanup, rc = PTR_ERR(tgt));
2430 op_data->op_fid1 = rid;
2431 rc = md_readpage(tgt->ltd_exp, op_data, pages, request);
2435 nrdpgs = ((*request)->rq_bulk->bd_nob_transferred + CFS_PAGE_SIZE - 1)
2437 nlupgs = (*request)->rq_bulk->bd_nob_transferred >> LU_PAGE_SHIFT;
2438 LASSERT(!((*request)->rq_bulk->bd_nob_transferred & ~LU_PAGE_MASK));
2439 LASSERT(nrdpgs > 0 && nrdpgs <= op_data->op_npages);
2441 CDEBUG(D_INODE, "read %d(%d)/%d pages\n", nrdpgs, nlupgs,
2442 op_data->op_npages);
2444 for (i = 0; i < nrdpgs; i++) {
2445 #if CFS_PAGE_SIZE > LU_PAGE_SIZE
2446 struct lu_dirpage *first;
2450 struct lu_dirent *tmp = NULL;
2452 dp = cfs_kmap(pages[i]);
2454 lmv_hash_adjust(&dp->ldp_hash_start, hash_adj);
2455 lmv_hash_adjust(&dp->ldp_hash_end, hash_adj);
2456 LASSERT(le64_to_cpu(dp->ldp_hash_start) <=
2457 op_data->op_offset);
2459 if ((tgt0_idx != nr - 1) &&
2460 (le64_to_cpu(dp->ldp_hash_end) == MDS_DIR_END_OFF))
2462 dp->ldp_hash_end = cpu_to_le32(seg_size *
2465 ""DFID" reset end "LPX64" tgt %d\n",
2467 (__u64)le64_to_cpu(dp->ldp_hash_end),
2472 ent = lu_dirent_start(dp);
2473 #if CFS_PAGE_SIZE > LU_PAGE_SIZE
2475 hash_end = dp->ldp_hash_end;
2479 for (tmp = ent; ent != NULL;
2480 tmp = ent, ent = lu_dirent_next(ent)) {
2482 lmv_hash_adjust(&ent->lde_hash, hash_adj);
2485 #if CFS_PAGE_SIZE > LU_PAGE_SIZE
2486 dp = (struct lu_dirpage *)((char *)dp + LU_PAGE_SIZE);
2487 if (((unsigned long)dp & ~CFS_PAGE_MASK) && nlupgs > 0) {
2488 ent = lu_dirent_start(dp);
2491 lmv_hash_adjust(&dp->ldp_hash_end, hash_adj);
2492 if ((tgt0_idx != nr - 1) &&
2493 (le64_to_cpu(dp->ldp_hash_end) ==
2495 hash_end = cpu_to_le32(seg_size *
2498 ""DFID" reset end "LPX64" tgt %d\n",
2500 (__u64)le64_to_cpu(hash_end),
2504 hash_end = dp->ldp_hash_end;
2505 flags = dp->ldp_flags;
2508 /* enlarge the end entry lde_reclen from 0 to
2509 * first entry of next lu_dirpage, in this way
2510 * several lu_dirpages can be stored into one
2511 * client page on client. */
2512 tmp = ((void *)tmp) +
2513 le16_to_cpu(tmp->lde_reclen);
2515 cpu_to_le16((char *)(dp->ldp_entries) -
2520 first->ldp_hash_end = hash_end;
2521 first->ldp_flags &= ~cpu_to_le32(LDF_COLLIDE);
2522 first->ldp_flags |= flags & cpu_to_le32(LDF_COLLIDE);
2524 SET_BUT_UNUSED(tmp);
2526 cfs_kunmap(pages[i]);
2531 lmv_object_put_unlock(obj);
2535 static int lmv_unlink(struct obd_export *exp, struct md_op_data *op_data,
2536 struct ptlrpc_request **request)
2538 struct obd_device *obd = exp->exp_obd;
2539 struct lmv_obd *lmv = &obd->u.lmv;
2540 struct lmv_tgt_desc *tgt = NULL;
2541 struct lmv_object *obj;
2547 rc = lmv_check_connect(obd);
2554 LASSERT(op_data->op_namelen != 0);
2556 obj = lmv_object_find(obd, &op_data->op_fid1);
2558 sidx = raw_name2idx(obj->lo_hashtype,
2561 op_data->op_namelen);
2562 op_data->op_bias &= ~MDS_CHECK_SPLIT;
2563 op_data->op_fid1 = obj->lo_stripes[sidx].ls_fid;
2564 tgt = lmv_get_target(lmv,
2565 obj->lo_stripes[sidx].ls_mds);
2566 lmv_object_put(obj);
2567 CDEBUG(D_INODE, "UNLINK '%*s' in "DFID" -> %u\n",
2568 op_data->op_namelen, op_data->op_name,
2569 PFID(&op_data->op_fid1), sidx);
2573 tgt = lmv_find_target(lmv, &op_data->op_fid1);
2575 RETURN(PTR_ERR(tgt));
2576 op_data->op_bias |= MDS_CHECK_SPLIT;
2579 op_data->op_fsuid = cfs_curproc_fsuid();
2580 op_data->op_fsgid = cfs_curproc_fsgid();
2581 op_data->op_cap = cfs_curproc_cap_pack();
2584 * If child's fid is given, cancel unused locks for it if it is from
2585 * another export than parent.
2587 * LOOKUP lock for child (fid3) should also be cancelled on parent
2588 * tgt_tgt in mdc_unlink().
2590 op_data->op_flags |= MF_MDC_CANCEL_FID1 | MF_MDC_CANCEL_FID3;
2593 * Cancel FULL locks on child (fid3).
2595 rc = lmv_early_cancel(exp, op_data, tgt->ltd_idx, LCK_EX,
2596 MDS_INODELOCK_FULL, MF_MDC_CANCEL_FID3);
2599 rc = md_unlink(tgt->ltd_exp, op_data, request);
2601 if (rc == -ERESTART) {
2602 LASSERT(*request != NULL);
2603 DEBUG_REQ(D_WARNING|D_RPCTRACE, *request,
2604 "Got -ERESTART during unlink!\n");
2605 ptlrpc_req_finished(*request);
2609 * Directory got split. Time to update local object and repeat
2610 * the request with proper MDS.
2612 rc = lmv_handle_split(exp, &op_data->op_fid1);
2619 static int lmv_precleanup(struct obd_device *obd, enum obd_cleanup_stage stage)
2621 struct lmv_obd *lmv = &obd->u.lmv;
2625 case OBD_CLEANUP_EARLY:
2626 /* XXX: here should be calling obd_precleanup() down to
2629 case OBD_CLEANUP_EXPORTS:
2630 fld_client_proc_fini(&lmv->lmv_fld);
2631 lprocfs_obd_cleanup(obd);
2639 static int lmv_get_info(const struct lu_env *env, struct obd_export *exp,
2640 __u32 keylen, void *key, __u32 *vallen, void *val,
2641 struct lov_stripe_md *lsm)
2643 struct obd_device *obd;
2644 struct lmv_obd *lmv;
2648 obd = class_exp2obd(exp);
2650 CDEBUG(D_IOCTL, "Invalid client cookie "LPX64"\n",
2651 exp->exp_handle.h_cookie);
2656 if (keylen >= strlen("remote_flag") && !strcmp(key, "remote_flag")) {
2657 struct lmv_tgt_desc *tgts;
2660 rc = lmv_check_connect(obd);
2664 LASSERT(*vallen == sizeof(__u32));
2665 for (i = 0, tgts = lmv->tgts; i < lmv->desc.ld_tgt_count;
2669 * All tgts should be connected when this gets called.
2671 if (!tgts || !tgts->ltd_exp) {
2672 CERROR("target not setup?\n");
2676 if (!obd_get_info(env, tgts->ltd_exp, keylen, key,
2681 } else if (KEY_IS(KEY_MAX_EASIZE) || KEY_IS(KEY_CONN_DATA)) {
2682 rc = lmv_check_connect(obd);
2687 * Forwarding this request to first MDS, it should know LOV
2690 rc = obd_get_info(env, lmv->tgts[0].ltd_exp, keylen, key,
2692 if (!rc && KEY_IS(KEY_CONN_DATA)) {
2693 exp->exp_connect_flags =
2694 ((struct obd_connect_data *)val)->ocd_connect_flags;
2697 } else if (KEY_IS(KEY_TGT_COUNT)) {
2698 *((int *)val) = lmv->desc.ld_tgt_count;
2702 CDEBUG(D_IOCTL, "Invalid key\n");
2706 int lmv_set_info_async(const struct lu_env *env, struct obd_export *exp,
2707 obd_count keylen, void *key, obd_count vallen,
2708 void *val, struct ptlrpc_request_set *set)
2710 struct lmv_tgt_desc *tgt;
2711 struct obd_device *obd;
2712 struct lmv_obd *lmv;
2716 obd = class_exp2obd(exp);
2718 CDEBUG(D_IOCTL, "Invalid client cookie "LPX64"\n",
2719 exp->exp_handle.h_cookie);
2724 if (KEY_IS(KEY_READ_ONLY) || KEY_IS(KEY_FLUSH_CTX)) {
2727 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
2728 tgt = &lmv->tgts[i];
2733 err = obd_set_info_async(env, tgt->ltd_exp,
2734 keylen, key, vallen, val, set);
2745 int lmv_packmd(struct obd_export *exp, struct lov_mds_md **lmmp,
2746 struct lov_stripe_md *lsm)
2748 struct obd_device *obd = class_exp2obd(exp);
2749 struct lmv_obd *lmv = &obd->u.lmv;
2750 struct lmv_stripe_md *meap;
2751 struct lmv_stripe_md *lsmp;
2756 mea_size = lmv_get_easize(lmv);
2760 if (*lmmp && !lsm) {
2761 OBD_FREE_LARGE(*lmmp, mea_size);
2766 if (*lmmp == NULL) {
2767 OBD_ALLOC_LARGE(*lmmp, mea_size);
2775 lsmp = (struct lmv_stripe_md *)lsm;
2776 meap = (struct lmv_stripe_md *)*lmmp;
2778 if (lsmp->mea_magic != MEA_MAGIC_LAST_CHAR &&
2779 lsmp->mea_magic != MEA_MAGIC_ALL_CHARS)
2782 meap->mea_magic = cpu_to_le32(lsmp->mea_magic);
2783 meap->mea_count = cpu_to_le32(lsmp->mea_count);
2784 meap->mea_master = cpu_to_le32(lsmp->mea_master);
2786 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
2787 meap->mea_ids[i] = meap->mea_ids[i];
2788 fid_cpu_to_le(&meap->mea_ids[i], &meap->mea_ids[i]);
2794 int lmv_unpackmd(struct obd_export *exp, struct lov_stripe_md **lsmp,
2795 struct lov_mds_md *lmm, int lmm_size)
2797 struct obd_device *obd = class_exp2obd(exp);
2798 struct lmv_stripe_md **tmea = (struct lmv_stripe_md **)lsmp;
2799 struct lmv_stripe_md *mea = (struct lmv_stripe_md *)lmm;
2800 struct lmv_obd *lmv = &obd->u.lmv;
2806 mea_size = lmv_get_easize(lmv);
2810 if (*lsmp != NULL && lmm == NULL) {
2811 OBD_FREE_LARGE(*tmea, mea_size);
2816 LASSERT(mea_size == lmm_size);
2818 OBD_ALLOC_LARGE(*tmea, mea_size);
2825 if (mea->mea_magic == MEA_MAGIC_LAST_CHAR ||
2826 mea->mea_magic == MEA_MAGIC_ALL_CHARS ||
2827 mea->mea_magic == MEA_MAGIC_HASH_SEGMENT)
2829 magic = le32_to_cpu(mea->mea_magic);
2832 * Old mea is not handled here.
2834 CERROR("Old not supportable EA is found\n");
2838 (*tmea)->mea_magic = magic;
2839 (*tmea)->mea_count = le32_to_cpu(mea->mea_count);
2840 (*tmea)->mea_master = le32_to_cpu(mea->mea_master);
2842 for (i = 0; i < (*tmea)->mea_count; i++) {
2843 (*tmea)->mea_ids[i] = mea->mea_ids[i];
2844 fid_le_to_cpu(&(*tmea)->mea_ids[i], &(*tmea)->mea_ids[i]);
2849 static int lmv_cancel_unused(struct obd_export *exp, const struct lu_fid *fid,
2850 ldlm_policy_data_t *policy, ldlm_mode_t mode,
2851 ldlm_cancel_flags_t flags, void *opaque)
2853 struct obd_device *obd = exp->exp_obd;
2854 struct lmv_obd *lmv = &obd->u.lmv;
2860 LASSERT(fid != NULL);
2862 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
2863 if (!lmv->tgts[i].ltd_exp || !lmv->tgts[i].ltd_active)
2866 err = md_cancel_unused(lmv->tgts[i].ltd_exp, fid,
2867 policy, mode, flags, opaque);
2874 int lmv_set_lock_data(struct obd_export *exp, __u64 *lockh, void *data,
2877 struct obd_device *obd = exp->exp_obd;
2878 struct lmv_obd *lmv = &obd->u.lmv;
2882 rc = md_set_lock_data(lmv->tgts[0].ltd_exp, lockh, data, bits);
2886 ldlm_mode_t lmv_lock_match(struct obd_export *exp, int flags,
2887 const struct lu_fid *fid, ldlm_type_t type,
2888 ldlm_policy_data_t *policy, ldlm_mode_t mode,
2889 struct lustre_handle *lockh)
2891 struct obd_device *obd = exp->exp_obd;
2892 struct lmv_obd *lmv = &obd->u.lmv;
2897 CDEBUG(D_INODE, "Lock match for "DFID"\n", PFID(fid));
2900 * With CMD every object can have two locks in different namespaces:
2901 * lookup lock in space of mds storing direntry and update/open lock in
2902 * space of mds storing inode. Thus we check all targets, not only that
2903 * one fid was created in.
2905 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
2906 rc = md_lock_match(lmv->tgts[i].ltd_exp, flags, fid,
2907 type, policy, mode, lockh);
2915 int lmv_get_lustre_md(struct obd_export *exp, struct ptlrpc_request *req,
2916 struct obd_export *dt_exp, struct obd_export *md_exp,
2917 struct lustre_md *md)
2919 struct obd_device *obd = exp->exp_obd;
2920 struct lmv_obd *lmv = &obd->u.lmv;
2923 rc = md_get_lustre_md(lmv->tgts[0].ltd_exp, req, dt_exp, md_exp, md);
2927 int lmv_free_lustre_md(struct obd_export *exp, struct lustre_md *md)
2929 struct obd_device *obd = exp->exp_obd;
2930 struct lmv_obd *lmv = &obd->u.lmv;
2934 obd_free_memmd(exp, (void *)&md->mea);
2935 RETURN(md_free_lustre_md(lmv->tgts[0].ltd_exp, md));
2938 int lmv_set_open_replay_data(struct obd_export *exp,
2939 struct obd_client_handle *och,
2940 struct ptlrpc_request *open_req)
2942 struct obd_device *obd = exp->exp_obd;
2943 struct lmv_obd *lmv = &obd->u.lmv;
2944 struct lmv_tgt_desc *tgt;
2947 tgt = lmv_find_target(lmv, &och->och_fid);
2949 RETURN(PTR_ERR(tgt));
2951 RETURN(md_set_open_replay_data(tgt->ltd_exp, och, open_req));
2954 int lmv_clear_open_replay_data(struct obd_export *exp,
2955 struct obd_client_handle *och)
2957 struct obd_device *obd = exp->exp_obd;
2958 struct lmv_obd *lmv = &obd->u.lmv;
2959 struct lmv_tgt_desc *tgt;
2962 tgt = lmv_find_target(lmv, &och->och_fid);
2964 RETURN(PTR_ERR(tgt));
2966 RETURN(md_clear_open_replay_data(tgt->ltd_exp, och));
2969 static int lmv_get_remote_perm(struct obd_export *exp,
2970 const struct lu_fid *fid,
2971 struct obd_capa *oc, __u32 suppgid,
2972 struct ptlrpc_request **request)
2974 struct obd_device *obd = exp->exp_obd;
2975 struct lmv_obd *lmv = &obd->u.lmv;
2976 struct lmv_tgt_desc *tgt;
2980 rc = lmv_check_connect(obd);
2984 tgt = lmv_find_target(lmv, fid);
2986 RETURN(PTR_ERR(tgt));
2988 rc = md_get_remote_perm(tgt->ltd_exp, fid, oc, suppgid, request);
2992 static int lmv_renew_capa(struct obd_export *exp, struct obd_capa *oc,
2995 struct obd_device *obd = exp->exp_obd;
2996 struct lmv_obd *lmv = &obd->u.lmv;
2997 struct lmv_tgt_desc *tgt;
3001 rc = lmv_check_connect(obd);
3005 tgt = lmv_find_target(lmv, &oc->c_capa.lc_fid);
3007 RETURN(PTR_ERR(tgt));
3009 rc = md_renew_capa(tgt->ltd_exp, oc, cb);
3013 int lmv_unpack_capa(struct obd_export *exp, struct ptlrpc_request *req,
3014 const struct req_msg_field *field, struct obd_capa **oc)
3016 struct obd_device *obd = exp->exp_obd;
3017 struct lmv_obd *lmv = &obd->u.lmv;
3021 rc = md_unpack_capa(lmv->tgts[0].ltd_exp, req, field, oc);
3025 int lmv_intent_getattr_async(struct obd_export *exp,
3026 struct md_enqueue_info *minfo,
3027 struct ldlm_enqueue_info *einfo)
3029 struct md_op_data *op_data = &minfo->mi_data;
3030 struct obd_device *obd = exp->exp_obd;
3031 struct lmv_obd *lmv = &obd->u.lmv;
3032 struct lmv_object *obj;
3033 struct lmv_tgt_desc *tgt = NULL;
3038 rc = lmv_check_connect(obd);
3042 if (op_data->op_namelen) {
3043 obj = lmv_object_find(obd, &op_data->op_fid1);
3045 sidx = raw_name2idx(obj->lo_hashtype, obj->lo_objcount,
3046 (char *)op_data->op_name,
3047 op_data->op_namelen);
3048 op_data->op_fid1 = obj->lo_stripes[sidx].ls_fid;
3049 tgt = lmv_get_target(lmv, obj->lo_stripes[sidx].ls_mds);
3050 lmv_object_put(obj);
3055 tgt = lmv_find_target(lmv, &op_data->op_fid1);
3058 RETURN(PTR_ERR(tgt));
3060 rc = md_intent_getattr_async(tgt->ltd_exp, minfo, einfo);
3064 int lmv_revalidate_lock(struct obd_export *exp, struct lookup_intent *it,
3065 struct lu_fid *fid, __u64 *bits)
3067 struct obd_device *obd = exp->exp_obd;
3068 struct lmv_obd *lmv = &obd->u.lmv;
3069 struct lmv_tgt_desc *tgt;
3073 rc = lmv_check_connect(obd);
3077 tgt = lmv_find_target(lmv, fid);
3079 RETURN(PTR_ERR(tgt));
3081 rc = md_revalidate_lock(tgt->ltd_exp, it, fid, bits);
3086 * For lmv, only need to send request to master MDT, and the master MDT will
3087 * process with other slave MDTs. The only exception is Q_GETOQUOTA for which
3088 * we directly fetch data from the slave MDTs.
3090 int lmv_quotactl(struct obd_device *unused, struct obd_export *exp,
3091 struct obd_quotactl *oqctl)
3093 struct obd_device *obd = class_exp2obd(exp);
3094 struct lmv_obd *lmv = &obd->u.lmv;
3095 struct lmv_tgt_desc *tgt = &lmv->tgts[0];
3097 __u64 curspace, curinodes;
3100 if (!lmv->desc.ld_tgt_count || !tgt->ltd_active) {
3101 CERROR("master lmv inactive\n");
3105 if (oqctl->qc_cmd != Q_GETOQUOTA) {
3106 rc = obd_quotactl(tgt->ltd_exp, oqctl);
3110 curspace = curinodes = 0;
3111 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
3113 tgt = &lmv->tgts[i];
3115 if (tgt->ltd_exp == NULL)
3117 if (!tgt->ltd_active) {
3118 CDEBUG(D_HA, "mdt %d is inactive.\n", i);
3122 err = obd_quotactl(tgt->ltd_exp, oqctl);
3124 CERROR("getquota on mdt %d failed. %d\n", i, err);
3128 curspace += oqctl->qc_dqblk.dqb_curspace;
3129 curinodes += oqctl->qc_dqblk.dqb_curinodes;
3132 oqctl->qc_dqblk.dqb_curspace = curspace;
3133 oqctl->qc_dqblk.dqb_curinodes = curinodes;
3138 int lmv_quotacheck(struct obd_device *unused, struct obd_export *exp,
3139 struct obd_quotactl *oqctl)
3141 struct obd_device *obd = class_exp2obd(exp);
3142 struct lmv_obd *lmv = &obd->u.lmv;
3143 struct lmv_tgt_desc *tgt;
3147 for (i = 0, tgt = lmv->tgts; i < lmv->desc.ld_tgt_count; i++, tgt++) {
3150 if (!tgt->ltd_active) {
3151 CERROR("lmv idx %d inactive\n", i);
3155 err = obd_quotacheck(tgt->ltd_exp, oqctl);
3163 struct obd_ops lmv_obd_ops = {
3164 .o_owner = THIS_MODULE,
3165 .o_setup = lmv_setup,
3166 .o_cleanup = lmv_cleanup,
3167 .o_precleanup = lmv_precleanup,
3168 .o_process_config = lmv_process_config,
3169 .o_connect = lmv_connect,
3170 .o_disconnect = lmv_disconnect,
3171 .o_statfs = lmv_statfs,
3172 .o_get_info = lmv_get_info,
3173 .o_set_info_async = lmv_set_info_async,
3174 .o_packmd = lmv_packmd,
3175 .o_unpackmd = lmv_unpackmd,
3176 .o_notify = lmv_notify,
3177 .o_get_uuid = lmv_get_uuid,
3178 .o_iocontrol = lmv_iocontrol,
3179 .o_fid_delete = lmv_fid_delete,
3180 .o_quotacheck = lmv_quotacheck,
3181 .o_quotactl = lmv_quotactl
3184 struct md_ops lmv_md_ops = {
3185 .m_getstatus = lmv_getstatus,
3186 .m_change_cbdata = lmv_change_cbdata,
3187 .m_find_cbdata = lmv_find_cbdata,
3188 .m_close = lmv_close,
3189 .m_create = lmv_create,
3190 .m_done_writing = lmv_done_writing,
3191 .m_enqueue = lmv_enqueue,
3192 .m_getattr = lmv_getattr,
3193 .m_getxattr = lmv_getxattr,
3194 .m_getattr_name = lmv_getattr_name,
3195 .m_intent_lock = lmv_intent_lock,
3197 .m_rename = lmv_rename,
3198 .m_setattr = lmv_setattr,
3199 .m_setxattr = lmv_setxattr,
3201 .m_readpage = lmv_readpage,
3202 .m_unlink = lmv_unlink,
3203 .m_init_ea_size = lmv_init_ea_size,
3204 .m_cancel_unused = lmv_cancel_unused,
3205 .m_set_lock_data = lmv_set_lock_data,
3206 .m_lock_match = lmv_lock_match,
3207 .m_get_lustre_md = lmv_get_lustre_md,
3208 .m_free_lustre_md = lmv_free_lustre_md,
3209 .m_set_open_replay_data = lmv_set_open_replay_data,
3210 .m_clear_open_replay_data = lmv_clear_open_replay_data,
3211 .m_renew_capa = lmv_renew_capa,
3212 .m_unpack_capa = lmv_unpack_capa,
3213 .m_get_remote_perm = lmv_get_remote_perm,
3214 .m_intent_getattr_async = lmv_intent_getattr_async,
3215 .m_revalidate_lock = lmv_revalidate_lock
3218 int __init lmv_init(void)
3220 struct lprocfs_static_vars lvars;
3223 lmv_object_cache = cfs_mem_cache_create("lmv_objects",
3224 sizeof(struct lmv_object),
3226 if (!lmv_object_cache) {
3227 CERROR("Error allocating lmv objects cache\n");
3231 lprocfs_lmv_init_vars(&lvars);
3233 rc = class_register_type(&lmv_obd_ops, &lmv_md_ops,
3234 lvars.module_vars, LUSTRE_LMV_NAME, NULL);
3236 cfs_mem_cache_destroy(lmv_object_cache);
3242 static void lmv_exit(void)
3244 class_unregister_type(LUSTRE_LMV_NAME);
3246 LASSERTF(cfs_atomic_read(&lmv_object_count) == 0,
3247 "Can't free lmv objects cache, %d object(s) busy\n",
3248 cfs_atomic_read(&lmv_object_count));
3249 cfs_mem_cache_destroy(lmv_object_cache);
3252 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
3253 MODULE_DESCRIPTION("Lustre Logical Metadata Volume OBD driver");
3254 MODULE_LICENSE("GPL");
3256 module_init(lmv_init);
3257 module_exit(lmv_exit);