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.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, 2015, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
33 #define DEBUG_SUBSYSTEM S_LMV
34 #include <linux/slab.h>
35 #include <linux/module.h>
36 #include <linux/init.h>
37 #include <linux/pagemap.h>
39 #include <asm/div64.h>
40 #include <linux/seq_file.h>
41 #include <linux/namei.h>
42 #include <linux/uaccess.h>
44 #include <obd_support.h>
45 #include <lustre_net.h>
46 #include <obd_class.h>
47 #include <lustre_lmv.h>
48 #include <lprocfs_status.h>
49 #include <cl_object.h>
50 #include <lustre_fid.h>
51 #include <uapi/linux/lustre/lustre_ioctl.h>
52 #include <lustre_kernelcomm.h>
53 #include "lmv_internal.h"
55 static int lmv_check_connect(struct obd_device *obd);
57 static void lmv_activate_target(struct lmv_obd *lmv,
58 struct lmv_tgt_desc *tgt,
61 if (tgt->ltd_active == activate)
64 tgt->ltd_active = activate;
65 lmv->desc.ld_active_tgt_count += (activate ? 1 : -1);
66 tgt->ltd_exp->exp_obd->obd_inactive = !activate;
72 * -EINVAL : UUID can't be found in the LMV's target list
73 * -ENOTCONN: The UUID is found, but the target connection is bad (!)
74 * -EBADF : The UUID is found, but the OBD of the wrong type (!)
76 static int lmv_set_mdc_active(struct lmv_obd *lmv, const struct obd_uuid *uuid,
79 struct lmv_tgt_desc *tgt = NULL;
80 struct obd_device *obd;
84 CDEBUG(D_INFO, "Searching in lmv %p for uuid %s (activate=%d)\n",
85 lmv, uuid->uuid, activate);
87 spin_lock(&lmv->lmv_lock);
88 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
90 if (!tgt || !tgt->ltd_exp)
93 CDEBUG(D_INFO, "Target idx %d is %s conn %#llx\n", i,
94 tgt->ltd_uuid.uuid, tgt->ltd_exp->exp_handle.h_cookie);
96 if (obd_uuid_equals(uuid, &tgt->ltd_uuid))
100 if (i == lmv->desc.ld_tgt_count) {
105 obd = class_exp2obd(tgt->ltd_exp);
111 CDEBUG(D_INFO, "Found OBD %s=%s device %d (%p) type %s at LMV idx %d\n",
112 obd->obd_name, obd->obd_uuid.uuid, obd->obd_minor, obd,
113 obd->obd_type->typ_name, i);
114 LASSERT(strcmp(obd->obd_type->typ_name, LUSTRE_MDC_NAME) == 0);
116 if (tgt->ltd_active == activate) {
117 CDEBUG(D_INFO, "OBD %p already %sactive!\n", obd,
118 activate ? "" : "in");
122 CDEBUG(D_INFO, "Marking OBD %p %sactive\n", obd,
123 activate ? "" : "in");
124 lmv_activate_target(lmv, tgt, activate);
127 spin_unlock(&lmv->lmv_lock);
131 static struct obd_uuid *lmv_get_uuid(struct obd_export *exp)
133 struct lmv_obd *lmv = &exp->exp_obd->u.lmv;
134 struct lmv_tgt_desc *tgt = lmv->tgts[0];
136 return tgt ? obd_get_uuid(tgt->ltd_exp) : NULL;
139 static int lmv_notify(struct obd_device *obd, struct obd_device *watched,
140 enum obd_notify_event ev, void *data)
142 struct obd_connect_data *conn_data;
143 struct lmv_obd *lmv = &obd->u.lmv;
144 struct obd_uuid *uuid;
147 if (strcmp(watched->obd_type->typ_name, LUSTRE_MDC_NAME)) {
148 CERROR("unexpected notification of %s %s!\n",
149 watched->obd_type->typ_name,
154 uuid = &watched->u.cli.cl_target_uuid;
155 if (ev == OBD_NOTIFY_ACTIVE || ev == OBD_NOTIFY_INACTIVE) {
157 * Set MDC as active before notifying the observer, so the
158 * observer can use the MDC normally.
160 rc = lmv_set_mdc_active(lmv, uuid,
161 ev == OBD_NOTIFY_ACTIVE);
163 CERROR("%sactivation of %s failed: %d\n",
164 ev == OBD_NOTIFY_ACTIVE ? "" : "de",
168 } else if (ev == OBD_NOTIFY_OCD) {
169 conn_data = &watched->u.cli.cl_import->imp_connect_data;
171 * XXX: Make sure that ocd_connect_flags from all targets are
172 * the same. Otherwise one of MDTs runs wrong version or
173 * something like this. --umka
175 obd->obd_self_export->exp_connect_data = *conn_data;
179 * Pass the notification up the chain.
181 if (obd->obd_observer)
182 rc = obd_notify(obd->obd_observer, watched, ev, data);
187 static int lmv_connect(const struct lu_env *env,
188 struct obd_export **pexp, struct obd_device *obd,
189 struct obd_uuid *cluuid, struct obd_connect_data *data,
192 struct lmv_obd *lmv = &obd->u.lmv;
193 struct lustre_handle conn = { 0 };
194 struct obd_export *exp;
197 rc = class_connect(&conn, obd, cluuid);
199 CERROR("class_connection() returned %d\n", rc);
203 exp = class_conn2export(&conn);
206 lmv->cluuid = *cluuid;
207 lmv->conn_data = *data;
209 lmv->lmv_tgts_kobj = kobject_create_and_add("target_obds",
211 rc = lmv_check_connect(obd);
220 if (lmv->lmv_tgts_kobj)
221 kobject_put(lmv->lmv_tgts_kobj);
223 class_disconnect(exp);
228 static int lmv_init_ea_size(struct obd_export *exp, u32 easize, u32 def_easize)
230 struct obd_device *obd = exp->exp_obd;
231 struct lmv_obd *lmv = &obd->u.lmv;
236 if (lmv->max_easize < easize) {
237 lmv->max_easize = easize;
240 if (lmv->max_def_easize < def_easize) {
241 lmv->max_def_easize = def_easize;
248 if (lmv->connected == 0)
251 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
252 struct lmv_tgt_desc *tgt = lmv->tgts[i];
254 if (!tgt || !tgt->ltd_exp || !tgt->ltd_active) {
255 CWARN("%s: NULL export for %d\n", obd->obd_name, i);
259 rc = md_init_ea_size(tgt->ltd_exp, easize, def_easize);
261 CERROR("%s: obd_init_ea_size() failed on MDT target %d: rc = %d\n",
262 obd->obd_name, i, rc);
269 #define MAX_STRING_SIZE 128
271 static int lmv_connect_mdc(struct obd_device *obd, struct lmv_tgt_desc *tgt)
273 struct lmv_obd *lmv = &obd->u.lmv;
274 struct obd_uuid *cluuid = &lmv->cluuid;
275 struct obd_uuid lmv_mdc_uuid = { "LMV_MDC_UUID" };
276 struct obd_device *mdc_obd;
277 struct obd_export *mdc_exp;
278 struct lu_fld_target target;
281 mdc_obd = class_find_client_obd(&tgt->ltd_uuid, LUSTRE_MDC_NAME,
284 CERROR("target %s not attached\n", tgt->ltd_uuid.uuid);
288 CDEBUG(D_CONFIG, "connect to %s(%s) - %s, %s FOR %s\n",
289 mdc_obd->obd_name, mdc_obd->obd_uuid.uuid,
290 tgt->ltd_uuid.uuid, obd->obd_uuid.uuid, cluuid->uuid);
292 if (!mdc_obd->obd_set_up) {
293 CERROR("target %s is not set up\n", tgt->ltd_uuid.uuid);
297 rc = obd_connect(NULL, &mdc_exp, mdc_obd, &lmv_mdc_uuid,
298 &lmv->conn_data, NULL);
300 CERROR("target %s connect error %d\n", tgt->ltd_uuid.uuid, rc);
305 * Init fid sequence client for this mdc and add new fld target.
307 rc = obd_fid_init(mdc_obd, mdc_exp, LUSTRE_SEQ_METADATA);
311 target.ft_srv = NULL;
312 target.ft_exp = mdc_exp;
313 target.ft_idx = tgt->ltd_idx;
315 fld_client_add_target(&lmv->lmv_fld, &target);
317 rc = obd_register_observer(mdc_obd, obd);
319 obd_disconnect(mdc_exp);
320 CERROR("target %s register_observer error %d\n",
321 tgt->ltd_uuid.uuid, rc);
325 if (obd->obd_observer) {
327 * Tell the observer about the new target.
329 rc = obd_notify(obd->obd_observer, mdc_exp->exp_obd,
331 (void *)(tgt - lmv->tgts[0]));
333 obd_disconnect(mdc_exp);
339 tgt->ltd_exp = mdc_exp;
340 lmv->desc.ld_active_tgt_count++;
342 md_init_ea_size(tgt->ltd_exp, lmv->max_easize, lmv->max_def_easize);
344 CDEBUG(D_CONFIG, "Connected to %s(%s) successfully (%d)\n",
345 mdc_obd->obd_name, mdc_obd->obd_uuid.uuid,
346 atomic_read(&obd->obd_refcount));
348 if (lmv->lmv_tgts_kobj)
349 /* Even if we failed to create the link, that's fine */
350 rc = sysfs_create_link(lmv->lmv_tgts_kobj, &mdc_obd->obd_kobj,
355 static void lmv_del_target(struct lmv_obd *lmv, int index)
357 if (!lmv->tgts[index])
360 kfree(lmv->tgts[index]);
361 lmv->tgts[index] = NULL;
364 static int lmv_add_target(struct obd_device *obd, struct obd_uuid *uuidp,
365 __u32 index, int gen)
367 struct lmv_obd *lmv = &obd->u.lmv;
368 struct obd_device *mdc_obd;
369 struct lmv_tgt_desc *tgt;
370 int orig_tgt_count = 0;
373 CDEBUG(D_CONFIG, "Target uuid: %s. index %d\n", uuidp->uuid, index);
375 mdc_obd = class_find_client_obd(uuidp, LUSTRE_MDC_NAME,
378 CERROR("%s: Target %s not attached: rc = %d\n",
379 obd->obd_name, uuidp->uuid, -EINVAL);
383 mutex_lock(&lmv->lmv_init_mutex);
385 if ((index < lmv->tgts_size) && lmv->tgts[index]) {
386 tgt = lmv->tgts[index];
387 CERROR("%s: UUID %s already assigned at LOV target index %d: rc = %d\n",
389 obd_uuid2str(&tgt->ltd_uuid), index, -EEXIST);
390 mutex_unlock(&lmv->lmv_init_mutex);
394 if (index >= lmv->tgts_size) {
395 /* We need to reallocate the lmv target array. */
396 struct lmv_tgt_desc **newtgts, **old = NULL;
400 while (newsize < index + 1)
402 newtgts = kcalloc(newsize, sizeof(*newtgts), GFP_NOFS);
404 mutex_unlock(&lmv->lmv_init_mutex);
408 if (lmv->tgts_size) {
409 memcpy(newtgts, lmv->tgts,
410 sizeof(*newtgts) * lmv->tgts_size);
412 oldsize = lmv->tgts_size;
416 lmv->tgts_size = newsize;
420 CDEBUG(D_CONFIG, "tgts: %p size: %d\n", lmv->tgts,
424 tgt = kzalloc(sizeof(*tgt), GFP_NOFS);
426 mutex_unlock(&lmv->lmv_init_mutex);
430 mutex_init(&tgt->ltd_fid_mutex);
431 tgt->ltd_idx = index;
432 tgt->ltd_uuid = *uuidp;
434 lmv->tgts[index] = tgt;
435 if (index >= lmv->desc.ld_tgt_count) {
436 orig_tgt_count = lmv->desc.ld_tgt_count;
437 lmv->desc.ld_tgt_count = index + 1;
440 if (!lmv->connected) {
441 /* lmv_check_connect() will connect this target. */
442 mutex_unlock(&lmv->lmv_init_mutex);
446 /* Otherwise let's connect it ourselves */
447 mutex_unlock(&lmv->lmv_init_mutex);
448 rc = lmv_connect_mdc(obd, tgt);
450 spin_lock(&lmv->lmv_lock);
451 if (lmv->desc.ld_tgt_count == index + 1)
452 lmv->desc.ld_tgt_count = orig_tgt_count;
453 memset(tgt, 0, sizeof(*tgt));
454 spin_unlock(&lmv->lmv_lock);
456 int easize = sizeof(struct lmv_stripe_md) +
457 lmv->desc.ld_tgt_count * sizeof(struct lu_fid);
458 lmv_init_ea_size(obd->obd_self_export, easize, 0);
464 static int lmv_check_connect(struct obd_device *obd)
466 struct lmv_obd *lmv = &obd->u.lmv;
467 struct lmv_tgt_desc *tgt;
475 mutex_lock(&lmv->lmv_init_mutex);
476 if (lmv->connected) {
477 mutex_unlock(&lmv->lmv_init_mutex);
481 if (lmv->desc.ld_tgt_count == 0) {
482 mutex_unlock(&lmv->lmv_init_mutex);
483 CERROR("%s: no targets configured.\n", obd->obd_name);
490 mutex_unlock(&lmv->lmv_init_mutex);
491 CERROR("%s: no target configured for index 0.\n",
496 CDEBUG(D_CONFIG, "Time to connect %s to %s\n",
497 lmv->cluuid.uuid, obd->obd_name);
499 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
503 rc = lmv_connect_mdc(obd, tgt);
509 easize = lmv_mds_md_size(lmv->desc.ld_tgt_count, LMV_MAGIC);
510 lmv_init_ea_size(obd->obd_self_export, easize, 0);
511 mutex_unlock(&lmv->lmv_init_mutex);
523 --lmv->desc.ld_active_tgt_count;
524 rc2 = obd_disconnect(tgt->ltd_exp);
526 CERROR("LMV target %s disconnect on MDC idx %d: error %d\n",
527 tgt->ltd_uuid.uuid, i, rc2);
532 mutex_unlock(&lmv->lmv_init_mutex);
536 static int lmv_disconnect_mdc(struct obd_device *obd, struct lmv_tgt_desc *tgt)
538 struct lmv_obd *lmv = &obd->u.lmv;
539 struct obd_device *mdc_obd;
542 mdc_obd = class_exp2obd(tgt->ltd_exp);
545 mdc_obd->obd_force = obd->obd_force;
546 mdc_obd->obd_fail = obd->obd_fail;
547 mdc_obd->obd_no_recov = obd->obd_no_recov;
549 if (lmv->lmv_tgts_kobj)
550 sysfs_remove_link(lmv->lmv_tgts_kobj,
554 rc = obd_fid_fini(tgt->ltd_exp->exp_obd);
556 CERROR("Can't finalize fids factory\n");
558 CDEBUG(D_INFO, "Disconnected from %s(%s) successfully\n",
559 tgt->ltd_exp->exp_obd->obd_name,
560 tgt->ltd_exp->exp_obd->obd_uuid.uuid);
562 obd_register_observer(tgt->ltd_exp->exp_obd, NULL);
563 rc = obd_disconnect(tgt->ltd_exp);
565 if (tgt->ltd_active) {
566 CERROR("Target %s disconnect error %d\n",
567 tgt->ltd_uuid.uuid, rc);
571 lmv_activate_target(lmv, tgt, 0);
576 static int lmv_disconnect(struct obd_export *exp)
578 struct obd_device *obd = class_exp2obd(exp);
579 struct lmv_obd *lmv = &obd->u.lmv;
586 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
587 if (!lmv->tgts[i] || !lmv->tgts[i]->ltd_exp)
590 lmv_disconnect_mdc(obd, lmv->tgts[i]);
593 if (lmv->lmv_tgts_kobj)
594 kobject_put(lmv->lmv_tgts_kobj);
598 * This is the case when no real connection is established by
599 * lmv_check_connect().
602 class_export_put(exp);
603 rc = class_disconnect(exp);
608 static int lmv_fid2path(struct obd_export *exp, int len, void *karg,
611 struct obd_device *obddev = class_exp2obd(exp);
612 struct lmv_obd *lmv = &obddev->u.lmv;
613 struct getinfo_fid2path *gf;
614 struct lmv_tgt_desc *tgt;
615 struct getinfo_fid2path *remote_gf = NULL;
616 int remote_gf_size = 0;
620 tgt = lmv_find_target(lmv, &gf->gf_fid);
625 rc = obd_iocontrol(OBD_IOC_FID2PATH, tgt->ltd_exp, len, gf, uarg);
626 if (rc != 0 && rc != -EREMOTE)
629 /* If remote_gf != NULL, it means just building the
630 * path on the remote MDT, copy this path segment to gf
633 struct getinfo_fid2path *ori_gf;
637 if (strlen(ori_gf->gf_path) + 1 +
638 strlen(gf->gf_path) + 1 > ori_gf->gf_pathlen) {
643 ptr = ori_gf->gf_path;
645 memmove(ptr + strlen(gf->gf_path) + 1, ptr,
646 strlen(ori_gf->gf_path));
648 strcpy(ptr, gf->gf_path);
649 ptr += strlen(gf->gf_path);
653 CDEBUG(D_INFO, "%s: get path %s " DFID " rec: %llu ln: %u\n",
654 tgt->ltd_exp->exp_obd->obd_name,
655 gf->gf_path, PFID(&gf->gf_fid), gf->gf_recno,
661 /* sigh, has to go to another MDT to do path building further */
663 remote_gf_size = sizeof(*remote_gf) + PATH_MAX;
664 remote_gf = kzalloc(remote_gf_size, GFP_NOFS);
669 remote_gf->gf_pathlen = PATH_MAX;
672 if (!fid_is_sane(&gf->gf_fid)) {
673 CERROR("%s: invalid FID " DFID ": rc = %d\n",
674 tgt->ltd_exp->exp_obd->obd_name,
675 PFID(&gf->gf_fid), -EINVAL);
680 tgt = lmv_find_target(lmv, &gf->gf_fid);
686 remote_gf->gf_fid = gf->gf_fid;
687 remote_gf->gf_recno = -1;
688 remote_gf->gf_linkno = -1;
689 memset(remote_gf->gf_path, 0, remote_gf->gf_pathlen);
691 goto repeat_fid2path;
698 static int lmv_hsm_req_count(struct lmv_obd *lmv,
699 const struct hsm_user_request *hur,
700 const struct lmv_tgt_desc *tgt_mds)
703 struct lmv_tgt_desc *curr_tgt;
705 /* count how many requests must be sent to the given target */
706 for (i = 0; i < hur->hur_request.hr_itemcount; i++) {
707 curr_tgt = lmv_find_target(lmv, &hur->hur_user_item[i].hui_fid);
708 if (IS_ERR(curr_tgt))
709 return PTR_ERR(curr_tgt);
710 if (obd_uuid_equals(&curr_tgt->ltd_uuid, &tgt_mds->ltd_uuid))
716 static int lmv_hsm_req_build(struct lmv_obd *lmv,
717 struct hsm_user_request *hur_in,
718 const struct lmv_tgt_desc *tgt_mds,
719 struct hsm_user_request *hur_out)
722 struct lmv_tgt_desc *curr_tgt;
724 /* build the hsm_user_request for the given target */
725 hur_out->hur_request = hur_in->hur_request;
727 for (i = 0; i < hur_in->hur_request.hr_itemcount; i++) {
728 curr_tgt = lmv_find_target(lmv,
729 &hur_in->hur_user_item[i].hui_fid);
730 if (IS_ERR(curr_tgt))
731 return PTR_ERR(curr_tgt);
732 if (obd_uuid_equals(&curr_tgt->ltd_uuid, &tgt_mds->ltd_uuid)) {
733 hur_out->hur_user_item[nr_out] =
734 hur_in->hur_user_item[i];
738 hur_out->hur_request.hr_itemcount = nr_out;
739 memcpy(hur_data(hur_out), hur_data(hur_in),
740 hur_in->hur_request.hr_data_len);
745 static int lmv_hsm_ct_unregister(struct lmv_obd *lmv, unsigned int cmd, int len,
746 struct lustre_kernelcomm *lk,
751 /* unregister request (call from llapi_hsm_copytool_fini) */
752 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
753 struct lmv_tgt_desc *tgt = lmv->tgts[i];
755 if (!tgt || !tgt->ltd_exp)
758 /* best effort: try to clean as much as possible
759 * (continue on error)
761 obd_iocontrol(cmd, lmv->tgts[i]->ltd_exp, len, lk, uarg);
764 /* Whatever the result, remove copytool from kuc groups.
765 * Unreached coordinators will get EPIPE on next requests
766 * and will unregister automatically.
768 return libcfs_kkuc_group_rem(lk->lk_uid, lk->lk_group);
771 static int lmv_hsm_ct_register(struct lmv_obd *lmv, unsigned int cmd, int len,
772 struct lustre_kernelcomm *lk, void __user *uarg)
777 bool any_set = false;
778 struct kkuc_ct_data kcd = { 0 };
780 /* All or nothing: try to register to all MDS.
781 * In case of failure, unregister from previous MDS,
782 * except if it because of inactive target.
784 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
785 struct lmv_tgt_desc *tgt = lmv->tgts[i];
787 if (!tgt || !tgt->ltd_exp)
790 err = obd_iocontrol(cmd, tgt->ltd_exp, len, lk, uarg);
792 if (tgt->ltd_active) {
793 /* permanent error */
794 CERROR("error: iocontrol MDC %s on MDTidx %d cmd %x: err = %d\n",
795 tgt->ltd_uuid.uuid, i, cmd, err);
797 lk->lk_flags |= LK_FLG_STOP;
798 /* unregister from previous MDS */
799 for (j = 0; j < i; j++) {
802 if (!tgt || !tgt->ltd_exp)
804 obd_iocontrol(cmd, tgt->ltd_exp, len,
809 /* else: transient error.
810 * kuc will register to the missing MDT when it is back
818 /* no registration done: return error */
821 /* at least one registration done, with no failure */
822 filp = fget(lk->lk_wfd);
826 kcd.kcd_magic = KKUC_CT_DATA_MAGIC;
827 kcd.kcd_uuid = lmv->cluuid;
828 kcd.kcd_archive = lk->lk_data;
830 rc = libcfs_kkuc_group_add(filp, lk->lk_uid, lk->lk_group,
838 static int lmv_iocontrol(unsigned int cmd, struct obd_export *exp,
839 int len, void *karg, void __user *uarg)
841 struct obd_device *obddev = class_exp2obd(exp);
842 struct lmv_obd *lmv = &obddev->u.lmv;
843 struct lmv_tgt_desc *tgt = NULL;
847 u32 count = lmv->desc.ld_tgt_count;
853 case IOC_OBD_STATFS: {
854 struct obd_ioctl_data *data = karg;
855 struct obd_device *mdc_obd;
856 struct obd_statfs stat_buf = {0};
859 memcpy(&index, data->ioc_inlbuf2, sizeof(__u32));
863 tgt = lmv->tgts[index];
864 if (!tgt || !tgt->ltd_active)
867 mdc_obd = class_exp2obd(tgt->ltd_exp);
872 if (copy_to_user(data->ioc_pbuf2, obd2cli_tgt(mdc_obd),
873 min((int)data->ioc_plen2,
874 (int)sizeof(struct obd_uuid))))
877 rc = obd_statfs(NULL, tgt->ltd_exp, &stat_buf,
878 cfs_time_shift_64(-OBD_STATFS_CACHE_SECONDS),
882 if (copy_to_user(data->ioc_pbuf1, &stat_buf,
883 min((int)data->ioc_plen1,
884 (int)sizeof(stat_buf))))
888 case OBD_IOC_QUOTACTL: {
889 struct if_quotactl *qctl = karg;
890 struct obd_quotactl *oqctl;
892 if (qctl->qc_valid == QC_MDTIDX) {
893 if (count <= qctl->qc_idx)
896 tgt = lmv->tgts[qctl->qc_idx];
897 if (!tgt || !tgt->ltd_exp)
899 } else if (qctl->qc_valid == QC_UUID) {
900 for (i = 0; i < count; i++) {
904 if (!obd_uuid_equals(&tgt->ltd_uuid,
920 LASSERT(tgt && tgt->ltd_exp);
921 oqctl = kzalloc(sizeof(*oqctl), GFP_NOFS);
925 QCTL_COPY(oqctl, qctl);
926 rc = obd_quotactl(tgt->ltd_exp, oqctl);
928 QCTL_COPY(qctl, oqctl);
929 qctl->qc_valid = QC_MDTIDX;
930 qctl->obd_uuid = tgt->ltd_uuid;
935 case OBD_IOC_CHANGELOG_SEND:
936 case OBD_IOC_CHANGELOG_CLEAR: {
937 struct ioc_changelog *icc = karg;
939 if (icc->icc_mdtindex >= count)
942 tgt = lmv->tgts[icc->icc_mdtindex];
943 if (!tgt || !tgt->ltd_exp || !tgt->ltd_active)
945 rc = obd_iocontrol(cmd, tgt->ltd_exp, sizeof(*icc), icc, NULL);
948 case LL_IOC_GET_CONNECT_FLAGS: {
951 if (!tgt || !tgt->ltd_exp)
953 rc = obd_iocontrol(cmd, tgt->ltd_exp, len, karg, uarg);
956 case LL_IOC_FID2MDTIDX: {
957 struct lu_fid *fid = karg;
960 rc = lmv_fld_lookup(lmv, fid, &mdt_index);
965 * Note: this is from llite(see ll_dir_ioctl()), @uarg does not
966 * point to user space memory for FID2MDTIDX.
968 *(__u32 *)uarg = mdt_index;
971 case OBD_IOC_FID2PATH: {
972 rc = lmv_fid2path(exp, len, karg, uarg);
975 case LL_IOC_HSM_STATE_GET:
976 case LL_IOC_HSM_STATE_SET:
977 case LL_IOC_HSM_ACTION: {
978 struct md_op_data *op_data = karg;
980 tgt = lmv_find_target(lmv, &op_data->op_fid1);
987 rc = obd_iocontrol(cmd, tgt->ltd_exp, len, karg, uarg);
990 case LL_IOC_HSM_PROGRESS: {
991 const struct hsm_progress_kernel *hpk = karg;
993 tgt = lmv_find_target(lmv, &hpk->hpk_fid);
996 rc = obd_iocontrol(cmd, tgt->ltd_exp, len, karg, uarg);
999 case LL_IOC_HSM_REQUEST: {
1000 struct hsm_user_request *hur = karg;
1001 unsigned int reqcount = hur->hur_request.hr_itemcount;
1006 /* if the request is about a single fid
1007 * or if there is a single MDS, no need to split
1010 if (reqcount == 1 || count == 1) {
1011 tgt = lmv_find_target(lmv,
1012 &hur->hur_user_item[0].hui_fid);
1014 return PTR_ERR(tgt);
1015 rc = obd_iocontrol(cmd, tgt->ltd_exp, len, karg, uarg);
1017 /* split fid list to their respective MDS */
1018 for (i = 0; i < count; i++) {
1019 struct hsm_user_request *req;
1024 if (!tgt || !tgt->ltd_exp)
1027 nr = lmv_hsm_req_count(lmv, hur, tgt);
1030 if (nr == 0) /* nothing for this MDS */
1033 /* build a request with fids for this MDS */
1034 reqlen = offsetof(typeof(*hur),
1036 + hur->hur_request.hr_data_len;
1037 req = libcfs_kvzalloc(reqlen, GFP_NOFS);
1041 rc1 = lmv_hsm_req_build(lmv, hur, tgt, req);
1045 rc1 = obd_iocontrol(cmd, tgt->ltd_exp, reqlen,
1048 if (rc1 != 0 && rc == 0)
1055 case LL_IOC_LOV_SWAP_LAYOUTS: {
1056 struct md_op_data *op_data = karg;
1057 struct lmv_tgt_desc *tgt1, *tgt2;
1059 tgt1 = lmv_find_target(lmv, &op_data->op_fid1);
1061 return PTR_ERR(tgt1);
1063 tgt2 = lmv_find_target(lmv, &op_data->op_fid2);
1065 return PTR_ERR(tgt2);
1067 if (!tgt1->ltd_exp || !tgt2->ltd_exp)
1070 /* only files on same MDT can have their layouts swapped */
1071 if (tgt1->ltd_idx != tgt2->ltd_idx)
1074 rc = obd_iocontrol(cmd, tgt1->ltd_exp, len, karg, uarg);
1077 case LL_IOC_HSM_CT_START: {
1078 struct lustre_kernelcomm *lk = karg;
1080 if (lk->lk_flags & LK_FLG_STOP)
1081 rc = lmv_hsm_ct_unregister(lmv, cmd, len, lk, uarg);
1083 rc = lmv_hsm_ct_register(lmv, cmd, len, lk, uarg);
1087 for (i = 0; i < count; i++) {
1088 struct obd_device *mdc_obd;
1092 if (!tgt || !tgt->ltd_exp)
1094 /* ll_umount_begin() sets force flag but for lmv, not
1095 * mdc. Let's pass it through
1097 mdc_obd = class_exp2obd(tgt->ltd_exp);
1098 mdc_obd->obd_force = obddev->obd_force;
1099 err = obd_iocontrol(cmd, tgt->ltd_exp, len, karg, uarg);
1101 if (tgt->ltd_active) {
1102 CERROR("%s: error: iocontrol MDC %s on MDTidx %d cmd %x: err = %d\n",
1103 lmv2obd_dev(lmv)->obd_name,
1104 tgt->ltd_uuid.uuid, i, cmd, err);
1119 * This is _inode_ placement policy function (not name).
1121 static int lmv_placement_policy(struct obd_device *obd,
1122 struct md_op_data *op_data, u32 *mds)
1124 struct lmv_obd *lmv = &obd->u.lmv;
1128 if (lmv->desc.ld_tgt_count == 1) {
1133 if (op_data->op_default_stripe_offset != -1) {
1134 *mds = op_data->op_default_stripe_offset;
1139 * If stripe_offset is provided during setdirstripe
1140 * (setdirstripe -i xx), xx MDS will be chosen.
1142 if (op_data->op_cli_flags & CLI_SET_MEA && op_data->op_data) {
1143 struct lmv_user_md *lum;
1145 lum = op_data->op_data;
1146 if (le32_to_cpu(lum->lum_stripe_offset) != (__u32)-1) {
1147 *mds = le32_to_cpu(lum->lum_stripe_offset);
1150 * -1 means default, which will be in the same MDT with
1153 *mds = op_data->op_mds;
1154 lum->lum_stripe_offset = cpu_to_le32(op_data->op_mds);
1158 * Allocate new fid on target according to operation type and
1161 *mds = op_data->op_mds;
1167 int __lmv_fid_alloc(struct lmv_obd *lmv, struct lu_fid *fid, u32 mds)
1169 struct lmv_tgt_desc *tgt;
1172 tgt = lmv_get_target(lmv, mds, NULL);
1174 return PTR_ERR(tgt);
1177 * New seq alloc and FLD setup should be atomic. Otherwise we may find
1178 * on server that seq in new allocated fid is not yet known.
1180 mutex_lock(&tgt->ltd_fid_mutex);
1182 if (tgt->ltd_active == 0 || !tgt->ltd_exp) {
1188 * Asking underlaying tgt layer to allocate new fid.
1190 rc = obd_fid_alloc(NULL, tgt->ltd_exp, fid, NULL);
1192 LASSERT(fid_is_sane(fid));
1197 mutex_unlock(&tgt->ltd_fid_mutex);
1201 int lmv_fid_alloc(const struct lu_env *env, struct obd_export *exp,
1202 struct lu_fid *fid, struct md_op_data *op_data)
1204 struct obd_device *obd = class_exp2obd(exp);
1205 struct lmv_obd *lmv = &obd->u.lmv;
1212 rc = lmv_placement_policy(obd, op_data, &mds);
1214 CERROR("Can't get target for allocating fid, rc %d\n",
1219 rc = __lmv_fid_alloc(lmv, fid, mds);
1221 CERROR("Can't alloc new fid, rc %d\n", rc);
1228 static int lmv_setup(struct obd_device *obd, struct lustre_cfg *lcfg)
1230 struct lmv_obd *lmv = &obd->u.lmv;
1231 struct lprocfs_static_vars lvars = { NULL };
1232 struct lmv_desc *desc;
1235 if (LUSTRE_CFG_BUFLEN(lcfg, 1) < 1) {
1236 CERROR("LMV setup requires a descriptor\n");
1240 desc = (struct lmv_desc *)lustre_cfg_buf(lcfg, 1);
1241 if (sizeof(*desc) > LUSTRE_CFG_BUFLEN(lcfg, 1)) {
1242 CERROR("Lmv descriptor size wrong: %d > %d\n",
1243 (int)sizeof(*desc), LUSTRE_CFG_BUFLEN(lcfg, 1));
1247 lmv->tgts_size = 32U;
1248 lmv->tgts = kcalloc(lmv->tgts_size, sizeof(*lmv->tgts), GFP_NOFS);
1252 obd_str2uuid(&lmv->desc.ld_uuid, desc->ld_uuid.uuid);
1253 lmv->desc.ld_tgt_count = 0;
1254 lmv->desc.ld_active_tgt_count = 0;
1255 lmv->max_def_easize = 0;
1256 lmv->max_easize = 0;
1258 spin_lock_init(&lmv->lmv_lock);
1259 mutex_init(&lmv->lmv_init_mutex);
1261 lprocfs_lmv_init_vars(&lvars);
1263 lprocfs_obd_setup(obd, lvars.obd_vars, lvars.sysfs_vars);
1264 rc = ldebugfs_seq_create(obd->obd_debugfs_entry, "target_obd",
1265 0444, &lmv_proc_target_fops, obd);
1267 CWARN("%s: error adding LMV target_obd file: rc = %d\n",
1269 rc = fld_client_init(&lmv->lmv_fld, obd->obd_name,
1270 LUSTRE_CLI_FLD_HASH_DHT);
1272 CERROR("Can't init FLD, err %d\n", rc);
1282 static int lmv_cleanup(struct obd_device *obd)
1284 struct lmv_obd *lmv = &obd->u.lmv;
1286 fld_client_fini(&lmv->lmv_fld);
1290 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
1293 lmv_del_target(lmv, i);
1301 static int lmv_process_config(struct obd_device *obd, u32 len, void *buf)
1303 struct lustre_cfg *lcfg = buf;
1304 struct obd_uuid obd_uuid;
1309 switch (lcfg->lcfg_command) {
1311 /* modify_mdc_tgts add 0:lustre-clilmv 1:lustre-MDT0000_UUID
1312 * 2:0 3:1 4:lustre-MDT0000-mdc_UUID
1314 if (LUSTRE_CFG_BUFLEN(lcfg, 1) > sizeof(obd_uuid.uuid)) {
1319 obd_str2uuid(&obd_uuid, lustre_cfg_buf(lcfg, 1));
1321 if (sscanf(lustre_cfg_buf(lcfg, 2), "%u", &index) != 1) {
1325 if (sscanf(lustre_cfg_buf(lcfg, 3), "%d", &gen) != 1) {
1329 rc = lmv_add_target(obd, &obd_uuid, index, gen);
1332 CERROR("Unknown command: %d\n", lcfg->lcfg_command);
1340 static int lmv_statfs(const struct lu_env *env, struct obd_export *exp,
1341 struct obd_statfs *osfs, __u64 max_age, __u32 flags)
1343 struct obd_device *obd = class_exp2obd(exp);
1344 struct lmv_obd *lmv = &obd->u.lmv;
1345 struct obd_statfs *temp;
1349 temp = kzalloc(sizeof(*temp), GFP_NOFS);
1353 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
1354 if (!lmv->tgts[i] || !lmv->tgts[i]->ltd_exp)
1357 rc = obd_statfs(env, lmv->tgts[i]->ltd_exp, temp,
1360 CERROR("can't stat MDS #%d (%s), error %d\n", i,
1361 lmv->tgts[i]->ltd_exp->exp_obd->obd_name,
1368 /* If the statfs is from mount, it will needs
1369 * retrieve necessary information from MDT0.
1370 * i.e. mount does not need the merged osfs
1372 * And also clients can be mounted as long as
1373 * MDT0 is in service
1375 if (flags & OBD_STATFS_FOR_MDT0)
1378 osfs->os_bavail += temp->os_bavail;
1379 osfs->os_blocks += temp->os_blocks;
1380 osfs->os_ffree += temp->os_ffree;
1381 osfs->os_files += temp->os_files;
1390 static int lmv_getstatus(struct obd_export *exp,
1393 struct obd_device *obd = exp->exp_obd;
1394 struct lmv_obd *lmv = &obd->u.lmv;
1396 return md_getstatus(lmv->tgts[0]->ltd_exp, fid);
1399 static int lmv_getxattr(struct obd_export *exp, const struct lu_fid *fid,
1400 u64 valid, const char *name,
1401 const char *input, int input_size, int output_size,
1402 int flags, struct ptlrpc_request **request)
1404 struct obd_device *obd = exp->exp_obd;
1405 struct lmv_obd *lmv = &obd->u.lmv;
1406 struct lmv_tgt_desc *tgt;
1408 tgt = lmv_find_target(lmv, fid);
1410 return PTR_ERR(tgt);
1412 return md_getxattr(tgt->ltd_exp, fid, valid, name, input,
1413 input_size, output_size, flags, request);
1416 static int lmv_setxattr(struct obd_export *exp, const struct lu_fid *fid,
1417 u64 valid, const char *name,
1418 const char *input, int input_size, int output_size,
1419 int flags, __u32 suppgid,
1420 struct ptlrpc_request **request)
1422 struct obd_device *obd = exp->exp_obd;
1423 struct lmv_obd *lmv = &obd->u.lmv;
1424 struct lmv_tgt_desc *tgt;
1426 tgt = lmv_find_target(lmv, fid);
1428 return PTR_ERR(tgt);
1430 return md_setxattr(tgt->ltd_exp, fid, valid, name, input,
1431 input_size, output_size, flags, suppgid,
1435 static int lmv_getattr(struct obd_export *exp, struct md_op_data *op_data,
1436 struct ptlrpc_request **request)
1438 struct obd_device *obd = exp->exp_obd;
1439 struct lmv_obd *lmv = &obd->u.lmv;
1440 struct lmv_tgt_desc *tgt;
1442 tgt = lmv_find_target(lmv, &op_data->op_fid1);
1444 return PTR_ERR(tgt);
1446 if (op_data->op_flags & MF_GET_MDT_IDX) {
1447 op_data->op_mds = tgt->ltd_idx;
1451 return md_getattr(tgt->ltd_exp, op_data, request);
1454 static int lmv_null_inode(struct obd_export *exp, const struct lu_fid *fid)
1456 struct obd_device *obd = exp->exp_obd;
1457 struct lmv_obd *lmv = &obd->u.lmv;
1460 CDEBUG(D_INODE, "CBDATA for " DFID "\n", PFID(fid));
1463 * With DNE every object can have two locks in different namespaces:
1464 * lookup lock in space of MDT storing direntry and update/open lock in
1465 * space of MDT storing inode.
1467 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
1468 if (!lmv->tgts[i] || !lmv->tgts[i]->ltd_exp)
1470 md_null_inode(lmv->tgts[i]->ltd_exp, fid);
1476 static int lmv_close(struct obd_export *exp, struct md_op_data *op_data,
1477 struct md_open_data *mod, struct ptlrpc_request **request)
1479 struct obd_device *obd = exp->exp_obd;
1480 struct lmv_obd *lmv = &obd->u.lmv;
1481 struct lmv_tgt_desc *tgt;
1483 tgt = lmv_find_target(lmv, &op_data->op_fid1);
1485 return PTR_ERR(tgt);
1487 CDEBUG(D_INODE, "CLOSE " DFID "\n", PFID(&op_data->op_fid1));
1488 return md_close(tgt->ltd_exp, op_data, mod, request);
1492 * Choosing the MDT by name or FID in @op_data.
1493 * For non-striped directory, it will locate MDT by fid.
1494 * For striped-directory, it will locate MDT by name. And also
1495 * it will reset op_fid1 with the FID of the chosen stripe.
1497 static struct lmv_tgt_desc *
1498 lmv_locate_target_for_name(struct lmv_obd *lmv, struct lmv_stripe_md *lsm,
1499 const char *name, int namelen, struct lu_fid *fid,
1502 const struct lmv_oinfo *oinfo;
1503 struct lmv_tgt_desc *tgt;
1505 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_NAME_HASH)) {
1506 if (cfs_fail_val >= lsm->lsm_md_stripe_count)
1507 return ERR_PTR(-EBADF);
1508 oinfo = &lsm->lsm_md_oinfo[cfs_fail_val];
1510 oinfo = lsm_name_to_stripe_info(lsm, name, namelen);
1512 return ERR_CAST(oinfo);
1516 *fid = oinfo->lmo_fid;
1518 *mds = oinfo->lmo_mds;
1520 tgt = lmv_get_target(lmv, oinfo->lmo_mds, NULL);
1522 CDEBUG(D_INFO, "locate on mds %u " DFID "\n", oinfo->lmo_mds,
1523 PFID(&oinfo->lmo_fid));
1528 * Locate mds by fid or name
1530 * For striped directory (lsm != NULL), it will locate the stripe
1531 * by name hash (see lsm_name_to_stripe_info()). Note: if the hash_type
1532 * is unknown, it will return -EBADFD, and lmv_intent_lookup might need
1533 * walk through all of stripes to locate the entry.
1535 * For normal direcotry, it will locate MDS by FID directly.
1536 * \param[in] lmv LMV device
1537 * \param[in] op_data client MD stack parameters, name, namelen
1539 * \param[in] fid object FID used to locate MDS.
1541 * retval pointer to the lmv_tgt_desc if succeed.
1542 * ERR_PTR(errno) if failed.
1544 struct lmv_tgt_desc*
1545 lmv_locate_mds(struct lmv_obd *lmv, struct md_op_data *op_data,
1548 struct lmv_stripe_md *lsm = op_data->op_mea1;
1549 struct lmv_tgt_desc *tgt;
1552 * During creating VOLATILE file, it should honor the mdt
1553 * index if the file under striped dir is being restored, see
1556 if (op_data->op_bias & MDS_CREATE_VOLATILE &&
1557 (int)op_data->op_mds != -1) {
1560 tgt = lmv_get_target(lmv, op_data->op_mds, NULL);
1565 /* refill the right parent fid */
1566 for (i = 0; i < lsm->lsm_md_stripe_count; i++) {
1567 struct lmv_oinfo *oinfo;
1569 oinfo = &lsm->lsm_md_oinfo[i];
1570 if (oinfo->lmo_mds == op_data->op_mds) {
1571 *fid = oinfo->lmo_fid;
1576 if (i == lsm->lsm_md_stripe_count)
1577 *fid = lsm->lsm_md_oinfo[0].lmo_fid;
1583 if (!lsm || !op_data->op_namelen) {
1584 tgt = lmv_find_target(lmv, fid);
1588 op_data->op_mds = tgt->ltd_idx;
1593 return lmv_locate_target_for_name(lmv, lsm, op_data->op_name,
1594 op_data->op_namelen, fid,
1598 static int lmv_create(struct obd_export *exp, struct md_op_data *op_data,
1599 const void *data, size_t datalen, umode_t mode,
1600 uid_t uid, gid_t gid, cfs_cap_t cap_effective,
1601 __u64 rdev, struct ptlrpc_request **request)
1603 struct obd_device *obd = exp->exp_obd;
1604 struct lmv_obd *lmv = &obd->u.lmv;
1605 struct lmv_tgt_desc *tgt;
1608 if (!lmv->desc.ld_active_tgt_count)
1611 tgt = lmv_locate_mds(lmv, op_data, &op_data->op_fid1);
1613 return PTR_ERR(tgt);
1615 CDEBUG(D_INODE, "CREATE name '%.*s' on " DFID " -> mds #%x\n",
1616 (int)op_data->op_namelen, op_data->op_name,
1617 PFID(&op_data->op_fid1), op_data->op_mds);
1619 rc = lmv_fid_alloc(NULL, exp, &op_data->op_fid2, op_data);
1623 if (exp_connect_flags(exp) & OBD_CONNECT_DIR_STRIPE) {
1625 * Send the create request to the MDT where the object
1628 tgt = lmv_find_target(lmv, &op_data->op_fid2);
1630 return PTR_ERR(tgt);
1632 op_data->op_mds = tgt->ltd_idx;
1634 CDEBUG(D_CONFIG, "Server doesn't support striped dirs\n");
1637 CDEBUG(D_INODE, "CREATE obj " DFID " -> mds #%x\n",
1638 PFID(&op_data->op_fid1), op_data->op_mds);
1640 op_data->op_flags |= MF_MDC_CANCEL_FID1;
1641 rc = md_create(tgt->ltd_exp, op_data, data, datalen, mode, uid, gid,
1642 cap_effective, rdev, request);
1647 CDEBUG(D_INODE, "Created - " DFID "\n", PFID(&op_data->op_fid2));
1653 lmv_enqueue(struct obd_export *exp, struct ldlm_enqueue_info *einfo,
1654 const union ldlm_policy_data *policy,
1655 struct lookup_intent *it, struct md_op_data *op_data,
1656 struct lustre_handle *lockh, __u64 extra_lock_flags)
1658 struct obd_device *obd = exp->exp_obd;
1659 struct lmv_obd *lmv = &obd->u.lmv;
1660 struct lmv_tgt_desc *tgt;
1662 CDEBUG(D_INODE, "ENQUEUE '%s' on " DFID "\n",
1663 LL_IT2STR(it), PFID(&op_data->op_fid1));
1665 tgt = lmv_locate_mds(lmv, op_data, &op_data->op_fid1);
1667 return PTR_ERR(tgt);
1669 CDEBUG(D_INODE, "ENQUEUE '%s' on " DFID " -> mds #%u\n",
1670 LL_IT2STR(it), PFID(&op_data->op_fid1), tgt->ltd_idx);
1672 return md_enqueue(tgt->ltd_exp, einfo, policy, it, op_data, lockh,
1677 lmv_getattr_name(struct obd_export *exp, struct md_op_data *op_data,
1678 struct ptlrpc_request **preq)
1680 struct ptlrpc_request *req = NULL;
1681 struct obd_device *obd = exp->exp_obd;
1682 struct lmv_obd *lmv = &obd->u.lmv;
1683 struct lmv_tgt_desc *tgt;
1684 struct mdt_body *body;
1687 tgt = lmv_locate_mds(lmv, op_data, &op_data->op_fid1);
1689 return PTR_ERR(tgt);
1691 CDEBUG(D_INODE, "GETATTR_NAME for %*s on " DFID " -> mds #%u\n",
1692 (int)op_data->op_namelen, op_data->op_name,
1693 PFID(&op_data->op_fid1), tgt->ltd_idx);
1695 rc = md_getattr_name(tgt->ltd_exp, op_data, preq);
1699 body = req_capsule_server_get(&(*preq)->rq_pill, &RMF_MDT_BODY);
1700 if (body->mbo_valid & OBD_MD_MDS) {
1701 struct lu_fid rid = body->mbo_fid1;
1703 CDEBUG(D_INODE, "Request attrs for " DFID "\n",
1706 tgt = lmv_find_target(lmv, &rid);
1708 ptlrpc_req_finished(*preq);
1710 return PTR_ERR(tgt);
1713 op_data->op_fid1 = rid;
1714 op_data->op_valid |= OBD_MD_FLCROSSREF;
1715 op_data->op_namelen = 0;
1716 op_data->op_name = NULL;
1717 rc = md_getattr_name(tgt->ltd_exp, op_data, &req);
1718 ptlrpc_req_finished(*preq);
1725 #define md_op_data_fid(op_data, fl) \
1726 (fl == MF_MDC_CANCEL_FID1 ? &op_data->op_fid1 : \
1727 fl == MF_MDC_CANCEL_FID2 ? &op_data->op_fid2 : \
1728 fl == MF_MDC_CANCEL_FID3 ? &op_data->op_fid3 : \
1729 fl == MF_MDC_CANCEL_FID4 ? &op_data->op_fid4 : \
1732 static int lmv_early_cancel(struct obd_export *exp, struct lmv_tgt_desc *tgt,
1733 struct md_op_data *op_data, int op_tgt,
1734 enum ldlm_mode mode, int bits, int flag)
1736 struct lu_fid *fid = md_op_data_fid(op_data, flag);
1737 struct obd_device *obd = exp->exp_obd;
1738 struct lmv_obd *lmv = &obd->u.lmv;
1739 union ldlm_policy_data policy = { { 0 } };
1742 if (!fid_is_sane(fid))
1746 tgt = lmv_find_target(lmv, fid);
1748 return PTR_ERR(tgt);
1751 if (tgt->ltd_idx != op_tgt) {
1752 CDEBUG(D_INODE, "EARLY_CANCEL on " DFID "\n", PFID(fid));
1753 policy.l_inodebits.bits = bits;
1754 rc = md_cancel_unused(tgt->ltd_exp, fid, &policy,
1755 mode, LCF_ASYNC, NULL);
1758 "EARLY_CANCEL skip operation target %d on " DFID "\n",
1760 op_data->op_flags |= flag;
1768 * llite passes fid of an target inode in op_data->op_fid1 and id of directory in
1771 static int lmv_link(struct obd_export *exp, struct md_op_data *op_data,
1772 struct ptlrpc_request **request)
1774 struct obd_device *obd = exp->exp_obd;
1775 struct lmv_obd *lmv = &obd->u.lmv;
1776 struct lmv_tgt_desc *tgt;
1779 LASSERT(op_data->op_namelen != 0);
1781 CDEBUG(D_INODE, "LINK " DFID ":%*s to " DFID "\n",
1782 PFID(&op_data->op_fid2), (int)op_data->op_namelen,
1783 op_data->op_name, PFID(&op_data->op_fid1));
1785 op_data->op_fsuid = from_kuid(&init_user_ns, current_fsuid());
1786 op_data->op_fsgid = from_kgid(&init_user_ns, current_fsgid());
1787 op_data->op_cap = cfs_curproc_cap_pack();
1788 if (op_data->op_mea2) {
1789 struct lmv_stripe_md *lsm = op_data->op_mea2;
1790 const struct lmv_oinfo *oinfo;
1792 oinfo = lsm_name_to_stripe_info(lsm, op_data->op_name,
1793 op_data->op_namelen);
1795 return PTR_ERR(oinfo);
1797 op_data->op_fid2 = oinfo->lmo_fid;
1800 tgt = lmv_locate_mds(lmv, op_data, &op_data->op_fid2);
1802 return PTR_ERR(tgt);
1805 * Cancel UPDATE lock on child (fid1).
1807 op_data->op_flags |= MF_MDC_CANCEL_FID2;
1808 rc = lmv_early_cancel(exp, NULL, op_data, tgt->ltd_idx, LCK_EX,
1809 MDS_INODELOCK_UPDATE, MF_MDC_CANCEL_FID1);
1813 return md_link(tgt->ltd_exp, op_data, request);
1816 static int lmv_rename(struct obd_export *exp, struct md_op_data *op_data,
1817 const char *old, size_t oldlen,
1818 const char *new, size_t newlen,
1819 struct ptlrpc_request **request)
1821 struct obd_device *obd = exp->exp_obd;
1822 struct lmv_obd *lmv = &obd->u.lmv;
1823 struct obd_export *target_exp;
1824 struct lmv_tgt_desc *src_tgt;
1825 struct lmv_tgt_desc *tgt_tgt;
1826 struct mdt_body *body;
1829 LASSERT(oldlen != 0);
1831 CDEBUG(D_INODE, "RENAME %.*s in " DFID ":%d to %.*s in " DFID ":%d\n",
1832 (int)oldlen, old, PFID(&op_data->op_fid1),
1833 op_data->op_mea1 ? op_data->op_mea1->lsm_md_stripe_count : 0,
1834 (int)newlen, new, PFID(&op_data->op_fid2),
1835 op_data->op_mea2 ? op_data->op_mea2->lsm_md_stripe_count : 0);
1837 op_data->op_fsuid = from_kuid(&init_user_ns, current_fsuid());
1838 op_data->op_fsgid = from_kgid(&init_user_ns, current_fsgid());
1839 op_data->op_cap = cfs_curproc_cap_pack();
1841 if (op_data->op_cli_flags & CLI_MIGRATE) {
1842 LASSERTF(fid_is_sane(&op_data->op_fid3), "invalid FID " DFID "\n",
1843 PFID(&op_data->op_fid3));
1845 if (op_data->op_mea1) {
1846 struct lmv_stripe_md *lsm = op_data->op_mea1;
1847 struct lmv_tgt_desc *tmp;
1849 /* Fix the parent fid for striped dir */
1850 tmp = lmv_locate_target_for_name(lmv, lsm, old,
1855 return PTR_ERR(tmp);
1858 rc = lmv_fid_alloc(NULL, exp, &op_data->op_fid2, op_data);
1861 src_tgt = lmv_find_target(lmv, &op_data->op_fid3);
1862 if (IS_ERR(src_tgt))
1863 return PTR_ERR(src_tgt);
1865 target_exp = src_tgt->ltd_exp;
1867 if (op_data->op_mea1) {
1868 struct lmv_stripe_md *lsm = op_data->op_mea1;
1870 src_tgt = lmv_locate_target_for_name(lmv, lsm, old,
1875 src_tgt = lmv_find_target(lmv, &op_data->op_fid1);
1877 if (IS_ERR(src_tgt))
1878 return PTR_ERR(src_tgt);
1880 if (op_data->op_mea2) {
1881 struct lmv_stripe_md *lsm = op_data->op_mea2;
1883 tgt_tgt = lmv_locate_target_for_name(lmv, lsm, new,
1888 tgt_tgt = lmv_find_target(lmv, &op_data->op_fid2);
1890 if (IS_ERR(tgt_tgt))
1891 return PTR_ERR(tgt_tgt);
1893 target_exp = tgt_tgt->ltd_exp;
1897 * LOOKUP lock on src child (fid3) should also be cancelled for
1898 * src_tgt in mdc_rename.
1900 op_data->op_flags |= MF_MDC_CANCEL_FID1 | MF_MDC_CANCEL_FID3;
1903 * Cancel UPDATE locks on tgt parent (fid2), tgt_tgt is its
1906 rc = lmv_early_cancel(exp, NULL, op_data, src_tgt->ltd_idx,
1907 LCK_EX, MDS_INODELOCK_UPDATE,
1908 MF_MDC_CANCEL_FID2);
1912 * Cancel LOOKUP locks on source child (fid3) for parent tgt_tgt.
1914 if (fid_is_sane(&op_data->op_fid3)) {
1915 struct lmv_tgt_desc *tgt;
1917 tgt = lmv_find_target(lmv, &op_data->op_fid1);
1919 return PTR_ERR(tgt);
1921 /* Cancel LOOKUP lock on its parent */
1922 rc = lmv_early_cancel(exp, tgt, op_data, src_tgt->ltd_idx,
1923 LCK_EX, MDS_INODELOCK_LOOKUP,
1924 MF_MDC_CANCEL_FID3);
1928 rc = lmv_early_cancel(exp, NULL, op_data, src_tgt->ltd_idx,
1929 LCK_EX, MDS_INODELOCK_FULL,
1930 MF_MDC_CANCEL_FID3);
1937 * Cancel all the locks on tgt child (fid4).
1939 if (fid_is_sane(&op_data->op_fid4)) {
1940 struct lmv_tgt_desc *tgt;
1942 rc = lmv_early_cancel(exp, NULL, op_data, src_tgt->ltd_idx,
1943 LCK_EX, MDS_INODELOCK_FULL,
1944 MF_MDC_CANCEL_FID4);
1948 tgt = lmv_find_target(lmv, &op_data->op_fid4);
1950 return PTR_ERR(tgt);
1953 * Since the target child might be destroyed, and it might
1954 * become orphan, and we can only check orphan on the local
1955 * MDT right now, so we send rename request to the MDT where
1956 * target child is located. If target child does not exist,
1957 * then it will send the request to the target parent
1959 target_exp = tgt->ltd_exp;
1962 rc = md_rename(target_exp, op_data, old, oldlen, new, newlen, request);
1963 if (rc && rc != -EREMOTE)
1966 body = req_capsule_server_get(&(*request)->rq_pill, &RMF_MDT_BODY);
1970 /* Not cross-ref case, just get out of here. */
1971 if (likely(!(body->mbo_valid & OBD_MD_MDS)))
1974 CDEBUG(D_INODE, "%s: try rename to another MDT for " DFID "\n",
1975 exp->exp_obd->obd_name, PFID(&body->mbo_fid1));
1977 op_data->op_fid4 = body->mbo_fid1;
1978 ptlrpc_req_finished(*request);
1983 static int lmv_setattr(struct obd_export *exp, struct md_op_data *op_data,
1984 void *ea, size_t ealen, 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;
1990 CDEBUG(D_INODE, "SETATTR for " DFID ", valid 0x%x\n",
1991 PFID(&op_data->op_fid1), op_data->op_attr.ia_valid);
1993 op_data->op_flags |= MF_MDC_CANCEL_FID1;
1994 tgt = lmv_find_target(lmv, &op_data->op_fid1);
1996 return PTR_ERR(tgt);
1998 return md_setattr(tgt->ltd_exp, op_data, ea, ealen, request);
2001 static int lmv_sync(struct obd_export *exp, const struct lu_fid *fid,
2002 struct ptlrpc_request **request)
2004 struct obd_device *obd = exp->exp_obd;
2005 struct lmv_obd *lmv = &obd->u.lmv;
2006 struct lmv_tgt_desc *tgt;
2008 tgt = lmv_find_target(lmv, fid);
2010 return PTR_ERR(tgt);
2012 return md_sync(tgt->ltd_exp, fid, request);
2016 * Get current minimum entry from striped directory
2018 * This function will search the dir entry, whose hash value is the
2019 * closest(>=) to @hash_offset, from all of sub-stripes, and it is
2020 * only being called for striped directory.
2022 * \param[in] exp export of LMV
2023 * \param[in] op_data parameters transferred beween client MD stack
2024 * stripe_information will be included in this
2026 * \param[in] cb_op ldlm callback being used in enqueue in
2028 * \param[in] hash_offset the hash value, which is used to locate
2029 * minum(closet) dir entry
2030 * \param[in|out] stripe_offset the caller use this to indicate the stripe
2031 * index of last entry, so to avoid hash conflict
2032 * between stripes. It will also be used to
2033 * return the stripe index of current dir entry.
2034 * \param[in|out] entp the minum entry and it also is being used
2035 * to input the last dir entry to resolve the
2038 * \param[out] ppage the page which holds the minum entry
2040 * \retval = 0 get the entry successfully
2041 * negative errno (< 0) does not get the entry
2043 static int lmv_get_min_striped_entry(struct obd_export *exp,
2044 struct md_op_data *op_data,
2045 struct md_callback *cb_op,
2046 __u64 hash_offset, int *stripe_offset,
2047 struct lu_dirent **entp,
2048 struct page **ppage)
2050 struct lmv_stripe_md *lsm = op_data->op_mea1;
2051 struct obd_device *obd = exp->exp_obd;
2052 struct lmv_obd *lmv = &obd->u.lmv;
2053 struct lu_dirent *min_ent = NULL;
2054 struct page *min_page = NULL;
2055 struct lmv_tgt_desc *tgt;
2061 stripe_count = lsm->lsm_md_stripe_count;
2062 for (i = 0; i < stripe_count; i++) {
2063 __u64 stripe_hash = hash_offset;
2064 struct lu_dirent *ent = NULL;
2065 struct page *page = NULL;
2066 struct lu_dirpage *dp;
2068 tgt = lmv_get_target(lmv, lsm->lsm_md_oinfo[i].lmo_mds, NULL);
2075 * op_data will be shared by each stripe, so we need
2076 * reset these value for each stripe
2078 op_data->op_fid1 = lsm->lsm_md_oinfo[i].lmo_fid;
2079 op_data->op_fid2 = lsm->lsm_md_oinfo[i].lmo_fid;
2080 op_data->op_data = lsm->lsm_md_oinfo[i].lmo_root;
2082 rc = md_read_page(tgt->ltd_exp, op_data, cb_op, stripe_hash,
2087 dp = page_address(page);
2088 for (ent = lu_dirent_start(dp); ent;
2089 ent = lu_dirent_next(ent)) {
2090 /* Skip dummy entry */
2091 if (!le16_to_cpu(ent->lde_namelen))
2094 if (le64_to_cpu(ent->lde_hash) < hash_offset)
2097 if (le64_to_cpu(ent->lde_hash) == hash_offset &&
2098 (*entp == ent || i < *stripe_offset))
2101 /* skip . and .. for other stripes */
2102 if (i && (!strncmp(ent->lde_name, ".",
2103 le16_to_cpu(ent->lde_namelen)) ||
2104 !strncmp(ent->lde_name, "..",
2105 le16_to_cpu(ent->lde_namelen))))
2111 stripe_hash = le64_to_cpu(dp->ldp_hash_end);
2118 * reach the end of current stripe, go to next stripe
2120 if (stripe_hash == MDS_DIR_END_OFF)
2127 if (le64_to_cpu(min_ent->lde_hash) >
2128 le64_to_cpu(ent->lde_hash)) {
2151 *stripe_offset = min_idx;
2158 * Build dir entry page from a striped directory
2160 * This function gets one entry by @offset from a striped directory. It will
2161 * read entries from all of stripes, and choose one closest to the required
2162 * offset(&offset). A few notes
2163 * 1. skip . and .. for non-zero stripes, because there can only have one .
2164 * and .. in a directory.
2165 * 2. op_data will be shared by all of stripes, instead of allocating new
2166 * one, so need to restore before reusing.
2167 * 3. release the entry page if that is not being chosen.
2169 * \param[in] exp obd export refer to LMV
2170 * \param[in] op_data hold those MD parameters of read_entry
2171 * \param[in] cb_op ldlm callback being used in enqueue in mdc_read_entry
2172 * \param[out] ldp the entry being read
2173 * \param[out] ppage the page holding the entry. Note: because the entry
2174 * will be accessed in upper layer, so we need hold the
2175 * page until the usages of entry is finished, see
2176 * ll_dir_entry_next.
2178 * retval =0 if get entry successfully
2179 * <0 cannot get entry
2181 static int lmv_read_striped_page(struct obd_export *exp,
2182 struct md_op_data *op_data,
2183 struct md_callback *cb_op,
2184 __u64 offset, struct page **ppage)
2186 struct inode *master_inode = op_data->op_data;
2187 struct lu_fid master_fid = op_data->op_fid1;
2188 __u64 hash_offset = offset;
2190 struct page *min_ent_page = NULL;
2191 struct page *ent_page = NULL;
2192 struct lu_dirent *min_ent = NULL;
2193 struct lu_dirent *last_ent;
2194 struct lu_dirent *ent;
2195 struct lu_dirpage *dp;
2202 * Allocate a page and read entries from all of stripes and fill
2203 * the page by hash order
2205 ent_page = alloc_page(GFP_KERNEL);
2209 /* Initialize the entry page */
2210 dp = kmap(ent_page);
2211 memset(dp, 0, sizeof(*dp));
2212 dp->ldp_hash_start = cpu_to_le64(offset);
2213 ldp_flags = LDF_COLLIDE;
2216 left_bytes = PAGE_SIZE - sizeof(*dp);
2222 /* Find the minum entry from all sub-stripes */
2223 rc = lmv_get_min_striped_entry(exp, op_data, cb_op, hash_offset,
2230 * If it can not get minum entry, it means it already reaches
2231 * the end of this directory
2234 last_ent->lde_reclen = 0;
2235 hash_offset = MDS_DIR_END_OFF;
2239 ent_size = le16_to_cpu(min_ent->lde_reclen);
2242 * the last entry lde_reclen is 0, but it might not
2243 * the end of this entry of this temporay entry
2246 ent_size = lu_dirent_calc_size(
2247 le16_to_cpu(min_ent->lde_namelen),
2248 le32_to_cpu(min_ent->lde_attrs));
2249 if (ent_size > left_bytes) {
2250 last_ent->lde_reclen = cpu_to_le16(0);
2251 hash_offset = le64_to_cpu(min_ent->lde_hash);
2255 memcpy(ent, min_ent, ent_size);
2258 * Replace . with master FID and Replace .. with the parent FID
2261 if (!strncmp(ent->lde_name, ".",
2262 le16_to_cpu(ent->lde_namelen)) &&
2263 le16_to_cpu(ent->lde_namelen) == 1)
2264 fid_cpu_to_le(&ent->lde_fid, &master_fid);
2265 else if (!strncmp(ent->lde_name, "..",
2266 le16_to_cpu(ent->lde_namelen)) &&
2267 le16_to_cpu(ent->lde_namelen) == 2)
2268 fid_cpu_to_le(&ent->lde_fid, &op_data->op_fid3);
2270 left_bytes -= ent_size;
2271 ent->lde_reclen = cpu_to_le16(ent_size);
2273 ent = (void *)ent + ent_size;
2274 hash_offset = le64_to_cpu(min_ent->lde_hash);
2275 if (hash_offset == MDS_DIR_END_OFF) {
2276 last_ent->lde_reclen = 0;
2282 kunmap(min_ent_page);
2283 put_page(min_ent_page);
2287 __free_page(ent_page);
2291 ldp_flags |= LDF_EMPTY;
2292 dp->ldp_flags |= cpu_to_le32(ldp_flags);
2293 dp->ldp_hash_end = cpu_to_le64(hash_offset);
2297 * We do not want to allocate md_op_data during each
2298 * dir entry reading, so op_data will be shared by every stripe,
2299 * then we need to restore it back to original value before
2300 * return to the upper layer
2302 op_data->op_fid1 = master_fid;
2303 op_data->op_fid2 = master_fid;
2304 op_data->op_data = master_inode;
2311 static int lmv_read_page(struct obd_export *exp, struct md_op_data *op_data,
2312 struct md_callback *cb_op, __u64 offset,
2313 struct page **ppage)
2315 struct lmv_stripe_md *lsm = op_data->op_mea1;
2316 struct obd_device *obd = exp->exp_obd;
2317 struct lmv_obd *lmv = &obd->u.lmv;
2318 struct lmv_tgt_desc *tgt;
2321 return lmv_read_striped_page(exp, op_data, cb_op, offset, ppage);
2323 tgt = lmv_find_target(lmv, &op_data->op_fid1);
2325 return PTR_ERR(tgt);
2327 return md_read_page(tgt->ltd_exp, op_data, cb_op, offset, ppage);
2331 * Unlink a file/directory
2333 * Unlink a file or directory under the parent dir. The unlink request
2334 * usually will be sent to the MDT where the child is located, but if
2335 * the client does not have the child FID then request will be sent to the
2336 * MDT where the parent is located.
2338 * If the parent is a striped directory then it also needs to locate which
2339 * stripe the name of the child is located, and replace the parent FID
2340 * (@op->op_fid1) with the stripe FID. Note: if the stripe is unknown,
2341 * it will walk through all of sub-stripes until the child is being
2344 * \param[in] exp export refer to LMV
2345 * \param[in] op_data different parameters transferred beween client
2346 * MD stacks, name, namelen, FIDs etc.
2347 * op_fid1 is the parent FID, op_fid2 is the child
2349 * \param[out] request point to the request of unlink.
2351 * retval 0 if succeed
2352 * negative errno if failed.
2354 static int lmv_unlink(struct obd_export *exp, struct md_op_data *op_data,
2355 struct ptlrpc_request **request)
2357 struct lmv_stripe_md *lsm = op_data->op_mea1;
2358 struct obd_device *obd = exp->exp_obd;
2359 struct lmv_obd *lmv = &obd->u.lmv;
2360 struct lmv_tgt_desc *parent_tgt = NULL;
2361 struct lmv_tgt_desc *tgt = NULL;
2362 struct mdt_body *body;
2363 int stripe_index = 0;
2367 /* For striped dir, we need to locate the parent as well */
2369 struct lmv_tgt_desc *tmp;
2371 LASSERT(op_data->op_name && op_data->op_namelen);
2373 tmp = lmv_locate_target_for_name(lmv, lsm,
2375 op_data->op_namelen,
2380 * return -EBADFD means unknown hash type, might
2381 * need try all sub-stripe here
2383 if (IS_ERR(tmp) && PTR_ERR(tmp) != -EBADFD)
2384 return PTR_ERR(tmp);
2387 * Note: both migrating dir and unknown hash dir need to
2388 * try all of sub-stripes, so we need start search the
2389 * name from stripe 0, but migrating dir is already handled
2390 * inside lmv_locate_target_for_name(), so we only check
2391 * unknown hash type directory here
2393 if (!lmv_is_known_hash_type(lsm->lsm_md_hash_type)) {
2394 struct lmv_oinfo *oinfo;
2396 oinfo = &lsm->lsm_md_oinfo[stripe_index];
2398 op_data->op_fid1 = oinfo->lmo_fid;
2399 op_data->op_mds = oinfo->lmo_mds;
2404 /* Send unlink requests to the MDT where the child is located */
2405 if (likely(!fid_is_zero(&op_data->op_fid2)))
2406 tgt = lmv_find_target(lmv, &op_data->op_fid2);
2408 tgt = lmv_get_target(lmv, op_data->op_mds, NULL);
2410 tgt = lmv_locate_mds(lmv, op_data, &op_data->op_fid1);
2413 return PTR_ERR(tgt);
2415 op_data->op_fsuid = from_kuid(&init_user_ns, current_fsuid());
2416 op_data->op_fsgid = from_kgid(&init_user_ns, current_fsgid());
2417 op_data->op_cap = cfs_curproc_cap_pack();
2420 * If child's fid is given, cancel unused locks for it if it is from
2421 * another export than parent.
2423 * LOOKUP lock for child (fid3) should also be cancelled on parent
2424 * tgt_tgt in mdc_unlink().
2426 op_data->op_flags |= MF_MDC_CANCEL_FID1 | MF_MDC_CANCEL_FID3;
2429 * Cancel FULL locks on child (fid3).
2431 parent_tgt = lmv_find_target(lmv, &op_data->op_fid1);
2432 if (IS_ERR(parent_tgt))
2433 return PTR_ERR(parent_tgt);
2435 if (parent_tgt != tgt) {
2436 rc = lmv_early_cancel(exp, parent_tgt, op_data, tgt->ltd_idx,
2437 LCK_EX, MDS_INODELOCK_LOOKUP,
2438 MF_MDC_CANCEL_FID3);
2441 rc = lmv_early_cancel(exp, NULL, op_data, tgt->ltd_idx, LCK_EX,
2442 MDS_INODELOCK_FULL, MF_MDC_CANCEL_FID3);
2446 CDEBUG(D_INODE, "unlink with fid=" DFID "/" DFID " -> mds #%u\n",
2447 PFID(&op_data->op_fid1), PFID(&op_data->op_fid2), tgt->ltd_idx);
2449 rc = md_unlink(tgt->ltd_exp, op_data, request);
2450 if (rc != 0 && rc != -EREMOTE && rc != -ENOENT)
2453 /* Try next stripe if it is needed. */
2454 if (rc == -ENOENT && lsm && lmv_need_try_all_stripes(lsm)) {
2455 struct lmv_oinfo *oinfo;
2458 if (stripe_index >= lsm->lsm_md_stripe_count)
2461 oinfo = &lsm->lsm_md_oinfo[stripe_index];
2463 op_data->op_fid1 = oinfo->lmo_fid;
2464 op_data->op_mds = oinfo->lmo_mds;
2466 ptlrpc_req_finished(*request);
2469 goto try_next_stripe;
2472 body = req_capsule_server_get(&(*request)->rq_pill, &RMF_MDT_BODY);
2476 /* Not cross-ref case, just get out of here. */
2477 if (likely(!(body->mbo_valid & OBD_MD_MDS)))
2480 CDEBUG(D_INODE, "%s: try unlink to another MDT for " DFID "\n",
2481 exp->exp_obd->obd_name, PFID(&body->mbo_fid1));
2483 /* This is a remote object, try remote MDT, Note: it may
2484 * try more than 1 time here, Considering following case
2485 * /mnt/lustre is root on MDT0, remote1 is on MDT1
2486 * 1. Initially A does not know where remote1 is, it send
2487 * unlink RPC to MDT0, MDT0 return -EREMOTE, it will
2488 * resend unlink RPC to MDT1 (retry 1st time).
2490 * 2. During the unlink RPC in flight,
2491 * client B mv /mnt/lustre/remote1 /mnt/lustre/remote2
2492 * and create new remote1, but on MDT0
2494 * 3. MDT1 get unlink RPC(from A), then do remote lock on
2495 * /mnt/lustre, then lookup get fid of remote1, and find
2496 * it is remote dir again, and replay -EREMOTE again.
2498 * 4. Then A will resend unlink RPC to MDT0. (retry 2nd times).
2500 * In theory, it might try unlimited time here, but it should
2501 * be very rare case.
2503 op_data->op_fid2 = body->mbo_fid1;
2504 ptlrpc_req_finished(*request);
2510 static int lmv_precleanup(struct obd_device *obd)
2512 fld_client_debugfs_fini(&obd->u.lmv.lmv_fld);
2513 lprocfs_obd_cleanup(obd);
2518 * Get by key a value associated with a LMV device.
2520 * Dispatch request to lower-layer devices as needed.
2522 * \param[in] env execution environment for this thread
2523 * \param[in] exp export for the LMV device
2524 * \param[in] keylen length of key identifier
2525 * \param[in] key identifier of key to get value for
2526 * \param[in] vallen size of \a val
2527 * \param[out] val pointer to storage location for value
2529 * \retval 0 on success
2530 * \retval negative negated errno on failure
2532 static int lmv_get_info(const struct lu_env *env, struct obd_export *exp,
2533 __u32 keylen, void *key, __u32 *vallen, void *val)
2535 struct obd_device *obd;
2536 struct lmv_obd *lmv;
2539 obd = class_exp2obd(exp);
2541 CDEBUG(D_IOCTL, "Invalid client cookie %#llx\n",
2542 exp->exp_handle.h_cookie);
2547 if (keylen >= strlen("remote_flag") && !strcmp(key, "remote_flag")) {
2550 LASSERT(*vallen == sizeof(__u32));
2551 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
2552 struct lmv_tgt_desc *tgt = lmv->tgts[i];
2555 * All tgts should be connected when this gets called.
2557 if (!tgt || !tgt->ltd_exp)
2560 if (!obd_get_info(env, tgt->ltd_exp, keylen, key,
2565 } else if (KEY_IS(KEY_MAX_EASIZE) ||
2566 KEY_IS(KEY_DEFAULT_EASIZE) ||
2567 KEY_IS(KEY_CONN_DATA)) {
2569 * Forwarding this request to first MDS, it should know LOV
2572 rc = obd_get_info(env, lmv->tgts[0]->ltd_exp, keylen, key,
2574 if (!rc && KEY_IS(KEY_CONN_DATA))
2575 exp->exp_connect_data = *(struct obd_connect_data *)val;
2577 } else if (KEY_IS(KEY_TGT_COUNT)) {
2578 *((int *)val) = lmv->desc.ld_tgt_count;
2582 CDEBUG(D_IOCTL, "Invalid key\n");
2587 * Asynchronously set by key a value associated with a LMV device.
2589 * Dispatch request to lower-layer devices as needed.
2591 * \param[in] env execution environment for this thread
2592 * \param[in] exp export for the LMV device
2593 * \param[in] keylen length of key identifier
2594 * \param[in] key identifier of key to store value for
2595 * \param[in] vallen size of value to store
2596 * \param[in] val pointer to data to be stored
2597 * \param[in] set optional list of related ptlrpc requests
2599 * \retval 0 on success
2600 * \retval negative negated errno on failure
2602 static int lmv_set_info_async(const struct lu_env *env, struct obd_export *exp,
2603 u32 keylen, void *key, u32 vallen,
2604 void *val, struct ptlrpc_request_set *set)
2606 struct lmv_tgt_desc *tgt;
2607 struct obd_device *obd;
2608 struct lmv_obd *lmv;
2611 obd = class_exp2obd(exp);
2613 CDEBUG(D_IOCTL, "Invalid client cookie %#llx\n",
2614 exp->exp_handle.h_cookie);
2619 if (KEY_IS(KEY_READ_ONLY) || KEY_IS(KEY_FLUSH_CTX) ||
2620 KEY_IS(KEY_DEFAULT_EASIZE)) {
2623 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
2626 if (!tgt || !tgt->ltd_exp)
2629 err = obd_set_info_async(env, tgt->ltd_exp,
2630 keylen, key, vallen, val, set);
2641 static int lmv_unpack_md_v1(struct obd_export *exp, struct lmv_stripe_md *lsm,
2642 const struct lmv_mds_md_v1 *lmm1)
2644 struct lmv_obd *lmv = &exp->exp_obd->u.lmv;
2650 lsm->lsm_md_magic = le32_to_cpu(lmm1->lmv_magic);
2651 lsm->lsm_md_stripe_count = le32_to_cpu(lmm1->lmv_stripe_count);
2652 lsm->lsm_md_master_mdt_index = le32_to_cpu(lmm1->lmv_master_mdt_index);
2653 if (OBD_FAIL_CHECK(OBD_FAIL_UNKNOWN_LMV_STRIPE))
2654 lsm->lsm_md_hash_type = LMV_HASH_TYPE_UNKNOWN;
2656 lsm->lsm_md_hash_type = le32_to_cpu(lmm1->lmv_hash_type);
2657 lsm->lsm_md_layout_version = le32_to_cpu(lmm1->lmv_layout_version);
2658 cplen = strlcpy(lsm->lsm_md_pool_name, lmm1->lmv_pool_name,
2659 sizeof(lsm->lsm_md_pool_name));
2661 if (cplen >= sizeof(lsm->lsm_md_pool_name))
2664 CDEBUG(D_INFO, "unpack lsm count %d, master %d hash_type %d layout_version %d\n",
2665 lsm->lsm_md_stripe_count, lsm->lsm_md_master_mdt_index,
2666 lsm->lsm_md_hash_type, lsm->lsm_md_layout_version);
2668 stripe_count = le32_to_cpu(lmm1->lmv_stripe_count);
2669 for (i = 0; i < stripe_count; i++) {
2670 fid_le_to_cpu(&lsm->lsm_md_oinfo[i].lmo_fid,
2671 &lmm1->lmv_stripe_fids[i]);
2672 rc = lmv_fld_lookup(lmv, &lsm->lsm_md_oinfo[i].lmo_fid,
2673 &lsm->lsm_md_oinfo[i].lmo_mds);
2676 CDEBUG(D_INFO, "unpack fid #%d " DFID "\n", i,
2677 PFID(&lsm->lsm_md_oinfo[i].lmo_fid));
2683 static int lmv_unpackmd(struct obd_export *exp, struct lmv_stripe_md **lsmp,
2684 const union lmv_mds_md *lmm, size_t lmm_size)
2686 struct lmv_stripe_md *lsm;
2687 bool allocated = false;
2697 for (i = 0; i < lsm->lsm_md_stripe_count; i++) {
2699 * For migrating inode, the master stripe and master
2700 * object will be the same, so do not need iput, see
2703 if (!(lsm->lsm_md_hash_type & LMV_HASH_FLAG_MIGRATION &&
2704 !i) && lsm->lsm_md_oinfo[i].lmo_root)
2705 iput(lsm->lsm_md_oinfo[i].lmo_root);
2713 if (le32_to_cpu(lmm->lmv_magic) == LMV_MAGIC_STRIPE)
2717 if (le32_to_cpu(lmm->lmv_magic) != LMV_MAGIC_V1 &&
2718 le32_to_cpu(lmm->lmv_magic) != LMV_USER_MAGIC) {
2719 CERROR("%s: invalid lmv magic %x: rc = %d\n",
2720 exp->exp_obd->obd_name, le32_to_cpu(lmm->lmv_magic),
2725 if (le32_to_cpu(lmm->lmv_magic) == LMV_MAGIC_V1)
2726 lsm_size = lmv_stripe_md_size(lmv_mds_md_stripe_count_get(lmm));
2729 * Unpack default dirstripe(lmv_user_md) to lmv_stripe_md,
2730 * stripecount should be 0 then.
2732 lsm_size = lmv_stripe_md_size(0);
2735 lsm = libcfs_kvzalloc(lsm_size, GFP_NOFS);
2742 switch (le32_to_cpu(lmm->lmv_magic)) {
2744 rc = lmv_unpack_md_v1(exp, lsm, &lmm->lmv_md_v1);
2747 CERROR("%s: unrecognized magic %x\n", exp->exp_obd->obd_name,
2748 le32_to_cpu(lmm->lmv_magic));
2753 if (rc && allocated) {
2761 void lmv_free_memmd(struct lmv_stripe_md *lsm)
2763 lmv_unpackmd(NULL, &lsm, NULL, 0);
2765 EXPORT_SYMBOL(lmv_free_memmd);
2767 static int lmv_cancel_unused(struct obd_export *exp, const struct lu_fid *fid,
2768 union ldlm_policy_data *policy,
2769 enum ldlm_mode mode, enum ldlm_cancel_flags flags,
2772 struct obd_device *obd = exp->exp_obd;
2773 struct lmv_obd *lmv = &obd->u.lmv;
2780 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
2781 struct lmv_tgt_desc *tgt = lmv->tgts[i];
2783 if (!tgt || !tgt->ltd_exp || !tgt->ltd_active)
2786 err = md_cancel_unused(tgt->ltd_exp, fid, policy, mode, flags,
2794 static int lmv_set_lock_data(struct obd_export *exp,
2795 const struct lustre_handle *lockh,
2796 void *data, __u64 *bits)
2798 struct lmv_obd *lmv = &exp->exp_obd->u.lmv;
2799 struct lmv_tgt_desc *tgt = lmv->tgts[0];
2801 if (!tgt || !tgt->ltd_exp)
2804 return md_set_lock_data(tgt->ltd_exp, lockh, data, bits);
2807 static enum ldlm_mode lmv_lock_match(struct obd_export *exp, __u64 flags,
2808 const struct lu_fid *fid,
2809 enum ldlm_type type,
2810 union ldlm_policy_data *policy,
2811 enum ldlm_mode mode,
2812 struct lustre_handle *lockh)
2814 struct obd_device *obd = exp->exp_obd;
2815 struct lmv_obd *lmv = &obd->u.lmv;
2820 CDEBUG(D_INODE, "Lock match for " DFID "\n", PFID(fid));
2823 * With DNE every object can have two locks in different namespaces:
2824 * lookup lock in space of MDT storing direntry and update/open lock in
2825 * space of MDT storing inode. Try the MDT that the FID maps to first,
2826 * since this can be easily found, and only try others if that fails.
2828 for (i = 0, tgt = lmv_find_target_index(lmv, fid);
2829 i < lmv->desc.ld_tgt_count;
2830 i++, tgt = (tgt + 1) % lmv->desc.ld_tgt_count) {
2832 CDEBUG(D_HA, "%s: " DFID " is inaccessible: rc = %d\n",
2833 obd->obd_name, PFID(fid), tgt);
2837 if (!lmv->tgts[tgt] || !lmv->tgts[tgt]->ltd_exp ||
2838 !lmv->tgts[tgt]->ltd_active)
2841 rc = md_lock_match(lmv->tgts[tgt]->ltd_exp, flags, fid,
2842 type, policy, mode, lockh);
2850 static int lmv_get_lustre_md(struct obd_export *exp,
2851 struct ptlrpc_request *req,
2852 struct obd_export *dt_exp,
2853 struct obd_export *md_exp,
2854 struct lustre_md *md)
2856 struct lmv_obd *lmv = &exp->exp_obd->u.lmv;
2857 struct lmv_tgt_desc *tgt = lmv->tgts[0];
2859 if (!tgt || !tgt->ltd_exp)
2861 return md_get_lustre_md(tgt->ltd_exp, req, dt_exp, md_exp, md);
2864 static int lmv_free_lustre_md(struct obd_export *exp, struct lustre_md *md)
2866 struct obd_device *obd = exp->exp_obd;
2867 struct lmv_obd *lmv = &obd->u.lmv;
2868 struct lmv_tgt_desc *tgt = lmv->tgts[0];
2871 lmv_free_memmd(md->lmv);
2874 if (!tgt || !tgt->ltd_exp)
2876 return md_free_lustre_md(tgt->ltd_exp, md);
2879 static int lmv_set_open_replay_data(struct obd_export *exp,
2880 struct obd_client_handle *och,
2881 struct lookup_intent *it)
2883 struct obd_device *obd = exp->exp_obd;
2884 struct lmv_obd *lmv = &obd->u.lmv;
2885 struct lmv_tgt_desc *tgt;
2887 tgt = lmv_find_target(lmv, &och->och_fid);
2889 return PTR_ERR(tgt);
2891 return md_set_open_replay_data(tgt->ltd_exp, och, it);
2894 static int lmv_clear_open_replay_data(struct obd_export *exp,
2895 struct obd_client_handle *och)
2897 struct obd_device *obd = exp->exp_obd;
2898 struct lmv_obd *lmv = &obd->u.lmv;
2899 struct lmv_tgt_desc *tgt;
2901 tgt = lmv_find_target(lmv, &och->och_fid);
2903 return PTR_ERR(tgt);
2905 return md_clear_open_replay_data(tgt->ltd_exp, och);
2908 static int lmv_intent_getattr_async(struct obd_export *exp,
2909 struct md_enqueue_info *minfo)
2911 struct md_op_data *op_data = &minfo->mi_data;
2912 struct obd_device *obd = exp->exp_obd;
2913 struct lmv_obd *lmv = &obd->u.lmv;
2914 struct lmv_tgt_desc *ptgt = NULL;
2915 struct lmv_tgt_desc *ctgt = NULL;
2917 if (!fid_is_sane(&op_data->op_fid2))
2920 ptgt = lmv_locate_mds(lmv, op_data, &op_data->op_fid1);
2922 return PTR_ERR(ptgt);
2924 ctgt = lmv_locate_mds(lmv, op_data, &op_data->op_fid2);
2926 return PTR_ERR(ctgt);
2929 * if child is on remote MDT, we need 2 async RPCs to fetch both LOOKUP
2930 * lock on parent, and UPDATE lock on child MDT, which makes all
2931 * complicated. Considering remote dir is rare case, and not supporting
2932 * it in statahead won't cause any issue, drop its support for now.
2937 return md_intent_getattr_async(ptgt->ltd_exp, minfo);
2940 static int lmv_revalidate_lock(struct obd_export *exp, struct lookup_intent *it,
2941 struct lu_fid *fid, __u64 *bits)
2943 struct obd_device *obd = exp->exp_obd;
2944 struct lmv_obd *lmv = &obd->u.lmv;
2945 struct lmv_tgt_desc *tgt;
2947 tgt = lmv_find_target(lmv, fid);
2949 return PTR_ERR(tgt);
2951 return md_revalidate_lock(tgt->ltd_exp, it, fid, bits);
2955 lmv_get_fid_from_lsm(struct obd_export *exp,
2956 const struct lmv_stripe_md *lsm,
2957 const char *name, int namelen, struct lu_fid *fid)
2959 const struct lmv_oinfo *oinfo;
2962 oinfo = lsm_name_to_stripe_info(lsm, name, namelen);
2964 return PTR_ERR(oinfo);
2966 *fid = oinfo->lmo_fid;
2972 * For lmv, only need to send request to master MDT, and the master MDT will
2973 * process with other slave MDTs. The only exception is Q_GETOQUOTA for which
2974 * we directly fetch data from the slave MDTs.
2976 static int lmv_quotactl(struct obd_device *unused, struct obd_export *exp,
2977 struct obd_quotactl *oqctl)
2979 struct obd_device *obd = class_exp2obd(exp);
2980 struct lmv_obd *lmv = &obd->u.lmv;
2981 struct lmv_tgt_desc *tgt = lmv->tgts[0];
2983 __u64 curspace = 0, curinodes = 0;
2986 if (!tgt || !tgt->ltd_exp || !tgt->ltd_active ||
2987 !lmv->desc.ld_tgt_count) {
2988 CERROR("master lmv inactive\n");
2992 if (oqctl->qc_cmd != Q_GETOQUOTA)
2993 return obd_quotactl(tgt->ltd_exp, oqctl);
2995 for (i = 0; i < lmv->desc.ld_tgt_count; i++) {
3000 if (!tgt || !tgt->ltd_exp || !tgt->ltd_active)
3003 err = obd_quotactl(tgt->ltd_exp, oqctl);
3005 CERROR("getquota on mdt %d failed. %d\n", i, err);
3009 curspace += oqctl->qc_dqblk.dqb_curspace;
3010 curinodes += oqctl->qc_dqblk.dqb_curinodes;
3013 oqctl->qc_dqblk.dqb_curspace = curspace;
3014 oqctl->qc_dqblk.dqb_curinodes = curinodes;
3019 static int lmv_merge_attr(struct obd_export *exp,
3020 const struct lmv_stripe_md *lsm,
3021 struct cl_attr *attr,
3022 ldlm_blocking_callback cb_blocking)
3026 rc = lmv_revalidate_slaves(exp, lsm, cb_blocking, 0);
3030 for (i = 0; i < lsm->lsm_md_stripe_count; i++) {
3031 struct inode *inode = lsm->lsm_md_oinfo[i].lmo_root;
3033 CDEBUG(D_INFO, "" DFID " size %llu, blocks %llu nlink %u, atime %lu ctime %lu, mtime %lu.\n",
3034 PFID(&lsm->lsm_md_oinfo[i].lmo_fid),
3035 i_size_read(inode), (unsigned long long)inode->i_blocks,
3036 inode->i_nlink, LTIME_S(inode->i_atime),
3037 LTIME_S(inode->i_ctime), LTIME_S(inode->i_mtime));
3039 /* for slave stripe, it needs to subtract nlink for . and .. */
3041 attr->cat_nlink += inode->i_nlink - 2;
3043 attr->cat_nlink = inode->i_nlink;
3045 attr->cat_size += i_size_read(inode);
3046 attr->cat_blocks += inode->i_blocks;
3048 if (attr->cat_atime < LTIME_S(inode->i_atime))
3049 attr->cat_atime = LTIME_S(inode->i_atime);
3051 if (attr->cat_ctime < LTIME_S(inode->i_ctime))
3052 attr->cat_ctime = LTIME_S(inode->i_ctime);
3054 if (attr->cat_mtime < LTIME_S(inode->i_mtime))
3055 attr->cat_mtime = LTIME_S(inode->i_mtime);
3060 static struct obd_ops lmv_obd_ops = {
3061 .owner = THIS_MODULE,
3063 .cleanup = lmv_cleanup,
3064 .precleanup = lmv_precleanup,
3065 .process_config = lmv_process_config,
3066 .connect = lmv_connect,
3067 .disconnect = lmv_disconnect,
3068 .statfs = lmv_statfs,
3069 .get_info = lmv_get_info,
3070 .set_info_async = lmv_set_info_async,
3071 .notify = lmv_notify,
3072 .get_uuid = lmv_get_uuid,
3073 .iocontrol = lmv_iocontrol,
3074 .quotactl = lmv_quotactl
3077 static struct md_ops lmv_md_ops = {
3078 .getstatus = lmv_getstatus,
3079 .null_inode = lmv_null_inode,
3081 .create = lmv_create,
3082 .enqueue = lmv_enqueue,
3083 .getattr = lmv_getattr,
3084 .getxattr = lmv_getxattr,
3085 .getattr_name = lmv_getattr_name,
3086 .intent_lock = lmv_intent_lock,
3088 .rename = lmv_rename,
3089 .setattr = lmv_setattr,
3090 .setxattr = lmv_setxattr,
3092 .read_page = lmv_read_page,
3093 .unlink = lmv_unlink,
3094 .init_ea_size = lmv_init_ea_size,
3095 .cancel_unused = lmv_cancel_unused,
3096 .set_lock_data = lmv_set_lock_data,
3097 .lock_match = lmv_lock_match,
3098 .get_lustre_md = lmv_get_lustre_md,
3099 .free_lustre_md = lmv_free_lustre_md,
3100 .merge_attr = lmv_merge_attr,
3101 .set_open_replay_data = lmv_set_open_replay_data,
3102 .clear_open_replay_data = lmv_clear_open_replay_data,
3103 .intent_getattr_async = lmv_intent_getattr_async,
3104 .revalidate_lock = lmv_revalidate_lock,
3105 .get_fid_from_lsm = lmv_get_fid_from_lsm,
3106 .unpackmd = lmv_unpackmd,
3109 static int __init lmv_init(void)
3111 struct lprocfs_static_vars lvars;
3113 lprocfs_lmv_init_vars(&lvars);
3115 return class_register_type(&lmv_obd_ops, &lmv_md_ops,
3116 LUSTRE_LMV_NAME, NULL);
3119 static void lmv_exit(void)
3121 class_unregister_type(LUSTRE_LMV_NAME);
3124 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
3125 MODULE_DESCRIPTION("Lustre Logical Metadata Volume");
3126 MODULE_VERSION(LUSTRE_VERSION_STRING);
3127 MODULE_LICENSE("GPL");
3129 module_init(lmv_init);
3130 module_exit(lmv_exit);