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 <lustre/lustre_idl.h>
53 #include <obd_support.h>
54 #include <lustre_lib.h>
55 #include <lustre_net.h>
56 #include <obd_class.h>
57 #include <lprocfs_status.h>
58 #include <lustre_lite.h>
59 #include <lustre_fid.h>
60 #include "lmv_internal.h"
63 cfs_mem_cache_t *lmv_object_cache;
64 cfs_atomic_t lmv_object_count = CFS_ATOMIC_INIT(0);
66 static void lmv_activate_target(struct lmv_obd *lmv,
67 struct lmv_tgt_desc *tgt,
70 if (tgt->ltd_active == activate)
73 tgt->ltd_active = activate;
74 lmv->desc.ld_active_tgt_count += (activate ? 1 : -1);
80 * -EINVAL : UUID can't be found in the LMV's target list
81 * -ENOTCONN: The UUID is found, but the target connection is bad (!)
82 * -EBADF : The UUID is found, but the OBD of the wrong type (!)
84 static int lmv_set_mdc_active(struct lmv_obd *lmv, struct obd_uuid *uuid,
87 struct lmv_tgt_desc *tgt;
88 struct obd_device *obd;
93 CDEBUG(D_INFO, "Searching in lmv %p for uuid %s (activate=%d)\n",
94 lmv, uuid->uuid, activate);
96 spin_lock(&lmv->lmv_lock);
97 for (i = 0, tgt = lmv->tgts; i < lmv->desc.ld_tgt_count; i++, tgt++) {
98 if (tgt->ltd_exp == NULL)
101 CDEBUG(D_INFO, "Target idx %d is %s conn "LPX64"\n",
102 i, tgt->ltd_uuid.uuid, tgt->ltd_exp->exp_handle.h_cookie);
104 if (obd_uuid_equals(uuid, &tgt->ltd_uuid))
108 if (i == lmv->desc.ld_tgt_count)
109 GOTO(out_lmv_lock, rc = -EINVAL);
111 obd = class_exp2obd(tgt->ltd_exp);
113 GOTO(out_lmv_lock, rc = -ENOTCONN);
115 CDEBUG(D_INFO, "Found OBD %s=%s device %d (%p) type %s at LMV idx %d\n",
116 obd->obd_name, obd->obd_uuid.uuid, obd->obd_minor, obd,
117 obd->obd_type->typ_name, i);
118 LASSERT(strcmp(obd->obd_type->typ_name, LUSTRE_MDC_NAME) == 0);
120 if (tgt->ltd_active == activate) {
121 CDEBUG(D_INFO, "OBD %p already %sactive!\n", obd,
122 activate ? "" : "in");
123 GOTO(out_lmv_lock, rc);
126 CDEBUG(D_INFO, "Marking OBD %p %sactive\n", obd,
127 activate ? "" : "in");
128 lmv_activate_target(lmv, tgt, activate);
132 spin_unlock(&lmv->lmv_lock);
136 static int lmv_set_mdc_data(struct lmv_obd *lmv, struct obd_uuid *uuid,
137 struct obd_connect_data *data)
139 struct lmv_tgt_desc *tgt;
143 LASSERT(data != NULL);
145 spin_lock(&lmv->lmv_lock);
146 for (i = 0, tgt = lmv->tgts; i < lmv->desc.ld_tgt_count; i++, tgt++) {
147 if (tgt->ltd_exp == NULL)
150 if (obd_uuid_equals(uuid, &tgt->ltd_uuid)) {
151 lmv->datas[tgt->ltd_idx] = *data;
155 spin_unlock(&lmv->lmv_lock);
159 struct obd_uuid *lmv_get_uuid(struct obd_export *exp) {
160 struct obd_device *obd = exp->exp_obd;
161 struct lmv_obd *lmv = &obd->u.lmv;
162 return obd_get_uuid(lmv->tgts[0].ltd_exp);
165 static int lmv_notify(struct obd_device *obd, struct obd_device *watched,
166 enum obd_notify_event ev, void *data)
168 struct obd_connect_data *conn_data;
169 struct lmv_obd *lmv = &obd->u.lmv;
170 struct obd_uuid *uuid;
174 if (strcmp(watched->obd_type->typ_name, LUSTRE_MDC_NAME)) {
175 CERROR("unexpected notification of %s %s!\n",
176 watched->obd_type->typ_name,
181 uuid = &watched->u.cli.cl_target_uuid;
182 if (ev == OBD_NOTIFY_ACTIVE || ev == OBD_NOTIFY_INACTIVE) {
184 * Set MDC as active before notifying the observer, so the
185 * observer can use the MDC normally.
187 rc = lmv_set_mdc_active(lmv, uuid,
188 ev == OBD_NOTIFY_ACTIVE);
190 CERROR("%sactivation of %s failed: %d\n",
191 ev == OBD_NOTIFY_ACTIVE ? "" : "de",
195 } else if (ev == OBD_NOTIFY_OCD) {
196 conn_data = &watched->u.cli.cl_import->imp_connect_data;
199 * Set connect data to desired target, update exp_connect_flags.
201 rc = lmv_set_mdc_data(lmv, uuid, conn_data);
203 CERROR("can't set connect data to target %s, rc %d\n",
209 * XXX: Make sure that ocd_connect_flags from all targets are
210 * the same. Otherwise one of MDTs runs wrong version or
211 * something like this. --umka
213 obd->obd_self_export->exp_connect_flags =
214 conn_data->ocd_connect_flags;
217 else if (ev == OBD_NOTIFY_DISCON) {
219 * For disconnect event, flush fld cache for failout MDS case.
221 fld_client_flush(&lmv->lmv_fld);
225 * Pass the notification up the chain.
227 if (obd->obd_observer)
228 rc = obd_notify(obd->obd_observer, watched, ev, data);
234 * This is fake connect function. Its purpose is to initialize lmv and say
235 * caller that everything is okay. Real connection will be performed later.
237 static int lmv_connect(const struct lu_env *env,
238 struct obd_export **exp, struct obd_device *obd,
239 struct obd_uuid *cluuid, struct obd_connect_data *data,
243 struct proc_dir_entry *lmv_proc_dir;
245 struct lmv_obd *lmv = &obd->u.lmv;
246 struct lustre_handle conn = { 0 };
251 * We don't want to actually do the underlying connections more than
252 * once, so keep track.
255 if (lmv->refcount > 1) {
260 rc = class_connect(&conn, obd, cluuid);
262 CERROR("class_connection() returned %d\n", rc);
266 *exp = class_conn2export(&conn);
267 class_export_get(*exp);
271 lmv->cluuid = *cluuid;
274 lmv->conn_data = *data;
277 lmv_proc_dir = lprocfs_register("target_obds", obd->obd_proc_entry,
279 if (IS_ERR(lmv_proc_dir)) {
280 CERROR("could not register /proc/fs/lustre/%s/%s/target_obds.",
281 obd->obd_type->typ_name, obd->obd_name);
287 * All real clients should perform actual connection right away, because
288 * it is possible, that LMV will not have opportunity to connect targets
289 * and MDC stuff will be called directly, for instance while reading
290 * ../mdc/../kbytesfree procfs file, etc.
292 if (data->ocd_connect_flags & OBD_CONNECT_REAL)
293 rc = lmv_check_connect(obd);
298 lprocfs_remove(&lmv_proc_dir);
305 static void lmv_set_timeouts(struct obd_device *obd)
307 struct lmv_tgt_desc *tgts;
312 if (lmv->server_timeout == 0)
315 if (lmv->connected == 0)
318 for (i = 0, tgts = lmv->tgts; i < lmv->desc.ld_tgt_count; i++, tgts++) {
319 if (tgts->ltd_exp == NULL)
322 obd_set_info_async(NULL, tgts->ltd_exp, sizeof(KEY_INTERMDS),
323 KEY_INTERMDS, 0, NULL, NULL);
327 static int lmv_init_ea_size(struct obd_export *exp, int easize,
328 int def_easize, int cookiesize)
330 struct obd_device *obd = exp->exp_obd;
331 struct lmv_obd *lmv = &obd->u.lmv;
337 if (lmv->max_easize < easize) {
338 lmv->max_easize = easize;
341 if (lmv->max_def_easize < def_easize) {
342 lmv->max_def_easize = def_easize;
345 if (lmv->max_cookiesize < cookiesize) {
346 lmv->max_cookiesize = cookiesize;
352 if (lmv->connected == 0)
355 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
356 if (lmv->tgts[i].ltd_exp == NULL) {
357 CWARN("%s: NULL export for %d\n", obd->obd_name, i);
361 rc = md_init_ea_size(lmv->tgts[i].ltd_exp, easize, def_easize,
364 CERROR("obd_init_ea_size() failed on MDT target %d, "
365 "error %d.\n", i, rc);
372 #define MAX_STRING_SIZE 128
374 int lmv_connect_mdc(struct obd_device *obd, struct lmv_tgt_desc *tgt)
377 struct proc_dir_entry *lmv_proc_dir;
379 struct lmv_obd *lmv = &obd->u.lmv;
380 struct obd_uuid *cluuid = &lmv->cluuid;
381 struct obd_connect_data *mdc_data = NULL;
382 struct obd_uuid lmv_mdc_uuid = { "LMV_MDC_UUID" };
383 struct obd_device *mdc_obd;
384 struct obd_export *mdc_exp;
385 struct lu_fld_target target;
389 mdc_obd = class_find_client_obd(&tgt->ltd_uuid, LUSTRE_MDC_NAME,
392 CERROR("target %s not attached\n", tgt->ltd_uuid.uuid);
396 CDEBUG(D_CONFIG, "connect to %s(%s) - %s, %s FOR %s\n",
397 mdc_obd->obd_name, mdc_obd->obd_uuid.uuid,
398 tgt->ltd_uuid.uuid, obd->obd_uuid.uuid,
401 if (!mdc_obd->obd_set_up) {
402 CERROR("target %s is not set up\n", tgt->ltd_uuid.uuid);
406 rc = obd_connect(NULL, &mdc_exp, mdc_obd, &lmv_mdc_uuid,
407 &lmv->conn_data, NULL);
409 CERROR("target %s connect error %d\n", tgt->ltd_uuid.uuid, rc);
414 * Init fid sequence client for this mdc and add new fld target.
416 rc = obd_fid_init(mdc_exp);
420 target.ft_srv = NULL;
421 target.ft_exp = mdc_exp;
422 target.ft_idx = tgt->ltd_idx;
424 fld_client_add_target(&lmv->lmv_fld, &target);
426 mdc_data = &class_exp2cliimp(mdc_exp)->imp_connect_data;
428 rc = obd_register_observer(mdc_obd, obd);
430 obd_disconnect(mdc_exp);
431 CERROR("target %s register_observer error %d\n",
432 tgt->ltd_uuid.uuid, rc);
436 if (obd->obd_observer) {
438 * Tell the observer about the new target.
440 rc = obd_notify(obd->obd_observer, mdc_exp->exp_obd,
441 OBD_NOTIFY_ACTIVE, (void *)(tgt - lmv->tgts));
443 obd_disconnect(mdc_exp);
449 tgt->ltd_exp = mdc_exp;
450 lmv->desc.ld_active_tgt_count++;
453 * Copy connect data, it may be used later.
455 lmv->datas[tgt->ltd_idx] = *mdc_data;
457 md_init_ea_size(tgt->ltd_exp, lmv->max_easize,
458 lmv->max_def_easize, lmv->max_cookiesize);
460 CDEBUG(D_CONFIG, "Connected to %s(%s) successfully (%d)\n",
461 mdc_obd->obd_name, mdc_obd->obd_uuid.uuid,
462 cfs_atomic_read(&obd->obd_refcount));
465 lmv_proc_dir = lprocfs_srch(obd->obd_proc_entry, "target_obds");
467 struct proc_dir_entry *mdc_symlink;
469 LASSERT(mdc_obd->obd_type != NULL);
470 LASSERT(mdc_obd->obd_type->typ_name != NULL);
471 mdc_symlink = lprocfs_add_symlink(mdc_obd->obd_name,
474 mdc_obd->obd_type->typ_name,
476 if (mdc_symlink == NULL) {
477 CERROR("Could not register LMV target "
478 "/proc/fs/lustre/%s/%s/target_obds/%s.",
479 obd->obd_type->typ_name, obd->obd_name,
481 lprocfs_remove(&lmv_proc_dir);
489 int lmv_add_target(struct obd_device *obd, struct obd_uuid *tgt_uuid)
491 struct lmv_obd *lmv = &obd->u.lmv;
492 struct lmv_tgt_desc *tgt;
496 CDEBUG(D_CONFIG, "Target uuid: %s.\n", tgt_uuid->uuid);
500 if (lmv->desc.ld_active_tgt_count >= LMV_MAX_TGT_COUNT) {
501 lmv_init_unlock(lmv);
502 CERROR("Can't add %s, LMV module compiled for %d MDCs. "
503 "That many MDCs already configured.\n",
504 tgt_uuid->uuid, LMV_MAX_TGT_COUNT);
507 if (lmv->desc.ld_tgt_count == 0) {
508 struct obd_device *mdc_obd;
510 mdc_obd = class_find_client_obd(tgt_uuid, LUSTRE_MDC_NAME,
513 lmv_init_unlock(lmv);
514 CERROR("Target %s not attached\n", tgt_uuid->uuid);
518 spin_lock(&lmv->lmv_lock);
519 tgt = lmv->tgts + lmv->desc.ld_tgt_count++;
520 tgt->ltd_uuid = *tgt_uuid;
521 spin_unlock(&lmv->lmv_lock);
523 if (lmv->connected) {
524 rc = lmv_connect_mdc(obd, tgt);
526 spin_lock(&lmv->lmv_lock);
527 lmv->desc.ld_tgt_count--;
528 memset(tgt, 0, sizeof(*tgt));
529 spin_unlock(&lmv->lmv_lock);
531 int easize = sizeof(struct lmv_stripe_md) +
532 lmv->desc.ld_tgt_count *
533 sizeof(struct lu_fid);
534 lmv_init_ea_size(obd->obd_self_export, easize, 0, 0);
538 lmv_init_unlock(lmv);
542 int lmv_check_connect(struct obd_device *obd)
544 struct lmv_obd *lmv = &obd->u.lmv;
545 struct lmv_tgt_desc *tgt;
555 if (lmv->connected) {
556 lmv_init_unlock(lmv);
560 if (lmv->desc.ld_tgt_count == 0) {
561 lmv_init_unlock(lmv);
562 CERROR("%s: no targets configured.\n", obd->obd_name);
566 CDEBUG(D_CONFIG, "Time to connect %s to %s\n",
567 lmv->cluuid.uuid, obd->obd_name);
569 LASSERT(lmv->tgts != NULL);
571 for (i = 0, tgt = lmv->tgts; i < lmv->desc.ld_tgt_count; i++, tgt++) {
572 rc = lmv_connect_mdc(obd, tgt);
577 lmv_set_timeouts(obd);
578 class_export_put(lmv->exp);
580 easize = lmv_get_easize(lmv);
581 lmv_init_ea_size(obd->obd_self_export, easize, 0, 0);
582 lmv_init_unlock(lmv);
591 --lmv->desc.ld_active_tgt_count;
592 rc2 = obd_disconnect(tgt->ltd_exp);
594 CERROR("LMV target %s disconnect on "
595 "MDC idx %d: error %d\n",
596 tgt->ltd_uuid.uuid, i, rc2);
600 class_disconnect(lmv->exp);
601 lmv_init_unlock(lmv);
605 static int lmv_disconnect_mdc(struct obd_device *obd, struct lmv_tgt_desc *tgt)
608 struct proc_dir_entry *lmv_proc_dir;
610 struct lmv_obd *lmv = &obd->u.lmv;
611 struct obd_device *mdc_obd;
615 LASSERT(tgt != NULL);
616 LASSERT(obd != NULL);
618 mdc_obd = class_exp2obd(tgt->ltd_exp);
621 mdc_obd->obd_force = obd->obd_force;
622 mdc_obd->obd_fail = obd->obd_fail;
623 mdc_obd->obd_no_recov = obd->obd_no_recov;
627 lmv_proc_dir = lprocfs_srch(obd->obd_proc_entry, "target_obds");
629 struct proc_dir_entry *mdc_symlink;
631 mdc_symlink = lprocfs_srch(lmv_proc_dir, mdc_obd->obd_name);
633 lprocfs_remove(&mdc_symlink);
635 CERROR("/proc/fs/lustre/%s/%s/target_obds/%s missing\n",
636 obd->obd_type->typ_name, obd->obd_name,
641 rc = obd_fid_fini(tgt->ltd_exp);
643 CERROR("Can't finanize fids factory\n");
645 CDEBUG(D_INFO, "Disconnected from %s(%s) successfully\n",
646 tgt->ltd_exp->exp_obd->obd_name,
647 tgt->ltd_exp->exp_obd->obd_uuid.uuid);
649 obd_register_observer(tgt->ltd_exp->exp_obd, NULL);
650 rc = obd_disconnect(tgt->ltd_exp);
652 if (tgt->ltd_active) {
653 CERROR("Target %s disconnect error %d\n",
654 tgt->ltd_uuid.uuid, rc);
658 lmv_activate_target(lmv, tgt, 0);
663 static int lmv_disconnect(struct obd_export *exp)
665 struct obd_device *obd = class_exp2obd(exp);
667 struct proc_dir_entry *lmv_proc_dir;
669 struct lmv_obd *lmv = &obd->u.lmv;
678 * Only disconnect the underlying layers on the final disconnect.
681 if (lmv->refcount != 0)
684 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
685 if (lmv->tgts[i].ltd_exp == NULL)
687 lmv_disconnect_mdc(obd, &lmv->tgts[i]);
691 lmv_proc_dir = lprocfs_srch(obd->obd_proc_entry, "target_obds");
693 lprocfs_remove(&lmv_proc_dir);
695 CERROR("/proc/fs/lustre/%s/%s/target_obds missing\n",
696 obd->obd_type->typ_name, obd->obd_name);
702 * This is the case when no real connection is established by
703 * lmv_check_connect().
706 class_export_put(exp);
707 rc = class_disconnect(exp);
708 if (lmv->refcount == 0)
713 static int lmv_iocontrol(unsigned int cmd, struct obd_export *exp,
714 int len, void *karg, void *uarg)
716 struct obd_device *obddev = class_exp2obd(exp);
717 struct lmv_obd *lmv = &obddev->u.lmv;
721 int count = lmv->desc.ld_tgt_count;
728 case IOC_OBD_STATFS: {
729 struct obd_ioctl_data *data = karg;
730 struct obd_device *mdc_obd;
731 struct obd_statfs stat_buf = {0};
734 memcpy(&index, data->ioc_inlbuf2, sizeof(__u32));
735 if ((index >= count))
738 if (!lmv->tgts[index].ltd_active)
741 mdc_obd = class_exp2obd(lmv->tgts[index].ltd_exp);
746 if (cfs_copy_to_user(data->ioc_pbuf2, obd2cli_tgt(mdc_obd),
747 min((int) data->ioc_plen2,
748 (int) sizeof(struct obd_uuid))))
751 rc = obd_statfs(NULL, lmv->tgts[index].ltd_exp, &stat_buf,
752 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
756 if (cfs_copy_to_user(data->ioc_pbuf1, &stat_buf,
757 min((int) data->ioc_plen1,
758 (int) sizeof(stat_buf))))
762 case OBD_IOC_QUOTACTL: {
763 struct if_quotactl *qctl = karg;
764 struct lmv_tgt_desc *tgt = NULL;
765 struct obd_quotactl *oqctl;
767 if (qctl->qc_valid == QC_MDTIDX) {
768 if (qctl->qc_idx < 0 || count <= qctl->qc_idx)
771 tgt = &lmv->tgts[qctl->qc_idx];
774 } else if (qctl->qc_valid == QC_UUID) {
775 for (i = 0; i < count; i++) {
777 if (!obd_uuid_equals(&tgt->ltd_uuid,
781 if (tgt->ltd_exp == NULL)
793 LASSERT(tgt && tgt->ltd_exp);
794 OBD_ALLOC_PTR(oqctl);
798 QCTL_COPY(oqctl, qctl);
799 rc = obd_quotactl(tgt->ltd_exp, oqctl);
801 QCTL_COPY(qctl, oqctl);
802 qctl->qc_valid = QC_MDTIDX;
803 qctl->obd_uuid = tgt->ltd_uuid;
808 case OBD_IOC_CHANGELOG_SEND:
809 case OBD_IOC_CHANGELOG_CLEAR: {
810 struct ioc_changelog *icc = karg;
812 if (icc->icc_mdtindex >= count)
815 rc = obd_iocontrol(cmd, lmv->tgts[icc->icc_mdtindex].ltd_exp,
816 sizeof(*icc), icc, NULL);
819 case LL_IOC_GET_CONNECT_FLAGS: {
820 rc = obd_iocontrol(cmd, lmv->tgts[0].ltd_exp, len, karg, uarg);
825 for (i = 0; i < count; i++) {
827 struct obd_device *mdc_obd;
829 if (lmv->tgts[i].ltd_exp == NULL)
831 /* ll_umount_begin() sets force flag but for lmv, not
832 * mdc. Let's pass it through */
833 mdc_obd = class_exp2obd(lmv->tgts[i].ltd_exp);
834 mdc_obd->obd_force = obddev->obd_force;
835 err = obd_iocontrol(cmd, lmv->tgts[i].ltd_exp, len,
837 if (err == -ENODATA && cmd == OBD_IOC_POLL_QUOTACHECK) {
840 if (lmv->tgts[i].ltd_active) {
841 CERROR("error: iocontrol MDC %s on MDT"
842 "idx %d cmd %x: err = %d\n",
843 lmv->tgts[i].ltd_uuid.uuid,
858 static int lmv_all_chars_policy(int count, const char *name,
869 static int lmv_nid_policy(struct lmv_obd *lmv)
871 struct obd_import *imp;
875 * XXX: To get nid we assume that underlying obd device is mdc.
877 imp = class_exp2cliimp(lmv->tgts[0].ltd_exp);
878 id = imp->imp_connection->c_self ^ (imp->imp_connection->c_self >> 32);
879 return id % lmv->desc.ld_tgt_count;
882 static int lmv_choose_mds(struct lmv_obd *lmv, struct md_op_data *op_data,
883 placement_policy_t placement)
886 case PLACEMENT_CHAR_POLICY:
887 return lmv_all_chars_policy(lmv->desc.ld_tgt_count,
889 op_data->op_namelen);
890 case PLACEMENT_NID_POLICY:
891 return lmv_nid_policy(lmv);
897 CERROR("Unsupported placement policy %x\n", placement);
902 * This is _inode_ placement policy function (not name).
904 static int lmv_placement_policy(struct obd_device *obd,
905 struct md_op_data *op_data,
908 struct lmv_obd *lmv = &obd->u.lmv;
909 struct lmv_object *obj;
913 LASSERT(mds != NULL);
915 if (lmv->desc.ld_tgt_count == 1) {
921 * Allocate new fid on target according to operation type and parent
924 obj = lmv_object_find(obd, &op_data->op_fid1);
925 if (obj != NULL || op_data->op_name == NULL ||
926 op_data->op_opc != LUSTRE_OPC_MKDIR) {
928 * Allocate fid for non-dir or for null name or for case parent
935 * If we have this flag turned on, and we see that
936 * parent dir is split, this means, that caller did not
937 * notice split yet. This is race and we would like to
938 * let caller know that.
940 if (op_data->op_bias & MDS_CHECK_SPLIT)
945 * Allocate new fid on same mds where parent fid is located and
946 * where operation will be sent. In case of split dir, ->op_fid1
947 * and ->op_mds here will contain fid and mds of slave directory
948 * object (assigned by caller).
950 *mds = op_data->op_mds;
954 * Parent directory is not split and we want to create a
955 * directory in it. Let's calculate where to place it according
956 * to operation data @op_data.
958 *mds = lmv_choose_mds(lmv, op_data, lmv->lmv_placement);
963 CERROR("Can't choose MDS, err = %d\n", rc);
965 LASSERT(*mds < lmv->desc.ld_tgt_count);
971 int __lmv_fid_alloc(struct lmv_obd *lmv, struct lu_fid *fid,
974 struct lmv_tgt_desc *tgt;
978 tgt = lmv_get_target(lmv, mds);
981 * New seq alloc and FLD setup should be atomic. Otherwise we may find
982 * on server that seq in new allocated fid is not yet known.
984 mutex_lock(&tgt->ltd_fid_mutex);
986 if (!tgt->ltd_active)
987 GOTO(out, rc = -ENODEV);
990 * Asking underlaying tgt layer to allocate new fid.
992 rc = obd_fid_alloc(tgt->ltd_exp, fid, NULL);
994 LASSERT(fid_is_sane(fid));
1000 mutex_unlock(&tgt->ltd_fid_mutex);
1004 int lmv_fid_alloc(struct obd_export *exp, struct lu_fid *fid,
1005 struct md_op_data *op_data)
1007 struct obd_device *obd = class_exp2obd(exp);
1008 struct lmv_obd *lmv = &obd->u.lmv;
1013 LASSERT(op_data != NULL);
1014 LASSERT(fid != NULL);
1016 rc = lmv_placement_policy(obd, op_data, &mds);
1018 CERROR("Can't get target for allocating fid, "
1023 rc = __lmv_fid_alloc(lmv, fid, mds);
1025 CERROR("Can't alloc new fid, rc %d\n", rc);
1032 static int lmv_setup(struct obd_device *obd, struct lustre_cfg *lcfg)
1034 struct lmv_obd *lmv = &obd->u.lmv;
1035 struct lprocfs_static_vars lvars;
1036 struct lmv_desc *desc;
1041 if (LUSTRE_CFG_BUFLEN(lcfg, 1) < 1) {
1042 CERROR("LMV setup requires a descriptor\n");
1046 desc = (struct lmv_desc *)lustre_cfg_buf(lcfg, 1);
1047 if (sizeof(*desc) > LUSTRE_CFG_BUFLEN(lcfg, 1)) {
1048 CERROR("Lmv descriptor size wrong: %d > %d\n",
1049 (int)sizeof(*desc), LUSTRE_CFG_BUFLEN(lcfg, 1));
1053 lmv->tgts_size = LMV_MAX_TGT_COUNT * sizeof(struct lmv_tgt_desc);
1055 OBD_ALLOC(lmv->tgts, lmv->tgts_size);
1056 if (lmv->tgts == NULL)
1059 for (i = 0; i < LMV_MAX_TGT_COUNT; i++) {
1060 mutex_init(&lmv->tgts[i].ltd_fid_mutex);
1061 lmv->tgts[i].ltd_idx = i;
1064 lmv->datas_size = LMV_MAX_TGT_COUNT * sizeof(struct obd_connect_data);
1066 OBD_ALLOC(lmv->datas, lmv->datas_size);
1067 if (lmv->datas == NULL)
1068 GOTO(out_free_tgts, rc = -ENOMEM);
1070 obd_str2uuid(&lmv->desc.ld_uuid, desc->ld_uuid.uuid);
1071 lmv->desc.ld_tgt_count = 0;
1072 lmv->desc.ld_active_tgt_count = 0;
1073 lmv->max_cookiesize = 0;
1074 lmv->max_def_easize = 0;
1075 lmv->max_easize = 0;
1076 lmv->lmv_placement = PLACEMENT_CHAR_POLICY;
1078 spin_lock_init(&lmv->lmv_lock);
1079 mutex_init(&lmv->init_mutex);
1081 rc = lmv_object_setup(obd);
1083 CERROR("Can't setup LMV object manager, error %d.\n", rc);
1084 GOTO(out_free_datas, rc);
1087 lprocfs_lmv_init_vars(&lvars);
1088 lprocfs_obd_setup(obd, lvars.obd_vars);
1091 rc = lprocfs_seq_create(obd->obd_proc_entry, "target_obd",
1092 0444, &lmv_proc_target_fops, obd);
1094 CWARN("%s: error adding LMV target_obd file: rc = %d\n",
1098 rc = fld_client_init(&lmv->lmv_fld, obd->obd_name,
1099 LUSTRE_CLI_FLD_HASH_DHT);
1101 CERROR("Can't init FLD, err %d\n", rc);
1102 GOTO(out_free_datas, rc);
1108 OBD_FREE(lmv->datas, lmv->datas_size);
1111 OBD_FREE(lmv->tgts, lmv->tgts_size);
1116 static int lmv_cleanup(struct obd_device *obd)
1118 struct lmv_obd *lmv = &obd->u.lmv;
1121 fld_client_fini(&lmv->lmv_fld);
1122 lmv_object_cleanup(obd);
1123 OBD_FREE(lmv->datas, lmv->datas_size);
1124 OBD_FREE(lmv->tgts, lmv->tgts_size);
1129 static int lmv_process_config(struct obd_device *obd, obd_count len, void *buf)
1131 struct lustre_cfg *lcfg = buf;
1132 struct obd_uuid tgt_uuid;
1136 switch(lcfg->lcfg_command) {
1138 if (LUSTRE_CFG_BUFLEN(lcfg, 1) > sizeof(tgt_uuid.uuid))
1139 GOTO(out, rc = -EINVAL);
1141 obd_str2uuid(&tgt_uuid, lustre_cfg_string(lcfg, 1));
1142 rc = lmv_add_target(obd, &tgt_uuid);
1145 CERROR("Unknown command: %d\n", lcfg->lcfg_command);
1146 GOTO(out, rc = -EINVAL);
1153 static int lmv_statfs(const struct lu_env *env, struct obd_export *exp,
1154 struct obd_statfs *osfs, __u64 max_age, __u32 flags)
1156 struct obd_device *obd = class_exp2obd(exp);
1157 struct lmv_obd *lmv = &obd->u.lmv;
1158 struct obd_statfs *temp;
1163 rc = lmv_check_connect(obd);
1167 OBD_ALLOC(temp, sizeof(*temp));
1171 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
1172 if (lmv->tgts[i].ltd_exp == NULL)
1175 rc = obd_statfs(env, lmv->tgts[i].ltd_exp, temp,
1178 CERROR("can't stat MDS #%d (%s), error %d\n", i,
1179 lmv->tgts[i].ltd_exp->exp_obd->obd_name,
1181 GOTO(out_free_temp, rc);
1186 osfs->os_bavail += temp->os_bavail;
1187 osfs->os_blocks += temp->os_blocks;
1188 osfs->os_ffree += temp->os_ffree;
1189 osfs->os_files += temp->os_files;
1195 OBD_FREE(temp, sizeof(*temp));
1199 static int lmv_getstatus(struct obd_export *exp,
1201 struct obd_capa **pc)
1203 struct obd_device *obd = exp->exp_obd;
1204 struct lmv_obd *lmv = &obd->u.lmv;
1208 rc = lmv_check_connect(obd);
1212 rc = md_getstatus(lmv->tgts[0].ltd_exp, fid, pc);
1216 static int lmv_getxattr(struct obd_export *exp, const struct lu_fid *fid,
1217 struct obd_capa *oc, obd_valid valid, const char *name,
1218 const char *input, int input_size, int output_size,
1219 int flags, struct ptlrpc_request **request)
1221 struct obd_device *obd = exp->exp_obd;
1222 struct lmv_obd *lmv = &obd->u.lmv;
1223 struct lmv_tgt_desc *tgt;
1227 rc = lmv_check_connect(obd);
1231 tgt = lmv_find_target(lmv, fid);
1233 RETURN(PTR_ERR(tgt));
1235 rc = md_getxattr(tgt->ltd_exp, fid, oc, valid, name, input,
1236 input_size, output_size, flags, request);
1241 static int lmv_setxattr(struct obd_export *exp, const struct lu_fid *fid,
1242 struct obd_capa *oc, obd_valid valid, const char *name,
1243 const char *input, int input_size, int output_size,
1244 int flags, __u32 suppgid,
1245 struct ptlrpc_request **request)
1247 struct obd_device *obd = exp->exp_obd;
1248 struct lmv_obd *lmv = &obd->u.lmv;
1249 struct lmv_tgt_desc *tgt;
1253 rc = lmv_check_connect(obd);
1257 tgt = lmv_find_target(lmv, fid);
1259 RETURN(PTR_ERR(tgt));
1261 rc = md_setxattr(tgt->ltd_exp, fid, oc, valid, name, input,
1262 input_size, output_size, flags, suppgid,
1268 static int lmv_getattr(struct obd_export *exp, struct md_op_data *op_data,
1269 struct ptlrpc_request **request)
1271 struct obd_device *obd = exp->exp_obd;
1272 struct lmv_obd *lmv = &obd->u.lmv;
1273 struct lmv_tgt_desc *tgt;
1274 struct lmv_object *obj;
1279 rc = lmv_check_connect(obd);
1283 tgt = lmv_find_target(lmv, &op_data->op_fid1);
1285 RETURN(PTR_ERR(tgt));
1287 if (op_data->op_flags & MF_GET_MDT_IDX) {
1288 op_data->op_mds = tgt->ltd_idx;
1292 rc = md_getattr(tgt->ltd_exp, op_data, request);
1296 obj = lmv_object_find_lock(obd, &op_data->op_fid1);
1298 CDEBUG(D_INODE, "GETATTR for "DFID" %s\n", PFID(&op_data->op_fid1),
1299 obj ? "(split)" : "");
1302 * If object is split, then we loop over all the slaves and gather size
1303 * attribute. In ideal world we would have to gather also mds field from
1304 * all slaves, as object is spread over the cluster and this is
1305 * definitely interesting information and it is not good to loss it,
1309 struct mdt_body *body;
1311 if (*request == NULL) {
1312 lmv_object_put(obj);
1316 body = req_capsule_server_get(&(*request)->rq_pill,
1318 LASSERT(body != NULL);
1320 for (i = 0; i < obj->lo_objcount; i++) {
1321 if (lmv->tgts[i].ltd_exp == NULL) {
1322 CWARN("%s: NULL export for %d\n",
1328 * Skip master object.
1330 if (lu_fid_eq(&obj->lo_fid, &obj->lo_stripes[i].ls_fid))
1333 body->size += obj->lo_stripes[i].ls_size;
1336 lmv_object_put_unlock(obj);
1342 static int lmv_change_cbdata(struct obd_export *exp, const struct lu_fid *fid,
1343 ldlm_iterator_t it, void *data)
1345 struct obd_device *obd = exp->exp_obd;
1346 struct lmv_obd *lmv = &obd->u.lmv;
1351 rc = lmv_check_connect(obd);
1355 CDEBUG(D_INODE, "CBDATA for "DFID"\n", PFID(fid));
1358 * With CMD every object can have two locks in different namespaces:
1359 * lookup lock in space of mds storing direntry and update/open lock in
1360 * space of mds storing inode.
1362 for (i = 0; i < lmv->desc.ld_tgt_count; i++)
1363 md_change_cbdata(lmv->tgts[i].ltd_exp, fid, it, data);
1368 static int lmv_find_cbdata(struct obd_export *exp, const struct lu_fid *fid,
1369 ldlm_iterator_t it, void *data)
1371 struct obd_device *obd = exp->exp_obd;
1372 struct lmv_obd *lmv = &obd->u.lmv;
1377 rc = lmv_check_connect(obd);
1381 CDEBUG(D_INODE, "CBDATA for "DFID"\n", PFID(fid));
1384 * With CMD every object can have two locks in different namespaces:
1385 * lookup lock in space of mds storing direntry and update/open lock in
1386 * space of mds storing inode.
1388 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
1389 rc = md_find_cbdata(lmv->tgts[i].ltd_exp, fid, it, data);
1398 static int lmv_close(struct obd_export *exp, struct md_op_data *op_data,
1399 struct md_open_data *mod, struct ptlrpc_request **request)
1401 struct obd_device *obd = exp->exp_obd;
1402 struct lmv_obd *lmv = &obd->u.lmv;
1403 struct lmv_tgt_desc *tgt;
1407 rc = lmv_check_connect(obd);
1411 tgt = lmv_find_target(lmv, &op_data->op_fid1);
1413 RETURN(PTR_ERR(tgt));
1415 CDEBUG(D_INODE, "CLOSE "DFID"\n", PFID(&op_data->op_fid1));
1416 rc = md_close(tgt->ltd_exp, op_data, mod, request);
1421 * Called in the case MDS returns -ERESTART on create on open, what means that
1422 * directory is split and its LMV presentation object has to be updated.
1424 int lmv_handle_split(struct obd_export *exp, const struct lu_fid *fid)
1426 struct obd_device *obd = exp->exp_obd;
1427 struct lmv_obd *lmv = &obd->u.lmv;
1428 struct ptlrpc_request *req = NULL;
1429 struct lmv_tgt_desc *tgt;
1430 struct lmv_object *obj;
1431 struct lustre_md md;
1432 struct md_op_data *op_data;
1439 mealen = lmv_get_easize(lmv);
1441 valid = OBD_MD_FLEASIZE | OBD_MD_FLDIREA | OBD_MD_MEA;
1443 tgt = lmv_find_target(lmv, fid);
1445 RETURN(PTR_ERR(tgt));
1448 * Time to update mea of parent fid.
1451 OBD_ALLOC_PTR(op_data);
1452 if (op_data == NULL)
1455 op_data->op_fid1 = *fid;
1456 op_data->op_mode = mealen;
1457 op_data->op_valid = valid;
1459 rc = md_getattr(tgt->ltd_exp, op_data, &req);
1460 OBD_FREE_PTR(op_data);
1462 CERROR("md_getattr() failed, error %d\n", rc);
1466 rc = md_get_lustre_md(tgt->ltd_exp, req, NULL, exp, &md);
1468 CERROR("md_get_lustre_md() failed, error %d\n", rc);
1473 GOTO(cleanup, rc = -ENODATA);
1475 obj = lmv_object_create(exp, fid, md.mea);
1479 lmv_object_put(obj);
1481 obd_free_memmd(exp, (void *)&md.mea);
1485 ptlrpc_req_finished(req);
1489 int lmv_create(struct obd_export *exp, struct md_op_data *op_data,
1490 const void *data, int datalen, int mode, __u32 uid,
1491 __u32 gid, cfs_cap_t cap_effective, __u64 rdev,
1492 struct ptlrpc_request **request)
1494 struct obd_device *obd = exp->exp_obd;
1495 struct lmv_obd *lmv = &obd->u.lmv;
1496 struct lmv_tgt_desc *tgt;
1497 struct lmv_object *obj;
1503 rc = lmv_check_connect(obd);
1507 if (!lmv->desc.ld_active_tgt_count)
1513 obj = lmv_object_find(obd, &op_data->op_fid1);
1515 sidx = raw_name2idx(obj->lo_hashtype, obj->lo_objcount,
1516 op_data->op_name, op_data->op_namelen);
1517 op_data->op_fid1 = obj->lo_stripes[sidx].ls_fid;
1518 op_data->op_bias &= ~MDS_CHECK_SPLIT;
1519 op_data->op_mds = obj->lo_stripes[sidx].ls_mds;
1520 tgt = lmv_get_target(lmv, op_data->op_mds);
1521 lmv_object_put(obj);
1523 tgt = lmv_find_target(lmv, &op_data->op_fid1);
1524 op_data->op_bias |= MDS_CHECK_SPLIT;
1525 op_data->op_mds = tgt->ltd_idx;
1529 RETURN(PTR_ERR(tgt));
1531 rc = lmv_fid_alloc(exp, &op_data->op_fid2, op_data);
1532 if (rc == -ERESTART)
1537 CDEBUG(D_INODE, "CREATE '%*s' on "DFID" -> mds #%x\n",
1538 op_data->op_namelen, op_data->op_name, PFID(&op_data->op_fid1),
1541 op_data->op_flags |= MF_MDC_CANCEL_FID1;
1542 rc = md_create(tgt->ltd_exp, op_data, data, datalen, mode, uid, gid,
1543 cap_effective, rdev, request);
1545 if (*request == NULL)
1547 CDEBUG(D_INODE, "Created - "DFID"\n", PFID(&op_data->op_fid2));
1548 } else if (rc == -ERESTART) {
1549 LASSERT(*request != NULL);
1550 DEBUG_REQ(D_WARNING|D_RPCTRACE, *request,
1551 "Got -ERESTART during create!\n");
1552 ptlrpc_req_finished(*request);
1556 * Directory got split. Time to update local object and repeat
1557 * the request with proper MDS.
1559 rc = lmv_handle_split(exp, &op_data->op_fid1);
1561 rc = lmv_allocate_slaves(obd, &op_data->op_fid1,
1562 op_data, &op_data->op_fid2);
1571 static int lmv_done_writing(struct obd_export *exp,
1572 struct md_op_data *op_data,
1573 struct md_open_data *mod)
1575 struct obd_device *obd = exp->exp_obd;
1576 struct lmv_obd *lmv = &obd->u.lmv;
1577 struct lmv_tgt_desc *tgt;
1581 rc = lmv_check_connect(obd);
1585 tgt = lmv_find_target(lmv, &op_data->op_fid1);
1587 RETURN(PTR_ERR(tgt));
1589 rc = md_done_writing(tgt->ltd_exp, op_data, mod);
1594 lmv_enqueue_slaves(struct obd_export *exp, struct ldlm_enqueue_info *einfo,
1595 struct lookup_intent *it, struct md_op_data *op_data,
1596 struct lustre_handle *lockh, void *lmm, int lmmsize)
1598 struct obd_device *obd = exp->exp_obd;
1599 struct lmv_obd *lmv = &obd->u.lmv;
1600 struct lmv_stripe_md *mea = op_data->op_mea1;
1601 struct md_op_data *op_data2;
1602 struct lmv_tgt_desc *tgt;
1607 OBD_ALLOC_PTR(op_data2);
1608 if (op_data2 == NULL)
1611 LASSERT(mea != NULL);
1612 for (i = 0; i < mea->mea_count; i++) {
1613 memset(op_data2, 0, sizeof(*op_data2));
1614 op_data2->op_fid1 = mea->mea_ids[i];
1615 op_data2->op_bias = 0;
1617 tgt = lmv_find_target(lmv, &op_data2->op_fid1);
1619 GOTO(cleanup, rc = PTR_ERR(tgt));
1621 if (tgt->ltd_exp == NULL)
1624 rc = md_enqueue(tgt->ltd_exp, einfo, it, op_data2,
1625 lockh + i, lmm, lmmsize, NULL, 0);
1627 CDEBUG(D_INODE, "Take lock on slave "DFID" -> %d/%d\n",
1628 PFID(&mea->mea_ids[i]), rc, it->d.lustre.it_status);
1633 if (it->d.lustre.it_data) {
1634 struct ptlrpc_request *req;
1635 req = (struct ptlrpc_request *)it->d.lustre.it_data;
1636 ptlrpc_req_finished(req);
1639 if (it->d.lustre.it_status)
1640 GOTO(cleanup, rc = it->d.lustre.it_status);
1645 OBD_FREE_PTR(op_data2);
1649 * Drop all taken locks.
1652 if (lockh[i].cookie)
1653 ldlm_lock_decref(lockh + i, einfo->ei_mode);
1654 lockh[i].cookie = 0;
1661 lmv_enqueue_remote(struct obd_export *exp, struct ldlm_enqueue_info *einfo,
1662 struct lookup_intent *it, struct md_op_data *op_data,
1663 struct lustre_handle *lockh, void *lmm, int lmmsize,
1664 int extra_lock_flags)
1666 struct ptlrpc_request *req = it->d.lustre.it_data;
1667 struct obd_device *obd = exp->exp_obd;
1668 struct lmv_obd *lmv = &obd->u.lmv;
1669 struct lustre_handle plock;
1670 struct lmv_tgt_desc *tgt;
1671 struct md_op_data *rdata;
1673 struct mdt_body *body;
1678 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
1679 LASSERT(body != NULL);
1681 if (!(body->valid & OBD_MD_MDS))
1684 CDEBUG(D_INODE, "REMOTE_ENQUEUE '%s' on "DFID" -> "DFID"\n",
1685 LL_IT2STR(it), PFID(&op_data->op_fid1), PFID(&body->fid1));
1688 * We got LOOKUP lock, but we really need attrs.
1690 pmode = it->d.lustre.it_lock_mode;
1691 LASSERT(pmode != 0);
1692 memcpy(&plock, lockh, sizeof(plock));
1693 it->d.lustre.it_lock_mode = 0;
1694 it->d.lustre.it_data = NULL;
1697 it->d.lustre.it_disposition &= ~DISP_ENQ_COMPLETE;
1698 ptlrpc_req_finished(req);
1700 tgt = lmv_find_target(lmv, &fid1);
1702 GOTO(out, rc = PTR_ERR(tgt));
1704 OBD_ALLOC_PTR(rdata);
1706 GOTO(out, rc = -ENOMEM);
1708 rdata->op_fid1 = fid1;
1709 rdata->op_bias = MDS_CROSS_REF;
1711 rc = md_enqueue(tgt->ltd_exp, einfo, it, rdata, lockh,
1712 lmm, lmmsize, NULL, extra_lock_flags);
1713 OBD_FREE_PTR(rdata);
1716 ldlm_lock_decref(&plock, pmode);
1721 lmv_enqueue(struct obd_export *exp, struct ldlm_enqueue_info *einfo,
1722 struct lookup_intent *it, struct md_op_data *op_data,
1723 struct lustre_handle *lockh, void *lmm, int lmmsize,
1724 struct ptlrpc_request **req, __u64 extra_lock_flags)
1726 struct obd_device *obd = exp->exp_obd;
1727 struct lmv_obd *lmv = &obd->u.lmv;
1728 struct lmv_tgt_desc *tgt;
1729 struct lmv_object *obj;
1734 rc = lmv_check_connect(obd);
1738 CDEBUG(D_INODE, "ENQUEUE '%s' on "DFID"\n",
1739 LL_IT2STR(it), PFID(&op_data->op_fid1));
1741 if (op_data->op_mea1 && it && it->it_op == IT_UNLINK) {
1742 rc = lmv_enqueue_slaves(exp, einfo, it, op_data,
1743 lockh, lmm, lmmsize);
1747 obj = lmv_object_find(obd, &op_data->op_fid1);
1748 if (obj && op_data->op_namelen) {
1749 sidx = raw_name2idx(obj->lo_hashtype,
1751 (char *)op_data->op_name,
1752 op_data->op_namelen);
1753 op_data->op_fid1 = obj->lo_stripes[sidx].ls_fid;
1754 tgt = lmv_get_target(lmv, obj->lo_stripes[sidx].ls_mds);
1756 tgt = lmv_find_target(lmv, &op_data->op_fid1);
1759 lmv_object_put(obj);
1762 RETURN(PTR_ERR(tgt));
1764 CDEBUG(D_INODE, "ENQUEUE '%s' on "DFID" -> mds #%d\n",
1765 LL_IT2STR(it), PFID(&op_data->op_fid1), tgt->ltd_idx);
1767 rc = md_enqueue(tgt->ltd_exp, einfo, it, op_data, lockh,
1768 lmm, lmmsize, req, extra_lock_flags);
1770 if (rc == 0 && it && it->it_op == IT_OPEN) {
1771 rc = lmv_enqueue_remote(exp, einfo, it, op_data, lockh,
1772 lmm, lmmsize, extra_lock_flags);
1778 lmv_getattr_name(struct obd_export *exp,struct md_op_data *op_data,
1779 struct ptlrpc_request **request)
1781 struct ptlrpc_request *req = NULL;
1782 struct obd_device *obd = exp->exp_obd;
1783 struct lmv_obd *lmv = &obd->u.lmv;
1784 struct lu_fid rid = op_data->op_fid1;
1785 struct lmv_tgt_desc *tgt;
1786 struct mdt_body *body;
1787 struct lmv_object *obj;
1788 obd_valid valid = op_data->op_valid;
1794 rc = lmv_check_connect(obd);
1801 obj = lmv_object_find(obd, &rid);
1803 sidx = raw_name2idx(obj->lo_hashtype, obj->lo_objcount,
1804 op_data->op_name, op_data->op_namelen);
1805 rid = obj->lo_stripes[sidx].ls_fid;
1806 tgt = lmv_get_target(lmv, obj->lo_stripes[sidx].ls_mds);
1807 op_data->op_mds = obj->lo_stripes[sidx].ls_mds;
1808 valid &= ~OBD_MD_FLCKSPLIT;
1809 lmv_object_put(obj);
1811 tgt = lmv_find_target(lmv, &rid);
1812 valid |= OBD_MD_FLCKSPLIT;
1813 op_data->op_mds = tgt->ltd_idx;
1816 RETURN(PTR_ERR(tgt));
1818 CDEBUG(D_INODE, "GETATTR_NAME for %*s on "DFID" - "DFID" -> mds #%d\n",
1819 op_data->op_namelen, op_data->op_name, PFID(&op_data->op_fid1),
1820 PFID(&rid), tgt->ltd_idx);
1822 op_data->op_valid = valid;
1823 op_data->op_fid1 = rid;
1824 rc = md_getattr_name(tgt->ltd_exp, op_data, request);
1826 body = req_capsule_server_get(&(*request)->rq_pill,
1828 LASSERT(body != NULL);
1830 if (body->valid & OBD_MD_MDS) {
1832 CDEBUG(D_INODE, "Request attrs for "DFID"\n",
1835 tgt = lmv_find_target(lmv, &rid);
1837 ptlrpc_req_finished(*request);
1838 RETURN(PTR_ERR(tgt));
1841 op_data->op_fid1 = rid;
1842 op_data->op_valid |= OBD_MD_FLCROSSREF;
1843 op_data->op_namelen = 0;
1844 op_data->op_name = NULL;
1845 rc = md_getattr_name(tgt->ltd_exp, op_data, &req);
1846 ptlrpc_req_finished(*request);
1849 } else if (rc == -ERESTART) {
1850 LASSERT(*request != NULL);
1851 DEBUG_REQ(D_WARNING|D_RPCTRACE, *request,
1852 "Got -ERESTART during getattr!\n");
1853 ptlrpc_req_finished(*request);
1857 * Directory got split. Time to update local object and repeat
1858 * the request with proper MDS.
1860 rc = lmv_handle_split(exp, &rid);
1867 #define md_op_data_fid(op_data, fl) \
1868 (fl == MF_MDC_CANCEL_FID1 ? &op_data->op_fid1 : \
1869 fl == MF_MDC_CANCEL_FID2 ? &op_data->op_fid2 : \
1870 fl == MF_MDC_CANCEL_FID3 ? &op_data->op_fid3 : \
1871 fl == MF_MDC_CANCEL_FID4 ? &op_data->op_fid4 : \
1874 static int lmv_early_cancel_slaves(struct obd_export *exp,
1875 struct md_op_data *op_data, int op_tgt,
1876 ldlm_mode_t mode, int bits, int flag)
1878 struct obd_device *obd = exp->exp_obd;
1879 struct lmv_obd *lmv = &obd->u.lmv;
1880 ldlm_policy_data_t policy = {{0}};
1881 struct lu_fid *op_fid;
1882 struct lu_fid *st_fid;
1883 struct lmv_tgt_desc *tgt;
1884 struct lmv_object *obj;
1889 op_fid = md_op_data_fid(op_data, flag);
1890 if (!fid_is_sane(op_fid))
1893 obj = lmv_object_find(obd, op_fid);
1897 policy.l_inodebits.bits = bits;
1898 for (i = 0; i < obj->lo_objcount; i++) {
1899 tgt = lmv_get_target(lmv, obj->lo_stripes[i].ls_mds);
1900 st_fid = &obj->lo_stripes[i].ls_fid;
1901 if (op_tgt != tgt->ltd_idx) {
1902 CDEBUG(D_INODE, "EARLY_CANCEL slave "DFID" -> mds #%d\n",
1903 PFID(st_fid), tgt->ltd_idx);
1904 rc = md_cancel_unused(tgt->ltd_exp, st_fid, &policy,
1905 mode, LCF_ASYNC, NULL);
1907 GOTO(out_put_obj, rc);
1910 "EARLY_CANCEL skip operation target %d on "DFID"\n",
1911 op_tgt, PFID(st_fid));
1913 * Do not cancel locks for operation target, they will
1914 * be handled later in underlaying layer when calling
1915 * function we run on behalf of.
1918 op_data->op_flags |= flag;
1923 lmv_object_put(obj);
1927 static int lmv_early_cancel(struct obd_export *exp, struct md_op_data *op_data,
1928 int op_tgt, ldlm_mode_t mode, int bits, int flag)
1930 struct lu_fid *fid = md_op_data_fid(op_data, flag);
1931 struct obd_device *obd = exp->exp_obd;
1932 struct lmv_obd *lmv = &obd->u.lmv;
1933 struct lmv_tgt_desc *tgt;
1934 ldlm_policy_data_t policy = {{0}};
1935 struct lmv_object *obj;
1939 if (!fid_is_sane(fid))
1942 obj = lmv_object_find(obd, fid);
1944 rc = lmv_early_cancel_slaves(exp, op_data, op_tgt, mode,
1946 lmv_object_put(obj);
1948 tgt = lmv_find_target(lmv, fid);
1950 RETURN(PTR_ERR(tgt));
1952 if (tgt->ltd_idx != op_tgt) {
1953 CDEBUG(D_INODE, "EARLY_CANCEL on "DFID"\n", PFID(fid));
1954 policy.l_inodebits.bits = bits;
1955 rc = md_cancel_unused(tgt->ltd_exp, fid, &policy,
1956 mode, LCF_ASYNC, NULL);
1959 "EARLY_CANCEL skip operation target %d on "DFID"\n",
1961 op_data->op_flags |= flag;
1970 * llite passes fid of an target inode in op_data->op_fid1 and id of directory in
1973 static int lmv_link(struct obd_export *exp, struct md_op_data *op_data,
1974 struct ptlrpc_request **request)
1976 struct obd_device *obd = exp->exp_obd;
1977 struct lmv_obd *lmv = &obd->u.lmv;
1978 struct lmv_tgt_desc *tgt;
1979 struct lmv_object *obj;
1986 rc = lmv_check_connect(obd);
1993 LASSERT(op_data->op_namelen != 0);
1995 CDEBUG(D_INODE, "LINK "DFID":%*s to "DFID"\n",
1996 PFID(&op_data->op_fid2), op_data->op_namelen,
1997 op_data->op_name, PFID(&op_data->op_fid1));
1999 obj = lmv_object_find(obd, &op_data->op_fid2);
2001 sidx = raw_name2idx(obj->lo_hashtype,
2004 op_data->op_namelen);
2005 op_data->op_fid2 = obj->lo_stripes[sidx].ls_fid;
2006 mds = obj->lo_stripes[sidx].ls_mds;
2007 lmv_object_put(obj);
2009 rc = lmv_fld_lookup(lmv, &op_data->op_fid2, &mds);
2014 CDEBUG(D_INODE, "Forward to mds #%x ("DFID")\n",
2015 mds, PFID(&op_data->op_fid1));
2017 op_data->op_fsuid = cfs_curproc_fsuid();
2018 op_data->op_fsgid = cfs_curproc_fsgid();
2019 op_data->op_cap = cfs_curproc_cap_pack();
2020 tgt = lmv_get_target(lmv, mds);
2023 * Cancel UPDATE lock on child (fid1).
2025 op_data->op_flags |= MF_MDC_CANCEL_FID2;
2026 rc = lmv_early_cancel(exp, op_data, tgt->ltd_idx, LCK_EX,
2027 MDS_INODELOCK_UPDATE, MF_MDC_CANCEL_FID1);
2029 rc = md_link(tgt->ltd_exp, op_data, request);
2030 if (rc == -ERESTART) {
2031 LASSERT(*request != NULL);
2032 DEBUG_REQ(D_WARNING|D_RPCTRACE, *request,
2033 "Got -ERESTART during link!\n");
2034 ptlrpc_req_finished(*request);
2038 * Directory got split. Time to update local object and repeat
2039 * the request with proper MDS.
2041 rc = lmv_handle_split(exp, &op_data->op_fid2);
2049 static int lmv_rename(struct obd_export *exp, struct md_op_data *op_data,
2050 const char *old, int oldlen, const char *new, int newlen,
2051 struct ptlrpc_request **request)
2053 struct obd_device *obd = exp->exp_obd;
2054 struct lmv_obd *lmv = &obd->u.lmv;
2055 struct lmv_tgt_desc *src_tgt;
2059 struct lmv_object *obj;
2064 LASSERT(oldlen != 0);
2066 CDEBUG(D_INODE, "RENAME %*s in "DFID" to %*s in "DFID"\n",
2067 oldlen, old, PFID(&op_data->op_fid1),
2068 newlen, new, PFID(&op_data->op_fid2));
2070 rc = lmv_check_connect(obd);
2077 obj = lmv_object_find(obd, &op_data->op_fid1);
2079 sidx = raw_name2idx(obj->lo_hashtype, obj->lo_objcount,
2080 (char *)old, oldlen);
2081 op_data->op_fid1 = obj->lo_stripes[sidx].ls_fid;
2082 mds1 = obj->lo_stripes[sidx].ls_mds;
2083 CDEBUG(D_INODE, "Parent obj "DFID"\n", PFID(&op_data->op_fid1));
2084 lmv_object_put(obj);
2086 rc = lmv_fld_lookup(lmv, &op_data->op_fid1, &mds1);
2091 obj = lmv_object_find(obd, &op_data->op_fid2);
2094 * Directory is already split, so we have to forward request to
2097 sidx = raw_name2idx(obj->lo_hashtype, obj->lo_objcount,
2098 (char *)new, newlen);
2100 mds2 = obj->lo_stripes[sidx].ls_mds;
2101 op_data->op_fid2 = obj->lo_stripes[sidx].ls_fid;
2102 CDEBUG(D_INODE, "Parent obj "DFID"\n", PFID(&op_data->op_fid2));
2103 lmv_object_put(obj);
2105 rc = lmv_fld_lookup(lmv, &op_data->op_fid2, &mds2);
2110 op_data->op_fsuid = cfs_curproc_fsuid();
2111 op_data->op_fsgid = cfs_curproc_fsgid();
2112 op_data->op_cap = cfs_curproc_cap_pack();
2114 src_tgt = lmv_get_target(lmv, mds1);
2117 * LOOKUP lock on src child (fid3) should also be cancelled for
2118 * src_tgt in mdc_rename.
2120 op_data->op_flags |= MF_MDC_CANCEL_FID1 | MF_MDC_CANCEL_FID3;
2123 * Cancel UPDATE locks on tgt parent (fid2), tgt_tgt is its
2126 rc = lmv_early_cancel(exp, op_data, src_tgt->ltd_idx,
2127 LCK_EX, MDS_INODELOCK_UPDATE,
2128 MF_MDC_CANCEL_FID2);
2131 * Cancel LOOKUP locks on tgt child (fid4) for parent tgt_tgt.
2134 rc = lmv_early_cancel(exp, op_data, src_tgt->ltd_idx,
2135 LCK_EX, MDS_INODELOCK_LOOKUP,
2136 MF_MDC_CANCEL_FID4);
2140 * Cancel all the locks on tgt child (fid4).
2143 rc = lmv_early_cancel(exp, op_data, src_tgt->ltd_idx,
2144 LCK_EX, MDS_INODELOCK_FULL,
2145 MF_MDC_CANCEL_FID4);
2148 rc = md_rename(src_tgt->ltd_exp, op_data, old, oldlen,
2149 new, newlen, request);
2151 if (rc == -ERESTART) {
2152 LASSERT(*request != NULL);
2153 DEBUG_REQ(D_WARNING|D_RPCTRACE, *request,
2154 "Got -ERESTART during rename!\n");
2155 ptlrpc_req_finished(*request);
2159 * Directory got split. Time to update local object and repeat
2160 * the request with proper MDS.
2162 rc = lmv_handle_split(exp, &op_data->op_fid1);
2169 static int lmv_setattr(struct obd_export *exp, struct md_op_data *op_data,
2170 void *ea, int ealen, void *ea2, int ea2len,
2171 struct ptlrpc_request **request,
2172 struct md_open_data **mod)
2174 struct obd_device *obd = exp->exp_obd;
2175 struct lmv_obd *lmv = &obd->u.lmv;
2176 struct ptlrpc_request *req;
2177 struct lmv_tgt_desc *tgt;
2178 struct lmv_object *obj;
2183 rc = lmv_check_connect(obd);
2187 obj = lmv_object_find(obd, &op_data->op_fid1);
2189 CDEBUG(D_INODE, "SETATTR for "DFID", valid 0x%x%s\n",
2190 PFID(&op_data->op_fid1), op_data->op_attr.ia_valid,
2191 obj ? ", split" : "");
2193 op_data->op_flags |= MF_MDC_CANCEL_FID1;
2195 for (i = 0; i < obj->lo_objcount; i++) {
2196 op_data->op_fid1 = obj->lo_stripes[i].ls_fid;
2198 tgt = lmv_get_target(lmv, obj->lo_stripes[i].ls_mds);
2204 rc = md_setattr(tgt->ltd_exp, op_data, ea, ealen,
2205 ea2, ea2len, &req, mod);
2207 if (lu_fid_eq(&obj->lo_fid, &obj->lo_stripes[i].ls_fid)) {
2209 * This is master object and this request should
2210 * be returned back to llite.
2214 ptlrpc_req_finished(req);
2220 lmv_object_put(obj);
2222 tgt = lmv_find_target(lmv, &op_data->op_fid1);
2224 RETURN(PTR_ERR(tgt));
2226 rc = md_setattr(tgt->ltd_exp, op_data, ea, ealen, ea2,
2227 ea2len, request, mod);
2232 static int lmv_sync(struct obd_export *exp, const struct lu_fid *fid,
2233 struct obd_capa *oc, struct ptlrpc_request **request)
2235 struct obd_device *obd = exp->exp_obd;
2236 struct lmv_obd *lmv = &obd->u.lmv;
2237 struct lmv_tgt_desc *tgt;
2241 rc = lmv_check_connect(obd);
2245 tgt = lmv_find_target(lmv, fid);
2247 RETURN(PTR_ERR(tgt));
2249 rc = md_sync(tgt->ltd_exp, fid, oc, request);
2254 * Main purpose of LMV blocking ast is to remove split directory LMV
2255 * presentation object (struct lmv_object) attached to the lock being revoked.
2257 int lmv_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
2258 void *data, int flag)
2260 struct lustre_handle lockh;
2261 struct lmv_object *obj;
2266 case LDLM_CB_BLOCKING:
2267 ldlm_lock2handle(lock, &lockh);
2268 rc = ldlm_cli_cancel(&lockh);
2270 CDEBUG(D_INODE, "ldlm_cli_cancel: %d\n", rc);
2274 case LDLM_CB_CANCELING:
2276 * Time to drop cached attrs for split directory object
2278 obj = lock->l_ast_data;
2280 CDEBUG(D_INODE, "Cancel %s on "LPU64"/"LPU64
2281 ", master "DFID"\n",
2282 lock->l_resource->lr_name.name[3] == 1 ?
2283 "LOOKUP" : "UPDATE",
2284 lock->l_resource->lr_name.name[0],
2285 lock->l_resource->lr_name.name[1],
2286 PFID(&obj->lo_fid));
2287 lmv_object_put(obj);
2296 static void lmv_hash_adjust(__u64 *hash, __u64 hash_adj)
2300 val = le64_to_cpu(*hash);
2302 val += MAX_HASH_SIZE;
2303 if (val != MDS_DIR_END_OFF)
2304 *hash = cpu_to_le64(val - hash_adj);
2307 static __u32 lmv_node_rank(struct obd_export *exp, const struct lu_fid *fid)
2310 struct obd_import *imp;
2313 * XXX: to get nid we assume that underlying obd device is mdc.
2315 imp = class_exp2cliimp(exp);
2316 id = imp->imp_connection->c_self + fid_flatten(fid);
2318 CDEBUG(D_INODE, "Readpage node rank: "LPX64" "DFID" "LPX64" "LPX64"\n",
2319 imp->imp_connection->c_self, PFID(fid), id, id ^ (id >> 32));
2321 return id ^ (id >> 32);
2324 static int lmv_readpage(struct obd_export *exp, struct md_op_data *op_data,
2325 struct page **pages, struct ptlrpc_request **request)
2327 struct obd_device *obd = exp->exp_obd;
2328 struct lmv_obd *lmv = &obd->u.lmv;
2329 struct lmv_object *obj;
2330 struct lu_fid rid = op_data->op_fid1;
2331 __u64 offset = op_data->op_offset;
2341 /* number of pages read, in CFS_PAGE_SIZE */
2343 /* number of pages transferred in LU_PAGE_SIZE */
2345 struct lmv_stripe *los;
2346 struct lmv_tgt_desc *tgt;
2347 struct lu_dirpage *dp;
2348 struct lu_dirent *ent;
2351 rc = lmv_check_connect(obd);
2355 CDEBUG(D_INODE, "READPAGE at "LPX64" from "DFID"\n", offset, PFID(&rid));
2358 * This case handle directory lookup in clustered metadata case (i.e.
2359 * split directory is located on multiple md servers.)
2360 * each server keeps directory entries for certain range of hashes.
2361 * E.g. we have N server and suppose hash range is 0 to MAX_HASH.
2362 * first server will keep records with hashes [ 0 ... MAX_HASH / N - 1],
2363 * second one with hashes [MAX_HASH / N ... 2 * MAX_HASH / N] and
2365 * readdir can simply start reading entries from 0 - N server in
2366 * order but that will not scale well as all client will request dir in
2367 * to server in same order.
2368 * Following algorithm does optimization:
2369 * Instead of doing readdir in 1, 2, ...., N order, client with a
2370 * rank R does readdir in R, R + 1, ..., N, 1, ... R - 1 order.
2371 * (every client has rank R)
2372 * But ll_readdir() expect offset range [0 to MAX_HASH/N) but
2373 * since client ask dir from MDS{R} client has pages with offsets
2374 * [R*MAX_HASH/N ... (R + 1)*MAX_HASH/N] there for we do hash_adj
2375 * on hash values that we get.
2377 obj = lmv_object_find_lock(obd, &rid);
2379 nr = obj->lo_objcount;
2381 seg_size = MAX_HASH_SIZE;
2382 do_div(seg_size, nr);
2383 los = obj->lo_stripes;
2384 tgt = lmv_get_target(lmv, los[0].ls_mds);
2385 rank = lmv_node_rank(tgt->ltd_exp, &rid) % nr;
2387 do_div(tgt_tmp, seg_size);
2388 tgt0_idx = do_div(tgt_tmp, nr);
2389 tgt_idx = (tgt0_idx + rank) % nr;
2391 if (tgt_idx < tgt0_idx)
2395 * Last segment has unusual length due to division
2398 hash_adj = MAX_HASH_SIZE - seg_size * nr;
2402 hash_adj += rank * seg_size;
2404 CDEBUG(D_INODE, "Readpage hash adjustment: %x "LPX64" "
2405 LPX64"/%x -> "LPX64"/%x\n", rank, hash_adj,
2406 offset, tgt0_idx, offset + hash_adj, tgt_idx);
2408 offset = (offset + hash_adj) & MAX_HASH_SIZE;
2409 rid = obj->lo_stripes[tgt_idx].ls_fid;
2410 tgt = lmv_get_target(lmv, los[tgt_idx].ls_mds);
2412 CDEBUG(D_INODE, "Forward to "DFID" with offset %lu i %d\n",
2413 PFID(&rid), (unsigned long)offset, tgt_idx);
2415 tgt = lmv_find_target(lmv, &rid);
2418 GOTO(cleanup, rc = PTR_ERR(tgt));
2420 op_data->op_fid1 = rid;
2421 rc = md_readpage(tgt->ltd_exp, op_data, pages, request);
2425 nrdpgs = ((*request)->rq_bulk->bd_nob_transferred + CFS_PAGE_SIZE - 1)
2427 nlupgs = (*request)->rq_bulk->bd_nob_transferred >> LU_PAGE_SHIFT;
2428 LASSERT(!((*request)->rq_bulk->bd_nob_transferred & ~LU_PAGE_MASK));
2429 LASSERT(nrdpgs > 0 && nrdpgs <= op_data->op_npages);
2431 CDEBUG(D_INODE, "read %d(%d)/%d pages\n", nrdpgs, nlupgs,
2432 op_data->op_npages);
2434 for (i = 0; i < nrdpgs; i++) {
2435 #if CFS_PAGE_SIZE > LU_PAGE_SIZE
2436 struct lu_dirpage *first;
2440 struct lu_dirent *tmp = NULL;
2442 dp = cfs_kmap(pages[i]);
2444 lmv_hash_adjust(&dp->ldp_hash_start, hash_adj);
2445 lmv_hash_adjust(&dp->ldp_hash_end, hash_adj);
2446 LASSERT(le64_to_cpu(dp->ldp_hash_start) <=
2447 op_data->op_offset);
2449 if ((tgt0_idx != nr - 1) &&
2450 (le64_to_cpu(dp->ldp_hash_end) == MDS_DIR_END_OFF))
2452 dp->ldp_hash_end = cpu_to_le32(seg_size *
2455 ""DFID" reset end "LPX64" tgt %d\n",
2457 (__u64)le64_to_cpu(dp->ldp_hash_end),
2462 ent = lu_dirent_start(dp);
2463 #if CFS_PAGE_SIZE > LU_PAGE_SIZE
2465 hash_end = dp->ldp_hash_end;
2469 for (tmp = ent; ent != NULL;
2470 tmp = ent, ent = lu_dirent_next(ent)) {
2472 lmv_hash_adjust(&ent->lde_hash, hash_adj);
2475 #if CFS_PAGE_SIZE > LU_PAGE_SIZE
2476 dp = (struct lu_dirpage *)((char *)dp + LU_PAGE_SIZE);
2477 if (((unsigned long)dp & ~CFS_PAGE_MASK) && nlupgs > 0) {
2478 ent = lu_dirent_start(dp);
2481 lmv_hash_adjust(&dp->ldp_hash_end, hash_adj);
2482 if ((tgt0_idx != nr - 1) &&
2483 (le64_to_cpu(dp->ldp_hash_end) ==
2485 hash_end = cpu_to_le32(seg_size *
2488 ""DFID" reset end "LPX64" tgt %d\n",
2490 (__u64)le64_to_cpu(hash_end),
2494 hash_end = dp->ldp_hash_end;
2495 flags = dp->ldp_flags;
2498 /* enlarge the end entry lde_reclen from 0 to
2499 * first entry of next lu_dirpage, in this way
2500 * several lu_dirpages can be stored into one
2501 * client page on client. */
2502 tmp = ((void *)tmp) +
2503 le16_to_cpu(tmp->lde_reclen);
2505 cpu_to_le16((char *)(dp->ldp_entries) -
2510 first->ldp_hash_end = hash_end;
2511 first->ldp_flags &= ~cpu_to_le32(LDF_COLLIDE);
2512 first->ldp_flags |= flags & cpu_to_le32(LDF_COLLIDE);
2514 SET_BUT_UNUSED(tmp);
2516 cfs_kunmap(pages[i]);
2521 lmv_object_put_unlock(obj);
2525 static int lmv_unlink(struct obd_export *exp, struct md_op_data *op_data,
2526 struct ptlrpc_request **request)
2528 struct obd_device *obd = exp->exp_obd;
2529 struct lmv_obd *lmv = &obd->u.lmv;
2530 struct lmv_tgt_desc *tgt = NULL;
2531 struct lmv_object *obj;
2537 rc = lmv_check_connect(obd);
2544 LASSERT(op_data->op_namelen != 0);
2546 obj = lmv_object_find(obd, &op_data->op_fid1);
2548 sidx = raw_name2idx(obj->lo_hashtype,
2551 op_data->op_namelen);
2552 op_data->op_bias &= ~MDS_CHECK_SPLIT;
2553 op_data->op_fid1 = obj->lo_stripes[sidx].ls_fid;
2554 tgt = lmv_get_target(lmv,
2555 obj->lo_stripes[sidx].ls_mds);
2556 lmv_object_put(obj);
2557 CDEBUG(D_INODE, "UNLINK '%*s' in "DFID" -> %u\n",
2558 op_data->op_namelen, op_data->op_name,
2559 PFID(&op_data->op_fid1), sidx);
2563 tgt = lmv_find_target(lmv, &op_data->op_fid1);
2565 RETURN(PTR_ERR(tgt));
2566 op_data->op_bias |= MDS_CHECK_SPLIT;
2569 op_data->op_fsuid = cfs_curproc_fsuid();
2570 op_data->op_fsgid = cfs_curproc_fsgid();
2571 op_data->op_cap = cfs_curproc_cap_pack();
2574 * If child's fid is given, cancel unused locks for it if it is from
2575 * another export than parent.
2577 * LOOKUP lock for child (fid3) should also be cancelled on parent
2578 * tgt_tgt in mdc_unlink().
2580 op_data->op_flags |= MF_MDC_CANCEL_FID1 | MF_MDC_CANCEL_FID3;
2583 * Cancel FULL locks on child (fid3).
2585 rc = lmv_early_cancel(exp, op_data, tgt->ltd_idx, LCK_EX,
2586 MDS_INODELOCK_FULL, MF_MDC_CANCEL_FID3);
2589 rc = md_unlink(tgt->ltd_exp, op_data, request);
2591 if (rc == -ERESTART) {
2592 LASSERT(*request != NULL);
2593 DEBUG_REQ(D_WARNING|D_RPCTRACE, *request,
2594 "Got -ERESTART during unlink!\n");
2595 ptlrpc_req_finished(*request);
2599 * Directory got split. Time to update local object and repeat
2600 * the request with proper MDS.
2602 rc = lmv_handle_split(exp, &op_data->op_fid1);
2609 static int lmv_precleanup(struct obd_device *obd, enum obd_cleanup_stage stage)
2611 struct lmv_obd *lmv = &obd->u.lmv;
2615 case OBD_CLEANUP_EARLY:
2616 /* XXX: here should be calling obd_precleanup() down to
2619 case OBD_CLEANUP_EXPORTS:
2620 fld_client_proc_fini(&lmv->lmv_fld);
2621 lprocfs_obd_cleanup(obd);
2629 static int lmv_get_info(const struct lu_env *env, struct obd_export *exp,
2630 __u32 keylen, void *key, __u32 *vallen, void *val,
2631 struct lov_stripe_md *lsm)
2633 struct obd_device *obd;
2634 struct lmv_obd *lmv;
2638 obd = class_exp2obd(exp);
2640 CDEBUG(D_IOCTL, "Invalid client cookie "LPX64"\n",
2641 exp->exp_handle.h_cookie);
2646 if (keylen >= strlen("remote_flag") && !strcmp(key, "remote_flag")) {
2647 struct lmv_tgt_desc *tgts;
2650 rc = lmv_check_connect(obd);
2654 LASSERT(*vallen == sizeof(__u32));
2655 for (i = 0, tgts = lmv->tgts; i < lmv->desc.ld_tgt_count;
2659 * All tgts should be connected when this gets called.
2661 if (!tgts || !tgts->ltd_exp) {
2662 CERROR("target not setup?\n");
2666 if (!obd_get_info(env, tgts->ltd_exp, keylen, key,
2671 } else if (KEY_IS(KEY_MAX_EASIZE) || KEY_IS(KEY_CONN_DATA)) {
2672 rc = lmv_check_connect(obd);
2677 * Forwarding this request to first MDS, it should know LOV
2680 rc = obd_get_info(env, lmv->tgts[0].ltd_exp, keylen, key,
2682 if (!rc && KEY_IS(KEY_CONN_DATA)) {
2683 exp->exp_connect_flags =
2684 ((struct obd_connect_data *)val)->ocd_connect_flags;
2687 } else if (KEY_IS(KEY_TGT_COUNT)) {
2688 *((int *)val) = lmv->desc.ld_tgt_count;
2692 CDEBUG(D_IOCTL, "Invalid key\n");
2696 int lmv_set_info_async(const struct lu_env *env, struct obd_export *exp,
2697 obd_count keylen, void *key, obd_count vallen,
2698 void *val, struct ptlrpc_request_set *set)
2700 struct lmv_tgt_desc *tgt;
2701 struct obd_device *obd;
2702 struct lmv_obd *lmv;
2706 obd = class_exp2obd(exp);
2708 CDEBUG(D_IOCTL, "Invalid client cookie "LPX64"\n",
2709 exp->exp_handle.h_cookie);
2714 if (KEY_IS(KEY_READ_ONLY) || KEY_IS(KEY_FLUSH_CTX)) {
2717 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
2718 tgt = &lmv->tgts[i];
2723 err = obd_set_info_async(env, tgt->ltd_exp,
2724 keylen, key, vallen, val, set);
2735 int lmv_packmd(struct obd_export *exp, struct lov_mds_md **lmmp,
2736 struct lov_stripe_md *lsm)
2738 struct obd_device *obd = class_exp2obd(exp);
2739 struct lmv_obd *lmv = &obd->u.lmv;
2740 struct lmv_stripe_md *meap;
2741 struct lmv_stripe_md *lsmp;
2746 mea_size = lmv_get_easize(lmv);
2750 if (*lmmp && !lsm) {
2751 OBD_FREE_LARGE(*lmmp, mea_size);
2756 if (*lmmp == NULL) {
2757 OBD_ALLOC_LARGE(*lmmp, mea_size);
2765 lsmp = (struct lmv_stripe_md *)lsm;
2766 meap = (struct lmv_stripe_md *)*lmmp;
2768 if (lsmp->mea_magic != MEA_MAGIC_LAST_CHAR &&
2769 lsmp->mea_magic != MEA_MAGIC_ALL_CHARS)
2772 meap->mea_magic = cpu_to_le32(lsmp->mea_magic);
2773 meap->mea_count = cpu_to_le32(lsmp->mea_count);
2774 meap->mea_master = cpu_to_le32(lsmp->mea_master);
2776 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
2777 meap->mea_ids[i] = meap->mea_ids[i];
2778 fid_cpu_to_le(&meap->mea_ids[i], &meap->mea_ids[i]);
2784 int lmv_unpackmd(struct obd_export *exp, struct lov_stripe_md **lsmp,
2785 struct lov_mds_md *lmm, int lmm_size)
2787 struct obd_device *obd = class_exp2obd(exp);
2788 struct lmv_stripe_md **tmea = (struct lmv_stripe_md **)lsmp;
2789 struct lmv_stripe_md *mea = (struct lmv_stripe_md *)lmm;
2790 struct lmv_obd *lmv = &obd->u.lmv;
2796 mea_size = lmv_get_easize(lmv);
2800 if (*lsmp != NULL && lmm == NULL) {
2801 OBD_FREE_LARGE(*tmea, mea_size);
2806 LASSERT(mea_size == lmm_size);
2808 OBD_ALLOC_LARGE(*tmea, mea_size);
2815 if (mea->mea_magic == MEA_MAGIC_LAST_CHAR ||
2816 mea->mea_magic == MEA_MAGIC_ALL_CHARS ||
2817 mea->mea_magic == MEA_MAGIC_HASH_SEGMENT)
2819 magic = le32_to_cpu(mea->mea_magic);
2822 * Old mea is not handled here.
2824 CERROR("Old not supportable EA is found\n");
2828 (*tmea)->mea_magic = magic;
2829 (*tmea)->mea_count = le32_to_cpu(mea->mea_count);
2830 (*tmea)->mea_master = le32_to_cpu(mea->mea_master);
2832 for (i = 0; i < (*tmea)->mea_count; i++) {
2833 (*tmea)->mea_ids[i] = mea->mea_ids[i];
2834 fid_le_to_cpu(&(*tmea)->mea_ids[i], &(*tmea)->mea_ids[i]);
2839 static int lmv_cancel_unused(struct obd_export *exp, const struct lu_fid *fid,
2840 ldlm_policy_data_t *policy, ldlm_mode_t mode,
2841 ldlm_cancel_flags_t flags, void *opaque)
2843 struct obd_device *obd = exp->exp_obd;
2844 struct lmv_obd *lmv = &obd->u.lmv;
2850 LASSERT(fid != NULL);
2852 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
2853 if (!lmv->tgts[i].ltd_exp || !lmv->tgts[i].ltd_active)
2856 err = md_cancel_unused(lmv->tgts[i].ltd_exp, fid,
2857 policy, mode, flags, opaque);
2864 int lmv_set_lock_data(struct obd_export *exp, __u64 *lockh, void *data,
2867 struct obd_device *obd = exp->exp_obd;
2868 struct lmv_obd *lmv = &obd->u.lmv;
2872 rc = md_set_lock_data(lmv->tgts[0].ltd_exp, lockh, data, bits);
2876 ldlm_mode_t lmv_lock_match(struct obd_export *exp, __u64 flags,
2877 const struct lu_fid *fid, ldlm_type_t type,
2878 ldlm_policy_data_t *policy, ldlm_mode_t mode,
2879 struct lustre_handle *lockh)
2881 struct obd_device *obd = exp->exp_obd;
2882 struct lmv_obd *lmv = &obd->u.lmv;
2887 CDEBUG(D_INODE, "Lock match for "DFID"\n", PFID(fid));
2890 * With CMD every object can have two locks in different namespaces:
2891 * lookup lock in space of mds storing direntry and update/open lock in
2892 * space of mds storing inode. Thus we check all targets, not only that
2893 * one fid was created in.
2895 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
2896 rc = md_lock_match(lmv->tgts[i].ltd_exp, flags, fid,
2897 type, policy, mode, lockh);
2905 int lmv_get_lustre_md(struct obd_export *exp, struct ptlrpc_request *req,
2906 struct obd_export *dt_exp, struct obd_export *md_exp,
2907 struct lustre_md *md)
2909 struct obd_device *obd = exp->exp_obd;
2910 struct lmv_obd *lmv = &obd->u.lmv;
2913 rc = md_get_lustre_md(lmv->tgts[0].ltd_exp, req, dt_exp, md_exp, md);
2917 int lmv_free_lustre_md(struct obd_export *exp, struct lustre_md *md)
2919 struct obd_device *obd = exp->exp_obd;
2920 struct lmv_obd *lmv = &obd->u.lmv;
2924 obd_free_memmd(exp, (void *)&md->mea);
2925 RETURN(md_free_lustre_md(lmv->tgts[0].ltd_exp, md));
2928 int lmv_set_open_replay_data(struct obd_export *exp,
2929 struct obd_client_handle *och,
2930 struct ptlrpc_request *open_req)
2932 struct obd_device *obd = exp->exp_obd;
2933 struct lmv_obd *lmv = &obd->u.lmv;
2934 struct lmv_tgt_desc *tgt;
2937 tgt = lmv_find_target(lmv, &och->och_fid);
2939 RETURN(PTR_ERR(tgt));
2941 RETURN(md_set_open_replay_data(tgt->ltd_exp, och, open_req));
2944 int lmv_clear_open_replay_data(struct obd_export *exp,
2945 struct obd_client_handle *och)
2947 struct obd_device *obd = exp->exp_obd;
2948 struct lmv_obd *lmv = &obd->u.lmv;
2949 struct lmv_tgt_desc *tgt;
2952 tgt = lmv_find_target(lmv, &och->och_fid);
2954 RETURN(PTR_ERR(tgt));
2956 RETURN(md_clear_open_replay_data(tgt->ltd_exp, och));
2959 static int lmv_get_remote_perm(struct obd_export *exp,
2960 const struct lu_fid *fid,
2961 struct obd_capa *oc, __u32 suppgid,
2962 struct ptlrpc_request **request)
2964 struct obd_device *obd = exp->exp_obd;
2965 struct lmv_obd *lmv = &obd->u.lmv;
2966 struct lmv_tgt_desc *tgt;
2970 rc = lmv_check_connect(obd);
2974 tgt = lmv_find_target(lmv, fid);
2976 RETURN(PTR_ERR(tgt));
2978 rc = md_get_remote_perm(tgt->ltd_exp, fid, oc, suppgid, request);
2982 static int lmv_renew_capa(struct obd_export *exp, struct obd_capa *oc,
2985 struct obd_device *obd = exp->exp_obd;
2986 struct lmv_obd *lmv = &obd->u.lmv;
2987 struct lmv_tgt_desc *tgt;
2991 rc = lmv_check_connect(obd);
2995 tgt = lmv_find_target(lmv, &oc->c_capa.lc_fid);
2997 RETURN(PTR_ERR(tgt));
2999 rc = md_renew_capa(tgt->ltd_exp, oc, cb);
3003 int lmv_unpack_capa(struct obd_export *exp, struct ptlrpc_request *req,
3004 const struct req_msg_field *field, struct obd_capa **oc)
3006 struct obd_device *obd = exp->exp_obd;
3007 struct lmv_obd *lmv = &obd->u.lmv;
3011 rc = md_unpack_capa(lmv->tgts[0].ltd_exp, req, field, oc);
3015 int lmv_intent_getattr_async(struct obd_export *exp,
3016 struct md_enqueue_info *minfo,
3017 struct ldlm_enqueue_info *einfo)
3019 struct md_op_data *op_data = &minfo->mi_data;
3020 struct obd_device *obd = exp->exp_obd;
3021 struct lmv_obd *lmv = &obd->u.lmv;
3022 struct lmv_object *obj;
3023 struct lmv_tgt_desc *tgt = NULL;
3028 rc = lmv_check_connect(obd);
3032 if (op_data->op_namelen) {
3033 obj = lmv_object_find(obd, &op_data->op_fid1);
3035 sidx = raw_name2idx(obj->lo_hashtype, obj->lo_objcount,
3036 (char *)op_data->op_name,
3037 op_data->op_namelen);
3038 op_data->op_fid1 = obj->lo_stripes[sidx].ls_fid;
3039 tgt = lmv_get_target(lmv, obj->lo_stripes[sidx].ls_mds);
3040 lmv_object_put(obj);
3045 tgt = lmv_find_target(lmv, &op_data->op_fid1);
3048 RETURN(PTR_ERR(tgt));
3050 rc = md_intent_getattr_async(tgt->ltd_exp, minfo, einfo);
3054 int lmv_revalidate_lock(struct obd_export *exp, struct lookup_intent *it,
3055 struct lu_fid *fid, __u64 *bits)
3057 struct obd_device *obd = exp->exp_obd;
3058 struct lmv_obd *lmv = &obd->u.lmv;
3059 struct lmv_tgt_desc *tgt;
3063 rc = lmv_check_connect(obd);
3067 tgt = lmv_find_target(lmv, fid);
3069 RETURN(PTR_ERR(tgt));
3071 rc = md_revalidate_lock(tgt->ltd_exp, it, fid, bits);
3076 * For lmv, only need to send request to master MDT, and the master MDT will
3077 * process with other slave MDTs. The only exception is Q_GETOQUOTA for which
3078 * we directly fetch data from the slave MDTs.
3080 int lmv_quotactl(struct obd_device *unused, struct obd_export *exp,
3081 struct obd_quotactl *oqctl)
3083 struct obd_device *obd = class_exp2obd(exp);
3084 struct lmv_obd *lmv = &obd->u.lmv;
3085 struct lmv_tgt_desc *tgt = &lmv->tgts[0];
3087 __u64 curspace, curinodes;
3090 if (!lmv->desc.ld_tgt_count || !tgt->ltd_active) {
3091 CERROR("master lmv inactive\n");
3095 if (oqctl->qc_cmd != Q_GETOQUOTA) {
3096 rc = obd_quotactl(tgt->ltd_exp, oqctl);
3100 curspace = curinodes = 0;
3101 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
3103 tgt = &lmv->tgts[i];
3105 if (tgt->ltd_exp == NULL)
3107 if (!tgt->ltd_active) {
3108 CDEBUG(D_HA, "mdt %d is inactive.\n", i);
3112 err = obd_quotactl(tgt->ltd_exp, oqctl);
3114 CERROR("getquota on mdt %d failed. %d\n", i, err);
3118 curspace += oqctl->qc_dqblk.dqb_curspace;
3119 curinodes += oqctl->qc_dqblk.dqb_curinodes;
3122 oqctl->qc_dqblk.dqb_curspace = curspace;
3123 oqctl->qc_dqblk.dqb_curinodes = curinodes;
3128 int lmv_quotacheck(struct obd_device *unused, struct obd_export *exp,
3129 struct obd_quotactl *oqctl)
3131 struct obd_device *obd = class_exp2obd(exp);
3132 struct lmv_obd *lmv = &obd->u.lmv;
3133 struct lmv_tgt_desc *tgt;
3137 for (i = 0, tgt = lmv->tgts; i < lmv->desc.ld_tgt_count; i++, tgt++) {
3140 if (!tgt->ltd_active) {
3141 CERROR("lmv idx %d inactive\n", i);
3145 err = obd_quotacheck(tgt->ltd_exp, oqctl);
3153 struct obd_ops lmv_obd_ops = {
3154 .o_owner = THIS_MODULE,
3155 .o_setup = lmv_setup,
3156 .o_cleanup = lmv_cleanup,
3157 .o_precleanup = lmv_precleanup,
3158 .o_process_config = lmv_process_config,
3159 .o_connect = lmv_connect,
3160 .o_disconnect = lmv_disconnect,
3161 .o_statfs = lmv_statfs,
3162 .o_get_info = lmv_get_info,
3163 .o_set_info_async = lmv_set_info_async,
3164 .o_packmd = lmv_packmd,
3165 .o_unpackmd = lmv_unpackmd,
3166 .o_notify = lmv_notify,
3167 .o_get_uuid = lmv_get_uuid,
3168 .o_iocontrol = lmv_iocontrol,
3169 .o_quotacheck = lmv_quotacheck,
3170 .o_quotactl = lmv_quotactl
3173 struct md_ops lmv_md_ops = {
3174 .m_getstatus = lmv_getstatus,
3175 .m_change_cbdata = lmv_change_cbdata,
3176 .m_find_cbdata = lmv_find_cbdata,
3177 .m_close = lmv_close,
3178 .m_create = lmv_create,
3179 .m_done_writing = lmv_done_writing,
3180 .m_enqueue = lmv_enqueue,
3181 .m_getattr = lmv_getattr,
3182 .m_getxattr = lmv_getxattr,
3183 .m_getattr_name = lmv_getattr_name,
3184 .m_intent_lock = lmv_intent_lock,
3186 .m_rename = lmv_rename,
3187 .m_setattr = lmv_setattr,
3188 .m_setxattr = lmv_setxattr,
3190 .m_readpage = lmv_readpage,
3191 .m_unlink = lmv_unlink,
3192 .m_init_ea_size = lmv_init_ea_size,
3193 .m_cancel_unused = lmv_cancel_unused,
3194 .m_set_lock_data = lmv_set_lock_data,
3195 .m_lock_match = lmv_lock_match,
3196 .m_get_lustre_md = lmv_get_lustre_md,
3197 .m_free_lustre_md = lmv_free_lustre_md,
3198 .m_set_open_replay_data = lmv_set_open_replay_data,
3199 .m_clear_open_replay_data = lmv_clear_open_replay_data,
3200 .m_renew_capa = lmv_renew_capa,
3201 .m_unpack_capa = lmv_unpack_capa,
3202 .m_get_remote_perm = lmv_get_remote_perm,
3203 .m_intent_getattr_async = lmv_intent_getattr_async,
3204 .m_revalidate_lock = lmv_revalidate_lock
3207 int __init lmv_init(void)
3209 struct lprocfs_static_vars lvars;
3212 lmv_object_cache = cfs_mem_cache_create("lmv_objects",
3213 sizeof(struct lmv_object),
3215 if (!lmv_object_cache) {
3216 CERROR("Error allocating lmv objects cache\n");
3220 lprocfs_lmv_init_vars(&lvars);
3222 rc = class_register_type(&lmv_obd_ops, &lmv_md_ops,
3223 lvars.module_vars, LUSTRE_LMV_NAME, NULL);
3225 cfs_mem_cache_destroy(lmv_object_cache);
3231 static void lmv_exit(void)
3233 class_unregister_type(LUSTRE_LMV_NAME);
3235 LASSERTF(cfs_atomic_read(&lmv_object_count) == 0,
3236 "Can't free lmv objects cache, %d object(s) busy\n",
3237 cfs_atomic_read(&lmv_object_count));
3238 cfs_mem_cache_destroy(lmv_object_cache);
3241 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
3242 MODULE_DESCRIPTION("Lustre Logical Metadata Volume OBD driver");
3243 MODULE_LICENSE("GPL");
3245 module_init(lmv_init);
3246 module_exit(lmv_exit);